From b0ee8cae23735b2b23b35debdf6feb1e967a412a Mon Sep 17 00:00:00 2001 From: Bogdan Crisan Date: Mon, 3 Jun 2024 03:41:06 +0200 Subject: [PATCH 1/6] add utility function to check if someone is allowlisted --- cfg/cspell-dictionary.txt | 1 + src/typescript/frontend/src/lib/env.ts | 17 +++++- .../frontend/src/lib/utils/allowlist.ts | 57 +++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 src/typescript/frontend/src/lib/utils/allowlist.ts diff --git a/cfg/cspell-dictionary.txt b/cfg/cspell-dictionary.txt index 744394248..e25d3c194 100644 --- a/cfg/cspell-dictionary.txt +++ b/cfg/cspell-dictionary.txt @@ -25,6 +25,7 @@ eswatini faso fleur futuna +galxe gcloud hamsa headlessui diff --git a/src/typescript/frontend/src/lib/env.ts b/src/typescript/frontend/src/lib/env.ts index fc85e34c8..282a0c50a 100644 --- a/src/typescript/frontend/src/lib/env.ts +++ b/src/typescript/frontend/src/lib/env.ts @@ -4,6 +4,10 @@ let APTOS_NETWORK: Network; let INTEGRATOR_ADDRESS: string; let INTEGRATOR_FEE_RATE_BPS: number; +const ALLOWLISTER3K_URL: string | undefined = process.env.NEXT_PUBLIC_ALLOWLISTER3K_URL; +const IS_ALLOWLIST_ENABLED: boolean = process.env.NEXT_PUBLIC_IS_WHITELIST_ENABLED === "true"; +const GALXE_CAMPAIGN_ID: string | undefined = process.env.NEXT_PUBLIC_GALXE_CAMPAIGN_ID; + if (process.env.NEXT_PUBLIC_APTOS_NETWORK) { const network = process.env.NEXT_PUBLIC_APTOS_NETWORK; if (["mainnet", "testnet", "devnet", "local", "custom"].includes(network)) { @@ -27,6 +31,10 @@ if (process.env.NEXT_PUBLIC_INTEGRATOR_FEE_RATE_BPS) { throw new Error("Environment variable NEXT_PUBLIC_INTEGRATOR_FEE_RATE_BPS is undefined."); } +if (IS_ALLOWLIST_ENABLED && ALLOWLISTER3K_URL === undefined && GALXE_CAMPAIGN_ID === undefined) { + throw new Error("Allowlist is enabled but no allowlist provider is set."); +} + const vercel = process.env.VERCEL === "1"; const local = process.env.INBOX_URL === "http://localhost:3000"; if (vercel && local) { @@ -35,4 +43,11 @@ if (vercel && local) { ); } -export { APTOS_NETWORK, INTEGRATOR_ADDRESS, INTEGRATOR_FEE_RATE_BPS }; +export { + APTOS_NETWORK, + INTEGRATOR_ADDRESS, + INTEGRATOR_FEE_RATE_BPS, + ALLOWLISTER3K_URL, + GALXE_CAMPAIGN_ID, + IS_ALLOWLIST_ENABLED, +}; diff --git a/src/typescript/frontend/src/lib/utils/allowlist.ts b/src/typescript/frontend/src/lib/utils/allowlist.ts new file mode 100644 index 000000000..731cab7e4 --- /dev/null +++ b/src/typescript/frontend/src/lib/utils/allowlist.ts @@ -0,0 +1,57 @@ +import { ALLOWLISTER3K_URL, GALXE_CAMPAIGN_ID, IS_ALLOWLIST_ENABLED } from "lib/env"; + +export const GALXE_URL = "https://graphigo.prd.galaxy.eco/query"; + +// Checks if the given address is allow listed either in Galxe or in Allowlister3000. +// +// If IS_ALLOWLIST_ENABLED is not truthy, the function returns true. +// +// The address can be provided either as "0xabc" or directly "abc". +export async function isAllowListed(address: string): Promise { + if (!IS_ALLOWLIST_ENABLED) { + return true; + } + + if (!address.startsWith("0x")) { + address = `0x${address}`; + } + + if (GALXE_CAMPAIGN_ID !== undefined) { + const condition = await fetch(GALXE_URL, { + method: "POST", + headers: { + "Content-Type": "application/json", + Accept: "application/json", + }, + body: JSON.stringify({ + query: `{ + campaign(id: "${GALXE_CAMPAIGN_ID}") { + whitelistInfo(address: "${address}") { + maxCount + usedCount + claimedLoyaltyPoints + currentPeriodMaxLoyaltyPoints + currentPeriodClaimedLoyaltyPoints + } + } + }`, + }), + }) + .then((r) => r.json()) + .then((data) => data.data.campaign.whitelistInfo.usedCount === 1); + if (condition) { + return true; + } + } + + if (ALLOWLISTER3K_URL !== undefined) { + const condition = await fetch(`${ALLOWLISTER3K_URL}/${address}`) + .then((r) => r.text()) + .then((data) => data === "true"); + if (condition) { + return true; + } + } + + return false; +} From f2dff59327071035f5fed3fff336fec92d33180a Mon Sep 17 00:00:00 2001 From: Bogdan Crisan Date: Tue, 4 Jun 2024 19:21:21 +0200 Subject: [PATCH 2/6] add env vars to example file --- src/typescript/.env.example | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/typescript/.env.example b/src/typescript/.env.example index fadac70d0..c11fce254 100644 --- a/src/typescript/.env.example +++ b/src/typescript/.env.example @@ -1,4 +1,3 @@ - # To run your own local node for e2e testing, set this to "false" START_LOCAL_NODE_FOR_TEST="true" @@ -19,3 +18,9 @@ NEXT_PUBLIC_INTEGRATOR_ADDRESS="0xbabe8f471b7f4744502b1397530bafe80e3731b358c0df # The BPS fee rate for each swap, liquidity provision, or liquidity removal. NEXT_PUBLIC_INTEGRATOR_FEE_RATE_BPS="125" + +# If false, no allowlist is set up +# If true, GALXE_CAMPAIGN_ID (and|or) ALLOWLISTER3K_URL needs to be set +IS_ALLOWLIST_ENABLED="false" +GALXE_CAMPAIGN_ID="" +ALLOWLISTER3K_URL="" From bcc45c141c5cbb3a07e9a98d6f9a65882ae2f087 Mon Sep 17 00:00:00 2001 From: Bogdan Crisan Date: Tue, 4 Jun 2024 19:59:24 +0200 Subject: [PATCH 3/6] fix env vars --- src/typescript/.env.example | 4 ++-- src/typescript/frontend/src/lib/env.ts | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/typescript/.env.example b/src/typescript/.env.example index c11fce254..996ef3868 100644 --- a/src/typescript/.env.example +++ b/src/typescript/.env.example @@ -20,7 +20,7 @@ NEXT_PUBLIC_INTEGRATOR_ADDRESS="0xbabe8f471b7f4744502b1397530bafe80e3731b358c0df NEXT_PUBLIC_INTEGRATOR_FEE_RATE_BPS="125" # If false, no allowlist is set up -# If true, GALXE_CAMPAIGN_ID (and|or) ALLOWLISTER3K_URL needs to be set -IS_ALLOWLIST_ENABLED="false" +# If true, NEXT_PUBLIC_GALXE_CAMPAIGN_ID (and|or) ALLOWLISTER3K_URL needs to be set +NEXT_PUBLIC_IS_ALLOWLIST_ENABLED="false" GALXE_CAMPAIGN_ID="" ALLOWLISTER3K_URL="" diff --git a/src/typescript/frontend/src/lib/env.ts b/src/typescript/frontend/src/lib/env.ts index 282a0c50a..e8627d23b 100644 --- a/src/typescript/frontend/src/lib/env.ts +++ b/src/typescript/frontend/src/lib/env.ts @@ -4,9 +4,9 @@ let APTOS_NETWORK: Network; let INTEGRATOR_ADDRESS: string; let INTEGRATOR_FEE_RATE_BPS: number; -const ALLOWLISTER3K_URL: string | undefined = process.env.NEXT_PUBLIC_ALLOWLISTER3K_URL; -const IS_ALLOWLIST_ENABLED: boolean = process.env.NEXT_PUBLIC_IS_WHITELIST_ENABLED === "true"; -const GALXE_CAMPAIGN_ID: string | undefined = process.env.NEXT_PUBLIC_GALXE_CAMPAIGN_ID; +const ALLOWLISTER3K_URL: string | undefined = process.env.ALLOWLISTER3K_URL; +const IS_ALLOWLIST_ENABLED: boolean = process.env.NEXT_PUBLIC_IS_ALLOWLIST_ENABLED === "true"; +const GALXE_CAMPAIGN_ID: string | undefined = process.env.GALXE_CAMPAIGN_ID; if (process.env.NEXT_PUBLIC_APTOS_NETWORK) { const network = process.env.NEXT_PUBLIC_APTOS_NETWORK; From ca17d58f15ed00b1e038599acc11ac3bc7c0b251 Mon Sep 17 00:00:00 2001 From: Bogdan Crisan Date: Thu, 6 Jun 2024 19:20:03 +0200 Subject: [PATCH 4/6] add import server --- src/typescript/frontend/src/lib/utils/allowlist.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/typescript/frontend/src/lib/utils/allowlist.ts b/src/typescript/frontend/src/lib/utils/allowlist.ts index 731cab7e4..97565a808 100644 --- a/src/typescript/frontend/src/lib/utils/allowlist.ts +++ b/src/typescript/frontend/src/lib/utils/allowlist.ts @@ -1,3 +1,5 @@ +import "server-only"; + import { ALLOWLISTER3K_URL, GALXE_CAMPAIGN_ID, IS_ALLOWLIST_ENABLED } from "lib/env"; export const GALXE_URL = "https://graphigo.prd.galaxy.eco/query"; From 1495e2786dd7fbee86140e5e763941edbd53f7ab Mon Sep 17 00:00:00 2001 From: Bogdan Crisan Date: Thu, 6 Jun 2024 19:25:27 +0200 Subject: [PATCH 5/6] move private vars to build-env.ts --- src/typescript/frontend/src/lib/build-env.ts | 10 +++++++++- src/typescript/frontend/src/lib/env.ts | 8 -------- src/typescript/frontend/src/lib/utils/allowlist.ts | 3 ++- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/typescript/frontend/src/lib/build-env.ts b/src/typescript/frontend/src/lib/build-env.ts index 8abc145d3..e5d4eb655 100644 --- a/src/typescript/frontend/src/lib/build-env.ts +++ b/src/typescript/frontend/src/lib/build-env.ts @@ -1,4 +1,5 @@ import "server-only"; +import { IS_ALLOWLIST_ENABLED } from "./env"; let REVALIDATION_TIME: number; @@ -8,4 +9,11 @@ if (process.env.REVALIDATION_TIME) { if (process.env.NODE) throw new Error("Environment variable REVALIDATION_TIME is undefined."); } -export { REVALIDATION_TIME }; +const ALLOWLISTER3K_URL: string | undefined = process.env.ALLOWLISTER3K_URL; +const GALXE_CAMPAIGN_ID: string | undefined = process.env.GALXE_CAMPAIGN_ID; + +if (IS_ALLOWLIST_ENABLED && ALLOWLISTER3K_URL === undefined && GALXE_CAMPAIGN_ID === undefined) { + throw new Error("Allowlist is enabled but no allowlist provider is set."); +} + +export { ALLOWLISTER3K_URL, GALXE_CAMPAIGN_ID, REVALIDATION_TIME }; diff --git a/src/typescript/frontend/src/lib/env.ts b/src/typescript/frontend/src/lib/env.ts index e8627d23b..fadefdfd3 100644 --- a/src/typescript/frontend/src/lib/env.ts +++ b/src/typescript/frontend/src/lib/env.ts @@ -4,9 +4,7 @@ let APTOS_NETWORK: Network; let INTEGRATOR_ADDRESS: string; let INTEGRATOR_FEE_RATE_BPS: number; -const ALLOWLISTER3K_URL: string | undefined = process.env.ALLOWLISTER3K_URL; const IS_ALLOWLIST_ENABLED: boolean = process.env.NEXT_PUBLIC_IS_ALLOWLIST_ENABLED === "true"; -const GALXE_CAMPAIGN_ID: string | undefined = process.env.GALXE_CAMPAIGN_ID; if (process.env.NEXT_PUBLIC_APTOS_NETWORK) { const network = process.env.NEXT_PUBLIC_APTOS_NETWORK; @@ -31,10 +29,6 @@ if (process.env.NEXT_PUBLIC_INTEGRATOR_FEE_RATE_BPS) { throw new Error("Environment variable NEXT_PUBLIC_INTEGRATOR_FEE_RATE_BPS is undefined."); } -if (IS_ALLOWLIST_ENABLED && ALLOWLISTER3K_URL === undefined && GALXE_CAMPAIGN_ID === undefined) { - throw new Error("Allowlist is enabled but no allowlist provider is set."); -} - const vercel = process.env.VERCEL === "1"; const local = process.env.INBOX_URL === "http://localhost:3000"; if (vercel && local) { @@ -47,7 +41,5 @@ export { APTOS_NETWORK, INTEGRATOR_ADDRESS, INTEGRATOR_FEE_RATE_BPS, - ALLOWLISTER3K_URL, - GALXE_CAMPAIGN_ID, IS_ALLOWLIST_ENABLED, }; diff --git a/src/typescript/frontend/src/lib/utils/allowlist.ts b/src/typescript/frontend/src/lib/utils/allowlist.ts index 97565a808..6b5bcfe4d 100644 --- a/src/typescript/frontend/src/lib/utils/allowlist.ts +++ b/src/typescript/frontend/src/lib/utils/allowlist.ts @@ -1,6 +1,7 @@ import "server-only"; -import { ALLOWLISTER3K_URL, GALXE_CAMPAIGN_ID, IS_ALLOWLIST_ENABLED } from "lib/env"; +import { IS_ALLOWLIST_ENABLED } from "lib/env"; +import { ALLOWLISTER3K_URL, GALXE_CAMPAIGN_ID } from "lib/build-env"; export const GALXE_URL = "https://graphigo.prd.galaxy.eco/query"; From 5673b90c8511cf9989b42a88084da6d6f94902bb Mon Sep 17 00:00:00 2001 From: Bogdan Crisan Date: Thu, 6 Jun 2024 19:29:31 +0200 Subject: [PATCH 6/6] rename build-env to server-env --- src/typescript/frontend/src/lib/{build-env.ts => server-env.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/typescript/frontend/src/lib/{build-env.ts => server-env.ts} (100%) diff --git a/src/typescript/frontend/src/lib/build-env.ts b/src/typescript/frontend/src/lib/server-env.ts similarity index 100% rename from src/typescript/frontend/src/lib/build-env.ts rename to src/typescript/frontend/src/lib/server-env.ts