Skip to content

Commit

Permalink
add available lp normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
Ridel1e committed Oct 12, 2023
1 parent 65b682b commit 2cbc6ee
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
49 changes: 49 additions & 0 deletions src/common/utils/normalizeAvailableLp.ts
Original file line number Diff line number Diff line change
@@ -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];
};
16 changes: 14 additions & 2 deletions src/network/cardano/api/balance/lpBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Balance> = combineLatest([
balanceItems$,
ammPools$,
showUnverifiedPools$.pipe(
switchMap((showUnverifiedPools) => {
if (showUnverifiedPools) {
return unverifiedAmmPools$;
}
return allAmmPools$;
}),
),
]).pipe(
map(([balanceItems, pools]) =>
balanceItems.filter(([, info]) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -39,12 +40,14 @@ export const ActionsColumn: FC<ActionsColumnProps> = ({ 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,
Expand Down

0 comments on commit 2cbc6ee

Please sign in to comment.