Solidity

UniswapV2 addLiquidity 恢復 ds-math-sub-underflow

  • October 15, 2021

我正在嘗試將 Uniswapv2Router 與 Truffle 和 Ganache 一起使用,但無法確定我做錯了什麼。我正在使用 addLiquidty 方法如下: await uniswapV2Router02.addLiquidity(token0.address, token1.address, 500, 250, 250 , 125, accounts[0], 1618158769);拋出錯誤 Error: Returned error: VM Exception while processing transaction: revert ds-math-sub-underflow -- Reason given: ds-math-sub-underflow.

但是如果我註釋掉.minto(to)程式碼會正確執行直到那裡。

   function addLiquidity(
       address tokenA,
       address tokenB,
       uint amountADesired,
       uint amountBDesired,
       uint amountAMin,
       uint amountBMin,
       address to,
       uint deadline
   ) external ensure(deadline) returns (uint amountA, uint amountB, uint liquidity) {
       (amountA, amountB) = _addLiquidity(tokenA, tokenB, amountADesired, amountBDesired, amountAMin, amountBMin);
       address pair = UniswapV2Library.pairFor(factory, tokenA, tokenB);
       TransferHelper.safeTransferFrom(tokenA, msg.sender, pair, amountA);
       TransferHelper.safeTransferFrom(tokenB, msg.sender, pair, amountB);
       IUniswapV2Pair(pair);//.mint(to);
   }

即使我有一個簡單的薄荷實現只是為了測試正在發生的事情,這種情況也會發生。

 function mint(address to) external lock returns (uint liquidity) {
       liquidity = 3;
   }

問題是因為在使用時,應該按照文件https://uniswap.org/docs/v2/smart-contract-integration/quick-start/UniswapV2Factory中的說明部署建構目錄中的預編譯UniswapV2Factory合約。因此,即使我在契約中註釋掉了某些內容,它也沒有任何效果,因為它也使用了預編譯的契約。另一方面,錯誤是因為在合約上,檢索其每個令牌的餘額,並且因為它沒有足夠的,導致.@uniswap``UniswapV2Pair``UniswapV2Factory``UniswapV2Pair``ds-math-sub-underflow``UniswapV2Pair``function mint(address to)``liquidity = Math.sqrt(amount0.mul(amount1)).sub(MINIMUM_LIQUIDITY);``underflow

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