Skip to content

Commit

Permalink
add faucet script
Browse files Browse the repository at this point in the history
  • Loading branch information
wanwiset25 committed Sep 13, 2024
1 parent 52b2e57 commit 1b7be2d
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions subnet/deployment-generator/src/faucet.js
Original file line number Diff line number Diff line change
@@ -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=<subnet_url> node faucet.js <from wallet pk> <to wallet pubkey> <token amount>";
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));
}

0 comments on commit 1b7be2d

Please sign in to comment.