Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add create native mint with test #37

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
231 changes: 231 additions & 0 deletions clients/js/src/generated/instructions/createNativeMint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
/**
* 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,
getStructDecoder,
getStructEncoder,
getU8Decoder,
getU8Encoder,
transformEncoder,
type Address,
type Codec,
type Decoder,
type Encoder,
type IAccountMeta,
type IAccountSignerMeta,
type IInstruction,
type IInstructionWithAccounts,
type IInstructionWithData,
type ReadonlyAccount,
type TransactionSigner,
type WritableAccount,
type WritableSignerAccount,
} from '@solana/web3.js';
import { TOKEN_2022_PROGRAM_ADDRESS } from '../programs';
import { getAccountMetaFactory, type ResolvedAccount } from '../shared';

export const CREATE_NATIVE_MINT_DISCRIMINATOR = 31;

export function getCreateNativeMintDiscriminatorBytes() {
return getU8Encoder().encode(CREATE_NATIVE_MINT_DISCRIMINATOR);
}

export type CreateNativeMintInstruction<
TProgram extends string = typeof TOKEN_2022_PROGRAM_ADDRESS,
TAccountPayer extends string | IAccountMeta<string> = string,
TAccountNativeMint extends
| string
| IAccountMeta<string> = '9pan9bMn5HatX4EJdBwg9VgCa7Uz5HL8N1m5D3NdXejP',
TAccountTokenProgram extends
| string
| IAccountMeta<string> = 'TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb',
TAccountSystemProgram extends
| string
| IAccountMeta<string> = '11111111111111111111111111111111',
TRemainingAccounts extends readonly IAccountMeta<string>[] = [],
> = IInstruction<TProgram> &
IInstructionWithData<Uint8Array> &
IInstructionWithAccounts<
[
TAccountPayer extends string
? WritableSignerAccount<TAccountPayer> &
IAccountSignerMeta<TAccountPayer>
: TAccountPayer,
TAccountNativeMint extends string
? WritableAccount<TAccountNativeMint>
: TAccountNativeMint,
TAccountTokenProgram extends string
? ReadonlyAccount<TAccountTokenProgram>
: TAccountTokenProgram,
TAccountSystemProgram extends string
? ReadonlyAccount<TAccountSystemProgram>
: TAccountSystemProgram,
...TRemainingAccounts,
]
>;

export type CreateNativeMintInstructionData = { discriminator: number };

export type CreateNativeMintInstructionDataArgs = {};

export function getCreateNativeMintInstructionDataEncoder(): Encoder<CreateNativeMintInstructionDataArgs> {
return transformEncoder(
getStructEncoder([['discriminator', getU8Encoder()]]),
(value) => ({ ...value, discriminator: CREATE_NATIVE_MINT_DISCRIMINATOR })
);
}

export function getCreateNativeMintInstructionDataDecoder(): Decoder<CreateNativeMintInstructionData> {
return getStructDecoder([['discriminator', getU8Decoder()]]);
}

export function getCreateNativeMintInstructionDataCodec(): Codec<
CreateNativeMintInstructionDataArgs,
CreateNativeMintInstructionData
> {
return combineCodec(
getCreateNativeMintInstructionDataEncoder(),
getCreateNativeMintInstructionDataDecoder()
);
}

export type CreateNativeMintInput<
TAccountPayer extends string = string,
TAccountNativeMint extends string = string,
TAccountTokenProgram extends string = string,
TAccountSystemProgram extends string = string,
> = {
/** Funding account */
payer: TransactionSigner<TAccountPayer>;
/** The native mint address */
nativeMint?: Address<TAccountNativeMint>;
/** SPL Token program */
tokenProgram?: Address<TAccountTokenProgram>;
/** System program for reallocation funding. */
systemProgram?: Address<TAccountSystemProgram>;
};

export function getCreateNativeMintInstruction<
TAccountPayer extends string,
TAccountNativeMint extends string,
TAccountTokenProgram extends string,
TAccountSystemProgram extends string,
TProgramAddress extends Address = typeof TOKEN_2022_PROGRAM_ADDRESS,
>(
input: CreateNativeMintInput<
TAccountPayer,
TAccountNativeMint,
TAccountTokenProgram,
TAccountSystemProgram
>,
config?: { programAddress?: TProgramAddress }
): CreateNativeMintInstruction<
TProgramAddress,
TAccountPayer,
TAccountNativeMint,
TAccountTokenProgram,
TAccountSystemProgram
> {
// Program address.
const programAddress = config?.programAddress ?? TOKEN_2022_PROGRAM_ADDRESS;

// Original accounts.
const originalAccounts = {
payer: { value: input.payer ?? null, isWritable: true },
nativeMint: { value: input.nativeMint ?? null, isWritable: true },
tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
systemProgram: { value: input.systemProgram ?? null, isWritable: false },
};
const accounts = originalAccounts as Record<
keyof typeof originalAccounts,
ResolvedAccount
>;

// Resolve default values.
if (!accounts.nativeMint.value) {
accounts.nativeMint.value =
'9pan9bMn5HatX4EJdBwg9VgCa7Uz5HL8N1m5D3NdXejP' as Address<'9pan9bMn5HatX4EJdBwg9VgCa7Uz5HL8N1m5D3NdXejP'>;
}
if (!accounts.tokenProgram.value) {
accounts.tokenProgram.value =
'TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb' as Address<'TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb'>;
}
if (!accounts.systemProgram.value) {
accounts.systemProgram.value =
'11111111111111111111111111111111' as Address<'11111111111111111111111111111111'>;
}

const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
const instruction = {
accounts: [
getAccountMeta(accounts.payer),
getAccountMeta(accounts.nativeMint),
getAccountMeta(accounts.tokenProgram),
getAccountMeta(accounts.systemProgram),
],
programAddress,
data: getCreateNativeMintInstructionDataEncoder().encode({}),
} as CreateNativeMintInstruction<
TProgramAddress,
TAccountPayer,
TAccountNativeMint,
TAccountTokenProgram,
TAccountSystemProgram
>;

return instruction;
}

