Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ECO-1713] add utility function to check if someone is allowlisted #88

Merged
merged 6 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cfg/cspell-dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ eswatini
faso
fleur
futuna
galxe
gcloud
hamsa
headlessui
Expand Down
7 changes: 6 additions & 1 deletion src/typescript/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# To run your own local node for e2e testing, set this to "false"
START_LOCAL_NODE_FOR_TEST="true"

Expand All @@ -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, NEXT_PUBLIC_GALXE_CAMPAIGN_ID (and|or) ALLOWLISTER3K_URL needs to be set
NEXT_PUBLIC_IS_ALLOWLIST_ENABLED="false"
GALXE_CAMPAIGN_ID=""
ALLOWLISTER3K_URL=""
17 changes: 16 additions & 1 deletion src/typescript/frontend/src/lib/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.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;
if (["mainnet", "testnet", "devnet", "local", "custom"].includes(network)) {
Expand All @@ -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) {
Expand All @@ -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,
};
59 changes: 59 additions & 0 deletions src/typescript/frontend/src/lib/utils/allowlist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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";

// 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<boolean> {
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;
}
Loading