Skip to content

Commit

Permalink
Merge pull request #211 from bnb-chain/feat/tokenApi1216
Browse files Browse the repository at this point in the history
feat: Meson & Stargate token API
  • Loading branch information
Halibao-Lala authored Dec 30, 2024
2 parents fca0417 + be8248c commit 119bfa7
Show file tree
Hide file tree
Showing 59 changed files with 1,109 additions and 2,406 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* @aiden-cao @wenty22 @Halibao-Lala @doge95
* @aiden-cao @wenty22 @Halibao-Lala @doge95 @robot-ux
6 changes: 6 additions & 0 deletions .release/.changeset/bright-impalas-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@bnb-chain/canonical-bridge-widget": patch
"@bnb-chain/canonical-bridge-sdk": patch
---

Use stargate & meson api to fetch chain & token config
6 changes: 6 additions & 0 deletions .release/.changeset/dull-moose-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@bnb-chain/canonical-bridge-widget": patch
"@bnb-chain/canonical-bridge-sdk": patch
---

Remove duplicated tokens for stargate
5 changes: 5 additions & 0 deletions .release/.changeset/giant-rings-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bnb-chain/canonical-bridge-widget": patch
---

Fix stargate fee display and handle api error
6 changes: 6 additions & 0 deletions .release/.changeset/hungry-doors-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@bnb-chain/canonical-bridge-widget": patch
"@bnb-chain/canonical-bridge-sdk": patch
---

Update token element's info
6 changes: 6 additions & 0 deletions .release/.changeset/itchy-dots-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@bnb-chain/canonical-bridge-widget": patch
"@bnb-chain/canonical-bridge-sdk": patch
---

Fix meson token display symbol issue
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
16 changes: 16 additions & 0 deletions .release/.changeset/pre.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"mode": "pre",
"tag": "alpha",
"initialVersions": {
"@bnb-chain/canonical-bridge-sdk": "0.4.5",
"@bnb-chain/canonical-bridge-widget": "0.5.15"
},
"changesets": [
"bright-impalas-sniff",
"dull-moose-deliver",
"hungry-doors-lick",
"itchy-dots-enjoy",
"poor-kings-compete",
"serious-cars-worry"
]
}
5 changes: 5 additions & 0 deletions .release/.changeset/serious-cars-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bnb-chain/canonical-bridge-widget": patch
---

Add more token info to token element
1 change: 0 additions & 1 deletion .release/scripts/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ process.chdir(rootDir);

console.log('Install changeset dependencies...');
exec('pnpm install', (err, stdout) => {
console.log('shshshshsh');
if (stdout) {
console.log(stdout);
}
Expand Down
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"tronwallet",
"tronweb",
"unauthenticate",
"vconsole",
"viem",
"Wagmi",
"walletkit"
Expand Down
2 changes: 1 addition & 1 deletion apps/canonical-bridge-server/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ STARGATE_ENDPOINT='https://mainnet.stargate-api.com/v1/metadata?version=v2'
MESON_ENDPOINT=https://relayer.meson.fi/api/v1

REDIS_URL=http://127.0.0.1:6379
DATABASE_URL=mysql://test:xxx@localhost:3306/bridge
DATABASE_URL=
9 changes: 5 additions & 4 deletions apps/canonical-bridge-server/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
FROM busybox:1.36.1 as builder
FROM busybox:1.36.1 AS builder
FROM node:20-buster

RUN useradd -u 9000 appuser
USER appuser

WORKDIR /opt/deploy

COPY . .
Expand All @@ -14,6 +11,10 @@ COPY --from=builder /bin/sleep /usr/local/bin/sleep

WORKDIR /opt/deploy/apps/canonical-bridge-server

RUN useradd -u 9000 appuser
RUN chown -R appuser /opt/deploy/apps/canonical-bridge-server
USER appuser

EXPOSE 3000

ENV NODE_ENV production
Expand Down
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.DAY);
}

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
65 changes: 42 additions & 23 deletions apps/canonical-bridge-server/src/shared/web3/web3.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,35 +60,49 @@ export class Web3Service {
}

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

return data;
try {
const { data } = await this.httpService.axiosRef.get<ITransferConfigsForAll>(
`${CBRIDGE_ENDPOINT}/v2/getTransferConfigsForAll`,
);
return data;
} catch (e) {
console.error(`Failed to retrieve cBridge data at ${new Date().getTime()}`, e.message);
}
}

async getDebridgeChains() {
const { data } = await this.httpService.axiosRef.get<{ chains: IDebridgeChain[] }>(
`${DEBRIDGE_ENDPOINT}/supported-chains-info`,
);
try {
const { data } = await this.httpService.axiosRef.get<{ chains: IDebridgeChain[] }>(
`${DEBRIDGE_ENDPOINT}/supported-chains-info`,
);

return data;
return data;
} catch (e) {
console.error(`Failed to retrieve DeBridge chain data at ${new Date().getTime()}`, e.message);
}
}

