Created
April 3, 2018 16:05
-
-
Save jtakalai/b4fab4c3e6bd5a7a0dc17f8bb88bed11 to your computer and use it in GitHub Desktop.
Homegrown Ethereum console
This file contains hidden or 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
// Helper functions for an "ethereum console", similar to geth --attach | |
var _ = require("lodash") | |
var Table = require("cli-table") | |
var BN = require("bignumber.js") | |
var Web3 = require("web3") | |
var web3 = new Web3() | |
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545')) | |
const repl = require('repl') | |
const ctx = repl.start().context | |
ctx.web3 = web3 | |
ctx.lo = _ | |
ctx.e = web3.eth | |
ctx.N = x => new BN(x) | |
ctx.B = function (i) { return web3.eth.getBalance(web3.eth.accounts[i]).toString() } | |
ctx.b = function (i) { return web3.fromWei(web3.eth.getBalance(web3.eth.accounts[i]), "ether").toString() } | |
ctx.BB = function () { return web3.eth.accounts.map((a) => { return web3.eth.getBalance(a) }) } | |
ctx.bb = function () { return web3.eth.accounts.map((a) => { return web3.fromWei(web3.eth.getBalance(a), "ether") }) } | |
ctx.move = function (fromI, toI, eth) { | |
const args = { | |
from: web3.eth.accounts[fromI], | |
to: web3.eth.accounts[toI], | |
value: web3.toWei(eth, "ether") | |
} | |
return web3.eth.sendTransaction(args) | |
} | |
ctx.list = function () { | |
const balances = ctx.bb() | |
const n = web3.eth.accounts.length | |
const rows = _.zip(_.range(n), web3.eth.accounts, balances) | |
var table = new Table({ | |
head: ["#", "Address", "ETH"] | |
}) | |
table.length = n | |
_.assign(table, rows) | |
console.log(table.toString()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment