我正在尝试将Uniswap V2集成到我的合同中,但是我做交易的尝试(从ETH到令牌)总是被恢复。
以下是我的可靠代码:
pragma solidity ^0.6.0;
// Included the interface found here: https://uniswap.org/docs/v2/smart-contracts/router/#interface
interface IUniswapV2Router01 {
...
}
contract MyContract {
address internal constant UNISWAP_ROUTER_ADDRESS = 0xcDbE04934d89e97a24BCc07c3562DC8CF17d8167; // Rinkeby
IUniswapV2Router01 public uniswapRouter;
constructor() public {
uniswapRouter = IUniswapV2Router01(UNISWAP_ROUTER_ADDRESS);
}
function swapEthForTokenWithUniswap(uint ethAmount, address tokenAddress) public onlyOwner {
// Verify we have enough funds
require(ethAmount <= address(this).balance, "Not enough Eth in contract to perform swap.");
// Build arguments for uniswap router call
address[] memory path = new address[](2);
path[0] = uniswapRouter.WETH();
path[1] = tokenAddress;
// Make the call and give it 15 seconds
// Set amountOutMin to 0 but no success with larger amounts either
uniswapRouter.swapExactETHForTokens.value(ethAmount)(0, path, address(this), now + 15);
}
function depositEth() external payable {
// Nothing to do
}
}在调用swapEthForTokenWithUniswap之前,我通过调用depositEth来保存1个ETH。我正在使用BAT令牌(0xda5b056cfb861282b4b59d29c9b395bcc238d29b)在Rinkeby上测试这一点。不幸的是,我在日志方面没有太多的信息(对于Solidity来说仍然是新的,所以可能不知道在哪里查找)。
下面是我尝试创建的一个失败事务的链接:https://rinkeby.etherscan.io/tx/0x465b095ee53ceff57850f7f1d6a1c0256f7bfc948563907d189d6bc4f9ad64c2
发布于 2020-05-01 16:29:35
所以我发现了问题。我正在使用BAT令牌(0xda5b056cfb861282b4b59d29c9b395bcc238d29b)进行测试,但此时还没有创建ETH/BAT交易对。
我应该先检查一下这对,以及这对的流动性,然后再进行互换。
我的代码使用DAI令牌(0xc7ad46e0b8a400bb3c915120d284aafba8fc4735)。
所有地址都在rinkeby网络上。
https://ethereum.stackexchange.com/questions/82951
复制相似问题