amountToUiAmount.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. getStructDecoder,
  11. getStructEncoder,
  12. getU64Decoder,
  13. getU64Encoder,
  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. } from '@solana/web3.js';
  27. import { TOKEN_PROGRAM_ADDRESS } from '../programs';
  28. import { getAccountMetaFactory, type ResolvedAccount } from '../shared';
  29. export type AmountToUiAmountInstruction<
  30. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  31. TAccountMint extends string | IAccountMeta<string> = string,
  32. TRemainingAccounts extends readonly IAccountMeta<string>[] = [],
  33. > = IInstruction<TProgram> &
  34. IInstructionWithData<Uint8Array> &
  35. IInstructionWithAccounts<
  36. [
  37. TAccountMint extends string
  38. ? ReadonlyAccount<TAccountMint>
  39. : TAccountMint,
  40. ...TRemainingAccounts,
  41. ]
  42. >;
  43. export type AmountToUiAmountInstructionData = {
  44. discriminator: number;
  45. /** The amount of tokens to reformat. */
  46. amount: bigint;
  47. };
  48. export type AmountToUiAmountInstructionDataArgs = {
  49. /** The amount of tokens to reformat. */
  50. amount: number | bigint;
  51. };
  52. export function getAmountToUiAmountInstructionDataEncoder(): Encoder<AmountToUiAmountInstructionDataArgs> {
  53. return transformEncoder(
  54. getStructEncoder([
  55. ['discriminator', getU8Encoder()],
  56. ['amount', getU64Encoder()],
  57. ]),
  58. (value) => ({ ...value, discriminator: 23 })
  59. );
  60. }
  61. export function getAmountToUiAmountInstructionDataDecoder(): Decoder<AmountToUiAmountInstructionData> {
  62. return getStructDecoder([
  63. ['discriminator', getU8Decoder()],
  64. ['amount', getU64Decoder()],
  65. ]);
  66. }
  67. export function getAmountToUiAmountInstructionDataCodec(): Codec<
  68. AmountToUiAmountInstructionDataArgs,
  69. AmountToUiAmountInstructionData
  70. > {
  71. return combineCodec(
  72. getAmountToUiAmountInstructionDataEncoder(),
  73. getAmountToUiAmountInstructionDataDecoder()
  74. );
  75. }
  76. export type AmountToUiAmountInput<TAccountMint extends string = string> = {
  77. /** The mint to calculate for. */
  78. mint: Address<TAccountMint>;
  79. amount: AmountToUiAmountInstructionDataArgs['amount'];
  80. };
  81. export function getAmountToUiAmountInstruction<TAccountMint extends string>(
  82. input: AmountToUiAmountInput<TAccountMint>
  83. ): AmountToUiAmountInstruction<typeof TOKEN_PROGRAM_ADDRESS, TAccountMint> {
  84. // Program address.
  85. const programAddress = TOKEN_PROGRAM_ADDRESS;
  86. // Original accounts.
  87. const originalAccounts = {
  88. mint: { value: input.mint ?? null, isWritable: false },
  89. };
  90. const accounts = originalAccounts as Record<
  91. keyof typeof originalAccounts,
  92. ResolvedAccount
  93. >;
  94. // Original args.
  95. const args = { ...input };
  96. const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  97. const instruction = {
  98. accounts: [getAccountMeta(accounts.mint)],
  99. programAddress,
  100. data: getAmountToUiAmountInstructionDataEncoder().encode(
  101. args as AmountToUiAmountInstructionDataArgs
  102. ),
  103. } as AmountToUiAmountInstruction<typeof TOKEN_PROGRAM_ADDRESS, TAccountMint>;
  104. return instruction;
  105. }
  106. export type ParsedAmountToUiAmountInstruction<
  107. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  108. TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
  109. > = {
  110. programAddress: Address<TProgram>;
  111. accounts: {
  112. /** The mint to calculate for. */
  113. mint: TAccountMetas[0];
  114. };
  115. data: AmountToUiAmountInstructionData;
  116. };
  117. export function parseAmountToUiAmountInstruction<
  118. TProgram extends string,
  119. TAccountMetas extends readonly IAccountMeta[],
  120. >(
  121. instruction: IInstruction<TProgram> &
  122. IInstructionWithAccounts<TAccountMetas> &
  123. IInstructionWithData<Uint8Array>
  124. ): ParsedAmountToUiAmountInstruction<TProgram, TAccountMetas> {
  125. if (instruction.accounts.length < 1) {
  126. // TODO: Coded error.
  127. throw new Error('Not enough accounts');
  128. }
  129. let accountIndex = 0;
  130. const getNextAccount = () => {
  131. const accountMeta = instruction.accounts![accountIndex]!;
  132. accountIndex += 1;
  133. return accountMeta;
  134. };
  135. return {
  136. programAddress: instruction.programAddress,
  137. accounts: {
  138. mint: getNextAccount(),
  139. },
  140. data: getAmountToUiAmountInstructionDataDecoder().decode(instruction.data),
  141. };
  142. }