|
|
@@ -16,7 +16,8 @@ import {
|
|
|
amount2Decimal,
|
|
|
PriceRoundingType,
|
|
|
fetchToken,
|
|
|
- getErc20TokenContract
|
|
|
+ getErc20TokenContract,
|
|
|
+ TokenInfoFormatted
|
|
|
} from 'iziswap-sdk/lib/base'
|
|
|
import Web3 from 'web3'
|
|
|
import { BigNumber } from 'bignumber.js'
|
|
|
@@ -153,18 +154,17 @@ export class Web3Service {
|
|
|
const zkWallet = new Wallet(account.privateKey).connect(provider)
|
|
|
const chain = initialChainTable[chainId]
|
|
|
const web3 = new Web3(new Web3.providers.HttpProvider(web3Config[account.network].zksyncRpcUrl))
|
|
|
- console.log('address: ', zkWallet.address)
|
|
|
+ Logger.log(`address: ${zkWallet.address}`, 'addLiquidity')
|
|
|
|
|
|
const liquidityManagerContract = getLiquidityManagerContract(
|
|
|
web3Config[account.network].liquidityManagerAddress,
|
|
|
web3 as any
|
|
|
)
|
|
|
- console.log('liquidity manager address: ', web3Config[account.network].liquidityManagerAddress)
|
|
|
|
|
|
const tokenA = getGasToken(chainId)
|
|
|
- console.log('tokenA: ', tokenA)
|
|
|
+ Logger.log(`tokenA: ${JSON.stringify(tokenA, null, 4)}`, 'addLiquidity')
|
|
|
const tokenB = await fetchToken(web3Config[account.network].zkUsdcAddress, chain, web3 as any)
|
|
|
- console.log('tokenB: ', tokenB)
|
|
|
+ Logger.log(`tokenB: ${JSON.stringify(tokenB, null, 4)}`, 'addLiquidity')
|
|
|
|
|
|
await this.approve(
|
|
|
tokenB.address,
|
|
|
@@ -175,20 +175,20 @@ export class Web3Service {
|
|
|
|
|
|
const fee = 2000 // 2000 means 0.2%
|
|
|
const poolAddress = await getPoolAddress(liquidityManagerContract, tokenA, tokenB, fee)
|
|
|
- console.log('pool address: ', poolAddress)
|
|
|
+ Logger.log(`poolAddress: ${poolAddress}`, 'addLiquidity')
|
|
|
|
|
|
const poolContract = getPoolContract(poolAddress, web3 as any)
|
|
|
const state = await getPoolState(poolContract)
|
|
|
const currentPrice = point2PriceDecimal(tokenA, tokenB, state.currentPoint)
|
|
|
- Logger.log(`current price: 1${tokenA.symbol}=${currentPrice}${tokenB.symbol}`, 'addLiquidity')
|
|
|
+ Logger.log(`current point: ${state.currentPoint}`, 'addLiquidity')
|
|
|
|
|
|
const point1 = priceDecimal2Point(tokenA, tokenB, currentPrice * 0.98, PriceRoundingType.PRICE_ROUNDING_NEAREST)
|
|
|
const point2 = priceDecimal2Point(tokenA, tokenB, currentPrice * 1.02, PriceRoundingType.PRICE_ROUNDING_NEAREST)
|
|
|
- console.log('point range', point1, point2)
|
|
|
+ Logger.log(`point range: ${point1} - ${point2}`, 'addLiquidity')
|
|
|
let leftPoint = Math.min(point1, point2)
|
|
|
let rightPoint = Math.max(point1, point2)
|
|
|
const pointDelta = await getPointDelta(poolContract)
|
|
|
- console.log('point delta: ', pointDelta)
|
|
|
+ Logger.log(`point delta: ${pointDelta}`, 'addLiquidity')
|
|
|
leftPoint = pointDeltaRoundingDown(leftPoint, pointDelta)
|
|
|
rightPoint = pointDeltaRoundingUp(rightPoint, pointDelta)
|
|
|
|
|
|
@@ -213,8 +213,33 @@ export class Web3Service {
|
|
|
minAmountA: maxTestA.times(0.985).toFixed(0),
|
|
|
minAmountB: maxTestB.times(0.985).toFixed(0)
|
|
|
}
|
|
|
- console.log(JSON.stringify(mintParams, null, 4))
|
|
|
- console.log(amount2Decimal(new BigNumber(maxTestB), tokenB))
|
|
|
+ Logger.log(
|
|
|
+ `tokenAtoPay: ${amount2Decimal(new BigNumber(maxTestA), tokenA)}, tokenBtoPay: ${amount2Decimal(
|
|
|
+ new BigNumber(maxTestB),
|
|
|
+ tokenB
|
|
|
+ )}`,
|
|
|
+ 'addLiquidity'
|
|
|
+ )
|
|
|
+ const balanceA = await zkWallet.getBalance()
|
|
|
+ const balanceB = await zkWallet.getBalance(tokenB.address)
|
|
|
+ if (new BigNumber(balanceA.toString()).lt(maxTestA)) {
|
|
|
+ throw new InternalServerErrorException(
|
|
|
+ `${tokenA.symbol}余额不足, 需要${ethers.utils.formatUnits(
|
|
|
+ maxTestA.toString(),
|
|
|
+ tokenA.decimal
|
|
|
+ )}, 当前${ethers.utils.formatUnits(balanceA, tokenA.decimal)}`
|
|
|
+ )
|
|
|
+ }
|
|
|
+ if (new BigNumber(balanceB.toString()).lt(maxTestB)) {
|
|
|
+ throw new InternalServerErrorException(
|
|
|
+ `${tokenB.symbol}余额不足, 需要${ethers.utils.formatUnits(
|
|
|
+ maxTestB.toString(),
|
|
|
+ tokenB.decimal
|
|
|
+ )}, 当前${ethers.utils.formatUnits(balanceB, tokenB.decimal)}`
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ Logger.log(`mintParams: ${JSON.stringify(mintParams, null, 4)}`, 'addLiquidity')
|
|
|
|
|
|
const gasPrice = await web3.eth.getGasPrice()
|
|
|
const { mintCalling, options } = getMintCall(
|
|
|
@@ -228,9 +253,8 @@ export class Web3Service {
|
|
|
if (calling instanceof Array) {
|
|
|
calling = liquidityManagerContract.methods.multicall(mintCalling)
|
|
|
}
|
|
|
- console.log({ ...options, from: zkWallet.address })
|
|
|
const gasLimit = await calling.estimateGas({ from: zkWallet.address })
|
|
|
- console.log('gas limit: ', gasLimit)
|
|
|
+ Logger.log(`gas limit: ${gasLimit}`, 'addLiquidity')
|
|
|
|
|
|
// sign transaction
|
|
|
const tx = await zkWallet.sendTransaction({
|
|
|
@@ -240,7 +264,7 @@ export class Web3Service {
|
|
|
data: calling.encodeABI(),
|
|
|
gasLimit
|
|
|
})
|
|
|
- console.log('tx: ', tx)
|
|
|
+ Logger.log(`tx hash: ${tx.hash}`, 'addLiquidity')
|
|
|
return new Web3Result(account.address, tx.hash, web3Config[account.network], true)
|
|
|
}
|
|
|
|