Skip to content

Commit

Permalink
feat: Add cache time to server
Browse files Browse the repository at this point in the history
  • Loading branch information
wenty22 committed Dec 27, 2024
1 parent fb84eb5 commit e19a76c
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 96 deletions.
5 changes: 5 additions & 0 deletions .release/.changeset/poor-kings-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bnb-chain/canonical-bridge-widget": patch
---

Fix tron does not display due to its id
1 change: 1 addition & 0 deletions .release/.changeset/pre.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"dull-moose-deliver",
"hungry-doors-lick",
"itchy-dots-enjoy",
"poor-kings-compete",
"serious-cars-worry"
]
}
3 changes: 2 additions & 1 deletion apps/canonical-bridge-server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { AllExceptionFilter } from './common/filters/all-exception.filter';
import { HttpExceptionFilter } from './common/filters/http-exception.filter';
import { TransformInterceptor } from './common/interceptors/transform.interceptor';
import { TimeoutInterceptor } from './common/interceptors/timeout.interceptor';
import { REDIS_HOST, REDIS_PORT } from './common/constants';
import { REDIS_HOST, REDIS_PORT, TIME } from './common/constants';
import { TokenModule } from './module/token/token.module';
import { BullModule } from '@nestjs/bullmq';
import { Web3Module } from '@/shared/web3/web3.module';
Expand All @@ -32,6 +32,7 @@ import { BridgeModule } from '@/module/bridge/bridge.module';
HealthModule,
ScheduleModule.forRoot(),
CacheModule.register<RedisOptions>({
ttl: TIME.DAY,
isGlobal: true,
store: () => redisStore({ host: REDIS_HOST, port: REDIS_PORT }),
}),
Expand Down
9 changes: 9 additions & 0 deletions apps/canonical-bridge-server/src/common/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ export const JOB_KEY = {
CORN_PRICE_PREFIX: 'corn:price:',
};

export const TIME = {
SECOND: 1000,
MINUTE: 60 * 1000,
HOUR: 60 * 60 * 1000,
DAY: 24 * 60 * 60 * 1000,
WEEK: 7 * 24 * 60 * 60 * 1000,
MONTH: 30 * 24 * 60 * 60 * 1000,
};

