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

PoC: fix safeFetchAllMetadata throwing if facing broken token metadata #117

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
18 changes: 16 additions & 2 deletions clients/js/src/generated/accounts/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,29 @@ export async function fetchAllMetadata(
export async function safeFetchAllMetadata(
context: Pick<Context, 'rpc'>,
publicKeys: Array<PublicKey | Pda>,
options?: RpcGetAccountsOptions
options?: RpcGetAccountsOptions & {
skipCorruptedMetadata?: boolean
}
): Promise<Metadata[]> {
const maybeAccounts = await context.rpc.getAccounts(
publicKeys.map((key) => toPublicKey(key, false)),
options
);
return maybeAccounts
.filter((maybeAccount) => maybeAccount.exists)
.map((maybeAccount) => deserializeMetadata(maybeAccount as RpcAccount));
.map((maybeAccount) => {
if (!options?.skipCorruptedMetadata) {
return deserializeMetadata(maybeAccount as RpcAccount);
}
try {
// This can fail if the metadata is unexpectedly broken in very rare cases, e.g.
// UnexpectedAccountError: The account at the provided address ['...'] is not of the expected type [MetadataAccountData]
return deserializeMetadata(maybeAccount as RpcAccount);
} catch {
return null;
}
})
.filter((metadata) => metadata !== null)
}

export function getMetadataGpaBuilder(
Expand Down