Skip to content

Commit

Permalink
Purge metaplex-foundatoin/js
Browse files Browse the repository at this point in the history
  • Loading branch information
ChewingGlass committed Jan 19, 2024
1 parent 46386c5 commit 33fad91
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 29 deletions.
87 changes: 58 additions & 29 deletions hooks/useMetaplexMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,43 @@
import { TypedAccountParser } from "@helium/account-fetch-cache";
import { useAccount } from "@helium/account-fetch-cache-hooks";
import {
JsonMetadata,
PROGRAM_ID as MPL_PID,
Metadata,
parseMetadataAccount,
sol,
toMetadata,
} from "@metaplex-foundation/js";
} from "@metaplex-foundation/mpl-token-metadata";
import { NATIVE_MINT } from "@solana/spl-token";
import { AccountInfo, PublicKey } from "@solana/web3.js";
import axios from "axios";
import { useMemo } from "react";
import { useAsync } from "react-async-hook";

const MPL_PID = new PublicKey("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s");
import { useAccount } from "@helium/account-fetch-cache-hooks";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const cache: Record<string, Promise<any>> = {};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function getMetadata(uri: string | undefined): Promise<any | undefined> {
export function getMetadata(
uriIn: string | undefined
): Promise<any | undefined> {
const uri = uriIn?.replace(/\0/g, "");
if (uri) {
if (!cache[uri]) {
cache[uri] = axios
.get(uri, {
.get(uri.replace(/\0/g, ""), {
timeout: 3000,
})
.then((res) => res);
.then((res) => res.data)
.catch((err: any) => {
console.error(`Error at uri ${uri}`, err);
});
}
return cache[uri];
}
return Promise.resolve(undefined);
}

export const METADATA_PARSER: TypedAccountParser<Metadata> = (
publicKey: PublicKey,
_: PublicKey,
account: AccountInfo<Buffer>
) => {
return toMetadata(
parseMetadataAccount({
...account,
lamports: sol(account.lamports),
data: account.data,
publicKey,
})
);
return Metadata.fromAccountInfo(account)[0];
};

export function getMetadataId(mint: PublicKey): PublicKey {
Expand All @@ -53,14 +47,37 @@ export function getMetadataId(mint: PublicKey): PublicKey {
)[0];
}

type TokenInfo = {
name: string;
symbol: string;
logoURI: string;
};

export const tokenInfoToMetadata = (
tokenInfo: TokenInfo | null | undefined
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): any | undefined => {
if (!tokenInfo) return undefined;

return {
json: {
name: tokenInfo.name,
symbol: tokenInfo.symbol,
image: tokenInfo.logoURI,
},
symbol: tokenInfo.symbol,
name: tokenInfo.name,
};
};

const USDC = new PublicKey("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v");
export function useMetaplexMetadata(mint: PublicKey | undefined): {
loading: boolean;
metadata: Metadata | undefined;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
json: any | undefined;
symbol: string | undefined;
name: string | undefined;
image: string | undefined;
} {
const metadataAddr = useMemo(() => {
if (mint) {
Expand All @@ -71,12 +88,11 @@ export function useMetaplexMetadata(mint: PublicKey | undefined): {

const { info: metadataAcc, loading } = useAccount(
metadataAddr,
METADATA_PARSER,
true
METADATA_PARSER
);

const { result: json, loading: jsonLoading } = useAsync(getMetadata, [
metadataAcc?.uri,
metadataAcc?.data.uri.trim(),
]);

if (mint?.equals(NATIVE_MINT)) {
Expand All @@ -89,18 +105,31 @@ export function useMetaplexMetadata(mint: PublicKey | undefined): {
image:
"https://github.com/solana-labs/token-list/blob/main/assets/mainnet/So11111111111111111111111111111111111111112/logo.png?raw=true",
},
image: "https://github.com/solana-labs/token-list/blob/main/assets/mainnet/So11111111111111111111111111111111111111112/logo.png?raw=true",
symbol: "SOL",
name: "SOL",
};
}

if (mint?.equals(USDC)) {
return {
metadata: undefined,
loading: false,
json: {
name: "USDC",
symbol: "USDC",
image:
"https://github.com/solana-labs/token-list/blob/main/assets/mainnet/EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v/logo.png?raw=true",
},
symbol: "USDC",
name: "USDC",
};
}

return {
loading: jsonLoading || loading,
json: json?.data,
json,
metadata: metadataAcc,
symbol: json?.data.symbol || metadataAcc?.symbol,
name: json?.data.name || metadataAcc?.name,
image: json?.data.image,
symbol: json?.symbol || metadataAcc?.data?.symbol,
name: json?.name || metadataAcc?.data?.name,
};
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@helium/state-controller-sdk": "^0.0.8",
"@helium/voter-stake-registry-hooks": "^0.6.21",
"@helium/voter-stake-registry-sdk": "^0.6.21",
"@metaplex-foundation/mpl-token-metadata": "2.10.0",
"@project-serum/anchor": "^0.25.0",
"@solana/spl-token": "^0.3.8",
"@solana/wallet-adapter-base": "^0.9.22",
Expand Down
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,19 @@
resolved "https://registry.yarnpkg.com/@metaplex-foundation/cusper/-/cusper-0.0.2.tgz#dc2032a452d6c269e25f016aa4dd63600e2af975"
integrity sha512-S9RulC2fFCFOQraz61bij+5YCHhSO9llJegK8c8Y6731fSi6snUSQJdCUqYS8AIgR0TKbQvdvgSyIIdbDFZbBA==

"@metaplex-foundation/[email protected]":
version "2.10.0"
resolved "https://registry.yarnpkg.com/@metaplex-foundation/mpl-token-metadata/-/mpl-token-metadata-2.10.0.tgz#87785d305a23bd68075919ce995b3160408ddedc"
integrity sha512-oCAzs4Wl7m+8ZeW6VVX8NDNbMBNDbH0vbysBmY8Eh9Moq0inSjm2dtojzQGEFTpVSAEFEzinRLXtkeWqiiI3ug==
dependencies:
"@metaplex-foundation/beet" "^0.7.1"
"@metaplex-foundation/beet-solana" "^0.4.0"
"@metaplex-foundation/cusper" "^0.0.2"
"@solana/spl-token" "^0.3.6"
"@solana/web3.js" "^1.66.2"
bn.js "^5.2.0"
debug "^4.3.4"

"@metaplex-foundation/mpl-token-metadata@^2.10.0":
version "2.13.0"
resolved "https://registry.yarnpkg.com/@metaplex-foundation/mpl-token-metadata/-/mpl-token-metadata-2.13.0.tgz#ea498190ad4ed1d4c0b8218a72d03bd17a883d11"
Expand Down

0 comments on commit 33fad91

Please sign in to comment.