Skip to content

Commit

Permalink
feat: prepare sway scripts for mainnet (#280)
Browse files Browse the repository at this point in the history
Co-authored-by: K1-R1 <77465250+K1-R1@users.noreply.github.com>
Co-authored-by: Luiz Estácio | stacio.eth <luizstacio@gmail.com>
Co-authored-by: Viraz Malhotra <virajm72@gmail.com>
  • Loading branch information
4 people authored Oct 14, 2024
1 parent 2f61bd8 commit 5ddd191
Show file tree
Hide file tree
Showing 17 changed files with 1,950 additions and 42 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-rivers-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@fuel-bridge/test-utils': minor
---

Improve sway scripts
19 changes: 13 additions & 6 deletions packages/test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,26 @@
"scripts": {
"build": "tsup",
"build:watch": "tsup --watch",
"deploy:bridge": "pnpm ts-node src/scripts/bridge.ts",
"deploy:relay": "pnpm ts-node src/scripts/relay-deposit.ts"
"bridge:deploy": "pnpm ts-node src/scripts/deploy-bridge.ts",
"bridge:upgrade": "pnpm ts-node src/scripts/upgrade-bridge.ts",
"bridge:transfer-ownership": "pnpm ts-node src/scripts/transfer-bridge-ownership.ts",
"bridge:relay": "pnpm ts-node src/scripts/relay-deposit.ts",
"bridge:withdraw": "pnpm ts-node src/scripts/withdraw-init.ts",
"script:check-balances": "pnpm ts-node src/scripts/check-balances.ts"
},
"peerDependencies": {
"fuels": "0.94.4",
"ethers": "6.13.1"
"ethers": "6.13.1",
"fuels": "0.94.4"
},
"devDependencies": {
"@fuel-bridge/fungible-token": "workspace:*",
"@fuel-bridge/message-predicates": "workspace:*",
"@fuel-bridge/solidity-contracts": "workspace:*",
"@fuels/kms-account": "0.24.0-preview-63de5d8",
"@inquirer/prompts": "^5.3.8",
"dotenv": "^16.0.3",
"typescript": "^5.1.6",
"ts-node": "^10.9.1"
"inquirer": "^10.1.8",
"ts-node": "^10.9.1",
"typescript": "^5.1.6"
}
}
30 changes: 30 additions & 0 deletions packages/test-utils/src/scripts/check-balances.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* This is a stand-alone script that looks an address' balances
*/

import { password } from '@inquirer/prompts';
import { Provider, WalletUnlocked } from 'fuels';

let { L2_ADDRESS, L2_RPC } = process.env;

const main = async () => {
const provider = await Provider.create(L2_RPC, { resourceCacheTTL: -1 });

if (!L2_ADDRESS) {
const privKey = await password({ message: 'Enter private key' });
const wallet = new WalletUnlocked(privKey);
L2_ADDRESS = wallet.address.toB256();
}

await provider.getBalances(L2_ADDRESS).then(console.log);
};

main()
.then(() => {
console.log('\t> Finished');
process.exit(0);
})
.catch((e) => {
console.error(e);
process.exit(1);
});
44 changes: 44 additions & 0 deletions packages/test-utils/src/scripts/check-nonce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* This is a stand-alone script that looks for a message nonce
*/

import { BN, Message, Provider } from 'fuels';

let { L2_RPC, L2_MESSAGE_NONCE } = process.env;

const main = async () => {
if (!L2_MESSAGE_NONCE) {
console.log('Specify L2_MESSAGE_NONCE');
return;
}

if (!L2_RPC) {
console.log('Specify L2_RPC');
return;
}

const provider = await Provider.create(L2_RPC, { resourceCacheTTL: -1 });

const message: Message = await provider
.getMessageByNonce(new BN(L2_MESSAGE_NONCE).toHex(32))
.catch((e) => {
console.log(JSON.stringify(e, undefined, 2));
return null;
});

if (!message) {
console.log('Could not fetch message');
}

console.log(message);
};

main()
.then(() => {
console.log('\t> Finished');
process.exit(0);
})
.catch((e) => {
console.error(e);
process.exit(1);
});
31 changes: 31 additions & 0 deletions packages/test-utils/src/scripts/check-proxy-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* This is a stand-alone script that
* calls the bridge 's withdraw method
*/

import { Proxy } from '@fuel-bridge/fungible-token';

import { Provider } from 'fuels';

let { L2_RPC, L2_BRIDGE_ID } = process.env;
const L1_LLAMA_RPC = 'https://eth.llamarpc.com';
const main = async () => {
const fuel_provider = await Provider.create(L2_RPC, { resourceCacheTTL: -1 });

const proxy = new Proxy(L2_BRIDGE_ID, fuel_provider);

console.log('\t> Checking asset metadata...');

console.log('Owner', (await proxy.functions._proxy_owner().get()).value);
console.log('Target', (await proxy.functions.proxy_target().get()).value);
};

