thawToken.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. Address,
  10. Codec,
  11. Decoder,
  12. Encoder,
  13. IAccountMeta,
  14. IAccountSignerMeta,
  15. IInstruction,
  16. IInstructionWithAccounts,
  17. IInstructionWithData,
  18. ReadonlyAccount,
  19. ReadonlySignerAccount,
  20. TransactionSigner,
  21. WritableAccount,
  22. combineCodec,
  23. getStructDecoder,
  24. getStructEncoder,
  25. getU8Decoder,
  26. getU8Encoder,
  27. transformEncoder,
  28. } from '@solana/web3.js';
  29. import { TOKEN_PROGRAM_ADDRESS } from '../programs';
  30. import { ResolvedAccount, getAccountMetaFactory } from '../shared';
  31. export type ThawTokenInstruction<
  32. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  33. TAccountAccount extends string | IAccountMeta<string> = string,
  34. TAccountMint extends string | IAccountMeta<string> = string,
  35. TAccountOwner extends string | IAccountMeta<string> = string,
  36. TRemainingAccounts extends readonly IAccountMeta<string>[] = [],
  37. > = IInstruction<TProgram> &
  38. IInstructionWithData<Uint8Array> &
  39. IInstructionWithAccounts<
  40. [
  41. TAccountAccount extends string
  42. ? WritableAccount<TAccountAccount>
  43. : TAccountAccount,
  44. TAccountMint extends string
  45. ? ReadonlyAccount<TAccountMint>
  46. : TAccountMint,
  47. TAccountOwner extends string
  48. ? ReadonlySignerAccount<TAccountOwner> &
  49. IAccountSignerMeta<TAccountOwner>
  50. : TAccountOwner,
  51. ...TRemainingAccounts,
  52. ]
  53. >;
  54. export type ThawTokenInstructionData = { discriminator: number };
  55. export type ThawTokenInstructionDataArgs = {};
  56. export function getThawTokenInstructionDataEncoder(): Encoder<ThawTokenInstructionDataArgs> {
  57. return transformEncoder(
  58. getStructEncoder([['discriminator', getU8Encoder()]]),
  59. (value) => ({ ...value, discriminator: 11 })
  60. );
  61. }
  62. export function getThawTokenInstructionDataDecoder(): Decoder<ThawTokenInstructionData> {
  63. return getStructDecoder([['discriminator', getU8Decoder()]]);
  64. }
  65. export function getThawTokenInstructionDataCodec(): Codec<
  66. ThawTokenInstructionDataArgs,
  67. ThawTokenInstructionData
  68. > {
  69. return combineCodec(
  70. getThawTokenInstructionDataEncoder(),
  71. getThawTokenInstructionDataDecoder()
  72. );
  73. }
  74. export type ThawTokenInput<
  75. TAccountAccount extends string = string,
  76. TAccountMint extends string = string,
  77. TAccountOwner extends string = string,
  78. > = {
  79. account: Address<TAccountAccount>;
  80. mint: Address<TAccountMint>;
  81. owner: TransactionSigner<TAccountOwner>;
  82. };
  83. export function getThawTokenInstruction<
  84. TAccountAccount extends string,
  85. TAccountMint extends string,
  86. TAccountOwner extends string,
  87. >(
  88. input: ThawTokenInput<TAccountAccount, TAccountMint, TAccountOwner>
  89. ): ThawTokenInstruction<
  90. typeof TOKEN_PROGRAM_ADDRESS,
  91. TAccountAccount,
  92. TAccountMint,
  93. TAccountOwner
  94. > {
  95. // Program address.
  96. const programAddress = TOKEN_PROGRAM_ADDRESS;
  97. // Original accounts.
  98. const originalAccounts = {
  99. account: { value: input.account ?? null, isWritable: true },
  100. mint: { value: input.mint ?? null, isWritable: false },
  101. owner: { value: input.owner ?? null, isWritable: false },
  102. };
  103. const accounts = originalAccounts as Record<
  104. keyof typeof originalAccounts,
  105. ResolvedAccount
  106. >;
  107. const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  108. const instruction = {
  109. accounts: [
  110. getAccountMeta(accounts.account),
  111. getAccountMeta(accounts.mint),
  112. getAccountMeta(accounts.owner),
  113. ],
  114. programAddress,
  115. data: getThawTokenInstructionDataEncoder().encode({}),
  116. } as ThawTokenInstruction<
  117. typeof TOKEN_PROGRAM_ADDRESS,
  118. TAccountAccount,
  119. TAccountMint,
  120. TAccountOwner
  121. >;
  122. return instruction;
  123. }
  124. export type ParsedThawTokenInstruction<
  125. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  126. TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
  127. > = {
  128. programAddress: Address<TProgram>;
  129. accounts: {
  130. account: TAccountMetas[0];
  131. mint: TAccountMetas[1];
  132. owner: TAccountMetas[2];
  133. };
  134. data: ThawTokenInstructionData;
  135. };
  136. export function parseThawTokenInstruction<
  137. TProgram extends string,
  138. TAccountMetas extends readonly IAccountMeta[],
  139. >(
  140. instruction: IInstruction<TProgram> &
  141. IInstructionWithAccounts<TAccountMetas> &
  142. IInstructionWithData<Uint8Array>
  143. ): ParsedThawTokenInstruction<TProgram, TAccountMetas> {
  144. if (instruction.accounts.length < 3) {
  145. // TODO: Coded error.
  146. throw new Error('Not enough accounts');
  147. }
  148. let accountIndex = 0;
  149. const getNextAccount = () => {
  150. const accountMeta = instruction.accounts![accountIndex]!;
  151. accountIndex += 1;
  152. return accountMeta;
  153. };
  154. return {
  155. programAddress: instruction.programAddress,
  156. accounts: {
  157. account: getNextAccount(),
  158. mint: getNextAccount(),
  159. owner: getNextAccount(),
  160. },
  161. data: getThawTokenInstructionDataDecoder().decode(instruction.data),
  162. };
  163. }