-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Define IDL instructions for default account state extension (#9)
* Add instructions * Add tests
- Loading branch information
1 parent
99b7dfd
commit 91ffbcb
Showing
8 changed files
with
805 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
195 changes: 195 additions & 0 deletions
195
clients/js/src/generated/instructions/initializeDefaultAccountState.ts
This file contains 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,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 | ||
), | ||
}; | ||
} |
Oops, something went wrong.