Skip to content

Commit

Permalink
refactor: move tx fund logic into a common util method
Browse files Browse the repository at this point in the history
  • Loading branch information
viraj124 committed Jan 22, 2025
1 parent f4c5e50 commit 3ce7241
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 307 deletions.
115 changes: 14 additions & 101 deletions packages/integration-tests/fork-tests/bridge_erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ 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 @@ -29,12 +28,12 @@ import { Address, BN } from 'fuels';
import type {
AbstractAddress,
WalletUnlocked as FuelWallet,
MessageCoin,
MessageProof,
Provider,
ScriptTransactionRequest,
} from 'fuels';

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

const { expect } = chai;

describe('Bridging ERC20 tokens', async function () {
Expand All @@ -59,96 +58,6 @@ describe('Bridging ERC20 tokens', async function () {
await provider.produceBlocks(Number(blocksToForward)).catch(console.error);
}

async function fetchTransaction(
fuel_bridge: BridgeFungibleToken,
fuelTokenSender: FuelWallet,
to: string,
NUM_TOKENS: bigint,
DECIMAL_DIFF: bigint,
useMessageCoin: boolean
): Promise<ScriptTransactionRequest> {
if (useMessageCoin) {
const tx = await fuel_bridge.functions
.withdraw(to)
.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,
},
});

// 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,
recipient: incomingMessagesonFuel.messages[0].recipient,
nonce: incomingMessagesonFuel.messages[0].nonce,
daHeight: incomingMessagesonFuel.messages[0].daHeight,
amount: incomingMessagesonFuel.messages[0].amount,
};

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);

// update fee params
transactionRequest.gasLimit = cost.gasUsed;
transactionRequest.maxFee = cost.maxFee;

// verify that the message coin is consumed
incomingMessagesonFuel = await fuelTokenSender.getMessages();
expect(incomingMessagesonFuel.messages.length === 0);

return transactionRequest;
} else {
return await fuel_bridge.functions
.withdraw(to)
.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();
}
}

async function generateWithdrawalMessageProof(
fuel_bridge: BridgeFungibleToken,
fuelTokenSender: FuelWallet,
Expand All @@ -165,14 +74,18 @@ describe('Bridging ERC20 tokens', async function () {
fuel_testAssetId
);

const transactionRequest = await fetchTransaction(
fuel_bridge,
fuelTokenSender,
paddedAddress,
NUM_TOKENS,
DECIMAL_DIFF,
useMessageCoin
);
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
115 changes: 14 additions & 101 deletions packages/integration-tests/tests/bridge_erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ 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 @@ -34,11 +33,11 @@ import { Address, BN } from 'fuels';
import type {
AbstractAddress,
WalletUnlocked as FuelWallet,
MessageCoin,
MessageProof,
ScriptTransactionRequest,
} from 'fuels';

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

const { expect } = chai;

describe('Bridging ERC20 tokens', async function () {
Expand All @@ -62,96 +61,6 @@ describe('Bridging ERC20 tokens', async function () {
// override the default test timeout from 2000ms
this.timeout(DEFAULT_TIMEOUT_MS);

async function fetchTransaction(
fuel_bridge: BridgeFungibleToken,
fuelTokenSender: FuelWallet,
to: string,
NUM_TOKENS: bigint,
DECIMAL_DIFF: bigint,
useMessageCoin: boolean
): Promise<ScriptTransactionRequest> {
if (useMessageCoin) {
const tx = await fuel_bridge.functions
.withdraw(to)
.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,
},
});

// 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,
recipient: incomingMessagesonFuel.messages[0].recipient,
nonce: incomingMessagesonFuel.messages[0].nonce,
daHeight: incomingMessagesonFuel.messages[0].daHeight,
amount: incomingMessagesonFuel.messages[0].amount,
};

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);

// update fee params
transactionRequest.gasLimit = cost.gasUsed;
transactionRequest.maxFee = cost.maxFee;

// verify that the message coin is consumed
incomingMessagesonFuel = await fuelTokenSender.getMessages();
expect(incomingMessagesonFuel.messages.length === 0);

return transactionRequest;
} else {
return await fuel_bridge.functions
.withdraw(to)
.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();
}
}

async function generateWithdrawalMessageProof(
fuel_bridge: BridgeFungibleToken,
fuelTokenSender: FuelWallet,
Expand All @@ -168,14 +77,18 @@ describe('Bridging ERC20 tokens', async function () {
fuel_testAssetId
);

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

const tx = await fuelTokenSender.sendTransaction(transactionRequest);

Expand Down
Loading

0 comments on commit 3ce7241

Please sign in to comment.