@typesol/cli 0.30.0 → 0.31.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.
- package/CHANGELOG.md +14 -0
- package/README.md +4 -1
- package/dist/compiler/emit/calls.js +32 -0
- package/docs/case-study-raydium-cp-swap.md +122 -0
- package/docs/language.md +1 -1
- package/lib/typesol.d.ts +15 -0
- package/package.json +1 -1
- package/runtime/typesol_rt.h +56 -0
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,20 @@ codes, and says so here.
|
|
|
6
6
|
|
|
7
7
|
## Unreleased
|
|
8
8
|
|
|
9
|
+
## 0.31.0 — 2026-10-30
|
|
10
|
+
|
|
11
|
+
A second real program checked against its mainnet binary: Raydium CP-Swap's trading core, and the Token-2022 reads an
|
|
12
|
+
AMM needs. Programs that do not use the new reads compile to the same binaries as in 0.30.0.
|
|
13
|
+
|
|
14
|
+
- New: Token-2022 fee and extension reads: `transferFeeInverse(mint, amountReceived)` (spl-token-2022's
|
|
15
|
+
`calculate_inverse_epoch_fee`: what to send so the recipient gets that much), `transferFeeBasisPoints(mint)` and
|
|
16
|
+
`transferFeeMaximum(mint)` (this epoch's), and `extensionsWithin(account, [types…])` (accept only mints whose
|
|
17
|
+
extensions you handle).
|
|
18
|
+
- New: examples/raydium-cp-swap, Raydium CP-Swap's trading core (swaps both ways, deposit, withdraw, fee collection,
|
|
19
|
+
config and status admin; 9 of its 24 instructions), checked against the program deployed on mainnet on 59
|
|
20
|
+
transactions (creator fees, the price oracle's wrap and Token-2022 transfer fees included): same outcomes, account
|
|
21
|
+
bytes and events, 51–79% fewer compute units. docs/case-study-raydium-cp-swap.md.
|
|
22
|
+
|
|
9
23
|
## 0.30.0 — 2026-10-30
|
|
10
24
|
|
|
11
25
|
Editor errors that follow you through a program, faster. The compiler's output is the same as 0.29.0 for every
|
package/README.md
CHANGED
|
@@ -176,7 +176,9 @@ program's address.
|
|
|
176
176
|
Coming from Anchor: [docs/migrate-from-anchor.md](docs/migrate-from-anchor.md) maps every construct, `typesol port`
|
|
177
177
|
starts from your program's IDL, and [docs/case-study-escrow.md](docs/case-study-escrow.md) follows one complete Anchor
|
|
178
178
|
program through the port; [docs/case-study-multisig.md](docs/case-study-multisig.md) ports coral-xyz/multisig and
|
|
179
|
-
checks it against the binary deployed on mainnet (same outcomes and account bytes; 66–84% fewer compute units)
|
|
179
|
+
checks it against the binary deployed on mainnet (same outcomes and account bytes; 66–84% fewer compute units), and
|
|
180
|
+
[docs/case-study-raydium-cp-swap.md](docs/case-study-raydium-cp-swap.md) does the same for Raydium CP-Swap's swaps and liquidity
|
|
181
|
+
(same outcomes, account bytes and events on 59 transactions; 51–79% fewer compute units). Tests use `typesol/testing` (accounts by name, plain values; [docs/test-your-program.md](docs/test-your-program.md)). To go to
|
|
180
182
|
mainnet (wallet, cost, deploy, IDL, verification), follow [docs/mainnet.md](docs/mainnet.md).
|
|
181
183
|
|
|
182
184
|
## How it works
|
|
@@ -324,6 +326,7 @@ Details are in [docs/testing.md](docs/testing.md).
|
|
|
324
326
|
| [`relay.ts`](examples/relay.ts), [`caller.ts`](examples/caller.ts) | generic `invoke`, PDA-signed transfers, return data |
|
|
325
327
|
| [`profiles.ts`](examples/profiles.ts) | nested structs, FixedString, events |
|
|
326
328
|
| [`orderbook/`](examples/orderbook) | a three-file, 28 KiB zero-copy limit order book |
|
|
329
|
+
| [`raydium-cp-swap/`](examples/raydium-cp-swap) | Raydium CP-Swap's trading core: constant-product swaps, LP deposits and withdrawals, Token-2022 transfer fees, a price oracle |
|
|
327
330
|
| [`multisig.ts`](examples/multisig.ts) | coral-xyz/multisig: stored instructions run with `invokeInstruction`, a PDA signing, Anchor's Borsh accounts |
|
|
328
331
|
|
|
329
332
|
## Status
|
|
@@ -241,6 +241,38 @@ export class CallEmitter extends ExpressionEmitter {
|
|
|
241
241
|
this.line(`TS_TRY(ts_t22_transfer_fee(${m}, ${amt.c}, &${t}));`);
|
|
242
242
|
return { c: t, type: U64 };
|
|
243
243
|
}
|
|
244
|
+
case "transferFeeInverse": {
|
|
245
|
+
if (args.length !== 2)
|
|
246
|
+
this.fail(node, "transferFeeInverse(mint, amountReceived) takes the mint and the amount the recipient is to get");
|
|
247
|
+
const m = this.anyAccount(args[0], "the mint");
|
|
248
|
+
this.line(`TS_TRY(ts_t22_layout(${m}, 1));`);
|
|
249
|
+
const amt = this.expect(this.expr(args[1], U64), U64, args[1]);
|
|
250
|
+
const t = this.tmp("fee");
|
|
251
|
+
this.line(`uint64_t ${t};`);
|
|
252
|
+
this.line(`TS_TRY(ts_t22_transfer_fee_inverse(${m}, ${amt.c}, &${t}));`);
|
|
253
|
+
return { c: t, type: U64 };
|
|
254
|
+
}
|
|
255
|
+
case "transferFeeBasisPoints":
|
|
256
|
+
case "transferFeeMaximum": {
|
|
257
|
+
if (args.length !== 1)
|
|
258
|
+
this.fail(node, `${builtin}(mint) takes the mint`);
|
|
259
|
+
const m = this.anyAccount(args[0], "the mint");
|
|
260
|
+
this.line(`TS_TRY(ts_t22_layout(${m}, 1));`);
|
|
261
|
+
const t = this.tmp("tf");
|
|
262
|
+
this.line(`uint64_t ${t}b, ${t}m;`);
|
|
263
|
+
this.line(`TS_TRY(ts_t22_epoch_fee(${m}, &${t}b, &${t}m));`);
|
|
264
|
+
return builtin === "transferFeeBasisPoints" ? { c: `((uint16_t)${t}b)`, type: { kind: "int", name: "u16" } } : { c: `${t}m`, type: U64 };
|
|
265
|
+
}
|
|
266
|
+
case "extensionsWithin": {
|
|
267
|
+
if (args.length !== 2 || !ts.isArrayLiteralExpression(args[1]))
|
|
268
|
+
this.fail(node, "extensionsWithin(account, [types…]) takes a Token-2022 mint or account and a list of u16 extension types");
|
|
269
|
+
const m = this.anyAccount(args[0], "the account");
|
|
270
|
+
this.line(`TS_TRY(ts_t22_layout(${m}, 0));`);
|
|
271
|
+
const types = args[1].elements.map((e) => this.expect(this.expr(e, { kind: "int", name: "u16" }), { kind: "int", name: "u16" }, e).c);
|
|
272
|
+
const t = this.tmp("ext");
|
|
273
|
+
this.line(`const uint16_t ${t}[${Math.max(types.length, 1)}] = {${types.join(", ")}};`);
|
|
274
|
+
return { c: `ts_t22_extensions_within(${m}, ${t}, ${types.length})`, type: BOOL };
|
|
275
|
+
}
|
|
244
276
|
case "isTransferring": {
|
|
245
277
|
// TransferHookAccount (15) on a Token-2022 token account: set only while Token-2022 runs a transfer of it.
|
|
246
278
|
if (args.length !== 1)
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# Case study: Raydium CP-Swap
|
|
2
|
+
|
|
3
|
+
This page ports the trading core of a production AMM: [Raydium CP-Swap](https://github.com/raydium-io/raydium-cp-swap)
|
|
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/`](../examples/raydium-cp-swap)
|
|
6
|
+
(`state.ts`, `curve.ts`, `raydium_cp_swap.ts`). As with [coral-xyz/multisig](case-study-multisig.md), it is checked
|
|
7
|
+
against the binary that runs on mainnet, not a rebuild.
|
|
8
|
+
|
|
9
|
+
## What is ported
|
|
10
|
+
|
|
11
|
+
The port covers 9 of the original's 24 instructions:
|
|
12
|
+
|
|
13
|
+
- the four trading instructions: `swap_base_input`, `swap_base_output`, `deposit` and `withdraw`;
|
|
14
|
+
- `collect_protocol_fee` and `collect_fund_fee`;
|
|
15
|
+
- the admin instructions `create_amm_config`, `update_amm_config` and `update_pool_status`.
|
|
16
|
+
|
|
17
|
+
The port keeps the original's account layouts:
|
|
18
|
+
|
|
19
|
+
- `AmmConfig` is a Borsh account of fixed size.
|
|
20
|
+
- `PoolState` and `ObservationState` are zero-copy `#[repr(C, packed)]` structs, which is TypeSOL's fixed layout.
|
|
21
|
+
|
|
22
|
+
It also keeps the original's names, argument order and error codes, and emits the same events (`SwapEvent`,
|
|
23
|
+
`LpChangeEvent`). Existing pools, clients and indexers work with it unchanged.
|
|
24
|
+
|
|
25
|
+
These are not ported yet:
|
|
26
|
+
|
|
27
|
+
- **`initialize` and `initialize_with_permission`.** They create a pool at either a PDA or a signer keypair, chosen at
|
|
28
|
+
run time, and allocate a program account by hand: transfer, allocate and assign when the address was pre-funded.
|
|
29
|
+
TypeSOL's `Init<T>` fixes the choice at compile time and reports an existing account with its own error.
|
|
30
|
+
- **The creator-fee instructions**: `collect_creator_fee`, `collect_creator_fee_permissionless`,
|
|
31
|
+
`create_creator_fee_share` and `close_creator_fee_share`.
|
|
32
|
+
- **The permission instructions**: `create_permission_pda` and `close_permission_pda`.
|
|
33
|
+
- **The support-mint instructions**: `create_support_mint_associated` and `close_support_mint_associated`.
|
|
34
|
+
- **`collect_excess_lamports`.**
|
|
35
|
+
|
|
36
|
+
The pool logic these depend on is in the port (fee booking, the creator-fee split in the swap math, tested below with
|
|
37
|
+
the creator fee on), so a pool the original created and configured is traded the same way.
|
|
38
|
+
|
|
39
|
+
## What it needed
|
|
40
|
+
|
|
41
|
+
Four Token-2022 reads, added to the compiler for this port (`test/integrations/t22ext.test.ts`):
|
|
42
|
+
|
|
43
|
+
- `transferFeeInverse(mint, amountReceived)`: spl-token-2022's `calculate_inverse_epoch_fee`, used for deposits and
|
|
44
|
+
exact-output swaps.
|
|
45
|
+
- `transferFeeBasisPoints(mint)` and `transferFeeMaximum(mint)`: the fee in force this epoch.
|
|
46
|
+
- `extensionsWithin(account, [types])`: the original's list of mint extensions a pool accepts.
|
|
47
|
+
|
|
48
|
+
The rest uses features TypeSOL already had:
|
|
49
|
+
|
|
50
|
+
- `constraint(…)`, for Anchor's `address = pool_state.load()?.amm_config` and `constraint = …` in Anchor's order;
|
|
51
|
+
- u128 arithmetic for the constant-product curve and the price oracle;
|
|
52
|
+
- token CPIs signed by the pool authority PDA;
|
|
53
|
+
- events.
|
|
54
|
+
|
|
55
|
+
## How it was checked
|
|
56
|
+
|
|
57
|
+
[`test/examples/raydium-cp-swap.test.ts`](../test/examples/raydium-cp-swap.test.ts) loads the binary dumped from
|
|
58
|
+
mainnet on 2026-10-30 (slot 452376460; sha256 in [`test/programs/SOURCES.md`](../test/programs/SOURCES.md)) into one
|
|
59
|
+
LiteSVM and the port into another. Both use the same program id and the same clock.
|
|
60
|
+
|
|
61
|
+
**Setup.**
|
|
62
|
+
|
|
63
|
+
- Mints and token accounts are made by the same SPL Token / Token-2022 instructions in both.
|
|
64
|
+
- The program's own accounts (an `AmmConfig`, a pool's `PoolState` and its `ObservationState`) are written in both as
|
|
65
|
+
the original lays them out, because creating a config needs Raydium's admin key.
|
|
66
|
+
|
|
67
|
+
**Comparison.** For each transaction, the test requires the same outcome (success, or the same error down to the
|
|
68
|
+
code), the same lamports, owner and data for every account involved, and the same events byte for byte.
|
|
69
|
+
|
|
70
|
+
There are 59 transactions:
|
|
71
|
+
|
|
72
|
+
- **Swaps.** Both directions, by input and by output; the observation ring advancing as the clock moves; slippage
|
|
73
|
+
(6005); zero amounts.
|
|
74
|
+
- **Liquidity.** Deposit, a partial withdrawal, slippage both ways, withdrawing more LP than held, zero.
|
|
75
|
+
- **Fees.** Protocol and fund fees collected by their owners after trading, and refused for anyone else (6001).
|
|
76
|
+
- **Creator fees.** Pools with the creator fee on at 0.05%, taken from the input token, only token_0, or only
|
|
77
|
+
token_1: swaps both ways, by input and by output, booking the fee on the right side.
|
|
78
|
+
- **The price oracle.** A new point, a second swap within 15 seconds (added to the current point), and a ring at its
|
|
79
|
+
last slot whose cumulative price is 10 below `u128::MAX`: the next point wraps to slot 0 and the sum wraps around.
|
|
80
|
+
- **Token-2022.** A pool of two transfer-fee mints (1.5% and 0.37%): swaps both ways and by output, deposit and
|
|
81
|
+
withdrawal, through the inverse-fee path; and a pool of Token-2022 mints without a fee.
|
|
82
|
+
- **Refusals.**
|
|
83
|
+
- Another config (2012), another pool's vault (2003), another observation account (2012) or mint (2012).
|
|
84
|
+
- The same vault twice, a wrong pool authority (2006), another trader's account.
|
|
85
|
+
- Wrong vaults or LP mint on deposit (6004), the wrong token program.
|
|
86
|
+
- Swaps or deposits on a pool whose status disables them (6000).
|
|
87
|
+
- The admin instructions sent by anyone but the admin (2012, 6001).
|
|
88
|
+
|
|
89
|
+
## Cost
|
|
90
|
+
|
|
91
|
+
Compute units per transaction, mainnet original against the port, from that test:
|
|
92
|
+
|
|
93
|
+
| Instruction | Raydium (mainnet) | TypeSOL | |
|
|
94
|
+
| --- | ---: | ---: | ---: |
|
|
95
|
+
| `swap_base_input` | 24,142 | 7,478 | −69% |
|
|
96
|
+
| `swap_base_output` | 24,571 | 8,029 | −67% |
|
|
97
|
+
| `deposit` | 25,004 | 6,594 | −74% |
|
|
98
|
+
| `withdraw` | 25,211 | 6,451 | −74% |
|
|
99
|
+
| `collect_protocol_fee` | 20,205 | 4,200 | −79% |
|
|
100
|
+
| `swap_base_input`, Token-2022 transfer fees | 31,334 | 13,813 | −56% |
|
|
101
|
+
| `swap_base_output`, Token-2022 transfer fees | 32,511 | 15,935 | −51% |
|
|
102
|
+
| `deposit`, Token-2022 transfer fees | 33,206 | 14,834 | −55% |
|
|
103
|
+
|
|
104
|
+
The Token-2022 rows include the token program's own work, which is the same in both. Refusals cost far less. For
|
|
105
|
+
example, a swap through the wrong config costs 17,190 against 1,628.
|
|
106
|
+
|
|
107
|
+
On size, the port's binary is 105,952 bytes and the deployed original is 793,824 bytes. The original also contains the
|
|
108
|
+
15 instructions not ported here, so the size ratio overstates the gain for the same feature set.
|
|
109
|
+
|
|
110
|
+
## Where they differ
|
|
111
|
+
|
|
112
|
+
- **Panics.** Where the original calls `unwrap()` or `assert!`, it aborts with `ProgramFailedToComplete`. The port
|
|
113
|
+
fails with a numbered error instead:
|
|
114
|
+
- an overflow or underflow, 5100: an exact-output swap for more than the vault holds;
|
|
115
|
+
- a division by zero, 5101: the price of an empty side, or an exact-output swap for exactly what the vault holds;
|
|
116
|
+
- invalid input, 6003: a fee rate out of range in the admin instructions, which only the admin can reach.
|
|
117
|
+
|
|
118
|
+
The swaps and liquidity instructions reach none of these with the amounts a pool can hold.
|
|
119
|
+
- **Check order on double faults.** Account checks run in Anchor's order, so a transaction breaking one check gets the
|
|
120
|
+
same code. The port keeps the pool's `address = …` and `token::…` constraints as `constraint(…)` calls in Anchor's
|
|
121
|
+
order. A transaction that breaks a token check and an account-type check at once can still be reported differently.
|
|
122
|
+
- **Logs.** Error messages are TypeSOL's. Codes and events are the original's.
|
package/docs/language.md
CHANGED
|
@@ -51,7 +51,7 @@ The full reference. For a tour, start with the [README](../README.md).
|
|
|
51
51
|
| `++` `--` `+=` `-=` `*=` `/=` `%=` `&=` `\|=` `^=` `<<=` `>>=` | all checked |
|
|
52
52
|
| Sysvars | `now()`, `currentSlot()`, `currentEpoch()` (clock), `minimumBalance(account)`, `rentExempt(space)` (rent) |
|
|
53
53
|
| Account size | `realloc(account, newSize, payer)` on a `Mut<Struct>` — growing charges the payer the extra rent (at most 10 KiB per instruction, 3016); shrinking keeps the lamports in the account; never below the struct. Refused on a struct with a growable `Vec` (its push / pop / clear size the account) |
|
|
54
|
-
| Token-2022 extensions | `transferFee(mint, amount)` (current-epoch TransferFeeConfig, ceil + cap like spl-token-2022), `transferHookProgram(mint)`, `transferHookAuthority(mint)`, `permanentDelegate(mint)`, `mintCloseAuthority(mint)` (Option<Pubkey>), `hasExtension(account, type)`, `isTransferring(tokenAccount)` (inside a transfer hook: whether Token-2022 is transferring from it) |
|
|
54
|
+
| Token-2022 extensions | `transferFee(mint, amount)` (current-epoch TransferFeeConfig, ceil + cap like spl-token-2022), `transferFeeInverse(mint, amountReceived)` (the fee when the recipient is to get that much: spl-token-2022's `calculate_inverse_epoch_fee`), `transferFeeBasisPoints(mint)` / `transferFeeMaximum(mint)` (this epoch's; 0 without the extension), `extensionsWithin(account, [1n, 18n, …])` (every extension is one of those types), `transferHookProgram(mint)`, `transferHookAuthority(mint)`, `permanentDelegate(mint)`, `mintCloseAuthority(mint)` (Option<Pubkey>), `hasExtension(account, type)`, `isTransferring(tokenAccount)` (inside a transfer hook: whether Token-2022 is transferring from it) |
|
|
55
55
|
| Events through CPI | `emitCpi<Paid>({ … }, eventAuthority, self)` with `eventAuthority: EventAuthority, self: SelfProgram` parameters — Anchor `emit_cpi!`: the event rides in a self-CPI (inner instruction), not in logs; the program accepts Anchor's event instruction (2002 / 2006 otherwise) |
|
|
56
56
|
| Heap | 32 KiB by default; `const HEAP_SIZE_KIB = 64n` (32–256) lets the program use more, when clients add ComputeBudget `requestHeapFrame` |
|
|
57
57
|
| Docs | JSDoc on instructions, `@param`, structs, fields and data enums becomes `docs` in the IDL |
|
package/lib/typesol.d.ts
CHANGED
|
@@ -490,6 +490,21 @@ export declare function realloc(account: AccountMeta, newSize: u64, payer: Mut<S
|
|
|
490
490
|
* transferFee: the fee withheld when `amount` is sent now (TransferFeeConfig for the current epoch; 0 without it).
|
|
491
491
|
*/
|
|
492
492
|
export declare function transferFee(mint: AccountMeta | RemainingAccount, amount: u64): u64;
|
|
493
|
+
/**
|
|
494
|
+
* The fee withheld when the recipient is to get `amountReceived` (spl-token-2022's `calculate_inverse_epoch_fee`):
|
|
495
|
+
* send `amountReceived + transferFeeInverse(mint, amountReceived)`. 0 without TransferFeeConfig; fails (5100) where
|
|
496
|
+
* spl-token-2022 has no answer.
|
|
497
|
+
*/
|
|
498
|
+
export declare function transferFeeInverse(mint: AccountMeta | RemainingAccount, amountReceived: u64): u64;
|
|
499
|
+
/** The TransferFeeConfig in force this epoch: its basis points (0 without the extension). */
|
|
500
|
+
export declare function transferFeeBasisPoints(mint: AccountMeta | RemainingAccount): u16;
|
|
501
|
+
/** The TransferFeeConfig in force this epoch: its maximum fee (0 without the extension). */
|
|
502
|
+
export declare function transferFeeMaximum(mint: AccountMeta | RemainingAccount): u64;
|
|
503
|
+
/**
|
|
504
|
+
* Whether every Token-2022 extension of `account` is one of `types` (a list of u16 extension types, e.g.
|
|
505
|
+
* `[1n, 18n, 19n, 10n, 25n]`): a pool that accepts only mints it knows how to handle. True for legacy SPL Token accounts.
|
|
506
|
+
*/
|
|
507
|
+
export declare function extensionsWithin(account: AccountMeta | RemainingAccount, types: u16[]): boolean;
|
|
493
508
|
/** The TransferHook extension's program, if any. */
|
|
494
509
|
export declare function transferHookProgram(mint: AccountMeta | RemainingAccount): Option<Pubkey>;
|
|
495
510
|
/** The TransferHook extension's authority (who may change the hook program), if any. */
|
package/package.json
CHANGED
package/runtime/typesol_rt.h
CHANGED
|
@@ -1058,6 +1058,62 @@ static inline uint64_t ts_t22_transfer_fee(const TsAccount *mint, uint64_t amoun
|
|
|
1058
1058
|
return SUCCESS;
|
|
1059
1059
|
}
|
|
1060
1060
|
|
|
1061
|
+
/* The TransferFee in force this epoch (TransferFeeConfig, extension 1): basis points and maximum, both 0 without one. */
|
|
1062
|
+
static inline uint64_t ts_t22_epoch_fee(const TsAccount *mint, uint64_t *bps, uint64_t *maximum) {
|
|
1063
|
+
uint64_t off, len;
|
|
1064
|
+
*bps = 0;
|
|
1065
|
+
*maximum = 0;
|
|
1066
|
+
if (!ts_t22_ext(mint, 1, &off, &len) || len < 108) return SUCCESS;
|
|
1067
|
+
uint64_t epoch;
|
|
1068
|
+
TS_TRY(ts_clock_u64(2, &epoch));
|
|
1069
|
+
const uint8_t *newer = mint->data + off + 90;
|
|
1070
|
+
const uint8_t *fee = epoch >= ts_read_le(newer, 8) ? newer : mint->data + off + 72;
|
|
1071
|
+
*maximum = ts_read_le(fee + 8, 8);
|
|
1072
|
+
*bps = ts_read_le(fee + 16, 2);
|
|
1073
|
+
return SUCCESS;
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
/* The fee withheld when the recipient is to get `post` (spl-token-2022's calculate_inverse_epoch_fee): the amount to
|
|
1077
|
+
send, as calculate_pre_fee_amount finds it, then the fee on it. Fails (5100) where spl-token-2022 returns None. */
|
|
1078
|
+
static inline uint64_t ts_t22_transfer_fee_inverse(const TsAccount *mint, uint64_t post, uint64_t *out) {
|
|
1079
|
+
uint64_t bps, maximum, pre;
|
|
1080
|
+
*out = 0;
|
|
1081
|
+
TS_TRY(ts_t22_epoch_fee(mint, &bps, &maximum));
|
|
1082
|
+
if (bps == 0) pre = post;
|
|
1083
|
+
else if (post == 0) pre = 0;
|
|
1084
|
+
else if (bps == 10000) {
|
|
1085
|
+
if (__builtin_add_overflow(maximum, post, &pre)) TS_FAIL(TS_ERR_OVERFLOW, "TypeSOL: transfer fee overflow");
|
|
1086
|
+
} else {
|
|
1087
|
+
unsigned __int128 raw = ((unsigned __int128)post * 10000 + (10000 - bps) - 1) / (10000 - bps);
|
|
1088
|
+
if (raw - post >= maximum) {
|
|
1089
|
+
if (__builtin_add_overflow(post, maximum, &pre)) TS_FAIL(TS_ERR_OVERFLOW, "TypeSOL: transfer fee overflow");
|
|
1090
|
+
} else {
|
|
1091
|
+
if (raw > UINT64_MAX) TS_FAIL(TS_ERR_OVERFLOW, "TypeSOL: transfer fee overflow");
|
|
1092
|
+
pre = (uint64_t)raw;
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
if (bps == 0 || pre == 0) return SUCCESS;
|
|
1096
|
+
unsigned __int128 fee = ((unsigned __int128)pre * bps + 9999) / 10000;
|
|
1097
|
+
*out = fee > maximum ? maximum : (uint64_t)fee;
|
|
1098
|
+
return SUCCESS;
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
/* Whether every extension of a Token-2022 mint or account is one of `allowed` (true for legacy SPL Token ones). */
|
|
1102
|
+
static inline bool ts_t22_extensions_within(const TsAccount *a, const uint16_t *allowed, uint64_t n) {
|
|
1103
|
+
if (!ts_key_eq(&a->owner, &TS_TOKEN_2022_PROGRAM_ID) || a->data_len <= TS_TOKEN_ACCOUNT_LEN + 1) return true;
|
|
1104
|
+
uint64_t at = TS_TOKEN_ACCOUNT_LEN + 1;
|
|
1105
|
+
while (at + 4 <= a->data_len) {
|
|
1106
|
+
uint16_t t = (uint16_t)ts_read_le(a->data + at, 2);
|
|
1107
|
+
uint16_t len = (uint16_t)ts_read_le(a->data + at + 2, 2);
|
|
1108
|
+
if (t == 0) return true; /* uninitialized: end of the entries */
|
|
1109
|
+
bool ok = false;
|
|
1110
|
+
for (uint64_t i = 0; i < n; i++) ok = ok || allowed[i] == t;
|
|
1111
|
+
if (!ok) return false;
|
|
1112
|
+
at += 4 + (uint64_t)len;
|
|
1113
|
+
}
|
|
1114
|
+
return true;
|
|
1115
|
+
}
|
|
1116
|
+
|
|
1061
1117
|
/* ---- Metaplex Core and Bubblegum ----------------------------------------- */
|
|
1062
1118
|
static const SolPubkey TS_MPL_CORE_ID = {{175, 84, 171, 16, 189, 151, 165, 66, 160, 158, 247, 179, 152, 137, 221, 12, 211, 148, 164, 204, 233, 223, 166, 205, 201, 126, 190, 45, 35, 91, 167, 72}};
|
|
1063
1119
|
static const SolPubkey TS_BUBBLEGUM_ID = {{152, 139, 128, 235, 121, 53, 40, 105, 178, 36, 116, 95, 89, 221, 191, 138, 38, 88, 202, 19, 220, 104, 129, 33, 38, 53, 28, 174, 7, 193, 165, 165}};
|