首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Uniswap V2智能合同集成恢复将ETH替换为令牌的尝试

Uniswap V2智能合同集成恢复将ETH替换为令牌的尝试
EN

Ethereum用户
提问于 2020-04-30 20:44:27
回答 1查看 5.3K关注 0票数 2

我正在尝试将Uniswap V2集成到我的合同中,但是我做交易的尝试(从ETH到令牌)总是被恢复。

以下是我的可靠代码:

代码语言:javascript
复制
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

EN

回答 1

Ethereum用户

发布于 2020-05-01 16:29:35

所以我发现了问题。我正在使用BAT令牌(0xda5b056cfb861282b4b59d29c9b395bcc238d29b)进行测试,但此时还没有创建ETH/BAT交易对。

我应该先检查一下这对,以及这对的流动性,然后再进行互换。

我的代码使用DAI令牌(0xc7ad46e0b8a400bb3c915120d284aafba8fc4735)。

所有地址都在rinkeby网络上。

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

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

复制
相关文章

相似问题

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