approve.ts 6.2 KB

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