Skip to content

Commit

Permalink
some more changes for offline mode
Browse files Browse the repository at this point in the history
* add option to load privkey from environment variable
* add option to generate a random privkey
* add option to output a distribution summary
  • Loading branch information
pk910 committed Mar 11, 2023
1 parent 0559777 commit 0660222
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 6 deletions.
60 changes: 58 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "testnet-funding-tool",
"version": "1.0.6",
"version": "1.0.7",
"description": "",
"main": "index.js",
"scripts": {
Expand All @@ -14,6 +14,7 @@
"command-line-args": "^5.2.1",
"command-line-usage": "^6.1.3",
"ethereumjs-util": "^7.1.5",
"ethereumjs-wallet": "^1.0.2",
"eventsource": "^2.0.2",
"nexe": "^4.0.0-beta.16",
"node-fetch": "^2.6.7",
Expand Down
67 changes: 64 additions & 3 deletions src/funding-tool.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const Web3 = require('web3');
const EthTx = require('@ethereumjs/tx');
const EthCom = require('@ethereumjs/common');
const EthUtil = require('ethereumjs-util');
const EthWallet = require('ethereumjs-wallet');

const distributorContract = require("../Contracts/Distributor.json");

Expand Down Expand Up @@ -41,11 +42,16 @@ const optionDefinitions = [
},
{
name: 'privkey',
description: 'The private key of the wallet to send funds from.',
description: 'The private key of the wallet to send funds from.\n(Special: "env" to read from FUNDINGTOOL_PRIVKEY environment variable)',
alias: 'p',
type: String,
typeLabel: '{underline privkey}',
},
{
name: 'random-privkey',
description: 'Use random private key if no privkey supplied',
type: Boolean,
},
{
name: 'maxpending',
description: 'The maximum number of parallel pending transactions.',
Expand Down Expand Up @@ -94,6 +100,12 @@ const optionDefinitions = [
type: String,
typeLabel: '{underline txlist.txt}',
},
{
name: 'summary',
description: 'Output summary of distribution to file.',
type: String,
typeLabel: '{underline summary.txt}',
},
{
name: 'chainid',
description: 'ChainID of the network (For offline mode in combination with --output)',
Expand All @@ -116,6 +128,11 @@ var wallet = null;
var fundings = [];
var pendingQueue = [];
var distributor = null;
var stats = {
transferCount: 0,
transactionCount: 0,
totalAmount: BigInt(0),
};

main();

Expand All @@ -125,14 +142,14 @@ async function main() {
return;
}

if(!options['privkey']) {
var walletKey = loadPrivateKey();
if(!walletKey) {
printHelp();
console.log("No wallet privkey specified.");
console.log("");
return;
}

var walletKey = Buffer.from(options['privkey'], "hex");
var walletAddr = EthUtil.toChecksumAddress("0x"+EthUtil.privateToAddress(walletKey).toString("hex"));
wallet = {
privkey: walletKey,
Expand Down Expand Up @@ -163,6 +180,10 @@ async function main() {
return;
}

if(options['output'] && fs.existsSync(options['output'])) {
fs.unlinkSync(options['output']);
}

if(options['output'] && options['chainid']) {
// use in offline mode
web3 = new Web3();
Expand All @@ -179,6 +200,21 @@ async function main() {
await Promise.all(pendingQueue.map((entry) => entry.promise));

console.log("fundings complete!");

if(options['summary']) {
let summary = [
"WalletAddress: " + wallet.addr,
"TotalAmountWei: " + stats.totalAmount.toString(),
"TotalAmountEth: " + weiToEth(stats.totalAmount),
"TransactionCount: " + stats.transactionCount,
];
if(distributor) {
summary.push("TransferCount: " + stats.transferCount);
summary.push("DistributorAddr: " + distributor.addr);
}

fs.writeFileSync(options['summary'], summary.join("\n"));
}
}

function printHelp() {
Expand All @@ -204,6 +240,21 @@ function weiToEth(wei) {
return parseInt((wei / 1000000000000000n).toString()) / 1000;
}

function loadPrivateKey() {
if(options['privkey'] === "env" && (process.env.FUNDINGTOOL_PRIVKEY || "").match(/^[0-9a-f]{64}$/i)) {
return Buffer.from(process.env.FUNDINGTOOL_PRIVKEY, "hex");
}
if(options['privkey'] && options['privkey'].match(/^[0-9a-f]{64}$/i)) {
return Buffer.from(options['privkey'], "hex");
}
if(options['random-privkey']) {
let wallet = EthWallet.default.generate();
return Buffer.from(wallet.getPrivateKeyString().replace(/^0x/, ""), "hex");
}

return null;
}

function loadFundingFile(resArray, fundingList) {
var fundingLine, cmtPos, fundingEntry;
for(var i = 0; i < fundingList.length; i++) {
Expand Down Expand Up @@ -302,6 +353,10 @@ async function processFunding(address, amount) {
var txres = await publishTransaction(txhex);
console.log(" tx hash: " + txres[0]);

stats.transferCount++;
stats.transactionCount++;
stats.totalAmount += amount;

var txobj = {
nonce: wallet.nonce,
hash: txres[0],
Expand Down Expand Up @@ -399,6 +454,8 @@ async function deployDistributor() {
return distributorState.contractAddr;
}

stats.transactionCount++;

var nonce = wallet.nonce;
var rawTx = {
nonce: nonce,
Expand Down Expand Up @@ -434,6 +491,7 @@ async function processFundingBatch(batch) {
batch.forEach((entry) => {
console.log("process funding " + entry.address + ": " + weiToEth(entry.amount) + " ETH");
totalAmount += entry.amount;
stats.transferCount++;
});

if(totalAmount > wallet.balance && !wallet.offline) {
Expand Down Expand Up @@ -464,6 +522,9 @@ async function processFundingBatch(batch) {
var txres = await publishTransaction(txhex);
console.log(" tx hash: " + txres[0]);

stats.transactionCount++;
stats.totalAmount += totalAmount;

var txobj = {
nonce: nonce,
hash: txres[0],
Expand Down

0 comments on commit 0660222

Please sign in to comment.