mintTo.ts 6.6 KB

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