Ropsten

無效的 BigNumber 值 Uniswap 和 Ethers JS

  • October 2, 2021

嘿伙計們,我正在使用 Uniswap Javascript SDK 和 Ethers JS 進行交換,但我得到了這個

Error: invalid BigNumber value (argument="value", value=[-626648004,3158], code=INVALID_ARGUMENT, version=bignumber/5.1.1)

這是我的程式碼:


           await window.ethereum.enable();
           let provider = new ethers.getDefaultProvider('ropsten');
           const signer =  (new ethers.providers.Web3Provider(window.ethereum)).getSigner();
           console.log(provider.network.name);

           const amountIn = '1000000000000000000'; // 1 WETH

           const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
         
           const DENT = new Token(ChainId.ROPSTEN, "0xa51f5e00b3a454ba56c4add590d5cfeb2d197ae0", 8);
           const pair = await Fetcher.fetchPairData(DENT, WETH[DENT.chainId]);

           const route = new Route([pair], WETH[DENT.chainId]);
           console.log(route.midPrice.toSignificant(8));

           const trade = new Trade(route,  new TokenAmount(WETH[DENT.chainId], amountIn),  TradeType.EXACT_INPUT);

           const slippageTolerance = new Percent('50', '10000');  // 50 bips, or 0.50%

           const amountOutMin = trade.minimumAmountOut(slippageTolerance).raw; // needs to be converted to e.g. hex
           const path = [WETH[DENT.chainId].address, DENT.address];
           const to =  accounts[0]; // should be a checksummed recipient address

           const deadline = Math.floor(Date.now() / 1000) + 60 * 20; // 20 minutes from the current Unix time
           const value = trade.inputAmount.raw;  // // needs to be converted to e.g. hex

           const account = signer.connectUnchecked(provider);
      

           const uniswap = new ethers.Contract('0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D',['function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline) external payable returns (uint[] memory amounts)'], account);
           const tx = await uniswap.swapExactETHForTokens(amountOutMin, path, to, deadline, {value: value, gasPrice: 20e9});

我嘗試使用 ether 的 BigNumber 建構子,但仍然遇到同樣的錯誤。有誰知道發生了什麼?

我遇到了同樣的問題,我花了很長時間才找到問題的根本原因,因為錯誤消息並不是很有幫助。

只需將amountOutMinandvalue值轉換為String

const tx = await uniswap.swapExactETHForTokens(
     String(amountOutMin),
     path,
     to,
     deadline,
     { value: String(value), gasPrice: 20e9 }
   );

它現在應該像魅力一樣工作(:

你得到的錯誤是因為你的程式碼[-626648004,3158]值的某個地方正在被傳遞。

你能告訴哪一行給出了那個錯誤或提供堆棧跟踪嗎?這些資訊可以幫助我們幫助您:)

此外,如果您找到給出錯誤的行,您能否console.log在 JS 程式碼中輸入該行中涉及的變數輸入,其中之一應該是[-626648004,3158].

引用自:https://ethereum.stackexchange.com/questions/97788