Skip to content

Commit

Permalink
fix gnosis pricing
Browse files Browse the repository at this point in the history
  • Loading branch information
vrtnd committed May 10, 2024
1 parent ed3c1ba commit 2b7a232
Showing 1 changed file with 22 additions and 23 deletions.
45 changes: 22 additions & 23 deletions src/utils/prices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,34 @@ export const getSingleLlamaPrice = async (
return null;
};

const NAME_MAPPING: Record<string, string> = {
"b2-mainnet": "bsquared",
xdai: "gnosis",
};

export const getLlamaPrices = async (tokens: string[], timestamp?: number) => {
let finalPrices = {} as any;
let remainingTokens = tokens?.map((token) =>
token?.includes("b2-mainnet") ? `bsquared:${token?.split(":")[1]}` : token
);
const finalPrices: { [key: string]: any } = {};
let remainingTokens = tokens.map((token) => {
const [prefix, id] = token.split(":");
const mappedPrefix = NAME_MAPPING[prefix] || prefix;
return `${mappedPrefix}:${id}`;
});

while (remainingTokens.length > 0) {
const url = timestamp
? PRICES_API +
`/historical/${timestamp}/${remainingTokens
.slice(0, maxNumberOfPrices)
.reduce((acc, curr) => {
return acc + curr + ",";
}, "")
.slice(0, -1)}`
: PRICES_API +
`/current/${remainingTokens
.slice(0, maxNumberOfPrices)
.reduce((acc, curr) => {
return acc + curr + ",";
}, "")
.slice(0, -1)}`;
const res = await retry(async (_bail: any) => await axios.get(url));
? `${PRICES_API}/historical/${timestamp}/${remainingTokens.slice(0, maxNumberOfPrices).join(",")}`
: `${PRICES_API}/current/${remainingTokens.slice(0, maxNumberOfPrices).join(",")}`;
const res = await retry(async () => await axios.get(url));
const prices = res?.data?.coins;
finalPrices = { ...finalPrices, ...prices };
Object.assign(finalPrices, prices);
remainingTokens = remainingTokens.slice(maxNumberOfPrices);
}

return Object.fromEntries(
Object.entries(finalPrices || {}).map(([key, value]) =>
key?.includes("bsquared") ? [`b2-mainnet:${key?.split(":")[1]}`, value] : [key, value]
)
Object.entries(finalPrices).map(([key, value]) => {
const [prefix, id] = key.split(":");
const originalPrefix = Object.entries(NAME_MAPPING).find(([, mappedName]) => mappedName === prefix)?.[0];
return [`${originalPrefix || prefix}:${id}`, value];
})
);
};

0 comments on commit 2b7a232

Please sign in to comment.