createAccountWithSeed.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. addDecoderSizePrefix,
  10. addEncoderSizePrefix,
  11. combineCodec,
  12. getAddressDecoder,
  13. getAddressEncoder,
  14. getStructDecoder,
  15. getStructEncoder,
  16. getU32Decoder,
  17. getU32Encoder,
  18. getU64Decoder,
  19. getU64Encoder,
  20. getUtf8Decoder,
  21. getUtf8Encoder,
  22. transformEncoder,
  23. type Address,
  24. type Codec,
  25. type Decoder,
  26. type Encoder,
  27. type IAccountMeta,
  28. type IAccountSignerMeta,
  29. type IInstruction,
  30. type IInstructionWithAccounts,
  31. type IInstructionWithData,
  32. type ReadonlySignerAccount,
  33. type TransactionSigner,
  34. type WritableAccount,
  35. type WritableSignerAccount,
  36. } from '@solana/web3.js';
  37. import { SYSTEM_PROGRAM_ADDRESS } from '../programs';
  38. import { getAccountMetaFactory, type ResolvedAccount } from '../shared';
  39. export const CREATE_ACCOUNT_WITH_SEED_DISCRIMINATOR = 3;
  40. export function getCreateAccountWithSeedDiscriminatorBytes() {
  41. return getU32Encoder().encode(CREATE_ACCOUNT_WITH_SEED_DISCRIMINATOR);
  42. }
  43. export type CreateAccountWithSeedInstruction<
  44. TProgram extends string = typeof SYSTEM_PROGRAM_ADDRESS,
  45. TAccountPayer extends string | IAccountMeta<string> = string,
  46. TAccountNewAccount extends string | IAccountMeta<string> = string,
  47. TAccountBaseAccount extends string | IAccountMeta<string> = string,
  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. TAccountNewAccount extends string
  58. ? WritableAccount<TAccountNewAccount>
  59. : TAccountNewAccount,
  60. TAccountBaseAccount extends string
  61. ? ReadonlySignerAccount<TAccountBaseAccount> &
  62. IAccountSignerMeta<TAccountBaseAccount>
  63. : TAccountBaseAccount,
  64. ...TRemainingAccounts,
  65. ]
  66. >;
  67. export type CreateAccountWithSeedInstructionData = {
  68. discriminator: number;
  69. base: Address;
  70. seed: string;
  71. amount: bigint;
  72. space: bigint;
  73. programAddress: Address;
  74. };
  75. export type CreateAccountWithSeedInstructionDataArgs = {
  76. base: Address;
  77. seed: string;
  78. amount: number | bigint;
  79. space: number | bigint;
  80. programAddress: Address;
  81. };
  82. export function getCreateAccountWithSeedInstructionDataEncoder(): Encoder<CreateAccountWithSeedInstructionDataArgs> {
  83. return transformEncoder(
  84. getStructEncoder([
  85. ['discriminator', getU32Encoder()],
  86. ['base', getAddressEncoder()],
  87. ['seed', addEncoderSizePrefix(getUtf8Encoder(), getU64Encoder())],
  88. ['amount', getU64Encoder()],
  89. ['space', getU64Encoder()],
  90. ['programAddress', getAddressEncoder()],
  91. ]),
  92. (value) => ({
  93. ...value,
  94. discriminator: CREATE_ACCOUNT_WITH_SEED_DISCRIMINATOR,
  95. })
  96. );
  97. }
  98. export function getCreateAccountWithSeedInstructionDataDecoder(): Decoder<CreateAccountWithSeedInstructionData> {
  99. return getStructDecoder([
  100. ['discriminator', getU32Decoder()],
  101. ['base', getAddressDecoder()],
  102. ['seed', addDecoderSizePrefix(getUtf8Decoder(), getU64Decoder())],
  103. ['amount', getU64Decoder()],
  104. ['space', getU64Decoder()],
  105. ['programAddress', getAddressDecoder()],
  106. ]);
  107. }
  108. export function getCreateAccountWithSeedInstructionDataCodec(): Codec<
  109. CreateAccountWithSeedInstructionDataArgs,
  110. CreateAccountWithSeedInstructionData
  111. > {
  112. return combineCodec(
  113. getCreateAccountWithSeedInstructionDataEncoder(),
  114. getCreateAccountWithSeedInstructionDataDecoder()
  115. );
  116. }
  117. export type CreateAccountWithSeedInput<
  118. TAccountPayer extends string = string,
  119. TAccountNewAccount extends string = string,
  120. TAccountBaseAccount extends string = string,
  121. > = {
  122. payer: TransactionSigner<TAccountPayer>;
  123. newAccount: Address<TAccountNewAccount>;
  124. baseAccount: TransactionSigner<TAccountBaseAccount>;
  125. base: CreateAccountWithSeedInstructionDataArgs['base'];
  126. seed: CreateAccountWithSeedInstructionDataArgs['seed'];
  127. amount: CreateAccountWithSeedInstructionDataArgs['amount'];
  128. space: CreateAccountWithSeedInstructionDataArgs['space'];
  129. programAddress: CreateAccountWithSeedInstructionDataArgs['programAddress'];
  130. };
  131. export function getCreateAccountWithSeedInstruction<
  132. TAccountPayer extends string,
  133. TAccountNewAccount extends string,
  134. TAccountBaseAccount extends string,
  135. TProgramAddress extends Address = typeof SYSTEM_PROGRAM_ADDRESS,
  136. >(
  137. input: CreateAccountWithSeedInput<
  138. TAccountPayer,
  139. TAccountNewAccount,
  140. TAccountBaseAccount
  141. >,
  142. config?: { programAddress?: TProgramAddress }
  143. ): CreateAccountWithSeedInstruction<
  144. TProgramAddress,
  145. TAccountPayer,
  146. TAccountNewAccount,
  147. TAccountBaseAccount
  148. > {
  149. // Program address.
  150. const programAddress = config?.programAddress ?? SYSTEM_PROGRAM_ADDRESS;
  151. // Original accounts.
  152. const originalAccounts = {
  153. payer: { value: input.payer ?? null, isWritable: true },
  154. newAccount: { value: input.newAccount ?? null, isWritable: true },
  155. baseAccount: { value: input.baseAccount ?? null, isWritable: false },
  156. };
  157. const accounts = originalAccounts as Record<
  158. keyof typeof originalAccounts,
  159. ResolvedAccount
  160. >;
  161. // Original args.
  162. const args = { ...input };
  163. const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  164. const instruction = {
  165. accounts: [
  166. getAccountMeta(accounts.payer),
  167. getAccountMeta(accounts.newAccount),
  168. getAccountMeta(accounts.baseAccount),
  169. ],
  170. programAddress,
  171. data: getCreateAccountWithSeedInstructionDataEncoder().encode(
  172. args as CreateAccountWithSeedInstructionDataArgs
  173. ),
  174. } as CreateAccountWithSeedInstruction<
  175. TProgramAddress,
  176. TAccountPayer,
  177. TAccountNewAccount,
  178. TAccountBaseAccount
  179. >;
  180. return instruction;
  181. }
  182. export type ParsedCreateAccountWithSeedInstruction<
  183. TProgram extends string = typeof SYSTEM_PROGRAM_ADDRESS,
  184. TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
  185. > = {
  186. programAddress: Address<TProgram>;
  187. accounts: {
  188. payer: TAccountMetas[0];
  189. newAccount: TAccountMetas[1];
  190. baseAccount: TAccountMetas[2];
  191. };
  192. data: CreateAccountWithSeedInstructionData;
  193. };
  194. export function parseCreateAccountWithSeedInstruction<
  195. TProgram extends string,
  196. TAccountMetas extends readonly IAccountMeta[],
  197. >(
  198. instruction: IInstruction<TProgram> &
  199. IInstructionWithAccounts<TAccountMetas> &
  200. IInstructionWithData<Uint8Array>
  201. ): ParsedCreateAccountWithSeedInstruction<TProgram, TAccountMetas> {
  202. if (instruction.accounts.length < 3) {
  203. // TODO: Coded error.
  204. throw new Error('Not enough accounts');
  205. }
  206. let accountIndex = 0;
  207. const getNextAccount = () => {
  208. const accountMeta = instruction.accounts![accountIndex]!;
  209. accountIndex += 1;
  210. return accountMeta;
  211. };
  212. return {
  213. programAddress: instruction.programAddress,
  214. accounts: {
  215. payer: getNextAccount(),
  216. newAccount: getNextAccount(),
  217. baseAccount: getNextAccount(),
  218. },
  219. data: getCreateAccountWithSeedInstructionDataDecoder().decode(
  220. instruction.data
  221. ),
  222. };
  223. }