Skip to content

Commit

Permalink
feat: add deposit script (#188)
Browse files Browse the repository at this point in the history
Co-authored-by: Mad <46090742+DefiCake@users.noreply.github.com>
  • Loading branch information
luizstacio and DefiCake authored Jun 3, 2024
1 parent 163e13c commit 95a1620
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .changeset/nine-dryers-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
1 change: 1 addition & 0 deletions packages/integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"test:erc20": "pnpm mocha -b -r ts-node/register 'tests/bridge_erc20.ts'",
"test:erc721": "pnpm mocha -b -r ts-node/register 'tests/bridge_erc721.ts'",
"test:transfer": "pnpm mocha -b -r ts-node/register 'tests/transfer_eth.ts'",
"depositETH": "pnpm ts-node scripts/depositETH.ts",
"bridgeETH": "pnpm ts-node scripts/bridgeETH.ts",
"bridgeERC20": "pnpm ts-node scripts/bridgeERC20.ts",
"transfer": "pnpm ts-node scripts/transfer.ts"
Expand Down
5 changes: 3 additions & 2 deletions packages/integration-tests/scripts/bridgeETH.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@ const ETH_AMOUNT = '0.1';

// parse events from logs to get the message nonce
const event = fuelMessagePortal.interface.parseLog(eSendTxResult.logs[0]);
const depositMessageNonce = new BN(event.args.nonce.toHexString());
const depositMessageNonce = new BN(event.args.nonce.toString());
const depositRecipient = Address.fromB256(event.args.recipient);

// wait for message to appear in fuel client
console.log('Waiting for ETH to arrive on Fuel...');
const depositMessage = await waitForMessage(
env.fuel.provider,
fuelAccount.address,
depositRecipient,
depositMessageNonce,
FUEL_MESSAGE_TIMEOUT_MS
);
Expand Down
72 changes: 72 additions & 0 deletions packages/integration-tests/scripts/depositETH.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import type { TestEnvironment } from '@fuel-bridge/test-utils';
import {
setupEnvironment,
logETHBalances,
waitForMessage,
FUEL_MESSAGE_TIMEOUT_MS,
} from '@fuel-bridge/test-utils';
import { parseEther } from 'ethers';
import { Address, BN } from 'fuels';

const ETH_AMOUNT = '0.1';

// This script is a demonstration of how the base asset (ETH) is bridged to and from the Fuel chain
(async function depositETH() {
// basic setup routine which creates the connections (the "providers") to both chains,
// funds addresses for us to test with and populates the official contract deployments
// on the Ethereum chain for interacting with the Fuel chain
console.log('Setting up environment...');
console.log('');
const timeoutCheck = FUEL_MESSAGE_TIMEOUT_MS * 5;
const env: TestEnvironment = await setupEnvironment({
skip_deployer_balance: true,
});
const ethereumAccount = env.eth.signers[0];
const fuelAccount = env.fuel.signers[0];
const fuelAccountAddress = fuelAccount.address.toHexString();
const fuelMessagePortal = env.eth.fuelMessagePortal.connect(ethereumAccount);

/////////////////////////////
// Bridge Ethereum -> Fuel //
/////////////////////////////

// note balances of both accounts before transfer
await logETHBalances(ethereumAccount, fuelAccount);

// use the FuelMessagePortal to directly send ETH to the fuel account
console.log(`Sending ${ETH_AMOUNT} ETH from Ethereum...`);
const eSendTx = await fuelMessagePortal.depositETH(fuelAccountAddress, {
value: parseEther(ETH_AMOUNT),
});
const eSendTxResult = await eSendTx.wait();
if (eSendTxResult.status !== 1) {
console.log(eSendTxResult);
throw new Error('failed to call depositETH');
}

// parse events from logs to get the message nonce
const event = fuelMessagePortal.interface.parseLog(eSendTxResult.logs[0]);
const depositMessageNonce = new BN(event.args.nonce.toString());
const depositRecipient = Address.fromB256(event.args.recipient);

// wait for message to appear in fuel client
console.log('Waiting for ETH to arrive on Fuel...');
const depositMessage = await waitForMessage(
env.fuel.provider,
depositRecipient,
depositMessageNonce,
timeoutCheck
);
if (depositMessage == null) {
throw new Error(
`message took longer than ${timeoutCheck}ms to arrive on Fuel`
);
}
console.log('');

// the sent ETH is now spendable on Fuel
console.log('ETH was bridged to Fuel successfully!!');

// note balances of both accounts after transfer
await logETHBalances(ethereumAccount, fuelAccount);
})();
14 changes: 8 additions & 6 deletions packages/test-utils/src/utils/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export interface SetupOptions {
pk_fuel_deployer?: string;
pk_fuel_signer1?: string;
pk_fuel_signer2?: string;
skip_deployer_balance?: boolean;
}

// The test environment
Expand Down Expand Up @@ -125,6 +126,7 @@ export async function setupEnvironment(

const fuel_erc721_gateway_addr: string =
process.env.FUEL_ERC20_GATEWAY_ADDRESS || '';
const skip_deployer_balance = !opts.skip_deployer_balance;

// Create provider from http_fuel_client
const fuel_provider = await FuelProvider.create(http_fuel_client);
Expand All @@ -139,7 +141,7 @@ export async function setupEnvironment(
}
const fuel_deployer = Wallet.fromPrivateKey(pk_fuel_deployer, fuel_provider);
const fuel_deployerBalance = await fuel_deployer.getBalance();
if (fuel_deployerBalance.lt(fuels_parseEther('5'))) {
if (fuel_deployerBalance.lt(fuels_parseEther('5')) && skip_deployer_balance) {
throw new Error(
'Fuel deployer balance is very low (' +
fuels_formatEther(fuel_deployerBalance) +
Expand All @@ -148,7 +150,7 @@ export async function setupEnvironment(
}
const fuel_signer1 = Wallet.fromPrivateKey(pk_fuel_signer1, fuel_provider);
const fuel_signer1Balance = await fuel_signer1.getBalance();
if (fuel_signer1Balance.lt(fuels_parseEther('1'))) {
if (fuel_signer1Balance.lt(fuels_parseEther('1')) && skip_deployer_balance) {
const tx = await fuel_deployer.transfer(
fuel_signer1.address,
fuels_parseEther('1').toHex()
Expand All @@ -157,7 +159,7 @@ export async function setupEnvironment(
}
const fuel_signer2 = Wallet.fromPrivateKey(pk_fuel_signer2, fuel_provider);
const fuel_signer2Balance = await fuel_signer2.getBalance();
if (fuel_signer2Balance.lt(fuels_parseEther('1'))) {
if (fuel_signer2Balance.lt(fuels_parseEther('1')) && skip_deployer_balance) {
const tx = await fuel_deployer.transfer(
fuel_signer2.address,
fuels_parseEther('1').toHex()
Expand All @@ -181,7 +183,7 @@ export async function setupEnvironment(
new ethers.Wallet(pk_eth_deployer, eth_provider)
);
const eth_deployerBalance = await eth_provider.getBalance(eth_deployer);
if (eth_deployerBalance < parseEther('5')) {
if (eth_deployerBalance < parseEther('5') && skip_deployer_balance) {
throw new Error(
'Ethereum deployer balance is very low (' +
formatEther(eth_deployerBalance) +
Expand All @@ -192,7 +194,7 @@ export async function setupEnvironment(
new ethers.Wallet(pk_eth_signer1, eth_provider)
);
const eth_signer1Balance = await eth_provider.getBalance(eth_signer1);
if (eth_signer1Balance < parseEther('1')) {
if (eth_signer1Balance < parseEther('1') && skip_deployer_balance) {
const tx = await eth_deployer.sendTransaction({
to: eth_signer1,
value: parseEther('1'),
Expand All @@ -203,7 +205,7 @@ export async function setupEnvironment(
new ethers.Wallet(pk_eth_signer2, eth_provider)
);
const eth_signer2Balance = await eth_provider.getBalance(eth_signer2);
if (eth_signer2Balance < parseEther('1')) {
if (eth_signer2Balance < parseEther('1') && skip_deployer_balance) {
const tx = await eth_deployer.sendTransaction({
to: eth_signer2,
value: parseEther('1'),
Expand Down

0 comments on commit 95a1620

Please sign in to comment.