Skip to content

Commit

Permalink
fix: Filter the token whose price is 0
Browse files Browse the repository at this point in the history
  • Loading branch information
wenty22 committed Jan 24, 2025
1 parent 7c3a46e commit 407aeff
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
21 changes: 15 additions & 6 deletions apps/canonical-bridge-server/src/module/token/token.processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,24 @@ export class TokenProcessor extends WorkerHost {
};
})
.filter((t) => {
return t.symbol && t.price && t.chainId && now - new Date(t.updateAt).getTime() < TIME.DAY;
return (
t.symbol &&
Number(t.price) &&
t.chainId &&
now - new Date(t.updateAt).getTime() < TIME.DAY
);
})
.reduce((r, c) => {
const { symbol, address, chainId } = c;

if (chainId === 1) {
const key = `${chainId}:${symbol.toLowerCase()}`;
r[key] = { price: c.price, decimals: c.decimals };
r[key] = { price: Number(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 };
r[key] = { price: Number(c.price), decimals: c.decimals };
return r;
}, {});

Expand All @@ -151,20 +156,24 @@ export class TokenProcessor extends WorkerHost {
};
})
.filter(
(t) => t.symbol && t.price && t.chainId && now - new Date(t.updateAt).getTime() < TIME.DAY,
(t) =>
t.symbol &&
Number(t.price) &&
t.chainId &&
now - new Date(t.updateAt).getTime() < TIME.DAY,
)
.reduce((r, c) => {
const { symbol, address, chainId } = c;

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

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

r[key] = { price: c.price, id: c.id };
r[key] = { price: Number(c.price), id: c.id };
return r;
}, {});

Expand Down
12 changes: 9 additions & 3 deletions apps/canonical-bridge-server/src/module/token/token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,17 @@ export class TokenService {
}

const [cmcRes, llamaRes] = await Promise.allSettled(reqArr);
if (cmcRes.status === 'fulfilled' && cmcRes.value?.[0]?.quote?.USD?.price !== undefined) {
return cmcRes.value?.[0]?.quote.USD?.price;
if (cmcRes.status === 'fulfilled') {
const price = cmcRes.value?.[0]?.quote?.USD?.price;
if (price !== undefined) {
return Number(price);
}
}
if (llamaRes.status === 'fulfilled' && llamaRes.value?.coins) {
return Object.values<ICoinPrice>(llamaRes.value.coins ?? {})?.[0]?.price;
const price = Object.values<ICoinPrice>(llamaRes.value.coins ?? {})?.[0]?.price;
if (price !== undefined) {
return Number(price);
}
}

const cmcPrices = await this.cache.get(`${CACHE_KEY.CMC_CONFIG_V2}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export function useTokenUpperLimit() {
})
).data;
}
return null;
},
});

Expand All @@ -45,7 +46,7 @@ export function useTokenUpperLimit() {
return {
isError: true,
};
} else if (price !== undefined) {
} else if (price) {
const upperLimit = bridgeConfig.transfer.dollarUpperLimit / price;
const value = parseUnits(String(upperLimit), selectedToken.decimals);

Expand Down

0 comments on commit 407aeff

Please sign in to comment.