-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52b2e57
commit 172e325
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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=<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(); | ||
} | ||
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)); | ||
} |