1

I have setup my private blockchain for testing. And created and deployed following contract:

pragma solidity 0.4.11;

contract Multiply7 {
   event Print(uint);
   function multiply(uint input) returns (uint) {
      Print(input * 7);
      return input * 7;
   }
}

Generated abi and code using solc:

6060604052341561000c57fe5b5b60df8061001b6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063c6888fa114603a575bfe5b3415604157fe5b60556004808035906020019091905050606b565b6040518082815260200191505060405180910390f35b60007f24abdb5865df5079dcc5ac590ff6f01d5c16edbc5fab4e195d9febd1114503da600783026040518082815260200191505060405180910390a16007820290505b9190505600a165627a7a7230582089c25c3ddf32ed3ef55436bff071176b9b807c9cac456721766027c17261657e0029

ABI here:

[{"constant":false,"inputs":[{"name":"input","type":"uint256"}],"name":"multiply","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"","type":"uint256"}],"name":"Print","type":"event"}]

Deployed contract and got transaction receipt:

eth.sendTransaction({from: eth.accounts[0], data: code})
contractAddress = eth.getTransactionReceipt('0x2d1483f05c0420284ccc4f1ad4003466cc6b51e119c1009d6edfd966a9ec399f').contractAddress
//0x84a56a919232516d2f928ed1b66423e974de4c8b

Now I am accessing this contract:

myContract = eth.contract(abi)
contract = myContract.at('0x84a56a919232516d2f928ed1b66423e974de4c8b')
contract.multiple.call(6) // always returns 0

When I paste code from example from here everything works just fine.

I am not understanding what is the problem with my approach. My solc code and abi are correct? Any help would be appreciated. Thank you.

  • If you mark your multiply(...) function with a constant modifier, your call should work. Otherwise you have to send a transaction to execute your multiply(...) function. – Bokky WeAreAllAssange PooBah May 23 '17 at 6:04
1

Try increasing gaslimit:

eth.sendTransaction({from: eth.accounts[0], data: code, gas: 100000})

Edit:

As default gas is 90000 which may be less for transaction with data. Check eth.getCode('0x84a56a919232516d2f928ed1b66423e974de4c8b') before you access the contract. If it doesn't return your contract code, contract wasn't deployed properly.

  • What this has to do with gas limit? – Prashant Prabhakar Singh May 22 '17 at 12:05
  • @PrashantPrabhakarSingh Because default gas in sendTransaction is 90000, which is enough for transaction with no data but fails for contract creation (which may take more than default gas) – Jaynti Kanani May 22 '17 at 17:12
  • 1
    hmm.. that may be the case if the contract is deployed without any data. You can edit the answer explaining others the logic behind your statement. Upvoted. :) – Prashant Prabhakar Singh May 23 '17 at 3:50
  • @PrashantPrabhakarSingh Added explanation. Thanks. – Jaynti Kanani May 23 '17 at 4:07
0

I think you should call contract.multiply.call(6) not contract.multiple

  • I think that was question typo. contract.multiply.call(6) doesn't work as he says. – Jaynti Kanani May 22 '17 at 5:43
0

The problem here is upon creation of your contract you do not send enough gas. By default it is 90000 ( for Geth ) or the results given after creating it are

Transaction cost: 112321 gas. Execution cost: 44689 gas.

As you can see more gas are required to create the contract than the default value.

You have to send more gas during your transaction in order to make the creation successful by adding the following parameter :

eth.sendTransaction({from: eth.accounts[0], data: code, gas: 200000})

And speaking about gas you should mark your method constant like this

function multiply (uint input) constant returns (uint)

Constant indicates that your function is not supposed to modify the states, and no gas will be used because it allows to the function to be executed locally/off blockchain in your node.

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.