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

Commit 2062613

Browse files
author
Joe C
authored
token-js: refactor metadata pointer tests
The `MetadataPointer` tests are a little bit difficult to follow, so this change refactors them to be a bit more legible, so onlookers can see what's going on and learn how to use the `MetadataPointer` instructions & state from observing the tests.
1 parent 4114325 commit 2062613

File tree

1 file changed

+61
-60
lines changed

1 file changed

+61
-60
lines changed

token/js/test/unit/metadataPointer.test.ts

Lines changed: 61 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -8,133 +8,134 @@ import {
88
getMetadataPointerState,
99
} from '../../src';
1010

11-
describe('SPL Token 2022 Metadata Extension', () => {
12-
it('can create createInitializeMetadataPointerInstruction', () => {
13-
const mint = PublicKey.unique();
14-
const authority = new PublicKey('1111111QLbz7JHiBTspS962RLKV8GndWFwiEaqKM');
15-
const metadataAddress = new PublicKey('1111111ogCyDbaRMvkdsHB3qfdyFYaG1WtRUAfdh');
11+
const AUTHORITY_ADDRESS_BYTES = Buffer.alloc(32).fill(8);
12+
const METADATA_ADDRESS_BYTES = Buffer.alloc(32).fill(5);
13+
const NULL_OPTIONAL_NONZERO_PUBKEY_BYTES = Buffer.alloc(32).fill(0);
1614

15+
describe('SPL Token 2022 MetadataPointer Extension', () => {
16+
it('can create InitializeMetadataPointerInstruction', () => {
17+
const mint = PublicKey.unique();
18+
const authority = new PublicKey(AUTHORITY_ADDRESS_BYTES);
19+
const metadataAddress = new PublicKey(METADATA_ADDRESS_BYTES);
1720
const instruction = createInitializeMetadataPointerInstruction(
1821
mint,
1922
authority,
2023
metadataAddress,
2124
TOKEN_2022_PROGRAM_ID
2225
);
23-
2426
expect(instruction).to.deep.equal(
2527
new TransactionInstruction({
2628
programId: TOKEN_2022_PROGRAM_ID,
2729
keys: [{ isSigner: false, isWritable: true, pubkey: mint }],
28-
data: Buffer.from([
29-
// Output of rust implementation
30-
39, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
31-
0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
30+
data: Buffer.concat([
31+
Buffer.from([
32+
39, // Token instruction discriminator
33+
0, // MetadataPointer instruction discriminator
34+
]),
35+
AUTHORITY_ADDRESS_BYTES,
36+
METADATA_ADDRESS_BYTES,
3237
]),
3338
})
3439
);
3540
});
36-
37-
it('can create createUpdateMetadataPointerInstruction', () => {
41+
it('can create UpdateMetadataPointerInstruction', () => {
3842
const mint = PublicKey.unique();
3943
const authority = PublicKey.unique();
40-
const metadataAddress = new PublicKey('11111112cMQwSC9qirWGjZM6gLGwW69X22mqwLLGP');
41-
44+
const metadataAddress = new PublicKey(METADATA_ADDRESS_BYTES);
4245
const instruction = createUpdateMetadataPointerInstruction(mint, authority, metadataAddress);
43-
4446
expect(instruction).to.deep.equal(
4547
new TransactionInstruction({
4648
programId: TOKEN_2022_PROGRAM_ID,
4749
keys: [
4850
{ isSigner: false, isWritable: true, pubkey: mint },
4951
{ isSigner: true, isWritable: false, pubkey: authority },
5052
],
51-
data: Buffer.from([
52-
// Output of rust implementation
53-
39, 1, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
54-
0,
53+
data: Buffer.concat([
54+
Buffer.from([
55+
39, // Token instruction discriminator
56+
1, // MetadataPointer instruction discriminator
57+
]),
58+
METADATA_ADDRESS_BYTES,
5559
]),
5660
})
5761
);
5862
});
59-
60-
it('can create createUpdateMetadataPointerInstruction to none', () => {
63+
it('can create UpdateMetadataPointerInstruction to none', () => {
6164
const mint = PublicKey.unique();
6265
const authority = PublicKey.unique();
6366
const metadataAddress = null;
64-
6567
const instruction = createUpdateMetadataPointerInstruction(mint, authority, metadataAddress);
66-
6768
expect(instruction).to.deep.equal(
6869
new TransactionInstruction({
6970
programId: TOKEN_2022_PROGRAM_ID,
7071
keys: [
7172
{ isSigner: false, isWritable: true, pubkey: mint },
7273
{ isSigner: true, isWritable: false, pubkey: authority },
7374
],
74-
data: Buffer.from([
75-
// Output of rust implementation
76-
39, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77-
0,
75+
data: Buffer.concat([
76+
Buffer.from([
77+
39, // Token instruction discriminator
78+
1, // MetadataPointer instruction discriminator
79+
]),
80+
NULL_OPTIONAL_NONZERO_PUBKEY_BYTES,
7881
]),
7982
})
8083
);
8184
});
82-
8385
it('can get state with authority and metadata address', async () => {
8486
const mintInfo = {
85-
tlvData: Buffer.from([
86-
18, 0, 64, 0, 134, 125, 9, 16, 205, 223, 26, 224, 220, 174, 52, 213, 193, 216, 9, 80, 82, 181, 8, 228,
87-
75, 112, 233, 116, 2, 183, 51, 228, 88, 64, 179, 158, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
88-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
87+
tlvData: Buffer.concat([
88+
Buffer.from([
89+
// Extension discriminator
90+
18, 0,
91+
// Extension length
92+
64, 0,
93+
]),
94+
AUTHORITY_ADDRESS_BYTES,
95+
METADATA_ADDRESS_BYTES,
8996
]),
9097
} as Mint;
91-
9298
const metadataPointer = getMetadataPointerState(mintInfo);
93-
9499
expect(metadataPointer).to.deep.equal({
95-
authority: new PublicKey([
96-
134, 125, 9, 16, 205, 223, 26, 224, 220, 174, 52, 213, 193, 216, 9, 80, 82, 181, 8, 228, 75, 112, 233,
97-
116, 2, 183, 51, 228, 88, 64, 179, 158,
98-
]),
99-
metadataAddress: new PublicKey([
100-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
101-
]),
100+
authority: new PublicKey(AUTHORITY_ADDRESS_BYTES),
101+
metadataAddress: new PublicKey(METADATA_ADDRESS_BYTES),
102102
});
103103
});
104104
it('can get state with only metadata address', async () => {
105105
const mintInfo = {
106-
tlvData: Buffer.from([
107-
18, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
108-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
106+
tlvData: Buffer.concat([
107+
Buffer.from([
108+
// Extension discriminator
109+
18, 0,
110+
// Extension length
111+
64, 0,
112+
]),
113+
NULL_OPTIONAL_NONZERO_PUBKEY_BYTES,
114+
METADATA_ADDRESS_BYTES,
109115
]),
110116
} as Mint;
111-
112117
const metadataPointer = getMetadataPointerState(mintInfo);
113-
114118
expect(metadataPointer).to.deep.equal({
115119
authority: null,
116-
metadataAddress: new PublicKey([
117-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
118-
]),
120+
metadataAddress: new PublicKey(METADATA_ADDRESS_BYTES),
119121
});
120122
});
121-
122123
it('can get state with only authority address', async () => {
123124
const mintInfo = {
124-
tlvData: Buffer.from([
125-
18, 0, 64, 0, 16, 218, 238, 42, 17, 19, 152, 173, 216, 24, 229, 204, 215, 108, 49, 98, 233, 115, 53,
126-
252, 9, 156, 216, 23, 14, 157, 139, 132, 28, 182, 4, 191, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
127-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
125+
tlvData: Buffer.concat([
126+
Buffer.from([
127+
// Extension discriminator
128+
18, 0,
129+
// Extension length
130+
64, 0,
131+
]),
132+
AUTHORITY_ADDRESS_BYTES,
133+
NULL_OPTIONAL_NONZERO_PUBKEY_BYTES,
128134
]),
129135
} as Mint;
130-
131136
const metadataPointer = getMetadataPointerState(mintInfo);
132-
133137
expect(metadataPointer).to.deep.equal({
134-
authority: new PublicKey([
135-
16, 218, 238, 42, 17, 19, 152, 173, 216, 24, 229, 204, 215, 108, 49, 98, 233, 115, 53, 252, 9, 156, 216,
136-
23, 14, 157, 139, 132, 28, 182, 4, 191,
137-
]),
138+
authority: new PublicKey(AUTHORITY_ADDRESS_BYTES),
138139
metadataAddress: null,
139140
});
140141
});

0 commit comments

Comments
 (0)