Skip to content

Commit 3b990b6

Browse files
authored
js: bump to 1.6.0, add test for parseSystemInstruction (#69)
1 parent b5e92aa commit 3b990b6

8 files changed

Lines changed: 1013 additions & 641 deletions

File tree

clients/js/.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src/generated/**

clients/js/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,18 @@
4343
},
4444
"homepage": "https://github.com/solana-program/system#readme",
4545
"peerDependencies": {
46-
"@solana/kit": "^5.0"
46+
"@solana/kit": "^6.0.0"
4747
},
4848
"devDependencies": {
49-
"@solana/eslint-config-solana": "^5.0.0",
50-
"@solana/kit": "^5.0",
49+
"@solana/eslint-config-solana": "^6.0.0",
50+
"@solana/kit": "^6.0.0",
5151
"@types/node": "^24",
52-
"eslint": "^9.35.0",
52+
"eslint": "^9.39.2",
5353
"prettier": "^3.7.4",
5454
"rimraf": "^6.1.2",
5555
"tsup": "^8.1.2",
5656
"typedoc": "^0.28.15",
57-
"typescript": "^5.5.3",
57+
"typescript": "^5.9.3",
5858
"vitest": "^4.0.15"
5959
}
6060
}

clients/js/pnpm-lock.yaml

Lines changed: 721 additions & 527 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

clients/js/src/generated/programs/system.ts

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,29 @@
66
* @see https://github.com/codama-idl/codama
77
*/
88

9-
import { containsBytes, getU32Encoder, type Address, type ReadonlyUint8Array } from '@solana/kit';
109
import {
10+
assertIsInstructionWithAccounts,
11+
containsBytes,
12+
getU32Encoder,
13+
type Address,
14+
type Instruction,
15+
type InstructionWithData,
16+
type ReadonlyUint8Array,
17+
} from '@solana/kit';
18+
import {
19+
parseAdvanceNonceAccountInstruction,
20+
parseAllocateInstruction,
21+
parseAllocateWithSeedInstruction,
22+
parseAssignInstruction,
23+
parseAssignWithSeedInstruction,
24+
parseAuthorizeNonceAccountInstruction,
25+
parseCreateAccountInstruction,
26+
parseCreateAccountWithSeedInstruction,
27+
parseInitializeNonceAccountInstruction,
28+
parseTransferSolInstruction,
29+
parseTransferSolWithSeedInstruction,
30+
parseUpgradeNonceAccountInstruction,
31+
parseWithdrawNonceAccountInstruction,
1132
type ParsedAdvanceNonceAccountInstruction,
1233
type ParsedAllocateInstruction,
1334
type ParsedAllocateWithSeedInstruction,
@@ -107,3 +128,92 @@ export type ParsedSystemInstruction<TProgram extends string = '11111111111111111
107128
| ({ instructionType: SystemInstruction.AssignWithSeed } & ParsedAssignWithSeedInstruction<TProgram>)
108129
| ({ instructionType: SystemInstruction.TransferSolWithSeed } & ParsedTransferSolWithSeedInstruction<TProgram>)
109130
| ({ instructionType: SystemInstruction.UpgradeNonceAccount } & ParsedUpgradeNonceAccountInstruction<TProgram>);
131+
132+
export function parseSystemInstruction<TProgram extends string>(
133+
instruction: Instruction<TProgram> & InstructionWithData<ReadonlyUint8Array>,
134+
): ParsedSystemInstruction<TProgram> {
135+
const instructionType = identifySystemInstruction(instruction);
136+
switch (instructionType) {
137+
case SystemInstruction.CreateAccount: {
138+
assertIsInstructionWithAccounts(instruction);
139+
return { instructionType: SystemInstruction.CreateAccount, ...parseCreateAccountInstruction(instruction) };
140+
}
141+
case SystemInstruction.Assign: {
142+
assertIsInstructionWithAccounts(instruction);
143+
return { instructionType: SystemInstruction.Assign, ...parseAssignInstruction(instruction) };
144+
}
145+
case SystemInstruction.TransferSol: {
146+
assertIsInstructionWithAccounts(instruction);
147+
return { instructionType: SystemInstruction.TransferSol, ...parseTransferSolInstruction(instruction) };
148+
}
149+
case SystemInstruction.CreateAccountWithSeed: {
150+
assertIsInstructionWithAccounts(instruction);
151+
return {
152+
instructionType: SystemInstruction.CreateAccountWithSeed,
153+
...parseCreateAccountWithSeedInstruction(instruction),
154+
};
155+
}
156+
case SystemInstruction.AdvanceNonceAccount: {
157+
assertIsInstructionWithAccounts(instruction);
158+
return {
159+
instructionType: SystemInstruction.AdvanceNonceAccount,
160+
...parseAdvanceNonceAccountInstruction(instruction),
161+
};
162+
}
163+
case SystemInstruction.WithdrawNonceAccount: {
164+
assertIsInstructionWithAccounts(instruction);
165+
return {
166+
instructionType: SystemInstruction.WithdrawNonceAccount,
167+
...parseWithdrawNonceAccountInstruction(instruction),
168+
};
169+
}
170+
case SystemInstruction.InitializeNonceAccount: {
171+
assertIsInstructionWithAccounts(instruction);
172+
return {
173+
instructionType: SystemInstruction.InitializeNonceAccount,
174+
...parseInitializeNonceAccountInstruction(instruction),
175+
};
176+
}
177+
case SystemInstruction.AuthorizeNonceAccount: {
178+
assertIsInstructionWithAccounts(instruction);
179+
return {
180+
instructionType: SystemInstruction.AuthorizeNonceAccount,
181+
...parseAuthorizeNonceAccountInstruction(instruction),
182+
};
183+
}
184+
case SystemInstruction.Allocate: {
185+
assertIsInstructionWithAccounts(instruction);
186+
return { instructionType: SystemInstruction.Allocate, ...parseAllocateInstruction(instruction) };
187+
}
188+
case SystemInstruction.AllocateWithSeed: {
189+
assertIsInstructionWithAccounts(instruction);
190+
return {
191+
instructionType: SystemInstruction.AllocateWithSeed,
192+
...parseAllocateWithSeedInstruction(instruction),
193+
};
194+
}
195+
case SystemInstruction.AssignWithSeed: {
196+
assertIsInstructionWithAccounts(instruction);
197+
return {
198+
instructionType: SystemInstruction.AssignWithSeed,
199+
...parseAssignWithSeedInstruction(instruction),
200+
};
201+
}
202+
case SystemInstruction.TransferSolWithSeed: {
203+
assertIsInstructionWithAccounts(instruction);
204+
return {
205+
instructionType: SystemInstruction.TransferSolWithSeed,
206+
...parseTransferSolWithSeedInstruction(instruction),
207+
};
208+
}
209+
case SystemInstruction.UpgradeNonceAccount: {
210+
assertIsInstructionWithAccounts(instruction);
211+
return {
212+
instructionType: SystemInstruction.UpgradeNonceAccount,
213+
...parseUpgradeNonceAccountInstruction(instruction),
214+
};
215+
}
216+
default:
217+
throw new Error(`Unrecognized instruction type: ${instructionType as string}`);
218+
}
219+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { AccountRole, address, createNoopSigner } from '@solana/kit';
2+
import {
3+
getTransferSolInstruction,
4+
parseSystemInstruction,
5+
SYSTEM_PROGRAM_ADDRESS,
6+
SystemInstruction,
7+
TRANSFER_SOL_DISCRIMINATOR,
8+
} from '../src';
9+
import { it, expect } from 'vitest';
10+
11+
it('parses a transfer SOL instruction', () => {
12+
const source = createNoopSigner(address('9aE3e5LQT2qKDpnoYytMVNFq7ZtbBFCpTyvKUJUx5MkX'));
13+
const destination = address('HgnzqSaAXxcw3acG33VRMMjsuDSCVr6oT56iyY9BWmzH');
14+
15+
// Given an instruction that transfers SOL from one account to another.
16+
const transferSolInstruction = getTransferSolInstruction({
17+
source,
18+
destination,
19+
amount: 1_000_000_000,
20+
});
21+
22+
// When we parse this instruction.
23+
const parsedInstruction = parseSystemInstruction(transferSolInstruction);
24+
25+
// Then we expect it to be a transfer SOL instruction with the correct accounts and data.
26+
expect(parsedInstruction.instructionType).toStrictEqual(SystemInstruction.TransferSol);
27+
expect(parsedInstruction.programAddress).toBe(SYSTEM_PROGRAM_ADDRESS);
28+
expect(parsedInstruction.accounts).toStrictEqual({
29+
source: {
30+
address: source.address,
31+
role: AccountRole.WRITABLE_SIGNER,
32+
signer: source,
33+
},
34+
destination: {
35+
address: destination,
36+
role: AccountRole.WRITABLE,
37+
},
38+
});
39+
expect(parsedInstruction.data).toStrictEqual({
40+
amount: 1_000_000_000n,
41+
discriminator: TRANSFER_SOL_DISCRIMINATOR,
42+
});
43+
});

clients/js/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@
2020
"target": "ESNext"
2121
},
2222
"exclude": ["node_modules"],
23-
"include": ["src", "test"]
23+
"include": ["src", "test", "*.ts"]
2424
}

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
"generate:clients": "codama run --all"
66
},
77
"devDependencies": {
8-
"@codama/renderers-js": "^1.5.5",
8+
"@codama/renderers-js": "^1.7.0",
99
"@codama/renderers-rust": "~1.0",
10-
"@solana/prettier-config-solana": "0.0.5",
11-
"codama": "^1.4.4"
10+
"@solana/prettier-config-solana": "0.0.6",
11+
"codama": "^1.5.0"
1212
},
1313
"engines": {
1414
"node": ">=v20.0.0"

0 commit comments

Comments
 (0)