Skip to content

Commit

Permalink
Merge pull request #294 from bnb-chain/wenty/aggregator
Browse files Browse the repository at this point in the history
refactor: Move chain's utils to UtilService
  • Loading branch information
wenty22 authored Jan 22, 2025
2 parents d4bf73d + 5e4932e commit 1094af1
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import { BridgeController } from '@/module/bridge/bridge.controller';
import { BridgeService } from '@/module/bridge/bridge.service';
import { BridgeProcessor } from '@/module/bridge/bridge.processor';
import { BridgeSchedule } from '@/module/bridge/bridge.schedule';
import { UtilService } from '@/shared/util/util.service';

@Module({
imports: [BullModule.registerQueue({ name: Queues.SyncBridge })],
controllers: [BridgeController],
providers: [BridgeService, BridgeProcessor, BridgeSchedule],
providers: [BridgeService, BridgeProcessor, BridgeSchedule, UtilService],
})
export class BridgeModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ import {
isNativeToken,
EVM_NATIVE_TOKEN_ADDRESS,
} from '@bnb-chain/canonical-bridge-sdk';
import { chains } from '@/common/constants/chains';
import { UtilService } from '@/shared/util/util.service';

@Processor(Queues.SyncBridge)
export class BridgeProcessor extends WorkerHost {
private logger = new Logger(BridgeProcessor.name);

constructor(
private web3Service: Web3Service,
private utilService: UtilService,
@Inject(CACHE_MANAGER) private cache: Cache,
) {
super();
Expand Down Expand Up @@ -178,7 +179,7 @@ export class BridgeProcessor extends WorkerHost {
return true;
}

const chainInfo = chains.find((e) => e.id === chainId);
const chainInfo = this.utilService.getChainConfigByChainId(chainId);
if (!chainInfo) {
return false;
}
Expand Down
12 changes: 7 additions & 5 deletions apps/canonical-bridge-server/src/module/token/token.processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ITokenJob } from '@/module/token/token.interface';
import { Web3Service } from '@/shared/web3/web3.service';
import { TokenService } from '@/module/token/token.service';
import { CACHE_MANAGER, Cache } from '@nestjs/cache-manager';
import { UtilService } from '@/shared/util/util.service';

@Processor(Queues.SyncToken)
export class TokenProcessor extends WorkerHost {
Expand All @@ -21,6 +22,7 @@ export class TokenProcessor extends WorkerHost {
constructor(
private web3Service: Web3Service,
private tokenService: TokenService,
private utilService: UtilService,
@Inject(CACHE_MANAGER) private cache: Cache,
@InjectQueue(Queues.SyncToken) private syncToken: Queue<ITokenJob>,
) {
Expand Down Expand Up @@ -105,7 +107,7 @@ export class TokenProcessor extends WorkerHost {
const now = Date.now();
const config = tokens
.map((t) => {
const chainConfig = this.tokenService.getChainConfigByLlamaPlatform(t.platform);
const chainConfig = this.utilService.getChainConfigByLlamaPlatform(t.platform);
return {
...t,
chainId: chainConfig?.id ?? t.chainId,
Expand All @@ -117,7 +119,7 @@ export class TokenProcessor extends WorkerHost {
.reduce((r, c) => {
const { address, chainId } = c;

const formattedAddr = this.tokenService.getFormattedAddress(chainId, address);
const formattedAddr = this.utilService.getFormattedAddress(chainId, address);
const key = formattedAddr ? `${chainId}:${formattedAddr}` : chainId;

r[key] = { price: c.price, decimals: c.decimals };
Expand All @@ -136,8 +138,8 @@ export class TokenProcessor extends WorkerHost {
const config = tokens
.map((t) => {
const chainConfig =
this.tokenService.getChainConfigByCmcPlatform(t.platformSlug) ||
this.tokenService.getChainConfigByCmcPlatform(t.slug);
this.utilService.getChainConfigByCmcPlatform(t.platformSlug) ||
this.utilService.getChainConfigByCmcPlatform(t.slug);

return {
...t,
Expand All @@ -148,7 +150,7 @@ export class TokenProcessor extends WorkerHost {
.reduce((r, c) => {
const { address, chainId } = c;

const formattedAddr = this.tokenService.getFormattedAddress(chainId, address);
const formattedAddr = this.utilService.getFormattedAddress(chainId, address);
const key = formattedAddr ? `${chainId}:${formattedAddr}` : chainId;

r[key] = { price: c.price, id: c.id };
Expand Down
45 changes: 7 additions & 38 deletions apps/canonical-bridge-server/src/module/token/token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { CACHE_KEY, CMC_PRICE_REQUEST_LIMIT, LLAMA_PRICE_REQUEST_LIMIT } from '@
import { get, isEmpty } from 'lodash';
import { Prisma } from '@prisma/client';
import { Cache, CACHE_MANAGER } from '@nestjs/cache-manager';
import { chains } from '@/common/constants/chains';
import { Web3Service } from '@/shared/web3/web3.service';
import { UtilService } from '@/shared/util/util.service';

@Injectable()
export class TokenService {
Expand All @@ -24,6 +24,7 @@ export class TokenService {
constructor(
private web3Service: Web3Service,
private databaseService: DatabaseService,
private utilService: UtilService,
@Inject(CACHE_MANAGER) private cache: Cache,
) {}

Expand Down Expand Up @@ -93,7 +94,7 @@ export class TokenService {
}

async getCmcTokenIdsForPriceJob() {
const platforms = this.getChainCmcPlatforms();
const platforms = this.utilService.getChainCmcPlatforms();

const tokens = await this.databaseService.getTokens(
this.cmcTokenStart,
Expand Down Expand Up @@ -131,8 +132,8 @@ export class TokenService {
}

async getLlamaTokenIdsForPriceJob() {
const chainIds = this.getChainIds();
const platforms = this.getChainLlamaPlatforms();
const chainIds = this.utilService.getChainIds();
const platforms = this.utilService.getChainLlamaPlatforms();

const tokens = await this.databaseService.getCoingeckoTokens(
this.llamaTokenStart,
Expand All @@ -159,10 +160,10 @@ export class TokenService {
}

async getTokenPrice(chainId: number, tokenAddress?: string) {
const cmcPlatform = this.getChainConfigByChainId(chainId)?.extra?.cmcPlatform;
const cmcPlatform = this.utilService.getChainConfigByChainId(chainId)?.extra?.cmcPlatform;
const cmcToken = await this.databaseService.getToken(cmcPlatform, tokenAddress);

const llamaPlatform = this.getChainConfigByChainId(chainId)?.extra?.llamaPlatform;
const llamaPlatform = this.utilService.getChainConfigByChainId(chainId)?.extra?.llamaPlatform;
const llamaToken = await this.databaseService.getCoingeckoToken(
chainId,
llamaPlatform,
Expand Down Expand Up @@ -191,36 +192,4 @@ export class TokenService {
return Object.values<ICoinPrice>(llamaRes.value.coins ?? {})?.[0]?.price;
}
}

public getFormattedAddress(chainId?: number, address?: string) {
const chainInfo = chains.find((e) => e.id === chainId);
if (chainInfo?.chainType !== 'evm') {
return address;
}
return address?.toLowerCase();
}

public getChainConfigByCmcPlatform(platform: string) {
return chains.find((e) => e.extra?.cmcPlatform === platform);
}

public getChainConfigByLlamaPlatform(platform: string) {
return chains.find((e) => e.extra?.llamaPlatform === platform);
}

public getChainConfigByChainId(chainId: number) {
return chains.find((e) => e.id === chainId);
}

public getChainCmcPlatforms() {
return chains.filter((e) => e.extra?.cmcPlatform).map((e) => e.extra?.cmcPlatform);
}

public getChainLlamaPlatforms() {
return chains.filter((e) => e.extra?.llamaPlatform).map((e) => e.extra?.llamaPlatform);
}

public getChainIds() {
return chains.map((e) => e.id);
}
}
33 changes: 33 additions & 0 deletions apps/canonical-bridge-server/src/shared/util/util.service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { chains } from '@/common/constants/chains';
import { Injectable, Logger } from '@nestjs/common';
import { AxiosRequestConfig } from '@nestjs/terminus/dist/health-indicator/http/axios.interfaces';

Expand All @@ -18,4 +19,36 @@ export class UtilService {
// todo
return Array.isArray(res) ? res.map((r) => r.result || r) : res.result || res.data || res;
}

public getFormattedAddress(chainId?: number, address?: string) {
const chainInfo = chains.find((e) => e.id === chainId);
if (chainInfo?.chainType !== 'evm') {
return address;
}
return address?.toLowerCase();
}

public getChainConfigByCmcPlatform(platform: string) {
return chains.find((e) => e.extra?.cmcPlatform === platform);
}

public getChainConfigByLlamaPlatform(platform: string) {
return chains.find((e) => e.extra?.llamaPlatform === platform);
}

public getChainConfigByChainId(chainId: number) {
return chains.find((e) => e.id === chainId);
}

public getChainCmcPlatforms() {
return chains.filter((e) => e.extra?.cmcPlatform).map((e) => e.extra?.cmcPlatform);
}

public getChainLlamaPlatforms() {
return chains.filter((e) => e.extra?.llamaPlatform).map((e) => e.extra?.llamaPlatform);
}

public getChainIds() {
return chains.map((e) => e.id);
}
}

0 comments on commit 1094af1

Please sign in to comment.