bsc pancakeV3 SPL报错的问题

求教各位大佬 想直接与pool交互 绕过路由合约 但是交易报错SPL 这是什么问题

image.png

执行函数:exactInputInternal

测试网报错连接:https://testnet.bscscan.com/tx/0xe9e6481f02794e8a80560b6983b0485eb8a2dfb55ebc6b53d61f11992cf4887d

合约代码:

pragma solidity =0.8.19;
pragma abicoder v2;
import "https://github.com/pancakeswap/pancake-v3-contracts/blob/main/projects/v3-core/contracts/interfaces/IPancakeV3Pool.sol";
import "https://github.com/pancakeswap/pancake-v3-contracts/blob/main/projects/v3-periphery/contracts/libraries/TransferHelper.sol";

import "./path.sol";

library Constants {
    /// @dev Used for identifying cases when this contract's balance of a token is to be used
    uint256 internal constant CONTRACT_BALANCE = 0;

    /// @dev Used as a flag for identifying msg.sender, saves gas by sending more 0 bytes
    address internal constant MSG_SENDER = address(1);

    /// @dev Used as a flag for identifying address(this), saves gas by sending more 0 bytes
    address internal constant ADDRESS_THIS = address(2);
}

struct ExactInputSingleParams {
    address tokenIn;
    address tokenOut;
    uint24 fee;
    address recipient;
    uint256 amountIn;
    uint256 amountOutMinimum;
    uint160 sqrtPriceLimitX96;
}

contract SwapExamples {
    receive() external payable {}
    IPancakeV3Pool poolAddress = IPancakeV3Pool(0xe62C422c1E8083CE3b4526Ff0b16388354AB6E64);

    struct SwapCallbackData {
        bytes path;
        address payer;
    }
    address public constant DAI = 0x8d008B313C1d6C7fE2982F62d32Da7507cF43551;
    address public constant WBNB = 0xae13d989daC2f0dEbFf460aC112a837C89BAa7cd;
    address public constant pool = 0xe62C422c1E8083CE3b4526Ff0b16388354AB6E64;
    uint24 public constant poolFee = 2500;

    uint160 internal constant MIN_SQRT_RATIO = 4295128739;
    /// @dev The maximum value that can be returned from #getSqrtRatioAtTick. Equivalent to getSqrtRatioAtTick(MAX_TICK)
    uint160 internal constant MAX_SQRT_RATIO = 1461446703485210103287273052203988822378723970342;

    function toInt256(uint256 y) public pure returns (int256 z) {
        require(y < 2**255);
        z = int256(y);
    }

    function safeTransfer(
        address token,
        address to,
        uint256 value
    ) public {
        (bool success, bytes memory data) = token.call(abi.encodeWithSelector(IERC20.transfer.selector, to, value));
        require(success && (data.length == 0 || abi.decode(data, (bool))), 'ST');
    }

    function safeTransferFrom(
        address from,
        address to,
        uint256 value
    ) external {
        (bool success, bytes memory data) =
            DAI.call(abi.encodeWithSelector(IERC20.transferFrom.selector, from, to, value));
        require(success && (data.length == 0 || abi.decode(data, (bool))), 'STF');
    }

    function safe(uint256 amountIn) external {
        TransferHelper.safeTransferFrom(DAI, address(this), pool , amountIn);
    }

    function exactInputInternal(
        uint256 amountIn
    ) external returns (uint256 amountOut) {
        address recipient = address(this);
        safeTransfer(DAI,  pool , amountIn);
        uint160 sqrtPriceLimitX96 = 0;
        bool zeroForOne = false;

        ExactInputSingleParams memory data =
            ExactInputSingleParams({
                tokenIn: DAI,
                tokenOut: WBNB,
                fee: poolFee,
                recipient: recipient,
                amountIn: amountIn,
                amountOutMinimum: 0,
                sqrtPriceLimitX96: sqrtPriceLimitX96
            });

        (int256 amount0, int256 amount1) =
            poolAddress.swap(
                recipient,
                true,
                toInt256(amountIn),
                sqrtPriceLimitX96 == 0
                ? (zeroForOne ? MIN_SQRT_RATIO + 1 : MAX_SQRT_RATIO - 1)
                : sqrtPriceLimitX96,
                abi.encode(data)
            );

        amountOut = uint256(-(true ? amount1 : amount0));
        require(amountOut >= 0);
    }
}
请先 登录 后评论

1 个回答

Tiny熊
  擅长:智能合约,以太坊
请先 登录 后评论
  • 1 关注
  • 0 收藏,1360 浏览
  • 不爱你ZC 提出于 2023-12-21 14:37