Javascript
從 uniswap 路由器 02 呼叫 getAmountsOut 後嘗試除法時出錯
我在使用 uniswap 路由器 02 通過元遮罩實現交換時遇到很多問題。我可以呼叫 getAmountsOut 函式,但在嘗試執行以下操作時出現錯誤:
const getAmountOutMin = async () => { const amounts = await quickswap.methods.getAmountsOut(amountIn, [baseContract, targetContract]).call(); console.log(amounts) setAmountOutMin(amounts[1].sub(amounts[1].div(10))); console.log(amountOutMin) }
我收到以下錯誤:
Unhandled Rejection (TypeError): amounts[1].div is not a function getAmountOutMin 133 | const amounts = await quickswap.methods.getAmountsOut(amountIn, [baseContract, targetContract]).call(); 134 | console.log(amounts) 135 | //Our execution price will be a bit different, we need some flexbility > 136 | setAmountOutMin(amounts[1].sub(amounts[1].div(10))); | ^ 137 | console.log(amountOutMin) 138 | } 139 | getAmountOutMin();
當我在控制台中記錄金額的值時,我得到了這個:
[ "1000000000000000000", "2767484742082040" ]
我錯過了什麼?
感謝 Ismael 幫助解決問題。
問題已通過轉換金額解決
$$ 1 $$到一個號碼。這是我所做的並且它有效:
// in useeffect const getAmountOutMin = async () => { setAmounts(await contract.methods.getAmountsOut(amountIn, [baseAddress, targetAddress]).call()))} // minimum value is 95% of original const amountOut1 = (Math.round(Number(amounts[1]) * 0.95)).toString() // amountOutMin input for swapping functions such as SwapExactETHForTokens setAmountOutMin(ethers.utils.parseUnits(amountOut1, 'wei'))