2

I want to store a string from a script in node.js to blockchain. This is the contract.

pragma solidity ^0.4.0;

contract SimpleStorage {
    string storedData;

    function set(string x) {
        storedData = x;
    }

    function get() constant returns (string) {
        return storedData;
    }
}

In order to deploy the contract on net(in my case testnet) I need to create an instance of the contract. In order to create an instance and deploy it I need 3 things: web3 library, abi and address of contract:

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
abi = JSON.parse(....)  //==> I got abi following this tutorial(https://medium.com/@mvmurthy/full-stack-hello-world-voting-ethereum-dapp-tutorial-part-1-40d2d0d807c2)
StoreContract = web3.eth.contract(abi);
contractInstance = VotingContract.at('CONTRACT ADDRESS');
//And now I can use contract function
contractInstance.set(string data)

So my question is....How do I get the contract address???

  • In the posted code here contractInstance = VotingContract.at('CONTRACT ADDRESS'); it should be StoreContract not VotingContract – Achala Dissanayake Sep 15 '17 at 18:39
0

You need address and ABI to invoke a contract. Before invoking a contract you need to deploy it. You have only got the abi definition. You need to deploy the contract by letting it be mined and get the address. Use,

var simpleStorage = StoreContract.new(your_parameters ,{from:web3.eth.accounts[0], data: compiled_code , gas: gas_amount}, call_back_function)

Once the contract is mined, it will return something like this,

Contract mined! address: contract_address 

where you can find the contract address at contract_address

Refer the basic greeter example provided in the Ethereum web site for a detailed explanation on beginning with smart contracts.

UPDATE-1:

If you carefully read the blog post you referred, it's clearly given that you need to send a transaction for the contract to be deployed.(it has some nice diagrams too)

This is the code snippet from the blog post,

abiDefinition = JSON.parse(compiledCode.contracts[':Voting'].interface)
VotingContract = web3.eth.contract(abiDefinition)
byteCode = compiledCode.contracts[':Voting'].bytecode
  // the following line is what you need to do get the address
deployedContract = VotingContract.new(['Rama','Nick','Jose'],{data: byteCode, from:
web3.eth.accounts[0], gas: 4700000}) 
deployedContract.address
contractInstance = VotingContract.at(deployedContract.address)

and from what you posted,

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
abi = JSON.parse(....)  //==> I got abi following this tutorial(https://medium.com/@mvmurthy/full-stack-hello-world-voting-ethereum-dapp-tutorial-part-1-40d2d0d807c2)
//at this point you need to send the transaction using "StoredContract.new(params,transaction details, callback[optional]);"  
StoreContract = web3.eth.contract(abi);
contractInstance = VotingContract.at('CONTRACT ADDRESS');

You don't need a address to deploy a contract, you just need to send a transaction with the contract details. Once the contract is deployed(after the transaction is mined) it will output the address which you can use to call the particular instance.

UPDATE-2:

You may deploy many instance of the same contract by sending transactions and they are stored at different addresses. You can use a particular instance using the respective address (eg: to call a function like setter in your case). Refer the following answer posted here.

Just like code can be in source form, or converted/compiled to executable form, the same thing applies to contracts.

From source code, many copies of the executable form can be generated.

In source code form, a contract in Solidity is like a class in OOP terms: it can be instantiated many times, and can be inherited from.

Making a contract executable, usually involves 2 steps:

converting the source to Ethereum Virtual Machine (EVM) bytecode deploying the EVM to an Ethereum blockchain The same contract source code or EVM can be deployed to a blockchain many times. Each instance of a deployed contract has a unique address.

  • Ok,I'll try,thanks.Just last question: is the contract ok?I red somewhere it could be problem with string parameter – Nick9214 Sep 14 '17 at 16:43
  • No wait..I have a doubt..I need contract address to deploy my contract,but in order to have contract address first I have to deploy my contract??I am a little bit confused – Nick9214 Sep 14 '17 at 16:47
  • I am using TestRPC fwi – Nick9214 Sep 14 '17 at 16:52
  • I think it's clearly mentioned in the blog post you referred here . let me update the answer relating the blog post. – Achala Dissanayake Sep 14 '17 at 17:43
  • So...(just to be sure I've understood you)once deployed,I can use that address to create new istance of my contract? – Nick9214 Sep 14 '17 at 18:31

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.