Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit e4596f7

Browse files
committed
Hardcode the discriminators so that you don't have to compute them at runtime
1 parent b1e526d commit e4596f7

File tree

6 files changed

+60
-35
lines changed

6 files changed

+60
-35
lines changed

token-group/js/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@
4646
"@solana/web3.js": "^1.95.3"
4747
},
4848
"dependencies": {
49-
"@solana/codecs": "2.0.0-rc.1",
50-
"@solana/spl-type-length-value": "0.1.0"
49+
"@solana/codecs": "2.0.0-rc.1"
5150
},
5251
"devDependencies": {
52+
"@solana/spl-type-length-value": "0.2.0",
5353
"@solana/web3.js": "^1.95.3",
5454
"@types/chai": "^5.0.0",
5555
"@types/mocha": "^10.0.9",

token-group/js/src/instruction.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
getU64Encoder,
99
transformEncoder,
1010
} from '@solana/codecs';
11-
import { splDiscriminate } from '@solana/spl-type-length-value';
1211
import { SystemProgram, TransactionInstruction } from '@solana/web3.js';
1312

1413
function getInstructionEncoder<T extends object>(discriminator: Uint8Array, dataEncoder: Encoder<T>): Encoder<T> {
@@ -43,7 +42,10 @@ export function createInitializeGroupInstruction(args: InitializeGroupInstructio
4342
],
4443
data: Buffer.from(
4544
getInstructionEncoder(
46-
splDiscriminate('spl_token_group_interface:initialize_token_group'),
45+
new Uint8Array([
46+
/* await splDiscriminate('spl_token_group_interface:initialize_token_group') */
47+
121, 113, 108, 39, 54, 51, 0, 4,
48+
]),
4749
getStructEncoder([
4850
['updateAuthority', getPublicKeyEncoder()],
4951
['maxSize', getU64Encoder()],
@@ -70,7 +72,10 @@ export function createUpdateGroupMaxSizeInstruction(args: UpdateGroupMaxSize): T
7072
],
7173
data: Buffer.from(
7274
getInstructionEncoder(
73-
splDiscriminate('spl_token_group_interface:update_group_max_size'),
75+
new Uint8Array([
76+
/* await splDiscriminate('spl_token_group_interface:update_group_max_size') */
77+
108, 37, 171, 143, 248, 30, 18, 110,
78+
]),
7479
getStructEncoder([['maxSize', getU64Encoder()]]),
7580
).encode({ maxSize }),
7681
),
@@ -95,7 +100,10 @@ export function createUpdateGroupAuthorityInstruction(args: UpdateGroupAuthority
95100
],
96101
data: Buffer.from(
97102
getInstructionEncoder(
98-
splDiscriminate('spl_token_group_interface:update_authority'),
103+
new Uint8Array([
104+
/* await splDiscriminate('spl_token_group_interface:update_authority') */
105+
161, 105, 88, 1, 237, 221, 216, 203,
106+
]),
99107
getStructEncoder([['newAuthority', getPublicKeyEncoder()]]),
100108
).encode({ newAuthority: newAuthority ?? SystemProgram.programId }),
101109
),
@@ -125,7 +133,10 @@ export function createInitializeMemberInstruction(args: InitializeMember): Trans
125133
],
126134
data: Buffer.from(
127135
getInstructionEncoder(
128-
splDiscriminate('spl_token_group_interface:initialize_member'),
136+
new Uint8Array([
137+
/* await splDiscriminate('spl_token_group_interface:initialize_member') */
138+
152, 32, 222, 176, 223, 237, 116, 134,
139+
]),
129140
getStructEncoder([]),
130141
).encode({}),
131142
),

token-group/js/test/instruction.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('Token Group Instructions', () => {
3030
const mintAuthority = new PublicKey('66666666666666666666666666666666666666666666');
3131
const maxSize = BigInt(100);
3232

33-
it('Can create InitializeGroup Instruction', () => {
33+
it('Can create InitializeGroup Instruction', async () => {
3434
checkPackUnpack(
3535
createInitializeGroupInstruction({
3636
programId,
@@ -40,7 +40,7 @@ describe('Token Group Instructions', () => {
4040
updateAuthority,
4141
maxSize,
4242
}),
43-
splDiscriminate('spl_token_group_interface:initialize_token_group'),
43+
await splDiscriminate('spl_token_group_interface:initialize_token_group'),
4444
getStructDecoder([
4545
['updateAuthority', fixDecoderSize(getBytesDecoder(), 32)],
4646
['maxSize', getU64Decoder()],
@@ -49,35 +49,35 @@ describe('Token Group Instructions', () => {
4949
);
5050
});
5151

52-
it('Can create UpdateGroupMaxSize Instruction', () => {
52+
it('Can create UpdateGroupMaxSize Instruction', async () => {
5353
checkPackUnpack(
5454
createUpdateGroupMaxSizeInstruction({
5555
programId,
5656
group,
5757
updateAuthority,
5858
maxSize,
5959
}),
60-
splDiscriminate('spl_token_group_interface:update_group_max_size'),
60+
await splDiscriminate('spl_token_group_interface:update_group_max_size'),
6161
getStructDecoder([['maxSize', getU64Decoder()]]),
6262
{ maxSize },
6363
);
6464
});
6565

66-
it('Can create UpdateGroupAuthority Instruction', () => {
66+
it('Can create UpdateGroupAuthority Instruction', async () => {
6767
checkPackUnpack(
6868
createUpdateGroupAuthorityInstruction({
6969
programId,
7070
group,
7171
currentAuthority: updateAuthority,
7272
newAuthority: PublicKey.default,
7373
}),
74-
splDiscriminate('spl_token_group_interface:update_authority'),
74+
await splDiscriminate('spl_token_group_interface:update_authority'),
7575
getStructDecoder([['newAuthority', fixDecoderSize(getBytesDecoder(), 32)]]),
7676
{ newAuthority: Uint8Array.from(PublicKey.default.toBuffer()) },
7777
);
7878
});
7979

80-
it('Can create InitializeMember Instruction', () => {
80+
it('Can create InitializeMember Instruction', async () => {
8181
const member = new PublicKey('22222222222222222222222222222222222222222222');
8282
const memberMint = new PublicKey('33333333333333333333333333333333333333333333');
8383
const memberMintAuthority = new PublicKey('44444444444444444444444444444444444444444444');
@@ -93,7 +93,7 @@ describe('Token Group Instructions', () => {
9393
group,
9494
groupUpdateAuthority,
9595
}),
96-
splDiscriminate('spl_token_group_interface:initialize_member'),
96+
await splDiscriminate('spl_token_group_interface:initialize_member'),
9797
getStructDecoder([]),
9898
{},
9999
);

token-metadata/js/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@
4646
"@solana/web3.js": "^1.95.3"
4747
},
4848
"dependencies": {
49-
"@solana/codecs": "2.0.0-rc.1",
50-
"@solana/spl-type-length-value": "0.1.0"
49+
"@solana/codecs": "2.0.0-rc.1"
5150
},
5251
"devDependencies": {
52+
"@solana/spl-type-length-value": "0.2.0",
5353
"@solana/web3.js": "^1.95.3",
5454
"@types/chai": "^5.0.0",
5555
"@types/mocha": "^10.0.9",

token-metadata/js/src/instruction.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
transformEncoder,
1515
} from '@solana/codecs';
1616
import type { VariableSizeEncoder } from '@solana/codecs';
17-
import { splDiscriminate } from '@solana/spl-type-length-value';
1817
import type { PublicKey } from '@solana/web3.js';
1918
import { SystemProgram, TransactionInstruction } from '@solana/web3.js';
2019

@@ -66,7 +65,10 @@ export function createInitializeInstruction(args: InitializeInstructionArgs): Tr
6665
],
6766
data: Buffer.from(
6867
getInstructionEncoder(
69-
splDiscriminate('spl_token_metadata_interface:initialize_account'),
68+
new Uint8Array([
69+
/* await splDiscriminate('spl_token_metadata_interface:initialize_account') */
70+
210, 225, 30, 162, 88, 184, 77, 141,
71+
]),
7072
getStructEncoder([
7173
['name', getStringEncoder()],
7274
['symbol', getStringEncoder()],
@@ -99,7 +101,10 @@ export function createUpdateFieldInstruction(args: UpdateFieldInstruction): Tran
99101
],
100102
data: Buffer.from(
101103
getInstructionEncoder(
102-
splDiscriminate('spl_token_metadata_interface:updating_field'),
104+
new Uint8Array([
105+
/* await splDiscriminate('spl_token_metadata_interface:updating_field') */
106+
221, 233, 49, 45, 181, 202, 220, 200,
107+
]),
103108
getStructEncoder([
104109
['field', getDataEnumCodec(getFieldCodec())],
105110
['value', getStringEncoder()],
@@ -127,7 +132,10 @@ export function createRemoveKeyInstruction(args: RemoveKeyInstructionArgs) {
127132
],
128133
data: Buffer.from(
129134
getInstructionEncoder(
130-
splDiscriminate('spl_token_metadata_interface:remove_key_ix'),
135+
new Uint8Array([
136+
/* await splDiscriminate('spl_token_metadata_interface:remove_key_ix') */
137+
234, 18, 32, 56, 89, 141, 37, 181,
138+
]),
131139
getStructEncoder([
132140
['idempotent', getBooleanEncoder()],
133141
['key', getStringEncoder()],
@@ -155,7 +163,10 @@ export function createUpdateAuthorityInstruction(args: UpdateAuthorityInstructio
155163
],
156164
data: Buffer.from(
157165
getInstructionEncoder(
158-
splDiscriminate('spl_token_metadata_interface:update_the_authority'),
166+
new Uint8Array([
167+
/* await splDiscriminate('spl_token_metadata_interface:update_the_authority') */
168+
215, 228, 166, 228, 84, 100, 86, 123,
169+
]),
159170
getStructEncoder([['newAuthority', getPublicKeyEncoder()]]),
160171
).encode({ newAuthority: newAuthority ?? SystemProgram.programId }),
161172
),
@@ -176,7 +187,10 @@ export function createEmitInstruction(args: EmitInstructionArgs): TransactionIns
176187
keys: [{ isSigner: false, isWritable: false, pubkey: metadata }],
177188
data: Buffer.from(
178189
getInstructionEncoder(
179-
splDiscriminate('spl_token_metadata_interface:emitter'),
190+
new Uint8Array([
191+
/* await splDiscriminate('spl_token_metadata_interface:emitter') */
192+
250, 166, 180, 250, 13, 12, 184, 70,
193+
]),
180194
getStructEncoder([
181195
['start', getOptionEncoder(getU64Encoder())],
182196
['end', getOptionEncoder(getU64Encoder())],

token-metadata/js/test/instruction.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('Token Metadata Instructions', () => {
4848
const mint = new PublicKey('55555555555555555555555555555555555555555555');
4949
const mintAuthority = new PublicKey('66666666666666666666666666666666666666666666');
5050

51-
it('Can create Initialize Instruction', () => {
51+
it('Can create Initialize Instruction', async () => {
5252
const name = 'My test token';
5353
const symbol = 'TEST';
5454
const uri = 'http://test.test';
@@ -63,7 +63,7 @@ describe('Token Metadata Instructions', () => {
6363
symbol,
6464
uri,
6565
}),
66-
splDiscriminate('spl_token_metadata_interface:initialize_account'),
66+
await splDiscriminate('spl_token_metadata_interface:initialize_account'),
6767
getStructDecoder([
6868
['name', getStringDecoder()],
6969
['symbol', getStringDecoder()],
@@ -73,7 +73,7 @@ describe('Token Metadata Instructions', () => {
7373
);
7474
});
7575

76-
it('Can create Update Field Instruction', () => {
76+
it('Can create Update Field Instruction', async () => {
7777
const field = 'MyTestField';
7878
const value = 'http://test.uri';
7979
checkPackUnpack(
@@ -84,7 +84,7 @@ describe('Token Metadata Instructions', () => {
8484
field,
8585
value,
8686
}),
87-
splDiscriminate('spl_token_metadata_interface:updating_field'),
87+
await splDiscriminate('spl_token_metadata_interface:updating_field'),
8888
getStructDecoder([
8989
['key', getDataEnumCodec(getFieldCodec())],
9090
['value', getStringDecoder()],
@@ -93,7 +93,7 @@ describe('Token Metadata Instructions', () => {
9393
);
9494
});
9595

96-
it('Can create Update Field Instruction with Field Enum', () => {
96+
it('Can create Update Field Instruction with Field Enum', async () => {
9797
const field = 'Name';
9898
const value = 'http://test.uri';
9999
checkPackUnpack(
@@ -104,7 +104,7 @@ describe('Token Metadata Instructions', () => {
104104
field,
105105
value,
106106
}),
107-
splDiscriminate('spl_token_metadata_interface:updating_field'),
107+
await splDiscriminate('spl_token_metadata_interface:updating_field'),
108108
getStructDecoder([
109109
['key', getDataEnumCodec(getFieldCodec())],
110110
['value', getStringDecoder()],
@@ -113,7 +113,7 @@ describe('Token Metadata Instructions', () => {
113113
);
114114
});
115115

116-
it('Can create Remove Key Instruction', () => {
116+
it('Can create Remove Key Instruction', async () => {
117117
checkPackUnpack(
118118
createRemoveKeyInstruction({
119119
programId,
@@ -122,7 +122,7 @@ describe('Token Metadata Instructions', () => {
122122
key: 'MyTestField',
123123
idempotent: true,
124124
}),
125-
splDiscriminate('spl_token_metadata_interface:remove_key_ix'),
125+
await splDiscriminate('spl_token_metadata_interface:remove_key_ix'),
126126
getStructDecoder([
127127
['idempotent', getBooleanDecoder()],
128128
['key', getStringDecoder()],
@@ -131,7 +131,7 @@ describe('Token Metadata Instructions', () => {
131131
);
132132
});
133133

134-
it('Can create Update Authority Instruction', () => {
134+
it('Can create Update Authority Instruction', async () => {
135135
const newAuthority = PublicKey.default;
136136
checkPackUnpack(
137137
createUpdateAuthorityInstruction({
@@ -140,13 +140,13 @@ describe('Token Metadata Instructions', () => {
140140
oldAuthority: updateAuthority,
141141
newAuthority,
142142
}),
143-
splDiscriminate('spl_token_metadata_interface:update_the_authority'),
143+
await splDiscriminate('spl_token_metadata_interface:update_the_authority'),
144144
getStructDecoder([['newAuthority', fixDecoderSize(getBytesDecoder(), 32)]]),
145145
{ newAuthority: Uint8Array.from(newAuthority.toBuffer()) },
146146
);
147147
});
148148

149-
it('Can create Emit Instruction', () => {
149+
it('Can create Emit Instruction', async () => {
150150
const start: Option<bigint> = some(0n);
151151
const end: Option<bigint> = some(10n);
152152
checkPackUnpack(
@@ -156,7 +156,7 @@ describe('Token Metadata Instructions', () => {
156156
start: 0n,
157157
end: 10n,
158158
}),
159-
splDiscriminate('spl_token_metadata_interface:emitter'),
159+
await splDiscriminate('spl_token_metadata_interface:emitter'),
160160
getStructDecoder([
161161
['start', getOptionDecoder(getU64Decoder())],
162162
['end', getOptionDecoder(getU64Decoder())],

0 commit comments

Comments
 (0)