Testrpc

TestRPC 和數據的持久化

  • September 21, 2017

我在 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 時我都必須重複上述命令……有一種方法可以避免這種情況,或者至少可以自動完成?

(這是我實際使用合約的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);

編輯 在此處輸入圖像描述

Testrpc 接受一個參數--db

--db:指定保存鏈數據庫的目錄路徑。如果數據庫已經存在,TestRPC 將初始化該鏈而不是創建一個新鏈。

引用自:https://ethereum.stackexchange.com/questions/26759