createAccount.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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/metaplex-foundation/kinobi
  7. */
  8. import { BASE_ACCOUNT_SIZE } from '@solana/accounts';
  9. import {
  10. Address,
  11. getAddressDecoder,
  12. getAddressEncoder,
  13. } from '@solana/addresses';
  14. import {
  15. Codec,
  16. Decoder,
  17. Encoder,
  18. combineCodec,
  19. mapEncoder,
  20. } from '@solana/codecs-core';
  21. import {
  22. getStructDecoder,
  23. getStructEncoder,
  24. } from '@solana/codecs-data-structures';
  25. import {
  26. getU32Decoder,
  27. getU32Encoder,
  28. getU64Decoder,
  29. getU64Encoder,
  30. } from '@solana/codecs-numbers';
  31. import {
  32. AccountRole,
  33. IAccountMeta,
  34. IInstruction,
  35. IInstructionWithAccounts,
  36. IInstructionWithData,
  37. WritableSignerAccount,
  38. } from '@solana/instructions';
  39. import { IAccountSignerMeta, TransactionSigner } from '@solana/signers';
  40. import {
  41. IInstructionWithByteDelta,
  42. ResolvedAccount,
  43. accountMetaWithDefault,
  44. getAccountMetasWithSigners,
  45. } from '../shared';
  46. export type CreateAccountInstruction<
  47. TProgram extends string = '11111111111111111111111111111111',
  48. TAccountPayer extends string | IAccountMeta<string> = string,
  49. TAccountNewAccount extends string | IAccountMeta<string> = string,
  50. TRemainingAccounts extends Array<IAccountMeta<string>> = []
  51. > = IInstruction<TProgram> &
  52. IInstructionWithData<Uint8Array> &
  53. IInstructionWithAccounts<
  54. [
  55. TAccountPayer extends string
  56. ? WritableSignerAccount<TAccountPayer>
  57. : TAccountPayer,
  58. TAccountNewAccount extends string
  59. ? WritableSignerAccount<TAccountNewAccount>
  60. : TAccountNewAccount,
  61. ...TRemainingAccounts
  62. ]
  63. >;
  64. export type CreateAccountInstructionWithSigners<
  65. TProgram extends string = '11111111111111111111111111111111',
  66. TAccountPayer extends string | IAccountMeta<string> = string,
  67. TAccountNewAccount extends string | IAccountMeta<string> = string,
  68. TRemainingAccounts extends Array<IAccountMeta<string>> = []
  69. > = IInstruction<TProgram> &
  70. IInstructionWithData<Uint8Array> &
  71. IInstructionWithAccounts<
  72. [
  73. TAccountPayer extends string
  74. ? WritableSignerAccount<TAccountPayer> &
  75. IAccountSignerMeta<TAccountPayer>
  76. : TAccountPayer,
  77. TAccountNewAccount extends string
  78. ? WritableSignerAccount<TAccountNewAccount> &
  79. IAccountSignerMeta<TAccountNewAccount>
  80. : TAccountNewAccount,
  81. ...TRemainingAccounts
  82. ]
  83. >;
  84. export type CreateAccountInstructionData = {
  85. discriminator: number;
  86. lamports: bigint;
  87. space: bigint;
  88. programAddress: Address;
  89. };
  90. export type CreateAccountInstructionDataArgs = {
  91. lamports: number | bigint;
  92. space: number | bigint;
  93. programAddress: Address;
  94. };
  95. export function getCreateAccountInstructionDataEncoder(): Encoder<CreateAccountInstructionDataArgs> {
  96. return mapEncoder(
  97. getStructEncoder([
  98. ['discriminator', getU32Encoder()],
  99. ['lamports', getU64Encoder()],
  100. ['space', getU64Encoder()],
  101. ['programAddress', getAddressEncoder()],
  102. ]),
  103. (value) => ({ ...value, discriminator: 0 })
  104. );
  105. }
  106. export function getCreateAccountInstructionDataDecoder(): Decoder<CreateAccountInstructionData> {
  107. return getStructDecoder([
  108. ['discriminator', getU32Decoder()],
  109. ['lamports', getU64Decoder()],
  110. ['space', getU64Decoder()],
  111. ['programAddress', getAddressDecoder()],
  112. ]);
  113. }
  114. export function getCreateAccountInstructionDataCodec(): Codec<
  115. CreateAccountInstructionDataArgs,
  116. CreateAccountInstructionData
  117. > {
  118. return combineCodec(
  119. getCreateAccountInstructionDataEncoder(),
  120. getCreateAccountInstructionDataDecoder()
  121. );
  122. }
  123. export type CreateAccountInput<
  124. TAccountPayer extends string,
  125. TAccountNewAccount extends string
  126. > = {
  127. payer: Address<TAccountPayer>;
  128. newAccount: Address<TAccountNewAccount>;
  129. lamports: CreateAccountInstructionDataArgs['lamports'];
  130. space: CreateAccountInstructionDataArgs['space'];
  131. programAddress: CreateAccountInstructionDataArgs['programAddress'];
  132. };
  133. export type CreateAccountInputWithSigners<
  134. TAccountPayer extends string,
  135. TAccountNewAccount extends string
  136. > = {
  137. payer: TransactionSigner<TAccountPayer>;
  138. newAccount: TransactionSigner<TAccountNewAccount>;
  139. lamports: CreateAccountInstructionDataArgs['lamports'];
  140. space: CreateAccountInstructionDataArgs['space'];
  141. programAddress: CreateAccountInstructionDataArgs['programAddress'];
  142. };
  143. export function getCreateAccountInstruction<
  144. TAccountPayer extends string,
  145. TAccountNewAccount extends string,
  146. TProgram extends string = '11111111111111111111111111111111'
  147. >(
  148. input: CreateAccountInputWithSigners<TAccountPayer, TAccountNewAccount>
  149. ): CreateAccountInstructionWithSigners<
  150. TProgram,
  151. TAccountPayer,
  152. TAccountNewAccount
  153. > &
  154. IInstructionWithByteDelta;
  155. export function getCreateAccountInstruction<
  156. TAccountPayer extends string,
  157. TAccountNewAccount extends string,
  158. TProgram extends string = '11111111111111111111111111111111'
  159. >(
  160. input: CreateAccountInput<TAccountPayer, TAccountNewAccount>
  161. ): CreateAccountInstruction<TProgram, TAccountPayer, TAccountNewAccount> &
  162. IInstructionWithByteDelta;
  163. export function getCreateAccountInstruction<
  164. TAccountPayer extends string,
  165. TAccountNewAccount extends string,
  166. TProgram extends string = '11111111111111111111111111111111'
  167. >(
  168. input: CreateAccountInput<TAccountPayer, TAccountNewAccount>
  169. ): IInstruction & IInstructionWithByteDelta {
  170. // Program address.
  171. const programAddress =
  172. '11111111111111111111111111111111' as Address<'11111111111111111111111111111111'>;
  173. // Original accounts.
  174. type AccountMetas = Parameters<
  175. typeof getCreateAccountInstructionRaw<
  176. TProgram,
  177. TAccountPayer,
  178. TAccountNewAccount
  179. >
  180. >[0];
  181. const accounts: Record<keyof AccountMetas, ResolvedAccount> = {
  182. payer: { value: input.payer ?? null, isWritable: true },
  183. newAccount: { value: input.newAccount ?? null, isWritable: true },
  184. };
  185. // Original args.
  186. const args = { ...input };
  187. // Bytes created or reallocated by the instruction.
  188. const byteDelta: number = [Number(args.space) + BASE_ACCOUNT_SIZE].reduce(
  189. (a, b) => a + b,
  190. 0
  191. );
  192. // Get account metas and signers.
  193. const accountMetas = getAccountMetasWithSigners(
  194. accounts,
  195. 'programId',
  196. programAddress
  197. );
  198. const instruction = getCreateAccountInstructionRaw(
  199. accountMetas as Record<keyof AccountMetas, IAccountMeta>,
  200. args as CreateAccountInstructionDataArgs,
  201. programAddress
  202. );
  203. return Object.freeze({ ...instruction, byteDelta });
  204. }
  205. export function getCreateAccountInstructionRaw<
  206. TProgram extends string = '11111111111111111111111111111111',
  207. TAccountPayer extends string | IAccountMeta<string> = string,
  208. TAccountNewAccount extends string | IAccountMeta<string> = string,
  209. TRemainingAccounts extends Array<IAccountMeta<string>> = []
  210. >(
  211. accounts: {
  212. payer: TAccountPayer extends string
  213. ? Address<TAccountPayer>
  214. : TAccountPayer;
  215. newAccount: TAccountNewAccount extends string
  216. ? Address<TAccountNewAccount>
  217. : TAccountNewAccount;
  218. },
  219. args: CreateAccountInstructionDataArgs,
  220. programAddress: Address<TProgram> = '11111111111111111111111111111111' as Address<TProgram>,
  221. remainingAccounts?: TRemainingAccounts
  222. ) {
  223. return {
  224. accounts: [
  225. accountMetaWithDefault(accounts.payer, AccountRole.WRITABLE_SIGNER),
  226. accountMetaWithDefault(accounts.newAccount, AccountRole.WRITABLE_SIGNER),
  227. ...(remainingAccounts ?? []),
  228. ],
  229. data: getCreateAccountInstructionDataEncoder().encode(args),
  230. programAddress,
  231. } as CreateAccountInstruction<
  232. TProgram,
  233. TAccountPayer,
  234. TAccountNewAccount,
  235. TRemainingAccounts
  236. >;
  237. }
  238. export type ParsedCreateAccountInstruction<
  239. TProgram extends string = '11111111111111111111111111111111',
  240. TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[]
  241. > = {
  242. programAddress: Address<TProgram>;
  243. accounts: {
  244. payer: TAccountMetas[0];
  245. newAccount: TAccountMetas[1];
  246. };
  247. data: CreateAccountInstructionData;
  248. };
  249. export function parseCreateAccountInstruction<
  250. TProgram extends string,
  251. TAccountMetas extends readonly IAccountMeta[]
  252. >(
  253. instruction: IInstruction<TProgram> &
  254. IInstructionWithAccounts<TAccountMetas> &
  255. IInstructionWithData<Uint8Array>
  256. ): ParsedCreateAccountInstruction<TProgram, TAccountMetas> {
  257. if (instruction.accounts.length < 2) {
  258. // TODO: Coded error.
  259. throw new Error('Not enough accounts');
  260. }
  261. let accountIndex = 0;
  262. const getNextAccount = () => {
  263. const accountMeta = instruction.accounts![accountIndex]!;
  264. accountIndex += 1;
  265. return accountMeta;
  266. };
  267. return {
  268. programAddress: instruction.programAddress,
  269. accounts: {
  270. payer: getNextAccount(),
  271. newAccount: getNextAccount(),
  272. },
  273. data: getCreateAccountInstructionDataDecoder().decode(instruction.data),
  274. };
  275. }