diff --git a/subnet/deployment-generator/src/faucet.js b/subnet/deployment-generator/src/faucet.js new file mode 100644 index 0000000..f1f1199 --- /dev/null +++ b/subnet/deployment-generator/src/faucet.js @@ -0,0 +1,71 @@ +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(); +} +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)); +}