/**
 * This code was AUTOGENERATED using the Codama library.
 * Please DO NOT EDIT THIS FILE, instead use visitors
 * to add features, then rerun Codama to update it.
 *
 * @see https://github.com/codama-idl/codama
 */

import {
  combineCodec,
  fixDecoderSize,
  fixEncoderSize,
  getAddressEncoder,
  getBytesDecoder,
  getBytesEncoder,
  getProgramDerivedAddress,
  getStructDecoder,
  getStructEncoder,
  getU64Decoder,
  getU64Encoder,
  transformEncoder,
  type AccountMeta,
  type AccountSignerMeta,
  type Address,
  type FixedSizeCodec,
  type FixedSizeDecoder,
  type FixedSizeEncoder,
  type Instruction,
  type InstructionWithAccounts,
  type InstructionWithData,
  type ReadonlyAccount,
  type ReadonlySignerAccount,
  type ReadonlyUint8Array,
  type TransactionSigner,
  type WritableAccount,
  type WritableSignerAccount,
} from "@solana/kit";
import { findEventAuthorityPda, findTransferLiabilityPda } from "../pdas";
import { SVM_SPOKE_PROGRAM_ADDRESS } from "../programs";
import {
  expectAddress,
  getAccountMetaFactory,
  type ResolvedAccount,
} from "../shared";

export const BRIDGE_TOKENS_TO_HUB_POOL_DISCRIMINATOR = new Uint8Array([
  1, 83, 255, 59, 232, 55, 64, 216,
]);

export function getBridgeTokensToHubPoolDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    BRIDGE_TOKENS_TO_HUB_POOL_DISCRIMINATOR,
  );
}

export type BridgeTokensToHubPoolInstruction<
  TProgram extends string = typeof SVM_SPOKE_PROGRAM_ADDRESS,
  TAccountSigner extends string | AccountMeta<string> = string,
  TAccountPayer extends string | AccountMeta<string> = string,
  TAccountMint extends string | AccountMeta<string> = string,
  TAccountState extends string | AccountMeta<string> = string,
  TAccountTransferLiability extends string | AccountMeta<string> = string,
  TAccountVault extends string | AccountMeta<string> = string,
  TAccountTokenMessengerMinterSenderAuthority extends
    | string
    | AccountMeta<string> = string,
  TAccountMessageTransmitter extends string | AccountMeta<string> = string,
  TAccountTokenMessenger extends string | AccountMeta<string> = string,
  TAccountRemoteTokenMessenger extends string | AccountMeta<string> = string,
  TAccountTokenMinter extends string | AccountMeta<string> = string,
  TAccountLocalToken extends string | AccountMeta<string> = string,
  TAccountCctpEventAuthority extends string | AccountMeta<string> = string,
  TAccountMessageSentEventData extends string | AccountMeta<string> = string,
  TAccountMessageTransmitterProgram extends string | AccountMeta<string> =
    "CCTPmbSD7gX1bxKPAmg77w8oFzNFpaQiQUWD43TKaecd",
  TAccountTokenMessengerMinterProgram extends string | AccountMeta<string> =
    "CCTPiPYPc6AsJuwueEnWgSgucamXDZwBd53dQ11YiKX3",
  TAccountTokenProgram extends string | AccountMeta<string> =
    "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
  TAccountSystemProgram extends string | AccountMeta<string> =
    "11111111111111111111111111111111",
  TAccountEventAuthority extends string | AccountMeta<string> = string,
  TAccountProgram extends string | AccountMeta<string> = string,
  TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> &
  InstructionWithData<ReadonlyUint8Array> &
  InstructionWithAccounts<
    [
      TAccountSigner extends string
        ? ReadonlySignerAccount<TAccountSigner> &
            AccountSignerMeta<TAccountSigner>
        : TAccountSigner,
      TAccountPayer extends string
        ? WritableSignerAccount<TAccountPayer> &
            AccountSignerMeta<TAccountPayer>
        : TAccountPayer,
      TAccountMint extends string
        ? WritableAccount<TAccountMint>
        : TAccountMint,
      TAccountState extends string
        ? ReadonlyAccount<TAccountState>
        : TAccountState,
      TAccountTransferLiability extends string
        ? WritableAccount<TAccountTransferLiability>
        : TAccountTransferLiability,
      TAccountVault extends string
        ? WritableAccount<TAccountVault>
        : TAccountVault,
      TAccountTokenMessengerMinterSenderAuthority extends string
        ? ReadonlyAccount<TAccountTokenMessengerMinterSenderAuthority>
        : TAccountTokenMessengerMinterSenderAuthority,
      TAccountMessageTransmitter extends string
        ? WritableAccount<TAccountMessageTransmitter>
        : TAccountMessageTransmitter,
      TAccountTokenMessenger extends string
        ? ReadonlyAccount<TAccountTokenMessenger>
        : TAccountTokenMessenger,
      TAccountRemoteTokenMessenger extends string
        ? ReadonlyAccount<TAccountRemoteTokenMessenger>
        : TAccountRemoteTokenMessenger,
      TAccountTokenMinter extends string
        ? ReadonlyAccount<TAccountTokenMinter>
        : TAccountTokenMinter,
      TAccountLocalToken extends string
        ? WritableAccount<TAccountLocalToken>
        : TAccountLocalToken,
      TAccountCctpEventAuthority extends string
        ? ReadonlyAccount<TAccountCctpEventAuthority>
        : TAccountCctpEventAuthority,
      TAccountMessageSentEventData extends string
        ? WritableSignerAccount<TAccountMessageSentEventData> &
            AccountSignerMeta<TAccountMessageSentEventData>
        : TAccountMessageSentEventData,
      TAccountMessageTransmitterProgram extends string
        ? ReadonlyAccount<TAccountMessageTransmitterProgram>
        : TAccountMessageTransmitterProgram,
      TAccountTokenMessengerMinterProgram extends string
        ? ReadonlyAccount<TAccountTokenMessengerMinterProgram>
        : TAccountTokenMessengerMinterProgram,
      TAccountTokenProgram extends string
        ? ReadonlyAccount<TAccountTokenProgram>
        : TAccountTokenProgram,
      TAccountSystemProgram extends string
        ? ReadonlyAccount<TAccountSystemProgram>
        : TAccountSystemProgram,
      TAccountEventAuthority extends string
        ? ReadonlyAccount<TAccountEventAuthority>
        : TAccountEventAuthority,
      TAccountProgram extends string
        ? ReadonlyAccount<TAccountProgram>
        : TAccountProgram,
      ...TRemainingAccounts,
    ]
  >;

