withdrawNonceAccount.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. getStructDecoder,
  11. getStructEncoder,
  12. getU32Decoder,
  13. getU32Encoder,
  14. getU64Decoder,
  15. getU64Encoder,
  16. transformEncoder,
  17. type AccountMeta,
  18. type AccountSignerMeta,
  19. type Address,
  20. type FixedSizeCodec,
  21. type FixedSizeDecoder,
  22. type FixedSizeEncoder,
  23. type Instruction,
  24. type InstructionWithAccounts,
  25. type InstructionWithData,
  26. type ReadonlyAccount,
  27. type ReadonlySignerAccount,
  28. type ReadonlyUint8Array,
  29. type TransactionSigner,
  30. type WritableAccount,
  31. } from '@solana/kit';
  32. import { SYSTEM_PROGRAM_ADDRESS } from '../programs';
  33. import { getAccountMetaFactory, type ResolvedAccount } from '../shared';
  34. export const WITHDRAW_NONCE_ACCOUNT_DISCRIMINATOR = 5;
  35. export function getWithdrawNonceAccountDiscriminatorBytes() {
  36. return getU32Encoder().encode(WITHDRAW_NONCE_ACCOUNT_DISCRIMINATOR);
  37. }
  38. export type WithdrawNonceAccountInstruction<
  39. TProgram extends string = typeof SYSTEM_PROGRAM_ADDRESS,
  40. TAccountNonceAccount extends string | AccountMeta<string> = string,
  41. TAccountRecipientAccount extends string | AccountMeta<string> = string,
  42. TAccountRecentBlockhashesSysvar extends
  43. | string
  44. | AccountMeta<string> = 'SysvarRecentB1ockHashes11111111111111111111',
  45. TAccountRentSysvar extends
  46. | string
  47. | AccountMeta<string> = 'SysvarRent111111111111111111111111111111111',
  48. TAccountNonceAuthority extends string | AccountMeta<string> = string,
  49. TRemainingAccounts extends readonly AccountMeta<string>[] = [],
  50. > = Instruction<TProgram> &
  51. InstructionWithData<ReadonlyUint8Array> &
  52. InstructionWithAccounts<
  53. [
  54. TAccountNonceAccount extends string
  55. ? WritableAccount<TAccountNonceAccount>
  56. : TAccountNonceAccount,
  57. TAccountRecipientAccount extends string
  58. ? WritableAccount<TAccountRecipientAccount>
  59. : TAccountRecipientAccount,
  60. TAccountRecentBlockhashesSysvar extends string
  61. ? ReadonlyAccount<TAccountRecentBlockhashesSysvar>
  62. : TAccountRecentBlockhashesSysvar,
  63. TAccountRentSysvar extends string
  64. ? ReadonlyAccount<TAccountRentSysvar>
  65. : TAccountRentSysvar,
  66. TAccountNonceAuthority extends string
  67. ? ReadonlySignerAccount<TAccountNonceAuthority> &
  68. AccountSignerMeta<TAccountNonceAuthority>
  69. : TAccountNonceAuthority,
  70. ...TRemainingAccounts,
  71. ]
  72. >;
  73. export type WithdrawNonceAccountInstructionData = {
  74. discriminator: number;
  75. withdrawAmount: bigint;
  76. };
  77. export type WithdrawNonceAccountInstructionDataArgs = {
  78. withdrawAmount: number | bigint;
  79. };
  80. export function getWithdrawNonceAccountInstructionDataEncoder(): FixedSizeEncoder<WithdrawNonceAccountInstructionDataArgs> {
  81. return transformEncoder(
  82. getStructEncoder([
  83. ['discriminator', getU32Encoder()],
  84. ['withdrawAmount', getU64Encoder()],
  85. ]),
  86. (value) => ({
  87. ...value,
  88. discriminator: WITHDRAW_NONCE_ACCOUNT_DISCRIMINATOR,
  89. })
  90. );
  91. }
  92. export function getWithdrawNonceAccountInstructionDataDecoder(): FixedSizeDecoder<WithdrawNonceAccountInstructionData> {
  93. return getStructDecoder([
  94. ['discriminator', getU32Decoder()],
  95. ['withdrawAmount', getU64Decoder()],
  96. ]);
  97. }
  98. export function getWithdrawNonceAccountInstructionDataCodec(): FixedSizeCodec<
  99. WithdrawNonceAccountInstructionDataArgs,
  100. WithdrawNonceAccountInstructionData
  101. > {
  102. return combineCodec(
  103. getWithdrawNonceAccountInstructionDataEncoder(),
  104. getWithdrawNonceAccountInstructionDataDecoder()
  105. );
  106. }
  107. export type WithdrawNonceAccountInput<
  108. TAccountNonceAccount extends string = string,
  109. TAccountRecipientAccount extends string = string,
  110. TAccountRecentBlockhashesSysvar extends string = string,
  111. TAccountRentSysvar extends string = string,
  112. TAccountNonceAuthority extends string = string,
  113. > = {
  114. nonceAccount: Address<TAccountNonceAccount>;
  115. recipientAccount: Address<TAccountRecipientAccount>;
  116. recentBlockhashesSysvar?: Address<TAccountRecentBlockhashesSysvar>;
  117. rentSysvar?: Address<TAccountRentSysvar>;
  118. nonceAuthority: TransactionSigner<TAccountNonceAuthority>;
  119. withdrawAmount: WithdrawNonceAccountInstructionDataArgs['withdrawAmount'];
  120. };
  121. export function getWithdrawNonceAccountInstruction<
  122. TAccountNonceAccount extends string,
  123. TAccountRecipientAccount extends string,
  124. TAccountRecentBlockhashesSysvar extends string,
  125. TAccountRentSysvar extends string,
  126. TAccountNonceAuthority extends string,
  127. TProgramAddress extends Address = typeof SYSTEM_PROGRAM_ADDRESS,
  128. >(
  129. input: WithdrawNonceAccountInput<
  130. TAccountNonceAccount,
  131. TAccountRecipientAccount,
  132. TAccountRecentBlockhashesSysvar,
  133. TAccountRentSysvar,
  134. TAccountNonceAuthority
  135. >,
  136. config?: { programAddress?: TProgramAddress }
  137. ): WithdrawNonceAccountInstruction<
  138. TProgramAddress,
  139. TAccountNonceAccount,
  140. TAccountRecipientAccount,
  141. TAccountRecentBlockhashesSysvar,
  142. TAccountRentSysvar,
  143. TAccountNonceAuthority
  144. > {
  145. // Program address.
  146. const programAddress = config?.programAddress ?? SYSTEM_PROGRAM_ADDRESS;
  147. // Original accounts.
  148. const originalAccounts = {
  149. nonceAccount: { value: input.nonceAccount ?? null, isWritable: true },
  150. recipientAccount: {
  151. value: input.recipientAccount ?? null,
  152. isWritable: true,
  153. },
  154. recentBlockhashesSysvar: {
  155. value: input.recentBlockhashesSysvar ?? null,
  156. isWritable: false,
  157. },
  158. rentSysvar: { value: input.rentSysvar ?? null, isWritable: false },
  159. nonceAuthority: { value: input.nonceAuthority ?? null, isWritable: false },
  160. };
  161. const accounts = originalAccounts as Record<
  162. keyof typeof originalAccounts,
  163. ResolvedAccount
  164. >;
  165. // Original args.
  166. const args = { ...input };
  167. // Resolve default values.
  168. if (!accounts.recentBlockhashesSysvar.value) {
  169. accounts.recentBlockhashesSysvar.value =
  170. 'SysvarRecentB1ockHashes11111111111111111111' as Address<'SysvarRecentB1ockHashes11111111111111111111'>;
  171. }
  172. if (!accounts.rentSysvar.value) {
  173. accounts.rentSysvar.value =
  174. 'SysvarRent111111111111111111111111111111111' as Address<'SysvarRent111111111111111111111111111111111'>;
  175. }
  176. const getAccountMeta = getAccountMetaFactory(programAddress, 'omitted');
  177. return Object.freeze({
  178. accounts: [
  179. getAccountMeta(accounts.nonceAccount),
  180. getAccountMeta(accounts.recipientAccount),
  181. getAccountMeta(accounts.recentBlockhashesSysvar),
  182. getAccountMeta(accounts.rentSysvar),
  183. getAccountMeta(accounts.nonceAuthority),
  184. ],
  185. data: getWithdrawNonceAccountInstructionDataEncoder().encode(
  186. args as WithdrawNonceAccountInstructionDataArgs
  187. ),
  188. programAddress,
  189. } as WithdrawNonceAccountInstruction<
  190. TProgramAddress,
  191. TAccountNonceAccount,
  192. TAccountRecipientAccount,
  193. TAccountRecentBlockhashesSysvar,
  194. TAccountRentSysvar,
  195. TAccountNonceAuthority
  196. >);
  197. }
  198. export type ParsedWithdrawNonceAccountInstruction<
  199. TProgram extends string = typeof SYSTEM_PROGRAM_ADDRESS,
  200. TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
  201. > = {
  202. programAddress: Address<TProgram>;
  203. accounts: {
  204. nonceAccount: TAccountMetas[0];
  205. recipientAccount: TAccountMetas[1];
  206. recentBlockhashesSysvar: TAccountMetas[2];
  207. rentSysvar: TAccountMetas[3];
  208. nonceAuthority: TAccountMetas[4];
  209. };
  210. data: WithdrawNonceAccountInstructionData;
  211. };
  212. export function parseWithdrawNonceAccountInstruction<
  213. TProgram extends string,
  214. TAccountMetas extends readonly AccountMeta[],
  215. >(
  216. instruction: Instruction<TProgram> &
  217. InstructionWithAccounts<TAccountMetas> &
  218. InstructionWithData<ReadonlyUint8Array>
  219. ): ParsedWithdrawNonceAccountInstruction<TProgram, TAccountMetas> {
  220. if (instruction.accounts.length < 5) {
  221. // TODO: Coded error.
  222. throw new Error('Not enough accounts');
  223. }
  224. let accountIndex = 0;
  225. const getNextAccount = () => {
  226. const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!;
  227. accountIndex += 1;
  228. return accountMeta;
  229. };
  230. return {
  231. programAddress: instruction.programAddress,
  232. accounts: {
  233. nonceAccount: getNextAccount(),
  234. recipientAccount: getNextAccount(),
  235. recentBlockhashesSysvar: getNextAccount(),
  236. rentSysvar: getNextAccount(),
  237. nonceAuthority: getNextAccount(),
  238. },
  239. data: getWithdrawNonceAccountInstructionDataDecoder().decode(
  240. instruction.data
  241. ),
  242. };
  243. }