首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“错误:事务已被EVM恢复”<- -甚至不知道从哪里开始

问“错误:事务已被EVM恢复”<- -甚至不知道从哪里开始
EN

Ethereum用户
提问于 2023-02-15 21:34:25
回答 2查看 178关注 0票数 1

因此,首先,我检查了关于这个主题的每一个问题,这似乎可以归结为代码的细节。我找不到他们中任何一个人的直通线。

也就是说,我有一个机器人,它代表套利合约签署和发送区块链交易。但是,当我尝试签名和发送事务时,我会得到下面的错误堆栈。出什么问题了,我怎么解决这个问题?

守则:

代码语言:javascript
复制
  if (config.PROJECT_SETTINGS.isDeployed) {
      const tradeData = await arbitrage.methods.executeTrade(startOnUniswap, _token0Contract._address, _token1Contract._address, payOut).encodeABI();
      const nonce = await web3.eth.getTransactionCount(account);
      const gasPrice = await web3.eth.getGasPrice();
      const gasLimit = 200000;
      const rawTransaction = {
         from: account,
         nonce: web3.utils.toHex(nonce),
         gasPrice: web3.utils.toHex(gasPrice),
         gasLimit: web3.utils.toHex(gasLimit),
         to: arbitrage._address,
         data: tradeData,
                };
const signedTransaction = await web3.eth.accounts.signTransaction(rawTransaction, process.env.PRIVATE_KEY);
await web3.eth.sendSignedTransaction(signedTransaction.rawTransaction);
            }

套利合同:

代码语言:javascript
复制
// SPDX-License-Identifier: MIT
pragma solidity <=0.8.10;

import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

interface Structs {
    struct Val {
        uint256 value;
    }

    enum ActionType {
        Deposit, // supply tokens
        Withdraw, // borrow tokens
        Transfer, // transfer balance between accounts
        Buy, // buy an amount of some token (externally)
        Sell, // sell an amount of some token (externally)
        Trade, // trade tokens against another account
        Liquidate, // liquidate an undercollateralized or expiring account
        Vaporize, // use excess tokens to zero-out a completely negative account
        Call // send arbitrary data to an address
    }

    enum AssetDenomination {
        Wei // the amount is denominated in wei
    }

    enum AssetReference {
        Delta // the amount is given as a delta from the current value
    }

    struct AssetAmount {
        bool sign; // true if positive
        AssetDenomination denomination;
        AssetReference ref;
        uint256 value;
    }

    struct ActionArgs {
        ActionType actionType;
        uint256 accountId;
        AssetAmount amount;
        uint256 primaryMarketId;
        uint256 secondaryMarketId;
        address otherAddress;
        uint256 otherAccountId;
        bytes data;
    }

    struct Info {
        address owner; // The address that owns the account
        uint256 number; // A nonce that allows a single address to control many accounts
    }

    struct Wei {
        bool sign; // true if positive
        uint256 value;
    }
}

abstract contract DyDxPool is Structs {
    function getAccountWei(Info memory account, uint256 marketId)
        public
        view
        virtual
        returns (Wei memory);

    function operate(Info[] memory, ActionArgs[] memory) public virtual;
}

contract DyDxFlashLoan is Structs {
    DyDxPool pool = DyDxPool(0x1E0447b19BB6EcFdAe1e4AE1694b0C3659614e4e);

    address public WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
    mapping(address => uint256) public currencies;

    constructor() {
        currencies[WETH] = 1;
    }

    modifier onlyPool() {
        require(
            msg.sender == address(pool),
            "FlashLoan: could be called by DyDx pool only"
        );
        _;
    }

    function tokenToMarketId(address token) public view returns (uint256) {
        uint256 marketId = currencies[token];
        require(marketId != 0, "FlashLoan: Unsupported token");
        return marketId - 1;
    }

    // the DyDx will call `callFunction(address sender, Info memory accountInfo, bytes memory data) public` after during `operate` call
    function flashloan(
        address token,
        uint256 amount,
        bytes memory data
    ) internal {
        IERC20(token).approve(address(pool), amount + 1);
        Info[] memory infos = new Info[](1);
        ActionArgs[] memory args = new ActionArgs[](3);

        infos[0] = Info(address(this), 0);

        AssetAmount memory wamt = AssetAmount(
            false,
            AssetDenomination.Wei,
            AssetReference.Delta,
            amount
        );
        ActionArgs memory withdraw;
        withdraw.actionType = ActionType.Withdraw;
        withdraw.accountId = 0;
        withdraw.amount = wamt;
        withdraw.primaryMarketId = tokenToMarketId(token);
        withdraw.otherAddress = address(this);

        args[0] = withdraw;

        ActionArgs memory call;
        call.actionType = ActionType.Call;
        call.accountId = 0;
        call.otherAddress = address(this);
        call.data = data;

        args[1] = call;

        ActionArgs memory deposit;
        AssetAmount memory damt = AssetAmount(
            true,
            AssetDenomination.Wei,
            AssetReference.Delta,
            amount + 1
        );
        deposit.actionType = ActionType.Deposit;
        deposit.accountId = 0;
        deposit.amount = damt;
        deposit.primaryMarketId = tokenToMarketId(token);
        deposit.otherAddress = address(this);

        args[2] = deposit;

        pool.operate(infos, args);
    }
}

