diff --git a/apollo/src/aggregation/aggregation.history.graphql b/apollo/src/aggregation/aggregation.history.graphql index c67321e..6a3489c 100644 --- a/apollo/src/aggregation/aggregation.history.graphql +++ b/apollo/src/aggregation/aggregation.history.graphql @@ -82,6 +82,11 @@ type SortedLnBridgeRelayInfos { records: [LnBridgeRelayInfo] } +type TokenInfo { + tokenKey: String! + chains: [SupportChains] +} + type SupportChains { fromChain: String! toChains: [String] @@ -104,6 +109,7 @@ type Query { queryLnBridgeRelayInfos(fromChain: String, toChain: String, version: String, bridge: String, relayer: String, row: Int, page: Int): LnBridgeRelayInfos sortedLnBridgeRelayInfos(fromChain: String, toChain: String, version: String, bridge: String, token: String, row: Int, amount: String, decimals: Int): SortedLnBridgeRelayInfos queryLnBridgeSupportChains(tokenKey: String): [SupportChains] + queryLnBridgeSupportedChains(tokenKey: String): [TokenInfo] queryMaxTransfer(fromChain: String, toChain: String, bridge: String, token: String, balance: String): BigInt } diff --git a/apollo/src/aggregation/aggregation.resolver.ts b/apollo/src/aggregation/aggregation.resolver.ts index 79b8a65..5d5ae33 100644 --- a/apollo/src/aggregation/aggregation.resolver.ts +++ b/apollo/src/aggregation/aggregation.resolver.ts @@ -2,6 +2,7 @@ import { Args, Query, Mutation, Resolver } from '@nestjs/graphql'; import { isEmpty, isNull, isUndefined } from 'lodash'; import { AggregationService } from './aggregation.service'; import { Prisma } from '@prisma/client'; +import { TokenInfo } from '../graphql'; import * as ethUtil from 'ethereumjs-util'; import Web3 from 'web3'; @@ -399,11 +400,19 @@ export class AggregationResolver { @Query() async queryLnBridgeSupportChains(@Args('tokenKey') tokenKey: string) { + const tokens = await this.queryLnBridgeSupportedChains(tokenKey); + return tokens?.find((e) => e.tokenKey === tokenKey)?.chains; + } + + @Query() + async queryLnBridgeSupportedChains(@Args('tokenKey') tokenKey: string) { const baseFilters = { - tokenKey, paused: false, OR: [{ transferLimit: { not: '0' } }, { margin: { not: '0' } }], }; + if (tokenKey.length > 0) { + baseFilters['tokenKey'] = tokenKey; + } const where = { ...baseFilters, @@ -412,27 +421,30 @@ export class AggregationResolver { const records = await this.aggregationService.queryLnBridgeRelayInfos({ where, }); - const supportChains = new Map(); + const now = Math.floor(Date.now() / 1000); - for (const record of records.records) { + return records.records.reduce((result, record) => { if (record.heartbeatTimestamp + this.heartbeatTimeout < now) { - continue; + return result; } - const toChains = supportChains.get(record.fromChain); + let token = result.find((e) => e.tokenKey === record.tokenKey); + if (!token) { + token = { tokenKey: record.tokenKey, chains: [] }; + result.push(token); + } - if (!toChains) { - supportChains.set(record.fromChain, [record.toChain]); - } else { - if (!toChains.includes(record.toChain)) { - toChains.push(record.toChain); - } + let chain = token.chains.find((chain) => chain.fromChain === record.fromChain); + if (!chain) { + chain = { fromChain: record.fromChain, toChains: [] }; + token.chains.push(chain); } - } - return Array.from(supportChains, ([fromChain, toChains]) => ({ - fromChain, - toChains, - })); + + if (!chain.toChains.includes(record.toChain)) { + chain.toChains.push(record.toChain); + } + return result; + }, [] as TokenInfo[]); } @Query() diff --git a/apollo/src/graphql.ts b/apollo/src/graphql.ts index 0b425d0..d3b9d28 100644 --- a/apollo/src/graphql.ts +++ b/apollo/src/graphql.ts @@ -88,6 +88,11 @@ export class SortedLnBridgeRelayInfos { records?: Nullable[]>; } +export class TokenInfo { + tokenKey: string; + chains?: Nullable[]>; +} + export class SupportChains { fromChain: string; toChains?: Nullable[]>; @@ -187,6 +192,10 @@ export abstract class IQuery { tokenKey?: Nullable ): Nullable[]> | Promise[]>>; + abstract queryLnBridgeSupportedChains( + tokenKey?: Nullable + ): Nullable[]> | Promise[]>>; + abstract queryMaxTransfer( fromChain?: Nullable, toChain?: Nullable, diff --git a/apollo/src/lnv3/lnv3.service.ts b/apollo/src/lnv3/lnv3.service.ts index aa2719d..6eaeb77 100644 --- a/apollo/src/lnv3/lnv3.service.ts +++ b/apollo/src/lnv3/lnv3.service.ts @@ -172,11 +172,13 @@ export class Lnv3Service implements OnModuleInit { for (const level0Indexer of toChain.level0Indexers) { const service = this.sourceServices.get(level0Indexer.indexerType); try { - return await service.queryMultiRelayStatus( - level0Indexer.url, - toChain.chainConfig.id, - transferIds - ) ?? []; + return ( + (await service.queryMultiRelayStatus( + level0Indexer.url, + toChain.chainConfig.id, + transferIds + )) ?? [] + ); } catch (err) { this.logger.warn( `try to get multi relay status failed, id ${toChain.chainConfig.id}, type ${level0Indexer.indexerType}, transferIds ${transferIds} err ${err}`