export type BridgeTokensToHubPoolInstructionData = {
  discriminator: ReadonlyUint8Array;
  amount: bigint;
};

export type BridgeTokensToHubPoolInstructionDataArgs = {
  amount: number | bigint;
};

export function getBridgeTokensToHubPoolInstructionDataEncoder(): FixedSizeEncoder<BridgeTokensToHubPoolInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["amount", getU64Encoder()],
    ]),
    (value) => ({
      ...value,
      discriminator: BRIDGE_TOKENS_TO_HUB_POOL_DISCRIMINATOR,
    }),
  );
}

export function getBridgeTokensToHubPoolInstructionDataDecoder(): FixedSizeDecoder<BridgeTokensToHubPoolInstructionData> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["amount", getU64Decoder()],
  ]);
}

export function getBridgeTokensToHubPoolInstructionDataCodec(): FixedSizeCodec<
  BridgeTokensToHubPoolInstructionDataArgs,
  BridgeTokensToHubPoolInstructionData
> {
  return combineCodec(
    getBridgeTokensToHubPoolInstructionDataEncoder(),
    getBridgeTokensToHubPoolInstructionDataDecoder(),
  );
}

export type BridgeTokensToHubPoolAsyncInput<
  TAccountSigner extends string = string,
  TAccountPayer extends string = string,
  TAccountMint extends string = string,
  TAccountState extends string = string,
  TAccountTransferLiability extends string = string,
  TAccountVault extends string = string,
  TAccountTokenMessengerMinterSenderAuthority extends string = string,
  TAccountMessageTransmitter extends string = string,
  TAccountTokenMessenger extends string = string,
  TAccountRemoteTokenMessenger extends string = string,
  TAccountTokenMinter extends string = string,
  TAccountLocalToken extends string = string,
  TAccountCctpEventAuthority extends string = string,
  TAccountMessageSentEventData extends string = string,
  TAccountMessageTransmitterProgram extends string = string,
  TAccountTokenMessengerMinterProgram extends string = string,
  TAccountTokenProgram extends string = string,
  TAccountSystemProgram extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountProgram extends string = string,