contract Arbitrage is DyDxFlashLoan {
    IUniswapV2Router02 public immutable sRouter;
    IUniswapV2Router02 public immutable uRouter;

    address public owner;

    constructor(address _sRouter, address _uRouter) {
        sRouter = IUniswapV2Router02(_sRouter); // Sushiswap
        uRouter = IUniswapV2Router02(_uRouter); // Uniswap
        owner = msg.sender;
    }

    function executeTrade(
        bool _startOnUniswap,
        address _token0,
        address _token1,
        uint256 _flashAmount
    ) external {
        uint256 balanceBefore = IERC20(_token0).balanceOf(address(this));

        bytes memory data = abi.encode(
            _startOnUniswap,
            _token0,
            _token1,
            _flashAmount,
            balanceBefore
        );

        flashloan(_token0, _flashAmount, data); // execution goes to `callFunction`
    }

    function callFunction(
        address, /* sender */
        Info calldata, /* accountInfo */
        bytes calldata data
    ) external onlyPool {
        (
            bool startOnUniswap,
            address token0,
            address token1,
            uint256 flashAmount,
            uint256 balanceBefore
        ) = abi.decode(data, (bool, address, address, uint256, uint256));

        uint256 balanceAfter = IERC20(token0).balanceOf(address(this));

        require(
            balanceAfter - balanceBefore == flashAmount,
            "contract did not get the loan"
        );

        // Use the money here!
        address[] memory path = new address[](2);

        path[0] = token0;
        path[1] = token1;

        if (startOnUniswap) {
            _swapOnUniswap(path, flashAmount, 0);

            path[0] = token1;
            path[1] = token0;

            _swapOnSushiswap(
                path,
                IERC20(token1).balanceOf(address(this)),
                (flashAmount + 1)
            );
        } else {
            _swapOnSushiswap(path, flashAmount, 0);

            path[0] = token1;
            path[1] = token0;

            _swapOnUniswap(
                path,
                IERC20(token1).balanceOf(address(this)),
                (flashAmount + 1)
            );
        }

        IERC20(token0).transfer(
            owner,
            IERC20(token0).balanceOf(address(this)) - (flashAmount + 1)
        );
    }

    // -- INTERNAL FUNCTIONS -- //

    function _swapOnUniswap(
        address[] memory _path,
        uint256 _amountIn,
        uint256 _amountOut
    ) internal {
        require(
            IERC20(_path[0]).approve(address(uRouter), _amountIn),
            "Uniswap approval failed."
        );

        uRouter.swapExactTokensForTokens(
            _amountIn,
            _amountOut,
            _path,
            address(this),
            (block.timestamp + 1200)
        );
    }

    function _swapOnSushiswap(
        address[] memory _path,
        uint256 _amountIn,
        uint256 _amountOut
    ) internal {
        require(
            IERC20(_path[0]).approve(address(sRouter), _amountIn),
            "Sushiswap approval failed."
        );

        sRouter.swapExactTokensForTokens(
            _amountIn,
            _amountOut,
            _path,
            address(this),
            (block.timestamp + 1200)
        );
    }
}

错误:

