Skip to content

Commit

Permalink
Add more helpers to get init instructions for extensions
Browse files Browse the repository at this point in the history
lorisleiva committed Oct 10, 2024
1 parent 1f85414 commit dac96d9
Showing 3 changed files with 60 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -6,7 +6,12 @@ import {
getInitializeTransferFeeConfigInstruction,
} from './generated';

export function getInitializeInstructionsForMintExtensions(
/**
* Given a mint address and a list of mint extensions, returns a list of
* instructions that MUST be run _before_ the `initializeMint` instruction
* to properly initialize the given extensions on the mint account.
*/
export function getPreInitializeInstructionsForMintExtensions(
mint: Address,
extensions: ExtensionArgs[]
): IInstruction[] {
@@ -42,3 +47,37 @@ export function getInitializeInstructionsForMintExtensions(
}
});
}

/**
* Given a mint address and a list of mint extensions, returns a list of
* instructions that MUST be run _after_ the `initializeMint` instruction
* to properly initialize the given extensions on the mint account.
*/
export function getPostInitializeInstructionsForMintExtensions(
_mint: Address,
extensions: ExtensionArgs[]
): IInstruction[] {
return extensions.flatMap((extension) => {
switch (extension.__kind) {
default:
return [];
}
});
}

/**
* Given a token address and a list of token extensions, returns a list of
* instructions that MUST be run _after_ the `initializeAccount` instruction
* to properly initialize the given extensions on the token account.
*/
export function getPostInitializeInstructionsForTokenExtensions(
_token: Address,
extensions: ExtensionArgs[]
): IInstruction[] {
return extensions.flatMap((extension) => {
switch (extension.__kind) {
default:
return [];
}
});
}
2 changes: 1 addition & 1 deletion clients/js/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './generated';

export * from './getInitializeInstructionsForMintExtensions';
export * from './getInitializeInstructionsForExtensions';
export * from './getTokenSize';
export * from './getMintSize';
23 changes: 19 additions & 4 deletions clients/js/test/_setup.ts
Original file line number Diff line number Diff line change
@@ -28,11 +28,12 @@ import {
ExtensionArgs,
TOKEN_2022_PROGRAM_ADDRESS,
getInitializeAccountInstruction,
getInitializeInstructionsForMintExtensions,
getPreInitializeInstructionsForMintExtensions,
getInitializeMintInstruction,
getMintSize,
getMintToInstruction,
getTokenSize,
getPostInitializeInstructionsForMintExtensions,
} from '../src';

type Client = {
@@ -170,11 +171,15 @@ export const createMint = async (
});
await sendAndConfirmInstructions(input.client, input.payer, [
createAccount,
...getInitializeInstructionsForMintExtensions(
...getPreInitializeInstructionsForMintExtensions(
mint.address,
input.extensions ?? []
),
initMint,
...getPostInitializeInstructionsForMintExtensions(
mint.address,
input.extensions ?? []
),
]);
return mint.address;
};
@@ -183,8 +188,18 @@ export const createToken = async (
input: Omit<Parameters<typeof getCreateTokenInstructions>[0], 'token'>
): Promise<Address> => {
const token = await generateKeyPairSigner();
const instructions = await getCreateTokenInstructions({ ...input, token });
await sendAndConfirmInstructions(input.client, input.payer, instructions);
const [createAccount, initToken] = await getCreateTokenInstructions({
...input,
token,
});
await sendAndConfirmInstructions(input.client, input.payer, [
createAccount,
initToken,
...getPostInitializeInstructionsForMintExtensions(
token.address,
input.extensions ?? []
),
]);
return token.address;
};

0 comments on commit dac96d9

Please sign in to comment.