createAccount.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. BASE_ACCOUNT_SIZE,
  10. combineCodec,
  11. getAddressDecoder,
  12. getAddressEncoder,
  13. getLamportsDecoder,
  14. getLamportsEncoder,
  15. getStructDecoder,
  16. getStructEncoder,
  17. getU32Decoder,
  18. getU32Encoder,
  19. getU64Decoder,
  20. getU64Encoder,
  21. transformEncoder,
  22. type Address,
  23. type Codec,
  24. type Decoder,
  25. type Encoder,
  26. type IAccountMeta,
  27. type IAccountSignerMeta,
  28. type IInstruction,
  29. type IInstructionWithAccounts,
  30. type IInstructionWithData,
  31. type LamportsUnsafeBeyond2Pow53Minus1,
  32. type TransactionSigner,
  33. type WritableSignerAccount,
  34. } from '@solana/web3.js';
  35. import { SYSTEM_PROGRAM_ADDRESS } from '../programs';
  36. import {
  37. getAccountMetaFactory,
  38. type IInstructionWithByteDelta,
  39. type ResolvedAccount,
  40. } from '../shared';
  41. export const CREATE_ACCOUNT_DISCRIMINATOR = 0;
  42. export function getCreateAccountDiscriminatorBytes() {
  43. return getU32Encoder().encode(CREATE_ACCOUNT_DISCRIMINATOR);
  44. }
  45. export type CreateAccountInstruction<
  46. TProgram extends string = typeof SYSTEM_PROGRAM_ADDRESS,
  47. TAccountPayer extends string | IAccountMeta<string> = string,
  48. TAccountNewAccount extends string | IAccountMeta<string> = string,
  49. TRemainingAccounts extends readonly IAccountMeta<string>[] = [],
  50. > = IInstruction<TProgram> &
  51. IInstructionWithData<Uint8Array> &
  52. IInstructionWithAccounts<
  53. [
  54. TAccountPayer extends string
  55. ? WritableSignerAccount<TAccountPayer> &
  56. IAccountSignerMeta<TAccountPayer>
  57. : TAccountPayer,
  58. TAccountNewAccount extends string
  59. ? WritableSignerAccount<TAccountNewAccount> &
  60. IAccountSignerMeta<TAccountNewAccount>
  61. : TAccountNewAccount,
  62. ...TRemainingAccounts,
  63. ]
  64. >;
  65. export type CreateAccountInstructionData = {
  66. discriminator: number;
  67. lamports: LamportsUnsafeBeyond2Pow53Minus1;
  68. space: bigint;
  69. programAddress: Address;
  70. };
  71. export type CreateAccountInstructionDataArgs = {
  72. lamports: LamportsUnsafeBeyond2Pow53Minus1;
  73. space: number | bigint;
  74. programAddress: Address;
  75. };
  76. export function getCreateAccountInstructionDataEncoder(): Encoder<CreateAccountInstructionDataArgs> {
  77. return transformEncoder(
  78. getStructEncoder([
  79. ['discriminator', getU32Encoder()],
  80. ['lamports', getLamportsEncoder(getU64Encoder())],
  81. ['space', getU64Encoder()],
  82. ['programAddress', getAddressEncoder()],
  83. ]),
  84. (value) => ({ ...value, discriminator: CREATE_ACCOUNT_DISCRIMINATOR })
  85. );
  86. }
  87. export function getCreateAccountInstructionDataDecoder(): Decoder<CreateAccountInstructionData> {
  88. return getStructDecoder([
  89. ['discriminator', getU32Decoder()],
  90. ['lamports', getLamportsDecoder(getU64Decoder())],
  91. ['space', getU64Decoder()],
  92. ['programAddress', getAddressDecoder()],
  93. ]);
  94. }
  95. export function getCreateAccountInstructionDataCodec(): Codec<
  96. CreateAccountInstructionDataArgs,
  97. CreateAccountInstructionData
  98. > {
  99. return combineCodec(
  100. getCreateAccountInstructionDataEncoder(),
  101. getCreateAccountInstructionDataDecoder()
  102. );
  103. }
  104. export type CreateAccountInput<
  105. TAccountPayer extends string = string,
  106. TAccountNewAccount extends string = string,
  107. > = {
  108. payer: TransactionSigner<TAccountPayer>;
  109. newAccount: TransactionSigner<TAccountNewAccount>;
  110. lamports: CreateAccountInstructionDataArgs['lamports'];
  111. space: CreateAccountInstructionDataArgs['space'];
  112. programAddress: CreateAccountInstructionDataArgs['programAddress'];
  113. };
  114. export function getCreateAccountInstruction<
  115. TAccountPayer extends string,
  116. TAccountNewAccount extends string,
  117. >(
  118. input: CreateAccountInput<TAccountPayer, TAccountNewAccount>
  119. ): CreateAccountInstruction<
  120. typeof SYSTEM_PROGRAM_ADDRESS,
  121. TAccountPayer,
  122. TAccountNewAccount
  123. > &
  124. IInstructionWithByteDelta {
  125. // Program address.
  126. const programAddress = SYSTEM_PROGRAM_ADDRESS;
  127. // Original accounts.
  128. const originalAccounts = {
  129. payer: { value: input.payer ?? null, isWritable: true },
  130. newAccount: { value: input.newAccount ?? null, isWritable: true },
  131. };
  132. const accounts = originalAccounts as Record<
  133. keyof typeof originalAccounts,
  134. ResolvedAccount
  135. >;
  136. // Original args.
  137. const args = { ...input };
  138. // Bytes created or reallocated by the instruction.
  139. const byteDelta: number = [Number(args.space) + BASE_ACCOUNT_SIZE].reduce(
  140. (a, b) => a + b,
  141. 0
  142. );
  143. const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  144. const instruction = {
  145. accounts: [
  146. getAccountMeta(accounts.payer),
  147. getAccountMeta(accounts.newAccount),
  148. ],
  149. programAddress,
  150. data: getCreateAccountInstructionDataEncoder().encode(
  151. args as CreateAccountInstructionDataArgs
  152. ),
  153. } as CreateAccountInstruction<
  154. typeof SYSTEM_PROGRAM_ADDRESS,
  155. TAccountPayer,
  156. TAccountNewAccount
  157. >;
  158. return Object.freeze({ ...instruction, byteDelta });
  159. }
  160. export type ParsedCreateAccountInstruction<
  161. TProgram extends string = typeof SYSTEM_PROGRAM_ADDRESS,
  162. TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
  163. > = {
  164. programAddress: Address<TProgram>;
  165. accounts: {
  166. payer: TAccountMetas[0];
  167. newAccount: TAccountMetas[1];
  168. };
  169. data: CreateAccountInstructionData;
  170. };
  171. export function parseCreateAccountInstruction<
  172. TProgram extends string,
  173. TAccountMetas extends readonly IAccountMeta[],
  174. >(
  175. instruction: IInstruction<TProgram> &
  176. IInstructionWithAccounts<TAccountMetas> &
  177. IInstructionWithData<Uint8Array>
  178. ): ParsedCreateAccountInstruction<TProgram, TAccountMetas> {
  179. if (instruction.accounts.length < 2) {
  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. payer: getNextAccount(),
  193. newAccount: getNextAccount(),
  194. },
  195. data: getCreateAccountInstructionDataDecoder().decode(instruction.data),
  196. };
  197. }