Skip to content

Commit

Permalink
Define IDL instructions for default account state extension (#9)
Browse files Browse the repository at this point in the history
* Add instructions

* Add tests
  • Loading branch information
lorisleiva authored Oct 8, 2024
1 parent 99b7dfd commit 91ffbcb
Show file tree
Hide file tree
Showing 8 changed files with 805 additions and 1 deletion.
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 @@ -33,6 +33,7 @@ export * from './initializeAccount';
export * from './initializeAccount2';
export * from './initializeAccount3';
export * from './initializeConfidentialTransferMint';
export * from './initializeDefaultAccountState';
export * from './initializeImmutableOwner';
export * from './initializeMint';
export * from './initializeMint2';
Expand All @@ -53,5 +54,6 @@ export * from './transferChecked';
export * from './transferCheckedWithFee';
export * from './uiAmountToAmount';
export * from './updateConfidentialTransferMint';
export * from './updateDefaultAccountState';
export * from './withdrawWithheldTokensFromAccounts';
export * from './withdrawWithheldTokensFromMint';
195 changes: 195 additions & 0 deletions clients/js/src/generated/instructions/initializeDefaultAccountState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
/**
* 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/kinobi-so/kinobi
*/

import {
combineCodec,
getStructDecoder,
getStructEncoder,
getU8Decoder,
getU8Encoder,
transformEncoder,
type Address,
type Codec,
type Decoder,
type Encoder,
type IAccountMeta,
type IInstruction,
type IInstructionWithAccounts,
type IInstructionWithData,
type WritableAccount,
} from '@solana/web3.js';
import { TOKEN_2022_PROGRAM_ADDRESS } from '../programs';
import { getAccountMetaFactory, type ResolvedAccount } from '../shared';
import {
getAccountStateDecoder,
getAccountStateEncoder,
type AccountState,
type AccountStateArgs,
} from '../types';

export const INITIALIZE_DEFAULT_ACCOUNT_STATE_DISCRIMINATOR = 28;

export function getInitializeDefaultAccountStateDiscriminatorBytes() {
return getU8Encoder().encode(INITIALIZE_DEFAULT_ACCOUNT_STATE_DISCRIMINATOR);
}

export const INITIALIZE_DEFAULT_ACCOUNT_STATE_DEFAULT_ACCOUNT_STATE_DISCRIMINATOR = 0;

export function getInitializeDefaultAccountStateDefaultAccountStateDiscriminatorBytes() {
return getU8Encoder().encode(
INITIALIZE_DEFAULT_ACCOUNT_STATE_DEFAULT_ACCOUNT_STATE_DISCRIMINATOR
);
}

export type InitializeDefaultAccountStateInstruction<
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 InitializeDefaultAccountStateInstructionData = {
discriminator: number;
defaultAccountStateDiscriminator: number;
/** The state each new token account should start with. */
state: AccountState;
};

export type InitializeDefaultAccountStateInstructionDataArgs = {
/** The state each new token account should start with. */
state: AccountStateArgs;
};

export function getInitializeDefaultAccountStateInstructionDataEncoder(): Encoder<InitializeDefaultAccountStateInstructionDataArgs> {
return transformEncoder(
getStructEncoder([
['discriminator', getU8Encoder()],
['defaultAccountStateDiscriminator', getU8Encoder()],
['state', getAccountStateEncoder()],
]),
(value) => ({
...value,
discriminator: INITIALIZE_DEFAULT_ACCOUNT_STATE_DISCRIMINATOR,
defaultAccountStateDiscriminator:
INITIALIZE_DEFAULT_ACCOUNT_STATE_DEFAULT_ACCOUNT_STATE_DISCRIMINATOR,
})
);
}

export function getInitializeDefaultAccountStateInstructionDataDecoder(): Decoder<InitializeDefaultAccountStateInstructionData> {
return getStructDecoder([
['discriminator', getU8Decoder()],
['defaultAccountStateDiscriminator', getU8Decoder()],
['state', getAccountStateDecoder()],
]);
}

export function getInitializeDefaultAccountStateInstructionDataCodec(): Codec<
InitializeDefaultAccountStateInstructionDataArgs,
InitializeDefaultAccountStateInstructionData
> {
return combineCodec(
getInitializeDefaultAccountStateInstructionDataEncoder(),
getInitializeDefaultAccountStateInstructionDataDecoder()
);
}

export type InitializeDefaultAccountStateInput<
TAccountMint extends string = string,
> = {
/** The mint. */
mint: Address<TAccountMint>;
state: InitializeDefaultAccountStateInstructionDataArgs['state'];
};

export function getInitializeDefaultAccountStateInstruction<
TAccountMint extends string,
>(
input: InitializeDefaultAccountStateInput<TAccountMint>
): InitializeDefaultAccountStateInstruction<
typeof TOKEN_2022_PROGRAM_ADDRESS,
TAccountMint
> {
// Program address.
const 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: getInitializeDefaultAccountStateInstructionDataEncoder().encode(
args as InitializeDefaultAccountStateInstructionDataArgs
),
} as InitializeDefaultAccountStateInstruction<
typeof TOKEN_2022_PROGRAM_ADDRESS,
TAccountMint
>;

return instruction;
}

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

export function parseInitializeDefaultAccountStateInstruction<
TProgram extends string,
TAccountMetas extends readonly IAccountMeta[],
>(
instruction: IInstruction<TProgram> &
IInstructionWithAccounts<TAccountMetas> &
IInstructionWithData<Uint8Array>
): ParsedInitializeDefaultAccountStateInstruction<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: getInitializeDefaultAccountStateInstructionDataDecoder().decode(
instruction.data
),
};
}
Loading

0 comments on commit 91ffbcb

Please sign in to comment.