> = {
  signer: TransactionSigner<TAccountSigner>;
  payer: TransactionSigner<TAccountPayer>;
  mint: Address<TAccountMint>;
  state: Address<TAccountState>;
  transferLiability?: Address<TAccountTransferLiability>;
  vault?: Address<TAccountVault>;
  tokenMessengerMinterSenderAuthority: Address<TAccountTokenMessengerMinterSenderAuthority>;
  messageTransmitter: Address<TAccountMessageTransmitter>;
  tokenMessenger: Address<TAccountTokenMessenger>;
  remoteTokenMessenger: Address<TAccountRemoteTokenMessenger>;
  tokenMinter: Address<TAccountTokenMinter>;
  localToken: Address<TAccountLocalToken>;
  cctpEventAuthority: Address<TAccountCctpEventAuthority>;
  messageSentEventData: TransactionSigner<TAccountMessageSentEventData>;
  messageTransmitterProgram?: Address<TAccountMessageTransmitterProgram>;
  tokenMessengerMinterProgram?: Address<TAccountTokenMessengerMinterProgram>;
  tokenProgram?: Address<TAccountTokenProgram>;
  systemProgram?: Address<TAccountSystemProgram>;
  eventAuthority?: Address<TAccountEventAuthority>;
  program: Address<TAccountProgram>;
  amount: BridgeTokensToHubPoolInstructionDataArgs["amount"];
};

export async function getBridgeTokensToHubPoolInstructionAsync<
  TAccountSigner extends string,
  TAccountPayer extends string,
  TAccountMint extends string,
  TAccountState extends string,
  TAccountTransferLiability extends string,
  TAccountVault extends string,
  TAccountTokenMessengerMinterSenderAuthority extends string,
  TAccountMessageTransmitter extends string,
  TAccountTokenMessenger extends string,
  TAccountRemoteTokenMessenger extends string,
  TAccountTokenMinter extends string,
  TAccountLocalToken extends string,
  TAccountCctpEventAuthority extends string,
  TAccountMessageSentEventData extends string,
  TAccountMessageTransmitterProgram extends string,
  TAccountTokenMessengerMinterProgram extends string,
  TAccountTokenProgram extends string,
  TAccountSystemProgram extends string,
  TAccountEventAuthority extends string,
  TAccountProgram extends string,
  TProgramAddress extends Address = typeof SVM_SPOKE_PROGRAM_ADDRESS,
>(
  input: BridgeTokensToHubPoolAsyncInput<
    TAccountSigner,
    TAccountPayer,
    TAccountMint,
    TAccountState,
    TAccountTransferLiability,
    TAccountVault,
    TAccountTokenMessengerMinterSenderAuthority,
    TAccountMessageTransmitter,
    TAccountTokenMessenger,
    TAccountRemoteTokenMessenger,
    TAccountTokenMinter,
    TAccountLocalToken,
    TAccountCctpEventAuthority,
    TAccountMessageSentEventData,
    TAccountMessageTransmitterProgram,
    TAccountTokenMessengerMinterProgram,
    TAccountTokenProgram,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): Promise<
  BridgeTokensToHubPoolInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountPayer,
    TAccountMint,
    TAccountState,
    TAccountTransferLiability,
    TAccountVault,
    TAccountTokenMessengerMinterSenderAuthority,
    TAccountMessageTransmitter,
    TAccountTokenMessenger,
    TAccountRemoteTokenMessenger,
    TAccountTokenMinter,
    TAccountLocalToken,
    TAccountCctpEventAuthority,
    TAccountMessageSentEventData,
    TAccountMessageTransmitterProgram,
    TAccountTokenMessengerMinterProgram,
    TAccountTokenProgram,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >
> {
  // Program address.
  const programAddress = config?.programAddress ?? SVM_SPOKE_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    signer: { value: input.signer ?? null, isWritable: false },
    payer: { value: input.payer ?? null, isWritable: true },
    mint: { value: input.mint ?? null, isWritable: true },
    state: { value: input.state ?? null, isWritable: false },
    transferLiability: {
      value: input.transferLiability ?? null,
      isWritable: true,
    },
    vault: { value: input.vault ?? null, isWritable: true },
    tokenMessengerMinterSenderAuthority: {
      value: input.tokenMessengerMinterSenderAuthority ?? null,
      isWritable: false,
    },
    messageTransmitter: {
      value: input.messageTransmitter ?? null,
      isWritable: true,
    },
    tokenMessenger: { value: input.tokenMessenger ?? null, isWritable: false },
    remoteTokenMessenger: {
      value: input.remoteTokenMessenger ?? null,
      isWritable: false,
    },
    tokenMinter: { value: input.tokenMinter ?? null, isWritable: false },
    localToken: { value: input.localToken ?? null, isWritable: true },
    cctpEventAuthority: {
      value: input.cctpEventAuthority ?? null,
      isWritable: false,
    },
    messageSentEventData: {
      value: input.messageSentEventData ?? null,
      isWritable: true,
    },
    messageTransmitterProgram: {
      value: input.messageTransmitterProgram ?? null,
      isWritable: false,
    },
    tokenMessengerMinterProgram: {
      value: input.tokenMessengerMinterProgram ?? null,
      isWritable: false,
    },
    tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
    systemProgram: { value: input.systemProgram ?? null, isWritable: false },
    eventAuthority: { value: input.eventAuthority ?? null, isWritable: false },
    program: { value: input.program ?? null, isWritable: false },
  };
  const accounts = originalAccounts as Record<
    keyof typeof originalAccounts,
    ResolvedAccount
  >;

  // Original args.
  const args = { ...input };

  // Resolve default values.
  if (!accounts.transferLiability.value) {
    accounts.transferLiability.value = await findTransferLiabilityPda({
      mint: expectAddress(accounts.mint.value),
    });
  }
  if (!accounts.tokenProgram.value) {
    accounts.tokenProgram.value =
      "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" as Address<"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA">;
  }
  if (!accounts.vault.value) {
    accounts.vault.value = await getProgramDerivedAddress({
      programAddress:
        "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL" as Address<"ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL">,
      seeds: [
        getAddressEncoder().encode(expectAddress(accounts.state.value)),
        getAddressEncoder().encode(expectAddress(accounts.tokenProgram.value)),
        getAddressEncoder().encode(expectAddress(accounts.mint.value)),
      ],
    });
  }
  if (!accounts.messageTransmitterProgram.value) {
    accounts.messageTransmitterProgram.value =
      "CCTPmbSD7gX1bxKPAmg77w8oFzNFpaQiQUWD43TKaecd" as Address<"CCTPmbSD7gX1bxKPAmg77w8oFzNFpaQiQUWD43TKaecd">;
  }
  if (!accounts.tokenMessengerMinterProgram.value) {
    accounts.tokenMessengerMinterProgram.value =
      "CCTPiPYPc6AsJuwueEnWgSgucamXDZwBd53dQ11YiKX3" as Address<"CCTPiPYPc6AsJuwueEnWgSgucamXDZwBd53dQ11YiKX3">;
  }
  if (!accounts.systemProgram.value) {
    accounts.systemProgram.value =
      "11111111111111111111111111111111" as Address<"11111111111111111111111111111111">;
  }
  if (!accounts.eventAuthority.value) {
    accounts.eventAuthority.value = await findEventAuthorityPda();
  }

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.signer),
      getAccountMeta(accounts.payer),
      getAccountMeta(accounts.mint),
      getAccountMeta(accounts.state),
      getAccountMeta(accounts.transferLiability),
      getAccountMeta(accounts.vault),
      getAccountMeta(accounts.tokenMessengerMinterSenderAuthority),
      getAccountMeta(accounts.messageTransmitter),
      getAccountMeta(accounts.tokenMessenger),
      getAccountMeta(accounts.remoteTokenMessenger),
      getAccountMeta(accounts.tokenMinter),
      getAccountMeta(accounts.localToken),
      getAccountMeta(accounts.cctpEventAuthority),
      getAccountMeta(accounts.messageSentEventData),
      getAccountMeta(accounts.messageTransmitterProgram),
      getAccountMeta(accounts.tokenMessengerMinterProgram),
      getAccountMeta(accounts.tokenProgram),
      getAccountMeta(accounts.systemProgram),
      getAccountMeta(accounts.eventAuthority),
      getAccountMeta(accounts.program),
    ],
    data: getBridgeTokensToHubPoolInstructionDataEncoder().encode(
      args as BridgeTokensToHubPoolInstructionDataArgs,
    ),
    programAddress,
  } as BridgeTokensToHubPoolInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountPayer,
    TAccountMint,
    TAccountState,
    TAccountTransferLiability,
    TAccountVault,
    TAccountTokenMessengerMinterSenderAuthority,
    TAccountMessageTransmitter,
    TAccountTokenMessenger,
    TAccountRemoteTokenMessenger,
    TAccountTokenMinter,
    TAccountLocalToken,
    TAccountCctpEventAuthority,
    TAccountMessageSentEventData,
    TAccountMessageTransmitterProgram,
    TAccountTokenMessengerMinterProgram,
    TAccountTokenProgram,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

