From bb71dd1e5c64f0b6e83c0b5d8634da68b893d0c4 Mon Sep 17 00:00:00 2001 From: Ogichain <119630960+ogichain@users.noreply.github.com> Date: Sun, 22 Sep 2024 22:52:37 +0000 Subject: [PATCH] perf and style changes --- contracts/predicate/scripts/run-predicate.ts | 62 +++++++++----------- docker/erc20-deployer/deployer/src/index.ts | 13 +++- 2 files changed, 40 insertions(+), 35 deletions(-) diff --git a/contracts/predicate/scripts/run-predicate.ts b/contracts/predicate/scripts/run-predicate.ts index df31e88d8..eac5716e0 100644 --- a/contracts/predicate/scripts/run-predicate.ts +++ b/contracts/predicate/scripts/run-predicate.ts @@ -15,45 +15,39 @@ if (!NEXT_PUBLIC_FUEL_CHAIN_NAME || !PRIVATE_KEY) { const providerUrl = FUEL_CHAIN.providerUrl; -async function main() { - const binHex = hexlify(await fs.readFile(BIN_PATH)); - const provider = await Provider.create(providerUrl); - const wallet = Wallet.fromPrivateKey(PRIVATE_KEY!, provider); - const { minGasPrice: gasPrice } = wallet.provider.getGasConfig(); - const walletAddress = wallet.address.toB256(); - const abiPath = resolve(__dirname, '../out/debug/predicate-app-abi.json'); - const abi = await fs.readFile(abiPath, 'utf-8'); - const abiJson = JSON.parse(abi); - const predicate = new Predicate(binHex, provider, abiJson); - - console.log('💰 Funding predicate...'); - const tx1 = await wallet.transfer(predicate.address, AMOUNT, BaseAssetId, { - gasPrice, - }); - - const res1 = await tx1.waitForResult(); - const predicateBalance = bn(await predicate.getBalance()); - console.log(`→ Transaction Id: ${res1.id}`); - console.log(`→ Predicate balance: ${predicateBalance.format()}`); - console.log(`→ Predicate Id: ${predicate.address.toB256()}`); - console.log(`📝 Wallet address: ${walletAddress}\n`); - - console.log('⌛️ Running predicate...'); - predicate.setData(walletAddress); +async function loadFile(filePath: string): Promise { + return fs.readFile(filePath, 'utf-8'); +} +async function main() { try { - const tx2 = await predicate.transfer( - wallet.address, - AMOUNT - 150_000, - BaseAssetId, - { - gasPrice, - }, - ); + const binHex = hexlify(await fs.readFile(BIN_PATH)); + const provider = await Provider.create(providerUrl); + const wallet = Wallet.fromPrivateKey(PRIVATE_KEY!, provider); + const { minGasPrice: gasPrice } = await wallet.provider.getGasConfig(); + const walletAddress = wallet.address.toB256(); + const abiPath = resolve(__dirname, '../out/debug/predicate-app-abi.json'); + const abiJson = JSON.parse(await loadFile(abiPath)); + const predicate = new Predicate(binHex, provider, abiJson); + + console.log('💰 Funding predicate...'); + const tx1 = await wallet.transfer(predicate.address, AMOUNT, BaseAssetId, { gasPrice }); + const res1 = await tx1.waitForResult(); + const predicateBalance = bn(await predicate.getBalance()); + + console.log(`→ Transaction Id: ${res1.id}`); + console.log(`→ Predicate balance: ${predicateBalance.format()}`); + console.log(`→ Predicate Id: ${predicate.address.toB256()}`); + console.log(`📝 Wallet address: ${walletAddress}\n`); + + console.log('⌛️ Running predicate...'); + predicate.setData(walletAddress); + + const tx2 = await predicate.transfer(wallet.address, AMOUNT - 150_000, BaseAssetId, { gasPrice }); const res2 = await tx2.waitForResult(); console.log(`→ Transaction Id: ${res2.id}`); } catch (error: any) { - console.error(error?.response?.errors?.[0].message); + console.error(error?.response?.errors?.[0]?.message || error.message); } } diff --git a/docker/erc20-deployer/deployer/src/index.ts b/docker/erc20-deployer/deployer/src/index.ts index 94621ad25..9eff95327 100644 --- a/docker/erc20-deployer/deployer/src/index.ts +++ b/docker/erc20-deployer/deployer/src/index.ts @@ -6,6 +6,7 @@ import { setupEnvironment, } from '@fuel-bridge/test-utils'; +// Environment variables const { PORT, L1_CHAIN_HTTP, @@ -16,24 +17,32 @@ const { const APP_PORT = PORT || 9090; async function main() { + // Setup environment const env = await setupEnvironment({ http_ethereum_client: L1_CHAIN_HTTP, http_fuel_client: FUEL_GRAPHQL_ENDPOINT, http_deployer: DEPLOYMENTS_HTTP, pk_eth_signer2: PK_ETH_WALLET, }); + + // Deploy or get existing ERC20 contract const ETHToken = await getOrDeployECR20Contract(env); + + // Deploy or get existing L2 bridge const { contract, implementation } = await getOrDeployL2Bridge( env, env.eth.fuelERC20Gateway, ); + // Set asset issuer ID const fuelContractId = contract.id.toHexString(); await env.eth.fuelERC20Gateway.setAssetIssuerId(fuelContractId); + // Get ERC20 address and token ID const erc20Address = (await ETHToken.getAddress()).toLowerCase(); const tokenId = getTokenId(contract, erc20Address); + // Start the server with deployment details await startServer({ ETH_ERC20: erc20Address, FUEL_TokenContract: fuelContractId, @@ -45,11 +54,12 @@ async function main() { function startServer(deployments: Record) { return new Promise((resolve) => { createServer((req, res) => { - // add cors headers + // Add CORS headers res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Request-Method', '*'); res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET'); res.setHeader('Access-Control-Allow-Headers', '*'); + if (req.method === 'OPTIONS') { res.writeHead(200); res.end(); @@ -74,6 +84,7 @@ function startServer(deployments: Record) { }); } +// Start the main function main() .then(() => { console.log(`Server listening on port ${APP_PORT}`);