1、安装
brew install node
npm install -g ethereumjs-testrpc
2、使用
testrpc <options>
options:
-a或-accounts。指定启动时生成多个多个账户
➜ ~ testrpc -a 1
EthereumJS TestRPC v6.0.3 (ganache-core: 2.0.2)
Available Accounts
==================
(0) 0x566ae79bc4998ca7d35da3dbd46dfdb44582fa78
Private Keys
==================
(0) bd3da9fe19e9090db7e3bf21d54b5772689ac7ede8fff5c4eb5bdb89dfe22daf
HD Wallet
==================
Mnemonic: boring memory robust crop jump play polar leaf fetch sound make arrow
Base HD Path: m/44'/60'/0'/0/{account_index}
Listening on localhost:8545
-b或--blocktime。设置每个多少秒生成一个区块,用于自动挖矿。默认0,不会自动挖矿。
testrpc -b 1
EthereumJS TestRPC v6.0.3 (ganache-core: 2.0.2)
Available Accounts
==================
(0) 0x67ff216743647ea9b7205ff0267da451d63a7dcd
(1) 0x450f2896c47c6e8763b6d389c40166584d0ced40
(2) 0x09d58801bc4175425bf23cf42c645c250ac91645
(3) 0x7383fa0f1997fdcec3ff651d9957d374e8ffb226
(4) 0x1f16340cbc8a644079e3bae3138d2071f278b6e7
(5) 0x84c9517725d67c652265f3ec78fe9476916e2367
(6) 0xeffbf37de26be67be6a71fd1064ce62f6bc501f6
(7) 0xa13cc62fa4dd7efc52e796fa6a982f6e4c603307
(8) 0x05b70cb3505590adb1940abde54c530b555a4dbc
(9) 0x454dec238cb8d07ffd32a5b2256ec02102941d85
Private Keys
==================
(0) 9fa86667fec95e1c9cfa7ff48fe8957a73fc1522f9d0c7a75f0ce6992be83086
(1) 104476884731317f65abc25ae8352fb3856d96917d618466bbbf1d0f8be3c25e
(2) e9b99526c6cdfee041ea4161044d7ffcdf9f4dc0d7f4ccfb25c72c45d6970a2a
(3) 2dbc3f0e106f7377059b75819e81d0bb8d655c56eb6cd992c0752025e1b64789
(4) 7c194ed334848597fc83ac3c6c80c9cfd3127297a2efe7f09d779c38fcf16a7c
(5) 0530a0e82ff5d6a216b6f1b6c42cdbca8895a886dbeaa5944dac60ca62b58fb3
(6) 84c812e3327c07f1716b7a993e2b96c9e3cd77894a9bb4f6e313f4ba8432783a
(7) b663ce4d3542a14656fb096c27ec449a9c4e5a72d20071e3d3594cadf9787617
(8) 140989ed8dc4b508c138a9368da31eb5dfa0b1cbb4479e50de48287766d4cd35
(9) 856708d8b84d8fc952daca9095cfb7d31a40a1a9c9c7670fe0855266b4052b8e
HD Wallet
==================
Mnemonic: paper pride pencil treat dilemma sniff august reward immense idea matrix peasant
Base HD Path: m/44'/60'/0'/0/{account_index}
Listening on localhost:8545
-d或--deterministic。产生一个基于之前定义的助记词符的确定地址。你不需要关心之前定义的助记词符是什么,如果你使用了testrpc -d ,那么你每次启动testrpc都会使用同一个助记词符
➜ ~ testrpc -a 1 -d
EthereumJS TestRPC v6.0.3 (ganache-core: 2.0.2)
Available Accounts
==================
(0) 0x90f8bf6a479f320ead074411a4b0e7944ea8c9c1
Private Keys
==================
(0) 4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d
HD Wallet
==================
Mnemonic: myth like bonus scare over problem client lizard pioneer submit female collect
Base HD Path: m/44'/60'/0'/0/{account_index}
Listening on localhost:8545
-n或--secure默认锁上所有可用的账户(有利于第三方的交易)
-m或--mnemonic使用一个指定的分层确定钱包助记词符(HD wallet mnemonic)来生成初始的地址
➜ ~ testrpc -a 1 -m "squirrel shield mass midnight aim ocean sudden brave bounce swamp attract tower"
EthereumJS TestRPC v6.0.3 (ganache-core: 2.0.2)
Available Accounts
==================
(0) 0xd4ce85753a9245ec87f9ddb4f10feef237a8b922
Private Keys
==================
(0) 3b5d26302347447b47ae251c00102e99cf6d44dc3bb48adf0b0a0be152ee988e
HD Wallet
==================
Mnemonic: squirrel shield mass midnight aim ocean sudden brave bounce swamp attract tower
Base HD Path: m/44'/60'/0'/0/{account_index}
Listening on localhost:8545
-p或--port。用于监听的端口。默认8545
➜ ~ testrpc -a 1 -d -p 9999
EthereumJS TestRPC v6.0.3 (ganache-core: 2.0.2)
Available Accounts
==================
(0) 0x90f8bf6a479f320ead074411a4b0e7944ea8c9c1
Private Keys
==================
(0) 4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d
HD Wallet
==================
Mnemonic: myth like bonus scare over problem client lizard pioneer submit female collect
Base HD Path: m/44'/60'/0'/0/{account_index}
Listening on localhost:9999
-h 或--hostname。用于设置监听的域名,默认是localhost
-s 或--seed 使用任意的数据生成分层确定钱包助记词符。种子是一串由随机数生成器生成的随机数。这串随机数可以用力啊生成钱包中不同账户的公私钥对
➜ ~ testrpc -a 1 -s '11'
EthereumJS TestRPC v6.0.3 (ganache-core: 2.0.2)
Available Accounts
==================
(0) 0x542d03ebb5f091bf2b08b5766ed896b62310d92a
Private Keys
==================
(0) c2ad48afbef7564d65589afce4c6e0e02e902ca46a79d259add7a31e5f5a1cda
HD Wallet
==================
Mnemonic: donor curve veteran risk blind morning jewel sock quarter rescue security biology
Base HD Path: m/44'/60'/0'/0/{account_index}
Listening on localhost:8545
-g或--gasPrice 自定义gas价格(默认20000000000)
-l或--gasLimit 自定义gas限额(默认0x47E7C4)
-f 或--fork。从另一个当前正在运行着的以太坊客户端所给的区块编号开始分叉。参数值应该是另一个客户端的HTTP访问地址和端口号,如:http://localhost:8545.你可以使用@来指定区块编号:http://localhost:8545@1223131
testrpc -a 1 -p 9999 -f http://localhost:8545@16
-i或--network-id。指定网络id,TestRPC将用来识别它自己(默认当前时间,或如果配置了分叉网路的话,那么使用分叉网络的id)
--db指定一个保存区块数据的目录路径。如果目录中已经存在一个数据库了,TestRPC会初始化那个链,而不再创建一个新链了。
--debug 输出用于debug的虚拟机操作码(opcodes)
--mem。输出TestRPC的内存使用统计。这会替换正常的输出
特殊options
--account 设置--account=···(注意account后面没有‘s’)使用私钥和他们的余额来生成初始账号
testrpc --account="<privatekey>,balance"
testrpc --account="0x590f47c8d2aac75460c08cd761fa81559e2053ce608f9f4fb68c27a73cb86379,1000" --account="0x590f47c8d2aac75460c08cd761fa81559e2053ce608f9f4fb68c27a73cb86379,1000"
-u或--unlock 申明--unlock···多次,在解锁账户时既可以想--unlock···传入公钥地址也可以传入账户的索引号。当结合-secure使用时--unlock会覆盖指定账户的锁定状态
testrpc --secure --unlock “0x1234...” --unlock “0x1234...”
也可以指定索引值来解锁账户:
testrpc --secure -u 0 -u 1
3类(Library)
testrpc也可以作为一个类库使用
(1)当做一个Web3 provider
var TestPRC = require("ethereumjs-testrpc");
web3.setProvider(TestPRC.provider());
(2)当做一个普通的http服务
var TestPRC = require("ethereumjs-testrpc");
var server = TestRPC.server();
server.listen(port,function(err,blockchain){...});
.provider()和.server()都会返回一个对象,通过这个返回的对象你可以设置testrpc的行为,对象中的参数可选的,可用的参数如下:
accounts。数组对象,每个对象需要有个balance的键,键对应的值用十六进制表示,通用secreKey键也必须要指明,它表示账户的私钥。如果没有指名secretkey就会自动生成一个
const web3= require(‘web3’)
const TestRPC = require(‘ethereumjs-testrpc’)
const web3 = new Web3()
//设置参数
web3.setProvider(TestRPC.provider({
port:9999
accounts[
{balance,'0x12'},
{balance,'0x12'}
]
}))
web3.eth.getAccounts(err,result) =>{
if (err){console.log(err)
console.log(result)
}
}