Skip to content

Commit

Permalink
test: token withdrawal tests using message coin (#385)
Browse files Browse the repository at this point in the history
- close #274
  • Loading branch information
viraj124 authored Feb 12, 2025
1 parent 644a1d6 commit 7602794
Show file tree
Hide file tree
Showing 6 changed files with 401 additions and 600 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"turbo": "^1.10.7"
},
"devDependencies": {
"@changesets/cli": "^2.26.2",
"@changesets/cli": "^2.27.11",
"@fuel-ts/forc": "0.73.0"
},
"pnpm": {
Expand Down
94 changes: 75 additions & 19 deletions packages/integration-tests/fork-tests/bridge_erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
getBlock,
FUEL_CALL_TX_PARAMS,
hardhatSkipTime,
fuels_parseEther,
} from '@fuel-bridge/test-utils';
import chai from 'chai';
import { toBeHex, parseEther } from 'ethers';
Expand All @@ -32,6 +33,8 @@ import type {
Provider,
} from 'fuels';

import { fundWithdrawalTransactionWithBaseAssetResource } from '../utils/utils';

const { expect } = chai;

describe('Bridging ERC20 tokens', async function () {
Expand Down Expand Up @@ -61,7 +64,8 @@ describe('Bridging ERC20 tokens', async function () {
fuelTokenSender: FuelWallet,
ethereumTokenReceiverAddress: string,
NUM_TOKENS: bigint,
DECIMAL_DIFF: bigint
DECIMAL_DIFF: bigint,
useMessageCoin: boolean
): Promise<MessageProof | null> {
// withdraw tokens back to the base chain
fuel_bridge.account = fuelTokenSender;
Expand All @@ -70,22 +74,19 @@ describe('Bridging ERC20 tokens', async function () {
const fuelTokenSenderBalance = await fuelTokenSender.getBalance(
fuel_testAssetId
);
const transactionRequest = await fuel_bridge.functions
.withdraw(paddedAddress)
.addContracts([fuel_bridge, fuel_bridgeImpl])
.txParams({
tip: 0,
maxFee: 1,
})
.callParams({
forward: {
amount: new BN(NUM_TOKENS.toString()).div(
new BN(DECIMAL_DIFF.toString())
),
assetId: fuel_testAssetId,
},
})
.fundWithRequiredCoins();

const transactionRequest =
await fundWithdrawalTransactionWithBaseAssetResource(
env,
fuel_bridge,
fuelTokenSender,
paddedAddress,
NUM_TOKENS,
9n,
fuel_bridgeImpl,
fuel_testAssetId,
useMessageCoin
);

const tx = await fuelTokenSender.sendTransaction(transactionRequest);
const fWithdrawTxResult = await tx.waitForResult();
Expand Down Expand Up @@ -296,6 +297,59 @@ describe('Bridging ERC20 tokens', async function () {
);
});

it('Bridge ETH to Fuel to be used as Message Coin during token withdrawal', async () => {
// use the FuelMessagePortal to directly send ETH which should be immediately spendable
const tx = await env.eth.fuelMessagePortal
.connect(ethereumTokenSender)
.depositETH(fuelTokenReceiverAddress, {
value: parseEther('1'),
});
const receipt = await tx.wait();
expect(receipt.status).to.equal(1);

// parse events from logs
const filter = env.eth.fuelMessagePortal.filters.MessageSent(
null, // Args set to null since there should be just 1 event for MessageSent
null,
null,
null,
null
);

const [event, ...restOfEvents] =
await env.eth.fuelMessagePortal.queryFilter(
filter,
receipt.blockNumber,
receipt.blockNumber
);
expect(restOfEvents.length).to.be.eq(0); // Should be only 1 event

const fuelETHMessageNonce = new BN(event.args.nonce.toString());

fuelTokenMessageReceiver = fuelTokenReceiver.address;

// wait for message to appear in fuel client
expect(
await waitForMessage(
env.fuel.provider,
fuelTokenMessageReceiver,
fuelETHMessageNonce,
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();

// eth as bridged once at the start
expect(incomingMessagesonFuel.messages.length === 1).to.be.true;

// 1 eth was bridged
expect(
incomingMessagesonFuel.messages[0].amount.eq(fuels_parseEther('1'))
).to.be.true;
});

it('Bridge ERC20 via FuelERC20Gateway', async () => {
// approve FuelERC20Gateway to spend the tokens
await eth_testToken
Expand Down Expand Up @@ -455,7 +509,8 @@ describe('Bridging ERC20 tokens', async function () {
fuelTokenSender,
ethereumTokenReceiverAddress,
NUM_TOKENS,
DECIMAL_DIFF
DECIMAL_DIFF,
true
);
});

Expand Down Expand Up @@ -573,7 +628,8 @@ describe('Bridging ERC20 tokens', async function () {
fuelTokenSender,
ethereumTokenReceiverAddress,
NUM_TOKENS,
DECIMAL_DIFF
DECIMAL_DIFF,
false
);

// relay message
Expand Down
95 changes: 76 additions & 19 deletions packages/integration-tests/tests/bridge_erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
getBlock,
FUEL_CALL_TX_PARAMS,
hardhatSkipTime,
fuels_parseEther,
} from '@fuel-bridge/test-utils';
import chai from 'chai';
import { toBeHex, parseEther } from 'ethers';
Expand All @@ -36,6 +37,8 @@ import type {
MessageProof,
} from 'fuels';

import { fundWithdrawalTransactionWithBaseAssetResource } from '../utils/utils';

const { expect } = chai;

describe('Bridging ERC20 tokens', async function () {
Expand Down Expand Up @@ -64,7 +67,8 @@ describe('Bridging ERC20 tokens', async function () {
fuelTokenSender: FuelWallet,
ethereumTokenReceiverAddress: string,
NUM_TOKENS: bigint,
DECIMAL_DIFF: bigint
DECIMAL_DIFF: bigint,
useMessageCoin: boolean
): Promise<MessageProof> {
// withdraw tokens back to the base chain
fuel_bridge.account = fuelTokenSender;
Expand All @@ -73,24 +77,22 @@ describe('Bridging ERC20 tokens', async function () {
const fuelTokenSenderBalance = await fuelTokenSender.getBalance(
fuel_testAssetId
);
const transactionRequest = await fuel_bridge.functions
.withdraw(paddedAddress)
.addContracts([fuel_bridge, fuel_bridgeImpl])
.txParams({
tip: 0,
maxFee: 1,
})
.callParams({
forward: {
amount: new BN(NUM_TOKENS.toString()).div(
new BN(DECIMAL_DIFF.toString())
),
assetId: fuel_testAssetId,
},
})
.fundWithRequiredCoins();

const transactionRequest =
await fundWithdrawalTransactionWithBaseAssetResource(
env,
fuel_bridge,
fuelTokenSender,
paddedAddress,
NUM_TOKENS,
9n,
fuel_bridgeImpl,
fuel_testAssetId,
useMessageCoin
);

const tx = await fuelTokenSender.sendTransaction(transactionRequest);

const fWithdrawTxResult = await tx.waitForResult();
expect(fWithdrawTxResult.status).to.equal('success');

Expand Down Expand Up @@ -347,6 +349,59 @@ describe('Bridging ERC20 tokens', async function () {
);
});

it('Bridge ETH to Fuel to be used as Message Coin during token withdrawal', async () => {
// use the FuelMessagePortal to directly send ETH which should be immediately spendable
const tx = await env.eth.fuelMessagePortal
.connect(ethereumTokenSender)
.depositETH(fuelTokenReceiverAddress, {
value: parseEther('1'),
});
const receipt = await tx.wait();
expect(receipt.status).to.equal(1);

// parse events from logs
const filter = env.eth.fuelMessagePortal.filters.MessageSent(
null, // Args set to null since there should be just 1 event for MessageSent
null,
null,
null,
null
);

const [event, ...restOfEvents] =
await env.eth.fuelMessagePortal.queryFilter(
filter,
receipt.blockNumber,
receipt.blockNumber
);
expect(restOfEvents.length).to.be.eq(0); // Should be only 1 event

const fuelETHMessageNonce = new BN(event.args.nonce.toString());

fuelTokenMessageReceiver = fuelTokenReceiver.address;

// wait for message to appear in fuel client
expect(
await waitForMessage(
env.fuel.provider,
fuelTokenMessageReceiver,
fuelETHMessageNonce,
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();

// eth as bridged once at the start
expect(incomingMessagesonFuel.messages.length === 1).to.be.true;

// 1 eth was bridged
expect(
incomingMessagesonFuel.messages[0].amount.eq(fuels_parseEther('1'))
).to.be.true;
});

it('Bridge ERC20 token with permit via FuelERC20Gateway', async () => {
const tokenName = await eth_permitTestToken.name();
const tokenAddress = await eth_permitTestToken.getAddress();
Expand Down Expand Up @@ -591,7 +646,8 @@ describe('Bridging ERC20 tokens', async function () {
fuelTokenSender,
ethereumTokenReceiverAddress,
NUM_TOKENS,
DECIMAL_DIFF
DECIMAL_DIFF,
true
);
});

Expand Down Expand Up @@ -726,7 +782,8 @@ describe('Bridging ERC20 tokens', async function () {
fuelTokenSender,
ethereumTokenReceiverAddress,
NUM_TOKENS,
DECIMAL_DIFF
DECIMAL_DIFF,
false
);

// relay message
Expand Down
Loading

0 comments on commit 7602794

Please sign in to comment.