Skip to content

Commit

Permalink
fix: remove use of Coin to fund when message coin is used
Browse files Browse the repository at this point in the history
  • Loading branch information
viraj124 committed Jan 21, 2025
1 parent 3dc09e0 commit f4c5e50
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 69 deletions.
53 changes: 31 additions & 22 deletions packages/integration-tests/fork-tests/bridge_erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { Address, BN } from 'fuels';
import type {
AbstractAddress,
WalletUnlocked as FuelWallet,
MessageCoin,
MessageProof,
Provider,
ScriptTransactionRequest,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 () => {
Expand Down
50 changes: 26 additions & 24 deletions packages/integration-tests/tests/bridge_erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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 () => {
Expand Down
56 changes: 33 additions & 23 deletions packages/integration-tests/tests/bridge_mainnet_tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { Address, BN } from 'fuels';
import type {
AbstractAddress,
WalletUnlocked as FuelWallet,
MessageCoin,
MessageProof,
ScriptTransactionRequest,
} from 'fuels';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 () => {
Expand Down

0 comments on commit f4c5e50

Please sign in to comment.