Skip to content

Commit

Permalink
Merge pull request #429 from curvefi/fix/fix-getUsdRate-for-lite-netw…
Browse files Browse the repository at this point in the history
…orks

fix: fixed getUsdRate for lite networks
  • Loading branch information
fedorovdg authored Dec 9, 2024
2 parents 5db84b7 + 2180ae0 commit e0a9270
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@curvefi/api",
"version": "2.65.18",
"version": "2.65.19",
"description": "JavaScript library for curve.fi",
"main": "lib/index.js",
"author": "Macket",
Expand Down
33 changes: 28 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,12 +507,35 @@ export const _getUsdRate = async (assetId: string): Promise<number> => {
if ((_usdRatesCache[assetId]?.time || 0) + 600000 < Date.now()) {
const url = [nativeTokenName, 'ethereum', 'bitcoin', 'link', 'curve-dao-token', 'stasis-eurs'].includes(assetId.toLowerCase()) ?
`https://api.coingecko.com/api/v3/simple/price?ids=${assetId}&vs_currencies=usd` :
`https://api.coingecko.com/api/v3/simple/token_price/${chainName}?contract_addresses=${assetId}&vs_currencies=usd`
const response = await axios.get(url);
`https://api.coingecko.com/api/v3/simple/token_price/${chainName}?contract_addresses=${assetId}&vs_currencies=usd`;

try {
_usdRatesCache[assetId] = {'rate': response.data[assetId]['usd'] ?? 0, 'time': Date.now()};
} catch (err) { // TODO pay attention!
_usdRatesCache[assetId] = {'rate': 0, 'time': Date.now()};
const response = await axios.get(url, {
validateStatus: (status) => status < 500,
});

if (response.status === 200 && response.data[assetId]?.usd !== undefined) {
_usdRatesCache[assetId] = {
'rate': response.data[assetId].usd,
'time': Date.now(),
};
} else {
if (!curve.isLiteChain) {
console.warn(`Non-200 response for ${assetId}:`, response.status, response.data);
}
_usdRatesCache[assetId] = {
'rate': 0,
'time': Date.now(),
};
}
} catch (err: any) {
if (!curve.isLiteChain) {
console.error(`Error fetching USD rate for ${assetId}:`, err.message);
}
_usdRatesCache[assetId] = {
'rate': 0,
'time': Date.now(),
};
}
}

Expand Down

0 comments on commit e0a9270

Please sign in to comment.