syncNative.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 ReadonlyUint8Array,
  24. type WritableAccount,
  25. } from '@solana/kit';
  26. import { TOKEN_PROGRAM_ADDRESS } from '../programs';
  27. import { getAccountMetaFactory, type ResolvedAccount } from '../shared';
  28. export const SYNC_NATIVE_DISCRIMINATOR = 17;
  29. export function getSyncNativeDiscriminatorBytes() {
  30. return getU8Encoder().encode(SYNC_NATIVE_DISCRIMINATOR);
  31. }
  32. export type SyncNativeInstruction<
  33. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  34. TAccountAccount extends string | AccountMeta<string> = string,
  35. TRemainingAccounts extends readonly AccountMeta<string>[] = [],
  36. > = Instruction<TProgram> &
  37. InstructionWithData<ReadonlyUint8Array> &
  38. InstructionWithAccounts<
  39. [
  40. TAccountAccount extends string
  41. ? WritableAccount<TAccountAccount>
  42. : TAccountAccount,
  43. ...TRemainingAccounts,
  44. ]
  45. >;
  46. export type SyncNativeInstructionData = { discriminator: number };
  47. export type SyncNativeInstructionDataArgs = {};
  48. export function getSyncNativeInstructionDataEncoder(): FixedSizeEncoder<SyncNativeInstructionDataArgs> {
  49. return transformEncoder(
  50. getStructEncoder([['discriminator', getU8Encoder()]]),
  51. (value) => ({ ...value, discriminator: SYNC_NATIVE_DISCRIMINATOR })
  52. );
  53. }
  54. export function getSyncNativeInstructionDataDecoder(): FixedSizeDecoder<SyncNativeInstructionData> {
  55. return getStructDecoder([['discriminator', getU8Decoder()]]);
  56. }
  57. export function getSyncNativeInstructionDataCodec(): FixedSizeCodec<
  58. SyncNativeInstructionDataArgs,
  59. SyncNativeInstructionData
  60. > {
  61. return combineCodec(
  62. getSyncNativeInstructionDataEncoder(),
  63. getSyncNativeInstructionDataDecoder()
  64. );
  65. }
  66. export type SyncNativeInput<TAccountAccount extends string = string> = {
  67. /** The native token account to sync with its underlying lamports. */
  68. account: Address<TAccountAccount>;
  69. };
  70. export function getSyncNativeInstruction<
  71. TAccountAccount extends string,
  72. TProgramAddress extends Address = typeof TOKEN_PROGRAM_ADDRESS,
  73. >(
  74. input: SyncNativeInput<TAccountAccount>,
  75. config?: { programAddress?: TProgramAddress }
  76. ): SyncNativeInstruction<TProgramAddress, TAccountAccount> {
  77. // Program address.
  78. const programAddress = config?.programAddress ?? TOKEN_PROGRAM_ADDRESS;
  79. // Original accounts.
  80. const originalAccounts = {
  81. account: { value: input.account ?? null, isWritable: true },
  82. };
  83. const accounts = originalAccounts as Record<
  84. keyof typeof originalAccounts,
  85. ResolvedAccount
  86. >;
  87. const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  88. return Object.freeze({
  89. accounts: [getAccountMeta(accounts.account)],
  90. data: getSyncNativeInstructionDataEncoder().encode({}),
  91. programAddress,
  92. } as SyncNativeInstruction<TProgramAddress, TAccountAccount>);
  93. }
  94. export type ParsedSyncNativeInstruction<
  95. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  96. TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
  97. > = {
  98. programAddress: Address<TProgram>;
  99. accounts: {
  100. /** The native token account to sync with its underlying lamports. */
  101. account: TAccountMetas[0];
  102. };
  103. data: SyncNativeInstructionData;
  104. };
  105. export function parseSyncNativeInstruction<
  106. TProgram extends string,
  107. TAccountMetas extends readonly AccountMeta[],
  108. >(
  109. instruction: Instruction<TProgram> &
  110. InstructionWithAccounts<TAccountMetas> &
  111. InstructionWithData<ReadonlyUint8Array>
  112. ): ParsedSyncNativeInstruction<TProgram, TAccountMetas> {
  113. if (instruction.accounts.length < 1) {
  114. // TODO: Coded error.
  115. throw new Error('Not enough accounts');
  116. }
  117. let accountIndex = 0;
  118. const getNextAccount = () => {
  119. const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!;
  120. accountIndex += 1;
  121. return accountMeta;
  122. };
  123. return {
  124. programAddress: instruction.programAddress,
  125. accounts: { account: getNextAccount() },
  126. data: getSyncNativeInstructionDataDecoder().decode(instruction.data),
  127. };
  128. }