transferChecked.ts 7.7 KB

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