Skip to content

Commit

Permalink
add new locks
Browse files Browse the repository at this point in the history
  • Loading branch information
Ridel1e committed Feb 19, 2024
1 parent a6db1cc commit 918576a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 57 deletions.
6 changes: 2 additions & 4 deletions src/network/cardano/api/ammPools/analyticPoolNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export interface AmmPoolDescriptor {
readonly treasuryY: string;
readonly verified: true;
readonly poolLqBound: string;
readonly version: 'v2' | 'v3' | 'v4';
readonly version: 'v1' | 'v2' | 'v3' | 'v4';
};
readonly metrics?: {
readonly poolId: PoolId;
Expand Down Expand Up @@ -75,9 +75,7 @@ export class AnalyticPoolNetwork {
)
.then((res) =>
res.data.filter(
(apd) =>
apd.pool.poolType === 'cfmm' &&
(apd.pool.version === 'v4' || apd.pool.version === 'v2'),
(apd) => apd.pool.poolType === 'cfmm' && apd.pool.version !== 'v3',
),
);
}
Expand Down
72 changes: 46 additions & 26 deletions src/network/cardano/api/common/tokenLocks.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { extractPaymentCred } from '@spectrumlabs/cardano-dex-sdk';
import { RustModule } from '@spectrumlabs/cardano-dex-sdk/build/main/utils/rustLoader';
import axios from 'axios';
import {
first,
map,
Observable,
publishReplay,
refCount,
switchMap,
} from 'rxjs';
import groupBy from 'lodash/groupBy';
import last from 'lodash/last';
import uniq from 'lodash/uniq';
import uniqBy from 'lodash/uniqBy';
import { combineLatest, defer, filter, first, map, switchMap } from 'rxjs';

import { PoolId } from '../../../../common/types.ts';
import { cardanoNetworkData } from '../../utils/cardanoNetworkData.ts';
import { getAddresses } from '../addresses/addresses.ts';

export interface CardanoTokenLock {
Expand All @@ -21,23 +19,45 @@ export interface CardanoTokenLock {
readonly redeemer: string;
}

export const getTokenLocksByPoolId = (
poolId: PoolId,
): Observable<CardanoTokenLock[]> =>
getAddresses().pipe(
first(),
map((addresses) =>
const getLocksByPaymentCreds = (creds: string[]): Promise<CardanoTokenLock[]> =>
axios
.post(`${cardanoNetworkData.analyticUrl}history/locks`, creds)
.then((res) => res.data)
.catch(() => [] as CardanoTokenLock[]);

export const locks$ = defer(() => getAddresses()).pipe(
filter((addresses) => addresses.length > 0),
first(),
map((addresses) =>
uniq(
addresses.map((addr) => extractPaymentCred(addr, RustModule.CardanoWasm)),
),
switchMap((creds) => {
return axios
.get<CardanoTokenLock[]>(
`http://195.201.9.29:8091/v1/pools/locks?poolId=${poolId}`,
)
.then((res) => res.data)
.then((data) => data.filter((i) => creds.includes(i.redeemer)))
.catch(() => []);
}),
publishReplay(1),
refCount(),
);
),
switchMap((creds) => {
const credsBatches: string[][] = [[]];
//
for (const cred of creds) {
const lastItem = last(credsBatches);
if (!lastItem) {
break;
}
if (lastItem.length >= 400) {
credsBatches.push([cred]);
} else {
lastItem.push(cred);
}
}
return combineLatest(
credsBatches.map((batch) => getLocksByPaymentCreds(batch)),
);
}),
map((locksBatches) => {
return groupBy(
uniqBy(
locksBatches.flatMap((lb) => lb),
(item) => item.entityId,
),
(item) => item.poolId,
);
}),
);
49 changes: 22 additions & 27 deletions src/network/cardano/api/positions/positions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { DateTime } from 'luxon';
import {
combineLatest,
debounceTime,
defaultIfEmpty,
map,
publishReplay,
refCount,
Expand All @@ -19,7 +18,7 @@ import {
unverifiedAmmPools$,
} from '../ammPools/ammPools';
import { lpBalance$ } from '../balance/lpBalance';
import { getTokenLocksByPoolId } from '../common/tokenLocks.ts';
import { locks$ } from '../common/tokenLocks.ts';
import { networkContext$ } from '../networkContext/networkContext';

export const positions$ = combineLatest([
Expand All @@ -32,35 +31,31 @@ export const positions$ = combineLatest([
}),
),
lpBalance$.pipe(startWith(new Balance([]))),
locks$,
networkContext$,
]).pipe(
debounceTime(200),
switchMap(([ammPools, lpWalletBalance, networkContext]) =>
combineLatest(
ammPools
.filter((ap) => lpWalletBalance.get(ap.lp.asset).isPositive())
.map((ap) =>
getTokenLocksByPoolId(ap.id).pipe(
map((locks) => {
return new Position(
ap,
lpWalletBalance.get(ap.lp.asset),
false,
locks.map((l) => ({
redeemer: l.redeemer,
active: true,
boxId: l.entityId,
deadline: 0,
unlockDate: DateTime.fromMillis(l.deadline),
lockedAsset: new Currency(BigInt(l.amount), ap.lp.asset),
currentBlock: networkContext.height,
})),
[],
);
}),
map(([ammPools, lpWalletBalance, locks, networkContext]) =>
ammPools
.filter((ap) => lpWalletBalance.get(ap.lp.asset).isPositive())
.map(
(ap) =>
new Position(
ap,
lpWalletBalance.get(ap.lp.asset),
false,
locks[ap.id]?.map((l) => ({
redeemer: l.redeemer,
active: true,
boxId: l.entityId,
deadline: 0,
unlockDate: DateTime.fromMillis(l.deadline),
lockedAsset: new Currency(BigInt(l.amount), ap.lp.asset),
currentBlock: networkContext.height,
})) || [],
[],
),
),
).pipe(defaultIfEmpty([])),
),
),
publishReplay(1),
refCount(),
Expand Down

0 comments on commit 918576a

Please sign in to comment.