我对swapExactETHForTokens()有个问题。每次我将输入值更改为Zero以外的值时,它似乎都会失败。
为了帮助您遵循de DATA的路线,我向您展示:
我的问题和进展,有不同的配置:
1.使用{value:msg.value},{值:0}或无值。
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)
此消息来自getAmoutOut(),由getAmountsOut()调用,由swapExactETHForTokens()调用,因为输入量不超过0。因此,这些数据似乎得到了很好的认可。
2.具有非零值ex:{ value :1000000000000000000}
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)
IUniswapV2Router02(PANCAKE_ROUTER).swapExactETHForTokens{value:1000000000000000000}(0,path,address(this),deadline)
4.没有值和path=CAKE (而不是path=CAKE)
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)
看来with是一个非零值,数据不会穿透函数。我用的是草帽,我用的是0.6.6。我做错什么了吗?经过两天的研究,我已经失去了所有的头发。
这是我的函数,从pancakeCall()调用:
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()函数
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
发布于 2022-09-20 01:00:51
你是如何调用这个函数的?在使用value:msg.value的情况下,在调用函数时,实际上是否发送了任何ETH (在您的情况下是BNB )?当你使用价值:100.,你的合同已经有资金了吗?
https://ethereum.stackexchange.com/questions/135950
复制相似问题