0

I am using a smart contract on the TestRPC net. In order to deploy my contract for the first time I write this in the node 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

In this way I get contract abi and address. The only problem is that evry time I restart TestRPC I have to repeat the above commands...There is a way to avoid this or,at least,make this automatical?

(this is the js script I actually use to use the contract:

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);

EDIT enter image description here

1

Testrpc accept a parameter --db

--db: Specify a path to a directory to save the chain database. If a database already exists, the TestRPC will initialize that chain instead of creating a new one.

  • So i write : "Testrpc -- db/home/ " and it creates a db in that directory?? – Nick9214 Sep 20 '17 at 15:37
  • 1
    You probably also want -d to deterministicaly generate accounts, so everytime you have the same accounts generates. Create a directory mydatadir and then execute testrpc --db mydatadir -d. But remember testrpc is mostly for quick tests if you want a more complex development you probably want to setup a private testnet or use a public testnet like Ropsten. – Ismael Sep 20 '17 at 17:30
  • For what I have to do now,I just need I can use my contract without repeating evry commands evry time – Nick9214 Sep 21 '17 at 12:28
  • I got an error,look at the post – Nick9214 Sep 21 '17 at 14:52
  • 1
    The error say that some other process is using port 8545, likely you have another instance testrpc or geth running. – Ismael Sep 21 '17 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.