getAccountDataSize.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * This code was AUTOGENERATED using the codama library.
  3. * Please DO NOT EDIT THIS FILE, instead use visitors
  4. * to add features, then rerun codama to update it.
  5. *
  6. * @see https://github.com/codama-idl/codama
  7. */
  8. import {
  9. combineCodec,
  10. getStructDecoder,
  11. getStructEncoder,
  12. getU8Decoder,
  13. getU8Encoder,
  14. transformEncoder,
  15. type AccountMeta,
  16. type Address,
  17. type FixedSizeCodec,
  18. type FixedSizeDecoder,
  19. type FixedSizeEncoder,
  20. type Instruction,
  21. type InstructionWithAccounts,
  22. type InstructionWithData,
  23. type ReadonlyAccount,
  24. type ReadonlyUint8Array,
  25. } from '@solana/kit';
  26. import { TOKEN_PROGRAM_ADDRESS } from '../programs';
  27. import { getAccountMetaFactory, type ResolvedAccount } from '../shared';
  28. export const GET_ACCOUNT_DATA_SIZE_DISCRIMINATOR = 21;
  29. export function getGetAccountDataSizeDiscriminatorBytes() {
  30. return getU8Encoder().encode(GET_ACCOUNT_DATA_SIZE_DISCRIMINATOR);
  31. }
  32. export type GetAccountDataSizeInstruction<
  33. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  34. TAccountMint extends string | AccountMeta<string> = string,
  35. TRemainingAccounts extends readonly AccountMeta<string>[] = [],
  36. > = Instruction<TProgram> &
  37. InstructionWithData<ReadonlyUint8Array> &
  38. InstructionWithAccounts<
  39. [
  40. TAccountMint extends string
  41. ? ReadonlyAccount<TAccountMint>
  42. : TAccountMint,
  43. ...TRemainingAccounts,
  44. ]
  45. >;
  46. export type GetAccountDataSizeInstructionData = { discriminator: number };
  47. export type GetAccountDataSizeInstructionDataArgs = {};
  48. export function getGetAccountDataSizeInstructionDataEncoder(): FixedSizeEncoder<GetAccountDataSizeInstructionDataArgs> {
  49. return transformEncoder(
  50. getStructEncoder([['discriminator', getU8Encoder()]]),
  51. (value) => ({
  52. ...value,
  53. discriminator: GET_ACCOUNT_DATA_SIZE_DISCRIMINATOR,
  54. })
  55. );
  56. }
  57. export function getGetAccountDataSizeInstructionDataDecoder(): FixedSizeDecoder<GetAccountDataSizeInstructionData> {
  58. return getStructDecoder([['discriminator', getU8Decoder()]]);
  59. }
  60. export function getGetAccountDataSizeInstructionDataCodec(): FixedSizeCodec<
  61. GetAccountDataSizeInstructionDataArgs,
  62. GetAccountDataSizeInstructionData
  63. > {
  64. return combineCodec(
  65. getGetAccountDataSizeInstructionDataEncoder(),
  66. getGetAccountDataSizeInstructionDataDecoder()
  67. );
  68. }
  69. export type GetAccountDataSizeInput<TAccountMint extends string = string> = {
  70. /** The mint to calculate for. */
  71. mint: Address<TAccountMint>;
  72. };
  73. export function getGetAccountDataSizeInstruction<
  74. TAccountMint extends string,
  75. TProgramAddress extends Address = typeof TOKEN_PROGRAM_ADDRESS,
  76. >(
  77. input: GetAccountDataSizeInput<TAccountMint>,
  78. config?: { programAddress?: TProgramAddress }
  79. ): GetAccountDataSizeInstruction<TProgramAddress, TAccountMint> {
  80. // Program address.
  81. const programAddress = config?.programAddress ?? TOKEN_PROGRAM_ADDRESS;
  82. // Original accounts.
  83. const originalAccounts = {
  84. mint: { value: input.mint ?? null, isWritable: false },
  85. };
  86. const accounts = originalAccounts as Record<
  87. keyof typeof originalAccounts,
  88. ResolvedAccount
  89. >;
  90. const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  91. const instruction = {
  92. accounts: [getAccountMeta(accounts.mint)],
  93. programAddress,
  94. data: getGetAccountDataSizeInstructionDataEncoder().encode({}),
  95. } as GetAccountDataSizeInstruction<TProgramAddress, TAccountMint>;
  96. return instruction;
  97. }
  98. export type ParsedGetAccountDataSizeInstruction<
  99. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  100. TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
  101. > = {
  102. programAddress: Address<TProgram>;
  103. accounts: {
  104. /** The mint to calculate for. */
  105. mint: TAccountMetas[0];
  106. };
  107. data: GetAccountDataSizeInstructionData;
  108. };
  109. export function parseGetAccountDataSizeInstruction<
  110. TProgram extends string,
  111. TAccountMetas extends readonly AccountMeta[],
  112. >(
  113. instruction: Instruction<TProgram> &
  114. InstructionWithAccounts<TAccountMetas> &
  115. InstructionWithData<ReadonlyUint8Array>
  116. ): ParsedGetAccountDataSizeInstruction<TProgram, TAccountMetas> {
  117. if (instruction.accounts.length < 1) {
  118. // TODO: Coded error.
  119. throw new Error('Not enough accounts');
  120. }
  121. let accountIndex = 0;
  122. const getNextAccount = () => {
  123. const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!;
  124. accountIndex += 1;
  125. return accountMeta;
  126. };
  127. return {
  128. programAddress: instruction.programAddress,
  129. accounts: {
  130. mint: getNextAccount(),
  131. },
  132. data: getGetAccountDataSizeInstructionDataDecoder().decode(
  133. instruction.data
  134. ),
  135. };
  136. }