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