forked from connext/indra
-
Notifications
You must be signed in to change notification settings - Fork 0
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
4815a87
commit 8092ba7
Showing
4 changed files
with
81 additions
and
2 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
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,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); | ||
}, | ||
}; |
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
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,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" |