1

I have a private blockchain.

I am creating a contract and then I am calling a function that returns the main account. I have the error calling this function.

This is the contract creation code:

var browser_untitled1_sol_mytokenContract = web3.eth.contract([{"constant":false,"inputs":[],"name":"GetMain","outputs":[{"name":"resultMain","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"GetBalance","outputs":[{"name":"result","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[],"payable":false,"type":"constructor"}]);
var browser_untitled1_sol_mytoken = browser_untitled1_sol_mytokenContract.new(
   {
     from: web3.eth.accounts[0], 
     data: '0x6060604052341561000f57600080fd5b5b33600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506301406f406000819055505b5b6101238061006d6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063266b1c72146047578063f8f8a912146099575b600080fd5b3415605157600080fd5b605760bf565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341560a357600080fd5b60a960ea565b6040518082815260200191505060405180910390f35b6000600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1690505b90565b6000805490508090505b905600a165627a7a72305820d65d41c2db3b45a3505ad0645f63c88098090cacaedf6000af2c10ba2dab8c720029', 
     gas: '4700000'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
 })

I call the function browser_untitled1_sol_mytoken.GetMain(); to get the main address and get the error:

Error: invalid address
    at web3.js:3879:15
    at web3.js:3705:20
    at web3.js:4948:28
    at map (<native code>)
    at web3.js:4947:12
    at web3.js:4973:18
    at web3.js:4998:23
    at web3.js:4061:16
    at apply (<native code>)
    at web3.js:4147:16
  • have you got the message Contract mined! address:.........? when you ve deployed your contract – Badr Bellaj Jul 5 '17 at 12:32
  • @Badr Bellaj >Yes I have the confirmation : >loadScript("Contrato4.js") null [object Object] true > null [object Object] Contract mined! address: 0x28affe3b07d6448ae6dbf232ec56f070bf91d036 transactionHash: 0x96a918c45d7292b02b9fbd50d0d5ee527ff7efa7f2c87d61aa8dd179df12f19c – Alienado Jul 5 '17 at 14:41
0

I believe, this line is in error:

 }, function (e, contract){

You don't get the contract address from contract.new(). You get the transaction hash. Then, as Badr says, you have to wait for the transaction to be mined.

I've had good luck with this library: https://gist.github.com/xavierlepretre/88682e871f4ad07be4534ae560692ee6

You just include the code in your JS and then when you send transactions, you follow up with (uses promises which are consistent with Truffle):

.then(function (txnHash) {
  return web3.eth.getTransactionReceiptMined(txnHash);
})
.then

It's readable and reliable.

Hope it helps.

  • I undrestand your point, but after I had the confirmation that it was mined. I can run >browser_untitled1_sol_mytoken.address "0x1e9e7465e394ae1b39344eaf81aea9393d4a690e" I can also get the contract details by running: `> browser_untitled1_sol_mytoken – Alienado Jul 5 '17 at 16:36
  • Unless I misunderstand the question, the difference is you are waiting for the transaction to be mined while the script is not waiting. It's trying to txnHash.address which is undefined. – Rob Hitchens - B9lab Jul 5 '17 at 18:42
  • Finally I found the issue. I had to add constant within the function to get that working. >browser_untitled1_sol_mytoken.GetBalance(); 21000000 – Alienado Jul 5 '17 at 22:42
  • Can someone explain what constant does within the function declaration? >function GetBalance()constant returns(uint256 result) { result=balance; return result; } – Alienado Jul 5 '17 at 22:46
  • Constant is read-only operation. it runs locally. It doesn't update the chain and it can't make a state change no matter what the function says. It returns return values instead of a transaction hash because the result is available without waiting for the network to decide what happened. – Rob Hitchens - B9lab Jul 5 '17 at 23:33
0

Try to set the default account and then execute:

web3.eth.defaultAccount = web3.eth.accounts[0]

Hope this helps...!

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.