简体   繁体   English

从 next.js 调用智能合约时,RPC 错误:执行已恢复

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

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.我已经查看了有关此错误的类似答案,似乎大多数都与使用错误的合约地址或 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.就我而言,我认为这不太可能,因为我每次重新部署时都使用文件保护程序来更新合约地址,并且在编译合约更新之前我使用npx hardhat clean I've heard some people point out that this error might be due to missing receive() and fallback() functions.我听说有人指出这个错误可能是由于缺少 receive() 和 fallback() 函数。 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.对于上下文,我试图在 js 上调用deposit function。

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:这些是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:这些是 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);
  }

Execution reverted is due to contract call failure. Execution reverted是由于合约调用失败。 The reverton is either by an accident, for example, when overflow occurs, or intentionally, for example, require() condition is not satisfied.恢复要么是偶然的,例如发生溢出时,要么是故意的,例如不满足require()条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM