freezeAccount.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 type FreezeAccountInstruction<
  33. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  34. TAccountAccount extends string | IAccountMeta<string> = string,
  35. TAccountMint 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. TAccountMint extends string
  46. ? ReadonlyAccount<TAccountMint>
  47. : TAccountMint,
  48. TAccountOwner extends string
  49. ? ReadonlyAccount<TAccountOwner>
  50. : TAccountOwner,
  51. ...TRemainingAccounts,
  52. ]
  53. >;
  54. export type FreezeAccountInstructionData = { discriminator: number };
  55. export type FreezeAccountInstructionDataArgs = {};
  56. export function getFreezeAccountInstructionDataEncoder(): Encoder<FreezeAccountInstructionDataArgs> {
  57. return transformEncoder(
  58. getStructEncoder([['discriminator', getU8Encoder()]]),
  59. (value) => ({ ...value, discriminator: 10 })
  60. );
  61. }
  62. export function getFreezeAccountInstructionDataDecoder(): Decoder<FreezeAccountInstructionData> {
  63. return getStructDecoder([['discriminator', getU8Decoder()]]);
  64. }
  65. export function getFreezeAccountInstructionDataCodec(): Codec<
  66. FreezeAccountInstructionDataArgs,
  67. FreezeAccountInstructionData
  68. > {
  69. return combineCodec(
  70. getFreezeAccountInstructionDataEncoder(),
  71. getFreezeAccountInstructionDataDecoder()
  72. );
  73. }
  74. export type FreezeAccountInput<
  75. TAccountAccount extends string = string,
  76. TAccountMint extends string = string,
  77. TAccountOwner extends string = string,
  78. > = {
  79. /** The account to freeze. */
  80. account: Address<TAccountAccount>;
  81. /** The token mint. */
  82. mint: Address<TAccountMint>;
  83. /** The mint freeze authority or its multisignature account. */
  84. owner: Address<TAccountOwner> | TransactionSigner<TAccountOwner>;
  85. multiSigners?: Array<TransactionSigner>;
  86. };
  87. export function getFreezeAccountInstruction<
  88. TAccountAccount extends string,
  89. TAccountMint extends string,
  90. TAccountOwner extends string,
  91. >(
  92. input: FreezeAccountInput<TAccountAccount, TAccountMint, TAccountOwner>
  93. ): FreezeAccountInstruction<
  94. typeof TOKEN_PROGRAM_ADDRESS,
  95. TAccountAccount,
  96. TAccountMint,
  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. mint: { value: input.mint ?? null, isWritable: false },
  107. owner: { value: input.owner ?? null, isWritable: false },
  108. };
  109. const accounts = originalAccounts as Record<
  110. keyof typeof originalAccounts,
  111. ResolvedAccount
  112. >;
  113. // Original args.
  114. const args = { ...input };
  115. // Remaining accounts.
  116. const remainingAccounts: IAccountMeta[] = (args.multiSigners ?? []).map(
  117. (signer) => ({
  118. address: signer.address,
  119. role: AccountRole.READONLY_SIGNER,
  120. signer,
  121. })
  122. );
  123. const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  124. const instruction = {
  125. accounts: [
  126. getAccountMeta(accounts.account),
  127. getAccountMeta(accounts.mint),
  128. getAccountMeta(accounts.owner),
  129. ...remainingAccounts,
  130. ],
  131. programAddress,
  132. data: getFreezeAccountInstructionDataEncoder().encode({}),
  133. } as FreezeAccountInstruction<
  134. typeof TOKEN_PROGRAM_ADDRESS,
  135. TAccountAccount,
  136. TAccountMint,
  137. (typeof input)['owner'] extends TransactionSigner<TAccountOwner>
  138. ? ReadonlySignerAccount<TAccountOwner> & IAccountSignerMeta<TAccountOwner>
  139. : TAccountOwner
  140. >;
  141. return instruction;
  142. }
  143. export type ParsedFreezeAccountInstruction<
  144. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  145. TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
  146. > = {
  147. programAddress: Address<TProgram>;
  148. accounts: {
  149. /** The account to freeze. */
  150. account: TAccountMetas[0];
  151. /** The token mint. */
  152. mint: TAccountMetas[1];
  153. /** The mint freeze authority or its multisignature account. */
  154. owner: TAccountMetas[2];
  155. };
  156. data: FreezeAccountInstructionData;
  157. };
  158. export function parseFreezeAccountInstruction<
  159. TProgram extends string,
  160. TAccountMetas extends readonly IAccountMeta[],
  161. >(
  162. instruction: IInstruction<TProgram> &
  163. IInstructionWithAccounts<TAccountMetas> &
  164. IInstructionWithData<Uint8Array>
  165. ): ParsedFreezeAccountInstruction<TProgram, TAccountMetas> {
  166. if (instruction.accounts.length < 3) {
  167. // TODO: Coded error.
  168. throw new Error('Not enough accounts');
  169. }
  170. let accountIndex = 0;
  171. const getNextAccount = () => {
  172. const accountMeta = instruction.accounts![accountIndex]!;
  173. accountIndex += 1;
  174. return accountMeta;
  175. };
  176. return {
  177. programAddress: instruction.programAddress,
  178. accounts: {
  179. account: getNextAccount(),
  180. mint: getNextAccount(),
  181. owner: getNextAccount(),
  182. },
  183. data: getFreezeAccountInstructionDataDecoder().decode(instruction.data),
  184. };
  185. }