export type BridgeTokensToHubPoolInput<
  TAccountSigner extends string = string,
  TAccountPayer extends string = string,
  TAccountMint extends string = string,
  TAccountState extends string = string,
  TAccountTransferLiability extends string = string,
  TAccountVault extends string = string,
  TAccountTokenMessengerMinterSenderAuthority extends string = string,
  TAccountMessageTransmitter extends string = string,
  TAccountTokenMessenger extends string = string,
  TAccountRemoteTokenMessenger extends string = string,
  TAccountTokenMinter extends string = string,
  TAccountLocalToken extends string = string,
  TAccountCctpEventAuthority extends string = string,
  TAccountMessageSentEventData extends string = string,
  TAccountMessageTransmitterProgram extends string = string,
  TAccountTokenMessengerMinterProgram extends string = string,
  TAccountTokenProgram extends string = string,
  TAccountSystemProgram extends string = string,
  TAccountEventAuthority extends string = string,
  TAccountProgram extends string = string,
> = {
  signer: TransactionSigner<TAccountSigner>;
  payer: TransactionSigner<TAccountPayer>;
  mint: Address<TAccountMint>;
  state: Address<TAccountState>;
  transferLiability: Address<TAccountTransferLiability>;
  vault: Address<TAccountVault>;
  tokenMessengerMinterSenderAuthority: Address<TAccountTokenMessengerMinterSenderAuthority>;
  messageTransmitter: Address<TAccountMessageTransmitter>;
  tokenMessenger: Address<TAccountTokenMessenger>;
  remoteTokenMessenger: Address<TAccountRemoteTokenMessenger>;
  tokenMinter: Address<TAccountTokenMinter>;
  localToken: Address<TAccountLocalToken>;
  cctpEventAuthority: Address<TAccountCctpEventAuthority>;
  messageSentEventData: TransactionSigner<TAccountMessageSentEventData>;
  messageTransmitterProgram?: Address<TAccountMessageTransmitterProgram>;
  tokenMessengerMinterProgram?: Address<TAccountTokenMessengerMinterProgram>;
  tokenProgram?: Address<TAccountTokenProgram>;
  systemProgram?: Address<TAccountSystemProgram>;
  eventAuthority: Address<TAccountEventAuthority>;
  program: Address<TAccountProgram>;
  amount: BridgeTokensToHubPoolInstructionDataArgs["amount"];
};

export function getBridgeTokensToHubPoolInstruction<
  TAccountSigner extends string,
  TAccountPayer extends string,
  TAccountMint extends string,
  TAccountState extends string,
  TAccountTransferLiability extends string,
  TAccountVault extends string,
  TAccountTokenMessengerMinterSenderAuthority extends string,
  TAccountMessageTransmitter extends string,
  TAccountTokenMessenger extends string,
  TAccountRemoteTokenMessenger extends string,
  TAccountTokenMinter extends string,
  TAccountLocalToken extends string,
  TAccountCctpEventAuthority extends string,
  TAccountMessageSentEventData extends string,
  TAccountMessageTransmitterProgram extends string,
  TAccountTokenMessengerMinterProgram extends string,
  TAccountTokenProgram extends string,
  TAccountSystemProgram extends string,
  TAccountEventAuthority extends string,
  TAccountProgram extends string,
  TProgramAddress extends Address = typeof SVM_SPOKE_PROGRAM_ADDRESS,
