Skip to content

Commit

Permalink
MIP-7: Add a print delegate (#84)
Browse files Browse the repository at this point in the history
* WIP print delegate.

* Full workflow.

* Arg and lifetime fixes.

* Simplifying instruction and fixing bug, adding test.

* Adding more tests.

* Adding better print helper for delegates.

* Fixing formatting.

* Use `DelegateScenario` to combine Metadata and Holder delegate functionality (#87)

* Use DelegateScenario to combine delegate and PDA creation functions

* Use DelegateScenario to combine revoke functions

* Adding WIP for totally unified handler.

* Fixing Kinobi issues.

* Fixing based on feedback and adding a few more Kinobi changes.

* Fixing client gen.

* Fixing formatting.

* Fixing based on feedback.

---------

Co-authored-by: Michael Danenberg <[email protected]>
  • Loading branch information
blockiosaurus and danenbm authored Jan 31, 2024
1 parent d4d64e6 commit cb043fe
Show file tree
Hide file tree
Showing 46 changed files with 5,690 additions and 153 deletions.
218 changes: 218 additions & 0 deletions clients/js/src/generated/accounts/holderDelegateRecord.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
/**
* 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 {
Account,
Context,
Pda,
PublicKey,
RpcAccount,
RpcGetAccountOptions,
RpcGetAccountsOptions,
assertAccountExists,
deserializeAccount,
gpaBuilder,
publicKey as toPublicKey,
} from '@metaplex-foundation/umi';
import {
Serializer,
publicKey as publicKeySerializer,
string,
struct,
u8,
} from '@metaplex-foundation/umi/serializers';
import {
HolderDelegateRoleSeedArgs,
getHolderDelegateRoleSeedSerializer,
} from '../../hooked';
import { Key, KeyArgs, getKeySerializer } from '../types';

export type HolderDelegateRecord = Account<HolderDelegateRecordAccountData>;

export type HolderDelegateRecordAccountData = {
key: Key;
bump: number;
mint: PublicKey;
delegate: PublicKey;
updateAuthority: PublicKey;
};

export type HolderDelegateRecordAccountDataArgs = {
key: KeyArgs;
bump: number;
mint: PublicKey;
delegate: PublicKey;
updateAuthority: PublicKey;
};

export function getHolderDelegateRecordAccountDataSerializer(): Serializer<
HolderDelegateRecordAccountDataArgs,
HolderDelegateRecordAccountData
> {
return struct<HolderDelegateRecordAccountData>(
[
['key', getKeySerializer()],
['bump', u8()],
['mint', publicKeySerializer()],
['delegate', publicKeySerializer()],
['updateAuthority', publicKeySerializer()],
],
{ description: 'HolderDelegateRecordAccountData' }
) as Serializer<
HolderDelegateRecordAccountDataArgs,
HolderDelegateRecordAccountData
>;
}

export function deserializeHolderDelegateRecord(
rawAccount: RpcAccount
): HolderDelegateRecord {
return deserializeAccount(
rawAccount,
getHolderDelegateRecordAccountDataSerializer()
);
}

export async function fetchHolderDelegateRecord(
context: Pick<Context, 'rpc'>,
publicKey: PublicKey | Pda,
options?: RpcGetAccountOptions
): Promise<HolderDelegateRecord> {
const maybeAccount = await context.rpc.getAccount(
toPublicKey(publicKey, false),
options
);
assertAccountExists(maybeAccount, 'HolderDelegateRecord');
return deserializeHolderDelegateRecord(maybeAccount);
}

export async function safeFetchHolderDelegateRecord(
context: Pick<Context, 'rpc'>,
publicKey: PublicKey | Pda,
options?: RpcGetAccountOptions
): Promise<HolderDelegateRecord | null> {
const maybeAccount = await context.rpc.getAccount(
toPublicKey(publicKey, false),
options
);
return maybeAccount.exists
? deserializeHolderDelegateRecord(maybeAccount)
: null;
}

export async function fetchAllHolderDelegateRecord(
context: Pick<Context, 'rpc'>,
publicKeys: Array<PublicKey | Pda>,
options?: RpcGetAccountsOptions
): Promise<HolderDelegateRecord[]> {
const maybeAccounts = await context.rpc.getAccounts(
publicKeys.map((key) => toPublicKey(key, false)),
options
);
return maybeAccounts.map((maybeAccount) => {
assertAccountExists(maybeAccount, 'HolderDelegateRecord');
return deserializeHolderDelegateRecord(maybeAccount);
});
}

export async function safeFetchAllHolderDelegateRecord(
context: Pick<Context, 'rpc'>,
publicKeys: Array<PublicKey | Pda>,
options?: RpcGetAccountsOptions
): Promise<HolderDelegateRecord[]> {
const maybeAccounts = await context.rpc.getAccounts(
publicKeys.map((key) => toPublicKey(key, false)),
options
);
return maybeAccounts
.filter((maybeAccount) => maybeAccount.exists)
.map((maybeAccount) =>
deserializeHolderDelegateRecord(maybeAccount as RpcAccount)
);
}

export function getHolderDelegateRecordGpaBuilder(
context: Pick<Context, 'rpc' | 'programs'>
) {
const programId = context.programs.getPublicKey(
'mplTokenMetadata',
'metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s'
);
return gpaBuilder(context, programId)
.registerFields<{
key: KeyArgs;
bump: number;
mint: PublicKey;
delegate: PublicKey;
updateAuthority: PublicKey;
}>({
key: [0, getKeySerializer()],
bump: [1, u8()],
mint: [2, publicKeySerializer()],
delegate: [34, publicKeySerializer()],
updateAuthority: [66, publicKeySerializer()],
})
.deserializeUsing<HolderDelegateRecord>((account) =>
deserializeHolderDelegateRecord(account)
);
}

export function getHolderDelegateRecordSize(): number {
return 98;
}

export function findHolderDelegateRecordPda(
context: Pick<Context, 'eddsa' | 'programs'>,
seeds: {
/** The address of the mint account */
mint: PublicKey;
/** The role of the holder delegate */
delegateRole: HolderDelegateRoleSeedArgs;
/** The address of the owner of the token */
owner: PublicKey;
/** The address of the delegate authority */
delegate: PublicKey;
}
): Pda {
const programId = context.programs.getPublicKey(
'mplTokenMetadata',
'metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s'
);
return context.eddsa.findPda(programId, [
string({ size: 'variable' }).serialize('metadata'),
publicKeySerializer().serialize(programId),
publicKeySerializer().serialize(seeds.mint),
getHolderDelegateRoleSeedSerializer().serialize(seeds.delegateRole),
publicKeySerializer().serialize(seeds.owner),
publicKeySerializer().serialize(seeds.delegate),
]);
}

export async function fetchHolderDelegateRecordFromSeeds(
context: Pick<Context, 'eddsa' | 'programs' | 'rpc'>,
seeds: Parameters<typeof findHolderDelegateRecordPda>[1],
options?: RpcGetAccountOptions
): Promise<HolderDelegateRecord> {
return fetchHolderDelegateRecord(
context,
findHolderDelegateRecordPda(context, seeds),
options
);
}

export async function safeFetchHolderDelegateRecordFromSeeds(
context: Pick<Context, 'eddsa' | 'programs' | 'rpc'>,
seeds: Parameters<typeof findHolderDelegateRecordPda>[1],
options?: RpcGetAccountOptions
): Promise<HolderDelegateRecord | null> {
return safeFetchHolderDelegateRecord(
context,
findHolderDelegateRecordPda(context, seeds),
options
);
}
1 change: 1 addition & 0 deletions clients/js/src/generated/accounts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export * from './deprecatedMasterEditionV1';
export * from './edition';
export * from './editionMarker';
export * from './editionMarkerV2';
export * from './holderDelegateRecord';
export * from './masterEdition';
export * from './metadata';
export * from './metadataDelegateRecord';
Expand Down
Loading

0 comments on commit cb043fe

Please sign in to comment.