export type ParsedCreateNativeMintInstruction<
TProgram extends string = typeof TOKEN_2022_PROGRAM_ADDRESS,
TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
> = {
programAddress: Address<TProgram>;
accounts: {
/** Funding account */
payer: TAccountMetas[0];
/** The native mint address */
nativeMint: TAccountMetas[1];
/** SPL Token program */
tokenProgram: TAccountMetas[2];
/** System program for reallocation funding. */
systemProgram: TAccountMetas[3];
};
data: CreateNativeMintInstructionData;
};

export function parseCreateNativeMintInstruction<
TProgram extends string,
TAccountMetas extends readonly IAccountMeta[],
>(
instruction: IInstruction<TProgram> &
IInstructionWithAccounts<TAccountMetas> &
IInstructionWithData<Uint8Array>
): ParsedCreateNativeMintInstruction<TProgram, TAccountMetas> {
if (instruction.accounts.length < 4) {
// 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: {
payer: getNextAccount(),
nativeMint: getNextAccount(),
tokenProgram: getNextAccount(),
systemProgram: getNextAccount(),
},
data: getCreateNativeMintInstructionDataDecoder().decode(instruction.data),
};
}
1 change: 1 addition & 0 deletions clients/js/src/generated/instructions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export * from './confidentialWithdraw';
export * from './configureConfidentialTransferAccount';
export * from './createAssociatedToken';
export * from './createAssociatedTokenIdempotent';
export * from './createNativeMint';
export * from './disableConfidentialCredits';
export * from './disableMemoTransfers';
export * from './disableNonConfidentialCredits';
Expand Down
8 changes: 8 additions & 0 deletions clients/js/src/generated/programs/token2022.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
type ParsedConfidentialTransferWithFeeInstruction,
type ParsedConfidentialWithdrawInstruction,
type ParsedConfigureConfidentialTransferAccountInstruction,
type ParsedCreateNativeMintInstruction,
type ParsedDisableConfidentialCreditsInstruction,
type ParsedDisableMemoTransfersInstruction,
type ParsedDisableNonConfidentialCreditsInstruction,
Expand Down Expand Up @@ -125,6 +126,7 @@ export enum Token2022Instruction {
ApproveChecked,
MintToChecked,
BurnChecked,
CreateNativeMint,
InitializeAccount2,
SyncNative,
InitializeAccount3,
Expand Down Expand Up @@ -229,6 +231,9 @@ export function identifyToken2022Instruction(
if (containsBytes(data, getU8Encoder().encode(15), 0)) {
return Token2022Instruction.BurnChecked;
}
if (containsBytes(data, getU8Encoder().encode(31), 0)) {
return Token2022Instruction.CreateNativeMint;
}
if (containsBytes(data, getU8Encoder().encode(16), 0)) {
return Token2022Instruction.InitializeAccount2;
}
Expand Down Expand Up @@ -571,6 +576,9 @@ export type ParsedToken2022Instruction<
| ({
instructionType: Token2022Instruction.BurnChecked;
} & ParsedBurnCheckedInstruction<TProgram>)
| ({
instructionType: Token2022Instruction.CreateNativeMint;
} & ParsedCreateNativeMintInstruction<TProgram>)
| ({
instructionType: Token2022Instruction.InitializeAccount2;
} & ParsedInitializeAccount2Instruction<TProgram>)
Expand Down
40 changes: 40 additions & 0 deletions clients/js/test/createNativeMint.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Account, address, none } from '@solana/web3.js';
import test from 'ava';
import { Mint, fetchMint, getCreateNativeMintInstruction } from '../src';
import {
createDefaultSolanaClient,
generateKeyPairSignerWithSol,
sendAndConfirmInstructions,
} from './_setup';

// Define the native SOL Token mint address
const SOL_MINT_ADDRESS = address(
'9pan9bMn5HatX4EJdBwg9VgCa7Uz5HL8N1m5D3NdXejP'
);

test('it successfully initializes a native mint account', async (t) => {
// Setup: create a Solana client and payer account
const solanaClient = createDefaultSolanaClient();
const payerKeyPair = await generateKeyPairSignerWithSol(solanaClient);

// Action: create the native mint account using the instruction
await sendAndConfirmInstructions(solanaClient, payerKeyPair, [
getCreateNativeMintInstruction({
payer: payerKeyPair,
}),
]);

// Verification: check if the mint account exists with the expected data
const mintAccount = await fetchMint(solanaClient.rpc, SOL_MINT_ADDRESS);
t.like(mintAccount, <Account<Mint>>{
address: SOL_MINT_ADDRESS,
data: {
mintAuthority: none(), // No authority for native mint
supply: 0n,
decimals: 9, // 9 decimals for SOL
isInitialized: true,
freezeAuthority: none(),
extensions: none(), // No extensions for the native mint
},
});
});
Loading