Skip to content

Commit

Permalink
chore: add util file
Browse files Browse the repository at this point in the history
  • Loading branch information
viraj124 committed Jan 22, 2025
1 parent 3ce7241 commit 41b5a57
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions packages/integration-tests/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import type { BridgeFungibleToken } from '@fuel-bridge/fungible-token';
import { type TestEnvironment } from '@fuel-bridge/test-utils';
import chai from 'chai';
import { BN } from 'fuels';
import type {
WalletUnlocked as FuelWallet,
MessageCoin,
ScriptTransactionRequest,
} from 'fuels';

const { expect } = chai;

// funds the withdrawal transaction with `MessageCoin` or `Coin` type
export async function fundWithdrawalTransactionWithBaseAssetResource(
env: TestEnvironment,
fuelBridge: BridgeFungibleToken,
fuelTokenSender: FuelWallet,
to: string,
amount: bigint,
l2DecimalDifference: bigint,
fuelBridgeImpl: BridgeFungibleToken,
fuelAsset: string,
useMessageCoin: boolean
): Promise<ScriptTransactionRequest> {
const tx = await fuelBridge.functions
.withdraw(to)
.addContracts([fuelBridge, fuelBridgeImpl])
.txParams({
tip: 0,
maxFee: 1,
})
.callParams({
forward: {
amount: new BN(amount.toString()).div(
new BN((10n ** (18n - l2DecimalDifference)).toString())
),
assetId: fuelAsset,
},
});
if (useMessageCoin) {
// 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);

// 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(amount.toString()).div(
new BN((10n ** (18n - l2DecimalDifference)).toString())
),
fuelAsset,
],
]);

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 tx.fundWithRequiredCoins();
}
}

0 comments on commit 41b5a57

Please sign in to comment.