initializeAccount2.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. getAddressDecoder,
  11. getAddressEncoder,
  12. getStructDecoder,
  13. getStructEncoder,
  14. getU8Decoder,
  15. getU8Encoder,
  16. transformEncoder,
  17. type Address,
  18. type Codec,
  19. type Decoder,
  20. type Encoder,
  21. type IAccountMeta,
  22. type IInstruction,
  23. type IInstructionWithAccounts,
  24. type IInstructionWithData,
  25. type ReadonlyAccount,
  26. type WritableAccount,
  27. } from '@solana/web3.js';
  28. import { TOKEN_PROGRAM_ADDRESS } from '../programs';
  29. import { getAccountMetaFactory, type ResolvedAccount } from '../shared';
  30. export const INITIALIZE_ACCOUNT2_DISCRIMINATOR = 16;
  31. export function getInitializeAccount2DiscriminatorBytes() {
  32. return getU8Encoder().encode(INITIALIZE_ACCOUNT2_DISCRIMINATOR);
  33. }
  34. export type InitializeAccount2Instruction<
  35. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  36. TAccountAccount extends string | IAccountMeta<string> = string,
  37. TAccountMint extends string | IAccountMeta<string> = string,
  38. TAccountRent extends
  39. | string
  40. | IAccountMeta<string> = 'SysvarRent111111111111111111111111111111111',
  41. TRemainingAccounts extends readonly IAccountMeta<string>[] = [],
  42. > = IInstruction<TProgram> &
  43. IInstructionWithData<Uint8Array> &
  44. IInstructionWithAccounts<
  45. [
  46. TAccountAccount extends string
  47. ? WritableAccount<TAccountAccount>
  48. : TAccountAccount,
  49. TAccountMint extends string
  50. ? ReadonlyAccount<TAccountMint>
  51. : TAccountMint,
  52. TAccountRent extends string
  53. ? ReadonlyAccount<TAccountRent>
  54. : TAccountRent,
  55. ...TRemainingAccounts,
  56. ]
  57. >;
  58. export type InitializeAccount2InstructionData = {
  59. discriminator: number;
  60. /** The new account's owner/multisignature. */
  61. owner: Address;
  62. };
  63. export type InitializeAccount2InstructionDataArgs = {
  64. /** The new account's owner/multisignature. */
  65. owner: Address;
  66. };
  67. export function getInitializeAccount2InstructionDataEncoder(): Encoder<InitializeAccount2InstructionDataArgs> {
  68. return transformEncoder(
  69. getStructEncoder([
  70. ['discriminator', getU8Encoder()],
  71. ['owner', getAddressEncoder()],
  72. ]),
  73. (value) => ({ ...value, discriminator: INITIALIZE_ACCOUNT2_DISCRIMINATOR })
  74. );
  75. }
  76. export function getInitializeAccount2InstructionDataDecoder(): Decoder<InitializeAccount2InstructionData> {
  77. return getStructDecoder([
  78. ['discriminator', getU8Decoder()],
  79. ['owner', getAddressDecoder()],
  80. ]);
  81. }
  82. export function getInitializeAccount2InstructionDataCodec(): Codec<
  83. InitializeAccount2InstructionDataArgs,
  84. InitializeAccount2InstructionData
  85. > {
  86. return combineCodec(
  87. getInitializeAccount2InstructionDataEncoder(),
  88. getInitializeAccount2InstructionDataDecoder()
  89. );
  90. }
  91. export type InitializeAccount2Input<
  92. TAccountAccount extends string = string,
  93. TAccountMint extends string = string,
  94. TAccountRent extends string = string,
  95. > = {
  96. /** The account to initialize. */
  97. account: Address<TAccountAccount>;
  98. /** The mint this account will be associated with. */
  99. mint: Address<TAccountMint>;
  100. /** Rent sysvar. */
  101. rent?: Address<TAccountRent>;
  102. owner: InitializeAccount2InstructionDataArgs['owner'];
  103. };
  104. export function getInitializeAccount2Instruction<
  105. TAccountAccount extends string,
  106. TAccountMint extends string,
  107. TAccountRent extends string,
  108. TProgramAddress extends Address = typeof TOKEN_PROGRAM_ADDRESS,
  109. >(
  110. input: InitializeAccount2Input<TAccountAccount, TAccountMint, TAccountRent>,
  111. config?: { programAddress?: TProgramAddress }
  112. ): InitializeAccount2Instruction<
  113. TProgramAddress,
  114. TAccountAccount,
  115. TAccountMint,
  116. TAccountRent
  117. > {
  118. // Program address.
  119. const programAddress = config?.programAddress ?? TOKEN_PROGRAM_ADDRESS;
  120. // Original accounts.
  121. const originalAccounts = {
  122. account: { value: input.account ?? null, isWritable: true },
  123. mint: { value: input.mint ?? null, isWritable: false },
  124. rent: { value: input.rent ?? null, isWritable: false },
  125. };
  126. const accounts = originalAccounts as Record<
  127. keyof typeof originalAccounts,
  128. ResolvedAccount
  129. >;
  130. // Original args.
  131. const args = { ...input };
  132. // Resolve default values.
  133. if (!accounts.rent.value) {
  134. accounts.rent.value =
  135. 'SysvarRent111111111111111111111111111111111' as Address<'SysvarRent111111111111111111111111111111111'>;
  136. }
  137. const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  138. const instruction = {
  139. accounts: [
  140. getAccountMeta(accounts.account),
  141. getAccountMeta(accounts.mint),
  142. getAccountMeta(accounts.rent),
  143. ],
  144. programAddress,
  145. data: getInitializeAccount2InstructionDataEncoder().encode(
  146. args as InitializeAccount2InstructionDataArgs
  147. ),
  148. } as InitializeAccount2Instruction<
  149. TProgramAddress,
  150. TAccountAccount,
  151. TAccountMint,
  152. TAccountRent
  153. >;
  154. return instruction;
  155. }
  156. export type ParsedInitializeAccount2Instruction<
  157. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  158. TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
  159. > = {
  160. programAddress: Address<TProgram>;
  161. accounts: {
  162. /** The account to initialize. */
  163. account: TAccountMetas[0];
  164. /** The mint this account will be associated with. */
  165. mint: TAccountMetas[1];
  166. /** Rent sysvar. */
  167. rent: TAccountMetas[2];
  168. };
  169. data: InitializeAccount2InstructionData;
  170. };
  171. export function parseInitializeAccount2Instruction<
  172. TProgram extends string,
  173. TAccountMetas extends readonly IAccountMeta[],
  174. >(
  175. instruction: IInstruction<TProgram> &
  176. IInstructionWithAccounts<TAccountMetas> &
  177. IInstructionWithData<Uint8Array>
  178. ): ParsedInitializeAccount2Instruction<TProgram, TAccountMetas> {
  179. if (instruction.accounts.length < 3) {
  180. // TODO: Coded error.
  181. throw new Error('Not enough accounts');
  182. }
  183. let accountIndex = 0;
  184. const getNextAccount = () => {
  185. const accountMeta = instruction.accounts![accountIndex]!;
  186. accountIndex += 1;
  187. return accountMeta;
  188. };
  189. return {
  190. programAddress: instruction.programAddress,
  191. accounts: {
  192. account: getNextAccount(),
  193. mint: getNextAccount(),
  194. rent: getNextAccount(),
  195. },
  196. data: getInitializeAccount2InstructionDataDecoder().decode(
  197. instruction.data
  198. ),
  199. };
  200. }