From f4c5e509ba694190d215d09fac31e0fad6bda0b5 Mon Sep 17 00:00:00 2001 From: viraj124 Date: Tue, 21 Jan 2025 23:47:17 +0530 Subject: [PATCH] fix: remove use of Coin to fund when message coin is used --- .../fork-tests/bridge_erc20.ts | 53 ++++++++++-------- .../integration-tests/tests/bridge_erc20.ts | 50 +++++++++-------- .../tests/bridge_mainnet_tokens.ts | 56 +++++++++++-------- 3 files changed, 90 insertions(+), 69 deletions(-) diff --git a/packages/integration-tests/fork-tests/bridge_erc20.ts b/packages/integration-tests/fork-tests/bridge_erc20.ts index abbc0d12..d913e0e5 100644 --- a/packages/integration-tests/fork-tests/bridge_erc20.ts +++ b/packages/integration-tests/fork-tests/bridge_erc20.ts @@ -29,6 +29,7 @@ import { Address, BN } from 'fuels'; import type { AbstractAddress, WalletUnlocked as FuelWallet, + MessageCoin, MessageProof, Provider, ScriptTransactionRequest, @@ -83,33 +84,48 @@ describe('Bridging ERC20 tokens', async function () { }, }); - const cost = await tx.getTransactionCost(); + // verify the incoming messages generated when base asset is minted on fuel + let incomingMessagesonFuel = await env.fuel.signers[0].getMessages(); - // message coin resource - const resources = await fuelTokenSender.getResourcesToSpend([ - [fuels_parseEther('1'), env.fuel.provider.getBaseAssetId()], - ]); + expect(incomingMessagesonFuel.messages.length === 1); + expect( + incomingMessagesonFuel.messages[0].amount === fuels_parseEther('1') + ); - expect(resources.length === 1); + // construct message coin + const messageCoin: MessageCoin = { + assetId: env.fuel.provider.getBaseAssetId(), + sender: incomingMessagesonFuel.messages[0].sender, + recipient: incomingMessagesonFuel.messages[0].recipient, + nonce: incomingMessagesonFuel.messages[0].nonce, + daHeight: incomingMessagesonFuel.messages[0].daHeight, + amount: incomingMessagesonFuel.messages[0].amount, + }; - const messageCoin: any = resources[0]; + const transactionRequest = await tx.getTransactionRequest(); - // making sure resource is of Message Coin type and DA Height is non zero - expect(messageCoin.daHeight > new BN(0)); + // add message coin as input to fund the tx + transactionRequest.addMessageInput(messageCoin); - const transactionRequest = await tx.getTransactionRequest(); + // add the erc20 token input which will be burnt on withdrawal + const resource = await fuelTokenSender.getResourcesToSpend([ + [ + new BN(NUM_TOKENS.toString()).div(new BN(DECIMAL_DIFF.toString())), + fuel_testAssetId, + ], + ]); - // add message coin input - await transactionRequest.addResources(resources); + transactionRequest.addResources(resource); + + // fetch tx cost + const cost = await fuelTokenSender.getTransactionCost(transactionRequest); // update fee params transactionRequest.gasLimit = cost.gasUsed; transactionRequest.maxFee = cost.maxFee; - await fuelTokenSender.fund(transactionRequest, cost); - // verify that the message coin is consumed - const incomingMessagesonFuel = await env.fuel.signers[0].getMessages(); + incomingMessagesonFuel = await fuelTokenSender.getMessages(); expect(incomingMessagesonFuel.messages.length === 0); return transactionRequest; @@ -407,13 +423,6 @@ describe('Bridging ERC20 tokens', async function () { FUEL_MESSAGE_TIMEOUT_MS ) ).to.not.be.null; - - // verify the incoming messages generated when base asset is minted on fuel - const incomingMessagesonFuel = await env.fuel.signers[0].getMessages(); - expect(incomingMessagesonFuel.messages.length === 1); - expect( - incomingMessagesonFuel.messages[0].amount === fuels_parseEther('1') - ); }); it('Bridge ERC20 via FuelERC20Gateway', async () => { diff --git a/packages/integration-tests/tests/bridge_erc20.ts b/packages/integration-tests/tests/bridge_erc20.ts index 70d9e9f8..ed42d09c 100644 --- a/packages/integration-tests/tests/bridge_erc20.ts +++ b/packages/integration-tests/tests/bridge_erc20.ts @@ -87,8 +87,15 @@ describe('Bridging ERC20 tokens', async function () { }, }); - const incomingMessagesonFuel = await env.fuel.signers[0].getMessages(); + // verify the incoming messages generated when base asset is minted on fuel + let incomingMessagesonFuel = await env.fuel.signers[0].getMessages(); + + expect(incomingMessagesonFuel.messages.length === 1); + expect( + incomingMessagesonFuel.messages[0].amount === fuels_parseEther('1') + ); + // construct message coin const messageCoin: MessageCoin = { assetId: env.fuel.provider.getBaseAssetId(), sender: incomingMessagesonFuel.messages[0].sender, @@ -98,21 +105,31 @@ describe('Bridging ERC20 tokens', async function () { amount: incomingMessagesonFuel.messages[0].amount, }; - // making sure resource is of Message Coin type and DA Height is non zero - expect(messageCoin.daHeight > new BN(0)); - const transactionRequest = await tx.getTransactionRequest(); + // add message coin as input to fund the tx transactionRequest.addMessageInput(messageCoin); + // add the erc20 token input which will be burnt on withdrawal + const resource = await fuelTokenSender.getResourcesToSpend([ + [ + new BN(NUM_TOKENS.toString()).div(new BN(DECIMAL_DIFF.toString())), + fuel_testAssetId, + ], + ]); + + transactionRequest.addResources(resource); + + // fetch tx cost const cost = await fuelTokenSender.getTransactionCost(transactionRequest); - console.log('tx cost', cost); // update fee params - transactionRequest.gasLimit = new BN(0x302d1b); + transactionRequest.gasLimit = cost.gasUsed; + transactionRequest.maxFee = cost.maxFee; - const inputs: any = await transactionRequest.inputs; - console.log('tx imputs', inputs); + // verify that the message coin is consumed + incomingMessagesonFuel = await fuelTokenSender.getMessages(); + expect(incomingMessagesonFuel.messages.length === 0); return transactionRequest; } else { @@ -162,8 +179,6 @@ describe('Bridging ERC20 tokens', async function () { const tx = await fuelTokenSender.sendTransaction(transactionRequest); - console.log('dddd', tx); - const fWithdrawTxResult = await tx.waitForResult(); expect(fWithdrawTxResult.status).to.equal('success'); @@ -425,7 +440,7 @@ describe('Bridging ERC20 tokens', async function () { const tx = await env.eth.fuelMessagePortal .connect(ethereumTokenSender) .depositETH(fuelTokenReceiverAddress, { - value: parseEther('10'), + value: parseEther('1'), }); const receipt = await tx.wait(); expect(receipt.status).to.equal(1); @@ -460,14 +475,6 @@ describe('Bridging ERC20 tokens', async function () { FUEL_MESSAGE_TIMEOUT_MS ) ).to.not.be.null; - - // verify the incoming messages generated when base asset is minted on fuel - const incomingMessagesonFuel = await env.fuel.signers[0].getMessages(); - console.log('incomingMessagesonFuel', incomingMessagesonFuel); - expect(incomingMessagesonFuel.messages.length === 1); - // expect( - // incomingMessagesonFuel.messages[0] === fuels_parseEther('1') - // ); }); it('Bridge ERC20 token with permit via FuelERC20Gateway', async () => { @@ -617,11 +624,6 @@ describe('Bridging ERC20 tokens', async function () { fuelTokenReceiverBalance.add(toBeHex(NUM_TOKENS / DECIMAL_DIFF)) ) ).to.be.true; - - const res = await fuelTokenReceiver.getResourcesToSpend([ - [new BN('1'), env.fuel.provider.getBaseAssetId()], - ]); - console.log(res); }); it('Check ERC20 permit token arrived on Fuel', async () => { diff --git a/packages/integration-tests/tests/bridge_mainnet_tokens.ts b/packages/integration-tests/tests/bridge_mainnet_tokens.ts index 4543e326..139e6472 100644 --- a/packages/integration-tests/tests/bridge_mainnet_tokens.ts +++ b/packages/integration-tests/tests/bridge_mainnet_tokens.ts @@ -40,6 +40,7 @@ import { Address, BN } from 'fuels'; import type { AbstractAddress, WalletUnlocked as FuelWallet, + MessageCoin, MessageProof, ScriptTransactionRequest, } from 'fuels'; @@ -101,33 +102,50 @@ describe('Bridge mainnet tokens', function () { }, }); - const cost = await tx.getTransactionCost(); + // verify the incoming messages generated when base asset is minted on fuel + let incomingMessagesonFuel = await env.fuel.signers[0].getMessages(); - // message coin resource - const resources = await fuelTokenSender.getResourcesToSpend([ - [fuels_parseEther('1'), env.fuel.provider.getBaseAssetId()], - ]); + expect(incomingMessagesonFuel.messages.length === 1); + expect( + incomingMessagesonFuel.messages[0].amount === fuels_parseEther('1') + ); - expect(resources.length === 1); + // construct message coin + const messageCoin: MessageCoin = { + assetId: env.fuel.provider.getBaseAssetId(), + sender: incomingMessagesonFuel.messages[0].sender, + recipient: incomingMessagesonFuel.messages[0].recipient, + nonce: incomingMessagesonFuel.messages[0].nonce, + daHeight: incomingMessagesonFuel.messages[0].daHeight, + amount: incomingMessagesonFuel.messages[0].amount, + }; - const messageCoin: any = resources[0]; + const transactionRequest = await tx.getTransactionRequest(); - // making sure resource is of Message Coin type and DA Height is non zero - expect(messageCoin.daHeight > new BN(0)); + // add message coin as input to fund the tx + transactionRequest.addMessageInput(messageCoin); + + // add the erc20 token input which will be burnt on withdrawal + const resource = await fuelTokenSender.getResourcesToSpend([ + [ + new BN(NUM_TOKENS.toString()).div( + new BN((10n ** (18n - decimals)).toString()) + ), + fuel_AssetId, + ], + ]); - const transactionRequest = await tx.getTransactionRequest(); + transactionRequest.addResources(resource); - // add message coin input - await transactionRequest.addResources(resources); + // fetch tx cost + const cost = await fuelTokenSender.getTransactionCost(transactionRequest); // update fee params transactionRequest.gasLimit = cost.gasUsed; transactionRequest.maxFee = cost.maxFee; - await fuelTokenSender.fund(transactionRequest, cost); - // verify that the message coin is consumed - const incomingMessagesonFuel = await env.fuel.signers[0].getMessages(); + incomingMessagesonFuel = await fuelTokenSender.getMessages(); expect(incomingMessagesonFuel.messages.length === 0); return transactionRequest; @@ -372,14 +390,6 @@ describe('Bridge mainnet tokens', function () { FUEL_MESSAGE_TIMEOUT_MS ) ).to.not.be.null; - - // verify the incoming messages generated when base asset is minted on fuel - const incomingMessagesonFuel = - await env.fuel.signers[0].getMessages(); - expect(incomingMessagesonFuel.messages.length === 1); - expect( - incomingMessagesonFuel.messages[0].amount === fuels_parseEther('1') - ); }); it('Bridge ERC20 via FuelERC20Gateway', async () => {