@typesol/cli 0.38.0 → 0.40.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/CHANGELOG.md +373 -2
  2. package/README.md +43 -26
  3. package/SECURITY.md +1 -1
  4. package/dist/cli.js +37 -3
  5. package/dist/compiler/analyze.d.ts +6 -11
  6. package/dist/compiler/analyze.js +331 -34
  7. package/dist/compiler/emit/alias.js +3 -2
  8. package/dist/compiler/emit/base.d.ts +7 -1
  9. package/dist/compiler/emit/base.js +2 -0
  10. package/dist/compiler/emit/borsh.js +63 -2
  11. package/dist/compiler/emit/calls.d.ts +14 -1
  12. package/dist/compiler/emit/calls.js +311 -21
  13. package/dist/compiler/emit/cpi-core.js +17 -5
  14. package/dist/compiler/emit/cpi.js +81 -27
  15. package/dist/compiler/emit/create.d.ts +7 -0
  16. package/dist/compiler/emit/create.js +27 -0
  17. package/dist/compiler/emit/expressions.d.ts +6 -0
  18. package/dist/compiler/emit/expressions.js +102 -6
  19. package/dist/compiler/emit/function.d.ts +7 -0
  20. package/dist/compiler/emit/function.js +164 -29
  21. package/dist/compiler/emit/program.js +27 -2
  22. package/dist/compiler/emit/shared.d.ts +15 -5
  23. package/dist/compiler/emit/shared.js +5 -0
  24. package/dist/compiler/emit/statements.d.ts +28 -1
  25. package/dist/compiler/emit/statements.js +270 -13
  26. package/dist/compiler/emit/storage.d.ts +10 -1
  27. package/dist/compiler/emit/storage.js +50 -11
  28. package/dist/compiler/hints.d.ts +5 -0
  29. package/dist/compiler/hints.js +88 -0
  30. package/dist/compiler/idl.d.ts +1 -1
  31. package/dist/compiler/idl.js +42 -11
  32. package/dist/compiler/index.d.ts +3 -0
  33. package/dist/compiler/index.js +6 -1
  34. package/dist/compiler/model.d.ts +47 -10
  35. package/dist/compiler/model.js +14 -4
  36. package/dist/testing.js +8 -0
  37. package/dist/tools/clientgen.js +10 -1
  38. package/dist/tools/portgen.d.ts +6 -0
  39. package/dist/tools/portgen.js +68 -17
  40. package/docs/architecture.md +5 -4
  41. package/docs/audit.md +31 -19
  42. package/docs/case-study-escrow.md +5 -5
  43. package/docs/case-study-jito-mev.md +9 -5
  44. package/docs/case-study-marginfi.md +797 -0
  45. package/docs/case-study-multisig.md +3 -3
  46. package/docs/case-study-raydium-amm.md +80 -0
  47. package/docs/case-study-raydium-clmm.md +44 -33
  48. package/docs/case-study-raydium-cp-swap.md +3 -3
  49. package/docs/cli.md +5 -5
  50. package/docs/client.md +1 -1
  51. package/docs/errors.md +13 -5
  52. package/docs/language.md +65 -32
  53. package/docs/migrate-from-anchor.md +4 -1
  54. package/docs/runtime.md +1 -1
  55. package/docs/security-model.md +2 -1
  56. package/docs/testing.md +34 -1
  57. package/docs/troubleshooting.md +6 -0
  58. package/lib/typesol.d.ts +118 -10
  59. package/package.json +9 -7
  60. package/runtime/typesol_rt.h +158 -17
  61. package/ts-plugin/index.js +24 -1
@@ -3,8 +3,8 @@
3
3
  This page ports a program that holds real authority on mainnet:
