approveChecked.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. AccountRole,
  10. Address,
  11. Codec,
  12. Decoder,
  13. Encoder,
  14. IAccountMeta,
  15. IAccountSignerMeta,
  16. IInstruction,
  17. IInstructionWithAccounts,
  18. IInstructionWithData,
  19. ReadonlyAccount,
  20. ReadonlySignerAccount,
  21. TransactionSigner,
  22. WritableAccount,
  23. combineCodec,
  24. getStructDecoder,
  25. getStructEncoder,
  26. getU64Decoder,
  27. getU64Encoder,
  28. getU8Decoder,
  29. getU8Encoder,
  30. transformEncoder,
  31. } from '@solana/web3.js';
  32. import { TOKEN_PROGRAM_ADDRESS } from '../programs';
  33. import { ResolvedAccount, getAccountMetaFactory } from '../shared';
  34. export type ApproveCheckedInstruction<
  35. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  36. TAccountSource extends string | IAccountMeta<string> = string,
  37. TAccountMint extends string | IAccountMeta<string> = string,
  38. TAccountDelegate extends string | IAccountMeta<string> = string,
  39. TAccountOwner extends string | IAccountMeta<string> = string,
  40. TRemainingAccounts extends readonly IAccountMeta<string>[] = [],
  41. > = IInstruction<TProgram> &
  42. IInstructionWithData<Uint8Array> &
  43. IInstructionWithAccounts<
  44. [
  45. TAccountSource extends string
  46. ? WritableAccount<TAccountSource>
  47. : TAccountSource,
  48. TAccountMint extends string
  49. ? ReadonlyAccount<TAccountMint>
  50. : TAccountMint,
  51. TAccountDelegate extends string
  52. ? ReadonlyAccount<TAccountDelegate>
  53. : TAccountDelegate,
  54. TAccountOwner extends string
  55. ? ReadonlyAccount<TAccountOwner>
  56. : TAccountOwner,
  57. ...TRemainingAccounts,
  58. ]
  59. >;
  60. export type ApproveCheckedInstructionData = {
  61. discriminator: number;
  62. /** The amount of tokens the delegate is approved for. */
  63. amount: bigint;
  64. /** Expected number of base 10 digits to the right of the decimal place. */
  65. decimals: number;
  66. };
  67. export type ApproveCheckedInstructionDataArgs = {
  68. /** The amount of tokens the delegate is approved for. */
  69. amount: number | bigint;
  70. /** Expected number of base 10 digits to the right of the decimal place. */
  71. decimals: number;
  72. };
  73. export function getApproveCheckedInstructionDataEncoder(): Encoder<ApproveCheckedInstructionDataArgs> {
  74. return transformEncoder(
  75. getStructEncoder([
  76. ['discriminator', getU8Encoder()],
  77. ['amount', getU64Encoder()],
  78. ['decimals', getU8Encoder()],
  79. ]),
  80. (value) => ({ ...value, discriminator: 13 })
  81. );
  82. }
  83. export function getApproveCheckedInstructionDataDecoder(): Decoder<ApproveCheckedInstructionData> {
  84. return getStructDecoder([
  85. ['discriminator', getU8Decoder()],
  86. ['amount', getU64Decoder()],
  87. ['decimals', getU8Decoder()],
  88. ]);
  89. }
  90. export function getApproveCheckedInstructionDataCodec(): Codec<
  91. ApproveCheckedInstructionDataArgs,
  92. ApproveCheckedInstructionData
  93. > {
  94. return combineCodec(
  95. getApproveCheckedInstructionDataEncoder(),
  96. getApproveCheckedInstructionDataDecoder()
  97. );
  98. }
  99. export type ApproveCheckedInput<
  100. TAccountSource extends string = string,
  101. TAccountMint extends string = string,
  102. TAccountDelegate extends string = string,
  103. TAccountOwner extends string = string,
  104. > = {
  105. /** The source account. */
  106. source: Address<TAccountSource>;
  107. /** The token mint. */
  108. mint: Address<TAccountMint>;
  109. /** The delegate. */
  110. delegate: Address<TAccountDelegate>;
  111. /** The source account owner or its multisignature account. */
  112. owner: Address<TAccountOwner> | TransactionSigner<TAccountOwner>;
  113. amount: ApproveCheckedInstructionDataArgs['amount'];
  114. decimals: ApproveCheckedInstructionDataArgs['decimals'];
  115. multiSigners?: Array<TransactionSigner>;
  116. };
  117. export function getApproveCheckedInstruction<
  118. TAccountSource extends string,
  119. TAccountMint extends string,
  120. TAccountDelegate extends string,
  121. TAccountOwner extends string,
  122. >(
  123. input: ApproveCheckedInput<
  124. TAccountSource,
  125. TAccountMint,
  126. TAccountDelegate,
  127. TAccountOwner
  128. >
  129. ): ApproveCheckedInstruction<
  130. typeof TOKEN_PROGRAM_ADDRESS,
  131. TAccountSource,
  132. TAccountMint,
  133. TAccountDelegate,
  134. (typeof input)['owner'] extends TransactionSigner<TAccountOwner>
  135. ? ReadonlySignerAccount<TAccountOwner> & IAccountSignerMeta<TAccountOwner>
  136. : TAccountOwner
  137. > {
  138. // Program address.
  139. const programAddress = TOKEN_PROGRAM_ADDRESS;
  140. // Original accounts.
  141. const originalAccounts = {
  142. source: { value: input.source ?? null, isWritable: true },
  143. mint: { value: input.mint ?? null, isWritable: false },
  144. delegate: { value: input.delegate ?? null, isWritable: false },
  145. owner: { value: input.owner ?? null, isWritable: false },
  146. };
  147. const accounts = originalAccounts as Record<
  148. keyof typeof originalAccounts,
  149. ResolvedAccount
  150. >;
  151. // Original args.
  152. const args = { ...input };
  153. // Remaining accounts.
  154. const remainingAccounts: IAccountMeta[] = (args.multiSigners ?? []).map(
  155. (signer) => ({
  156. address: signer.address,
  157. role: AccountRole.READONLY_SIGNER,
  158. signer,
  159. })
  160. );
  161. const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  162. const instruction = {
  163. accounts: [
  164. getAccountMeta(accounts.source),
  165. getAccountMeta(accounts.mint),
  166. getAccountMeta(accounts.delegate),
  167. getAccountMeta(accounts.owner),
  168. ...remainingAccounts,
  169. ],
  170. programAddress,
  171. data: getApproveCheckedInstructionDataEncoder().encode(
  172. args as ApproveCheckedInstructionDataArgs
  173. ),
  174. } as ApproveCheckedInstruction<
  175. typeof TOKEN_PROGRAM_ADDRESS,
  176. TAccountSource,
  177. TAccountMint,
  178. TAccountDelegate,
  179. (typeof input)['owner'] extends TransactionSigner<TAccountOwner>
  180. ? ReadonlySignerAccount<TAccountOwner> & IAccountSignerMeta<TAccountOwner>
  181. : TAccountOwner
  182. >;
  183. return instruction;
  184. }
  185. export type ParsedApproveCheckedInstruction<
  186. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  187. TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
  188. > = {
  189. programAddress: Address<TProgram>;
  190. accounts: {
  191. /** The source account. */
  192. source: TAccountMetas[0];
  193. /** The token mint. */
  194. mint: TAccountMetas[1];
  195. /** The delegate. */
  196. delegate: TAccountMetas[2];
  197. /** The source account owner or its multisignature account. */
  198. owner: TAccountMetas[3];
  199. };
  200. data: ApproveCheckedInstructionData;
  201. };
  202. export function parseApproveCheckedInstruction<
  203. TProgram extends string,
  204. TAccountMetas extends readonly IAccountMeta[],
  205. >(
  206. instruction: IInstruction<TProgram> &
  207. IInstructionWithAccounts<TAccountMetas> &
  208. IInstructionWithData<Uint8Array>
  209. ): ParsedApproveCheckedInstruction<TProgram, TAccountMetas> {
  210. if (instruction.accounts.length < 4) {
  211. // TODO: Coded error.
  212. throw new Error('Not enough accounts');
  213. }
  214. let accountIndex = 0;
  215. const getNextAccount = () => {
  216. const accountMeta = instruction.accounts![accountIndex]!;
  217. accountIndex += 1;
  218. return accountMeta;
  219. };
  220. return {
  221. programAddress: instruction.programAddress,
  222. accounts: {
  223. source: getNextAccount(),
  224. mint: getNextAccount(),
  225. delegate: getNextAccount(),
  226. owner: getNextAccount(),
  227. },
  228. data: getApproveCheckedInstructionDataDecoder().decode(instruction.data),
  229. };
  230. }