首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >输入非零值的Pancakeswap swapExactETHForTokens失败

输入非零值的Pancakeswap swapExactETHForTokens失败
EN

Ethereum用户
提问于 2022-09-19 21:01:07
回答 1查看 233关注 0票数 2

我对swapExactETHForTokens()有个问题。每次我将输入值更改为Zero以外的值时,它似乎都会失败。

为了帮助您遵循de DATA的路线,我向您展示:

  • swapExactETHForTokens()从pancakeswap合同函数swapExactETHForTokens(uint amountOutMin,address[] calldata path,address to,uint deadline)外部虚拟覆盖应确保(截止日期)返回(uint[]内存金额){ require(path == WETH,'PancakeRouter: INVALID_PATH');address[]=PancakeLibrary.getAmountsOut(工厂,msg.value,path);require(require >= amountOutMin,'PancakeRouter: INSUFFICIENT_OUTPUT_AMOUNT');IWETH(WETH).deposit{value:.deposit}();
  • 来自同一合同的getAmountsOut()。在第一个里面使用。函数getAmountsOut(地址工厂,uint amountIn,address[]内存路径)内部视图返回(uint[]内存数量){ require(path.length >= 2,'PancakeLibrary: INVALID_PATH');uint[]=新uintpath.length;path = amountIn;for (uint i;i< path.length - 1;i++) { (uint reserveIn,uint reserveOut) =getReserves(工厂,路径,路径);uint[]=getAmountOut(uint[],reserveIn,getReserves);}
  • 和从getAmountsOut()结尾调用的getAmountsOut():函数getAmountOut(uint amountIn,uint reserveIn,uint reserveOut)内部纯返回(uint amountOut) {需要量(amountIn> 0,'PancakeLibrary: INSUFFICIENT_INPUT_AMOUNT');要求(reserveIn>0& reserveOut > 0,'PancakeLibrary: INSUFFICIENT_LIQUIDITY');uint amountInWithFee = amountIn.mul(9975);uint算术=amountInWithFee.mul(AmountInWithFee);uint符号=amountInWithFee(10000)(# end 20#);#en21=分子/分母;

我的问题和进展,有不同的配置:

1.使用{value:msg.value},{值:0}或无值。

代码语言:javascript
运行
复制
IUniswapV2Router02(PANCAKE_ROUTER).swapExactETHForTokens(0,path,address(this),deadline)
IUniswapV2Router02(PANCAKE_ROUTER).swapExactETHForTokens{value:msg.value}(0,path,address(this),deadline)
IUniswapV2Router02(PANCAKE_ROUTER).swapExactETHForTokens{value:0}(0,path,address(this),deadline)
  • 在所有这些情况下,我都会得到错误消息‘INPUT_INPUT’

此消息来自getAmoutOut(),由getAmountsOut()调用,由swapExactETHForTokens()调用,因为输入量不超过0。因此,这些数据似乎得到了很好的认可。

2.具有非零值ex:{ value :1000000000000000000}

代码语言:javascript
运行
复制
IUniswapV2Router02(PANCAKE_ROUTER).swapExactETHForTokens{value:1000000000000000000}(0,path,address(this),deadline)
  • 我得到了错误‘错误:事务恢复:函数调用执行失败’。这表明函数在某个地方停止了。

To前进,我已经将path = WBNB_contract更改为path = Cake_contract。因此,它应该显示错误:‘Pancake路由器: INVALID_PATH'.

3.具有非零值,使用path=CAKE (而不是WBNB)

代码语言:javascript
运行
复制
IUniswapV2Router02(PANCAKE_ROUTER).swapExactETHForTokens{value:1000000000000000000}(0,path,address(this),deadline)
  • 再次给出‘错误:事务恢复:函数调用执行失败,对它们都执行了。所以数据似乎不属于函数内部。

4.没有值和path=CAKE (而不是path=CAKE)

代码语言:javascript
运行
复制
IUniswapV2Router02(PANCAKE_ROUTER).swapExactETHForTokens(0,path,address(this),deadline)
IUniswapV2Router02(PANCAKE_ROUTER).swapExactETHForTokens{value:0}(0,path,address(this),deadline)
IUniswapV2Router02(PANCAKE_ROUTER).swapExactETHForTokens{value:msg.value}(0,path,address(this),deadline)
  • 但是如果没有一个值,我就会得到错误‘Pancake路由器:无效_PATH’。因此,错误来自函数swapExactETHForTokens()的第一行。

看来with是一个非零值,数据不会穿透函数。我用的是草帽,我用的是0.6.6。我做错什么了吗?经过两天的研究,我已经失去了所有的头发。

这是我的函数,从pancakeCall()调用:

代码语言:javascript
运行
复制
function placeTrade(address _fromToken,address _toToken,uint256 _amountIn) payable public returns (uint256) {
    address pair = IUniswapV2Factory(PANCAKE_FACTORY).getPair(_fromToken,_toToken); 
    require(pair != address(0), "Pool does not exist (for trade)");

    // Calculate Amount Out
    address[] memory path = new address[](2);
    path[0] = _fromToken;
    path[1] = _toToken;

  
    uint256 amountRequired = IUniswapV2Router02(PANCAKE_ROUTER).getAmountsOut(_amountIn, path)[1];

    // Perform Arbitrage - Swap for another token
    uint256 amountReceived ;
    console.log('- fromToken:',path[0], "toToken:",path[1]);
    if (_fromToken == WBNB) {
         console.log('- BNB / token');

        TransferHelper.safeApprove(_fromToken, address(PANCAKE_ROUTER),_amountIn);
        amountReceived = IUniswapV2Router02(PANCAKE_ROUTER).swapExactETHForTokens{value:msg.value}(0,path,address(this),deadline)[1];

startTrade()函数是从pancakeswap调用中调用的,就在我要求从pancakeswap获得一个带swap()函数的闪存交换后。这是我的pancakeCall()函数

代码语言:javascript
运行
复制
    function pancakeCall(address _sender,uint256 _amount0,uint256 _amount1,bytes calldata _data) external {


    address token0 = IUniswapV2Pair(msg.sender).token0();
    address token1 = IUniswapV2Pair(msg.sender).token1();
    address pair = IUniswapV2Factory(PANCAKE_FACTORY).getPair(token0,token1);

    require(msg.sender == pair, "The sender needs to match the pair");
    require(_sender == address(this), "Sender should match this contract");

    (address starterToken, address tokenLink1, address tokenLink2,  uint256 amount, address myAddress) = abi.decode(_data,(address, address, address, uint256, address));


    uint256 fee = ((amount * 3) / 997) + 1;
    uint256 amountToRepay = amount + fee;

    uint256 loanAmount = _amount0 > 0 ? _amount0 : _amount1;

    uint256 trade1AcquiredCoin = placeTrade(starterToken, tokenLink1, loanAmount);//with starterToken =WBNB
EN

回答 1

Ethereum用户

发布于 2022-09-20 01:00:51

你是如何调用这个函数的?在使用value:msg.value的情况下,在调用函数时,实际上是否发送了任何ETH (在您的情况下是BNB )?当你使用价值:100.,你的合同已经有资金了吗?

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

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

复制
相关文章

相似问题

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