Skip to content

Commit

Permalink
added program idl and test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
adpthegreat committed Nov 13, 2024
1 parent 45ca918 commit 4bc93aa
Show file tree
Hide file tree
Showing 6 changed files with 356 additions and 2 deletions.
5 changes: 3 additions & 2 deletions clients/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@
"rewritePaths": {
"test/": "dist/test/"
}
}
},
"timeout": "60s"
},
"packageManager": "[email protected]"
}
}
203 changes: 203 additions & 0 deletions clients/js/src/generated/instructions/createNativeMint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
/**
* 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 = 27;

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> = string,
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,
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,
TAccountSystemProgram extends string = string,
> = {
/** Funding account (must be a system account) */
payer: TransactionSigner<TAccountPayer>;
/** The native mint address */
nativeMint: Address<TAccountNativeMint>;
/** System program for mint account funding */
systemProgram?: Address<TAccountSystemProgram>;
};

export function getCreateNativeMintInstruction<
TAccountPayer extends string,
TAccountNativeMint extends string,
TAccountSystemProgram extends string,
TProgramAddress extends Address = typeof TOKEN_2022_PROGRAM_ADDRESS,
>(
input: CreateNativeMintInput<
TAccountPayer,
TAccountNativeMint,
TAccountSystemProgram
>,
config?: { programAddress?: TProgramAddress }
): CreateNativeMintInstruction<
TProgramAddress,
TAccountPayer,
TAccountNativeMint,
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 },
systemProgram: { value: input.systemProgram ?? null, isWritable: false },
};
const accounts = originalAccounts as Record<
keyof typeof originalAccounts,
ResolvedAccount
>;

// Resolve default values.
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.systemProgram),
],
programAddress,
data: getCreateNativeMintInstructionDataEncoder().encode({}),
} as CreateNativeMintInstruction<
TProgramAddress,
TAccountPayer,
TAccountNativeMint,
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 (must be a system account) */
payer: TAccountMetas[0];
/** The native mint address */
nativeMint: TAccountMetas[1];
/** System program for mint account funding */
systemProgram: TAccountMetas[2];
};
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 < 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: {
payer: getNextAccount(),
nativeMint: 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 './disableCpiGuard';
export * from './disableMemoTransfers';
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 ParsedDisableCpiGuardInstruction,
type ParsedDisableMemoTransfersInstruction,
Expand Down Expand Up @@ -113,6 +114,7 @@ export function identifyToken2022Account(

export enum Token2022Instruction {
InitializeMint,
CreateNativeMint,
InitializeAccount,
InitializeMultisig,
Transfer,
Expand Down Expand Up @@ -190,6 +192,9 @@ export function identifyToken2022Instruction(
if (containsBytes(data, getU8Encoder().encode(0), 0)) {
return Token2022Instruction.InitializeMint;
}
if (containsBytes(data, getU8Encoder().encode(27), 0)) {
return Token2022Instruction.CreateNativeMint;
}
if (containsBytes(data, getU8Encoder().encode(1), 0)) {
return Token2022Instruction.InitializeAccount;
}
Expand Down Expand Up @@ -547,6 +552,9 @@ export type ParsedToken2022Instruction<
| ({
instructionType: Token2022Instruction.InitializeMint;
} & ParsedInitializeMintInstruction<TProgram>)
| ({
instructionType: Token2022Instruction.CreateNativeMint;
} & ParsedCreateNativeMintInstruction<TProgram>)
| ({
instructionType: Token2022Instruction.InitializeAccount;
} & ParsedInitializeAccountInstruction<TProgram>)
Expand Down
69 changes: 69 additions & 0 deletions clients/js/test/createNativeMint.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Account, address, none,} from '@solana/web3.js';
import test from 'ava';
import {
Mint,
fetchMint,
getCreateNativeMintInstruction,
} from '../src';
import {
createDefaultSolanaClient,
generateKeyPairSignerWithSol,
sendAndConfirmInstructions,
} from './_setup';

//Mint for native SOL Token accounts
const NATIVE_MINT = address("9pan9bMn5HatX4EJdBwg9VgCa7Uz5HL8N1m5D3NdXejP");

test('it creates a native mint account', async (t) => {
// Given a payer account.
const client = createDefaultSolanaClient();
const payer = await generateKeyPairSignerWithSol(client);

// When we create a native mint account.
await sendAndConfirmInstructions(client, payer, [
getCreateNativeMintInstruction({
payer: payer,
nativeMint: NATIVE_MINT,
})
]);

// Then we expect the native mint account to exist with the following data.
const nativeMintAccount = await fetchMint(client.rpc, NATIVE_MINT);
t.like(nativeMintAccount, <Account<Mint>>{
address: NATIVE_MINT,
data: {
mintAuthority: none(), // Native mint has no mint authority
supply: 0n,
decimals: 9, // Native SOL has 9 decimals
isInitialized: true,
freezeAuthority: none(),
extensions: none(), // Native mint doesn't have extensions
},
});
});

test('it fails to create native mint if it already exists', async (t) => {
// Given a payer and a mint account
const client = createDefaultSolanaClient();
const payer = await generateKeyPairSignerWithSol(client);

// When we try to create the same native mint twice
await sendAndConfirmInstructions(client, payer, [
getCreateNativeMintInstruction({
payer: payer,
nativeMint: NATIVE_MINT,
}),
]);

const promise = sendAndConfirmInstructions(client, payer, [
getCreateNativeMintInstruction({
payer: payer,
nativeMint: NATIVE_MINT,
}),
]);

// Then we expect the operation to fail
await t.throwsAsync(promise, {
message: /Account already exists/,
});
});
Loading

0 comments on commit 4bc93aa

Please sign in to comment.