closeAccount.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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/kinobi-so/kinobi
  7. */
  8. import {
  9. AccountRole,
  10. combineCodec,
  11. getStructDecoder,
  12. getStructEncoder,
  13. getU8Decoder,
  14. getU8Encoder,
  15. transformEncoder,
  16. type Address,
  17. type Codec,
  18. type Decoder,
  19. type Encoder,
  20. type IAccountMeta,
  21. type IAccountSignerMeta,
  22. type IInstruction,
  23. type IInstructionWithAccounts,
  24. type IInstructionWithData,
  25. type ReadonlyAccount,
  26. type ReadonlySignerAccount,
  27. type TransactionSigner,
  28. type WritableAccount,
  29. } from '@solana/web3.js';
  30. import { TOKEN_PROGRAM_ADDRESS } from '../programs';
  31. import { getAccountMetaFactory, type ResolvedAccount } from '../shared';
  32. export const CLOSE_ACCOUNT_DISCRIMINATOR = 9;
  33. export function getCloseAccountDiscriminatorBytes() {
  34. return getU8Encoder().encode(CLOSE_ACCOUNT_DISCRIMINATOR);
  35. }
  36. export type CloseAccountInstruction<
  37. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  38. TAccountAccount extends string | IAccountMeta<string> = string,
  39. TAccountDestination extends string | IAccountMeta<string> = string,
  40. TAccountOwner extends string | IAccountMeta<string> = string,
  41. TRemainingAccounts extends readonly IAccountMeta<string>[] = [],
  42. > = IInstruction<TProgram> &
  43. IInstructionWithData<Uint8Array> &
  44. IInstructionWithAccounts<
  45. [
  46. TAccountAccount extends string
  47. ? WritableAccount<TAccountAccount>
  48. : TAccountAccount,
  49. TAccountDestination extends string
  50. ? WritableAccount<TAccountDestination>
  51. : TAccountDestination,
  52. TAccountOwner extends string
  53. ? ReadonlyAccount<TAccountOwner>
  54. : TAccountOwner,
  55. ...TRemainingAccounts,
  56. ]
  57. >;
  58. export type CloseAccountInstructionData = { discriminator: number };
  59. export type CloseAccountInstructionDataArgs = {};
  60. export function getCloseAccountInstructionDataEncoder(): Encoder<CloseAccountInstructionDataArgs> {
  61. return transformEncoder(
  62. getStructEncoder([['discriminator', getU8Encoder()]]),
  63. (value) => ({ ...value, discriminator: CLOSE_ACCOUNT_DISCRIMINATOR })
  64. );
  65. }
  66. export function getCloseAccountInstructionDataDecoder(): Decoder<CloseAccountInstructionData> {
  67. return getStructDecoder([['discriminator', getU8Decoder()]]);
  68. }
  69. export function getCloseAccountInstructionDataCodec(): Codec<
  70. CloseAccountInstructionDataArgs,
  71. CloseAccountInstructionData
  72. > {
  73. return combineCodec(
  74. getCloseAccountInstructionDataEncoder(),
  75. getCloseAccountInstructionDataDecoder()
  76. );
  77. }
  78. export type CloseAccountInput<
  79. TAccountAccount extends string = string,
  80. TAccountDestination extends string = string,
  81. TAccountOwner extends string = string,
  82. > = {
  83. /** The account to close. */
  84. account: Address<TAccountAccount>;
  85. /** The destination account. */
  86. destination: Address<TAccountDestination>;
  87. /** The account's owner or its multisignature account. */
  88. owner: Address<TAccountOwner> | TransactionSigner<TAccountOwner>;
  89. multiSigners?: Array<TransactionSigner>;
  90. };
  91. export function getCloseAccountInstruction<
  92. TAccountAccount extends string,
  93. TAccountDestination extends string,
  94. TAccountOwner extends string,
  95. TProgramAddress extends Address = typeof TOKEN_PROGRAM_ADDRESS,
  96. >(
  97. input: CloseAccountInput<TAccountAccount, TAccountDestination, TAccountOwner>,
  98. config?: { programAddress?: TProgramAddress }
  99. ): CloseAccountInstruction<
  100. TProgramAddress,
  101. TAccountAccount,
  102. TAccountDestination,
  103. (typeof input)['owner'] extends TransactionSigner<TAccountOwner>
  104. ? ReadonlySignerAccount<TAccountOwner> & IAccountSignerMeta<TAccountOwner>
  105. : TAccountOwner
  106. > {
  107. // Program address.
  108. const programAddress = config?.programAddress ?? TOKEN_PROGRAM_ADDRESS;
  109. // Original accounts.
  110. const originalAccounts = {
  111. account: { value: input.account ?? null, isWritable: true },
  112. destination: { value: input.destination ?? null, isWritable: true },
  113. owner: { value: input.owner ?? null, isWritable: false },
  114. };
  115. const accounts = originalAccounts as Record<
  116. keyof typeof originalAccounts,
  117. ResolvedAccount
  118. >;
  119. // Original args.
  120. const args = { ...input };
  121. // Remaining accounts.
  122. const remainingAccounts: IAccountMeta[] = (args.multiSigners ?? []).map(
  123. (signer) => ({
  124. address: signer.address,
  125. role: AccountRole.READONLY_SIGNER,
  126. signer,
  127. })
  128. );
  129. const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  130. const instruction = {
  131. accounts: [
  132. getAccountMeta(accounts.account),
  133. getAccountMeta(accounts.destination),
  134. getAccountMeta(accounts.owner),
  135. ...remainingAccounts,
  136. ],
  137. programAddress,
  138. data: getCloseAccountInstructionDataEncoder().encode({}),
  139. } as CloseAccountInstruction<
  140. TProgramAddress,
  141. TAccountAccount,
  142. TAccountDestination,
  143. (typeof input)['owner'] extends TransactionSigner<TAccountOwner>
  144. ? ReadonlySignerAccount<TAccountOwner> & IAccountSignerMeta<TAccountOwner>
  145. : TAccountOwner
  146. >;
  147. return instruction;
  148. }
  149. export type ParsedCloseAccountInstruction<
  150. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  151. TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
  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 IAccountMeta[],
  167. >(
  168. instruction: IInstruction<TProgram> &
  169. IInstructionWithAccounts<TAccountMetas> &
  170. IInstructionWithData<Uint8Array>
  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![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. }