首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TestRPC与数据持久化

TestRPC与数据持久化
EN

Ethereum用户
提问于 2017-09-20 07:49:28
回答 1查看 318关注 0票数 0

我在TestRPC网上使用了一个智能合同。为了第一次部署契约,我在节点cmd中编写了以下内容:

代码语言:javascript
复制
>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脚本:

代码语言:javascript
复制
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);

编辑

EN

回答 1

Ethereum用户

回答已采纳

发布于 2017-09-20 08:53:26

Testrpc接受参数--db

--db:指定一个目录的路径以保存链数据库。如果数据库已经存在,TestRPC将初始化该链,而不是创建新的链。

票数 1
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/26759

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档