>(
  input: BridgeTokensToHubPoolInput<
    TAccountSigner,
    TAccountPayer,
    TAccountMint,
    TAccountState,
    TAccountTransferLiability,
    TAccountVault,
    TAccountTokenMessengerMinterSenderAuthority,
    TAccountMessageTransmitter,
    TAccountTokenMessenger,
    TAccountRemoteTokenMessenger,
    TAccountTokenMinter,
    TAccountLocalToken,
    TAccountCctpEventAuthority,
    TAccountMessageSentEventData,
    TAccountMessageTransmitterProgram,
    TAccountTokenMessengerMinterProgram,
    TAccountTokenProgram,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): BridgeTokensToHubPoolInstruction<
  TProgramAddress,
  TAccountSigner,
  TAccountPayer,
  TAccountMint,
  TAccountState,
  TAccountTransferLiability,
  TAccountVault,
  TAccountTokenMessengerMinterSenderAuthority,
  TAccountMessageTransmitter,
  TAccountTokenMessenger,
  TAccountRemoteTokenMessenger,
  TAccountTokenMinter,
  TAccountLocalToken,
  TAccountCctpEventAuthority,
  TAccountMessageSentEventData,
  TAccountMessageTransmitterProgram,
  TAccountTokenMessengerMinterProgram,
  TAccountTokenProgram,
  TAccountSystemProgram,
  TAccountEventAuthority,
  TAccountProgram
> {
  // Program address.
  const programAddress = config?.programAddress ?? SVM_SPOKE_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    signer: { value: input.signer ?? null, isWritable: false },
    payer: { value: input.payer ?? null, isWritable: true },
    mint: { value: input.mint ?? null, isWritable: true },
    state: { value: input.state ?? null, isWritable: false },
    transferLiability: {
      value: input.transferLiability ?? null,
      isWritable: true,
    },
    vault: { value: input.vault ?? null, isWritable: true },
    tokenMessengerMinterSenderAuthority: {
      value: input.tokenMessengerMinterSenderAuthority ?? null,
      isWritable: false,
    },
    messageTransmitter: {
      value: input.messageTransmitter ?? null,
      isWritable: true,
    },
    tokenMessenger: { value: input.tokenMessenger ?? null, isWritable: false },
    remoteTokenMessenger: {
      value: input.remoteTokenMessenger ?? null,
      isWritable: false,
    },
    tokenMinter: { value: input.tokenMinter ?? null, isWritable: false },
    localToken: { value: input.localToken ?? null, isWritable: true },
    cctpEventAuthority: {
      value: input.cctpEventAuthority ?? null,
      isWritable: false,
    },
    messageSentEventData: {
      value: input.messageSentEventData ?? null,
      isWritable: true,
    },
    messageTransmitterProgram: {
      value: input.messageTransmitterProgram ?? null,
      isWritable: false,
    },
    tokenMessengerMinterProgram: {
      value: input.tokenMessengerMinterProgram ?? null,
      isWritable: false,
    },
    tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
    systemProgram: { value: input.systemProgram ?? null, isWritable: false },
    eventAuthority: { value: input.eventAuthority ?? null, isWritable: false },
    program: { value: input.program ?? null, isWritable: false },
  };
  const accounts = originalAccounts as Record<
    keyof typeof originalAccounts,
    ResolvedAccount
  >;

  // Original args.
  const args = { ...input };

  // Resolve default values.
  if (!accounts.tokenProgram.value) {
    accounts.tokenProgram.value =
      "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" as Address<"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA">;
  }
  if (!accounts.messageTransmitterProgram.value) {
    accounts.messageTransmitterProgram.value =
      "CCTPmbSD7gX1bxKPAmg77w8oFzNFpaQiQUWD43TKaecd" as Address<"CCTPmbSD7gX1bxKPAmg77w8oFzNFpaQiQUWD43TKaecd">;
  }
  if (!accounts.tokenMessengerMinterProgram.value) {
    accounts.tokenMessengerMinterProgram.value =
      "CCTPiPYPc6AsJuwueEnWgSgucamXDZwBd53dQ11YiKX3" as Address<"CCTPiPYPc6AsJuwueEnWgSgucamXDZwBd53dQ11YiKX3">;
  }
  if (!accounts.systemProgram.value) {
    accounts.systemProgram.value =
      "11111111111111111111111111111111" as Address<"11111111111111111111111111111111">;
  }

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.signer),
      getAccountMeta(accounts.payer),
      getAccountMeta(accounts.mint),
      getAccountMeta(accounts.state),
      getAccountMeta(accounts.transferLiability),
      getAccountMeta(accounts.vault),
      getAccountMeta(accounts.tokenMessengerMinterSenderAuthority),
      getAccountMeta(accounts.messageTransmitter),
      getAccountMeta(accounts.tokenMessenger),
      getAccountMeta(accounts.remoteTokenMessenger),
      getAccountMeta(accounts.tokenMinter),
      getAccountMeta(accounts.localToken),
      getAccountMeta(accounts.cctpEventAuthority),
      getAccountMeta(accounts.messageSentEventData),
      getAccountMeta(accounts.messageTransmitterProgram),
      getAccountMeta(accounts.tokenMessengerMinterProgram),
      getAccountMeta(accounts.tokenProgram),
      getAccountMeta(accounts.systemProgram),
      getAccountMeta(accounts.eventAuthority),
      getAccountMeta(accounts.program),
    ],
    data: getBridgeTokensToHubPoolInstructionDataEncoder().encode(
      args as BridgeTokensToHubPoolInstructionDataArgs,
    ),
    programAddress,
  } as BridgeTokensToHubPoolInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountPayer,
    TAccountMint,
    TAccountState,
    TAccountTransferLiability,
    TAccountVault,
    TAccountTokenMessengerMinterSenderAuthority,
    TAccountMessageTransmitter,
    TAccountTokenMessenger,
    TAccountRemoteTokenMessenger,
    TAccountTokenMinter,
    TAccountLocalToken,
    TAccountCctpEventAuthority,
    TAccountMessageSentEventData,
    TAccountMessageTransmitterProgram,
    TAccountTokenMessengerMinterProgram,
    TAccountTokenProgram,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

