0

I am trying to buy DAI tokens from my smart contract using IUniswapV2Router02

 contract BuyExample{
          
    IUniswapV2Router02 usi = IUniswapV2Router02(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D);
       
        /**
         * @param amountOfEther The amount of ether the user has to buy the tokens.
         */
    function buyDai(uint256 amountOfEther) public payable returns(uint256){
    
    address cryptoToken = 0xc7AD46e0b8a400Bb3C915120d284AafbA8fc4735;
    
          uint deadline = now + 15; // I am only using 'now' for convenience, for mainnet I will pass deadline from frontend
            usi.swapETHForExactTokens.value(amountOfEther)(0, getPathForETHToToken(cryptoToken), address(this), deadline);
    
            // refund leftover ETH to user
            msg.sender.call.value(address(this).balance)("");
    
           return 1000;// a random value for now
        
        }

  function getPathForETHToToken(address crypto) private view returns (address[] memory) {
    address[] memory path = new address[](2);
    path[0] = usi.WETH();
    path[1] = crypto;
    
    return path;
  }

    }

The contract deploys successfully. I try to use it by calling:

c.buyDai(web3.utils.toWei('0.02'))

...from truffle console.

But it reverts with a long error message.

On kovan.etherscan.io, its status says Fail

What am I doing wrong please?

A running instance of the contract is here

3

Great, it seems you found my tutorial. You are mixing up the two different functions of

  1. swapETHForExactTokens
  2. swapExactETHForTokens

If you want to use the swapETHForExactTokens, the first parameter defines how many DAI you want to buy. You are passing along 0, so it fails. Instead pass the amount of DAI along and make sure to send enough ETH to pay for it. (you may add helper functions to get an ETH estimate, see my tutorial) And you will also need a fallback function to receive refunded ETH.

function buyDaiWithExactEth(uint256 amountOfDai) public payable returns(uint256) {
        address cryptoToken = 0xc7AD46e0b8a400Bb3C915120d284AafbA8fc4735;

        uint deadline = now + 15; // I am only using 'now' for convenience, for mainnet I will pass deadline from frontend
        usi.swapETHForExactTokens.value(msg.value)(amountOfDai, getPathForETHToToken(cryptoToken), address(this), deadline);

        // refund leftover ETH to user
        msg.sender.call.value(address(this).balance)("");

        return 1000; // not sure what this means?
}

receive() payable external {}

If you want to use swapExactETHForTokens, the first parameter simply defines the minimum amount of DAI that you want. So if you pass 0, it will always succeeed. You also don't need to refund any ETH, since it's all being used for the purchase.

function buyExactDaiWithEth() public payable returns(uint256) {
        address cryptoToken = 0xc7AD46e0b8a400Bb3C915120d284AafbA8fc4735;

        uint deadline = now + 15; // I am only using 'now' for convenience, for mainnet I will pass deadline from frontend
        usi.swapExactETHForTokens.value(msg.value)(0, getPathForETHToToken(cryptoToken), address(this), deadline);

        // no need to refund ETH
        return 1000;
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.