Skip to content

Add /verify-nft-holder/{contract_address} endpoint and related support work. #10

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

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,10 @@ functions:
- httpApi:
path: /beacon/writ-of-passage/verify
method: post
# /nft-holders/{nft_address}
verifyNftHolders:
handler: src/handlers/nft-holder.verifyNftHolders
events:
- httpApi:
path: /verify-nft-holder/{address}
method: post
4 changes: 3 additions & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ module.exports = {
"0x64bfb08217b30b70f287a1b7f0670bdd49f8a13f",
],
"Treasure | Ecofund (arb1)": ["0x482729215aaf99b3199e41125865821ed5a4978a"],
"Treasure | Ecofund (treasure)": ["0x3418e91949e17ac887c2daeaf7f0799ea9f38f22"],
"Treasure | Ecofund (treasure)": [
"0x3418e91949e17ac887c2daeaf7f0799ea9f38f22",
],
"Treasure | Contributor Allocation (arb1)": [
"0x4D3aAA252850EE7C82b299CB5778925BBE92f1fC", // Multisig
"0xfC05C3C2814DFCfD77Bf8F6796dF413D8BE3D346", // Liquifi Escrow Contract"
Expand Down
17 changes: 17 additions & 0 deletions src/handlers/nft-holder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { hasNft } = require("../services/nft-holder");
const { parseVulcanWallets } = require("../utils/vulcan");

exports.verifyNftHolders = async (event) => {
const { address } = event.pathParameters ?? {};
if (!address) {
return {
success: false,
reason: `'address' was undefined.`,
};
}
const wallets = parseVulcanWallets(event);
console.log(`Querying NFT (${address}) holder status for wallets:`, wallets);
return {
success: await hasNft(address, wallets),
};
};
2 changes: 2 additions & 0 deletions src/services/beacon.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ exports.hasFoundingCharacter = async (wallets) => {
...wallets.map((wallet) =>
fetchUserInventory({
userAddress: wallet,
chain: "arb",
collectionAddresses: [CONTRACT_BEACON],
})
),
Expand Down Expand Up @@ -46,6 +47,7 @@ exports.hasPet = async (wallets) => {
...wallets.map((wallet) =>
fetchUserInventory({
userAddress: wallet,
chain: "arb",
collectionAddresses: [CONTRACT_BEACON],
})
),
Expand Down
16 changes: 16 additions & 0 deletions src/services/nft-holder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { fetchUserInventory } = require("../utils/inventory");

exports.hasNft = async (address, wallets) => {
const inventories = await Promise.all(
wallets.map((wallet) =>
fetchUserInventory({
userAddress: wallet,
chain: "treasure",
collectionAddresses: [address],
limit: 1,
})
)
);

return inventories.flat().length > 0;
};
44 changes: 22 additions & 22 deletions src/utils/inventory.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
exports.fetchUserInventory = async ({
userAddress,
chain,
collectionAddresses = [],
tokens = [],
projection = "collectionAddr,collectionUrlSlug,queryUserQuantityOwned,metadata,image",
limit = undefined,
}) => {
const url = new URL("https://trove-api.treasure.lol/tokens-for-user");
url.searchParams.append("userAddress", userAddress);
url.searchParams.append("projection", projection);
if (tokens.length > 0) {
url.searchParams.append(
"ids",
tokens
.map(({ address, tokenId }) => `arb/${address}/${tokenId}`)
.join(",")
);
} else if (collectionAddresses.length > 0) {
url.searchParams.append(
"slugs",
collectionAddresses.map((address) => `arb/${address}`).join(",")
);
}
const url = new URL("https://trove-api.treasure.lol/tokens-for-user-page");

const body = {
userAddress,
projection,
limit,
ids: tokens.length
? tokens.map(({ address, tokenId }) => `${chain}/${address}/${tokenId}`)
: undefined,
slugs: collectionAddresses.length
? collectionAddresses.map((address) => `${chain}/${address}`)
: undefined,
};

const response = await fetch(url, {
headers: {
"X-API-Key": process.env.TROVE_API_KEY,
},
method: "POST",
headers: { "X-API-Key": process.env.TROVE_API_KEY },
body: JSON.stringify(body),
});

const results = await response.json();
if (!Array.isArray(results)) {
if (!results?.tokens || !Array.isArray(results.tokens)) {
throw new Error(
`Error fetching user inventory: ${results?.message ?? "Unknown error"}`
`Error fetching user inventory: ${results?.message ?? results?.errorMessage ?? "Unknown error"}`
);
}

return results
return results.tokens
.map(
({
collectionAddr: address,
Expand Down