-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |