burnChecked.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. ReadonlySignerAccount,
  19. TransactionSigner,
  20. WritableAccount,
  21. combineCodec,
  22. getStructDecoder,
  23. getStructEncoder,
  24. getU64Decoder,
  25. getU64Encoder,
  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 BurnCheckedInstruction<
  33. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  34. TAccountAccount extends string | IAccountMeta<string> = string,
  35. TAccountMint extends string | IAccountMeta<string> = string,
  36. TAccountAuthority 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. ? WritableAccount<TAccountMint>
  47. : TAccountMint,
  48. TAccountAuthority extends string
  49. ? ReadonlySignerAccount<TAccountAuthority> &
  50. IAccountSignerMeta<TAccountAuthority>
  51. : TAccountAuthority,
  52. ...TRemainingAccounts,
  53. ]
  54. >;
  55. export type BurnCheckedInstructionData = {
  56. discriminator: number;
  57. amount: bigint;
  58. decimals: number;
  59. };
  60. export type BurnCheckedInstructionDataArgs = {
  61. amount: number | bigint;
  62. decimals: number;
  63. };
  64. export function getBurnCheckedInstructionDataEncoder(): Encoder<BurnCheckedInstructionDataArgs> {
  65. return transformEncoder(
  66. getStructEncoder([
  67. ['discriminator', getU8Encoder()],
  68. ['amount', getU64Encoder()],
  69. ['decimals', getU8Encoder()],
  70. ]),
  71. (value) => ({ ...value, discriminator: 15 })
  72. );
  73. }
  74. export function getBurnCheckedInstructionDataDecoder(): Decoder<BurnCheckedInstructionData> {
  75. return getStructDecoder([
  76. ['discriminator', getU8Decoder()],
  77. ['amount', getU64Decoder()],
  78. ['decimals', getU8Decoder()],
  79. ]);
  80. }
  81. export function getBurnCheckedInstructionDataCodec(): Codec<
  82. BurnCheckedInstructionDataArgs,
  83. BurnCheckedInstructionData
  84. > {
  85. return combineCodec(
  86. getBurnCheckedInstructionDataEncoder(),
  87. getBurnCheckedInstructionDataDecoder()
  88. );
  89. }
  90. export type BurnCheckedInput<
  91. TAccountAccount extends string = string,
  92. TAccountMint extends string = string,
  93. TAccountAuthority extends string = string,
  94. > = {
  95. account: Address<TAccountAccount>;
  96. mint: Address<TAccountMint>;
  97. authority: TransactionSigner<TAccountAuthority>;
  98. amount: BurnCheckedInstructionDataArgs['amount'];
  99. decimals: BurnCheckedInstructionDataArgs['decimals'];
  100. };
  101. export function getBurnCheckedInstruction<
  102. TAccountAccount extends string,
  103. TAccountMint extends string,
  104. TAccountAuthority extends string,
  105. >(
  106. input: BurnCheckedInput<TAccountAccount, TAccountMint, TAccountAuthority>
  107. ): BurnCheckedInstruction<
  108. typeof TOKEN_PROGRAM_ADDRESS,
  109. TAccountAccount,
  110. TAccountMint,
  111. TAccountAuthority
  112. > {
  113. // Program address.
  114. const programAddress = TOKEN_PROGRAM_ADDRESS;
  115. // Original accounts.
  116. const originalAccounts = {
  117. account: { value: input.account ?? null, isWritable: true },
  118. mint: { value: input.mint ?? null, isWritable: true },
  119. authority: { value: input.authority ?? null, isWritable: false },
  120. };
  121. const accounts = originalAccounts as Record<
  122. keyof typeof originalAccounts,
  123. ResolvedAccount
  124. >;
  125. // Original args.
  126. const args = { ...input };
  127. const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  128. const instruction = {
  129. accounts: [
  130. getAccountMeta(accounts.account),
  131. getAccountMeta(accounts.mint),
  132. getAccountMeta(accounts.authority),
  133. ],
  134. programAddress,
  135. data: getBurnCheckedInstructionDataEncoder().encode(
  136. args as BurnCheckedInstructionDataArgs
  137. ),
  138. } as BurnCheckedInstruction<
  139. typeof TOKEN_PROGRAM_ADDRESS,
  140. TAccountAccount,
  141. TAccountMint,
  142. TAccountAuthority
  143. >;
  144. return instruction;
  145. }
  146. export type ParsedBurnCheckedInstruction<
  147. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  148. TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
  149. > = {
  150. programAddress: Address<TProgram>;
  151. accounts: {
  152. account: TAccountMetas[0];
  153. mint: TAccountMetas[1];
  154. authority: TAccountMetas[2];
  155. };
  156. data: BurnCheckedInstructionData;
  157. };
  158. export function parseBurnCheckedInstruction<
  159. TProgram extends string,
  160. TAccountMetas extends readonly IAccountMeta[],
  161. >(
  162. instruction: IInstruction<TProgram> &
  163. IInstructionWithAccounts<TAccountMetas> &
  164. IInstructionWithData<Uint8Array>
  165. ): ParsedBurnCheckedInstruction<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. authority: getNextAccount(),
  182. },
  183. data: getBurnCheckedInstructionDataDecoder().decode(instruction.data),
  184. };
  185. }