question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Call functions in contract returns promise error

See original GitHub issue

Here is the code I used to call setValue and getValue in contract Hello.sol deployed with etherjs 5.0.14 on android/windows10:

 import { ContractFactory, ethers } from "ethers";

 let url = "http://localhost:8545";
 const provider = new ethers.providers.JsonRpcProvider(url);
 const signer = provider.getSigner(); //forJsonRPC

 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]

Here is the 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;
  }

}

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 setValue (change state) and getValue (view only) functions in ethersjs?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
ricmoocommented, Nov 17, 2020

Exactly what @zemse mentioned.

Also, in a real application, you won’t write a value to a contract and immediately read it. You would set up event listeners and let the events from you or other users trigger that.

You only need to await here because you are attempting consistent writes and reads.

A full application will have more complex interactions, but will be largely asynchronous. 😃

1reaction
zemsecommented, Nov 17, 2020

You are correct that for most users, the UX waiting for tx to be confirmed is annoying. You can have a look at Uniswap’s UX how they handle this case. They push the transaction hash to a global array or transaction hashes. There user can click on a dropdown to see confirmation status and can do some more hacks with the transaction (like increasing gas price or so). This allows user to do other tasks in the dapp while tx is being confirmed in the background.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The javascript function is returning a promise ... - Stack Overflow
I am trying to make a Dapp which maintains the various versions of a trade(new versions are created when we update the data...
Read more >
Contract method returns a Promise instead of a value
You can use a callback function with .then() Try: var variable; this.contractInstance.methods.myMethod() .call({ from: account, gas: 5000000 }) ...
Read more >
Promise.prototype.then() - JavaScript - MDN Web Docs
A then call returns a promise that eventually rejects if the function throws an error or returns a rejected Promise.
Read more >
Function Call - NEAR Protocol Specification
LinkError is returned when wasmer runtime is unable to link the wasm module with provided imports. MethodResolveError occurs when the method in ...
Read more >
Uncaught (in promise) Error: call revert exception when trying ...
I just tried using Moralis in calling functions in the deployed contract in Ganache. I successfully did one transaction, but if I call...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found