Skip to content

Commit f733892

Browse files
authored
Add getInitializeInstructionsForMintExtensions helper (#10)
1 parent 91ffbcb commit f733892

File tree

7 files changed

+115
-111
lines changed

7 files changed

+115
-111
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { Address, IInstruction } from '@solana/web3.js';
2+
import {
3+
ExtensionArgs,
4+
getInitializeConfidentialTransferMintInstruction,
5+
getInitializeDefaultAccountStateInstruction,
6+
getInitializeTransferFeeConfigInstruction,
7+
} from './generated';
8+
9+
export function getInitializeInstructionsForMintExtensions(
10+
mint: Address,
11+
extensions: ExtensionArgs[]
12+
): IInstruction[] {
13+
return extensions.flatMap((extension) => {
14+
switch (extension.__kind) {
15+
case 'ConfidentialTransferMint':
16+
return [
17+
getInitializeConfidentialTransferMintInstruction({
18+
mint,
19+
...extension,
20+
}),
21+
];
22+
case 'DefaultAccountState':
23+
return [
24+
getInitializeDefaultAccountStateInstruction({
25+
mint,
26+
state: extension.state,
27+
}),
28+
];
29+
case 'TransferFeeConfig':
30+
return [
31+
getInitializeTransferFeeConfigInstruction({
32+
mint,
33+
transferFeeConfigAuthority: extension.transferFeeConfigAuthority,
34+
withdrawWithheldAuthority: extension.withdrawWithheldAuthority,
35+
transferFeeBasisPoints:
36+
extension.newerTransferFee.transferFeeBasisPoints,
37+
maximumFee: extension.newerTransferFee.maximumFee,
38+
}),
39+
];
40+
default:
41+
return [];
42+
}
43+
});
44+
}

clients/js/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './generated';
22

3+
export * from './getInitializeInstructionsForMintExtensions';
34
export * from './getTokenSize';
45
export * from './getMintSize';

clients/js/test/_setup.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
ExtensionArgs,
2929
TOKEN_2022_PROGRAM_ADDRESS,
3030
getInitializeAccountInstruction,
31+
getInitializeInstructionsForMintExtensions,
3132
getInitializeMintInstruction,
3233
getMintSize,
3334
getMintToInstruction,
@@ -163,8 +164,18 @@ export const createMint = async (
163164
input: Omit<Parameters<typeof getCreateMintInstructions>[0], 'mint'>
164165
): Promise<Address> => {
165166
const mint = await generateKeyPairSigner();
166-
const instructions = await getCreateMintInstructions({ ...input, mint });
167-
await sendAndConfirmInstructions(input.client, input.payer, instructions);
167+
const [createAccount, initMint] = await getCreateMintInstructions({
168+
...input,
169+
mint,
170+
});
171+
await sendAndConfirmInstructions(input.client, input.payer, [
172+
createAccount,
173+
...getInitializeInstructionsForMintExtensions(
174+
mint.address,
175+
input.extensions ?? []
176+
),
177+
initMint,
178+
]);
168179
return mint.address;
169180
};
170181

clients/js/test/extensions/confidentialTransfer/updateConfidentialTransferMint.test.ts

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,64 +10,53 @@ import {
1010
Mint,
1111
extension,
1212
fetchMint,
13-
getInitializeConfidentialTransferMintInstruction,
1413
getUpdateConfidentialTransferMintInstruction,
1514
} from '../../../src';
1615
import {
1716
createDefaultSolanaClient,
17+
createMint,
1818
generateKeyPairSignerWithSol,
19-
getCreateMintInstructions,
2019
sendAndConfirmInstructions,
2120
} from '../../_setup';
2221

2322
test('it updates a mint account with confidential transfer', async (t) => {
2423
// Given some signer accounts.
2524
const client = createDefaultSolanaClient();
26-
const [authority, confidentialTransferAuthority, mint] = await Promise.all([
25+
const [authority, confidentialTransferAuthority] = await Promise.all([
2726
generateKeyPairSignerWithSol(client),
2827
generateKeyPairSigner(),
29-
generateKeyPairSigner(),
3028
]);
3129

3230
// And a mint account initialized with a confidential transfer extension.
33-
const confidentialTransferExtension = extension('ConfidentialTransferMint', {
34-
authority: some(confidentialTransferAuthority.address),
35-
autoApproveNewAccounts: true,
36-
auditorElgamalPubkey: some(
37-
address('BTNEPmmWuj7Sg4Fo5i1FC5eiV2Aj4jiv9boarvE5XeaX')
38-
),
31+
const mint = await createMint({
32+
authority: authority.address,
33+
client,
34+
extensions: [
35+
extension('ConfidentialTransferMint', {
36+
authority: some(confidentialTransferAuthority.address),
37+
autoApproveNewAccounts: true,
38+
auditorElgamalPubkey: some(
39+
address('BTNEPmmWuj7Sg4Fo5i1FC5eiV2Aj4jiv9boarvE5XeaX')
40+
),
41+
}),
42+
],
43+
payer: authority,
3944
});
40-
const [createMintInstruction, initMintInstruction] =
41-
await getCreateMintInstructions({
42-
authority: authority.address,
43-
client,
44-
extensions: [confidentialTransferExtension],
45-
mint,
46-
payer: authority,
47-
});
48-
await sendAndConfirmInstructions(client, authority, [
49-
createMintInstruction,
50-
getInitializeConfidentialTransferMintInstruction({
51-
mint: mint.address,
52-
...confidentialTransferExtension,
53-
}),
54-
initMintInstruction,
55-
]);
5645

5746
// When we update the mint account with new confidential transfer configs.
5847
await sendAndConfirmInstructions(client, authority, [
5948
getUpdateConfidentialTransferMintInstruction({
60-
mint: mint.address,
49+
mint,
6150
authority: confidentialTransferAuthority,
6251
autoApproveNewAccounts: false,
6352
auditorElgamalPubkey: none(),
6453
}),
6554
]);
6655

6756
// Then we expect the mint account to have the following updated data.
68-
const mintAccount = await fetchMint(client.rpc, mint.address);
57+
const mintAccount = await fetchMint(client.rpc, mint);
6958
t.like(mintAccount, <Account<Mint>>{
70-
address: mint.address,
59+
address: mint,
7160
data: {
7261
mintAuthority: some(authority.address),
7362
extensions: some([

clients/js/test/extensions/defaultAccountState/initializeDefaultAccountState.test.ts

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from '../../../src';
1111
import {
1212
createDefaultSolanaClient,
13+
createMint,
1314
createToken,
1415
generateKeyPairSignerWithSol,
1516
getCreateMintInstructions,
@@ -64,38 +65,26 @@ test('it initializes a mint account with a default account state extension', asy
6465
test('it initializes a token account with the default state defined on the mint account', async (t) => {
6566
// Given some signer accounts.
6667
const client = createDefaultSolanaClient();
67-
const [authority, freezeAuthority, mint] = await Promise.all([
68+
const [authority, freezeAuthority] = await Promise.all([
6869
generateKeyPairSignerWithSol(client),
6970
generateKeyPairSigner(),
70-
generateKeyPairSigner(),
7171
]);
7272

7373
// And a mint account initialized with a default account state extension.
74-
const defaultAccountStateExtension = extension('DefaultAccountState', {
75-
state: AccountState.Frozen,
74+
const mint = await createMint({
75+
authority: authority.address,
76+
client,
77+
extensions: [
78+
extension('DefaultAccountState', { state: AccountState.Frozen }),
79+
],
80+
freezeAuthority: freezeAuthority.address,
81+
payer: authority,
7682
});
77-
const [createMintInstruction, initMintInstruction] =
78-
await getCreateMintInstructions({
79-
authority: authority.address,
80-
client,
81-
extensions: [defaultAccountStateExtension],
82-
freezeAuthority: freezeAuthority.address,
83-
mint,
84-
payer: authority,
85-
});
86-
await sendAndConfirmInstructions(client, authority, [
87-
createMintInstruction,
88-
getInitializeDefaultAccountStateInstruction({
89-
mint: mint.address,
90-
state: defaultAccountStateExtension.state,
91-
}),
92-
initMintInstruction,
93-
]);
9483

9584
// When we create a new token account for the mint.
9685
const token = await createToken({
9786
client,
98-
mint: mint.address,
87+
mint,
9988
owner: address('HHS1XymmkBpYAkg3XTbZLxgHa5n11PAWUCWdiVtRmzzS'),
10089
payer: authority,
10190
});

clients/js/test/extensions/defaultAccountState/updateDefaultAccountState.test.ts

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,60 +5,47 @@ import {
55
Mint,
66
extension,
77
fetchMint,
8-
getInitializeDefaultAccountStateInstruction,
98
getUpdateDefaultAccountStateInstruction,
109
} from '../../../src';
1110
import {
1211
createDefaultSolanaClient,
12+
createMint,
1313
generateKeyPairSignerWithSol,
14-
getCreateMintInstructions,
1514
sendAndConfirmInstructions,
1615
} from '../../_setup';
1716

1817
test('it updates the default state account on a mint account', async (t) => {
1918
// Given some signer accounts.
2019
const client = createDefaultSolanaClient();
21-
const [authority, freezeAuthority, mint] = await Promise.all([
20+
const [authority, freezeAuthority] = await Promise.all([
2221
generateKeyPairSignerWithSol(client),
2322
generateKeyPairSigner(),
24-
generateKeyPairSigner(),
2523
]);
2624

2725
// And a mint account initialized with a default account state extension.
28-
const defaultAccountStateExtension = extension('DefaultAccountState', {
29-
state: AccountState.Frozen,
26+
const mint = await createMint({
27+
authority: authority.address,
28+
client,
29+
extensions: [
30+
extension('DefaultAccountState', { state: AccountState.Frozen }),
31+
],
32+
freezeAuthority: freezeAuthority.address,
33+
payer: authority,
3034
});
31-
const [createMintInstruction, initMintInstruction] =
32-
await getCreateMintInstructions({
33-
authority: authority.address,
34-
client,
35-
extensions: [defaultAccountStateExtension],
36-
freezeAuthority: freezeAuthority.address,
37-
mint,
38-
payer: authority,
39-
});
40-
await sendAndConfirmInstructions(client, authority, [
41-
createMintInstruction,
42-
getInitializeDefaultAccountStateInstruction({
43-
mint: mint.address,
44-
state: defaultAccountStateExtension.state,
45-
}),
46-
initMintInstruction,
47-
]);
4835

4936
// When we update the default account state on the mint account.
5037
await sendAndConfirmInstructions(client, authority, [
5138
getUpdateDefaultAccountStateInstruction({
52-
mint: mint.address,
39+
mint,
5340
freezeAuthority,
5441
state: AccountState.Initialized,
5542
}),
5643
]);
5744

5845
// Then we expect the mint account to have the following updated data.
59-
const mintAccount = await fetchMint(client.rpc, mint.address);
46+
const mintAccount = await fetchMint(client.rpc, mint);
6047
t.like(mintAccount, <Account<Mint>>{
61-
address: mint.address,
48+
address: mint,
6249
data: {
6350
extensions: some([
6451
extension('DefaultAccountState', { state: AccountState.Initialized }),

0 commit comments

Comments
 (0)