closeAccount.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * This code was AUTOGENERATED using the kinobi library.
  3. * Please DO NOT EDIT THIS FILE, instead use visitors
  4. * to add features, then rerun kinobi to update it.
  5. *
  6. * @see https://github.com/metaplex-foundation/kinobi
  7. */
  8. import {
  9. AccountRole,
  10. Address,
  11. Codec,
  12. Decoder,
  13. Encoder,
  14. IAccountMeta,
  15. IAccountSignerMeta,
  16. IInstruction,
  17. IInstructionWithAccounts,
  18. IInstructionWithData,
  19. ReadonlyAccount,
  20. ReadonlySignerAccount,
  21. TransactionSigner,
  22. WritableAccount,
  23. combineCodec,
  24. getStructDecoder,
  25. getStructEncoder,
  26. getU8Decoder,
  27. getU8Encoder,
  28. transformEncoder,
  29. } from '@solana/web3.js';
  30. import { TOKEN_PROGRAM_ADDRESS } from '../programs';
  31. import { ResolvedAccount, getAccountMetaFactory } from '../shared';
  32. export type CloseAccountInstruction<
  33. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  34. TAccountAccount extends string | IAccountMeta<string> = string,
  35. TAccountDestination extends string | IAccountMeta<string> = string,
  36. TAccountOwner extends string | IAccountMeta<string> = string,
  37. TRemainingAccounts extends readonly IAccountMeta<string>[] = [],
  38. > = IInstruction<TProgram> &
  39. IInstructionWithData<Uint8Array> &
  40. IInstructionWithAccounts<
  41. [
  42. TAccountAccount extends string
  43. ? WritableAccount<TAccountAccount>
  44. : TAccountAccount,
  45. TAccountDestination extends string
  46. ? WritableAccount<TAccountDestination>
  47. : TAccountDestination,
  48. TAccountOwner extends string
  49. ? ReadonlyAccount<TAccountOwner>
  50. : TAccountOwner,
  51. ...TRemainingAccounts,
  52. ]
  53. >;
  54. export type CloseAccountInstructionData = { discriminator: number };
  55. export type CloseAccountInstructionDataArgs = {};
  56. export function getCloseAccountInstructionDataEncoder(): Encoder<CloseAccountInstructionDataArgs> {
  57. return transformEncoder(
  58. getStructEncoder([['discriminator', getU8Encoder()]]),
  59. (value) => ({ ...value, discriminator: 9 })
  60. );
  61. }
  62. export function getCloseAccountInstructionDataDecoder(): Decoder<CloseAccountInstructionData> {
  63. return getStructDecoder([['discriminator', getU8Decoder()]]);
  64. }
  65. export function getCloseAccountInstructionDataCodec(): Codec<
  66. CloseAccountInstructionDataArgs,
  67. CloseAccountInstructionData
  68. > {
  69. return combineCodec(
  70. getCloseAccountInstructionDataEncoder(),
  71. getCloseAccountInstructionDataDecoder()
  72. );
  73. }
  74. export type CloseAccountInput<
  75. TAccountAccount extends string = string,
  76. TAccountDestination extends string = string,
  77. TAccountOwner extends string = string,
  78. > = {
  79. /** The account to close. */
  80. account: Address<TAccountAccount>;
  81. /** The destination account. */
  82. destination: Address<TAccountDestination>;
  83. /** The account's owner or its multisignature account. */
  84. owner: Address<TAccountOwner> | TransactionSigner<TAccountOwner>;
  85. multiSigners?: Array<TransactionSigner>;
  86. };
  87. export function getCloseAccountInstruction<
  88. TAccountAccount extends string,
  89. TAccountDestination extends string,
  90. TAccountOwner extends string,
  91. >(
  92. input: CloseAccountInput<TAccountAccount, TAccountDestination, TAccountOwner>
  93. ): CloseAccountInstruction<
  94. typeof TOKEN_PROGRAM_ADDRESS,
  95. TAccountAccount,
  96. TAccountDestination,
  97. (typeof input)['owner'] extends TransactionSigner<TAccountOwner>
  98. ? ReadonlySignerAccount<TAccountOwner> & IAccountSignerMeta<TAccountOwner>
  99. : TAccountOwner
  100. > {
  101. // Program address.
  102. const programAddress = TOKEN_PROGRAM_ADDRESS;
  103. // Original accounts.
  104. const originalAccounts = {
  105. account: { value: input.account ?? null, isWritable: true },
  106. destination: { value: input.destination ?? null, isWritable: true },
  107. owner: { value: input.owner ?? null, isWritable: false },
  108. };
  109. const accounts = originalAccounts as Record<
  110. keyof typeof originalAccounts,
  111. ResolvedAccount
  112. >;
  113. // Remaining accounts.
  114. const remainingAccounts: IAccountMeta[] = (args.multiSigners ?? []).map(
  115. (signer) => ({
  116. address: signer.address,
  117. role: AccountRole.READONLY_SIGNER,
  118. signer,
  119. })
  120. );
  121. const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  122. const instruction = {
  123. accounts: [
  124. getAccountMeta(accounts.account),
  125. getAccountMeta(accounts.destination),
  126. getAccountMeta(accounts.owner),
  127. ...remainingAccounts,
  128. ],
  129. programAddress,
  130. data: getCloseAccountInstructionDataEncoder().encode({}),
  131. } as CloseAccountInstruction<
  132. typeof TOKEN_PROGRAM_ADDRESS,
  133. TAccountAccount,
  134. TAccountDestination,
  135. (typeof input)['owner'] extends TransactionSigner<TAccountOwner>
  136. ? ReadonlySignerAccount<TAccountOwner> & IAccountSignerMeta<TAccountOwner>
  137. : TAccountOwner
  138. >;
  139. return instruction;
  140. }
  141. export type ParsedCloseAccountInstruction<
  142. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  143. TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
  144. > = {
  145. programAddress: Address<TProgram>;
  146. accounts: {
  147. /** The account to close. */
  148. account: TAccountMetas[0];
  149. /** The destination account. */
  150. destination: TAccountMetas[1];
  151. /** The account's owner or its multisignature account. */
  152. owner: TAccountMetas[2];
  153. };
  154. data: CloseAccountInstructionData;
  155. };
  156. export function parseCloseAccountInstruction<
  157. TProgram extends string,
  158. TAccountMetas extends readonly IAccountMeta[],
  159. >(
  160. instruction: IInstruction<TProgram> &
  161. IInstructionWithAccounts<TAccountMetas> &
  162. IInstructionWithData<Uint8Array>
  163. ): ParsedCloseAccountInstruction<TProgram, TAccountMetas> {
  164. if (instruction.accounts.length < 3) {
  165. // TODO: Coded error.
  166. throw new Error('Not enough accounts');
  167. }
  168. let accountIndex = 0;
  169. const getNextAccount = () => {
  170. const accountMeta = instruction.accounts![accountIndex]!;
  171. accountIndex += 1;
  172. return accountMeta;
  173. };
  174. return {
  175. programAddress: instruction.programAddress,
  176. accounts: {
  177. account: getNextAccount(),
  178. destination: getNextAccount(),
  179. owner: getNextAccount(),
  180. },
  181. data: getCloseAccountInstructionDataDecoder().decode(instruction.data),
  182. };
  183. }