Uniswap – Troubleshooting Dai getAmountsOut Returning 0

daiuniswap

I'm trying to get the amount of WETH I can receive for 100 dai:

uniswapController.sol

contract UniswapController {
    address internal constant UNISWAP_ROUTER_ADDRESS = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;

    IUniswapV2Router02 public uniswapRouter;

    constructor() public {
        uniswapRouter = IUniswapV2Router02(UNISWAP_ROUTER_ADDRESS);
    }

    function getAmountsOut(uint fundingamount, address[] memory path) public view returns (uint[] memory) {
        return uniswapRouter.getAmountsOut(fundingamount, path);     
    }

js client code:

dai.address = 0x6B175474E89094C44Da98b954EedeAC495271d0F;
ethereum.address = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;

let amounts = await uniswapController.getAmountsOut(100, [dai.address, ethereum.address]);

let daioutput = amounts[0].toNumber();
let ethereumoutput = amounts[1].toNumber();

console.log("Dai output:", daioutput); // returns 100
console.log("ethereum output:", ethereumoutput); // returns 0

How do I return whatever partial WETH I can receive for 100 dai instead of just 0?

Best Answer

You should convert 100 to WEI.

If you use web3.js, you can do this

web3.utils.toWei(100);
Related Topic