send-tokens
A simple CLI tool (and library) for sending Ethereum ERC20 tokens using any of the following:
- A wallet's private key
- A keystore file
- An HD wallet mnemonic phrase
- A provider (node) wallet address
For the ether version of this package, check out
send-ether.
Migrating from 1.x:
Token amounts are now in whole units, by default, which is defined by the number
of decimal places a token contract supports. This means sending 100.123
tokens
with a token that supports 18 decimal places, will actually send
100.123 * 10^18
of the smallest unit of that token. This behavior can be
overridden with the --decimals
option.
Contents
Installation
npm install -g send-tokens
yarn global add send-tokens
Examples
TOKEN='0x1658164265555FA310d20bC601Dd32e9b996A436'
DST='0x0420DC92A955e3e139b52142f32Bd54C6D46c023'
PRIVATE_KEY='0x52c251b9e04740157471a724e9a3210b83fac5834b29c89d5bd57661bd2a7057'
MNEMONIC='butter crepes sugar flour eggs milk ...'
$ send-tokens --key $PRIVATE_KEY $TOKEN $DST 100.53
$ send-tokens --network ropsten --mnemonic "$MNEMONIC" $TOKEN $DST 32 -d 0
$ send-tokens --keystore './path/to/keystore.json' --password 'secret' $TOKEN $DST 99
$ send-tokens --provider 'http://localhost:8545' --confirmations 3 $TOKEN $DST 64.3163512
All Options
$ send-tokens --help
Usage: send-tokens [options] <token> <to> <amount>
Options:
-v, --version output the version number
-d, --decimals <n> decimal places amount is expressed in (default: max token supports)
-k, --key <hex> sending wallet's private key
-f, --key-file <file> sending wallet's private key file
-s, --keystore-file <file> sending wallet's keystore file
--password <password keystore file password
-m, --mnemonic <phrase> sending wallet's HD wallet phrase
--mnemonic-index <n> sending wallet's HD wallet account index (default: 0)
-a, --account <hex> sending wallet's account address (provider wallet)
-c, --confirmations <n> number of confirmations to wait for before returning (default: 0)
-p, --provider <uri> provider URI
-n, --network <name> network name
-G, --gas-price <gwei> explicit gas price, in gwei (e.g., 20)
-l, --log <file> append a JSON log to a file
--no-confirm bypass confirmation
-h, --help output usage information
JSON Logs
If you pass the --log
option, a JSON object describing the transfer
will be appended to a file when the transaction is mined, one object per line.
Log Entries
Log entries follow this structure:
{
id: '88fdd8a4b8084c36',
time: 1532471209842,
token: '0x1658164265555FA310d20bC601Dd32e9b996A436',
from: '0x0420DC92A955e3e139b52142f32Bd54C6D46c023',
to: '0x2621Ea417659Ad69BAE66AF05eBE5788e533E5e8',
amount: '20',
txId: '0xd9255f8365305ebffd77cb30d09f82745eaa232e42739f5fc2788fa46f1347e3',
block: 4912040,
gas: 40120
}
ENS Names
Anywhere you can pass an address, you can also pass an ENS name, like
'ethereum.eth'
, and the it will automatically be resolved to a real
address.
ENS resolution only works on the mainnet, rinkeby, and ropsten, and the name
must be fully registered with the ENS contract and a resolver.
Library Usage
The send-tokens
package can be used as a library through the sendTokens()
function.
sendTokens()
asynchronously resolves to a
transaction receipt
once the transaction has been mined (or confirmed, if the
confirmations
option is > 0).
sendTokens() Examples
const {sendTokens} = require('send-tokens');
const TOKEN_ADDRESS = '0x1658164265555FA310d20bC601Dd32e9b996A436';
const RECIPIENT = '0x0420DC92A955e3e139b52142f32Bd54C6D46c023';
const PRIVATE_KEY = '0x52c251b9e04740157471a724e9a3210b83fac5834b29c89d5bd57661bd2a7057';
let receipt = await sendTokens(TOKEN_ADDRESS, RECIPIENT, '100',
{key: PRIVATE_KEY});
const MNEMONIC = 'butter crepes sugar flour eggs milk ...';
receipt = await sendTokens(TOKEN_ADDRESS, RECIPIENT, '100.312',
{mnemonic: MNEMONIC, confirmations: 3});
const KEYSTORE = '{...}';
const PASSWORD = 'secret';
receipt = await sendTokens(TOKEN_ADDRESS, RECIPIENT, '32', {
keystore: KEYSTORE,
password: PASSWORD,
decimals: 0,
onTxId: console.log
});
Full sendTokens() Options
const {sendTokens} = require('send-tokens');
{tx: Object} = async sendTokens(
TOKEN_ADDRESS: String,
RECIPIENT: String,
TOKEN_AMOUNT: String,
{
quiet: Boolean,
log: String,
onTxId: Function,
decimals: Number,
account: String,
key: String,
mnemonic: String,
mnemonicIndex: Number,
keystore: String,
keystoreFile: String,
password: String,
network: String,
gasPrice: Number,
confirmations: Number,
infuraKey: String,
provider: String | Object,
web3: Object
});
toWallet()
Another exposed library function is toWallet()
, which returns an address
& private key pair from a private key, mnemonic, or keystore. Below are the
full options.
const {toWallet} = require('send-tokens');
{address: String, key: String} = toWallet({
key: String,
mnemonic: String,
mnemonicIndex: Number,
keystore: String,
password: String
});