Skip to content

Commit

Permalink
Merge pull request #8 from Scaffold-Stark/fix/cors-issue
Browse files Browse the repository at this point in the history
fix cors issue in dev environment when accessing api.coingecko.com
  • Loading branch information
0xquantum3labs authored Nov 6, 2024
2 parents 59f4b71 + a455870 commit 09b230a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
26 changes: 26 additions & 0 deletions packages/nextjs/app/api/price/[symbol]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export async function GET(_: Request, { params: { symbol } }: { params: { symbol: string } }) {
let apiUrl = "";
if (symbol === "ETH") {
apiUrl = "https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd";
} else if (symbol === "STRK") {
apiUrl = "https://api.coingecko.com/api/v3/simple/price?ids=starknet&vs_currencies=usd";
} else {
return Response.json({
ethereum: { usd: 0 },
starknet: { usd: 0 }
});
}
try {
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error(`coingecko response status: ${response.status}`);
}
const json = await response.json();
return Response.json(json);
} catch (e) {
return Response.json({
ethereum: { usd: 0 },
starknet: { usd: 0 }
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@ const updatePriceCache = async (symbol: string, retries = 3): Promise<number> =>
let attempt = 0;
while (attempt < retries) {
try {
let apiUrl = "";
if (symbol === "ETH") {
apiUrl = `https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd`;
} else if (symbol === "STRK") {
apiUrl = `https://api.coingecko.com/api/v3/simple/price?ids=starknet&vs_currencies=usd`;
}
const response = await fetch(apiUrl);
const response = await fetch(`/api/price/${symbol}`);
const data = await response.json();
const price = symbol === "ETH" ? data.ethereum.usd : data.starknet.usd;
priceCache[symbol] = price;
Expand Down
28 changes: 19 additions & 9 deletions packages/nextjs/public/networks/eth-network-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 09b230a

Please sign in to comment.