Skip to content

Commit

Permalink
support query tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoch05 committed Aug 1, 2024
1 parent a0416cd commit 7a1818a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 21 deletions.
6 changes: 6 additions & 0 deletions apollo/src/aggregation/aggregation.history.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ type SortedLnBridgeRelayInfos {
records: [LnBridgeRelayInfo]
}

type TokenInfo {
tokenKey: String!
chains: [SupportChains]
}

type SupportChains {
fromChain: String!
toChains: [String]
Expand All @@ -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
}

Expand Down
44 changes: 28 additions & 16 deletions apollo/src/aggregation/aggregation.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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,
Expand All @@ -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()
Expand Down
9 changes: 9 additions & 0 deletions apollo/src/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ export class SortedLnBridgeRelayInfos {
records?: Nullable<Nullable<LnBridgeRelayInfo>[]>;
}

export class TokenInfo {
tokenKey: string;
chains?: Nullable<Nullable<SupportChains>[]>;
}

export class SupportChains {
fromChain: string;
toChains?: Nullable<Nullable<string>[]>;
Expand Down Expand Up @@ -187,6 +192,10 @@ export abstract class IQuery {
tokenKey?: Nullable<string>
): Nullable<Nullable<SupportChains>[]> | Promise<Nullable<Nullable<SupportChains>[]>>;

abstract queryLnBridgeSupportedChains(
tokenKey?: Nullable<string>
): Nullable<Nullable<TokenInfo>[]> | Promise<Nullable<Nullable<TokenInfo>[]>>;

abstract queryMaxTransfer(
fromChain?: Nullable<string>,
toChain?: Nullable<string>,
Expand Down
12 changes: 7 additions & 5 deletions apollo/src/lnv3/lnv3.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down

0 comments on commit 7a1818a

Please sign in to comment.