/**
 * 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,
  transformEncoder,
  type AccountMeta,
  type AccountSignerMeta,
  type Address,
  type Codec,
  type Decoder,
  type Encoder,
  type Instruction,
  type InstructionWithAccounts,
  type InstructionWithData,
  type ReadonlyAccount,
  type ReadonlyUint8Array,
  type TransactionSigner,
  type WritableAccount,
  type WritableSignerAccount,
} from "@solana/kit";
import {
  findEventAuthorityPda,
  findMinimumDepositPda,
  findRentClaimPda,
  findRentFundPda,
  findStatePda,
} from "../pdas";
import { SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS } from "../programs";
import {
  expectAddress,
  getAccountMetaFactory,
  type ResolvedAccount,
} from "../shared";
import {
  getSponsoredCCTPQuoteDecoder,
  getSponsoredCCTPQuoteEncoder,
  type SponsoredCCTPQuote,
  type SponsoredCCTPQuoteArgs,
} from "../types";

export const DEPOSIT_FOR_BURN_DISCRIMINATOR = new Uint8Array([
  215, 60, 61, 46, 114, 55, 128, 176,
]);

export function getDepositForBurnDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    DEPOSIT_FOR_BURN_DISCRIMINATOR,
  );
}

export type DepositForBurnInstruction<
  TProgram extends string = typeof SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS,
  TAccountSigner extends string | AccountMeta<string> = string,
  TAccountState extends string | AccountMeta<string> = string,
  TAccountRentFund extends string | AccountMeta<string> = string,
  TAccountMinimumDeposit extends string | AccountMeta<string> = string,
  TAccountUsedNonce extends string | AccountMeta<string> = string,
  TAccountRentClaim extends string | AccountMeta<string> = string,
  TAccountDepositorTokenAccount extends string | AccountMeta<string> = string,
  TAccountBurnToken extends string | AccountMeta<string> = string,
  TAccountDenylistAccount 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> =
    "CCTPV2Sm4AdWt5296sk4P66VBZ7bEhcARwFaaS9YPbeC",
  TAccountTokenMessengerMinterProgram extends string | AccountMeta<string> =
    "CCTPV2vPZJS2u2BBsUoscuikbYjnpFmbFsvVuJdgUMQe",
  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
        ? WritableSignerAccount<TAccountSigner> &
            AccountSignerMeta<TAccountSigner>
        : TAccountSigner,
      TAccountState extends string
        ? ReadonlyAccount<TAccountState>
        : TAccountState,
      TAccountRentFund extends string
        ? WritableAccount<TAccountRentFund>
        : TAccountRentFund,
      TAccountMinimumDeposit extends string
        ? ReadonlyAccount<TAccountMinimumDeposit>
        : TAccountMinimumDeposit,
      TAccountUsedNonce extends string
        ? WritableAccount<TAccountUsedNonce>
        : TAccountUsedNonce,
      TAccountRentClaim extends string
        ? WritableAccount<TAccountRentClaim>
        : TAccountRentClaim,
      TAccountDepositorTokenAccount extends string
        ? WritableAccount<TAccountDepositorTokenAccount>
        : TAccountDepositorTokenAccount,
      TAccountBurnToken extends string
        ? WritableAccount<TAccountBurnToken>
        : TAccountBurnToken,
      TAccountDenylistAccount extends string
        ? ReadonlyAccount<TAccountDenylistAccount>
        : TAccountDenylistAccount,
      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 DepositForBurnInstructionData = {
  discriminator: ReadonlyUint8Array;
  quote: SponsoredCCTPQuote;
  signature: ReadonlyUint8Array;
};

export type DepositForBurnInstructionDataArgs = {
  quote: SponsoredCCTPQuoteArgs;
  signature: ReadonlyUint8Array;
};

export function getDepositForBurnInstructionDataEncoder(): Encoder<DepositForBurnInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["quote", getSponsoredCCTPQuoteEncoder()],
      ["signature", fixEncoderSize(getBytesEncoder(), 65)],
    ]),
    (value) => ({ ...value, discriminator: DEPOSIT_FOR_BURN_DISCRIMINATOR }),
  );
}

export function getDepositForBurnInstructionDataDecoder(): Decoder<DepositForBurnInstructionData> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["quote", getSponsoredCCTPQuoteDecoder()],
    ["signature", fixDecoderSize(getBytesDecoder(), 65)],
  ]);
}

export function getDepositForBurnInstructionDataCodec(): Codec<
  DepositForBurnInstructionDataArgs,
  DepositForBurnInstructionData
> {
  return combineCodec(
    getDepositForBurnInstructionDataEncoder(),
    getDepositForBurnInstructionDataDecoder(),
  );
}

export type DepositForBurnAsyncInput<
  TAccountSigner extends string = string,
  TAccountState extends string = string,
  TAccountRentFund extends string = string,
  TAccountMinimumDeposit extends string = string,
  TAccountUsedNonce extends string = string,
  TAccountRentClaim extends string = string,
  TAccountDepositorTokenAccount extends string = string,
  TAccountBurnToken extends string = string,
  TAccountDenylistAccount 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>;
  state?: Address<TAccountState>;
  rentFund?: Address<TAccountRentFund>;
  minimumDeposit?: Address<TAccountMinimumDeposit>;
  usedNonce: Address<TAccountUsedNonce>;
  rentClaim?: Address<TAccountRentClaim>;
  depositorTokenAccount?: Address<TAccountDepositorTokenAccount>;
  burnToken: Address<TAccountBurnToken>;
  denylistAccount: Address<TAccountDenylistAccount>;
  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>;
  quote: DepositForBurnInstructionDataArgs["quote"];
  signature: DepositForBurnInstructionDataArgs["signature"];
};

export async function getDepositForBurnInstructionAsync<
  TAccountSigner extends string,
  TAccountState extends string,
  TAccountRentFund extends string,
  TAccountMinimumDeposit extends string,
  TAccountUsedNonce extends string,
  TAccountRentClaim extends string,
  TAccountDepositorTokenAccount extends string,
  TAccountBurnToken extends string,
  TAccountDenylistAccount 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 SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS,
>(
  input: DepositForBurnAsyncInput<
    TAccountSigner,
    TAccountState,
    TAccountRentFund,
    TAccountMinimumDeposit,
    TAccountUsedNonce,
    TAccountRentClaim,
    TAccountDepositorTokenAccount,
    TAccountBurnToken,
    TAccountDenylistAccount,
    TAccountTokenMessengerMinterSenderAuthority,
    TAccountMessageTransmitter,
    TAccountTokenMessenger,
    TAccountRemoteTokenMessenger,
    TAccountTokenMinter,
    TAccountLocalToken,
    TAccountCctpEventAuthority,
    TAccountMessageSentEventData,
    TAccountMessageTransmitterProgram,
    TAccountTokenMessengerMinterProgram,
    TAccountTokenProgram,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): Promise<
  DepositForBurnInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountState,
    TAccountRentFund,
    TAccountMinimumDeposit,
    TAccountUsedNonce,
    TAccountRentClaim,
    TAccountDepositorTokenAccount,
    TAccountBurnToken,
    TAccountDenylistAccount,
    TAccountTokenMessengerMinterSenderAuthority,
    TAccountMessageTransmitter,
    TAccountTokenMessenger,
    TAccountRemoteTokenMessenger,
    TAccountTokenMinter,
    TAccountLocalToken,
    TAccountCctpEventAuthority,
    TAccountMessageSentEventData,
    TAccountMessageTransmitterProgram,
    TAccountTokenMessengerMinterProgram,
    TAccountTokenProgram,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    signer: { value: input.signer ?? null, isWritable: true },
    state: { value: input.state ?? null, isWritable: false },
    rentFund: { value: input.rentFund ?? null, isWritable: true },
    minimumDeposit: { value: input.minimumDeposit ?? null, isWritable: false },
    usedNonce: { value: input.usedNonce ?? null, isWritable: true },
    rentClaim: { value: input.rentClaim ?? null, isWritable: true },
    depositorTokenAccount: {
      value: input.depositorTokenAccount ?? null,
      isWritable: true,
    },
    burnToken: { value: input.burnToken ?? null, isWritable: true },
    denylistAccount: {
      value: input.denylistAccount ?? null,
      isWritable: false,
    },
    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.state.value) {
    accounts.state.value = await findStatePda();
  }
  if (!accounts.rentFund.value) {
    accounts.rentFund.value = await findRentFundPda();
  }
  if (!accounts.minimumDeposit.value) {
    accounts.minimumDeposit.value = await findMinimumDepositPda({
      burnToken: expectAddress(accounts.burnToken.value),
    });
  }
  if (!accounts.rentClaim.value) {
    accounts.rentClaim.value = await findRentClaimPda({
      signer: expectAddress(accounts.signer.value),
    });
  }
  if (!accounts.tokenProgram.value) {
    accounts.tokenProgram.value =
      "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" as Address<"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA">;
  }
  if (!accounts.depositorTokenAccount.value) {
    accounts.depositorTokenAccount.value = await getProgramDerivedAddress({
      programAddress:
        "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL" as Address<"ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL">,
      seeds: [
        getAddressEncoder().encode(expectAddress(accounts.signer.value)),
        getAddressEncoder().encode(expectAddress(accounts.tokenProgram.value)),
        getAddressEncoder().encode(expectAddress(accounts.burnToken.value)),
      ],
    });
  }
  if (!accounts.messageTransmitterProgram.value) {
    accounts.messageTransmitterProgram.value =
      "CCTPV2Sm4AdWt5296sk4P66VBZ7bEhcARwFaaS9YPbeC" as Address<"CCTPV2Sm4AdWt5296sk4P66VBZ7bEhcARwFaaS9YPbeC">;
  }
  if (!accounts.tokenMessengerMinterProgram.value) {
    accounts.tokenMessengerMinterProgram.value =
      "CCTPV2vPZJS2u2BBsUoscuikbYjnpFmbFsvVuJdgUMQe" as Address<"CCTPV2vPZJS2u2BBsUoscuikbYjnpFmbFsvVuJdgUMQe">;
  }
  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.state),
      getAccountMeta(accounts.rentFund),
      getAccountMeta(accounts.minimumDeposit),
      getAccountMeta(accounts.usedNonce),
      getAccountMeta(accounts.rentClaim),
      getAccountMeta(accounts.depositorTokenAccount),
      getAccountMeta(accounts.burnToken),
      getAccountMeta(accounts.denylistAccount),
      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: getDepositForBurnInstructionDataEncoder().encode(
      args as DepositForBurnInstructionDataArgs,
    ),
    programAddress,
  } as DepositForBurnInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountState,
    TAccountRentFund,
    TAccountMinimumDeposit,
    TAccountUsedNonce,
    TAccountRentClaim,
    TAccountDepositorTokenAccount,
    TAccountBurnToken,
    TAccountDenylistAccount,
    TAccountTokenMessengerMinterSenderAuthority,
    TAccountMessageTransmitter,
    TAccountTokenMessenger,
    TAccountRemoteTokenMessenger,
    TAccountTokenMinter,
    TAccountLocalToken,
    TAccountCctpEventAuthority,
    TAccountMessageSentEventData,
    TAccountMessageTransmitterProgram,
    TAccountTokenMessengerMinterProgram,
    TAccountTokenProgram,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

export type DepositForBurnInput<
  TAccountSigner extends string = string,
  TAccountState extends string = string,
  TAccountRentFund extends string = string,
  TAccountMinimumDeposit extends string = string,
  TAccountUsedNonce extends string = string,
  TAccountRentClaim extends string = string,
  TAccountDepositorTokenAccount extends string = string,
  TAccountBurnToken extends string = string,
  TAccountDenylistAccount 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>;
  state: Address<TAccountState>;
  rentFund: Address<TAccountRentFund>;
  minimumDeposit: Address<TAccountMinimumDeposit>;
  usedNonce: Address<TAccountUsedNonce>;
  rentClaim?: Address<TAccountRentClaim>;
  depositorTokenAccount: Address<TAccountDepositorTokenAccount>;
  burnToken: Address<TAccountBurnToken>;
  denylistAccount: Address<TAccountDenylistAccount>;
  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>;
  quote: DepositForBurnInstructionDataArgs["quote"];
  signature: DepositForBurnInstructionDataArgs["signature"];
};

export function getDepositForBurnInstruction<
  TAccountSigner extends string,
  TAccountState extends string,
  TAccountRentFund extends string,
  TAccountMinimumDeposit extends string,
  TAccountUsedNonce extends string,
  TAccountRentClaim extends string,
  TAccountDepositorTokenAccount extends string,
  TAccountBurnToken extends string,
  TAccountDenylistAccount 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 SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS,
>(
  input: DepositForBurnInput<
    TAccountSigner,
    TAccountState,
    TAccountRentFund,
    TAccountMinimumDeposit,
    TAccountUsedNonce,
    TAccountRentClaim,
    TAccountDepositorTokenAccount,
    TAccountBurnToken,
    TAccountDenylistAccount,
    TAccountTokenMessengerMinterSenderAuthority,
    TAccountMessageTransmitter,
    TAccountTokenMessenger,
    TAccountRemoteTokenMessenger,
    TAccountTokenMinter,
    TAccountLocalToken,
    TAccountCctpEventAuthority,
    TAccountMessageSentEventData,
    TAccountMessageTransmitterProgram,
    TAccountTokenMessengerMinterProgram,
    TAccountTokenProgram,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >,
  config?: { programAddress?: TProgramAddress },
): DepositForBurnInstruction<
  TProgramAddress,
  TAccountSigner,
  TAccountState,
  TAccountRentFund,
  TAccountMinimumDeposit,
  TAccountUsedNonce,
  TAccountRentClaim,
  TAccountDepositorTokenAccount,
  TAccountBurnToken,
  TAccountDenylistAccount,
  TAccountTokenMessengerMinterSenderAuthority,
  TAccountMessageTransmitter,
  TAccountTokenMessenger,
  TAccountRemoteTokenMessenger,
  TAccountTokenMinter,
  TAccountLocalToken,
  TAccountCctpEventAuthority,
  TAccountMessageSentEventData,
  TAccountMessageTransmitterProgram,
  TAccountTokenMessengerMinterProgram,
  TAccountTokenProgram,
  TAccountSystemProgram,
  TAccountEventAuthority,
  TAccountProgram
> {
  // Program address.
  const programAddress =
    config?.programAddress ?? SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    signer: { value: input.signer ?? null, isWritable: true },
    state: { value: input.state ?? null, isWritable: false },
    rentFund: { value: input.rentFund ?? null, isWritable: true },
    minimumDeposit: { value: input.minimumDeposit ?? null, isWritable: false },
    usedNonce: { value: input.usedNonce ?? null, isWritable: true },
    rentClaim: { value: input.rentClaim ?? null, isWritable: true },
    depositorTokenAccount: {
      value: input.depositorTokenAccount ?? null,
      isWritable: true,
    },
    burnToken: { value: input.burnToken ?? null, isWritable: true },
    denylistAccount: {
      value: input.denylistAccount ?? null,
      isWritable: false,
    },
    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 =
      "CCTPV2Sm4AdWt5296sk4P66VBZ7bEhcARwFaaS9YPbeC" as Address<"CCTPV2Sm4AdWt5296sk4P66VBZ7bEhcARwFaaS9YPbeC">;
  }
  if (!accounts.tokenMessengerMinterProgram.value) {
    accounts.tokenMessengerMinterProgram.value =
      "CCTPV2vPZJS2u2BBsUoscuikbYjnpFmbFsvVuJdgUMQe" as Address<"CCTPV2vPZJS2u2BBsUoscuikbYjnpFmbFsvVuJdgUMQe">;
  }
  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.state),
      getAccountMeta(accounts.rentFund),
      getAccountMeta(accounts.minimumDeposit),
      getAccountMeta(accounts.usedNonce),
      getAccountMeta(accounts.rentClaim),
      getAccountMeta(accounts.depositorTokenAccount),
      getAccountMeta(accounts.burnToken),
      getAccountMeta(accounts.denylistAccount),
      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: getDepositForBurnInstructionDataEncoder().encode(
      args as DepositForBurnInstructionDataArgs,
    ),
    programAddress,
  } as DepositForBurnInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountState,
    TAccountRentFund,
    TAccountMinimumDeposit,
    TAccountUsedNonce,
    TAccountRentClaim,
    TAccountDepositorTokenAccount,
    TAccountBurnToken,
    TAccountDenylistAccount,
    TAccountTokenMessengerMinterSenderAuthority,
    TAccountMessageTransmitter,
    TAccountTokenMessenger,
    TAccountRemoteTokenMessenger,
    TAccountTokenMinter,
    TAccountLocalToken,
    TAccountCctpEventAuthority,
    TAccountMessageSentEventData,
    TAccountMessageTransmitterProgram,
    TAccountTokenMessengerMinterProgram,
    TAccountTokenProgram,
    TAccountSystemProgram,
    TAccountEventAuthority,
    TAccountProgram
  >);
}

export type ParsedDepositForBurnInstruction<
  TProgram extends string = typeof SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    signer: TAccountMetas[0];
    state: TAccountMetas[1];
    rentFund: TAccountMetas[2];
    minimumDeposit: TAccountMetas[3];
    usedNonce: TAccountMetas[4];
    rentClaim?: TAccountMetas[5] | undefined;
    depositorTokenAccount: TAccountMetas[6];
    burnToken: TAccountMetas[7];
    denylistAccount: TAccountMetas[8];
    tokenMessengerMinterSenderAuthority: TAccountMetas[9];
    messageTransmitter: TAccountMetas[10];
    tokenMessenger: TAccountMetas[11];
    remoteTokenMessenger: TAccountMetas[12];
    tokenMinter: TAccountMetas[13];
    localToken: TAccountMetas[14];
    cctpEventAuthority: TAccountMetas[15];
    messageSentEventData: TAccountMetas[16];
    messageTransmitterProgram: TAccountMetas[17];
    tokenMessengerMinterProgram: TAccountMetas[18];
    tokenProgram: TAccountMetas[19];
    systemProgram: TAccountMetas[20];
    eventAuthority: TAccountMetas[21];
    program: TAccountMetas[22];
  };
  data: DepositForBurnInstructionData;
};

export function parseDepositForBurnInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedDepositForBurnInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 23) {
    // 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;
  };
  const getNextOptionalAccount = () => {
    const accountMeta = getNextAccount();
    return accountMeta.address === SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS
      ? undefined
      : accountMeta;
  };
  return {
    programAddress: instruction.programAddress,
    accounts: {
      signer: getNextAccount(),
      state: getNextAccount(),
      rentFund: getNextAccount(),
      minimumDeposit: getNextAccount(),
      usedNonce: getNextAccount(),
      rentClaim: getNextOptionalAccount(),
      depositorTokenAccount: getNextAccount(),
      burnToken: getNextAccount(),
      denylistAccount: 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: getDepositForBurnInstructionDataDecoder().decode(instruction.data),
  };
}
