I wrote code to buy DAI using ETH on Uniswap V2 as follows:

function buyCryptoOnUniswap(uint256 etherCost , address cryptoToken) public payable returns(uint256){
//address cryptoToken = 0x4F96Fe3b7A6Cf9725f59d353F723c1bDb64CA6Aa;

require(etherCost <= address(this).balance , "Insufficient funds")

    uint256 cryptoAmountPurchasable = estimateTokensThatETHCanBuy(etherCost , cryptoToken)[1];
    cryptoAmountPurchasable = cryptoAmountPurchasable.mul(11).div(10);// multiply by 1.1
    uint deadline = now + 300; // using 'now' for convenience, for mainnet pass deadline from frontend!

    usi.swapExactETHForTokens.value(etherCost)(0, getPathForETHToToken(cryptoToken), address(this), deadline);

   return cryptoAmountPurchasable;


 function estimateTokensThatETHCanBuy(uint ethAmount , address crypto) public view returns (uint[] memory) {
       return usi.getAmountsOut(ethAmount, getPathForETHToToken(crypto));

  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 code works but I was expecting an exchange rate of about 180 -> 230 but to my dismay, 0.1 ETH only gave me 1.926 DAI and 1 ETH gave me about 19.26 DAI also.

Here is the transaction using 0.1ETH.

I am using the DAI token for kovan: 0x4F96Fe3b7A6Cf9725f59d353F723c1bDb64CA6Aa.

Is something wrong with my code?

Please help.


I wrote code to buy DAI on Kyber and I got it at the expected rate range:

See the transaction here.

  • Take a good look at .mul(11).div(10);// divide by 1.1 – goodvibration Jul 2 '20 at 12:11
  • Hmm, true that...should be multiply by 1.1. Thanks. But it would still be off by a factor of at least 10, though – gbenroscience Jul 2 '20 at 12:14

This is because you're doing this on Kovan. The DAI/WETH uniswap pool on Kovan obviously doesn't use real (with value) DAI or WETH, so there is no arbitrage to push it to the market rate of ETH/DAI. See the pool here and look at the token amounts, ~660 kWETH and ~25,000 kDAI which gives a rate of ~38 DAI per ETH. The price has gone up since your test transaction, but it's just due to the testnet using valueless tokens


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.