thawAccount.ts 6.2 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 THAW_ACCOUNT_DISCRIMINATOR = 11;
  33. export function getThawAccountDiscriminatorBytes() {
  34. return getU8Encoder().encode(THAW_ACCOUNT_DISCRIMINATOR);
  35. }
  36. export type ThawAccountInstruction<
  37. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  38. TAccountAccount extends string | IAccountMeta<string> = string,
  39. TAccountMint 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. TAccountMint extends string
  50. ? ReadonlyAccount<TAccountMint>
  51. : TAccountMint,
  52. TAccountOwner extends string
  53. ? ReadonlyAccount<TAccountOwner>
  54. : TAccountOwner,
  55. ...TRemainingAccounts,
  56. ]
  57. >;
  58. export type ThawAccountInstructionData = { discriminator: number };
  59. export type ThawAccountInstructionDataArgs = {};
  60. export function getThawAccountInstructionDataEncoder(): Encoder<ThawAccountInstructionDataArgs> {
  61. return transformEncoder(
  62. getStructEncoder([['discriminator', getU8Encoder()]]),
  63. (value) => ({ ...value, discriminator: THAW_ACCOUNT_DISCRIMINATOR })
  64. );
  65. }
  66. export function getThawAccountInstructionDataDecoder(): Decoder<ThawAccountInstructionData> {
  67. return getStructDecoder([['discriminator', getU8Decoder()]]);
  68. }
  69. export function getThawAccountInstructionDataCodec(): Codec<
  70. ThawAccountInstructionDataArgs,
  71. ThawAccountInstructionData
  72. > {
  73. return combineCodec(
  74. getThawAccountInstructionDataEncoder(),
  75. getThawAccountInstructionDataDecoder()
  76. );
  77. }
  78. export type ThawAccountInput<
  79. TAccountAccount extends string = string,
  80. TAccountMint extends string = string,
  81. TAccountOwner extends string = string,
  82. > = {
  83. /** The account to thaw. */
  84. account: Address<TAccountAccount>;
  85. /** The token mint. */
  86. mint: Address<TAccountMint>;
  87. /** The mint freeze authority or its multisignature account. */
  88. owner: Address<TAccountOwner> | TransactionSigner<TAccountOwner>;
  89. multiSigners?: Array<TransactionSigner>;
  90. };
  91. export function getThawAccountInstruction<
  92. TAccountAccount extends string,
  93. TAccountMint extends string,
  94. TAccountOwner extends string,
  95. TProgramAddress extends Address = typeof TOKEN_PROGRAM_ADDRESS,
  96. >(
  97. input: ThawAccountInput<TAccountAccount, TAccountMint, TAccountOwner>,
  98. config?: { programAddress?: TProgramAddress }
  99. ): ThawAccountInstruction<
  100. TProgramAddress,
  101. TAccountAccount,
  102. TAccountMint,
  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. mint: { value: input.mint ?? null, isWritable: false },
  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.mint),
  134. getAccountMeta(accounts.owner),
  135. ...remainingAccounts,
  136. ],
  137. programAddress,
  138. data: getThawAccountInstructionDataEncoder().encode({}),
  139. } as ThawAccountInstruction<
  140. TProgramAddress,
  141. TAccountAccount,
  142. TAccountMint,
  143. (typeof input)['owner'] extends TransactionSigner<TAccountOwner>
  144. ? ReadonlySignerAccount<TAccountOwner> & IAccountSignerMeta<TAccountOwner>
  145. : TAccountOwner
  146. >;
  147. return instruction;
  148. }
  149. export type ParsedThawAccountInstruction<
  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 thaw. */
  156. account: TAccountMetas[0];
  157. /** The token mint. */
  158. mint: TAccountMetas[1];
  159. /** The mint freeze authority or its multisignature account. */
  160. owner: TAccountMetas[2];
  161. };
  162. data: ThawAccountInstructionData;
  163. };
  164. export function parseThawAccountInstruction<
  165. TProgram extends string,
  166. TAccountMetas extends readonly IAccountMeta[],
  167. >(
  168. instruction: IInstruction<TProgram> &
  169. IInstructionWithAccounts<TAccountMetas> &
  170. IInstructionWithData<Uint8Array>
  171. ): ParsedThawAccountInstruction<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. mint: getNextAccount(),
  187. owner: getNextAccount(),
  188. },
  189. data: getThawAccountInstructionDataDecoder().decode(instruction.data),
  190. };
  191. }