300 _swapSupportingFeeOnTransferTokens 循环交易 转账数量不对

以下是我的合约代码,核心实现和pancakeSwap routerV2 的_swapSupportingFeeOnTransferTokens基本一致,就是把pair 当作参数传了,我是想实现跨交易所套利。

 function arb(
        address[] calldata pairs,
        uint amountIn,
        uint amountOutMin,
        address[] calldata path
    ) external {
        TransferHelper.safeTransfer(path[0], pairs[0], amountIn);
        address _to = address(this);
        uint balanceBefore = IERC20(path[path.length - 1]).balanceOf(_to);
        _swapSupportingFeeOnTransferTokens(pairs, path, _to);
        require(
            IERC20(path[path.length - 1]).balanceOf(_to).sub(balanceBefore) >= amountOutMin,
            'PancakeRouter: INSUFFICIENT_OUTPUT_AMOUNT'
        );
    }
  function _swapSupportingFeeOnTransferTokens(address[] memory pairs, address[] memory path, address _to) internal virtual {
        for (uint i; i < path.length - 1; i++) {
            (address input, address output) = (path[i], path[i + 1]);
            (address token0,) = PancakeLibrary.sortTokens(input, output);
            IPancakePair pair = IPancakePair(pairs[i]);
            uint amountInput;
            uint amountOutput;
            { // scope to avoid stack too deep errors
                (uint reserve0, uint reserve1,) = pair.getReserves();
                (uint reserveInput, uint reserveOutput) = input == token0 ? (reserve0, reserve1) : (reserve1, reserve0);
                amountInput = IERC20(input).balanceOf(address(pair)).sub(reserveInput);
                amountOutput = PancakeLibrary.getAmountOut(amountInput, reserveInput, reserveOutput);
            }
            (uint amount0Out, uint amount1Out) = input == token0 ? (uint(0), amountOutput) : (amountOutput, uint(0));
            address to = i < path.length - 2 ? pairs[i + 1] : _to;
            pair.swap(amount0Out, amount1Out, to, new bytes(0));
        }
    }

入参: pairs : ["0x545cAb9E5d1173e26f882efCB7F4046828142f73","0x472D5D9266B5794309c353d703fC1A44A864AC82"] path:["0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c","0x14357D294fBabbE0fbF59503370c772d563b35b6","0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c"]

交易详情:https://bscscan.com/tx/0x53a260da17b94fc8603f8f67ad5bddd329314b48b20cc51f7a8f8fadd7b18654 大佬帮忙看看是哪里出问题了导致最终的转账的数量的不对,感谢

请先 登录 后评论

最佳答案 2023-09-13 23:33

pairs传错了, 你描述的是两个交易对,实际传了同一个交易对

请先 登录 后评论

其它 1 个回答

zen
请先 登录 后评论
  • 2 关注
  • 0 收藏,1501 浏览
  • rainn 提出于 2023-09-13 11:08