From 172e32507c6b7eb12ac1164a2efe5a142a2665cb Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Fri, 13 Sep 2024 13:43:00 +0400 Subject: [PATCH] add faucet script --- subnet/deployment-generator/src/faucet.js | 73 +++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 subnet/deployment-generator/src/faucet.js diff --git a/subnet/deployment-generator/src/faucet.js b/subnet/deployment-generator/src/faucet.js new file mode 100644 index 0000000..31eec88 --- /dev/null +++ b/subnet/deployment-generator/src/faucet.js @@ -0,0 +1,73 @@ +process.chdir(__dirname); +const fs = require("fs"); +const ethers = require("ethers"); +const path = require("path"); +const { error } = require("console"); + +const usage = + "Usage: SUBNET_URL= node faucet.js "; +const subnetURL = process.env.SUBNET_URL || ""; +if (subnetURL == "") { + console.log("Invalid SUBNET_URL"); + console.log(usage); + process.exit(); +} +console.log("RPC url:", subnetURL) + +const inputs = process.argv; + +processTransfer(inputs); + +async function processTransfer(inputs) { + try { + fromPK = inputs[2]; + toAddress = inputs[3]; + amount = inputs[4]; + const provider = new ethers.JsonRpcProvider(subnetURL); + const fromWallet = new ethers.Wallet(fromPK, provider); + let tx = { + to: toAddress, + value: ethers.parseEther(amount), + }; + let sendPromise = fromWallet.sendTransaction(tx); + txHash = await sendPromise.then((tx) => { + return tx.hash; + }); + console.log("TX submitted, confirming TX execution, txhash:", txHash) + + let receipt; + let count = 0; + while (!receipt) { + count++; + // console.log("tx receipt check loop", count); + if (count > 60) { + throw Error("Timeout: transaction did not execute after 60 seconds"); + } + await sleep(1000); + let receipt = await provider.getTransactionReceipt(txHash); + if (receipt && receipt.status == 1) { + console.log( + "Successfully transferred", + amount, + "subnet token" + ); + let fromBalance = await provider.getBalance(fromWallet.address); + fromBalance = ethers.formatEther(fromBalance); + let toBalance = await provider.getBalance(toAddress); + toBalance = ethers.formatEther(toBalance); + console.log("Current balance"); + console.log(fromWallet.address + ":", fromBalance); + console.log(toAddress + ":", toBalance); + process.exit(); + } + } + } catch (error) { + console.log(error); + console.log(usage); + process.exit(); + } +} + +function sleep(ms) { + return new Promise((resolve) => setTimeout(resolve, ms)); +}