Skip to content

Commit

Permalink
Merge pull request #362 from ergolabs/feature/ergopad-filter
Browse files Browse the repository at this point in the history
Feature/ergopad filter
  • Loading branch information
yasha-black authored Feb 2, 2022
2 parents 9981627 + 592b02c commit 04a30cb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 115 deletions.
7 changes: 7 additions & 0 deletions src/network/ergo/ammPools/ammPools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {

import { AmmPool } from '../../../common/models/AmmPool';
import { appTick$ } from '../../../common/streams/appTick';
import { LOCKED_TOKEN_ID } from '../../../components/common/ActionForm/ActionButton/ActionButton';
import { nativeNetworkPools, networkPools } from './common';

const BlacklistedAmmPoolId =
Expand Down Expand Up @@ -44,6 +45,12 @@ export const ammPools$ = combineLatest([
.concat(networkPools)
.filter((p) => p.id != BlacklistedAmmPoolId),
),
map((pools) =>
pools.filter(
(p) =>
p.x.asset.id !== LOCKED_TOKEN_ID && p.y.asset.id !== LOCKED_TOKEN_ID,
),
),
map((pools) => pools.map((p) => new AmmPool(p))),
publishReplay(1),
refCount(),
Expand Down
5 changes: 1 addition & 4 deletions src/services/new/assets.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AssetInfo } from '@ergolabs/ergo-sdk/build/main/entities/assetInfo';
import { find, uniqBy } from 'lodash';
import { uniqBy } from 'lodash';
import { map, Observable, publishReplay, refCount } from 'rxjs';

import { pools$ } from './pools';
Expand All @@ -18,9 +18,6 @@ export const lpAssets$ = pools$.pipe(
refCount(),
);

export const getAssetById = (id: string): Observable<AssetInfo> =>
assets$.pipe(map((assets) => find(assets, ['id', id])!));

export const getAvailableAssetFor = (
assetId: string,
): Observable<AssetInfo[]> =>
Expand Down
119 changes: 8 additions & 111 deletions src/services/new/pools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,23 @@ import {
makePools,
NetworkPools,
} from '@ergolabs/ergo-dex-sdk';
import { TokenLock } from '@ergolabs/ergo-dex-sdk/build/main/security/entities';
import { ErgoBox } from '@ergolabs/ergo-sdk';
import {
combineLatest,
defer,
from,
interval,
map,
Observable,
publishReplay,
refCount,
startWith,
switchMap,
zip,
} from 'rxjs';

import { AmmPool } from '../../common/models/AmmPool';
import { Currency } from '../../common/models/Currency';
import { tokenLocks$ } from '../../network/ergo/common/tokenLocks';
import { getListAvailableTokens } from '../../utils/getListAvailableTokens';
import { LOCKED_TOKEN_ID } from '../../components/common/ActionForm/ActionButton/ActionButton';
import { explorer } from '../explorer';
import { UPDATE_TIME, utxos$ } from './core';
import { UPDATE_TIME } from './core';

export const networkPools = (): NetworkPools => makePools(explorer);
export const nativeNetworkPools = (): NetworkPools => makeNativePools(explorer);
Expand All @@ -40,14 +35,6 @@ export interface PoolData {
readonly yAmount: Currency;
}

const utxosToTokenIds = (utxos: ErgoBox[]): string[] =>
Object.values(getListAvailableTokens(utxos)).map((token) => token.tokenId);

const filterPoolsByTokenIds = (
pools: BaseAmmPool[],
tokenIds: string[],
): BaseAmmPool[] => pools.filter((p) => tokenIds.includes(p.lp.asset.id));

const nativeNetworkPools$ = defer(() =>
from(nativeNetworkPools().getAll({ limit: 100, offset: 0 })),
).pipe(
Expand All @@ -73,103 +60,13 @@ export const pools$ = interval(UPDATE_TIME)
.concat(networkPools)
.filter((p) => p.id != BlacklistedPoolId),
),
map((pools) =>
pools.filter(
(p) =>
p.x.asset.id !== LOCKED_TOKEN_ID && p.y.asset.id !== LOCKED_TOKEN_ID,
),
),
map((pools) => pools.map((p) => new AmmPool(p))),
publishReplay(1),
refCount(),
);

const availableNativeNetworkPools$ = utxos$.pipe(
map(utxosToTokenIds),
switchMap((tokensIds) =>
from(
nativeNetworkPools().getByTokensUnion(tokensIds, {
limit: 500,
offset: 0,
}),
).pipe(map(([pools]) => pools)),
),
publishReplay(1),
refCount(),
);

const availableNetworkPools$ = utxos$.pipe(
map(utxosToTokenIds),
switchMap((tokensIds) =>
from(
networkPools().getByTokensUnion(tokensIds, {
limit: 500,
offset: 0,
}),
).pipe(map(([pools]) => pools)),
),
publishReplay(1),
refCount(),
);

export const availablePools$: Observable<AmmPool[]> = utxos$.pipe(
map(utxosToTokenIds),
switchMap((tokenIds) =>
combineLatest([
tokenLocks$,
zip([availableNativeNetworkPools$, availableNetworkPools$]),
]).pipe(
map(
([tokenLocks, [nativePools, pools]]) =>
[nativePools, pools, tokenLocks] as any,
),
map(
([nativePools, pools, tokenLocks]: [
BaseAmmPool[],
BaseAmmPool[],
TokenLock[],
]) => {
const allPools = nativePools.concat(pools);
const filteredPools = filterPoolsByTokenIds(allPools, tokenIds).map(
(bap) => {
return tokenLocks
.filter((l) => l.lockedAsset.asset.id === bap.lp.asset.id)
.reduce<BaseAmmPool>((bap, l) => {
//@ts-ignore
bap.lp.amount += l.lockedAsset.amount;
return bap;
}, bap);
},
);
const locksGroups = Object.values(
tokenLocks.reduce<{ [key: string]: TokenLock[] }>((acc, lock) => {
if (!acc[lock.lockedAsset.asset.id]) {
acc[lock.lockedAsset.asset.id] = [];
}

acc[lock.lockedAsset.asset.id].push(lock);

return acc;
}, {}),
);

const lockedPools = locksGroups
.filter(
(l) =>
!filteredPools.some(
(fp) => fp.lp.asset.id === l[0].lockedAsset.asset.id,
),
)
.map((l) => {
const pool = allPools.find(
(p) => p.lp.asset.id === l[0].lockedAsset.asset.id,
)!;
//@ts-ignore
l.forEach((lc) => (pool.lp.amount += lc.lockedAsset.amount));

return pool;
});

return filteredPools.concat(lockedPools as any);
},
),
),
),
map((pools) => pools.map((p) => new AmmPool(p))),
publishReplay(1),
refCount(),
);

0 comments on commit 04a30cb

Please sign in to comment.