Skip to content

Commit

Permalink
add drip cli function
Browse files Browse the repository at this point in the history
  • Loading branch information
LayneHaber committed Jul 1, 2020
1 parent 4815a87 commit 8092ba7
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
5 changes: 3 additions & 2 deletions modules/contracts/src.ts/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import { migrateCommand } from "./commands/migrate";
import { newTokenCommand } from "./commands/new-token";
import { snapshotCommand } from "./commands/snapshot";
import { useTokenCommand } from "./commands/use-token";
import { dripCommand } from "./commands/drip";

yargs
.command(dripCommand)
.command(fundCommand)
.command(migrateCommand)
.command(newTokenCommand)
.command(snapshotCommand)
.command(useTokenCommand)
.demandCommand(1, "Choose a command from the above list")
.help()
.argv;
.help().argv;
64 changes: 64 additions & 0 deletions modules/contracts/src.ts/commands/drip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Wallet, Contract, constants, utils } from "ethers";
import { Argv } from "yargs";

import { ConnextToken } from "../artifacts";
import { cliOpts } from "../constants";
import { getProvider } from "../utils";
import { getAddressBook } from "../address-book";

const { EtherSymbol } = constants;
const { formatEther } = utils;

export const drip = async (
recipient: Wallet, // wallet to send drip tx
addressBookPath: string,
): Promise<void> => {
if (!recipient || !addressBookPath) {
throw new Error("Missing required arguments");
}
const dripAttempt = async () => {
const chainId = (await recipient.provider.getNetwork()).chainId;
const addressBook = getAddressBook(addressBookPath, chainId.toString());
const tokenAddress = addressBook.getEntry("Token").address;
// NOTE: ConnextToken has drippable abi
const token = new Contract(tokenAddress, ConnextToken.abi, recipient);

// Log existing balances
const ercBal0 = `CXT ${formatEther(await token.balanceOf(recipient.address))} tokens`;
const ethBal0 = `${EtherSymbol} ${formatEther(await recipient.getBalance())}`;
console.log(`Balances before funding: ${ercBal0} | ${ethBal0}`);

const tx = await token.functions.drip();
console.log(`Dripping tokens to ${recipient} via tx ${tx.hash}`);
await recipient.provider.waitForTransaction(tx.hash);
const ercBal1 = `CXT ${formatEther(await token.balanceOf(recipient.address))}`;
const ethBal1 = `${EtherSymbol} ${formatEther(await recipient.getBalance())}`;
console.log(`Tx mined! New balances: ${ercBal1} | ${ethBal1}`);
};

try {
await dripAttempt();
} catch (e) {
if (e.message.includes("the tx doesn't have the correct nonce")) {
console.warn(`Wrong nonce, let's try one more time.`);
await dripAttempt();
} else {
throw e;
}
}
};

export const dripCommand = {
command: "drip",
describe: "Drip tokens to sender address",
builder: (yargs: Argv) => {
return yargs
.option("a", cliOpts.addressBook)
.option("k", cliOpts.privateKey)
.option("p", cliOpts.ethProvider)
.demandOption(["k", "p"]);
},
handler: async (argv: { [key: string]: any } & Argv["argv"]) => {
await drip(new Wallet(argv.privateKey, getProvider(argv.ethProvider)), argv.addressBook);
},
};
5 changes: 5 additions & 0 deletions modules/contracts/src.ts/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@ export const cliOpts = {
description: "The address of the token",
type: "string",
},
privateKey: {
alias: "private-key",
description: "An ethereum private key",
type: "string",
},
} as { [key: string]: Options };
9 changes: 9 additions & 0 deletions ops/drip.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
set -e

# Get env vars (defaults correspond to ganache funder)
private_key="${1:-"0xc87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3"}"
provider="${2:-"http://localhost:8545"}"
address_book="${3:-"./modules/contracts/address-book.json"}"

node modules/contracts/dist/src.ts/cli.js drip --private-key="$private_key" --eth-provider="$provider" --address-book="$address_book"

0 comments on commit 8092ba7

Please sign in to comment.