Skip to content

Commit

Permalink
Update USD pricing to use CoinGecko
Browse files Browse the repository at this point in the history
  • Loading branch information
CRBl69 committed Jan 2, 2025
1 parent 6e635ee commit 44a8c33
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/typescript/frontend/src/app/dev/verify_api_keys/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ const VerifyApiKeys = async () => {
throw new Error(`Couldn't fetch the price feed on the server. ${msg}`);
}

// Check that CoinMarketCap API key works
// Check that CoinGecko API key works
try {
await getAptPrice();
} catch (e) {
throw new Error(`Couldn't fetch APT price.\n\tInvalid CoinMarketCap API key.`);
throw new Error(`Couldn't fetch APT price.\n\tInvalid CoinGecko API key.`);
}

return <div className="bg-black w-full h-full m-auto pixel-heading-2">LGTM</div>;
Expand Down
27 changes: 16 additions & 11 deletions src/typescript/frontend/src/lib/queries/get-apt-price.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
"use server";

import { COINMARKETCAP_API_KEY } from "lib/server-env";
import { COINGECKO_API_KEY } from "lib/server-env";
import { unstable_cache } from "next/cache";

const COINMARKETCAP_APT_ID = "21794";
const COINMARKETCAP_ROOT_URL = "https://pro-api.coinmarketcap.com";
const COINMARKETCAP_ENDPOINT = "v2/cryptocurrency/quotes/latest";
const COINGECKO_APT_ID = "aptos";
const COINGECKO_ROOT_URL = "https://pro-api.coingecko.com/api";
const COINGECKO_ENDPOINT = "v3/simple/price";
const COINGECKO_PARAMS = "vs_currencies=usd&precision=4";

export const getAptPrice = unstable_cache(
async () =>
fetch(`${COINMARKETCAP_ROOT_URL}/${COINMARKETCAP_ENDPOINT}?id=${COINMARKETCAP_APT_ID}`, {
headers: {
Accept: "application/json",
"X-CMC_PRO_API_KEY": COINMARKETCAP_API_KEY,
},
})
fetch(
`${COINGECKO_ROOT_URL}/${COINGECKO_ENDPOINT}` +
`?ids=${COINGECKO_APT_ID}&${COINGECKO_PARAMS}`,
{
headers: {
Accept: "application/json",
"x-cg-pro-api-key": COINGECKO_API_KEY,
},
}
)
.then((r) => r.json())
.then((r) => r.data[COINMARKETCAP_APT_ID].quote.USD.price as number),
.then((r) => r.aptos.usd as number),
["apt-price"],
{ revalidate: 60 * 10 } // Ten minutes
);
6 changes: 3 additions & 3 deletions src/typescript/frontend/src/lib/server-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ if (typeof process.env.REVALIDATION_TIME === "undefined") {
throw new Error("Environment variable REVALIDATION_TIME is undefined.");
}

if (typeof process.env.COINMARKETCAP_API_KEY === "undefined") {
throw new Error("Environment variable COINMARKETCAP_API_KEY is undefined.");
if (typeof process.env.COINGECKO_API_KEY === "undefined") {
throw new Error("Environment variable COINGECKO_API_KEY is undefined.");
}

export const GEOBLOCKED: { countries: string[]; regions: string[] } = JSON.parse(
Expand All @@ -32,7 +32,7 @@ export const GEOBLOCKING_ENABLED = GEOBLOCKED.countries.length > 0 || GEOBLOCKED
export const ALLOWLISTER3K_URL: string | undefined = process.env.ALLOWLISTER3K_URL;
export const REVALIDATION_TIME: number = Number(process.env.REVALIDATION_TIME);
export const PRE_LAUNCH_TEASER: boolean = process.env.PRE_LAUNCH_TEASER === "true";
export const COINMARKETCAP_API_KEY: string = process.env.COINMARKETCAP_API_KEY;
export const COINGECKO_API_KEY: string = process.env.COINGECKO_API_KEY;

if (
APTOS_NETWORK.toString() === "local" &&
Expand Down

0 comments on commit 44a8c33

Please sign in to comment.