@typesol/cli 0.40.0 → 0.41.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.
@@ -1,6 +1,6 @@
1
1
  # Case study: Raydium CP-Swap
2
2
 
3
- This page ports the trading core of a production AMM: [Raydium CP-Swap](https://github.com/raydium-io/raydium-cp-swap)
3
+ This page ports a production AMM, every instruction of it: [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
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
@@ -8,32 +8,24 @@ against the binary that runs on mainnet, not a rebuild.
8
8
 
9
9
  ## What is ported
10
10
 
11
- The port covers 10 of the original's 24 instructions:
11
+ The port covers all 21 of the original's instructions:
12
12
 
13
- - `initialize`, which creates a pool with its first liquidity;
13
+ - pool creation: `initialize`, `initialize_v2` (the same without the rent sysvar, choosing the creator fee) and
14
+ `initialize_with_permission` (for a payer Raydium authorized, on behalf of another creator, with the creator fee on);
14
15
  - the four trading instructions: `swap_base_input`, `swap_base_output`, `deposit` and `withdraw`;
15
- - `collect_protocol_fee` and `collect_fund_fee`;
16
- - the admin instructions `create_amm_config`, `update_amm_config` and `update_pool_status`.
16
+ - the fees: `collect_protocol_fee`, `collect_fund_fee`, `collect_creator_fee` and
17
+ `collect_creator_fee_permissionless`;
18
+ - the admin instructions: `create_amm_config`, `update_amm_config`, `update_pool_status`, `create_permission_pda`,
19
+ `close_permission_pda`, `create_creator_fee_share`, `close_creator_fee_share`, `create_support_mint_associated`,
20
+ `close_support_mint_associated` and `collect_excess_lamports`.
17
21
 
18
22
  The port keeps the original's account layouts:
19
23
 
20
- - `AmmConfig` is a Borsh account of fixed size.
24
+ - `AmmConfig`, `Permission`, `CreatorFeeShare` and `SupportMintAssociated` are Borsh accounts of fixed size.
21
25
  - `PoolState` and `ObservationState` are zero-copy `#[repr(C, packed)]` structs, which is TypeSOL's fixed layout.
22
26
 
23
- It also keeps the original's names, argument order and error codes, and emits the same events (`SwapEvent`,
24
- `LpChangeEvent`). Existing pools, clients and indexers work with it unchanged.
25
-
26
- These are not ported yet:
27
-
28
- - **`initialize_with_permission`.** It is `initialize` for a creator the admin authorized, with the creator fee on.
29
- - **The creator-fee instructions**: `collect_creator_fee`, `collect_creator_fee_permissionless`,
30
- `create_creator_fee_share` and `close_creator_fee_share`.
31
- - **The permission instructions**: `create_permission_pda` and `close_permission_pda`.
32
- - **The support-mint instructions**: `create_support_mint_associated` and `close_support_mint_associated`.
33
- - **`collect_excess_lamports`.**
34
-
35
- The pool logic these depend on is in the port (fee booking, the creator-fee split in the swap math, tested below with
36
- the creator fee on), so a pool the original created and configured is traded the same way.
27
+ It also keeps the original's names, account order, argument order and error codes, and emits the same events
28
+ (`SwapEvent`, `LpChangeEvent`). Existing pools, clients and indexers work with it unchanged.
37
29
 
38
30
  ## What it needed
39
31
 
@@ -56,6 +48,10 @@ Four Token-2022 reads were added to the compiler for this port (`test/integratio
56
48
  - `transferFeeBasisPoints(mint)` and `transferFeeMaximum(mint)`: the fee in force this epoch.
57
49
  - `extensionsWithin(account, [types])`: the original's list of mint extensions a pool accepts.
58
50
 
51
+ The instructions added later needed none: `Ata<InitIfNeeded<…>>` for the creator's token accounts, `CloseTo` for the
52
+ closing instructions, `readData` to read a token account as `StateWithExtensions::unpack` does, and `invoke` for the
53
+ token instructions Anchor has no wrapper for (`WithdrawExcessLamports`, `SyncNative`, `UnwrapLamports`).
54
+
59
55
  The rest uses features TypeSOL already had:
60
56
 
61
57
  - `constraint(…)`, for Anchor's `address = pool_state.load()?.amm_config` and `constraint = …` in Anchor's order;
@@ -66,21 +62,24 @@ The rest uses features TypeSOL already had:
66
62
  ## How it was checked
67
63
 
68
64
  [`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
65
+ mainnet on 2026-11-07 (deployed at slot 463522946 on 2026-10-31; sha256 in [`test/programs/SOURCES.md`](https://github.com/typesol-lang/typesol/blob/main/test/programs/SOURCES.md)) into one
70
66
  LiteSVM and the port into another. Both use the same program id and the same clock.
71
67
 
72
68
  **Setup.**
73
69
 
74
70
  - Mints and token accounts are made by the same SPL Token / Token-2022 instructions in both.
75
- - The program's own accounts (an `AmmConfig`, a pool's `PoolState` and its `ObservationState`) are written in both as
76
- the original lays them out, because creating a config needs Raydium's admin key.
71
+ - Raydium's five signing keys (the admin, and the owners of permissions, fee shares, supported mints and excess
72
+ lamports) are replaced by test keys in both: in the original, at each place its code loads one (four 64-bit
73
+ immediates); in the port, in its source. The admin instructions then run in both.
74
+ - Pools for trading (an `AmmConfig`, a pool's `PoolState` and its `ObservationState`) are written in both as the
75
+ original lays them out, so each test starts from the state it needs.
77
76
 
78
77
  **Comparison.** For each transaction, the test requires the same outcome (success, or the same error down to the
79
78
  code), the same lamports, owner and data for every account involved, and the same events byte for byte.
80
79
 
81
- There are 70 transactions:
80
+ There are 136 transactions:
82
81
 
83
- - **Pool creation.** These cases are covered:
82
+ - **Pool creation.** With `initialize`, these cases are covered:
84
83
  - a pool at its PDA, at a signing keypair, and at a pre-funded address;
85
84
  - a pool with the creation fee, paid in wrapped SOL;
86
85
  - a pool of Token-2022 fee mints;
@@ -90,12 +89,26 @@ There are 70 transactions:
90
89
  - an unsupported extension (6007);
91
90
  - a disabled config (6000);
92
91
  - too little liquidity (6009).
92
+
93
+ `initialize_v2` with the creator fee off and on in each of its three modes, and its refusals.
94
+ `initialize_with_permission` without a permission (3007), with another payer's (2006), and with one.
93
95
  - **Swaps.** Both directions, by input and by output; the observation ring advancing as the clock moves; slippage
94
96
  (6005); zero amounts.
95
97
  - **Liquidity.** Deposit, a partial withdrawal, slippage both ways, withdrawing more LP than held, zero.
96
98
  - **Fees.** Protocol and fund fees collected by their owners after trading, and refused for anyone else (6001).
97
99
  - **Creator fees.** Pools with the creator fee on at 0.05%, taken from the input token, only token_0, or only
98
- token_1: swaps both ways, by input and by output, booking the fee on the right side.
100
+ token_1: swaps both ways, by input and by output, booking the fee on the right side. Collected by the creator (its
101
+ token accounts created on the way) at the config's share, again with nothing owed (6014), with the creator's own
102
+ share, by anyone with the permissionless form, at the config's share again after the share is closed, for SPL Token
103
+ and Token-2022 fee mints; a share above 100% (6003); anyone but the creator (2012).
104
+ - **Permissions, fee shares, supported mints.** Each created and closed by Raydium's keys and refused for anyone else
105
+ (6001, 6000); a fee share above 100% (6003); a permission created twice; a supported-mint entry for an SPL Token
106
+ mint (6000). A Token-2022 mint with a permanent delegate is refused for a pool (6007) until it is listed and the
107
+ entry passed, and again without the entry.
108
+ - **Excess lamports.** Swept from the pool and its observation account (rent kept), from an SPL Token and a Token-2022
109
+ vault and the LP mint (`WithdrawExcessLamports`), from wrapped SOL held by the pool authority (`SyncNative`, then
110
+ `UnwrapLamports` of exactly the difference); a trader's account (the token program's OwnerMismatch, 4); anyone but
111
+ the two keys (6001).
99
112
  - **The price oracle.** A new point, a second swap within 15 seconds (added to the current point), and a ring at its
100
113
  last slot whose cumulative price is 10 below `u128::MAX`: the next point wraps to slot 0 and the sum wraps around.
101
114
  - **Token-2022.** A pool of two transfer-fee mints (1.5% and 0.37%): swaps both ways and by output, deposit and
@@ -107,28 +120,45 @@ There are 70 transactions:
107
120
  - Swaps or deposits on a pool whose status disables them (6000).
108
121
  - The admin instructions sent by anyone but the admin (2012, 6001).
109
122
 
123
+ **Fuzzing.** A random sequence (`FUZZ_SEED` reproduces one, `CP_FUZZ_STEPS` lengthens it; 120 steps by default) runs
124
+ in two pools, one of SPL Token mints and one of Token-2022 mints with random transfer fees, each with the creator fee
125
+ on in a random mode at a random rate and protocol share: swaps both ways by input and by output with random amounts
126
+ and slippage limits, deposits, withdrawals, creator fee collections (by the creator and permissionless), protocol and
127
+ fund fee collections, and the clock moving on. Every step must give the same outcome, account bytes and events as the
128
+ original. Runs of 600 steps on several seeds found one difference, now fixed: an exact-output swap for the whole
129
+ vault or more panics in the original (`checked_sub(..).unwrap()`, `checked_ceil_div(..).unwrap()`), where the port
130
+ failed with 5100; it panics now at the original's location. These fuzz transactions are not in the 136 above.
131
+
110
132
  ## Cost
111
133
 
112
134
  Compute units per transaction, mainnet original against the port, from that test:
113
135
 
114
136
  | Instruction | Raydium (mainnet) | TypeSOL | |
115
137
  | --- | ---: | ---: | ---: |
116
- | `initialize` | 83,474 | 39,352 | −53% |
117
- | `initialize`, Token-2022 transfer fees | 101,962 | 55,795 | −45% |
118
- | `swap_base_input` | 24,142 | 5,734 | −76% |
119
- | `swap_base_output` | 24,571 | 5,941 | −76% |
120
- | `deposit` | 25,004 | 6,190 | −75% |
121
- | `withdraw` | 25,211 | 6,201 | −75% |
138
+ | `initialize` | 83,559 | 39,878 | −52% |
139
+ | `initialize`, Token-2022 transfer fees | 93,047 | 53,834 | −42% |
140
+ | `initialize_v2`, creator fee on | 86,286 | 41,760 | −52% |
141
+ | `initialize_with_permission` | 98,221 | 43,358 | −56% |
142
+ | `swap_base_input` | 24,162 | 5,734 | −76% |
143
+ | `swap_base_output` | 24,591 | 5,989 | −76% |
144
+ | `deposit` | 25,007 | 6,194 | −75% |
145
+ | `withdraw` | 25,214 | 6,203 | −75% |
122
146
  | `collect_protocol_fee` | 20,205 | 4,189 | −79% |
123
- | `swap_base_input`, Token-2022 transfer fees | 31,365 | 12,083 | −61% |
124
- | `swap_base_output`, Token-2022 transfer fees | 32,542 | 14,101 | −57% |
125
- | `deposit`, Token-2022 transfer fees | 33,237 | 14,482 | −56% |
147
+ | `collect_creator_fee`, creating the creator's two token accounts | 61,644 | 34,014 | −45% |
148
+ | `collect_creator_fee_permissionless` | 28,165 | 5,887 | −79% |
149
+ | `create_permission_pda` | 10,525 | 2,291 | −78% |
150
+ | `create_creator_fee_share` | 8,421 | 2,067 | −75% |
151
+ | `create_support_mint_associated` | 7,837 | 2,012 | −74% |
152
+ | `close_creator_fee_share` | 5,169 | 767 | −85% |
153
+ | `collect_excess_lamports`, wrapped SOL | 10,798 | 3,764 | −65% |
154
+ | `swap_base_input`, Token-2022 transfer fees | 31,339 | 12,082 | −61% |
155
+ | `swap_base_output`, Token-2022 transfer fees | 32,508 | 14,097 | −57% |
156
+ | `deposit`, Token-2022 transfer fees | 33,203 | 14,478 | −56% |
126
157
 
127
158
  The Token-2022 rows include the token program's own work, which is the same in both. Refusals cost far less. For
128
- example, a swap through the wrong config costs 17,190 against 1,656.
159
+ example, a swap through the wrong config costs 17,193 against 1,656.
129
160
 
130
- On size, the port's binary is 143,560 bytes and the deployed original is 793,824 bytes. The original also contains the
131
- 14 instructions not ported here, so the size ratio overstates the gain for the same feature set.
161
+ On size, the port's binary is 214,944 bytes and the deployed original is 749,848 bytes, for the same 21 instructions.
132
162
 
133
163
  Pool creation is mostly the token programs' and the System Program's own work (account creation, initialization,
134
164
  transfers), which costs the same in both. The difference is the program's part. Its cost also varies from run to run
@@ -141,12 +171,18 @@ when the values fit, and an instruction reads the Clock once. Together they took
141
171
 
142
172
  - **Panics.** Where the original calls `unwrap()` or `assert!`, it aborts with `ProgramFailedToComplete`. The port
143
173
  fails with a numbered error instead:
144
- - an overflow or underflow, 5100: an exact-output swap for more than the vault holds;
145
- - a division by zero, 5101: the price of an empty side, or an exact-output swap for exactly what the vault holds;
174
+ - a division by zero, 5101: the price of an empty side;
146
175
  - invalid input, 6003: a fee rate out of range in the admin instructions, which only the admin can reach.
176
+ - an overflow adding to the destination's lamports in `collect_excess_lamports`, 5100 where the original returns
177
+ `ArithmeticOverflow`: no account holds that many lamports.
147
178
 
148
- The swaps and liquidity instructions reach none of these with the amounts a pool can hold.
179
+ The swaps and liquidity instructions reach none of these with the amounts a pool can hold. An exact-output swap for
180
+ the whole vault or more, which a trader can send, panics as the original does (fuzzing found it).
149
181
  - **Check order on double faults.** Account checks run in Anchor's order, so a transaction breaking one check gets the
150
182
  same code. The port keeps the pool's `address = …` and `token::…` constraints as `constraint(…)` calls in Anchor's
151
183
  order. A transaction that breaks a token check and an account-type check at once can still be reported differently.
184
+ - **Token accounts in `collect_excess_lamports`.** The original reads a remaining account as a token account with
185
+ `StateWithExtensions::unpack`, which also walks the Token-2022 extension list. The port checks the size, the
186
+ account type byte, the state and the option tags, not the extension list: a Token-2022 account the token program
187
+ wrote always has a valid one.
152
188
  - **Logs.** Error messages are TypeSOL's. Codes and events are the original's.
@@ -0,0 +1,56 @@
1
+ # Stability and the road to 1.0
2
+
3
+ TypeSOL is pre-1.0. Until 1.0, a minor release (0.x → 0.y) may change the language, the account and IDL layout of
4
+ generated programs and error codes, and says so in the CHANGELOG ([versioning
5
+ policy](https://github.com/typesol-lang/.github/blob/main/VERSIONING.md)). This page says what 1.0 will promise, which
6
+ parts are already meant to stay as they are, and what has to be settled first.
7
+
8
+ ## What 1.0 will promise
9
+
10
+ From 1.0 on, within a major version:
11
+
12
+ - **Sources keep compiling.** A program that compiles with 1.x compiles with every later 1.y, to a program with the same
13
+ account layouts, IDL, instruction and account discriminators and error codes. New releases add; they do not take away.
14
+ - **Binaries are reproducible and only change for a reason.** A release that changes the binary of a program that uses
15
+ none of its new features says so, with the release manifest's diff (`binary-diff.txt`) as the evidence.
16
+ - **The checks stay.** No check a program gets from its parameter types (owner, discriminator, signer, writability,
17
+ aliasing, checked arithmetic, constraints) is removed or weakened.
18
+ - **The tools keep their interfaces**: `typesol build`, `test` (via `typesol/testing`), `check --json`, `deploy`,
19
+ `verify`, `idl`, the typed client, and `typesol.json`.
20
+
21
+ ## Stable already
22
+
23
+ Meant to stay as they are; a change before 1.0 would be a breaking change listed as one:
24
+
25
+ - Types: the integers (`u8` … `u128`, `i8` … `i128`), `boolean`, `Pubkey`, `Hash`, structs, `FixedArray`, `FixedVec`,
26
+ `Vec`, `FixedString`, `Option`, unit and data enums, `u256` in computation.
27
+ - Account parameters and their checks: `Signer`, `Account<T>`, `Mut<T>`, `Init<T>`, `InitIfNeeded<T>`, `Zeroed<T>`,
28
+ optional accounts, token accounts and mints, programs, sysvars, account groups; the constraints (`HasOne`, `Seeds`,
29
+ `CloseTo`, `Ata`, `Address`, `Owner`, `Executable`).
30
+ - Errors and events: error enums and their codes, `require`, `AnchorError`, `emit`, `emitCpi`.
31
+ - CPIs: System, SPL Token, Token-2022, Associated Token, Memo, generic `invoke` with PDA signing.
32
+ - Layouts: account data (Anchor's discriminator and fixed or `@layout borsh` layout), the IDL, the client.
33
+
34
+ ## Experimental
35
+
36
+ Useful today, but may still change before 1.0 (each change listed in the CHANGELOG):
37
+
38
+ - The native-program surface (0.40.0): `loadRaw` / `loadRawMut`, the instruction data readers, error code 0,
39
+ `ProgramError`, `panic`, `PANIC_ON_OVERFLOW`, `ERROR_LOG_PREFIX` / `PROGRAM_ERROR_LOG_PREFIX`, `logBase64`,
40
+ `createProgramAddress`.
41
+ - Metaplex CPIs (Token Metadata, Core, Bubblegum), oracles (Pyth, Switchboard), the Stake program.
42
+ - Functions chosen at run time and lists of functions, closures returned from functions.
43
+ - Tools: `typesol port`, `clone`, `profile`, `cpi`, the editor plugin, the MCP server and the agent files `init`
44
+ writes, the SBPF v1 and v2 targets.
45
+
46
+ ## Before 1.0
47
+
48
+ - **An external audit** of the compiler and runtime ([audit package](audit.md)), its findings fixed and published.
49
+ - **Settle the open edges**, each either supported or refused with a clear error:
50
+ - a struct holding a FixedVec as both an argument and account data (two layouts, one IDL type);
51
+ - helpers that take a writable struct which is not an account (a `loadRaw` view, a local), now refused;
52
+ - `require` with an error code computed at run time that is 0: fails as an overflow (5100) today, where a native
53
+ program returns CUSTOM_ZERO;
54
+ - which experimental parts above become stable, and which stay outside the 1.0 promise.
55
+ - **A release candidate** (1.0.0-rc) used for the showcase ports and the templates, with no breaking change between it
56
+ and 1.0.0.