export const STARGATE_CHAIN_INFO = [
{
chainId: 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CACHE_KEY, Queues, Tasks } from '@/common/constants';
import { CACHE_KEY, Queues, Tasks, TIME } from '@/common/constants';
import { Processor, WorkerHost } from '@nestjs/bullmq';
import { Inject, Logger } from '@nestjs/common';
import { Job } from 'bullmq';
Expand Down Expand Up @@ -45,24 +45,24 @@ export class BridgeProcessor extends WorkerHost {

const data = { chains: config.chains, tokens: tokenMap };

await this.cache.set(`${CACHE_KEY.DEBRIDGE_CONFIG}`, data);
await this.cache.set(`${CACHE_KEY.DEBRIDGE_CONFIG}`, data, TIME.DAY);
}

async fetchCBridge() {
const config = await this.web3Service.getTransferConfigsForAll();
if (!config) return;
await this.cache.set(`${CACHE_KEY.CBRIDGE_CONFIG}`, config);
await this.cache.set(`${CACHE_KEY.CBRIDGE_CONFIG}`, config, TIME.MINUTE * 5);
}

async fetchStargate() {
const config = await this.web3Service.getStargateConfigs();
if (!config) return;
await this.cache.set(`${CACHE_KEY.STARGATE_CONFIG}`, config);
await this.cache.set(`${CACHE_KEY.STARGATE_CONFIG}`, config, TIME.DAY);
}

async fetchMeson() {
const config = await this.web3Service.getMesonConfigs();
if (!config) return;
await this.cache.set(`${CACHE_KEY.MESON_CONFIG}`, config);
await this.cache.set(`${CACHE_KEY.MESON_CONFIG}`, config, TIME.DAY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class BridgeSchedule implements OnModuleInit {

constructor(@InjectQueue(Queues.SyncBridge) private syncBridge: Queue) {}

@Cron(CronExpression.EVERY_3_HOURS)
@Cron(CronExpression.EVERY_5_MINUTES)
async syncBridgeInfo() {
this.logger.log('syncBridgeInfo');
await this.syncBridge.add(Tasks.fetchCbridge, null, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { InjectQueue, Processor, WorkerHost } from '@nestjs/bullmq';
import { Inject, Logger } from '@nestjs/common';
import { CACHE_KEY, JOB_KEY, Queues, Tasks, TOKEN_REQUEST_LIMIT } from '@/common/constants';
import { CACHE_KEY, JOB_KEY, Queues, Tasks, TIME, TOKEN_REQUEST_LIMIT } from '@/common/constants';
import { Job, Queue } from 'bullmq';
import { ITokenJob } from '@/module/token/token.interface';
import { Web3Service } from '@/shared/web3/web3.service';
Expand Down Expand Up @@ -57,7 +57,7 @@ export class TokenProcessor extends WorkerHost {
return r;
}, {});

await this.cache.set(`${CACHE_KEY.LLAMA_CONFIG}`, config);
await this.cache.set(`${CACHE_KEY.LLAMA_CONFIG}`, config, TIME.MONTH);
return config;
}

Expand All @@ -79,7 +79,7 @@ export class TokenProcessor extends WorkerHost {
return r;
}, {});

await this.cache.set(`${CACHE_KEY.CMC_CONFIG}`, config);
await this.cache.set(`${CACHE_KEY.CMC_CONFIG}`, config, TIME.MONTH);
return config;
}

Expand All @@ -101,7 +101,7 @@ export class TokenProcessor extends WorkerHost {
{} as Record<string, string>,
);

await this.cache.set(`${CACHE_KEY.PLATFORM_MAPPING}`, mapping);
await this.cache.set(`${CACHE_KEY.PLATFORM_MAPPING}`, mapping, TIME.MONTH);
this.tokenService.syncCoingeckoTokens(coins, platforms);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class TokenSchedule implements OnModuleInit {
private tokensService: TokenService,
) {}

@Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT)
@Cron(CronExpression.EVERY_HOUR)
async syncCmcTokens() {
this.logger.log('syncCmcTokens');
await this.syncToken.add(
Expand Down
6 changes: 4 additions & 2 deletions apps/canonical-bridge-server/src/shared/web3/web3.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import {
} from '@/common/constants';
import { values } from 'lodash';

let counter = 0;

@Injectable()
export class Web3Service {
private logger = new Logger(Web3Service.name);
Expand Down Expand Up @@ -61,9 +63,9 @@ export class Web3Service {

async getTransferConfigsForAll() {
const { data } = await this.httpService.axiosRef.get<ITransferConfigsForAll>(
`${CBRIDGE_ENDPOINT}/v2/getTransferConfigsForAll`,
`${CBRIDGE_ENDPOINT}/v2/getTransferConfigsForAll${counter === 0 ? '' : '222222'}`,
);

counter += 1;
return data;
}

Expand Down
4 changes: 2 additions & 2 deletions apps/canonical-bridge-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
"react-dom": "~18.3.1",
"supports-color": "~9.4.0",
"tronweb": "~6.0.0",
"vconsole": "~3.15.1",
"viem": "~2.21.14",
"wagmi": "^2",
"vconsole": "~3.15.1"
"wagmi": "^2"
},
"devDependencies": {
"@babel/core": "^7.21.4",
Expand Down
168 changes: 89 additions & 79 deletions apps/canonical-bridge-ui/token-config/mainnet/useTransferConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function useTransferConfig() {

useEffect(() => {
const initConfig = async () => {
const [cBridgeRes, deBridgeRes, StargateRes, MesonRes] = await Promise.all([
const [cBridgeRes, deBridgeRes, stargateRes, mesonRes] = await Promise.all([
axios.get<{ data: ICBridgeTransferConfig }>(`${env.SERVER_ENDPOINT}/api/bridge/cbridge`),
axios.get<{ data: IDeBridgeTransferConfig }>(`${env.SERVER_ENDPOINT}/api/bridge/debridge`),
axios.get<{ data: IStargateApiTokenConfig[] }>(
Expand All @@ -26,9 +26,9 @@ export function useTransferConfig() {
]);

const cBridgeConfig = cBridgeRes.data.data;
const deBridgeConfig = handleDeBridgeConfig(deBridgeRes.data.data);
const mesonConfig = MesonRes.data.data;
const stargateConfig = StargateRes.data.data;
const deBridgeConfig = deBridgeRes.data.data;
const mesonConfig = mesonRes.data.data;
const stargateConfig = stargateRes.data.data;

const transferConfig: ITransferConfig = {
defaultSelectedInfo: {
Expand Down Expand Up @@ -115,83 +115,93 @@ export function useTransferConfig() {
'0x49D5c2BdFfac6CE2BFdB6640F4F80f226bc10bAB': 'WETH.e',
},
},
cBridge: {
config: cBridgeConfig,
exclude: {
chains: [],
tokens: {
56: ['0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c'],
42161: [
'0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9',
'0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8',
], // ['USDT', 'USDC.e']
},
},
bridgedTokenGroups: [],
},
deBridge: {
config: deBridgeConfig,
exclude: {
chains: [],
tokens: {
1: ['cUSDCv3', '0x5e21d1ee5cf0077b314c381720273ae82378d613'],
56: [
'0x67d66e8ec1fd25d98b3ccd3b19b7dc4b4b7fc493',
'0x0000000000000000000000000000000000000000',
'0x9c7beba8f6ef6643abd725e45a4e8387ef260649',
cBridge: cBridgeConfig
? {
config: cBridgeConfig,
exclude: {
chains: [],
tokens: {
56: ['0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c'],
42161: [
'0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9',
'0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8',
], // ['USDT', 'USDC.e']
},
},
bridgedTokenGroups: [],
}
: undefined,
deBridge: deBridgeConfig
? {
config: handleDeBridgeConfig(deBridgeRes.data.data),
exclude: {
chains: [],
tokens: {
1: ['cUSDCv3', '0x5e21d1ee5cf0077b314c381720273ae82378d613'],
56: [
'0x67d66e8ec1fd25d98b3ccd3b19b7dc4b4b7fc493',
'0x0000000000000000000000000000000000000000',
'0x9c7beba8f6ef6643abd725e45a4e8387ef260649',
],
137: ['cUSDCv3'],
42161: ['cUSDCv3'],
43114: ['BNB'],
7565164: [
'So11111111111111111111111111111111111111112',
'FmqVMWXBESyu4g6FT1uz1GABKdJ4j6wbuuLFwPJtqpmu',
],
},
},
bridgedTokenGroups: [
['USDT', 'USDT.e'],
['USDC', 'USDC.e'],
['WETH', 'WETH.e'],
['DAI', 'DAI.e'],
['WBTC', 'WBTC.e'],
['LINK', 'LINK.e'],
['AAVE', 'AAVE.e'],
['WOO', 'WOO.e'],
['BUSD', 'BUSD.e'],
['ALPHA', 'ALPHA.e'],
['SUSHI', 'SUSHI.e'],
['SWAP', 'SWAP.e'],
],
137: ['cUSDCv3'],
42161: ['cUSDCv3'],
43114: ['BNB'],
7565164: [
'So11111111111111111111111111111111111111112',
'FmqVMWXBESyu4g6FT1uz1GABKdJ4j6wbuuLFwPJtqpmu',
}
: undefined,
stargate: stargateConfig
? {
config: stargateConfig,
exclude: {
chains: [],
tokens: {},
},
bridgedTokenGroups: [
['ETH', 'mETH'],
['USDT', 'm.USDT'],
['USDC', 'USDC.e'],
],
},
},
bridgedTokenGroups: [
['USDT', 'USDT.e'],
['USDC', 'USDC.e'],
['WETH', 'WETH.e'],
['DAI', 'DAI.e'],
['WBTC', 'WBTC.e'],
['LINK', 'LINK.e'],
['AAVE', 'AAVE.e'],
['WOO', 'WOO.e'],
['BUSD', 'BUSD.e'],
['ALPHA', 'ALPHA.e'],
['SUSHI', 'SUSHI.e'],
['SWAP', 'SWAP.e'],
],
},
stargate: {
config: stargateConfig,
exclude: {
chains: [],
tokens: {},
},
bridgedTokenGroups: [
['ETH', 'mETH'],
['USDT', 'm.USDT'],
['USDC', 'USDC.e'],
],
},
layerZero: {
config: layerZeroConfig,
exclude: {
chains: [],
tokens: {},
},
bridgedTokenGroups: [],
},
meson: {
config: mesonConfig,
exclude: {
chains: [],
tokens: { 42161: ['SOL'] },
},
bridgedTokenGroups: [],
},
}
: undefined,
layerZero: layerZeroConfig
? {
config: layerZeroConfig,
exclude: {
chains: [],
tokens: {},
},
bridgedTokenGroups: [],
}
: undefined,
meson: mesonConfig
? {
config: mesonConfig,
exclude: {
chains: [],
tokens: { 42161: ['SOL'] },
},
bridgedTokenGroups: [],
}
: undefined,
};

setTransferConfig(transferConfig);
Expand Down
6 changes: 6 additions & 0 deletions packages/canonical-bridge-widget/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @bnb-chain/canonical-bridge-widget

## 0.5.16-alpha.5

### Patch Changes

- Fix tron does not display due to its id

## 0.5.16-alpha.4

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/canonical-bridge-widget/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bnb-chain/canonical-bridge-widget",
"version": "0.5.16-alpha.4",
"version": "0.5.16-alpha.5",
"description": "canonical bridge widget",
"author": "bnb-chain",
"private": false,
Expand Down

0 comments on commit e19a76c

Please sign in to comment.