Skip to content

Commit

Permalink
feat: Use token price on ethereum if can not get the token price on s…
Browse files Browse the repository at this point in the history
…pecified chain
  • Loading branch information
wenty22 committed Jan 23, 2025
1 parent c43dd43 commit 7c3a46e
Show file tree
Hide file tree
Showing 16 changed files with 89 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .release/.changeset/modern-toys-give.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 token price on ethereum if can not get the token price on specified chain
1 change: 1 addition & 0 deletions .release/.changeset/pre.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"blue-goats-shave",
"brown-suits-matter",
"curvy-dingos-end",
"modern-toys-give",
"smooth-vans-work",
"thick-donkeys-kneel"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,13 @@ export class BridgeProcessor extends WorkerHost {
llamaPrices = {},
chainId,
tokenAddress,
tokenSymbol,
}: {
cmcPrices: ITokenPriceRecord;
llamaPrices: ITokenPriceRecord;
chainId: number;
tokenAddress: string;
tokenSymbol: string;
}) {
if (isEmpty(cmcPrices) && isEmpty(llamaPrices)) {
return true;
Expand All @@ -195,7 +197,10 @@ export class BridgeProcessor extends WorkerHost {
key = `${chainId}:${tokenAddress}`;
}
}
const price = cmcPrices[key] ?? llamaPrices[key];

const symbolKey = `1:${tokenSymbol?.toLowerCase()}`;
const price =
cmcPrices[key] ?? llamaPrices[key] ?? cmcPrices[symbolKey] ?? llamaPrices[symbolKey];

return !!price;
}
Expand All @@ -214,6 +219,7 @@ export class BridgeProcessor extends WorkerHost {
return this.hasTokenPrice({
...priceConfig,
tokenAddress: e.token.address,
tokenSymbol: e.token.symbol,
chainId,
});
});
Expand All @@ -224,12 +230,14 @@ export class BridgeProcessor extends WorkerHost {
const orgHasPrice = this.hasTokenPrice({
...priceConfig,
tokenAddress: e.org_token.token.address,
tokenSymbol: e.org_token.token.symbol,
chainId: e.org_chain_id,
});

const peggedHasPrice = this.hasTokenPrice({
...priceConfig,
tokenAddress: e.pegged_token.token.address,
tokenSymbol: e.pegged_token.token.symbol,
chainId: e.pegged_chain_id,
});

Expand Down Expand Up @@ -259,6 +267,7 @@ export class BridgeProcessor extends WorkerHost {
return this.hasTokenPrice({
...priceConfig,
tokenAddress: e.address,
tokenSymbol: e.symbol,
chainId,
});
});
Expand All @@ -282,6 +291,7 @@ export class BridgeProcessor extends WorkerHost {
return this.hasTokenPrice({
...priceConfig,
tokenAddress: e.token.address,
tokenSymbol: e.token.symbol,
chainId: e.chainId,
});
});
Expand All @@ -301,6 +311,7 @@ export class BridgeProcessor extends WorkerHost {
return this.hasTokenPrice({
...priceConfig,
tokenAddress: e.addr ?? EVM_NATIVE_TOKEN_ADDRESS,
tokenSymbol: e.symbol,
chainId: chain.chainId === 'tron' ? TRON_CHAIN_ID : Number(chain.chainId),
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ export class TokenController {
}

@Get('/v2/price')
getTokenPrice(@Query('chainId') _chainId: string, @Query('tokenAddress') tokenAddress?: string) {
getTokenPrice(
@Query('chainId') _chainId: string,
@Query('tokenAddress') tokenAddress?: string,
@Query('tokenSymbol') tokenSymbol?: string,
) {
const chainId = Number(_chainId);
return this.tokenService.getTokenPrice(chainId, tokenAddress);
return this.tokenService.getTokenPrice(chainId, tokenAddress, tokenSymbol);
}

@Get('/v2/cmc')
Expand Down
21 changes: 16 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 @@ -114,14 +114,18 @@ export class TokenProcessor extends WorkerHost {
};
})
.filter((t) => {
return t.price && t.chainId && now - new Date(t.updateAt).getTime() < TIME.DAY;
return t.symbol && t.price && t.chainId && now - new Date(t.updateAt).getTime() < TIME.DAY;
})
.reduce((r, c) => {
const { address, chainId } = c;
const { symbol, address, chainId } = c;

if (chainId === 1) {
const key = `${chainId}:${symbol.toLowerCase()}`;
r[key] = { price: c.price, decimals: c.decimals };
}

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

r[key] = { price: c.price, decimals: c.decimals };
return r;
}, {});
Expand All @@ -146,9 +150,16 @@ export class TokenProcessor extends WorkerHost {
chainId: chainConfig?.id,
};
})
.filter((t) => t.price && t.chainId && now - new Date(t.updateAt).getTime() < TIME.DAY)
.filter(
(t) => t.symbol && t.price && t.chainId && now - new Date(t.updateAt).getTime() < TIME.DAY,
)
.reduce((r, c) => {
const { address, chainId } = c;
const { symbol, address, chainId } = c;

if (chainId === 1) {
const key = `${chainId}:${symbol.toLowerCase()}`;
r[key] = { price: c.price, id: c.id };
}

const formattedAddr = this.utilService.getFormattedAddress(chainId, address);
const key = formattedAddr ? `${chainId}:${formattedAddr}` : chainId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export class TokenService {
return this.databaseService.getAllCoingeckoTokens();
}

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

Expand Down Expand Up @@ -191,5 +191,11 @@ export class TokenService {
if (llamaRes.status === 'fulfilled' && llamaRes.value?.coins) {
return Object.values<ICoinPrice>(llamaRes.value.coins ?? {})?.[0]?.price;
}

const cmcPrices = await this.cache.get(`${CACHE_KEY.CMC_CONFIG_V2}`);
const llamaPrices = await this.cache.get(`${CACHE_KEY.CMC_CONFIG_V2}`);
const key = `${chainId}:${tokenSymbol}`;

return cmcPrices?.[key] ?? llamaPrices?.[key];
}
}
6 changes: 6 additions & 0 deletions packages/canonical-bridge-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @bnb-chain/canonical-bridge-sdk

## 0.5.0-alpha.11

### Patch Changes

- use token price on ethereum if can not get the token price on specified chain

## 0.5.0-alpha.10

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/canonical-bridge-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bnb-chain/canonical-bridge-sdk",
"version": "0.5.0-alpha.10",
"version": "0.5.0-alpha.11",
"description": "canonical bridge sdk",
"author": "bnb-chain",
"private": false,
Expand Down
8 changes: 8 additions & 0 deletions packages/canonical-bridge-widget/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# @bnb-chain/canonical-bridge-widget

## 0.6.0-alpha.17

### Patch Changes

- use token price on ethereum if can not get the token price on specified chain
- Updated dependencies
- @bnb-chain/canonical-bridge-sdk@0.5.0-alpha.11

## 0.6.0-alpha.16

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions 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.6.0-alpha.16",
"version": "0.6.0-alpha.17",
"description": "canonical bridge widget",
"author": "bnb-chain",
"private": false,
Expand Down Expand Up @@ -46,7 +46,7 @@
"@solana/web3.js": "^1",
"tronweb": "^6",
"@tronweb3/tronwallet-adapter-react-hooks": "^1",
"@bnb-chain/canonical-bridge-sdk": "^0.5.0-alpha.10"
"@bnb-chain/canonical-bridge-sdk": "^0.5.0-alpha.11"
},
"devDependencies": {
"@bnb-chain/canonical-bridge-sdk": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export function useTokenList(tokens: IBridgeToken[] = [], keyword?: string) {
chainId: fromChain?.id,
chainType: fromChain?.chainType,
tokenAddress: item?.address,
tokenSymbol: item.symbol,
});

