diff --git a/src/network/ergo/ammPools/ammPools.ts b/src/network/ergo/ammPools/ammPools.ts index f1c99e988..51d3683a5 100644 --- a/src/network/ergo/ammPools/ammPools.ts +++ b/src/network/ergo/ammPools/ammPools.ts @@ -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 = @@ -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(), diff --git a/src/services/new/assets.ts b/src/services/new/assets.ts index a14d57e25..b1a525e79 100644 --- a/src/services/new/assets.ts +++ b/src/services/new/assets.ts @@ -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'; @@ -18,9 +18,6 @@ export const lpAssets$ = pools$.pipe( refCount(), ); -export const getAssetById = (id: string): Observable => - assets$.pipe(map((assets) => find(assets, ['id', id])!)); - export const getAvailableAssetFor = ( assetId: string, ): Observable => diff --git a/src/services/new/pools.ts b/src/services/new/pools.ts index 48d47b5a8..dcb4291d3 100644 --- a/src/services/new/pools.ts +++ b/src/services/new/pools.ts @@ -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); @@ -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( @@ -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 = 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((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(), -);