initializeNonceAccount.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /**
  2. * This code was AUTOGENERATED using the Codama library.
  3. * Please DO NOT EDIT THIS FILE, instead use visitors
  4. * to add features, then rerun Codama to update it.
  5. *
  6. * @see https://github.com/codama-idl/codama
  7. */
  8. import {
  9. combineCodec,
  10. getAddressDecoder,
  11. getAddressEncoder,
  12. getStructDecoder,
  13. getStructEncoder,
  14. getU32Decoder,
  15. getU32Encoder,
  16. transformEncoder,
  17. type AccountMeta,
  18. type Address,
  19. type FixedSizeCodec,
  20. type FixedSizeDecoder,
  21. type FixedSizeEncoder,
  22. type Instruction,
  23. type InstructionWithAccounts,
  24. type InstructionWithData,
  25. type ReadonlyAccount,
  26. type ReadonlyUint8Array,
  27. type WritableAccount,
  28. } from '@solana/kit';
  29. import { SYSTEM_PROGRAM_ADDRESS } from '../programs';
  30. import { getAccountMetaFactory, type ResolvedAccount } from '../shared';
  31. export const INITIALIZE_NONCE_ACCOUNT_DISCRIMINATOR = 6;
  32. export function getInitializeNonceAccountDiscriminatorBytes() {
  33. return getU32Encoder().encode(INITIALIZE_NONCE_ACCOUNT_DISCRIMINATOR);
  34. }
  35. export type InitializeNonceAccountInstruction<
  36. TProgram extends string = typeof SYSTEM_PROGRAM_ADDRESS,
  37. TAccountNonceAccount extends string | AccountMeta<string> = string,
  38. TAccountRecentBlockhashesSysvar extends
  39. | string
  40. | AccountMeta<string> = 'SysvarRecentB1ockHashes11111111111111111111',
  41. TAccountRentSysvar extends
  42. | string
  43. | AccountMeta<string> = 'SysvarRent111111111111111111111111111111111',
  44. TRemainingAccounts extends readonly AccountMeta<string>[] = [],
  45. > = Instruction<TProgram> &
  46. InstructionWithData<ReadonlyUint8Array> &
  47. InstructionWithAccounts<
  48. [
  49. TAccountNonceAccount extends string
  50. ? WritableAccount<TAccountNonceAccount>
  51. : TAccountNonceAccount,
  52. TAccountRecentBlockhashesSysvar extends string
  53. ? ReadonlyAccount<TAccountRecentBlockhashesSysvar>
  54. : TAccountRecentBlockhashesSysvar,
  55. TAccountRentSysvar extends string
  56. ? ReadonlyAccount<TAccountRentSysvar>
  57. : TAccountRentSysvar,
  58. ...TRemainingAccounts,
  59. ]
  60. >;
  61. export type InitializeNonceAccountInstructionData = {
  62. discriminator: number;
  63. nonceAuthority: Address;
  64. };
  65. export type InitializeNonceAccountInstructionDataArgs = {
  66. nonceAuthority: Address;
  67. };
  68. export function getInitializeNonceAccountInstructionDataEncoder(): FixedSizeEncoder<InitializeNonceAccountInstructionDataArgs> {
  69. return transformEncoder(
  70. getStructEncoder([
  71. ['discriminator', getU32Encoder()],
  72. ['nonceAuthority', getAddressEncoder()],
  73. ]),
  74. (value) => ({
  75. ...value,
  76. discriminator: INITIALIZE_NONCE_ACCOUNT_DISCRIMINATOR,
  77. })
  78. );
  79. }
  80. export function getInitializeNonceAccountInstructionDataDecoder(): FixedSizeDecoder<InitializeNonceAccountInstructionData> {
  81. return getStructDecoder([
  82. ['discriminator', getU32Decoder()],
  83. ['nonceAuthority', getAddressDecoder()],
  84. ]);
  85. }
  86. export function getInitializeNonceAccountInstructionDataCodec(): FixedSizeCodec<
  87. InitializeNonceAccountInstructionDataArgs,
  88. InitializeNonceAccountInstructionData
  89. > {
  90. return combineCodec(
  91. getInitializeNonceAccountInstructionDataEncoder(),
  92. getInitializeNonceAccountInstructionDataDecoder()
  93. );
  94. }
  95. export type InitializeNonceAccountInput<
  96. TAccountNonceAccount extends string = string,
  97. TAccountRecentBlockhashesSysvar extends string = string,
  98. TAccountRentSysvar extends string = string,
  99. > = {
  100. nonceAccount: Address<TAccountNonceAccount>;
  101. recentBlockhashesSysvar?: Address<TAccountRecentBlockhashesSysvar>;
  102. rentSysvar?: Address<TAccountRentSysvar>;
  103. nonceAuthority: InitializeNonceAccountInstructionDataArgs['nonceAuthority'];
  104. };
  105. export function getInitializeNonceAccountInstruction<
  106. TAccountNonceAccount extends string,
  107. TAccountRecentBlockhashesSysvar extends string,
  108. TAccountRentSysvar extends string,
  109. TProgramAddress extends Address = typeof SYSTEM_PROGRAM_ADDRESS,
  110. >(
  111. input: InitializeNonceAccountInput<
  112. TAccountNonceAccount,
  113. TAccountRecentBlockhashesSysvar,
  114. TAccountRentSysvar
  115. >,
  116. config?: { programAddress?: TProgramAddress }
  117. ): InitializeNonceAccountInstruction<
  118. TProgramAddress,
  119. TAccountNonceAccount,
  120. TAccountRecentBlockhashesSysvar,
  121. TAccountRentSysvar
  122. > {
  123. // Program address.
  124. const programAddress = config?.programAddress ?? SYSTEM_PROGRAM_ADDRESS;
  125. // Original accounts.
  126. const originalAccounts = {
  127. nonceAccount: { value: input.nonceAccount ?? null, isWritable: true },
  128. recentBlockhashesSysvar: {
  129. value: input.recentBlockhashesSysvar ?? null,
  130. isWritable: false,
  131. },
  132. rentSysvar: { value: input.rentSysvar ?? null, isWritable: false },
  133. };
  134. const accounts = originalAccounts as Record<
  135. keyof typeof originalAccounts,
  136. ResolvedAccount
  137. >;
  138. // Original args.
  139. const args = { ...input };
  140. // Resolve default values.
  141. if (!accounts.recentBlockhashesSysvar.value) {
  142. accounts.recentBlockhashesSysvar.value =
  143. 'SysvarRecentB1ockHashes11111111111111111111' as Address<'SysvarRecentB1ockHashes11111111111111111111'>;
  144. }
  145. if (!accounts.rentSysvar.value) {
  146. accounts.rentSysvar.value =
  147. 'SysvarRent111111111111111111111111111111111' as Address<'SysvarRent111111111111111111111111111111111'>;
  148. }
  149. const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  150. return Object.freeze({
  151. accounts: [
  152. getAccountMeta(accounts.nonceAccount),
  153. getAccountMeta(accounts.recentBlockhashesSysvar),
  154. getAccountMeta(accounts.rentSysvar),
  155. ],
  156. data: getInitializeNonceAccountInstructionDataEncoder().encode(
  157. args as InitializeNonceAccountInstructionDataArgs
  158. ),
  159. programAddress,
  160. } as InitializeNonceAccountInstruction<
  161. TProgramAddress,
  162. TAccountNonceAccount,
  163. TAccountRecentBlockhashesSysvar,
  164. TAccountRentSysvar
  165. >);
  166. }
  167. export type ParsedInitializeNonceAccountInstruction<
  168. TProgram extends string = typeof SYSTEM_PROGRAM_ADDRESS,
  169. TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
  170. > = {
  171. programAddress: Address<TProgram>;
  172. accounts: {
  173. nonceAccount: TAccountMetas[0];
  174. recentBlockhashesSysvar: TAccountMetas[1];
  175. rentSysvar: TAccountMetas[2];
  176. };
  177. data: InitializeNonceAccountInstructionData;
  178. };
  179. export function parseInitializeNonceAccountInstruction<
  180. TProgram extends string,
  181. TAccountMetas extends readonly AccountMeta[],
  182. >(
  183. instruction: Instruction<TProgram> &
  184. InstructionWithAccounts<TAccountMetas> &
  185. InstructionWithData<ReadonlyUint8Array>
  186. ): ParsedInitializeNonceAccountInstruction<TProgram, TAccountMetas> {
  187. if (instruction.accounts.length < 3) {
  188. // TODO: Coded error.
  189. throw new Error('Not enough accounts');
  190. }
  191. let accountIndex = 0;
  192. const getNextAccount = () => {
  193. const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!;
  194. accountIndex += 1;
  195. return accountMeta;
  196. };
  197. return {
  198. programAddress: instruction.programAddress,
  199. accounts: {
  200. nonceAccount: getNextAccount(),
  201. recentBlockhashesSysvar: getNextAccount(),
  202. rentSysvar: getNextAccount(),
  203. },
  204. data: getInitializeNonceAccountInstructionDataDecoder().decode(
  205. instruction.data
  206. ),
  207. };
  208. }