-
Notifications
You must be signed in to change notification settings - Fork 22
Add fetchAllMixedAccounts helper for heterogeneous multi-account fetching #248
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
Open
blockiosaurus
wants to merge
3
commits into
v1.0
Choose a base branch
from
claude/multi-account-fetch-helper-L4obG
base: v1.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+758
−3
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| {% extends "layout.njk" %} | ||
|
|
||
| {% block main %} | ||
| {{ imports }} | ||
| {% for account in accounts %} | ||
| import { {{ account.pascalName }}, deserialize{{ account.pascalName }} } from './{{ account.camelName }}'; | ||
| {% endfor %} | ||
|
|
||
| export type {{ programPascalName }}Account = | ||
| {% for account in accounts %} | ||
| | {{ account.pascalName }} | ||
| {% endfor %}; | ||
|
|
||
| function accountDataMatches( | ||
| data: Uint8Array, | ||
| expected: Uint8Array, | ||
| offset: number | ||
| ): boolean { | ||
| if (data.length < offset + expected.length) return false; | ||
| for (let i = 0; i < expected.length; i++) { | ||
| if (data[offset + i] !== expected[i]) return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| export function deserialize{{ programPascalName }}Account( | ||
| rawAccount: RpcAccount | ||
| ): {{ programPascalName }}Account { | ||
| const data = rawAccount.data; | ||
| {% for account in accounts %} | ||
| if ({{ account.condition }}) { | ||
| return deserialize{{ account.pascalName }}(rawAccount); | ||
| } | ||
| {% endfor %} | ||
| throw new Error( | ||
| 'The provided account could not be identified as a {{ program.name }} account.' | ||
| ); | ||
| } | ||
|
|
||
| export async function fetchAll{{ programPascalName }}Accounts( | ||
| context: Pick<Context, 'rpc'>, | ||
| publicKeys: Array<PublicKey | Pda>, | ||
| options?: RpcGetAccountsOptions, | ||
| ): Promise<{{ programPascalName }}Account[]> { | ||
| const maybeAccounts = await context.rpc.getAccounts( | ||
| publicKeys.map((key) => toPublicKey(key, false)), | ||
| options, | ||
| ); | ||
| return maybeAccounts.map((maybeAccount) => { | ||
| assertAccountExists(maybeAccount); | ||
| return deserialize{{ programPascalName }}Account(maybeAccount); | ||
| }); | ||
| } | ||
|
|
||
| export async function safeFetchAll{{ programPascalName }}Accounts( | ||
| context: Pick<Context, 'rpc'>, | ||
| publicKeys: Array<PublicKey | Pda>, | ||
| options?: RpcGetAccountsOptions, | ||
| ): Promise<({{ programPascalName }}Account | null)[]> { | ||
| const maybeAccounts = await context.rpc.getAccounts( | ||
| publicKeys.map((key) => toPublicKey(key, false)), | ||
| options, | ||
| ); | ||
| return maybeAccounts.map((maybeAccount) => { | ||
| return maybeAccount.exists | ||
| ? deserialize{{ programPascalName }}Account(maybeAccount as RpcAccount) | ||
| : null; | ||
| }); | ||
| } | ||
| {% endblock %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
test/packages/js/src/generated/accounts/mplCandyMachineCoreHelpers.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /** | ||
| * This code was AUTOGENERATED using the kinobi library. | ||
| * Please DO NOT EDIT THIS FILE, instead use visitors | ||
| * to add features, then rerun kinobi to update it. | ||
| * | ||
| * @see https://github.com/metaplex-foundation/kinobi | ||
| */ | ||
|
|
||
| import { | ||
| Context, | ||
| Pda, | ||
| PublicKey, | ||
| RpcAccount, | ||
| RpcGetAccountsOptions, | ||
| assertAccountExists, | ||
| publicKey as toPublicKey, | ||
| } from '@metaplex-foundation/umi'; | ||
| import { CandyMachine, deserializeCandyMachine } from './candyMachine'; | ||
|
|
||
| export type MplCandyMachineCoreAccount = CandyMachine; | ||
|
|
||
| function accountDataMatches( | ||
| data: Uint8Array, | ||
| expected: Uint8Array, | ||
| offset: number | ||
| ): boolean { | ||
| if (data.length < offset + expected.length) return false; | ||
| for (let i = 0; i < expected.length; i++) { | ||
| if (data[offset + i] !== expected[i]) return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| export function deserializeMplCandyMachineCoreAccount( | ||
| rawAccount: RpcAccount | ||
| ): MplCandyMachineCoreAccount { | ||
| const data = rawAccount.data; | ||
| if ( | ||
| accountDataMatches( | ||
| data, | ||
| new Uint8Array([51, 173, 177, 113, 25, 241, 109, 189]), | ||
| 0 | ||
| ) | ||
| ) { | ||
| return deserializeCandyMachine(rawAccount); | ||
| } | ||
| throw new Error( | ||
| 'The provided account could not be identified as a mplCandyMachineCore account.' | ||
| ); | ||
| } | ||
|
|
||
| export async function fetchAllMplCandyMachineCoreAccounts( | ||
| context: Pick<Context, 'rpc'>, | ||
| publicKeys: Array<PublicKey | Pda>, | ||
| options?: RpcGetAccountsOptions | ||
| ): Promise<MplCandyMachineCoreAccount[]> { | ||
| const maybeAccounts = await context.rpc.getAccounts( | ||
| publicKeys.map((key) => toPublicKey(key, false)), | ||
| options | ||
| ); | ||
| return maybeAccounts.map((maybeAccount) => { | ||
| assertAccountExists(maybeAccount); | ||
| return deserializeMplCandyMachineCoreAccount(maybeAccount); | ||
| }); | ||
| } | ||
|
|
||
| export async function safeFetchAllMplCandyMachineCoreAccounts( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice |
||
| context: Pick<Context, 'rpc'>, | ||
| publicKeys: Array<PublicKey | Pda>, | ||
| options?: RpcGetAccountsOptions | ||
| ): Promise<(MplCandyMachineCoreAccount | null)[]> { | ||
| const maybeAccounts = await context.rpc.getAccounts( | ||
| publicKeys.map((key) => toPublicKey(key, false)), | ||
| options | ||
| ); | ||
| return maybeAccounts.map((maybeAccount) => { | ||
| return maybeAccount.exists | ||
| ? deserializeMplCandyMachineCoreAccount(maybeAccount as RpcAccount) | ||
| : null; | ||
| }); | ||
| } | ||
80 changes: 80 additions & 0 deletions
80
test/packages/js/src/generated/accounts/mplTokenAuthRulesHelpers.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /** | ||
| * This code was AUTOGENERATED using the kinobi library. | ||
| * Please DO NOT EDIT THIS FILE, instead use visitors | ||
| * to add features, then rerun kinobi to update it. | ||
| * | ||
| * @see https://github.com/metaplex-foundation/kinobi | ||
| */ | ||
|
|
||
| import { | ||
| Context, | ||
| Pda, | ||
| PublicKey, | ||
| RpcAccount, | ||
| RpcGetAccountsOptions, | ||
| assertAccountExists, | ||
| publicKey as toPublicKey, | ||
| } from '@metaplex-foundation/umi'; | ||
| import { u64 } from '@metaplex-foundation/umi/serializers'; | ||
| import { TaKey } from '../types'; | ||
| import { | ||
| FrequencyAccount, | ||
| deserializeFrequencyAccount, | ||
| } from './frequencyAccount'; | ||
|
|
||
| export type MplTokenAuthRulesAccount = FrequencyAccount; | ||
|
|
||
| function accountDataMatches( | ||
| data: Uint8Array, | ||
| expected: Uint8Array, | ||
| offset: number | ||
| ): boolean { | ||
| if (data.length < offset + expected.length) return false; | ||
| for (let i = 0; i < expected.length; i++) { | ||
| if (data[offset + i] !== expected[i]) return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| export function deserializeMplTokenAuthRulesAccount( | ||
| rawAccount: RpcAccount | ||
| ): MplTokenAuthRulesAccount { | ||
| const data = rawAccount.data; | ||
| if (accountDataMatches(data, u64().serialize(TaKey.Frequency), 0)) { | ||
| return deserializeFrequencyAccount(rawAccount); | ||
| } | ||
| throw new Error( | ||
| 'The provided account could not be identified as a mplTokenAuthRules account.' | ||
| ); | ||
| } | ||
|
|
||
| export async function fetchAllMplTokenAuthRulesAccounts( | ||
| context: Pick<Context, 'rpc'>, | ||
| publicKeys: Array<PublicKey | Pda>, | ||
| options?: RpcGetAccountsOptions | ||
| ): Promise<MplTokenAuthRulesAccount[]> { | ||
| const maybeAccounts = await context.rpc.getAccounts( | ||
| publicKeys.map((key) => toPublicKey(key, false)), | ||
| options | ||
| ); | ||
| return maybeAccounts.map((maybeAccount) => { | ||
| assertAccountExists(maybeAccount); | ||
| return deserializeMplTokenAuthRulesAccount(maybeAccount); | ||
| }); | ||
| } | ||
|
|
||
| export async function safeFetchAllMplTokenAuthRulesAccounts( | ||
| context: Pick<Context, 'rpc'>, | ||
| publicKeys: Array<PublicKey | Pda>, | ||
| options?: RpcGetAccountsOptions | ||
| ): Promise<(MplTokenAuthRulesAccount | null)[]> { | ||
| const maybeAccounts = await context.rpc.getAccounts( | ||
| publicKeys.map((key) => toPublicKey(key, false)), | ||
| options | ||
| ); | ||
| return maybeAccounts.map((maybeAccount) => { | ||
| return maybeAccount.exists | ||
| ? deserializeMplTokenAuthRulesAccount(maybeAccount as RpcAccount) | ||
| : null; | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we shouldn't ever need to cast if the types are correct. (we should make the types correct)