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;
}
}
getValue
is constant (pure
orview
)? TheerrorSignature=null
error implies thatethers.js
is trying to execute it as a transaction (rather than as an RPC).Hello.sol
.await
in contract deployment. Contract deploy is an async operation and needs toawait contract.deployTransaxtion.wait()
before calling the functions incontract
.