let value: number | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ function useSortedTokens() {
chainId: fromChainId,
chainType,
tokenAddress: item?.address,
tokenSymbol: item?.symbol,
});

let value: number | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function useTokenUpperLimit() {
chainId: fromChain?.id,
chainType: fromChain?.chainType,
tokenAddress: selectedToken?.address,
tokenSymbol: selectedToken?.symbol,
})
).data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ export function useTokenPrice() {
chainId,
chainType,
tokenAddress,
}: { chainId?: number; chainType?: ChainType; tokenAddress?: string } = {}) => {
tokenSymbol,
}: {
chainId?: number;
chainType?: ChainType;
tokenAddress?: string;
tokenSymbol?: string;
} = {}) => {
if (chainId && chainType && tokenAddress) {
const { cmcPrices, llamaPrices } = tokenPrices;

Expand All @@ -64,7 +70,13 @@ export function useTokenPrice() {
}
}

let price = cmcPrices?.[key]?.price ?? llamaPrices[key]?.price;
const symbolKey = `1:${tokenSymbol?.toLowerCase()}`;
let price =
cmcPrices[key]?.price ??
llamaPrices[key]?.price ??
cmcPrices[symbolKey]?.price ??
llamaPrices[symbolKey]?.price;

if (price !== undefined) {
price = Number(price);
}
Expand Down Expand Up @@ -95,15 +107,18 @@ export function useTokenPrice() {
chainId,
chainType,
tokenAddress,
tokenSymbol,
}: {
chainId: number;
chainType: ChainType;
tokenAddress: string;
tokenSymbol: string;
}) => {
const { data } = await axios.get<{ data: number }>(`${serverEndpoint}/api/token/v2/price`, {
params: {
chainId,
tokenAddress: isNativeToken(tokenAddress, chainType) ? undefined : tokenAddress,
tokenSymbol,
},
});
return data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const MaxLink: React.FC = () => {
chainId: fromChain?.id,
chainType: fromChain?.chainType,
tokenAddress: selectedToken?.address,
tokenSymbol: selectedToken?.symbol,
});

const setMaxAmount = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ export const usePriceValidation = () => {
chainId,
chainType,
tokenAddress,
tokenSymbol,
}: {
chainId: number;
chainType: ChainType;
tokenAddress: string;
tokenSymbol: string;
}) => {
const { data: price } = await fetchTokenPrice({
chainId,
chainType,
tokenAddress,
tokenSymbol,
});
return price;
},
Expand Down

0 comments on commit 7c3a46e

Please sign in to comment.