RPC Error: excution reverted" when calling smart contract from next.js

Viewed 29

I've looked at similar answers regarding this error, and it seems like the majority of them have to do with using the wrong contract address or the abi. In my case, I do not think this is likely because I am using file saver to update contract addresses every time I redeploy and I am using npx hardhat clean before compiling updates to contracts. I've heard some people point out that this error might be due to missing receive() and fallback() functions. In such a case, what is the logic I should put in the functions? I don't quite understand how they work even after reading up on them.

For context, I'm trying to call a deposit function on js.

This is the error I'm getting:

inpage.js:1 MetaMask - RPC Error: execution reverted {code: -32000, message: 'execution reverted'}
(anonymous) @ inpage.js:1
(anonymous) @ inpage.js:17
_runReturnHandlers @ inpage.js:17
_processRequest @ inpage.js:17
await in _processRequest (async)
_handle @ inpage.js:17
handle @ inpage.js:17
_rpcRequest @ inpage.js:1
(anonymous) @ inpage.js:1
request @ inpage.js:1
request @ VM2565:2
eval @ web3-provider.js?6870:65
send @ web3-provider.js?6870:128
eval @ json-rpc-provider.js?8679:572
eval @ json-rpc-provider.js?8679:8
__awaiter @ json-rpc-provider.js?8679:4
perform @ json-rpc-provider.js?8679:549
eval @ base-provider.js?4ba1:1550
fulfilled @ base-provider.js?4ba1:5
Promise.then (async)
step @ base-provider.js?4ba1:7
fulfilled @ base-provider.js?4ba1:5
Promise.then (async)
step @ base-provider.js?4ba1:7
eval @ base-provider.js?4ba1:8
__awaiter @ base-provider.js?4ba1:4
estimateGas @ base-provider.js?4ba1:1545
sendUncheckedTransaction @ json-rpc-provider.js?8679:203
eval @ json-rpc-provider.js?8679:253
fulfilled @ json-rpc-provider.js?8679:5
Promise.then (async)
step @ json-rpc-provider.js?8679:7
eval @ json-rpc-provider.js?8679:8
__awaiter @ json-rpc-provider.js?8679:4
sendTransaction @ json-rpc-provider.js?8679:249
eval @ index.js?b70c:323
fulfilled @ index.js?b70c:5
Promise.then (async)
step @ index.js?b70c:7
eval @ index.js?b70c:8
__awaiter @ index.js?b70c:4
eval @ index.js?b70c:312
_callee$ @ _app.js?e0ad:87
tryCatch @ runtime.js?ecd4:45
invoke @ runtime.js?ecd4:274
prototype.<computed> @ runtime.js?ecd4:97
asyncGeneratorStep @ _async_to_generator.mjs?1d45:3
_next @ _async_to_generator.mjs?1d45:25
Promise.then (async)
asyncGeneratorStep @ _async_to_generator.mjs?1d45:13
_next @ _async_to_generator.mjs?1d45:25
eval @ _async_to_generator.mjs?1d45:32
eval @ _async_to_generator.mjs?1d45:21
depositToJuniorPool @ _app.js?e0ad:78
onClick @ _app.js?e0ad:157
callCallback @ react-dom.development.js?ac89:4164
invokeGuardedCallbackDev @ react-dom.development.js?ac89:4213
invokeGuardedCallback @ react-dom.development.js?ac89:4277
invokeGuardedCallbackAndCatchFirstError @ react-dom.development.js?ac89:4291
executeDispatch @ react-dom.development.js?ac89:9041
processDispatchQueueItemsInOrder @ react-dom.development.js?ac89:9073
processDispatchQueue @ react-dom.development.js?ac89:9086
dispatchEventsForPlugins @ react-dom.development.js?ac89:9097
eval @ react-dom.development.js?ac89:9288
batchedUpdates$1 @ react-dom.development.js?ac89:26140
batchedUpdates @ react-dom.development.js?ac89:3991
dispatchEventForPluginEventSystem @ react-dom.development.js?ac89:9287
dispatchEventWithEnableCapturePhaseSelectiveHydrationWithoutDiscreteEventReplay @ react-dom.development.js?ac89:6465
dispatchEvent @ react-dom.development.js?ac89:6457
dispatchDiscreteEvent @ react-dom.development.js?ac89:6430
_app.js?e0ad:90 Error:  Error: cannot estimate gas; transaction may fail or may require manual gas limit [ See: https://links.ethers.org/v5-errors-UNPREDICTABLE_GAS_LIMIT ] (reason="execution reverted", method="estimateGas", transaction={"from":"0xFDE82216765027A0c15Dff066b16F24d0b6D4E8d","to":"0x765640bf43C65b0B50bE753a2889452ffa883152","data":"0xb6b55f25000000000000000000000000000000000000000000000000000000000000006e","accessList":null}, error={"code":-32000,"message":"execution reverted"}, code=UNPREDICTABLE_GAS_LIMIT, version=providers/5.7.0)
    at Logger.makeError (index.js?dd68:224:1)
    at Logger.throwError (index.js?dd68:233:1)
    at checkError (json-rpc-provider.js?8679:76:1)
    at Web3Provider.eval (json-rpc-provider.js?8679:575:1)
    at Generator.throw (<anonymous>)
    at rejected (json-rpc-provider.js?8679:6:42)

These are the relevant parts of the js:

async function depositToJuniorPool(amount) {
  if (typeof window.ethereum !== 'undefined') {
      const provider = new ethers.providers.Web3Provider(window.ethereum)
      const signer = provider.getSigner()
      const juniorPool = new ethers.Contract(juniorPoolAddress, JuniorPool.abi, signer)
      const usdc = new ethers.Contract(usdcAddress, IERC20withDec.abi, signer)
      console.log('juniorPool: ', juniorPool)
      try {
          await usdc.approve(juniorPoolAddress, amount)
          const val = await juniorPool.deposit(amount)
          console.log('val: ', val)
        } catch (err) {
          console.log('Error: ', err)
        }
}

}

These are the relevant parts for the sol:

  function deposit(uint256 amount) public payable override whenNotPaused withinTransactionLimit(amount) nonReentrant {
    require(amount >= 110, "Must deposit 110 or more");
    uint256 depositShares = getNumShares(amount);
    uint256 potentialNewTotalShares = totalShares().add(depositShares);
    require(poolWithinLimit(potentialNewTotalShares), "Deposit would put the Pool over the total limit.");
    emit DepositMade(msg.sender, amount, depositShares);
    bool success = doUSDCTransfer(msg.sender, address(this), amount);
    require(success, "Failed to transfer for deposit");

    config.getJuniorToken().juniorPoolMintTo(msg.sender, depositShares);
  }


  function juniorPoolMintTo(address to, uint256 amount) public onlyJuniorLendingPool {
    require(canMintForJuniorPool(amount), "Cannot mint: it would create an asset/liability mismatch");
    super._mint(to, amount);
  }

  function doUSDCTransfer(
    address from,
    address to,
    uint256 amount
  ) internal returns (bool) {
    require(to != address(0), "Can't send to zero address");
    IERC20withDec usdc = config.getUSDC();
    return usdc.transferFrom(from, to, amount);
  }
1 Answers

Execution reverted is due to contract call failure. The reverton is either by an accident, for example, when overflow occurs, or intentionally, for example, require() condition is not satisfied.

Related