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

Merging main with prod #162

Merged
merged 4 commits into from
Oct 15, 2024
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
119 changes: 59 additions & 60 deletions components/NftdataCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,72 +68,73 @@ const NftdataCard: React.FC<ReviewCardProps> = ({
}) => {
const [imageSrc, setImageSrc] = React.useState<string | null>(null);
const [attributes, setAttributes] = React.useState<any>(null);

const [isLoading, setIsLoading] = useState(true);

const getUserAddressFromCookie = () => {
return Cookies.get("erebrus_wallet") || "";
};
const userAddress = getUserAddressFromCookie();
const fetchMetaData = async () => {
if (
!metaData ||
!metaData.current_token_data ||
!metaData.current_token_data.token_uri
) {
console.log("Missing metadata or token URI");
setIsLoading(false);
return;
}

try {
let metadata;
if (chainSymbol === "sol") {
const response = await axios.get(metaData.current_token_data.token_uri);
metadata = response.data;
console.log("Solana Metadata:", metadata);
} else if (chainSymbol === "apt") {
const ipfsCid = metaData.current_token_data.token_uri.replace(
"ipfs://",
""
);
console.log("IPFS CID:", ipfsCid);
const metadataResponse = await axios.get(
`https://ipfs.io/ipfs/${ipfsCid}`
);
metadata = metadataResponse.data;
console.log("Aptos Metadata:", metadata);
} else {
console.log("Unsupported chain");
useEffect(() => {
const fetchMetaData = async () => {
if (
!metaData ||
!metaData.current_token_data ||
!metaData.current_token_data.token_uri
) {
console.log("Missing metadata or token URI");
setIsLoading(false);
return;
}

const imageUrl =
metadata?.image ||
metadata?.image_url ||
metadata?.imageUrl ||
metadata?.url;
setImageSrc(imageUrl?.replace("ipfs://", "https://ipfs.io/ipfs/"));
setAttributes({
name: metadata.name,
description: metadata.description,
symbol: metadata.symbol,
externalUrl: metadata.external_url,
collection: metadata.collection,
...metadata.attributes,
});
} catch (error) {
console.error("Error fetching metadata:", error);
} finally {
setIsLoading(false);
}
};
try {
let metadata;
if (chainSymbol === "sol") {
const response = await axios.get(metaData.current_token_data.token_uri);
metadata = response.data;
console.log("Solana Metadata:", metadata);
} else if (chainSymbol === "apt") {
const ipfsCid = metaData.current_token_data.token_uri.replace(
"ipfs://",
""
);
console.log("IPFS CID:", ipfsCid);
const metadataResponse = await axios.get(
`https://ipfs.io/ipfs/${ipfsCid}`
);
metadata = metadataResponse.data;
console.log("Aptos Metadata:", metadata);
} else {
console.log("Unsupported chain");
setIsLoading(false);
return;
}

const imageUrl =
metadata?.image ||
metadata?.image_url ||
metadata?.imageUrl ||
metadata?.url ||
metaData.current_token_data.cdn_asset_uris?.cdn_image_uri;
setImageSrc(imageUrl?.replace("ipfs://", "https://ipfs.io/ipfs/"));
setAttributes({
name: metadata.name || metaData.current_token_data.token_name,
description: metadata.description || metaData.current_token_data.description,
symbol: metadata.symbol,
externalUrl: metadata.external_url,
collection: metadata.collection || metaData.current_token_data.current_collection,
...metadata.attributes,
});
} catch (error) {
console.error("Error fetching metadata:", error);
} finally {
setIsLoading(false);
}
};

// Call fetchMetaData immediately if metaData is available
if (metaData && isLoading) {
fetchMetaData();
}
}, [metaData, chainSymbol]);


if (isLoading) {
return (
<div className="flex flex-col items-center justify-center w-full max-w-sm mx-auto">
Expand All @@ -151,6 +152,7 @@ const NftdataCard: React.FC<ReviewCardProps> = ({
return null;
}


return (
<div
className="w-full cursor-pointer rounded-3xl"
Expand All @@ -169,20 +171,17 @@ const NftdataCard: React.FC<ReviewCardProps> = ({
<div>
<div className="flex flex-col">
<div className="">
<img
<img
alt={metaData.current_token_data.token_name}
src={
imageSrc ||
metaData.current_token_data.cdn_asset_uris?.cdn_image_uri
}
src={imageSrc || "/path/to/placeholder/image.png"}
className="w-full h-48 object-cover rounded-lg"
onError={(e) => {
console.error(
"Image failed to load:",
(e.target as HTMLImageElement).src
);
(e.target as HTMLImageElement).src =
"/path/to/placeholder/image.png"; // Fallback image
"/path/to/placeholder/image.png";
}}
/>
</div>
Expand Down
74 changes: 70 additions & 4 deletions components/UserNFTs.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { Connection, PublicKey } from '@solana/web3.js';
import { Metaplex } from '@metaplex-foundation/js';
import { AptosClient, TokenClient } from 'aptos';
import axios from 'axios';
import Cookies from 'js-cookie';

const ALLOWED_COLLECTIONS = {
sol: ['SMB Gen2', 'sharx by sharky.fi', 'Superteam Member NFT', 'Deanslist', 'SMB Gen3', "Erebrus Community NFT #001"],
apt: ["Undying City Equipment Collection"]
};

const fetchUserNFTs = async (userAddress: string, chainSymbol: string) => {
const fetchUserNFTs = async (chainSymbol: string) => {
const userAddress = Cookies.get('erebrus_wallet');

if (!userAddress) {
console.log('No user address provided');
console.log('No user address found in cookies');
return [];
}

Expand Down Expand Up @@ -56,8 +60,70 @@ const fetchUserNFTs = async (userAddress: string, chainSymbol: string) => {

return filteredNFTs;

}
else {
} else if (chainSymbol === 'apt') {
const APTOS_GRAPHQL_ENDPOINT = process.env.NEXT_PUBLIC_APTOS_GRAPHQL_ENDPOINT;

const query = `
query GetAccountNfts($address: String) {
current_token_ownerships_v2(
where: {owner_address: {_eq: $address}, amount: {_gt: "0"}}
) {
current_token_data {
collection_id
largest_property_version_v1
current_collection {
collection_id
collection_name
description
creator_address
uri
__typename
}
description
token_name
token_data_id
token_standard
token_uri
__typename
}
owner_address
amount
__typename
}
}
`;

const variables = { address: userAddress };

const response = await axios.post(APTOS_GRAPHQL_ENDPOINT, {
query,
variables
});

const nfts = response.data.data.current_token_ownerships_v2;

const filteredNFTs = nfts
.filter(nft => nft.current_token_data.current_collection.collection_name === "Undying City Equipment Collection")
.map(nft => ({
amount: nft.amount,
current_token_data: {
token_name: nft.current_token_data.token_name,
token_uri: nft.current_token_data.token_uri,
description: nft.current_token_data.description,
token_data_id: nft.current_token_data.token_data_id,
cdn_asset_uris: {
cdn_image_uri: nft.current_token_data.token_uri,
},
collection: nft.current_token_data.current_collection.collection_name,
symbol: '',
current_collection: nft.current_token_data.current_collection,
},
}));

console.log('Filtered Aptos NFTs:', filteredNFTs);
return filteredNFTs;

} else {
console.log('NFT fetching for this chain not implemented yet');
return [];
}
Expand Down
2 changes: 1 addition & 1 deletion pages/subscription.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ const Subscription = () => {
setNftError(null);
try {
const chainSymbol = Cookies.get("Chain_symbol") || "sol"; // Default to "sol" if not set
const nfts = await fetchUserNFTs(wallet, chainSymbol);
const nfts = await fetchUserNFTs(wallet);
setnftdata(nfts);
} catch (error) {
console.error("Error fetching NFTs:", error);
Expand Down
Loading