123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- /**
- * 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 {
- AccountRole,
- combineCodec,
- getStructDecoder,
- getStructEncoder,
- getU64Decoder,
- getU64Encoder,
- getU8Decoder,
- getU8Encoder,
- transformEncoder,
- type Address,
- type Codec,
- type Decoder,
- type Encoder,
- type IAccountMeta,
- type IAccountSignerMeta,
- type IInstruction,
- type IInstructionWithAccounts,
- type IInstructionWithData,
- type ReadonlyAccount,
- type ReadonlySignerAccount,
- type TransactionSigner,
- type WritableAccount,
- } from '@solana/kit';
- import { TOKEN_PROGRAM_ADDRESS } from '../programs';
- import { getAccountMetaFactory, type ResolvedAccount } from '../shared';
- export const MINT_TO_DISCRIMINATOR = 7;
- export function getMintToDiscriminatorBytes() {
- return getU8Encoder().encode(MINT_TO_DISCRIMINATOR);
- }
- export type MintToInstruction<
- TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
- TAccountMint extends string | IAccountMeta<string> = string,
- TAccountToken extends string | IAccountMeta<string> = string,
- TAccountMintAuthority extends string | IAccountMeta<string> = string,
- TRemainingAccounts extends readonly IAccountMeta<string>[] = [],
- > = IInstruction<TProgram> &
- IInstructionWithData<Uint8Array> &
- IInstructionWithAccounts<
- [
- TAccountMint extends string
- ? WritableAccount<TAccountMint>
- : TAccountMint,
- TAccountToken extends string
- ? WritableAccount<TAccountToken>
- : TAccountToken,
- TAccountMintAuthority extends string
- ? ReadonlyAccount<TAccountMintAuthority>
- : TAccountMintAuthority,
- ...TRemainingAccounts,
- ]
- >;
- export type MintToInstructionData = {
- discriminator: number;
- /** The amount of new tokens to mint. */
- amount: bigint;
- };
- export type MintToInstructionDataArgs = {
- /** The amount of new tokens to mint. */
- amount: number | bigint;
- };
- export function getMintToInstructionDataEncoder(): Encoder<MintToInstructionDataArgs> {
- return transformEncoder(
- getStructEncoder([
- ['discriminator', getU8Encoder()],
- ['amount', getU64Encoder()],
- ]),
- (value) => ({ ...value, discriminator: MINT_TO_DISCRIMINATOR })
- );
- }
- export function getMintToInstructionDataDecoder(): Decoder<MintToInstructionData> {
- return getStructDecoder([
- ['discriminator', getU8Decoder()],
- ['amount', getU64Decoder()],
- ]);
- }
- export function getMintToInstructionDataCodec(): Codec<
- MintToInstructionDataArgs,
- MintToInstructionData
- > {
- return combineCodec(
- getMintToInstructionDataEncoder(),
- getMintToInstructionDataDecoder()
- );
- }
- export type MintToInput<
- TAccountMint extends string = string,
- TAccountToken extends string = string,
- TAccountMintAuthority extends string = string,
- > = {
- /** The mint account. */
- mint: Address<TAccountMint>;
- /** The account to mint tokens to. */
- token: Address<TAccountToken>;
- /** The mint's minting authority or its multisignature account. */
- mintAuthority:
- | Address<TAccountMintAuthority>
- | TransactionSigner<TAccountMintAuthority>;
- amount: MintToInstructionDataArgs['amount'];
- multiSigners?: Array<TransactionSigner>;
- };
- export function getMintToInstruction<
- TAccountMint extends string,
- TAccountToken extends string,
- TAccountMintAuthority extends string,
- TProgramAddress extends Address = typeof TOKEN_PROGRAM_ADDRESS,
- >(
- input: MintToInput<TAccountMint, TAccountToken, TAccountMintAuthority>,
- config?: { programAddress?: TProgramAddress }
- ): MintToInstruction<
- TProgramAddress,
- TAccountMint,
- TAccountToken,
- (typeof input)['mintAuthority'] extends TransactionSigner<TAccountMintAuthority>
- ? ReadonlySignerAccount<TAccountMintAuthority> &
- IAccountSignerMeta<TAccountMintAuthority>
- : TAccountMintAuthority
- > {
- // Program address.
- const programAddress = config?.programAddress ?? TOKEN_PROGRAM_ADDRESS;
- // Original accounts.
- const originalAccounts = {
- mint: { value: input.mint ?? null, isWritable: true },
- token: { value: input.token ?? null, isWritable: true },
- mintAuthority: { value: input.mintAuthority ?? null, isWritable: false },
- };
- const accounts = originalAccounts as Record<
- keyof typeof originalAccounts,
- ResolvedAccount
- >;
- // Original args.
- const args = { ...input };
- // Remaining accounts.
- const remainingAccounts: IAccountMeta[] = (args.multiSigners ?? []).map(
- (signer) => ({
- address: signer.address,
- role: AccountRole.READONLY_SIGNER,
- signer,
- })
- );
- const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
- const instruction = {
- accounts: [
- getAccountMeta(accounts.mint),
- getAccountMeta(accounts.token),
- getAccountMeta(accounts.mintAuthority),
- ...remainingAccounts,
- ],
- programAddress,
- data: getMintToInstructionDataEncoder().encode(
- args as MintToInstructionDataArgs
- ),
- } as MintToInstruction<
- TProgramAddress,
- TAccountMint,
- TAccountToken,
- (typeof input)['mintAuthority'] extends TransactionSigner<TAccountMintAuthority>
- ? ReadonlySignerAccount<TAccountMintAuthority> &
- IAccountSignerMeta<TAccountMintAuthority>
- : TAccountMintAuthority
- >;
- return instruction;
- }
- export type ParsedMintToInstruction<
- TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
- TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
- > = {
- programAddress: Address<TProgram>;
- accounts: {
- /** The mint account. */
- mint: TAccountMetas[0];
- /** The account to mint tokens to. */
- token: TAccountMetas[1];
- /** The mint's minting authority or its multisignature account. */
- mintAuthority: TAccountMetas[2];
- };
- data: MintToInstructionData;
- };
- export function parseMintToInstruction<
- TProgram extends string,
- TAccountMetas extends readonly IAccountMeta[],
- >(
- instruction: IInstruction<TProgram> &
- IInstructionWithAccounts<TAccountMetas> &
- IInstructionWithData<Uint8Array>
- ): ParsedMintToInstruction<TProgram, TAccountMetas> {
- if (instruction.accounts.length < 3) {
- // TODO: Coded error.
- throw new Error('Not enough accounts');
- }
- let accountIndex = 0;
- const getNextAccount = () => {
- const accountMeta = instruction.accounts![accountIndex]!;
- accountIndex += 1;
- return accountMeta;
- };
- return {
- programAddress: instruction.programAddress,
- accounts: {
- mint: getNextAccount(),
- token: getNextAccount(),
- mintAuthority: getNextAccount(),
- },
- data: getMintToInstructionDataDecoder().decode(instruction.data),
- };
- }
|