代码语言:javascript
复制
Error: Transaction has been reverted by the EVM:
{
  "blockHash": "0xeed5773a3228dd01bef285bbb3139f6d2170323058703e8cf30bc8ba6a927342",
  "blockNumber": 16632255,
  "contractAddress": null,
  "cumulativeGasUsed": 24687572,
  "effectiveGasPrice": 28667583352,
  "from": "0xdf58ca0a02bf9b43ce13b8ab5969e4b60f22c0a3",
  "gasUsed": 196577,
  "logs": [],
  "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  "status": false,
  "to": "0x4a15775657e7720bbd9cb7039484ed0510be6689",
  "transactionHash": "0x5a748a0fd8c21898021823a424da94576f5d794ed9867bd62a7bd3027473ce00",
  "transactionIndex": 301,
  "type": "0x0"
}
    at Object.TransactionError (/Users/TrentKennelly/trading_bot_V2/node_modules/web3-core-helpers/lib/errors.js:87:21)
    at Object.TransactionRevertedWithoutReasonError (/Users/TrentKennelly/trading_bot_V2/node_modules/web3-core-helpers/lib/errors.js:98:21)
    at /Users/TrentKennelly/trading_bot_V2/node_modules/web3-core-method/lib/index.js:396:57
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  receipt: {
    blockHash: '0xeed5773a3228dd01bef285bbb3139f6d2170323058703e8cf30bc8ba6a927342',
    blockNumber: 16632255,
    contractAddress: null,
    cumulativeGasUsed: 24687572,
    effectiveGasPrice: 28667583352,
    from: '0xdf58ca0a02bf9b43ce13b8ab5969e4b60f22c0a3',
    gasUsed: 196577,
    logs: [],
    logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
    status: false,
    to: '0x4a15775657e7720bbd9cb7039484ed0510be6689',
    transactionHash: '0x5a748a0fd8c21898021823a424da94576f5d794ed9867bd62a7bd3027473ce00',
    transactionIndex: 301,
    type: '0x0'
  }
}

我完全不知道这可能是什么。任何帮助都是非常感谢的!

编辑:感谢吴中的评论,我终于想出了如何至少理解这件事的问题。即使在验证了合同( Remix查看事务所需的)之后,混合仍然不起作用。

最后我使用了truffle debug 如这里所详述。这让我犯了一个我以前见过的错误,但我认为我修好了。所以我现在就问这个问题。

上述事务和0xb6242b8af50932cc975d180838a77f7b28b3fc4a8e963cfbca21e3fbaa30629d的错误如下:

代码语言:javascript
复制
TrentKennelly@Trent-MBP-2022 trading_bot_v2 % truffle debug 0xb6242b8af50932cc975d180838a77f7b28b3fc4a8e963cfbca21e3fbaa30629d --network mainnet
Starting Truffle Debugger...
TypeError: Cannot create property 'gasLimit' on string '0xfdf159'
    at Method.outputBlockFormatter (/usr/local/lib/node_modules/truffle/build/webpack:/node_modules/web3-core-helpers/lib/formatters.js:269:1)
    at Method.web3.eth.getBlock.method.outputFormatter (/usr/local/lib/node_modules/truffle/build/webpack:/packages/interface-adapter/dist/shim/overloads/ethereum.js:38:1)
    at Method.formatOutput (/usr/local/lib/node_modules/truffle/build/webpack:/node_modules/web3-core-method/lib/index.js:148:1)
    at sendTxCallback (/usr/local/lib/node_modules/truffle/build/webpack:/node_modules/web3-core-method/lib/index.js:536:1)
    at processTicksAndRejections (node:internal/process/task_queues:82:21)
Truffle v5.7.1 (core: 5.7.1)
Node v18.12.1
TrentKennelly@Trent-MBP-2022 trading_bot_v2 % truffle debug 0x5a748a0fd8c21898021823a424da94576f5d794ed9867bd62a7bd3027473ce00 --network mainnet
Starting Truffle Debugger...
TypeError: Cannot create property 'gasLimit' on string '0xfdf161'
    at Method.outputBlockFormatter (/usr/local/lib/node_modules/truffle/build/webpack:/node_modules/web3-core-helpers/lib/formatters.js:269:1)
    at Method.web3.eth.getBlock.method.outputFormatter (/usr/local/lib/node_modules/truffle/build/webpack:/packages/interface-adapter/dist/shim/overloads/ethereum.js:38:1)
    at Method.formatOutput (/usr/local/lib/node_modules/truffle/build/webpack:/node_modules/web3-core-method/lib/index.js:148:1)
    at sendTxCallback (/usr/local/lib/node_modules/truffle/build/webpack:/node_modules/web3-core-method/lib/index.js:536:1)
    at processTicksAndRejections (node:internal/process/task_queues:82:21)

所以..。对这件事出了什么问题有什么想法吗?

EN

回答 2

Ethereum用户

回答已采纳

发布于 2023-02-22 09:31:50

在吴中的帮助下调试后,我可以自信地说,问题是我的帐户里没有足够的钱来支付汽油费用。好像缓冲区不在那里。

票数 0
EN

Ethereum用户

发布于 2023-02-16 05:01:33

目前,Ethereum客户端不返回详细错误消息。当事务失败时,它只会抛出事务恢复错误。

你试过Remix的调试工具吗?

一个更硬的方法是使用跟踪 API,更具体地说是跟踪_打电话方法。它“模拟”事务,并返回每个内部事务的执行日志和结果(操作码/状态更改/成功)。

它对于调试复杂的智能契约(如仲裁)特别有帮助。

希望这能有所帮助。

票数 1
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/144998

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档