我在TestRPC网上使用了一个智能合同。为了第一次部署契约,我在节点cmd中编写了以下内容:
>Web3 = require('web3')
> web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
> code = fs.readFileSync('store.sol').toString()
> solc = require('solc')
> compiledCode = solc.compile(code)
> abiDefinition = JSON.parse(compiledCode.contracts[':SimpleStorage'].interface)
> SimpleStorage = web3.eth.contract(abiDefinition)
> byteCode = compiledCode.contracts[':SimpleStorage'].bytecode
> deployedContract = SimpleStorage.new(['Rama'],{data: byteCode, from: web3.eth.accounts[0], gas: 4700000})
> deployedContract.address这样我就能得到合同abi和地址。唯一的问题是,每次我重新启动TestRPC时,我必须重复上面的commands...There是避免这种情况的一种方法,或者至少使这是一种自动化的方法?
(这是我实际使用的用于使用契约的js脚本:
var net = require('net');
var HOST = '127.0.0.1';
var PORT = 10001;
var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider('localhost:8545'));
net.createServer(function(sock) {
sock.setEncoding('utf8');
sock.on('data', function(data) {
console.log('__________');
console.log(data);
console.log('__________');
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
abi = JSON.parse('[{"constant":false,"inputs":[{"name":"x","type":"string"}],"name":"set","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"}]');
SimpleStorage = web3.eth.contract(abi);
contractInstance = SimpleStorage.at('0x08cf472e9192d3c48e03f501472c8aecb9350c16');
contractInstance.set(data, {from: web3.eth.accounts[0], gas: 4700000});
x=contractInstance.get({from: web3.eth.accounts[0], gas: 4700000});
console.log(x);
});
// Aggiungiamo l'event handler 'close' per questa istanza di socket
sock.on('close', function(data) {
});
}).listen(PORT, HOST);
console.log('Server listening on ' + HOST +':'+ PORT);编辑

发布于 2017-09-20 08:53:26
Testrpc接受参数--db
--db:指定一个目录的路径以保存链数据库。如果数据库已经存在,TestRPC将初始化该链,而不是创建新的链。
https://ethereum.stackexchange.com/questions/26759
复制相似问题