0

I'm following the Storage contract example from the Geth site (https://geth.ethereum.org/docs/interacting-with-geth/javascript-console-contracts)

I am unable to call a 'retrieve' function on the contract and get the return value. instance.retrieve() appears to return a transaction receipt and instance.retrieve().call() produces and error that there is no 'call' method.

Contract

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Storage{

    uint256 value = 5;

    function set(uint256 number) public{
        value = number;
    }

    function retrieve() public view returns (uint256){
        return value;
    }
}

Deploy and Call

geth attach node1/geth.ipc
Welcome to the Geth JavaScript console!

instance: Geth/v1.13.5-stable-916d6a44/linux-amd64/go1.21.4
...

> var abi = [{"inputs":[],"name":"retrieve","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutabifunction"},{"inputs":[{"internalType":"uint256","name":"number","type":"uint256"}],"name":"set","outputs":[],"stateMutabilpe":"function"}];

> var bytecode = '0x6080604052600560005534801561001557600080fd5b50610150806100256000396000f3fe608060405234801561001057600065760003560e01c80632e64cec11461003b57806360fe47b114610059575b600080fd5b610043610075565b60405161005091906100a1565b604051809360381019061006e91906100ed565b61007e565b005b60008054905090565b8060008190555050565b6000819050919050565b61009b81610088565b82190506100b66000830184610092565b92915050565b600080fd5b6100ca81610088565b81146100d557600080fd5b50565b6000813590506100e7816100060208284031215610103576101026100bc565b5b6000610111848285016100d8565b9150509291505056fea2646970667358221220fdc5deb6ea2ff998fd03d6bfd547e018963aae027a164736f6c63430008170033'
> var contract = eth.contract(abi);
> var gas = eth.estimateGas({ data: bytecode });
> var tx = { from: eth.accounts[0], data: bytecode, gas: gas };
> var deployed_contract = contract.new(tx);
>  var instance = contract.at(deployed_contract.address);

> instance.set.sendTransaction(42, {from: eth.accounts[0], gas: 100000});
"0x977067e158d57e5788417abf262fdd4244808f4b711f11ed5ad84fad786fd898"
> eth.defaultAccount = web3.eth.accounts[0];
"0x2367d43e993ffbada5f45e13b6df5e24598530a9"

The Error

> instance.retrieve().call() >> 2
TypeError: Object has no member 'call'
        at <eval>:1:25(4)
> instance.retrieve() >> 2
0
> instance.retrieve().call();
TypeError: Object has no member 'call'
        at <eval>:1:25(4)

> instance.retrieve()
"0x98ede7bca5ae23c3bd219a06a8b9faa508aeab6bbc83c43819e6f3c80f23a0bb"
> eth.getTransactionReceipt("0x98ede7bca5ae23c3bd219a06a8b9faa508aeab6bbc83c43819e6f3c80f23a0bb");
{
  blockHash: "0x69ec3c72df383000cd03d190af5542a04ec17acf37239ffe7015beade952ffbf",
  blockNumber: 1651,
  contractAddress: null,
  cumulativeGasUsed: 23479,
  effectiveGasPrice: 1000000000,
  from: "0x2367d43e993ffbada5f45e13b6df5e24598530a9",
  gasUsed: 23479,
  logs: [],
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  status: "0x1",
  to: "0xd6f6bd102568074f70ebe44bd60edb9119a9089f",
  transactionHash: "0x98ede7bca5ae23c3bd219a06a8b9faa508aeab6bbc83c43819e6f3c80f23a0bb",
  transactionIndex: 0,
  type: "0x0"
}

I've tried a few thing, but still not sure how to get this to work, or find details in the documentation.

2 Answers 2

1

geth uses a very old version of web3 v0.20. Check the documentation in the github repo https://github.com/web3/web3.js/blob/0.20.7/DOCUMENTATION.md#contract-methods

In your case the equivalent should be

instance.retrieve.call(params..)

Or

instance.retrieve.sendTransaction(params.., {from: "0xxxx"})
0

fxed it. was copy-n-pasting from the example. it was the ()

instance.retrieve().call();

should be

instance.retrieve.call();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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