diff --git a/cpp/test/adapter-local.test.js b/cpp/test/adapter-local.test.js index d00de27..641cf6e 100644 --- a/cpp/test/adapter-local.test.js +++ b/cpp/test/adapter-local.test.js @@ -1,61 +1,66 @@ +const R = require('ramda') const { expect } = require('chai') -const { Blockchain, expectToThrow } = require('@eosnetwork/vert') +const { Asset } = require('@wharfkit/antelope') +const { Blockchain } = require('@eosnetwork/vert') const { deploy } = require('./utils/deploy') -const R = require('ramda') const { + no0x, active, + bytes32, precision, - getAccountCodeRaw, - getSymbolCodeRaw, - getSingletonInstance, + substract, + getSwapMemo, prettyTrace, -} = require('./utils/eos-ext') -const { - getXbytesHex, hexToString, + getXbytesHex, + getEventBytes, removeNullChars, -} = require('./utils/bytes-utils') -const { getEventBytes } = require('./utils/get-event-bytes') -const { substract } = require('./utils/wharfkit-ext') -const { getAccountsBalances } = require('./utils/get-token-balance') -const errors = require('./utils/errors') -const { no0x } = require('./utils') -const ethers = require('ethers') - -const getSwapMemo = (sender, destinationChainId, recipient, data) => - `${sender},${destinationChainId},${recipient},${R.isEmpty(data) ? '0' : '1'}` + getSymbolCodeRaw, + getAccountsBalances, + getSingletonInstance, + _0x, + fromEthersPublicKey, +} = require('./utils') -const attestation = 'deadbeef' +const { toBeHex } = require('ethers') +const { + Protocols, + Chains, + ProofcastEventAttestator, + Versions, +} = require('@pnetwork/event-attestator') describe('Adapter EOS -> ETH testing', () => { + const decimals = 4 const symbol = 'TKN' - const minFee = `0.0010 X${symbol}` - const precision4 = precision(4) - const maxSupply = '500000000.0000' - const tokenMaxSupply = '500000000.0000' - const userInitialBalance = `1000.0000 ${symbol}` - const tokenBytes = no0x( - ethers.zeroPadValue( - ethers.toBeHex(getSymbolCodeRaw(`0.0000 ${symbol}`).toString()), - 32, - ), - ) + const xsymbol = `X${symbol}` + const symbolPrecision = precision(decimals, symbol) + const xsymbolPrecision = precision(decimals, xsymbol) + const minFee = Asset.from(0.0018, xsymbolPrecision) + const maxSupply = 500000000 + const userInitialBalance = Asset.from(1000, symbolPrecision) const TABLE_STORAGE = 'storage' const FEE_BASIS_POINTS = 1750 const FEE_BASIS_POINTS_DIVISOR = 1000000 + const evmOriginChainId = Chains(Protocols.Evm).Mainnet + const evmAdapter = + '000000000000000000000000bcf063a9eb18bc3c6eb005791c61801b7cb16fe4' + const evmTopicZero = + '66756e6473206172652073616675207361667520736166752073616675202e2e' + const token = { symbol: symbol, account: `${symbol.toLowerCase()}.token`, - maxSupply: `${maxSupply} ${symbol}`, - bytes: tokenBytes, + maxSupply: Asset.from(maxSupply, symbolPrecision), + bytes: no0x(bytes32(toBeHex(Number(getSymbolCodeRaw(symbolPrecision))))), contract: null, } const xerc20 = { - symbol: `X${symbol}`, - account: `x${symbol.toLowerCase()}.token`, - maxSupply: `${maxSupply} X${symbol}`, + symbol: xsymbol, + account: `${xsymbol.toLowerCase()}.token`, + maxSupply: Asset.from(maxSupply, xsymbolPrecision), contract: null, } @@ -74,6 +79,12 @@ describe('Adapter EOS -> ETH testing', () => { contract: null, } + const evmEA = new ProofcastEventAttestator({ + version: Versions.V1, + protocolId: Protocols.Evm, + chainId: Chains(Protocols.Evm).Mainnet, + }) + const blockchain = new Blockchain() const user = 'user' @@ -85,26 +96,31 @@ describe('Adapter EOS -> ETH testing', () => { before(async () => { blockchain.createAccounts(user, evil, issuer, bridge, recipient, feemanager) + lockbox.contract = deploy( blockchain, lockbox.account, 'contracts/build/lockbox', ) + token.contract = deploy( blockchain, token.account, 'contracts/build/eosio.token', ) + xerc20.contract = deploy( blockchain, xerc20.account, 'contracts/build/xerc20.token', ) + adapter.contract = deploy( blockchain, adapter.account, 'contracts/build/adapter', ) + receiver.contract = deploy( blockchain, receiver.account, @@ -125,9 +141,9 @@ describe('Adapter EOS -> ETH testing', () => { await lockbox.contract.actions .create([ xerc20.account, - precision4(xerc20.symbol), + xsymbolPrecision, token.account, - precision4(token.symbol), + symbolPrecision, ]) .send(active(lockbox.account)) @@ -158,67 +174,47 @@ describe('Adapter EOS -> ETH testing', () => { await adapter.contract.actions .create([ xerc20.account, - precision4(xerc20.symbol), + xsymbolPrecision, token.account, - precision4(token.symbol), + symbolPrecision, token.bytes, minFee, ]) .send(active(adapter.account)) + }) + it('Should set the fee manager successfully', async () => { await adapter.contract.actions .setfeemanagr([feemanager]) .send(active(adapter.account)) + }) + + it('Should add the tee public key successfully', async () => { + const teePubKey = fromEthersPublicKey( + evmEA.signingKey.compressedPublicKey, + ) - const row = adapter.contract.tables - .regadapter(getAccountCodeRaw(adapter.account)) - .getTableRow(getSymbolCodeRaw(token.maxSupply)) - - const storage = getSingletonInstance(adapter.contract, TABLE_STORAGE) - - expect(row).to.be.deep.equal({ - token: token.account, - token_symbol: precision4(token.symbol), - token_bytes: token.bytes, - xerc20: xerc20.account, - xerc20_symbol: precision4(xerc20.symbol), - min_fee: minFee, - }) - - expect(row).to.be.deep.equal({ - token: token.account, - token_symbol: precision4(token.symbol), - token_bytes: token.bytes, - xerc20: xerc20.account, - xerc20_symbol: precision4(xerc20.symbol), - min_fee: minFee, - }) - - expect(storage).be.deep.equal({ - nonce: 0, - feesmanager: feemanager, - }) + const attestation = '' + await adapter.contract.actions + .settee([teePubKey, attestation]) + .send(active(adapter.account)) }) - }) - describe('adapter::swap', () => { - it('Should revert when calling the swap function directly', async () => { - const nonce = 3 - const eventBytes = '00000666' - const action = adapter.contract.actions - .swap([nonce, eventBytes]) - .send(active(evil)) - - await expectToThrow(action, errors.AUTH_MISSING(adapter.account)) + it('Should add the origin details successfully', async () => { + await adapter.contract.actions + .setorigin([no0x(bytes32(evmOriginChainId)), evmAdapter, evmTopicZero]) + .send(active(adapter.account)) }) + }) + describe('adapter::swap', () => { it('Should swap correctly', async () => { const data = '' const recipient = '0x68bbed6a47194eff1cf514b50ea91895597fc91e' - const destinationChainId = ethers.zeroPadValue('0x01', 32) + const destinationChainId = bytes32(Chains(Protocols.Evm).Mainnet) const memo = getSwapMemo(user, destinationChainId, recipient, data) - const amount = '10.0000' - const quantity = `${amount} ${symbol}` + const amount = 10 + const quantity = Asset.from(amount, symbolPrecision) const before = getAccountsBalances( [user, lockbox.account, adapter.account, feemanager], @@ -239,39 +235,26 @@ describe('Adapter EOS -> ETH testing', () => { after.storage = getSingletonInstance(adapter.contract, TABLE_STORAGE) expect( - substract( - before.user[token.symbol], - after.user[token.symbol], - ).toString(), - ).to.be.equal(quantity) + substract(before.user[token.symbol], after.user[token.symbol]), + ).to.be.deep.equal(quantity) expect( - substract( - after.lockbox[token.symbol], - before.lockbox[token.symbol], - ).toString(), - ).to.be.equal(quantity) + substract(after.lockbox[token.symbol], before.lockbox[token.symbol]), + ).to.be.deep.equal(quantity) expect( - substract( - after.lockbox[xerc20.symbol], - before.lockbox[xerc20.symbol], - ).toString(), - ).to.be.equal(`0.0000 ${xerc20.symbol}`) + substract(after.lockbox[xerc20.symbol], before.lockbox[xerc20.symbol]), + ).to.be.deep.equal(Asset.from(0, xsymbolPrecision)) - const intFees = ( - (parseInt(amount) * FEE_BASIS_POINTS) / - FEE_BASIS_POINTS_DIVISOR - ).toFixed(4) - - const fees = `${intFees} ${xerc20.symbol}` + const intFees = (amount * FEE_BASIS_POINTS) / FEE_BASIS_POINTS_DIVISOR + const fees = Asset.from(intFees, xsymbolPrecision) expect( substract( after.feemanager[xerc20.symbol], before.feemanager[xerc20.symbol], - ).toString(), - ).to.be.equal(fees) + ), + ).to.be.deep.equal(fees) expect(after.storage.nonce).to.be.equal(before.storage.nonce + 1) @@ -283,7 +266,7 @@ describe('Adapter EOS -> ETH testing', () => { expect(possibleSwap['First Receiver']).to.be.equal(adapter.account) expect(possibleSwap['Sender']).to.be.equal(adapter.account) - const eventBytes = getEventBytes(adapter.contract) + const eventBytes = adapter.contract.bc.console const expectedEventBytes = '00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000746b6e2e746f6b656e00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000008a88f6dc465640000000000000000000000000000000000000000000000000000000000075736572000000000000000000000000000000000000000000000000000000000000002a307836386262656436613437313934656666316366353134623530656139313839353539376663393165' expect(eventBytes).to.be.equal(expectedEventBytes) @@ -323,134 +306,4 @@ describe('Adapter EOS -> ETH testing', () => { // succession once the above is fixed // it('Should swap with userdata', async () => {}) }) - - // describe('adapter::settle', () => { - // it('Should settle the operation properly and send userdata', async () => { - // const quantity = `10.000000 TKN` - // const normalizedAmount = ethers - // .parseUnits(Asset.from(quantity).units.toString(), 18) - // .toString() - - // const operation = getOperationSample({ - // amount: normalizedAmount, - // }) - - // const metadata = getMetadataSample() - - // const before = getAccountsBalances( - // [user, recipient, lockbox.account, adapter.account], - // [token, xerc20], - // ) - - // const compressed = Uint8Array.from(, 16 - // Buffer.from( - // '0380472f799469d9af8790307a022802785c2b1e2f9c0930bdf9bafe193245e7a3', - // 'hex', - // ), - // ) - // const pubKey = PublicKey.from({ type: 'K1', compressed }) - // await adapter.contract.actions - // .settee([pubKey, attestation]) - // .send(active(adapter.account)) - - // const normalizedOriginChainId = hexStringToBytes('0000000000000000000000000000000000000000000000000000000000000001') - // const normalizedOriginAdapter = hexStringToBytes('000000000000000000000000cc9676b9bf25ce45a3a5f88205239afddecf1bc7') - // const normalizeTopicZero = hexStringToBytes('9b706941b48091a1c675b439064f40b9d43c577d9c7134cce93179b9b0bf2a52') - - // await adapter.contract.actions - // .setemitter([normalizedOriginChainId, normalizedOriginAdapter]) - // .send(active(adapter.account)) - - // await adapter.contract.actions - // .settopiczero([normalizedOriginChainId, normalizeTopicZero]) - // .send(active(adapter.account)) - - // await adapter.contract.actions - // .settle([user, operation, metadata]) - // .send(active(user)) - - // const after = getAccountsBalances( - // [user, recipient, lockbox.account, adapter.account], - // [token, xerc20], - // ) - - // console.log(before) - - // console.log(adapter.contract.bc.console) - - // expect( - // substract( - // after[recipient][token.symbol], - // before[recipient][token.symbol], - // ).toString(), - // ).to.be.equal(quantity) - - // expect( - // substract( - // after[recipient][xerc20.symbol], - // before[recipient][xerc20.symbol], - // ).toString(), - // ).to.be.equal(quantity) - - // expect( - // substract( - // before[lockbox.account][token.symbol], - // after[lockbox.account][token.symbol], - // ).toString(), - // ).to.be.equal(quantity) - - // expect(after[lockbox.account][xerc20.symbol]).to.be.equal( - // `0.0000 ${xerc20.symbol}`, - // ) - - // expect(after[adapter.account][token.symbol]).to.be.equal( - // `0.0000 ${token.symbol}`, - // ) - // expect(after[adapter.account][xerc20.symbol]).to.be.equal( - // `0.0000 ${xerc20.symbol}`, - // ) - // }) - - // it('Should send userdata to a receiver contract', async () => { - // const quantity = `1.0000 ${token.symbol}` - // const normalizedAmount = ethers - // .parseUnits(Asset.from(quantity).units.toString(), 18) - // .toString() - - // const metadata = getMetadataSample() - // const operation = getOperationSample({ - // amount: normalizedAmount, - // data: 'c0ffeec0ffeec0ffee', - // recipient: receiver.account, - // }) - - // const before = getAccountsBalances([receiver.account], [token, xerc20]) - - // // Fill in some tokens as collateral - // await token.contract.actions - // .transfer([user, lockbox.account, quantity, '']) - // .send(active(user)) - - // await adapter.contract.actions - // .settle([user, operation, metadata]) - // .send(active(user)) - - // const after = getAccountsBalances([receiver.account], [token, xerc20]) - // const receiverResults = receiver.contract.tables - // .results(getAccountCodeRaw(receiver.account)) - // .getTableRow(0n) - - // expect( - // substract( - // after[receiver.account][token.symbol], - // before[receiver.account][token.symbol], - // ).toString(), - // ).to.be.equal(quantity) - - // expect(receiverResults).to.be.deep.equal({ - // id: 0, - // data: operation.data, - // }) - // }) - // }) })