下面是我在与setValue一起部署的合同Hello.sol中调用D2和getValue的代码:
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); //<但它会抛出错误:
[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]我在跟踪签订合同的文件。在truffle/ganache中对相同的函数进行了测试,它们都工作得很好。在ethersjs中调用setter (更改状态)和getter (仅查看)函数缺少什么?
更新: 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;
}
}发布于 2021-07-15 13:28:23
尝试以下几种方法,
// 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();希望能帮上忙!
https://ethereum.stackexchange.com/questions/90177
复制相似问题