approve.ts 6.5 KB

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