createAssociatedTokenIdempotent.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. combineCodec,
  10. getStructDecoder,
  11. getStructEncoder,
  12. getU8Decoder,
  13. getU8Encoder,
  14. transformEncoder,
  15. type Address,
  16. type Codec,
  17. type Decoder,
  18. type Encoder,
  19. type IAccountMeta,
  20. type IAccountSignerMeta,
  21. type IInstruction,
  22. type IInstructionWithAccounts,
  23. type IInstructionWithData,
  24. type ReadonlyAccount,
  25. type TransactionSigner,
  26. type WritableAccount,
  27. type WritableSignerAccount,
  28. } from '@solana/web3.js';
  29. import { findAssociatedTokenPda } from '../pdas';
  30. import { ASSOCIATED_TOKEN_PROGRAM_ADDRESS } from '../programs';
  31. import {
  32. expectAddress,
  33. getAccountMetaFactory,
  34. type ResolvedAccount,
  35. } from '../shared';
  36. export type CreateAssociatedTokenIdempotentInstruction<
  37. TProgram extends string = typeof ASSOCIATED_TOKEN_PROGRAM_ADDRESS,
  38. TAccountPayer extends string | IAccountMeta<string> = string,
  39. TAccountAta extends string | IAccountMeta<string> = string,
  40. TAccountOwner extends string | IAccountMeta<string> = string,
  41. TAccountMint extends string | IAccountMeta<string> = string,
  42. TAccountSystemProgram extends
  43. | string
  44. | IAccountMeta<string> = '11111111111111111111111111111111',
  45. TAccountTokenProgram extends
  46. | string
  47. | IAccountMeta<string> = 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
  48. TRemainingAccounts extends readonly IAccountMeta<string>[] = [],
  49. > = IInstruction<TProgram> &
  50. IInstructionWithData<Uint8Array> &
  51. IInstructionWithAccounts<
  52. [
  53. TAccountPayer extends string
  54. ? WritableSignerAccount<TAccountPayer> &
  55. IAccountSignerMeta<TAccountPayer>
  56. : TAccountPayer,
  57. TAccountAta extends string ? WritableAccount<TAccountAta> : TAccountAta,
  58. TAccountOwner extends string
  59. ? ReadonlyAccount<TAccountOwner>
  60. : TAccountOwner,
  61. TAccountMint extends string
  62. ? ReadonlyAccount<TAccountMint>
  63. : TAccountMint,
  64. TAccountSystemProgram extends string
  65. ? ReadonlyAccount<TAccountSystemProgram>
  66. : TAccountSystemProgram,
  67. TAccountTokenProgram extends string
  68. ? ReadonlyAccount<TAccountTokenProgram>
  69. : TAccountTokenProgram,
  70. ...TRemainingAccounts,
  71. ]
  72. >;
  73. export type CreateAssociatedTokenIdempotentInstructionData = {
  74. discriminator: number;
  75. };
  76. export type CreateAssociatedTokenIdempotentInstructionDataArgs = {};
  77. export function getCreateAssociatedTokenIdempotentInstructionDataEncoder(): Encoder<CreateAssociatedTokenIdempotentInstructionDataArgs> {
  78. return transformEncoder(
  79. getStructEncoder([['discriminator', getU8Encoder()]]),
  80. (value) => ({ ...value, discriminator: 1 })
  81. );
  82. }
  83. export function getCreateAssociatedTokenIdempotentInstructionDataDecoder(): Decoder<CreateAssociatedTokenIdempotentInstructionData> {
  84. return getStructDecoder([['discriminator', getU8Decoder()]]);
  85. }
  86. export function getCreateAssociatedTokenIdempotentInstructionDataCodec(): Codec<
  87. CreateAssociatedTokenIdempotentInstructionDataArgs,
  88. CreateAssociatedTokenIdempotentInstructionData
  89. > {
  90. return combineCodec(
  91. getCreateAssociatedTokenIdempotentInstructionDataEncoder(),
  92. getCreateAssociatedTokenIdempotentInstructionDataDecoder()
  93. );
  94. }
  95. export type CreateAssociatedTokenIdempotentAsyncInput<
  96. TAccountPayer extends string = string,
  97. TAccountAta extends string = string,
  98. TAccountOwner extends string = string,
  99. TAccountMint extends string = string,
  100. TAccountSystemProgram extends string = string,
  101. TAccountTokenProgram extends string = string,
  102. > = {
  103. /** Funding account (must be a system account). */
  104. payer: TransactionSigner<TAccountPayer>;
  105. /** Associated token account address to be created. */
  106. ata?: Address<TAccountAta>;
  107. /** Wallet address for the new associated token account. */
  108. owner: Address<TAccountOwner>;
  109. /** The token mint for the new associated token account. */
  110. mint: Address<TAccountMint>;
  111. /** System program. */
  112. systemProgram?: Address<TAccountSystemProgram>;
  113. /** SPL Token program. */
  114. tokenProgram?: Address<TAccountTokenProgram>;
  115. };
  116. export async function getCreateAssociatedTokenIdempotentInstructionAsync<
  117. TAccountPayer extends string,
  118. TAccountAta extends string,
  119. TAccountOwner extends string,
  120. TAccountMint extends string,
  121. TAccountSystemProgram extends string,
  122. TAccountTokenProgram extends string,
  123. >(
  124. input: CreateAssociatedTokenIdempotentAsyncInput<
  125. TAccountPayer,
  126. TAccountAta,
  127. TAccountOwner,
  128. TAccountMint,
  129. TAccountSystemProgram,
  130. TAccountTokenProgram
  131. >
  132. ): Promise<
  133. CreateAssociatedTokenIdempotentInstruction<
  134. typeof ASSOCIATED_TOKEN_PROGRAM_ADDRESS,
  135. TAccountPayer,
  136. TAccountAta,
  137. TAccountOwner,
  138. TAccountMint,
  139. TAccountSystemProgram,
  140. TAccountTokenProgram
  141. >
  142. > {
  143. // Program address.
  144. const programAddress = ASSOCIATED_TOKEN_PROGRAM_ADDRESS;
  145. // Original accounts.
  146. const originalAccounts = {
  147. payer: { value: input.payer ?? null, isWritable: true },
  148. ata: { value: input.ata ?? null, isWritable: true },
  149. owner: { value: input.owner ?? null, isWritable: false },
  150. mint: { value: input.mint ?? null, isWritable: false },
  151. systemProgram: { value: input.systemProgram ?? null, isWritable: false },
  152. tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
  153. };
  154. const accounts = originalAccounts as Record<
  155. keyof typeof originalAccounts,
  156. ResolvedAccount
  157. >;
  158. // Resolve default values.
  159. if (!accounts.tokenProgram.value) {
  160. accounts.tokenProgram.value =
  161. 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA' as Address<'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'>;
  162. }
  163. if (!accounts.ata.value) {
  164. accounts.ata.value = await findAssociatedTokenPda({
  165. owner: expectAddress(accounts.owner.value),
  166. tokenProgram: expectAddress(accounts.tokenProgram.value),
  167. mint: expectAddress(accounts.mint.value),
  168. });
  169. }
  170. if (!accounts.systemProgram.value) {
  171. accounts.systemProgram.value =
  172. '11111111111111111111111111111111' as Address<'11111111111111111111111111111111'>;
  173. }
  174. const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  175. const instruction = {
  176. accounts: [
  177. getAccountMeta(accounts.payer),
  178. getAccountMeta(accounts.ata),
  179. getAccountMeta(accounts.owner),
  180. getAccountMeta(accounts.mint),
  181. getAccountMeta(accounts.systemProgram),
  182. getAccountMeta(accounts.tokenProgram),
  183. ],
  184. programAddress,
  185. data: getCreateAssociatedTokenIdempotentInstructionDataEncoder().encode({}),
  186. } as CreateAssociatedTokenIdempotentInstruction<
  187. typeof ASSOCIATED_TOKEN_PROGRAM_ADDRESS,
  188. TAccountPayer,
  189. TAccountAta,
  190. TAccountOwner,
  191. TAccountMint,
  192. TAccountSystemProgram,
  193. TAccountTokenProgram
  194. >;
  195. return instruction;
  196. }
  197. export type CreateAssociatedTokenIdempotentInput<
  198. TAccountPayer extends string = string,
  199. TAccountAta extends string = string,
  200. TAccountOwner extends string = string,
  201. TAccountMint extends string = string,
  202. TAccountSystemProgram extends string = string,
  203. TAccountTokenProgram extends string = string,
  204. > = {
  205. /** Funding account (must be a system account). */
  206. payer: TransactionSigner<TAccountPayer>;
  207. /** Associated token account address to be created. */
  208. ata: Address<TAccountAta>;
  209. /** Wallet address for the new associated token account. */
  210. owner: Address<TAccountOwner>;
  211. /** The token mint for the new associated token account. */
  212. mint: Address<TAccountMint>;
  213. /** System program. */
  214. systemProgram?: Address<TAccountSystemProgram>;
  215. /** SPL Token program. */
  216. tokenProgram?: Address<TAccountTokenProgram>;
  217. };
  218. export function getCreateAssociatedTokenIdempotentInstruction<
  219. TAccountPayer extends string,
  220. TAccountAta extends string,
  221. TAccountOwner extends string,
  222. TAccountMint extends string,
  223. TAccountSystemProgram extends string,
  224. TAccountTokenProgram extends string,
  225. >(
  226. input: CreateAssociatedTokenIdempotentInput<
  227. TAccountPayer,
  228. TAccountAta,
  229. TAccountOwner,
  230. TAccountMint,
  231. TAccountSystemProgram,
  232. TAccountTokenProgram
  233. >
  234. ): CreateAssociatedTokenIdempotentInstruction<
  235. typeof ASSOCIATED_TOKEN_PROGRAM_ADDRESS,
  236. TAccountPayer,
  237. TAccountAta,
  238. TAccountOwner,
  239. TAccountMint,
  240. TAccountSystemProgram,
  241. TAccountTokenProgram
  242. > {
  243. // Program address.
  244. const programAddress = ASSOCIATED_TOKEN_PROGRAM_ADDRESS;
  245. // Original accounts.
  246. const originalAccounts = {
  247. payer: { value: input.payer ?? null, isWritable: true },
  248. ata: { value: input.ata ?? null, isWritable: true },
  249. owner: { value: input.owner ?? null, isWritable: false },
  250. mint: { value: input.mint ?? null, isWritable: false },
  251. systemProgram: { value: input.systemProgram ?? null, isWritable: false },
  252. tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
  253. };
  254. const accounts = originalAccounts as Record<
  255. keyof typeof originalAccounts,
  256. ResolvedAccount
  257. >;
  258. // Resolve default values.
  259. if (!accounts.tokenProgram.value) {
  260. accounts.tokenProgram.value =
  261. 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA' as Address<'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'>;
  262. }
  263. if (!accounts.systemProgram.value) {
  264. accounts.systemProgram.value =
  265. '11111111111111111111111111111111' as Address<'11111111111111111111111111111111'>;
  266. }
  267. const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  268. const instruction = {
  269. accounts: [
  270. getAccountMeta(accounts.payer),
  271. getAccountMeta(accounts.ata),
  272. getAccountMeta(accounts.owner),
  273. getAccountMeta(accounts.mint),
  274. getAccountMeta(accounts.systemProgram),
  275. getAccountMeta(accounts.tokenProgram),
  276. ],
  277. programAddress,
  278. data: getCreateAssociatedTokenIdempotentInstructionDataEncoder().encode({}),
  279. } as CreateAssociatedTokenIdempotentInstruction<
  280. typeof ASSOCIATED_TOKEN_PROGRAM_ADDRESS,
  281. TAccountPayer,
  282. TAccountAta,
  283. TAccountOwner,
  284. TAccountMint,
  285. TAccountSystemProgram,
  286. TAccountTokenProgram
  287. >;
  288. return instruction;
  289. }
  290. export type ParsedCreateAssociatedTokenIdempotentInstruction<
  291. TProgram extends string = typeof ASSOCIATED_TOKEN_PROGRAM_ADDRESS,
  292. TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
  293. > = {
  294. programAddress: Address<TProgram>;
  295. accounts: {
  296. /** Funding account (must be a system account). */
  297. payer: TAccountMetas[0];
  298. /** Associated token account address to be created. */
  299. ata: TAccountMetas[1];
  300. /** Wallet address for the new associated token account. */
  301. owner: TAccountMetas[2];
  302. /** The token mint for the new associated token account. */
  303. mint: TAccountMetas[3];
  304. /** System program. */
  305. systemProgram: TAccountMetas[4];
  306. /** SPL Token program. */
  307. tokenProgram: TAccountMetas[5];
  308. };
  309. data: CreateAssociatedTokenIdempotentInstructionData;
  310. };
  311. export function parseCreateAssociatedTokenIdempotentInstruction<
  312. TProgram extends string,
  313. TAccountMetas extends readonly IAccountMeta[],
  314. >(
  315. instruction: IInstruction<TProgram> &
  316. IInstructionWithAccounts<TAccountMetas> &
  317. IInstructionWithData<Uint8Array>
  318. ): ParsedCreateAssociatedTokenIdempotentInstruction<TProgram, TAccountMetas> {
  319. if (instruction.accounts.length < 6) {
  320. // TODO: Coded error.
  321. throw new Error('Not enough accounts');
  322. }
  323. let accountIndex = 0;
  324. const getNextAccount = () => {
  325. const accountMeta = instruction.accounts![accountIndex]!;
  326. accountIndex += 1;
  327. return accountMeta;
  328. };
  329. return {
  330. programAddress: instruction.programAddress,
  331. accounts: {
  332. payer: getNextAccount(),
  333. ata: getNextAccount(),
  334. owner: getNextAccount(),
  335. mint: getNextAccount(),
  336. systemProgram: getNextAccount(),
  337. tokenProgram: getNextAccount(),
  338. },
  339. data: getCreateAssociatedTokenIdempotentInstructionDataDecoder().decode(
  340. instruction.data
  341. ),
  342. };
  343. }