From fe7f9cd9c75a99d297a3dcf39e50ae8651a97625 Mon Sep 17 00:00:00 2001 From: envin Date: Fri, 8 Nov 2024 18:52:03 +0100 Subject: [PATCH 1/4] feat(adapter_registry_table): retain precision for non local token --- cpp/contracts/adapter.cpp | 2 +- cpp/contracts/tables/adapter_registry.table.hpp | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/cpp/contracts/adapter.cpp b/cpp/contracts/adapter.cpp index a6e45e3..e3168b9 100644 --- a/cpp/contracts/adapter.cpp +++ b/cpp/contracts/adapter.cpp @@ -53,7 +53,7 @@ void adapter::create( check(is_account(token), "token account does not exist"); } - symbol non_local_token_symbol = symbol(symbol_code("XXX"), 0); + symbol non_local_token_symbol = symbol(symbol_code("XXX"), token_symbol.precision()); adapter_registry_table registry_data { .token = token, diff --git a/cpp/contracts/tables/adapter_registry.table.hpp b/cpp/contracts/tables/adapter_registry.table.hpp index 45d0204..df1cb5a 100644 --- a/cpp/contracts/tables/adapter_registry.table.hpp +++ b/cpp/contracts/tables/adapter_registry.table.hpp @@ -23,8 +23,9 @@ namespace eosio { // WETH ERC20 => local chain is Ethereum, then adapter's registry is // // |_____token____|___token_symbol___|______token_bytes_______|_____xerc20_____| - // | '' | '0,XXX' | bytes32(address(WETH)) | 'xweth.token' | - // NOTE: for not local token_symbol = '0,XXX' + // | '' | '18,XXX' | bytes32(address(WETH)) | 'xweth.token' | + // + // NOTE: for not local token_symbol = ',XXX' TABLE adapter_registry_table { name token; From 8c266dbc6b41bddab9e55abd70c69a97898c125d Mon Sep 17 00:00:00 2001 From: envin Date: Fri, 8 Nov 2024 18:52:36 +0100 Subject: [PATCH 2/4] feat(operation): use uint_128 amount --- cpp/contracts/adapter.cpp | 10 ++-------- cpp/contracts/operation.hpp | 2 +- cpp/contracts/pam.hpp | 2 +- cpp/contracts/tables/adapter_registry.table.hpp | 2 +- cpp/contracts/utils.hpp | 10 +++++----- 5 files changed, 10 insertions(+), 16 deletions(-) diff --git a/cpp/contracts/adapter.cpp b/cpp/contracts/adapter.cpp index e3168b9..52cd9b7 100644 --- a/cpp/contracts/adapter.cpp +++ b/cpp/contracts/adapter.cpp @@ -188,10 +188,6 @@ void adapter::settle(const name& caller, const operation& operation, const metad check(_registry.exists(), "contract not inizialized"); adapter_registry_table registry_data = _registry.get(); check(registry_data.token_bytes == operation.token, "underlying token does not match with adapter registry"); - // symbol check if underlying is EOS type - if (registry_data.token != name(0)) // TODO add test - check(registry_data.xerc20_symbol == operation.amount.symbol, "registered xerc20 symbols differs from the operation one"); - checksum256 event_id; // output pam::check_authorization(get_self(), operation, metadata, event_id); @@ -214,10 +210,8 @@ void adapter::settle(const name& caller, const operation& operation, const metad name xerc20 = registry_data.xerc20; check(is_account(xerc20), "Not valid xerc20 name"); - if (operation.amount.amount > 0) { - asset adj_operation_amount = (registry_data.token == name(0) && registry_data.token_symbol == symbol(symbol_code("XXX"), 0)) ? - adjust_precision(operation.amount, registry_data.xerc20_symbol) : - operation.amount; + if (operation.amount > 0) { + asset adj_operation_amount = adjust_precision(operation.amount, registry_data.token_symbol, registry_data.xerc20_symbol); asset quantity(adj_operation_amount.amount, registry_data.xerc20_symbol); lockbox_singleton _lockbox(xerc20, xerc20.value); action_mint _mint(registry_data.xerc20, {get_self(), "active"_n}); diff --git a/cpp/contracts/operation.hpp b/cpp/contracts/operation.hpp index 0b25643..cbe8bb2 100644 --- a/cpp/contracts/operation.hpp +++ b/cpp/contracts/operation.hpp @@ -14,7 +14,7 @@ namespace eosio { checksum256 token; // erc20 on EVM bytes originChainId; bytes destinationChainId; - asset amount; + uint128_t amount; bytes sender; name recipient; bytes data; diff --git a/cpp/contracts/pam.hpp b/cpp/contracts/pam.hpp index 156d182..67f202d 100644 --- a/cpp/contracts/pam.hpp +++ b/cpp/contracts/pam.hpp @@ -148,7 +148,7 @@ namespace eosio { bytes amount = extract_32bytes(event_data, offset); uint128_t amount_num = bytes32_to_uint128(amount); - check(to_wei(operation.amount) == amount_num, "amount do not match"); + check(operation.amount == amount_num, "amount do not match"); offset += 32; bytes sender = extract_32bytes(event_data, offset); diff --git a/cpp/contracts/tables/adapter_registry.table.hpp b/cpp/contracts/tables/adapter_registry.table.hpp index df1cb5a..394b867 100644 --- a/cpp/contracts/tables/adapter_registry.table.hpp +++ b/cpp/contracts/tables/adapter_registry.table.hpp @@ -25,7 +25,7 @@ namespace eosio { // |_____token____|___token_symbol___|______token_bytes_______|_____xerc20_____| // | '' | '18,XXX' | bytes32(address(WETH)) | 'xweth.token' | // - // NOTE: for not local token_symbol = ',XXX' + // NOTE: for not local token_symbol = ',XXX' TABLE adapter_registry_table { name token; diff --git a/cpp/contracts/utils.hpp b/cpp/contracts/utils.hpp index 05a359c..872d7bf 100644 --- a/cpp/contracts/utils.hpp +++ b/cpp/contracts/utils.hpp @@ -131,20 +131,20 @@ namespace eosio { return asset(amount / powint(10, exp), sym); } - asset adjust_precision(const asset& from_asset, const symbol& to_sym) { - int16_t exp = from_asset.symbol.precision() - to_sym.precision(); + asset adjust_precision(uint128_t amount, const symbol& from_symbol, const symbol& to_symbol) { + int16_t exp = from_symbol.precision() - to_symbol.precision(); uint128_t factor; uint128_t adjusted_amount; if (exp < 0) { exp = -exp; factor = powint(10, exp); - adjusted_amount = from_asset.amount * factor; + adjusted_amount = amount * factor; } else { print("WARNING: Operation precision exceeds destination symbol; amount will be floored to destination symbol precision."); factor = powint(10, exp); - adjusted_amount = from_asset.amount / factor; + adjusted_amount = amount / factor; } - return asset(adjusted_amount, to_sym); + return asset(adjusted_amount, to_symbol); } bytes extract_32bytes(const bytes& data, uint128_t offset) { From fecc26c25d26e73aed2df5b5bf14ad901f994436 Mon Sep 17 00:00:00 2001 From: envin Date: Fri, 8 Nov 2024 18:52:53 +0100 Subject: [PATCH 3/4] test: update tests --- cpp/test/adapter-non-local.test.js | 24 +++++++++++++++++++----- cpp/test/pam.test.js | 4 ++-- cpp/test/samples/evm-operations.js | 6 +++--- cpp/test/utils/get-operation-sample.js | 2 +- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/cpp/test/adapter-non-local.test.js b/cpp/test/adapter-non-local.test.js index 4510538..23daaea 100644 --- a/cpp/test/adapter-non-local.test.js +++ b/cpp/test/adapter-non-local.test.js @@ -22,7 +22,7 @@ const { evmAdapter, } = require('./samples/evm-operations') const { evmMetadataSamples, teePubKey } = require('./samples/evm-metadata') -const { adjustPrecision } = require('./utils/precision-utils') +const { adjustPrecision, fromWei } = require('./utils/precision-utils') const attestation = 'deadbeef' @@ -318,7 +318,7 @@ describe('Adapter EVM -> EOS testing', () => { expect(row).to.be.deep.equal({ token: '', - token_symbol: precision(0, 'XXX'), + token_symbol: precision(18, 'XXX'), token_bytes: evmUnderlyingToken.bytes, xerc20: evmXERC20.account, xerc20_symbol: precision(evmXERC20.precision, evmXERC20.symbol), @@ -517,6 +517,12 @@ describe('Adapter EVM -> EOS testing', () => { }) describe('adapter::settle', () => { + const createOperationAsset = amount => + Asset.from( + amount / 10 ** evmPrecision, + precision(evmXERC20.precision, evmXERC20.symbol), + ) + it('Should reject if adapter and token do not match', async () => { const operation = evmOperationSamples.pegin const metadata = evmMetadataSamples.pegin @@ -546,7 +552,10 @@ describe('Adapter EVM -> EOS testing', () => { [evmXERC20], ) - expect(after[recipient][evmXERC20.symbol]).to.be.equal(operation.amount) + const operationAsset = createOperationAsset(operation.amount) + expect(after[recipient][evmXERC20.symbol]).to.be.equal( + operationAsset.toString(), + ) expect(after[adapter.account][evmXERC20.symbol]).to.be.equal( `0.0000 ${evmXERC20.symbol}`, @@ -576,8 +585,9 @@ describe('Adapter EVM -> EOS testing', () => { [evmXERC20], ) + const operationAsset = createOperationAsset(operation.amount) expect(after[recipient][evmXERC20.symbol]).to.equal( - sum(operation.amount, beforeAsset).toString(), + sum(operationAsset, beforeAsset).toString(), ) expect(after[adapter.account][evmXERC20.symbol]).to.be.equal( @@ -606,7 +616,11 @@ describe('Adapter EVM -> EOS testing', () => { [evmXERC20], ) - const adjAmount = adjustPrecision(operation.amount, evmXERC20Precision) + const operationAsset = createOperationAsset(operation.amount) + const adjAmount = adjustPrecision( + operationAsset.toString(), + evmXERC20Precision, + ) const adjOperationAmount = Asset.from(`${adjAmount} ${evmXERC20.symbol}`) expect(after[recipient][evmXERC20.symbol]).to.equal( sum(adjOperationAmount, beforeAsset).toString(), diff --git a/cpp/test/pam.test.js b/cpp/test/pam.test.js index 0c31516..bedfc9e 100644 --- a/cpp/test/pam.test.js +++ b/cpp/test/pam.test.js @@ -59,7 +59,7 @@ describe('PAM testing', () => { const eosChainId = 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906' const operation = getOperationSample({ - amount: '1337.0000 TKN', + amount: '1337000000000000000000', sender: '0000000000000000000000002b5ad5c4795c026514f8317c7a215e218dccd6cf', token: '000000000000000000000000f2e246bb76df876cef8b38ae84130f4f55de395b', chainId: eosChainId, @@ -327,7 +327,7 @@ describe('PAM testing', () => { '0000000000000000000000000000000000000000000000746b6e2e746f6b656e' const originChainId = no0x(Chains(Protocols.Eos).Jungle) const destinationChainId = eosChainId - const amount = '9.9825 TKN' + const amount = '9982500000000000000' const sender = '0000000000000000000000000000000000000000000000000000000075736572' const recipient = 'recipient' diff --git a/cpp/test/samples/evm-operations.js b/cpp/test/samples/evm-operations.js index 5b3c6bb..99036cd 100644 --- a/cpp/test/samples/evm-operations.js +++ b/cpp/test/samples/evm-operations.js @@ -16,7 +16,7 @@ const evmOperationSamples = { '0000000000000000000000000000000000000000000000000000000000000001', // EVM mainnet chain id destinationChainId: 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906', // EOS chain id - amount: '5.87190615 XTST', + amount: '5871906150000000000', sender: '000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266', recipient: 'eosrecipient', data: '', @@ -30,7 +30,7 @@ const evmOperationSamples = { '0000000000000000000000000000000000000000000000000000000000000001', // ETH chain id destinationChainId: 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906', // EOS chain id - amount: '0.99749956 XTST', + amount: '997499560000000000', sender: '000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266', recipient: 'eosrecipient', data: '12345abcdefc0de1337f', @@ -44,7 +44,7 @@ const evmOperationSamples = { '0000000000000000000000000000000000000000000000000000000000000001', // ETH chain id destinationChainId: 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906', // EOS chain id - amount: '1.189215224969292133 XTST', + amount: '1189215224969292133', sender: '000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266', recipient: 'eosrecipient', data: '', diff --git a/cpp/test/utils/get-operation-sample.js b/cpp/test/utils/get-operation-sample.js index a135ce5..05fc0f5 100644 --- a/cpp/test/utils/get-operation-sample.js +++ b/cpp/test/utils/get-operation-sample.js @@ -13,7 +13,7 @@ const getOperationSample = _injectedOperation => '0000000000000000000000000000000000000000000000000000000000000001', // ETH chain id destinationChainId: 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906', // EOS chain id - amount: '5.889675 XTKN', + amount: '5889675000000000000', sender: '0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266', recipient: 'destinatieos', From 27c83dfb247c4eeb6a423cfa3c1b042dc4908d50 Mon Sep 17 00:00:00 2001 From: envin Date: Fri, 8 Nov 2024 22:57:06 +0100 Subject: [PATCH 4/4] chore: fix prettier path config --- cpp/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/package.json b/cpp/package.json index 1e5a84e..12f844b 100644 --- a/cpp/package.json +++ b/cpp/package.json @@ -12,7 +12,7 @@ "test": "yarn build && mocha", "lint": "./lint scripts/*.sh && npx prettier --check test/", "prettier:fix": "npx prettier --check --write test/", - "prettier": "npx prettier --cache --check --ignore-path ../.prettierignore --config ../.prettierrc ./src ./test" + "prettier": "npx prettier --cache --check --ignore-path ../.prettierignore --config ../.prettierrc ./contracts ./test" }, "devDependencies": { "@eosnetwork/vert": "^1.0.0",