2

Here is the code I used to call setValue and getValue in contract Hello.sol deployed with etherjs 5.0:

  import { ContractFactory, ethers } from "ethers";

  let contract_at_address = new ethers.Contract(contract.address, abi, signer); //<<==contract.address is the deployed address. abi and signer were defined earlier.
  await contract_at_address.setValue(10); //<<setter
  let value = await contract_at_address.getValue(); //<<==value shall be 10
  console.log("value contract at address : ", value)

But it throws error:

[Sun Nov 15 2020 23:10:11.457]  WARN     Possible Unhandled Promise Rejection (id: 0):
Error: call revert exception (method="getValue()", errorSignature=null, errorArgs=[null], reason=null, code=CALL_EXCEPTION, version=abi/5.0.5)
makeError@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:110810:32
throwError@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:110820:31
http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:118492:68
step@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:118174:29
fulfilled@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:118058:34
tryCallOne@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:26991:16
http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:27092:27
_callTimer@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:30531:17
_callImmediatesPass@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:30570:17
callImmediates@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:30787:33
__callImmediates@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:2736:35
http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:2522:34
__guard@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:2719:15
flushedQueue@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:2521:21
flushedQueue@[native code]
invokeCallbackAndReturnFlushedQueue@[native code]

I am following the doc for contract. The same functions are tested in truffle/ganache and they all work fine. What's missing here to call setter (change state) and getter (view only) functions in ethersjs?

UPDATE: Hello.sol

pragma solidity 0.7.0;

contract Hello {
    address owner;
    uint256 value;
    
    event initContract(address _owner);
    constructor() {
        owner = msg.sender;
        emit initContract(owner);
    }
    
    function setValue(uint256 _value) public {
        value = _value;
    }
    
    function getValue() view public returns (uint256) {
        return value;
    }
}
4
  • Are you sure that function getValue is constant (pure or view)? The errorSignature=null error implies that ethers.js is trying to execute it as a transaction (rather than as an RPC). Nov 16 '20 at 7:58
  • it is view function. Updated is the Hello.sol.
    – user938363
    Nov 16 '20 at 16:05
  • There are 2 ways to call function in contract doc. One is hello.getValue() and another is hello.functions.getValue(). I don't quite understand the difference but both of format throw error.
    – user938363
    Nov 16 '20 at 16:48
  • The problem is that it was missing await in contract deployment. Contract deploy is an async operation and needs to await contract.deployTransaxtion.wait() before calling the functions in contract.
    – user938363
    Nov 17 '20 at 22:24
1

Try out the following,

// Set Provider
const rpc = 'YOUR RPC';
const provider = new ethers.providers.JsonRpcProvider(rpc);

// Contract Instance
const contractInstance = new ethers.Contract(contractAddress, contractABI, provider);

// For view function
var result = await contractInstance.getValue();

Hope it 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.