main()
.then(() => {
console.log('\t> Finished');
process.exit(0);
})
.catch((e) => {
console.error(e);
process.exit(1);
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import {
WalletUnlocked,
ZeroBytes32,
} from 'fuels';
import { password } from '@inquirer/prompts';

const { L1_TOKEN_GATEWAY, L2_SIGNER, L2_RPC } = process.env;
let { L1_TOKEN_GATEWAY, L2_SIGNER, L2_RPC } = process.env;

// This helper avoids an exception in the case that the contract
// was already deployed, and returns the contract instead
Expand All @@ -37,6 +38,11 @@ function fetchIfDeployed(provider: Provider, wallet: WalletUnlocked) {

const main = async () => {
const provider = await Provider.create(L2_RPC, { resourceCacheTTL: -1 });

if (!L2_SIGNER) {
L2_SIGNER = await password({ message: 'Enter private key' });
}

const wallet = Wallet.fromPrivateKey(L2_SIGNER, provider);

console.log('\t> L2 Bridge deployment script initiated');
Expand Down
45 changes: 45 additions & 0 deletions packages/test-utils/src/scripts/deposit-to-coin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* This is a stand-alone script that self-transfers
* to convert a message coin into a coin utxo
*/

import { Provider, TransactionStatus, Wallet } from 'fuels';
import { password } from '@inquirer/prompts';

let { L2_SIGNER, L2_RPC } = process.env;

const main = async () => {
if (!L2_RPC) {
console.log('Must provide L2_RPC');
return;
}

const provider = await Provider.create(L2_RPC, { resourceCacheTTL: -1 });

if (!L2_SIGNER) {
L2_SIGNER = await password({ message: 'Enter private key' });
}

const wallet = Wallet.fromPrivateKey(L2_SIGNER, provider);
const balance = await wallet.getBalance();
const tx = await wallet.transfer(wallet.address, balance.div(2));

console.log('\tTransaction ID: ', tx.id);
const txResult = await tx.waitForResult();

if (txResult.status === TransactionStatus.success) {
console.log('\t> Transaction succeeded');
} else {
console.log('\t> Transaction errored');
}
};

main()
.then(() => {
console.log('\t> Finished');
process.exit(0);
})
.catch((e) => {
console.error(e);
process.exit(1);
});
83 changes: 52 additions & 31 deletions packages/test-utils/src/scripts/relay-deposit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* This is a stand-alone script that deploys the
* fetches a deposit messages and relays it to the bridge
* This is a stand-alone script that
* fetches a deposit message and relays it to the bridge
*/

import { Proxy } from '@fuel-bridge/fungible-token';
Expand All @@ -16,6 +16,7 @@ import {
getPredicateRoot,
hexlify,
} from 'fuels';
import { password } from '@inquirer/prompts';
import {
FUEL_MESSAGE_TIMEOUT_MS,
debug,
Expand All @@ -25,11 +26,16 @@ import {

const TOKEN_RECIPIENT_DATA_OFFSET = 160;

const { L2_SIGNER, L2_RPC, L2_BRIDGE_ID, L2_MESSAGE_NONCE, L2_TOKEN_RECEIVER } =
let { L2_SIGNER, L2_RPC, L2_BRIDGE_ID, L2_MESSAGE_NONCE, L2_TOKEN_RECEIVER } =
process.env;

const main = async () => {
const provider = await Provider.create(L2_RPC, { resourceCacheTTL: -1 });

if (!L2_SIGNER) {
L2_SIGNER = await password({ message: 'Enter private key' });
}

const wallet = Wallet.fromPrivateKey(L2_SIGNER, provider);

const proxy = new Proxy(L2_BRIDGE_ID, wallet);
Expand All @@ -43,47 +49,58 @@ const main = async () => {
.proxy_target()
.dryRun()
.then((result) => {
debug('bridge_proxy.target() succeeded, assuming proxy');
debug(`.proxy_target() returned ${result.value.bits}, assuming proxy`);
return result.value.bits;
})
.catch(() => {
debug('bridge.proxy_target() errored, assuming not proxy');
debug('.proxy_target() errored, assuming not proxy');
return null;
});

const predicateRoot = getPredicateRoot(contractMessagePredicate);

let nonce: BN;
let endCursor: string | undefined;

if (L2_MESSAGE_NONCE) nonce = new BN(L2_MESSAGE_NONCE);
else {
const response = await provider.getMessages(predicateRoot);
if (!response.messages || response.messages.length === 0) {
console.log('No messages in the predicate');
return;
else
while (true) {
const response = await provider.getMessages(predicateRoot, {
after: endCursor,
});

if (!response.messages || response.messages.length === 0) {
console.log('No messages in the predicate');
return;
}

const { messages } = response;

const message = messages.find((message) => {
const hex = hexlify(message.data).replace('0x', '');
const recipient = hex.substring(
TOKEN_RECIPIENT_DATA_OFFSET * 2,
TOKEN_RECIPIENT_DATA_OFFSET * 2 + 64 // Recipient is 32 bytes
);
const expectedRecipient = L2_TOKEN_RECEIVER || wallet.address.toB256();

return recipient === expectedRecipient.replace('0x', '');
});

if (!message) {
if (response.pageInfo.hasNextPage) {
endCursor = response.pageInfo.endCursor;
continue;
} else {
console.log('No messages for the recipient');
return;
}
}

nonce = new BN(message.nonce);
break;
}

const { messages } = response;

const message = messages.find((message) => {
const hex = hexlify(message.data).replace('0x', '');
const recipient = hex.substring(
TOKEN_RECIPIENT_DATA_OFFSET * 2,
TOKEN_RECIPIENT_DATA_OFFSET * 2 + 64 // Recipient is 32 bytes
);
const expectedRecipient = L2_TOKEN_RECEIVER || wallet.address.toB256();

return recipient === expectedRecipient.replace('0x', '');
});

if (!message) {
console.log('No messages for the recipient');
return;
}

nonce = new BN(message.nonce);
}

const message = await waitForMessage(
provider,
new Account(predicateRoot).address,
Expand All @@ -105,6 +122,10 @@ const main = async () => {

if (txResult.status === TransactionStatus.success) {
console.log('\t> Transaction succeeded');
console.log(
'\t > Minted asset IDs: ',
txResult.mintedAssets.map((asset) => asset.assetId)
);
} else {
console.log('\t> Transaction errored');
}
Expand Down
Loading

0 comments on commit 5ddd191

Please sign in to comment.