export type ParsedBridgeTokensToHubPoolInstruction<
  TProgram extends string = typeof SVM_SPOKE_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    signer: TAccountMetas[0];
    payer: TAccountMetas[1];
    mint: TAccountMetas[2];
    state: TAccountMetas[3];
    transferLiability: TAccountMetas[4];
    vault: TAccountMetas[5];
    tokenMessengerMinterSenderAuthority: TAccountMetas[6];
    messageTransmitter: TAccountMetas[7];
    tokenMessenger: TAccountMetas[8];
    remoteTokenMessenger: TAccountMetas[9];
    tokenMinter: TAccountMetas[10];
    localToken: TAccountMetas[11];
    cctpEventAuthority: TAccountMetas[12];
    messageSentEventData: TAccountMetas[13];
    messageTransmitterProgram: TAccountMetas[14];
    tokenMessengerMinterProgram: TAccountMetas[15];
    tokenProgram: TAccountMetas[16];
    systemProgram: TAccountMetas[17];
    eventAuthority: TAccountMetas[18];
    program: TAccountMetas[19];
  };
  data: BridgeTokensToHubPoolInstructionData;
};

export function parseBridgeTokensToHubPoolInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedBridgeTokensToHubPoolInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 20) {
    // TODO: Coded error.
    throw new Error("Not enough accounts");
  }
  let accountIndex = 0;
  const getNextAccount = () => {
    const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!;
    accountIndex += 1;
    return accountMeta;
  };
  return {
    programAddress: instruction.programAddress,
    accounts: {
      signer: getNextAccount(),
      payer: getNextAccount(),
      mint: getNextAccount(),
      state: getNextAccount(),
      transferLiability: getNextAccount(),
      vault: getNextAccount(),
      tokenMessengerMinterSenderAuthority: getNextAccount(),
      messageTransmitter: getNextAccount(),
      tokenMessenger: getNextAccount(),
      remoteTokenMessenger: getNextAccount(),
      tokenMinter: getNextAccount(),
      localToken: getNextAccount(),
      cctpEventAuthority: getNextAccount(),
      messageSentEventData: getNextAccount(),
      messageTransmitterProgram: getNextAccount(),
      tokenMessengerMinterProgram: getNextAccount(),
      tokenProgram: getNextAccount(),
      systemProgram: getNextAccount(),
      eventAuthority: getNextAccount(),
      program: getNextAccount(),
    },
    data: getBridgeTokensToHubPoolInstructionDataDecoder().decode(
      instruction.data,
    ),
  };
}
