Skip to content

Commit

Permalink
Calculating max swap/liquidity (#1306)
Browse files Browse the repository at this point in the history
* calc max

* more  max checks

* fix description
  • Loading branch information
mihaisc authored Feb 22, 2022
1 parent 7fed0aa commit 1a12a4b
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 7 deletions.
53 changes: 46 additions & 7 deletions src/pools/balancer/Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ import {
PoolPriceAndFees
} from '../../@types'
import { Config } from '../../models'
import {
getMaxAddLiquidity,
getMaxRemoveLiquidity,
getMaxSwapExactIn,
getMaxSwapExactOut
} from '../../utils/PoolHelpers'
import Decimal from 'decimal.js'
const MaxUint256 =
'115792089237316195423570985008687907853269984665640564039457584007913129639934'

Expand Down Expand Up @@ -847,6 +854,11 @@ export class Pool {
this.config
)

const maxSwap = await getMaxSwapExactIn(this, poolAddress, tokenInOutMarket.tokenIn)
if (new Decimal(amountsInOutMaxFee.tokenAmountIn).greaterThan(maxSwap)) {
throw new Error(`tokenAmountIn is greater than ${maxSwap.toString()}`)
}

amountsInOutMaxFee.tokenAmountIn = await amountToUnits(
this.web3,
tokenInOutMarket.tokenIn,
Expand Down Expand Up @@ -971,6 +983,11 @@ export class Pool {
)
let result = null

const maxSwap = await getMaxSwapExactOut(this, poolAddress, tokenInOutMarket.tokenIn)
if (new Decimal(amountsInOutMaxFee.tokenAmountOut).greaterThan(maxSwap)) {
throw new Error(`tokenAmountOut is greater than ${maxSwap.toString()}`)
}

amountsInOutMaxFee.maxAmountIn = await amountToUnits(
this.web3,
tokenInOutMarket.tokenIn,
Expand Down Expand Up @@ -1233,7 +1250,6 @@ export class Pool {
* Pay tokenAmountIn of baseToken to join the pool, getting poolAmountOut of the pool shares.
* @param {String} account
* @param {String} poolAddress
* @param {String} tokenIn
* @param {String} tokenAmountIn exact number of base tokens to spend
* @param {String} minPoolAmountOut minimum of pool shares expectex
* @return {TransactionReceipt}
Expand All @@ -1249,12 +1265,13 @@ export class Pool {
this.config
)
let result = null
const tokenIn = await this.getBaseToken(poolAddress)
const maxSwap = await getMaxAddLiquidity(this, poolAddress, tokenIn)
if (new Decimal(tokenAmountIn).greaterThan(maxSwap)) {
throw new Error(`tokenAmountOut is greater than ${maxSwap.toString()}`)
}

const amountInFormatted = await amountToUnits(
this.web3,
await this.getBaseToken(poolAddress),
tokenAmountIn
)
const amountInFormatted = await amountToUnits(this.web3, tokenIn, tokenAmountIn)
const estGas = await this.estJoinswapExternAmountIn(
account,
poolAddress,
Expand Down Expand Up @@ -1321,7 +1338,6 @@ export class Pool {
* Pay poolAmountIn pool shares into the pool, getting minTokenAmountOut of the baseToken
* @param {String} account
* @param {String} poolAddress
* @param {String} tokenOut
* @param {String} poolAmountIn exact number of pool shares to spend
* @param {String} minTokenAmountOut minimum amount of basetokens expected
* @return {TransactionReceipt}
Expand All @@ -1337,6 +1353,18 @@ export class Pool {
this.config
)
let result = null
const tokenOut = await this.getBaseToken(poolAddress)

const tokenAmountOut = await this.calcSingleOutGivenPoolIn(
poolAddress,
tokenOut,
poolAmountIn
)

const maxSwap = await getMaxRemoveLiquidity(this, poolAddress, tokenOut)
if (new Decimal(tokenAmountOut).greaterThan(maxSwap)) {
throw new Error(`tokenAmountOut is greater than ${maxSwap.toString()}`)
}

const minTokenOutFormatted = await amountToUnits(
this.web3,
Expand Down Expand Up @@ -1451,6 +1479,12 @@ export class Pool {
this.config
)

const maxSwap = await getMaxSwapExactOut(this, poolAddress, tokenIn)

if (new Decimal(tokenAmountOut).greaterThan(maxSwap)) {
throw new Error(`tokenAmountOut is greater than ${maxSwap.toString()}`)
}

const amountOutFormatted = await amountToUnits(this.web3, tokenOut, tokenAmountOut)

let amount = null
Expand Down Expand Up @@ -1509,6 +1543,11 @@ export class Pool {
this.config
)

const maxSwap = await getMaxSwapExactIn(this, poolAddress, tokenIn)
if (new Decimal(tokenAmountIn).greaterThan(maxSwap)) {
throw new Error(`tokenAmountIn is greater than ${maxSwap.toString()}`)
}

const amountInFormatted = await amountToUnits(this.web3, tokenIn, tokenAmountIn)

let amount = null
Expand Down
47 changes: 47 additions & 0 deletions src/utils/PoolHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import Decimal from 'decimal.js'
import { Pool } from '..'

export function calcMaxExactOut(balance: string): Decimal {
return new Decimal(balance).div(3.01)
}

export function calcMaxExactIn(balance: string): Decimal {
return new Decimal(balance).div(2.01)
}
export async function getMaxSwapExactOut(
poolInstance: Pool,
poolAddress: string,
tokenAddress: string
): Promise<Decimal> {
const reserve = await poolInstance.getReserve(poolAddress, tokenAddress)
return calcMaxExactOut(reserve)
}

export async function getMaxSwapExactIn(
poolInstance: Pool,
poolAddress: string,
tokenAddress: string
): Promise<Decimal> {
const reserve = await poolInstance.getReserve(poolAddress, tokenAddress)
return calcMaxExactIn(reserve)
}

export async function getMaxAddLiquidity(
poolInstance: Pool,
poolAddress: string,
tokenAddress: string
): Promise<Decimal> {
const reserve = await poolInstance.getReserve(poolAddress, tokenAddress)

return calcMaxExactIn(reserve)
}

export async function getMaxRemoveLiquidity(
poolInstance: Pool,
poolAddress: string,
tokenAddress: string
): Promise<Decimal> {
const reserve = await poolInstance.getReserve(poolAddress, tokenAddress)

return calcMaxExactIn(reserve)
}

0 comments on commit 1a12a4b

Please sign in to comment.