Uniswap
Uniswap Dai getAmountsOut 返回 0
我正在嘗試獲得 100 dai 可以獲得的 WETH 數量:
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客戶端程式碼:
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
如何以 100 dai 而不是 0 返還我可以收到的任何部分 WETH?
您應該將 100 轉換為 WEI。
如果你使用 web3.js,你可以這樣做
web3.utils.toWei(100);