Skip to content

Commit

Permalink
Merge pull request #62 from pnetwork-association/feat/check-precision
Browse files Browse the repository at this point in the history
Feat/check precision
  • Loading branch information
envin3 authored Nov 8, 2024
2 parents eb0b68e + 1b0a170 commit acb0c3d
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 21 deletions.
12 changes: 5 additions & 7 deletions cpp/contracts/adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -187,9 +187,7 @@ void adapter::settle(const name& caller, const operation& operation, const metad
registry_adapter _registry(get_self(), get_self().value);
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");
check(registry_data.xerc20_symbol == operation.amount.symbol, "registered xerc20 symbols differs from the operation one"); // TODO: test me

check(registry_data.token_bytes == operation.token, "underlying token does not match with adapter registry");
checksum256 event_id; // output
pam::check_authorization(get_self(), operation, metadata, event_id);

Expand All @@ -212,9 +210,9 @@ 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 quantity(operation.amount.amount, registry_data.xerc20_symbol);

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});
if (_lockbox.exists()) {
Expand Down
2 changes: 1 addition & 1 deletion cpp/contracts/operation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion cpp/contracts/pam.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions cpp/contracts/tables/adapter_registry.table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<origin-chain-precision>,XXX'

TABLE adapter_registry_table {
name token;
Expand Down
16 changes: 16 additions & 0 deletions cpp/contracts/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,22 @@ namespace eosio {
return asset(amount / powint(10, exp), sym);
}

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 = amount * factor;
} else {
print("WARNING: Operation precision exceeds destination symbol; amount will be floored to destination symbol precision.");
factor = powint(10, exp);
adjusted_amount = amount / factor;
}
return asset(adjusted_amount, to_symbol);
}

bytes extract_32bytes(const bytes& data, uint128_t offset) {
bytes _data(data.begin() + offset, data.begin() + offset + 32);
return _data;
Expand Down
2 changes: 1 addition & 1 deletion cpp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
54 changes: 50 additions & 4 deletions cpp/test/adapter-non-local.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}`,
Expand Down Expand Up @@ -576,8 +585,45 @@ describe('Adapter EVM -> EOS testing', () => {
[evmXERC20],
)

const operationAsset = createOperationAsset(operation.amount)
expect(after[recipient][evmXERC20.symbol]).to.equal(
sum(operationAsset, beforeAsset).toString(),
)

expect(after[adapter.account][evmXERC20.symbol]).to.be.equal(
`0.0000 ${evmXERC20.symbol}`,
)
})

it('Should truncate the passed amount to the set precision', async () => {
const operation = evmOperationSamples.peginLargePrecision
const metadata = evmMetadataSamples.peginLargePrecision

const before = getAccountsBalances(
[user, recipient, adapter.account],
[evmXERC20],
)
const beforeAsset = Asset.from(before[recipient][evmXERC20.symbol])

await adapter.contract.actions
.settle([user, operation, metadata])
.send(active(user))

console.log(adapter.contract.bc.console)

const after = getAccountsBalances(
[user, recipient, adapter.account],
[evmXERC20],
)

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(operation.amount, beforeAsset).toString(),
sum(adjOperationAmount, beforeAsset).toString(),
)

expect(after[adapter.account][evmXERC20.symbol]).to.be.equal(
Expand Down
4 changes: 2 additions & 2 deletions cpp/test/pam.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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'
Expand Down
6 changes: 6 additions & 0 deletions cpp/test/samples/evm-metadata.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions cpp/test/samples/evm-operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: '',
Expand All @@ -30,11 +30,25 @@ const evmOperationSamples = {
'0000000000000000000000000000000000000000000000000000000000000001', // ETH chain id
destinationChainId:
'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906', // EOS chain id
amount: '0.99749956 XTST',
amount: '997499560000000000',
sender: '000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266',
recipient: 'eosrecipient',
data: '12345abcdefc0de1337f',
},
peginLargePrecision: {
blockId: '9c6d1358f426fe23fc7cf0e67aa422ff27c4e5ec7899297a10f036e6cf6643da',
txId: '96d4d6885f072cb3f734d1b4add1d78d46487692cf9d912a6a91cfc6c65bc7d6',
nonce: 0,
token: '000000000000000000000000810090f35dfa6b18b5eb59d298e2a2443a2811e2',
originChainId:
'0000000000000000000000000000000000000000000000000000000000000001', // ETH chain id
destinationChainId:
'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906', // EOS chain id
amount: '1189215224969292133',
sender: '000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266',
recipient: 'eosrecipient',
data: '',
},
}

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion cpp/test/utils/get-operation-sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit acb0c3d

Please sign in to comment.