4
4
  [coral-xyz/multisig](https://github.com/coral-xyz/multisig) (`programs/multisig/src/lib.rs`, Anchor 0.25, Apache-2.0),
5
5
  deployed at `msigUdDBsR4zSUYqYEDrc1LcgtmuSDDM7KxpRUXNC6U`. The port is
6
- [`examples/multisig.ts`](https://github.com/typesol/typesol/blob/main/examples/multisig.ts). Instead of a twin built for the occasion, it is checked against the
7
- binary that runs on mainnet: [`test/examples/multisig.test.ts`](https://github.com/typesol/typesol/blob/main/test/examples/multisig.test.ts) runs both under the
6
+ [`showcase/mainnet/multisig/multisig.ts`](https://github.com/typesol-lang/typesol/blob/main/showcase/mainnet/multisig/multisig.ts). Instead of a twin built for the occasion, it is checked against the
7
+ binary that runs on mainnet: [`showcase/test/multisig.test.ts`](https://github.com/typesol-lang/typesol/blob/main/showcase/test/multisig.test.ts) runs both under the
8
8
  same program id and compares every outcome and every account byte. See
9
9
  [case-study-escrow.md](case-study-escrow.md) for a token program and [Migrate from Anchor](migrate-from-anchor.md) for
10
10
  the general reference.
@@ -99,7 +99,7 @@ existing clients and existing accounts work with the port unchanged.
99
99
  ## How it was checked
100
100
 
101
101
  `test/programs/coral_multisig.so` is the program dumped from mainnet on 2026-10-29 (sha256 in
102
- [`test/programs/SOURCES.md`](https://github.com/typesol/typesol/blob/main/test/programs/SOURCES.md)). The test loads it into one LiteSVM and the port into
102
+ [`test/programs/SOURCES.md`](https://github.com/typesol-lang/typesol/blob/main/test/programs/SOURCES.md)). The test loads it into one LiteSVM and the port into
103
103
  another, both at `msigUdDB…`, then sends each transaction to both. After each one it requires the same outcome (success,
104
104
  or the same error down to the code) and the same lamports, owner and data bytes for every account involved.
105
105
 
@@ -0,0 +1,80 @@
1
+ # Case study: Raydium AMM v4
2
+
3
+ This page ports [Raydium AMM v4](https://github.com/raydium-io/raydium-amm), Raydium's original constant-product AMM,
4
+ from `program/src` at d26944b (Apache-2.0). The port is checked against the binary that runs on mainnet as
5
+ `675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8` (deployed in slot 445763204, dumped in slot 466115597), not a rebuild.
6
+
7
+ Unlike the other ports here, AMM v4 is a native program: no Anchor. Its entrypoint reads a tag byte and the arguments
8
+ after it, takes its accounts by position (a swap accepts 17 or 18 of them, the v2 swaps 8), keeps its accounts as
9
+ `#[repr(C, packed)]` structs with no discriminator, and fails with built-in program errors and panics as often as with
10
+ its own errors. The port keeps all of that.
11
+
12
+ | File | Holds |
13
+ | --- | --- |
14
+ | [`raydium_amm.ts`](../showcase/mainnet/raydium-amm/raydium_amm.ts) | the entrypoint (one `@fallback` over `instructionData()` and `remainingAccounts()`), every instruction, the token and system CPIs, the P&L calculation |
15
+ | [`state.ts`](../showcase/mainnet/raydium-amm/state.ts) | AmmInfo (752 bytes), TargetOrders, AmmConfig, Fees, the ray_log records, the 61 errors (codes 0–60) |
16
+ | [`math.ts`](../showcase/mainnet/raydium-amm/math.ts) | decimal normalization, the constant-product amounts, the exchange rates, with the original's panics at their places |
17
+
18
+ ## What is ported
19
+
20
+ All of it. The live instructions: Initialize2 (tag 1, both its 19- and 21-account forms), Deposit (3, 11 or 14–15
21
+ accounts, the optional minimum), Withdraw (4, 11 or 20–23 accounts, the optional minimums), SetParams (6: status, state,
22
+ fees, open time), WithdrawPnl (7), SwapBaseIn / SwapBaseOut (9 / 11, 17 or 18 accounts), CreateConfigAccount (14),
23
+ UpdateConfigAccount (15), SwapBaseInV2 / SwapBaseOutV2 (16 / 17, 8 accounts) and WithdrawExcessLamports (18). The
24
+ retired ones (0, 2, 5, 8, 10, 12, 13) panic with `not implemented` at the original's source location, as it does.
25
+ The order book (OpenBook) was removed upstream in July 2026; its accounts are still accepted where the original
26
+ accepts them, and ignored.
27
+
28
+ ## What it needed
29
+
30
+ TypeSOL gained what a native program needs (docs/language.md, "Porting a native program"):
31
+
32
+ - `loadRaw<T>` / `loadRawMut<T>`: accounts with no discriminator, read from byte 0. The port makes the original's own
33
+ checks first (owner, an exact size, its error codes), then loads.
34
+ - `instructionData().readU64(offset)` and the other little-endian readers, for the hand-packed arguments (optional
35
+ ones are present when enough bytes follow).
36
+ - Errors from code 0 (returned as Solana's CUSTOM_ZERO, as `ProgramError::Custom(0)` is), `ProgramError.X` for the
37
+ built-in errors (`NotEnoughAccountKeys`, `InvalidInstructionData`, `InsufficientFunds`), `panic(message, location)`
38
+ and `PANIC_ON_OVERFLOW` for `unwrap()` and checked arithmetic, `ERROR_LOG_PREFIX` / `PROGRAM_ERROR_LOG_PREFIX` for the
39
+ entrypoint's "AMM error: …" / "Program error: …" lines.
40
+ - `logBase64` for the ray_log records (bincode of a packed struct, base64), `createProgramAddress` for the authority
41
+ derived from the stored nonce, and `isqrt` of a u256 (the P&L uses U256).
42
+
43
+ ## How it is checked
44
+
45
+ `showcase/test/raydium-amm.test.ts` runs the mainnet binary and the port in two LiteSVMs under the same program id
46
+ and clock. Raydium's three fixed keys (its admin, the pool fee destination and the lamports collector) are swapped for
47
+ test keys in both, in the original where its code loads them (lddw immediates) and in the port's source, so the admin
48
+ instructions run too. Every transaction must give the same outcome, the same bytes in every account it touches and
49
+ the same log lines: the ray_log records, the original's messages ("initialize2: …", "withdrawpnl …", mismatch lines),
50
+ the entrypoint's error lines and, for panics, the message and the source location the runtime reports.
51
+
52
+ Covered: pool creation in both forms (open now and later), swaps in both directions, base in and base out, in all three
53
+ account forms; refusals (slippage, more than held, zero, the whole pool out, wrong account counts, short data, a vault
54
+ as the user's account, a wrong token program); deposits on either side with and without the minimum, withdrawals with
55
+ and without minimums, amounts past u64; status, state, fees and open time changes and their refusals; the P&L taken by
56
+ the P&L owner after the pool grew; config creation and updates; the retired tags; excess lamports; and a random mix of
57
+ swaps, deposits and withdrawals (`FUZZ_SEED`, `FUZZ_STEPS`). 484 transactions in all at 400 fuzz steps.
58
+
59
+ ## Differences
60
+
61
+ None found in outcomes, account bytes or log lines. Two things are outside what the test compares:
62
+
63
+ - **Compute-unit lines** ("consumed N of M compute units") differ, by design (see below).
64
+ - **Overflow panics the inputs cannot reach.** The original's `checked_*().unwrap()` sites that user input can reach
65
+ (a base-out swap for the whole side or more, an exchange rate past u64, a zero total) panic in the port with the
66
+ original's message and location. Arithmetic that cannot overflow for any input (u64 by u64 products in u128, for
67
+ instance) still panics there under `PANIC_ON_OVERFLOW`, but with an empty location.
68
+
69
+ ## Cost
70
+
71
+ | | original | TypeSOL | |
72
+ | --- | ---: | ---: | ---: |
73
+ | swap base in (v2) | 15,222 | 6,597 | 43% |
74
+ | swap base out (v2) | 15,894 | 6,800 | 43% |
75
+ | swap base in (17 accounts) | 16,317 | 6,875 | 42% |
76
+ | deposit | 44,406 | 16,544 | 37% |
77
+ | withdraw | 45,278 | 16,045 | 35% |
78
+ | initialize2 | 107,403 | 62,158 | 58% |
79
+
80
+ The binary is 147,536 bytes against the original's 308,616.
@@ -3,7 +3,7 @@
3
3
  This page ports a production concentrated-liquidity AMM in full: [Raydium CLMM](https://github.com/raydium-io/raydium-clmm)
4
4
  (`programs/amm` at 6d7a0f8, Anchor 1.0.2, Apache-2.0). It is deployed on mainnet at
5
5
  `CAMMCzo5YL8w4VFF8KVHrK22GGUsp5VTaW7grrKgrWqK`, and its on-chain IDL matches that commit. The port is
6
- [`examples/raydium-clmm/`](https://github.com/typesol/typesol/blob/main/examples/raydium-clmm). As with
6
+ [`showcase/mainnet/raydium-clmm/`](https://github.com/typesol-lang/typesol/blob/main/showcase/mainnet/raydium-clmm). As with
7
7
  [Raydium CP-Swap](case-study-raydium-cp-swap.md), it is checked against the binary that runs on mainnet, not a rebuild.
8
8
 
9
9
  ## What is ported
@@ -81,12 +81,16 @@ The swap needed nothing new:
81
81
  hop's pool, config, vaults and token accounts from the remaining accounts, and moves tokens with `invoke`.
82
82
  - **The router's account count.** It takes more accounts than TypeSOL's default 32, so the program declares
83
83
  `MAX_ACCOUNTS = 255n`. That keeps its CPI account lists on the heap, which adds about 3% to a swap.
84
+ - **Routes back through a pool.** A route may pass one pool's accounts again (there and back, or A → B → B → A).
85
+ The router loads each hop's pool, observation, tick arrays and bitmap extension with `loadAccountMut<Dup<T>>`
86
+ inside its loop. A view declared in a loop body ends with the iteration, so the next hop loads the pool again and
87
+ sees what the earlier hop did to it, as in the original. Two live views of one account are still refused.
84
88
 
85
89
  ## How it was checked
86
90
 
87
- [`test/examples/raydium-clmm.test.ts`](https://github.com/typesol/typesol/blob/main/test/examples/raydium-clmm.test.ts)
91
+ [`showcase/test/raydium-clmm.test.ts`](https://github.com/typesol-lang/typesol/blob/main/showcase/test/raydium-clmm.test.ts)
88
92
  loads the binary dumped from mainnet on 2026-10-30 (sha256 in
89
- [`test/programs/SOURCES.md`](https://github.com/typesol/typesol/blob/main/test/programs/SOURCES.md)) into one LiteSVM
93
+ [`test/programs/SOURCES.md`](https://github.com/typesol-lang/typesol/blob/main/test/programs/SOURCES.md)) into one LiteSVM
90
94
  and the port into another. Both use the same program id and the same clock, and both have Metaplex Token Metadata.
91
95
 
92
96
  **Setup.**
@@ -105,7 +109,7 @@ and the port into another. Both use the same program id and the same clock, and
105
109
  **Comparison.** For each transaction, the test requires the same outcome (success, or the same error down to the
106
110
  code), the same lamports, owner and data for every account involved, and the same events byte for byte.
107
111
 
108
- There are 300 transactions:
112
+ There are 302 transactions:
109
113
 
110
114
  - **Configs and pools.**
111
115
  - `create_pool` with SPL Token and Token-2022 transfer-fee mints, the mainnet config and a pre-funded vault.
@@ -131,7 +135,8 @@ There are 300 transactions:
131
135
  - Fees owed and reward streams paid out; arrays past the pool's own bitmap, on both sides.
132
136
  - Refusals: slippage, tick order and spacing, wrong arrays, another owner's NFT, closing with liquidity left.
133
137
  - **Swaps.**
134
- - `swap`, `swap_v2` and a two-hop `swap_router_base_in`; both directions, by input and by output, across ticks and
138
+ - `swap`, `swap_v2`, a two-hop `swap_router_base_in`, and routes back through a pool (there and back in one pool;
139
+ four hops through two pools twice); both directions, by input and by output, across ticks and
135
140
  tick arrays, to a price limit.
136
141
  - Fee modes, protocol and fund fees collected, a dynamic fee across its filter and decay periods.
137
142
  - Limit orders filled at a tick while a reward stream accrues.
@@ -148,40 +153,47 @@ There are 300 transactions:
148
153
  the admin and by its authority (an extension too short, then a valid one), paid to a position, and what is left
149
154
  returned after it ends.
150
155
 
156
+ The test also runs a random sequence: swaps (both ways, exact in and out, price limits, thresholds), positions at
157
+ random ranges, and liquidity added and taken out, with a new seed on every run (CI's included) and the same outcome,
158
+ bytes and events required after every step. `FUZZ_SEED=<seed>` reproduces a run's choices and amounts,
159
+ `CLMM_FUZZ_STEPS=<n>` lengthens it. Its transactions may use 1.4M compute units: one early run found a swap that
160
+ the original needs more than 200,000 for, which the default limit would stop before it is compared.
161
+
151
162
  ## Cost
152
163
 
153
164
  Compute units per transaction, mainnet original against the port, from one run of that test:
154
165
 
155
166
  | Instruction | Raydium (mainnet) | TypeSOL | |
156
167
  | --- | ---: | ---: | ---: |
157
- | `create_pool` | 55,818 | 24,441 | −56% |
158
- | `create_customizable_pool_v2` | 51,673 | 23,730 | −54% |
159
- | `open_position_with_token22_nft`, in range | 109,875 | 63,145 | −43% |
160
- | `open_position` (Metaplex NFT) | 121,451 | 80,854 | −33% |
161
- | `increase_liquidity_v2` | 39,759 | 15,148 | −62% |
162
- | `decrease_liquidity_v2` | 40,615 | 15,694 | −61% |
163
- | `decrease_liquidity_v2`, with a reward paid | 58,766 | 20,180 | −66% |
164
- | `close_position`, Metaplex NFT | 12,532 | 3,689 | −71% |
168
+ | `create_pool` | 57,318 | 24,806 | −57% |
169
+ | `create_customizable_pool_v2` | 51,673 | 23,724 | −54% |
170
+ | `open_position_with_token22_nft`, in range | 115,875 | 64,208 | −45% |
171
+ | `open_position` (Metaplex NFT) | 113,951 | 76,312 | −33% |
172
+ | `increase_liquidity_v2` | 39,759 | 15,935 | −60% |
173
+ | `decrease_liquidity_v2` | 40,615 | 16,538 | −59% |
174
+ | `decrease_liquidity_v2`, with a reward paid | 58,182 | 20,302 | −65% |
175
+ | `close_position`, Metaplex NFT | 11,032 | 3,366 | −69% |
165
176
  | `close_position`, Token-2022 NFT (its mint closed too) | 22,395 | 10,376 | −54% |
166
- | `swap_v2` | 49,040 | 23,209 | −53% |
167
- | `swap_v2`, across tick arrays | 55,837 | 29,048 | −48% |
168
- | `swap_v2`, dynamic fee | 58,924 | 31,228 | −47% |
169
- | `swap_v2`, through limit orders | 74,417 | 37,989 | −49% |
170
- | `swap_v2`, the mainnet USDC/CC pool | 52,677 | 27,571 | −48% |
171
- | `swap` | 51,009 | 21,592 | −58% |
172
- | `swap_router_base_in`, two hops | 80,365 | 42,150 | −48% |
173
- | `open_limit_order` | 35,720 | 10,781 | −70% |
174
- | `settle_limit_order` | 15,682 | 6,496 | −59% |
175
- | `initialize_reward` | 23,105 | 9,562 | −59% |
177
+ | `swap_v2` | 49,023 | 22,349 | −54% |
178
+ | `swap_v2`, across tick arrays | 55,837 | 27,517 | −51% |
179
+ | `swap_v2`, dynamic fee | 58,924 | 29,890 | −49% |
180
+ | `swap_v2`, through limit orders | 74,417 | 35,675 | −52% |
181
+ | `swap_v2`, the mainnet USDC/CC pool | 52,677 | 26,536 | −50% |
182
+ | `swap` | 51,009 | 20,902 | −59% |
183
+ | `swap_router_base_in`, two hops | 80,365 | 39,499 | −51% |
184
+ | `swap_router_base_in`, four hops through two pools twice | 159,246 | 88,746 | −44% |
185
+ | `open_limit_order` | 34,220 | 9,951 | −71% |
186
+ | `settle_limit_order` | 15,682 | 6,066 | −61% |
187
+ | `initialize_reward` | 24,605 | 9,640 | −61% |
176
188
  | `collect_protocol_fee` | 13,626 | 2,355 | −83% |
177
189
  | `update_amm_config` | 3,028 | 491 | −84% |
178
190
 
179
191
  Opening a position is mostly the token programs', the System Program's and (for a Metaplex NFT) Token Metadata's
180
192
  work: the NFT mint, its metadata, the associated token account and the transfers. That work costs the same in both.
181
193
  Pool and position creation also vary from run to run with the PDA bump searches of the random keys the test uses, in
182
- both programs alike. Refusals cost far less; a swap of nothing costs 16,665 against 1,651.
194
+ both programs alike. Refusals cost far less; a swap of nothing costs 16,665 against 1,650.
183
195
 
184
- On size, the port's binary is 566,280 bytes and the deployed original 1,700,160 bytes, for the same 37 instructions.
196
+ On size, the port's binary is 552,656 bytes and the deployed original 1,700,160 bytes, for the same 37 instructions.
185
197
 
186
198
  ## Where they differ
187
199
 
@@ -189,20 +201,19 @@ On size, the port's binary is 566,280 bytes and the deployed original 1,700,160
189
201
  `ProgramFailedToComplete`; the port fails with a numbered error. Examples: a position past the pool's own bitmap
190
202
  opened without the extension account (the original reads `remaining_accounts[0]` of none), more than 10 operators
191
203
  (`copy_from_slice` of the wrong length). The test checks only that both fail there.
192
- - **Check order on double faults.** Anchor creates `init` accounts (a position, an order) before it checks the accounts
193
- after them. The port follows that order where a test showed it mattered (`create_support_mint_associated`);
194
- elsewhere it checks every account first and creates them at the start of the body. A transaction breaking one check
195
- gets the same code. One that breaks two at once can be reported differently.
204
+ - **Check order on double faults.** Anchor creates `init` accounts (a position, an order) before it checks the other
205
+ accounts' constraints. TypeSOL does the same with the creates a body starts with (since the Marginfi port, whose
206
+ fuzzer found a case), so a transaction breaking both gets Anchor's code there. A body that checks something before
207
+ its creates keeps that order, and a double fault there can still be reported differently.
196
208
  - **Reward mints' order.** The original keeps the operation account's reward mints in a hash set's order; the port
197
209
  keeps them in the order added. With two or more listed, the `OperationState` bytes differ. Only membership is ever
198
210
  read, and operators are sorted the same in both.
199
211
  - **Duplicate tick arrays in a swap.** The original loads every tick array it is given before it starts, so one passed
200
212
  twice fails at once. The port loads an array when the swap reaches it. Passed twice but never reached, it does
201
213
  not fail.
202
- - **The same pool twice in a route.** `swap_router_base_in` through one pool twice works in the original. The port
203
- loads each hop's pool mutably and refuses a second load of one account (2040).
204
- - **Two limit-order fills that empty extension arrays in one swap.** Each needs the extension writable. The port loads
205
- it for each such flip, and a second load in one instruction is refused (2040).
214
+ - **Two limit-order fills that empty extension arrays in one swap.** Each needs the extension writable. The port now
215
+ loads it within the tick array's loop iteration, so a second flip in a later array is allowed. No test sets this
216
+ case up against the original yet.
206
217
  - **IDL.**
207
218
  - For the Metaplex NFTs' associated token account, the original's IDL lists the SPL Token program as a constant
208
219
  seed. The port's lists the `token_program` account (fixed to that address) in `open_position`, and nothing in
@@ -2,7 +2,7 @@
2
2
 
3
3
  This page ports the trading core of a production AMM: [Raydium CP-Swap](https://github.com/raydium-io/raydium-cp-swap)
4
4
  (`programs/cp-swap`, Anchor 1.0.2, Apache-2.0). It is deployed on mainnet at
5
- `CPMMoo8L3F4NbTegBCKVNunggL7H1ZpdTHKxQB5qKP1C`. The port is [`examples/raydium-cp-swap/`](https://github.com/typesol/typesol/blob/main/examples/raydium-cp-swap)
5
+ `CPMMoo8L3F4NbTegBCKVNunggL7H1ZpdTHKxQB5qKP1C`. The port is [`showcase/mainnet/raydium-cp-swap/`](https://github.com/typesol-lang/typesol/blob/main/showcase/mainnet/raydium-cp-swap)
6
6
  (`state.ts`, `curve.ts`, `raydium_cp_swap.ts`). As with [coral-xyz/multisig](case-study-multisig.md), it is checked
7
7
  against the binary that runs on mainnet, not a rebuild.
8
8
 
@@ -65,8 +65,8 @@ The rest uses features TypeSOL already had:
65
65
 
66
66
  ## How it was checked
67
67
 
68
- [`test/examples/raydium-cp-swap.test.ts`](https://github.com/typesol/typesol/blob/main/test/examples/raydium-cp-swap.test.ts) loads the binary dumped from
69
- mainnet on 2026-10-30 (slot 452376460; sha256 in [`test/programs/SOURCES.md`](https://github.com/typesol/typesol/blob/main/test/programs/SOURCES.md)) into one
68
+ [`showcase/test/raydium-cp-swap.test.ts`](https://github.com/typesol-lang/typesol/blob/main/showcase/test/raydium-cp-swap.test.ts) loads the binary dumped from
69
+ mainnet on 2026-10-30 (slot 452376460; sha256 in [`test/programs/SOURCES.md`](https://github.com/typesol-lang/typesol/blob/main/test/programs/SOURCES.md)) into one
70
70
  LiteSVM and the port into another. Both use the same program id and the same clock.
71
71
 
72
72
  **Setup.**
package/docs/cli.md CHANGED
@@ -6,18 +6,18 @@ error. Exit codes: **0** success, **1** failure (compile errors, failed deploy,
6
6
 
7
7
  | Command | What it does |
8
8
  | --- | --- |
9
- | `typesol init [<dir>] [--name <program>] [--template counter\|vault\|escrow]` | Create a project: `typesol.json`, `programs/<name>.ts`, `tests/<name>.test.ts` (LiteSVM), `package.json`, `tsconfig.json`, `programs/tsconfig.json` (with the editor plugin) and `.gitignore`. `<name>` must be snake_case; by default it is derived from `<dir>` (`my-app` → `my_app`). The template picks the program and its test: `counter` (default, a PDA counter per user), `vault` (SOL at a PDA: deposit, withdraw above rent, close) or `escrow` (SPL tokens: make, take, refund, the offer PDA signing for its vault). Refuses to overwrite files. It warns, with the install commands, when the platform-tools that building needs are missing. |
9
+ | `typesol init [<dir>] [--name <program>] [--template counter\|vault\|escrow]` | Create a project: `typesol.json`, `programs/<name>.ts`, `tests/<name>.test.ts` (LiteSVM), `package.json`, `tsconfig.json`, `programs/tsconfig.json` (with the editor plugin), `.vscode/settings.json` (the workspace TypeScript, so the plugin loads) and `.gitignore`. `<name>` must be snake_case; by default it is derived from `<dir>` (`my-app` → `my_app`). The template picks the program and its test: `counter` (default, a PDA counter per user), `vault` (SOL at a PDA: deposit, withdraw above rent, close) or `escrow` (SPL tokens: make, take, refund, the offer PDA signing for its vault). Refuses to overwrite files. It warns, with the install commands, when the platform-tools that building needs are missing. |
10
10
  | `typesol new <program> [--template counter\|vault\|escrow]` | In a project: add a program, `programs/<program>.ts` and `tests/<program>.test.ts` from the template, and its entry in `typesol.json`. Refuses to overwrite files or reuse a name. |
11
11
  | `typesol doctor [--url <rpc>] [--config <cli config>]` | Check what building, testing and deploying need: Node.js, the platform-tools, the Solana CLI, its keypair, the RPC (`--url`, a cluster moniker, or the CLI config) and the keypair's balance there, and the project. Each problem is printed with the command that fixes it. Exit code 1 when building is impossible (Node.js or the platform-tools missing); deploy pieces are warnings. |
12
12
  | `typesol build [<program>…] [--sbpf v0\|v1\|v2] [--profile] [--quiet]` | In a project: build every program (or those named) into `<out>/<name>/`: `<name>.so`, `<name>.json` (Anchor IDL), `<name>.client.ts` (TypeScript client, see [client.md](client.md)), `<name>.c`, `<name>.build.json`. The first build creates `<out>/deploy/<name>-keypair.json` and records the address in `typesol.json`; later builds refuse a different keypair. Without the keypair (a fresh clone, CI) it builds at the recorded address and says so; only `deploy` needs the keypair. |
13
- | `typesol build <file.ts> [--out <dir>] [--program-id <keypair.json>] [--sbpf …] [--profile]` | Outside a project (or a file that is not one of its programs): build that file. A project program's `.ts` builds as that program. |
14
- | `typesol check [<program>… \| <file.ts>] [--json] [--stdin]` | Type-check and validate without building. In a project, no argument checks every program (or those named) and exits 1 if any has errors. `--json` prints `{ ok, diagnostics: [{ file, line, column, message }] }`; `--stdin` checks the text given on stdin as that file (its imports come from disk). |
13
+ | `typesol build <file.ts> [--out <dir>] [--program-id <keypair.json>] [--sbpf …] [--profile] [--quiet]` | Outside a project (or a file that is not one of its programs): build that file. A project program's `.ts` builds as that program. |
14
+ | `typesol check [<program>… \| <file.ts>] [--json] [--stdin \| --files]` | Type-check and validate without building. In a project, no argument checks every program (or those named) and exits 1 if any has errors. `--json` prints `{ ok, diagnostics: [{ file, line, column, message }] }`; `--stdin` checks the text given on stdin as that file (its imports come from disk); `--files` reads `{ "files": { "<absolute path>": "<text>", … } }` from stdin and checks with those texts instead of the disk (an editor's unsaved buffers). |
15
15
  | `typesol clone [<address>…] [--url <rpc>] [--config <cli config>] [--out <dir>]` | Copy cluster accounts and programs for the tests (Anchor's `[test.validator.clone]`): each account to `<address>.json` (lamports, owner, data), each program to `<address>.so` (its ELF, from the ProgramData account for upgradeable programs), in `tests/accounts` (`"accounts"` in `typesol.json`) or `--out`. Without addresses it clones the project's `"clone"` list. `testProgram` loads that directory into its LiteSVM; commit it so the tests run offline. |
16
16
  | `typesol emit-c <file.ts> [--out <file.c>] [--profile]` | Print or write the generated C. |
17
17
  | `typesol deploy [<program>…] [--url <rpc>] [--keypair <payer.json>] [--config <cli config>] [--dry-run] [--sbpf …]` | Build, then `solana program deploy --use-rpc` at the program keypair's address. Deploying again upgrades (the payer must be the upgrade authority). `--dry-run` builds and prints the commands. Before sending anything it checks that the RPC answers and that the payer holds enough for the rent (about twice the binary's on a first deploy, while the buffer exists), and says what to do if not (start `solana-test-validator`, `solana airdrop` on devnet or a local validator). Needs `solana` on PATH. |
18
18
  | `typesol idl publish [<program>…] [--url <rpc>] [--keypair <authority.json>] [--config <cli config>] [--non-canonical]` | Compile (the binary is not rebuilt), then write each IDL to the Program Metadata program (seed `idl`). Canonical IDLs must be signed by the upgrade authority. Defaults come from the Solana CLI config (`~/.config/solana/cli/config.yml`). |
19
19
  | `typesol cpi <idl.json> [--out <file.ts>] [--prefix <name>] [--address <base58>]` | Generate TypeSOL helpers that call another program from its IDL. Unsupported instructions are listed on stderr. |
20
- | `typesol port <idl.json> [--out <file.ts>]` | A TypeSOL program skeleton from an Anchor IDL (0.30+/1.x), to port an Anchor program: account structs, enums, errors, events and one function per instruction with its accounts and arguments, each body `require(false, …NotPorted)` until written. Instruction discriminators (an explicit `@discriminator` when the name derives another), account names and error codes are kept, so existing clients keep working where the layouts match; notes on stderr list where they do not (Borsh `String`/`Option` in account data, a `Vec` that is not the last field, custom discriminators or error codes). |
20
+ | `typesol port <idl.json> [--out <file.ts>]` | A TypeSOL program skeleton from an Anchor IDL (0.30+/1.x), to port an Anchor program: account structs, enums, errors, events and one function per instruction with its accounts and arguments, each body `require(false, …NotPorted)` until written. Instruction discriminators (an explicit `@discriminator` when the name derives another), account names and error codes are kept, so existing clients keep working where the layouts match; notes on stderr list where they do not (Borsh `String`/`Option` in account data, a `Vec` that is not the last field). Error codes that skip ahead are kept with `@code`, and zero-copy types keep their IDL `repr`, `serialization` and field names (`@repr`, `@serialization`, `@idlName`). |
21
21
  | `typesol client <idl.json> [--out <file.ts>] [--address <base58>]`, `typesol client --address <program id> [--url <rpc>] [--config <cli config>] [--out <file.ts>]` | Generate a typed TypeScript client on `@solana/kit` from an Anchor IDL (0.30+/1.x or TypeSOL); the IDL is validated first, and what the client leaves out is printed as notes. Without a file, the IDL is read from the chain: the canonical Program Metadata IDL, else Anchor's IDL account. See [client.md](client.md). |
22
22
  | `typesol verify <program\|file.ts> [<deployed.so>] [--address <program id>] [--url <rpc>] [--config <cli config>] [--sbpf …]` | Rebuild and compare with what is deployed. Without a `.so`, reads the program from the cluster (`--url`, cluster monikers such as `mainnet-beta` included, else the Solana CLI config) at `--address` or the address `typesol.json` records, under any loader (v2, upgradeable v3, v4); prints the upgrade authority (or that the program is immutable) and the last deploy slot, and compares the IDL published with `typesol idl publish` (a published IDL that differs is a mismatch). With a `.so` (e.g. `solana program dump`), compares with that file. Trailing zero padding is ignored; the SBPF version is read from the deployed ELF unless `--sbpf` is given. |
23
23
  | `typesol profile <logs.txt\|-> [--program <id>]` | Compute units per TypeScript line from the logs of a `--profile` build's transaction (exit 1 when the logs have no markers). |
@@ -36,7 +36,7 @@ error. Exit codes: **0** success, **1** failure (compile errors, failed deploy,
36
36
 
37
37
  `"clone": ["<address>", …]` lists the cluster accounts `typesol clone` copies for the tests, and `"accounts"` where
38
38
  they are kept (default `tests/accounts`). `"programs": { "vault": "programs/vault.ts" }` (a plain path) is accepted too. Keep `target/deploy/*-keypair.json`
39
- safe and out of git: it is each program's address and its first upgrade authority must not lose it. A clone without
39
+ safe and out of git: it is the program's address, and it is needed to deploy it again. A clone without
40
40
  it still builds and tests (`npm test` builds first) at the recorded address; deploying needs it.
41
41
 
42
42
  ## Editor
package/docs/client.md CHANGED
@@ -61,7 +61,7 @@ Kit's own transaction functions work with the instructions as well; `sendInstruc
61
61
 
62
62
  | | |
63
63
  | --- | --- |
64
- | `<program>.<instruction>(accounts, args)` | Builds the `Instruction`. Accounts with a fixed address and PDAs whose seeds are constants, arguments or other accounts' keys are optional and filled in; pass one to override. An optional account passed as `null` is left out (sent as the program id, the convention TypeSOL and Anchor share); left undefined, it is filled in like any other. |
64
+ | `<program>.<instruction>(accounts, args)` | Builds the `Instruction`. Accounts with a fixed address and PDAs whose seeds are constants, arguments (an Option one with its default too: Anchor's `id.unwrap_or(0)`) or other accounts' keys are optional and filled in; pass one to override. An optional account passed as `null` is left out (sent as the program id, the convention TypeSOL and Anchor share); left undefined, it is filled in like any other. |
65
65
  | `<program>.<instruction>(accounts, args, remaining?)` | The same, with remaining accounts appended in order (kit `AccountMeta`s). |
66
66
  | `<program>.accounts.<Name>` | `decode(bytes)`, `encode(value)`, `fetch(rpc, address)`, `fetchMaybe(rpc, address)`, `fetchMultiple(rpc, addresses)`, `fetchAll(rpc, filters?)` (getProgramAccounts by discriminator, plus `{ offset, bytes }` filters), `watch(rpcSubscriptions, address, { abortSignal })`, `discriminator`. `decode` checks the discriminator. |
67
67
  | `<program>.pdas.<account>(seeds)` | The PDA (address and bump) for each account an instruction derives, also one seeded from its own fields (from the field values). |
package/docs/errors.md CHANGED
@@ -7,8 +7,8 @@ Framework errors use Anchor's numbers where Anchor has the same check, so client
7
7
  | 100 / 101 / 102 | instruction data missing / unknown instruction / malformed arguments (InstructionMissing / FallbackNotFound / DidNotDeserialize) |
8
8
  | 2000 | a `Mut<…>` / `Init<…>` account passed read-only (ConstraintMut) |
9
9
  | 2001 | HasOne constraint failed (ConstraintHasOne) |
10
- | 2003 | a `constraint(acct, condition)` without its own error failed (ConstraintRaw) |
11
10
  | 2002 | a `Signed<…>` account (or the event authority) did not sign (ConstraintSigner) |
11
+ | 2003 | a `constraint(acct, condition)` without its own error failed (ConstraintRaw) |
12
12
  | 2004 | Owner constraint failed (ConstraintOwner) |
13
13
  | 2005 | Zeroed account is not rent-exempt (ConstraintRentExempt) |
14
14
  | 2006 | PDA seeds mismatch (ConstraintSeeds) |
@@ -23,6 +23,7 @@ Framework errors use Anchor's numbers where Anchor has the same check, so client
23
23
  | 2040 | the same account passed in two writable slots (ConstraintDuplicateMutableAccount) |
24
24
  | 0 (from the System Program) | an `Init` / `create` on an address already in use (AccountAlreadyInUse): the System Program refuses it, as under Anchor's `init` |
25
25
  | 3000 | `initAccount<T>` on an account whose discriminator is already set (AccountDiscriminatorAlreadySet) |
26
+ | 3001 | a zero-copy (`@serialization bytemuck`) account with fewer than 8 bytes of data (AccountDiscriminatorNotFound) |
26
27
  | 3002 | discriminator mismatch (AccountDiscriminatorMismatch) |
27
28
  | 3003 | data too small, not an initialized token account / mint, a read past the end, or a `@layout borsh` account whose bytes are not a valid Borsh value (AccountDidNotDeserialize) |
28
29
  | 3004 | a `@layout borsh` account too small for the value it would now hold (AccountDidNotSerialize) |
@@ -32,7 +33,7 @@ Framework errors use Anchor's numbers where Anchor has the same check, so client
32
33
  | 3008 / 3009 | wrong program address / not executable (InvalidProgramId / InvalidProgramExecutable) |
33
34
  | 3010 | account must sign (AccountNotSigner; also a new keypair account a `create(payer, null, …)` makes) |
34
35
  | 3011 | a `SystemAccount` owned by another program (AccountNotSystemOwned) |
35
- | 3012 | account not initialized: empty system account where a program account was expected (AccountNotInitialized) |
36
+ | 3012 | account not initialized: empty system account where a program account, a token account or a mint was expected (AccountNotInitialized; not for zero-copy accounts, which Anchor's AccountLoader reports as 3007) |
36
37
  | 3016 | account would grow more than 10 KiB in one instruction, or grow without a payer (AccountReallocExceedsLimit) |
37
38
  | 4101 | an `Init` / `create` whose payer is the account being created (TryingToInitPayerAsProgramAccount) |
38
39
  | 4102 | cast overflow (InvalidNumericConversion) |
@@ -49,21 +50,28 @@ Framework errors use Anchor's numbers where Anchor has the same check, so client
49
50
  | 5113 | more accounts in one instruction (or one CPI) than the program takes: 32, or its `MAX_ACCOUNTS` |
50
51
  | 5114 | a function with a result ended without returning one |
51
52
  | 5115 | an optional account that was left out was used |
52
- | 6000+ | your error enums' members, in declaration order (imported files first), then one `RequireFailed<N>` error (code 6000 + N) per distinct `require(cond, "message")` string |
53
+ | 6000+ | your error enums' members, in declaration order (imported files first), then one `RequireFailed<N>` error (code 6000 + N, after the highest code) per distinct `require(cond, "message")` string |
53
54
 
54
55
  A member's string is its message (the IDL's `msg`); `X = ""` leaves the message out, as an Anchor variant without
55
- `#[msg]`.
56
+ `#[msg]`. `/** @code 6200 */` before a member gives it that code, and the members after it continue from there (Anchor's
57
+ `Variant = 200`); codes only go up. `require(cond, "message")` errors come after the highest code.
56
58
 
57
59
  Any of Anchor's own codes can be raised directly: `require(cond, AnchorError.RequireKeysEqViolated)` fails with 2502,
58
60
  as `require_keys_eq!` does.
59
61
 
62
+ A code can also be computed at run time: `require(cond, code)` with a `u32` value fails with that code. This is how a
63
+ helper passes an error on as Rust's `?` does, when it returns the error as a value instead of failing (a price read
64
+ whose error the caller may record or ignore): `const r = price(feed); require(r.err === 0n, r.err);`. The code is
65
+ reported as it is (use one of your errors' codes, so clients can decode it); a zero code fails with 5100.
66
+
60
67
  ## Which error a transaction gets
61
68
 
62
69
  A transaction can break several checks; it fails with the first. Within each account's constraints the checks run in
63
70
  the order Anchor's generated code runs them, so a transaction gets the same code under both:
64
71
 
65
72
  1. The account count (3005), then each account in parameter order: its type, signer (3010), `Mut` (2000), owner,
66
- discriminator and size of a program account (3007 / 3012 / 3002 / 3003), token account or mint, program address.
73
+ discriminator and size of a program account (3007 / 3012 / 3002 / 3003; a zero-copy one as Anchor's AccountLoader:
74
+ 3007 / 3001 / 3002, then 3003), token account or mint, program address.
67
75
  2. Two writable parameters naming one account (2040).
68
76
  3. The instruction's arguments (102).
69
77
  4. Each account in parameter order, its constraints in Anchor's order: seeds (2006), associated token account (owner