syncNative.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 Address,
  16. type Codec,
  17. type Decoder,
  18. type Encoder,
  19. type IAccountMeta,
  20. type IInstruction,
  21. type IInstructionWithAccounts,
  22. type IInstructionWithData,
  23. type WritableAccount,
  24. } from '@solana/web3.js';
  25. import { TOKEN_PROGRAM_ADDRESS } from '../programs';
  26. import { getAccountMetaFactory, type ResolvedAccount } from '../shared';
  27. export const SYNC_NATIVE_DISCRIMINATOR = 17;
  28. export function getSyncNativeDiscriminatorBytes() {
  29. return getU8Encoder().encode(SYNC_NATIVE_DISCRIMINATOR);
  30. }
  31. export type SyncNativeInstruction<
  32. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  33. TAccountAccount extends string | IAccountMeta<string> = string,
  34. TRemainingAccounts extends readonly IAccountMeta<string>[] = [],
  35. > = IInstruction<TProgram> &
  36. IInstructionWithData<Uint8Array> &
  37. IInstructionWithAccounts<
  38. [
  39. TAccountAccount extends string
  40. ? WritableAccount<TAccountAccount>
  41. : TAccountAccount,
  42. ...TRemainingAccounts,
  43. ]
  44. >;
  45. export type SyncNativeInstructionData = { discriminator: number };
  46. export type SyncNativeInstructionDataArgs = {};
  47. export function getSyncNativeInstructionDataEncoder(): Encoder<SyncNativeInstructionDataArgs> {
  48. return transformEncoder(
  49. getStructEncoder([['discriminator', getU8Encoder()]]),
  50. (value) => ({ ...value, discriminator: SYNC_NATIVE_DISCRIMINATOR })
  51. );
  52. }
  53. export function getSyncNativeInstructionDataDecoder(): Decoder<SyncNativeInstructionData> {
  54. return getStructDecoder([['discriminator', getU8Decoder()]]);
  55. }
  56. export function getSyncNativeInstructionDataCodec(): Codec<
  57. SyncNativeInstructionDataArgs,
  58. SyncNativeInstructionData
  59. > {
  60. return combineCodec(
  61. getSyncNativeInstructionDataEncoder(),
  62. getSyncNativeInstructionDataDecoder()
  63. );
  64. }
  65. export type SyncNativeInput<TAccountAccount extends string = string> = {
  66. /** The native token account to sync with its underlying lamports. */
  67. account: Address<TAccountAccount>;
  68. };
  69. export function getSyncNativeInstruction<
  70. TAccountAccount extends string,
  71. TProgramAddress extends Address = typeof TOKEN_PROGRAM_ADDRESS,
  72. >(
  73. input: SyncNativeInput<TAccountAccount>,
  74. config?: { programAddress?: TProgramAddress }
  75. ): SyncNativeInstruction<TProgramAddress, TAccountAccount> {
  76. // Program address.
  77. const programAddress = config?.programAddress ?? TOKEN_PROGRAM_ADDRESS;
  78. // Original accounts.
  79. const originalAccounts = {
  80. account: { value: input.account ?? null, isWritable: true },
  81. };
  82. const accounts = originalAccounts as Record<
  83. keyof typeof originalAccounts,
  84. ResolvedAccount
  85. >;
  86. const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  87. const instruction = {
  88. accounts: [getAccountMeta(accounts.account)],
  89. programAddress,
  90. data: getSyncNativeInstructionDataEncoder().encode({}),
  91. } as SyncNativeInstruction<TProgramAddress, TAccountAccount>;
  92. return instruction;
  93. }
  94. export type ParsedSyncNativeInstruction<
  95. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  96. TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
  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 IAccountMeta[],
  108. >(
  109. instruction: IInstruction<TProgram> &
  110. IInstructionWithAccounts<TAccountMetas> &
  111. IInstructionWithData<Uint8Array>
  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![accountIndex]!;
  120. accountIndex += 1;
  121. return accountMeta;
  122. };
  123. return {
  124. programAddress: instruction.programAddress,
  125. accounts: {
  126. account: getNextAccount(),
  127. },
  128. data: getSyncNativeInstructionDataDecoder().decode(instruction.data),
  129. };
  130. }