closeAccount.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. AccountRole,
  10. combineCodec,
  11. getStructDecoder,
  12. getStructEncoder,
  13. getU8Decoder,
  14. getU8Encoder,
  15. transformEncoder,
  16. type AccountMeta,
  17. type AccountSignerMeta,
  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 ReadonlySignerAccount,
  27. type ReadonlyUint8Array,
  28. type TransactionSigner,
  29. type WritableAccount,
  30. } from '@solana/kit';
  31. import { TOKEN_PROGRAM_ADDRESS } from '../programs';
  32. import { getAccountMetaFactory, type ResolvedAccount } from '../shared';
  33. export const CLOSE_ACCOUNT_DISCRIMINATOR = 9;
  34. export function getCloseAccountDiscriminatorBytes() {
  35. return getU8Encoder().encode(CLOSE_ACCOUNT_DISCRIMINATOR);
  36. }
  37. export type CloseAccountInstruction<
  38. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  39. TAccountAccount extends string | AccountMeta<string> = string,
  40. TAccountDestination extends string | AccountMeta<string> = string,
  41. TAccountOwner extends string | AccountMeta<string> = string,
  42. TRemainingAccounts extends readonly AccountMeta<string>[] = [],
  43. > = Instruction<TProgram> &
  44. InstructionWithData<ReadonlyUint8Array> &
  45. InstructionWithAccounts<
  46. [
  47. TAccountAccount extends string
  48. ? WritableAccount<TAccountAccount>
  49. : TAccountAccount,
  50. TAccountDestination extends string
  51. ? WritableAccount<TAccountDestination>
  52. : TAccountDestination,
  53. TAccountOwner extends string
  54. ? ReadonlyAccount<TAccountOwner>
  55. : TAccountOwner,
  56. ...TRemainingAccounts,
  57. ]
  58. >;
  59. export type CloseAccountInstructionData = { discriminator: number };
  60. export type CloseAccountInstructionDataArgs = {};
  61. export function getCloseAccountInstructionDataEncoder(): FixedSizeEncoder<CloseAccountInstructionDataArgs> {
  62. return transformEncoder(
  63. getStructEncoder([['discriminator', getU8Encoder()]]),
  64. (value) => ({ ...value, discriminator: CLOSE_ACCOUNT_DISCRIMINATOR })
  65. );
  66. }
  67. export function getCloseAccountInstructionDataDecoder(): FixedSizeDecoder<CloseAccountInstructionData> {
  68. return getStructDecoder([['discriminator', getU8Decoder()]]);
  69. }
  70. export function getCloseAccountInstructionDataCodec(): FixedSizeCodec<
  71. CloseAccountInstructionDataArgs,
  72. CloseAccountInstructionData
  73. > {
  74. return combineCodec(
  75. getCloseAccountInstructionDataEncoder(),
  76. getCloseAccountInstructionDataDecoder()
  77. );
  78. }
  79. export type CloseAccountInput<
  80. TAccountAccount extends string = string,
  81. TAccountDestination extends string = string,
  82. TAccountOwner extends string = string,
  83. > = {
  84. /** The account to close. */
  85. account: Address<TAccountAccount>;
  86. /** The destination account. */
  87. destination: Address<TAccountDestination>;
  88. /** The account's owner or its multisignature account. */
  89. owner: Address<TAccountOwner> | TransactionSigner<TAccountOwner>;
  90. multiSigners?: Array<TransactionSigner>;
  91. };
  92. export function getCloseAccountInstruction<
  93. TAccountAccount extends string,
  94. TAccountDestination extends string,
  95. TAccountOwner extends string,
  96. TProgramAddress extends Address = typeof TOKEN_PROGRAM_ADDRESS,
  97. >(
  98. input: CloseAccountInput<TAccountAccount, TAccountDestination, TAccountOwner>,
  99. config?: { programAddress?: TProgramAddress }
  100. ): CloseAccountInstruction<
  101. TProgramAddress,
  102. TAccountAccount,
  103. TAccountDestination,
  104. (typeof input)['owner'] extends TransactionSigner<TAccountOwner>
  105. ? ReadonlySignerAccount<TAccountOwner> & AccountSignerMeta<TAccountOwner>
  106. : TAccountOwner
  107. > {
  108. // Program address.
  109. const programAddress = config?.programAddress ?? TOKEN_PROGRAM_ADDRESS;
  110. // Original accounts.
  111. const originalAccounts = {
  112. account: { value: input.account ?? null, isWritable: true },
  113. destination: { value: input.destination ?? null, isWritable: true },
  114. owner: { value: input.owner ?? null, isWritable: false },
  115. };
  116. const accounts = originalAccounts as Record<
  117. keyof typeof originalAccounts,
  118. ResolvedAccount
  119. >;
  120. // Original args.
  121. const args = { ...input };
  122. // Remaining accounts.
  123. const remainingAccounts: AccountMeta[] = (args.multiSigners ?? []).map(
  124. (signer) => ({
  125. address: signer.address,
  126. role: AccountRole.READONLY_SIGNER,
  127. signer,
  128. })
  129. );
  130. const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  131. return Object.freeze({
  132. accounts: [
  133. getAccountMeta(accounts.account),
  134. getAccountMeta(accounts.destination),
  135. getAccountMeta(accounts.owner),
  136. ...remainingAccounts,
  137. ],
  138. data: getCloseAccountInstructionDataEncoder().encode({}),
  139. programAddress,
  140. } as CloseAccountInstruction<
  141. TProgramAddress,
  142. TAccountAccount,
  143. TAccountDestination,
  144. (typeof input)['owner'] extends TransactionSigner<TAccountOwner>
  145. ? ReadonlySignerAccount<TAccountOwner> & AccountSignerMeta<TAccountOwner>
  146. : TAccountOwner
  147. >);
  148. }
  149. export type ParsedCloseAccountInstruction<
  150. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  151. TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
  152. > = {
  153. programAddress: Address<TProgram>;
  154. accounts: {
  155. /** The account to close. */
  156. account: TAccountMetas[0];
  157. /** The destination account. */
  158. destination: TAccountMetas[1];
  159. /** The account's owner or its multisignature account. */
  160. owner: TAccountMetas[2];
  161. };
  162. data: CloseAccountInstructionData;
  163. };
  164. export function parseCloseAccountInstruction<
  165. TProgram extends string,
  166. TAccountMetas extends readonly AccountMeta[],
  167. >(
  168. instruction: Instruction<TProgram> &
  169. InstructionWithAccounts<TAccountMetas> &
  170. InstructionWithData<ReadonlyUint8Array>
  171. ): ParsedCloseAccountInstruction<TProgram, TAccountMetas> {
  172. if (instruction.accounts.length < 3) {
  173. // TODO: Coded error.
  174. throw new Error('Not enough accounts');
  175. }
  176. let accountIndex = 0;
  177. const getNextAccount = () => {
  178. const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!;
  179. accountIndex += 1;
  180. return accountMeta;
  181. };
  182. return {
  183. programAddress: instruction.programAddress,
  184. accounts: {
  185. account: getNextAccount(),
  186. destination: getNextAccount(),
  187. owner: getNextAccount(),
  188. },
  189. data: getCloseAccountInstructionDataDecoder().decode(instruction.data),
  190. };
  191. }