From 2cbc6ee18a87e864be3ac5bd4d3598e260fce4ce Mon Sep 17 00:00:00 2001 From: ridel1e Date: Thu, 12 Oct 2023 12:51:06 +0300 Subject: [PATCH] add available lp normalization --- src/common/utils/normalizeAvailableLp.ts | 49 +++++++++++++++++++ src/network/cardano/api/balance/lpBalance.ts | 16 +++++- .../columns/ActionsColumn.tsx | 9 ++-- 3 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 src/common/utils/normalizeAvailableLp.ts diff --git a/src/common/utils/normalizeAvailableLp.ts b/src/common/utils/normalizeAvailableLp.ts new file mode 100644 index 000000000..95132864e --- /dev/null +++ b/src/common/utils/normalizeAvailableLp.ts @@ -0,0 +1,49 @@ +import { EmissionLP } from '@spectrumlabs/cardano-dex-sdk'; + +import { networkAsset } from '../../network/cardano/api/networkAsset/networkAsset'; +import { Currency } from '../models/Currency'; +import { Position } from '../models/Position'; + +const ADA_THRESHOLD = new Currency('15', networkAsset); + +// Only for n2t +export const normalizeAvailableLp = ( + position: Position, +): [Currency, Currency, Currency] => { + if ( + !position.pool.x.isAssetEquals(networkAsset) && + !position.pool.y.isAssetEquals(networkAsset) + ) { + return [position.availableLp, position.availableX, position.availableY]; + } + const totalLockedLp = EmissionLP - position.pool.lp.amount; + const [totalLockedX, totalLockedY] = position.pool.shares( + new Currency(totalLockedLp, position.pool.lp.asset), + ); + const totalAda = position.pool.x.isAssetEquals(networkAsset) + ? position.pool.x + : position.pool.y; + const totalLockedAda = totalLockedX.isAssetEquals(networkAsset) + ? totalLockedX + : totalLockedY; + const userLockedAda = position.availableX.isAssetEquals(networkAsset) + ? position.availableX + : position.availableY; + + if ( + totalLockedAda.minus(userLockedAda).gt(ADA_THRESHOLD) || + userLockedAda.lte(ADA_THRESHOLD) + ) { + return [position.availableLp, position.availableX, position.availableY]; + } + + const availableAdaToUnlock = userLockedAda.minus(ADA_THRESHOLD); + const availableLpToUnlock = new Currency( + (availableAdaToUnlock.amount * totalLockedLp) / totalAda.amount, + position.pool.lp.asset, + ); + const [availableXToUnlock, availableYToUnlock] = + position.pool.shares(availableLpToUnlock); + + return [availableLpToUnlock, availableXToUnlock, availableYToUnlock]; +}; diff --git a/src/network/cardano/api/balance/lpBalance.ts b/src/network/cardano/api/balance/lpBalance.ts index 26edf9027..50a64ae1a 100644 --- a/src/network/cardano/api/balance/lpBalance.ts +++ b/src/network/cardano/api/balance/lpBalance.ts @@ -5,16 +5,28 @@ import { Observable, publishReplay, refCount, + switchMap, } from 'rxjs'; import { AssetInfo } from '../../../../common/models/AssetInfo'; import { Balance } from '../../../../common/models/Balance'; -import { ammPools$ } from '../ammPools/ammPools'; +import { + allAmmPools$, + showUnverifiedPools$, + unverifiedAmmPools$, +} from '../ammPools/ammPools'; import { balanceItems$ } from './balance'; export const lpBalance$: Observable = combineLatest([ balanceItems$, - ammPools$, + showUnverifiedPools$.pipe( + switchMap((showUnverifiedPools) => { + if (showUnverifiedPools) { + return unverifiedAmmPools$; + } + return allAmmPools$; + }), + ), ]).pipe( map(([balanceItems, pools]) => balanceItems.filter(([, info]) => diff --git a/src/pages/Liquidity/common/columns/PoolsOrPositionsColumns/columns/ActionsColumn.tsx b/src/pages/Liquidity/common/columns/PoolsOrPositionsColumns/columns/ActionsColumn.tsx index 735057cc3..0f7f498d7 100644 --- a/src/pages/Liquidity/common/columns/PoolsOrPositionsColumns/columns/ActionsColumn.tsx +++ b/src/pages/Liquidity/common/columns/PoolsOrPositionsColumns/columns/ActionsColumn.tsx @@ -12,6 +12,7 @@ import { useNavigate } from 'react-router-dom'; import { useObservable } from '../../../../../../common/hooks/useObservable'; import { AmmPool } from '../../../../../../common/models/AmmPool'; import { isDeprecatedPool } from '../../../../../../common/utils/isDeprecatedPool'; +import { normalizeAvailableLp } from '../../../../../../common/utils/normalizeAvailableLp'; import { redeem } from '../../../../../../gateway/api/operations/redeem'; import { getPositionByAmmPoolId } from '../../../../../../gateway/api/positions'; @@ -39,12 +40,14 @@ export const ActionsColumn: FC = ({ ammPool }) => { if (!position) { return; } + const [availableLp, availableX, availableY] = + normalizeAvailableLp(position); redeem( position.pool, { - lpAmount: position.availableLp, - xAmount: position.availableX, - yAmount: position.availableY, + lpAmount: availableLp, + xAmount: availableX, + yAmount: availableY, percent: 100, }, true,