Skip to content

Commit

Permalink
Define IDL instructions for group pointer extension (#17)
Browse files Browse the repository at this point in the history
* Fix docs on metadata pointer extension instruction

* Add instructions for group pointer extension

* Add tests
  • Loading branch information
lorisleiva authored Oct 10, 2024
1 parent 664c0c9 commit 7880932
Show file tree
Hide file tree
Showing 9 changed files with 824 additions and 4 deletions.
2 changes: 2 additions & 0 deletions clients/js/src/generated/instructions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export * from './initializeAccount2';
export * from './initializeAccount3';
export * from './initializeConfidentialTransferMint';
export * from './initializeDefaultAccountState';
export * from './initializeGroupPointer';
export * from './initializeImmutableOwner';
export * from './initializeMetadataPointer';
export * from './initializeMint';
Expand All @@ -59,6 +60,7 @@ export * from './transferCheckedWithFee';
export * from './uiAmountToAmount';
export * from './updateConfidentialTransferMint';
export * from './updateDefaultAccountState';
export * from './updateGroupPointer';
export * from './updateMetadataPointer';
export * from './withdrawWithheldTokensFromAccounts';
export * from './withdrawWithheldTokensFromMint';
221 changes: 221 additions & 0 deletions clients/js/src/generated/instructions/initializeGroupPointer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
/**
* This code was AUTOGENERATED using the codama library.
* Please DO NOT EDIT THIS FILE, instead use visitors
* to add features, then rerun codama to update it.
*
* @see https://github.com/codama-idl/codama
*/

import {
combineCodec,
getAddressDecoder,
getAddressEncoder,
getOptionDecoder,
getOptionEncoder,
getStructDecoder,
getStructEncoder,
getU8Decoder,
getU8Encoder,
transformEncoder,
type Address,
type Codec,
type Decoder,
type Encoder,
type IAccountMeta,
type IInstruction,
type IInstructionWithAccounts,
type IInstructionWithData,
type Option,
type OptionOrNullable,
type WritableAccount,
} from '@solana/web3.js';
import { TOKEN_2022_PROGRAM_ADDRESS } from '../programs';
import { getAccountMetaFactory, type ResolvedAccount } from '../shared';

export const INITIALIZE_GROUP_POINTER_DISCRIMINATOR = 40;

export function getInitializeGroupPointerDiscriminatorBytes() {
return getU8Encoder().encode(INITIALIZE_GROUP_POINTER_DISCRIMINATOR);
}

export const INITIALIZE_GROUP_POINTER_GROUP_POINTER_DISCRIMINATOR = 0;

export function getInitializeGroupPointerGroupPointerDiscriminatorBytes() {
return getU8Encoder().encode(
INITIALIZE_GROUP_POINTER_GROUP_POINTER_DISCRIMINATOR
);
}

export type InitializeGroupPointerInstruction<
TProgram extends string = typeof TOKEN_2022_PROGRAM_ADDRESS,
TAccountMint extends string | IAccountMeta<string> = string,
TRemainingAccounts extends readonly IAccountMeta<string>[] = [],
> = IInstruction<TProgram> &
IInstructionWithData<Uint8Array> &
IInstructionWithAccounts<
[
TAccountMint extends string
? WritableAccount<TAccountMint>
: TAccountMint,
...TRemainingAccounts,
]
>;

export type InitializeGroupPointerInstructionData = {
discriminator: number;
groupPointerDiscriminator: number;
/** The public key for the account that can update the group address. */
authority: Option<Address>;
/** The account address that holds the group. */
groupAddress: Option<Address>;
};

export type InitializeGroupPointerInstructionDataArgs = {
/** The public key for the account that can update the group address. */
authority: OptionOrNullable<Address>;
/** The account address that holds the group. */
groupAddress: OptionOrNullable<Address>;
};

export function getInitializeGroupPointerInstructionDataEncoder(): Encoder<InitializeGroupPointerInstructionDataArgs> {
return transformEncoder(
getStructEncoder([
['discriminator', getU8Encoder()],
['groupPointerDiscriminator', getU8Encoder()],
[
'authority',
getOptionEncoder(getAddressEncoder(), {
prefix: null,
noneValue: 'zeroes',
}),
],
[
'groupAddress',
getOptionEncoder(getAddressEncoder(), {
prefix: null,
noneValue: 'zeroes',
}),
],
]),
(value) => ({
...value,
discriminator: INITIALIZE_GROUP_POINTER_DISCRIMINATOR,
groupPointerDiscriminator:
INITIALIZE_GROUP_POINTER_GROUP_POINTER_DISCRIMINATOR,
})
);
}

export function getInitializeGroupPointerInstructionDataDecoder(): Decoder<InitializeGroupPointerInstructionData> {
return getStructDecoder([
['discriminator', getU8Decoder()],
['groupPointerDiscriminator', getU8Decoder()],
[
'authority',
getOptionDecoder(getAddressDecoder(), {
prefix: null,
noneValue: 'zeroes',
}),
],
[
'groupAddress',
getOptionDecoder(getAddressDecoder(), {
prefix: null,
noneValue: 'zeroes',
}),
],
]);
}

export function getInitializeGroupPointerInstructionDataCodec(): Codec<
InitializeGroupPointerInstructionDataArgs,
InitializeGroupPointerInstructionData
> {
return combineCodec(
getInitializeGroupPointerInstructionDataEncoder(),
getInitializeGroupPointerInstructionDataDecoder()
);
}

export type InitializeGroupPointerInput<TAccountMint extends string = string> =
{
/** The mint to initialize. */
mint: Address<TAccountMint>;
authority: InitializeGroupPointerInstructionDataArgs['authority'];
groupAddress: InitializeGroupPointerInstructionDataArgs['groupAddress'];
};

export function getInitializeGroupPointerInstruction<
TAccountMint extends string,
TProgramAddress extends Address = typeof TOKEN_2022_PROGRAM_ADDRESS,
>(
input: InitializeGroupPointerInput<TAccountMint>,
config?: { programAddress?: TProgramAddress }
): InitializeGroupPointerInstruction<TProgramAddress, TAccountMint> {
// Program address.
const programAddress = config?.programAddress ?? TOKEN_2022_PROGRAM_ADDRESS;

// Original accounts.
const originalAccounts = {
mint: { value: input.mint ?? null, isWritable: true },
};
const accounts = originalAccounts as Record<
keyof typeof originalAccounts,
ResolvedAccount
>;

// Original args.
const args = { ...input };

const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
const instruction = {
accounts: [getAccountMeta(accounts.mint)],
programAddress,
data: getInitializeGroupPointerInstructionDataEncoder().encode(
args as InitializeGroupPointerInstructionDataArgs
),
} as InitializeGroupPointerInstruction<TProgramAddress, TAccountMint>;

return instruction;
}

export type ParsedInitializeGroupPointerInstruction<
TProgram extends string = typeof TOKEN_2022_PROGRAM_ADDRESS,
TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
> = {
programAddress: Address<TProgram>;
accounts: {
/** The mint to initialize. */
mint: TAccountMetas[0];
};
data: InitializeGroupPointerInstructionData;
};

export function parseInitializeGroupPointerInstruction<
TProgram extends string,
TAccountMetas extends readonly IAccountMeta[],
>(
instruction: IInstruction<TProgram> &
IInstructionWithAccounts<TAccountMetas> &
IInstructionWithData<Uint8Array>
): ParsedInitializeGroupPointerInstruction<TProgram, TAccountMetas> {
if (instruction.accounts.length < 1) {
// TODO: Coded error.
throw new Error('Not enough accounts');
}
let accountIndex = 0;
const getNextAccount = () => {
const accountMeta = instruction.accounts![accountIndex]!;
accountIndex += 1;
return accountMeta;
};
return {
programAddress: instruction.programAddress,
accounts: {
mint: getNextAccount(),
},
data: getInitializeGroupPointerInstructionDataDecoder().decode(
instruction.data
),
};
}
Loading

0 comments on commit 7880932

Please sign in to comment.