Skip to content

Commit

Permalink
feat(amm): handle empty pools
Browse files Browse the repository at this point in the history
  • Loading branch information
peterslany committed Apr 20, 2023
1 parent 22bb9aa commit 10d26ea
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 19 deletions.
19 changes: 7 additions & 12 deletions src/parachain/amm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,11 +326,7 @@ export class DefaultAMMAPI implements AMMAPI {
this._getPoolRewardAmountsYearly(lpTokenCurrencyId, blockTimeMs),
]);

// Do not include pools with zero liquidity.
if (this._poolHasZeroLiquidity(pooledCurrencies)) {
return null;
}

const isEmpty = this._poolHasZeroLiquidity(pooledCurrencies);
const totalSupply = new MonetaryAmount(lpToken, totalSupplyAmount);

return new StandardLiquidityPool(
Expand All @@ -339,7 +335,8 @@ export class DefaultAMMAPI implements AMMAPI {
yearlyRewards,
tradingFee,
isTradingActive,
totalSupply
totalSupply,
isEmpty
);
}

Expand Down Expand Up @@ -487,11 +484,7 @@ export class DefaultAMMAPI implements AMMAPI {
}
const { lpToken, actuallyPooledCurrencies, yearlyRewards, tradingFee, amplificationCoefficient, totalSupply } =
processedPoolData;

// Do not include pools with zero liquidity.
if (this._poolHasZeroLiquidity(actuallyPooledCurrencies)) {
return null;
}
const isEmpty = this._poolHasZeroLiquidity(actuallyPooledCurrencies);

if (poolData.isBase) {
return new StableLiquidityPool(
Expand All @@ -503,7 +496,8 @@ export class DefaultAMMAPI implements AMMAPI {
tradingFee,
poolId,
amplificationCoefficient,
totalSupply
totalSupply,
isEmpty
);
}

Expand All @@ -526,6 +520,7 @@ export class DefaultAMMAPI implements AMMAPI {
poolId,
amplificationCoefficient,
totalSupply,
isEmpty,
basePool
);
}
Expand Down
3 changes: 3 additions & 0 deletions src/parachain/amm/liquidity-pool/calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class LiquidityPoolCalculator<TLpToken extends LpCurrency> {
*
* @param amount Amount of one of the pooled currencies.
* @returns Monetary amounts of all pooled currencies in balanced proportion.
* @throws If pool is empty. Note: handle by checking `isEmpty` property of pool.
*/
public getLiquidityDepositInputAmounts(amount: MonetaryAmount<CurrencyExt>): Array<MonetaryAmount<CurrencyExt>> {
const inputCurrencyReserve = this._getCurrencyReserve(amount.currency);
Expand All @@ -41,6 +42,7 @@ class LiquidityPoolCalculator<TLpToken extends LpCurrency> {
* @note This method assumes all pooled currencies will be added in balance.
* @param amount Amount of one of the pooled currencies.
* @returns Expected amount of lp token that will be received after `amount` is added to pool.
* @throws If pool is empty. Note: handle by checking `isEmpty` property of pool.
*/
public getLiquidityDepositLpTokenAmount(amount: MonetaryAmount<CurrencyExt>): MonetaryAmount<TLpToken> {
const currencyReserveAmount = this._getCurrencyReserve(amount.currency);
Expand All @@ -57,6 +59,7 @@ class LiquidityPoolCalculator<TLpToken extends LpCurrency> {
* @note This method assumes all pooled currencies will be withdrawn in balance.
* @param amount Amount of liquidity in LP token to be withdrawn.
* @returns Amounts of pooled currencies to be returned to account.
* @throws If pool is empty. Note: handle by checking `isEmpty` property of pool.
*/
public getLiquidityWithdrawalPooledCurrencyAmounts(
amount: MonetaryAmount<TLpToken>
Expand Down
4 changes: 3 additions & 1 deletion src/parachain/amm/liquidity-pool/stable-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class StableLiquidityMetaPool extends StableLiquidityPool {
poolId: number,
amplificationCoefficient: Big,
totalSupply: MonetaryAmount<StableLpToken>,
isEmpty: boolean,
public basePool: StableLiquidityPool // Contains base pool object.
) {
super(
Expand All @@ -25,7 +26,8 @@ class StableLiquidityMetaPool extends StableLiquidityPool {
tradingFee,
poolId,
amplificationCoefficient,
totalSupply
totalSupply,
isEmpty
);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/parachain/amm/liquidity-pool/stable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class StableLiquidityPool extends LiquidityPoolCalculator<StableLpToken> impleme
public tradingFee: Big, // Decimal point
public poolId: number,
public amplificationCoefficient: Big,
public totalSupply: MonetaryAmount<StableLpToken>
public totalSupply: MonetaryAmount<StableLpToken>,
public isEmpty: boolean
) {
super(pooledCurrencies, totalSupply);
}
Expand Down
3 changes: 2 additions & 1 deletion src/parachain/amm/liquidity-pool/standard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class StandardLiquidityPool extends LiquidityPoolCalculator<StandardLpToken> imp
public rewardAmountsYearly: Array<MonetaryAmount<CurrencyExt>>,
public tradingFee: Big,
public isTradingActive: boolean, // True if in `Trading` state, false if in `Bootstrap` state
public totalSupply: MonetaryAmount<StandardLpToken>
public totalSupply: MonetaryAmount<StandardLpToken>,
public isEmpty: boolean
) {
super(pooledCurrencies, totalSupply);

Expand Down
12 changes: 8 additions & 4 deletions src/parachain/amm/liquidity-pool/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@ import { StableLiquidityPool } from "./stable";
import { isStablePool, isStandardPool, LiquidityPool } from "./types";
import { StableLiquidityMetaPool } from "./stable-meta";

const filterNonEmptyPools = (pools: Array<LiquidityPool>): Array<LiquidityPool> =>
pools.filter(({ isEmpty }) => !isEmpty);

/**
* Get all trading pairs based on provided pools.
*
* @param pools All standard and stable pools.
* @param nonEmptyPools All standard and stable pools.
* @returns {Array<TradingPair>} All trading pairs.
*/
const getAllTradingPairs = (pools: Array<LiquidityPool>): Array<TradingPair> => {
const stablePools = pools.filter(isStablePool);
const nonEmptyPools = filterNonEmptyPools(pools);
const stablePools = nonEmptyPools.filter(isStablePool);
const pairs: Array<TradingPair> = [];

pools.forEach((pool) => {
nonEmptyPools.forEach((pool) => {
if (isStandardPool(pool)) {
// Exclude pool in Bootstrap status
if (pool.isTradingActive) {
Expand Down Expand Up @@ -205,4 +209,4 @@ const getStableSwapOutputAmount = (
return outputAmount;
};

export { getAllTradingPairs, getStableSwapOutputAmount };
export { getAllTradingPairs, getStableSwapOutputAmount, filterNonEmptyPools };
1 change: 1 addition & 0 deletions src/parachain/amm/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface LiquidityPoolBase {
tradingFee: Big; // Decimal.
totalSupply: MonetaryAmount<LpCurrency>;
rewardAmountsYearly: Array<MonetaryAmount<CurrencyExt>>; // Array of monetary amounts containing reward per pool yearly.
isEmpty: boolean;
}

enum PoolType {
Expand Down

0 comments on commit 10d26ea

Please sign in to comment.