async getDebridgeChainTokens(chainId: number) {
const { data } = await this.httpService.axiosRef.get<{
tokens: Record<string, IDebridgeToken>;
}>(`${DEBRIDGE_ENDPOINT}/token-list?chainId=${chainId}`);
try {
const { data } = await this.httpService.axiosRef.get<{
tokens: Record<string, IDebridgeToken>;
}>(`${DEBRIDGE_ENDPOINT}/token-list?chainId=${chainId}`);

return data;
return data;
} catch (e) {
console.error(
`Failed to retrieve DeBridge token data from ${chainId} at ${new Date().getTime()}`,
e.message,
);
}
}

async getStargateConfigs() {
const { data } = await this.httpService.axiosRef.get<IStargateTokenList>(
`${STARGATE_ENDPOINT}`,
);
const processedTokenList = [];
try {
const { data } = await this.httpService.axiosRef.get<IStargateTokenList>(
`${STARGATE_ENDPOINT}`,
);
const processedTokenList = [];
const v2List = data.v2;
v2List.forEach((token) => {
const chainInfo = STARGATE_CHAIN_INFO.filter(
Expand All @@ -98,17 +112,22 @@ export class Web3Service {
processedTokenList.push({ ...token, endpointID: chainInfo[0].endpointID });
}
});
return processedTokenList;
} catch (e) {
console.log(`Failed to retrieve Stargate API data at ${new Date().getTime()}`, e);
console.error(`Failed to retrieve Stargate API data at ${new Date().getTime()}`, e.message);
}
return processedTokenList;
}

async getMesonConfigs() {
const { data } = await this.httpService.axiosRef.get<{ result: IMesonChain[] }>(
`${MESON_ENDPOINT}/limits`,
);
return data;
try {
const { data } = await this.httpService.axiosRef.get<{ result: IMesonChain[] }>(
`${MESON_ENDPOINT}/limits`,
);
return data;
} catch (e) {
console.log(`Failed to retrieve Meson API data at ${new Date().getTime()}`, e);
return [];
}
}

async getAssetPlatforms() {
Expand Down
15 changes: 15 additions & 0 deletions apps/canonical-bridge-ui/core/components/VConsole/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useMemo } from 'react';

export function VConsole() {
useMemo(async () => {
if (typeof window !== 'undefined') {
try {
new (await import('vconsole')).default();
} catch (err) {
// eslint-disable-next-line no-console
console.log(`Init vconsole error!`, err);
}
}
}, []);
return null;
}
6 changes: 3 additions & 3 deletions apps/canonical-bridge-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@
"@solana/web3.js": "~1.95.4",
"@tanstack/react-query": "~5.50.1",
"@tronweb3/tronwallet-adapter-react-hooks": "~1.1.9",
"axios": "~1.6.8",
"next": "~14.1.1",
"axios": "~1.7.4",
"next": "~14.2.21",
"pino-pretty": "~11.2.1",
"polished": "~4.3.1",
"react": "~18.3.1",
"react-dom": "~18.3.1",
"supports-color": "~9.4.0",
"tronweb": "~6.0.0",
"vconsole": "~3.15.1",
"viem": "~2.21.14",
"wagmi": "^2"
},
Expand All @@ -51,7 +52,6 @@
"eslint-config-next": "14.2.3",
"lint-staged": "~13.0.3",
"prettier": "~2.7.1",
"supports-color": "8.1.1",
"typescript": "5.5.4"
},
"lint-staged": {
Expand Down
2 changes: 2 additions & 0 deletions apps/canonical-bridge-ui/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { AppProps } from 'next/app';

import { ThemeProvider } from '@/core/components/ThemeProvider';
import { SvgDefs } from '@/core/components/icons/SvgDefs';
import { VConsole } from '@/core/components/VConsole';

const queryClient = new QueryClient({
defaultOptions: {
Expand All @@ -19,6 +20,7 @@ const queryClient = new QueryClient({
export default function App({ Component, ...restProps }: AppProps) {
return (
<>
<VConsole />
<Head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
</Head>
Expand Down
10 changes: 0 additions & 10 deletions apps/canonical-bridge-ui/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@ export default class Document extends NextDocument {
<Html lang="en">
<Head>
<link rel="stylesheet" href={`${env.ASSET_PREFIX}/fonts/index.css`} />
<script src="https://unpkg.com/vconsole@latest/dist/vconsole.min.js" defer></script>
<script
dangerouslySetInnerHTML={{
__html: `
window.onload = () => {
new window.VConsole();
}
`,
}}
/>
<style
dangerouslySetInnerHTML={{
__html: `
Expand Down
Loading

0 comments on commit 119bfa7

Please sign in to comment.