@typesol/cli 0.35.0 → 0.37.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.
@@ -119,8 +119,10 @@ Anchor instruction that takes or returns a struct with `String` / `Vec` / `Optio
119
119
  | `[test.validator.clone]` in Anchor.toml | `"clone": [...]` in typesol.json and `typesol clone` (loaded by `testProgram`) |
120
120
  | `anchor_spl::metadata::update_metadata_accounts_v2` | `metadataProgram.updateMetadataAccountsV2(…)` |
121
121
  | `anchor_spl::metadata::burn_nft` / mpl `BurnV1` | `metadataProgram.burnV1(…)` |
122
+ | `Sysvar<StakeHistory>` / `StakeHistory::get(epoch)` | `StakeHistorySysvar`: `effective(epoch)`, `activating(epoch)`, `deactivating(epoch)`, `has(epoch)` |
122
123
  | `EpochSchedule::get()?`, `LastRestartSlot::get()?` | `slotsPerEpoch()`, `epochWarmup()`, `firstNormalEpoch()`, `firstNormalSlot()`; `lastRestartSlot()` |
123
124
  | `anchor_spl::stake::{authorize, deactivate_stake, withdraw}`, `delegate_stake` | `stakeProgram.authorize / deactivate / withdraw / delegate(…)` |
125
+ | `RandomnessAccountData::parse(…)?.get_value(&clock)?` (switchboard-on-demand) | `randomness: SwitchboardRandomness`, `randomness.value()` / `valueU64()`, `randomness.seedSlot` |
124
126
  | `pub fn fallback(program_id, accounts, data)` | `/** @fallback */ export function fallback(): void` with `instructionData()` and `remainingAccounts()` |
125
127
  | `#[access_control(check(&ctx))]` | call the check first in the body (`check(vault, owner)`), or `constraint(acct, …)` for one that belongs with an account's checks |
126
128
  | `spl_memo::build_memo` + `invoke` | `memoProgram.memo(text, [signers])` |
package/lib/typesol.d.ts CHANGED
@@ -19,6 +19,12 @@ export type u16 = bigint;
19
19
  export type u32 = bigint;
20
20
  export type u64 = bigint;
21
21
  export type u128 = bigint;
22
+ /**
23
+ * 256-bit unsigned integers for computation (sqrt-price and liquidity math): locals, helper parameters and results,
24
+ * `u256(x)` and back with `u128(x)` / `u64(x)` (checked). Not in account data, arguments, events or return data
25
+ * (the IDL has no 256-bit integer).
26
+ */
27
+ export type u256 = bigint;
22
28
  export type i8 = bigint;
23
29
  export type i16 = bigint;
24
30
  export type i32 = bigint;
@@ -168,12 +174,14 @@ export interface RemainingSlice extends Iterable<CpiAccount> {
168
174
  export declare function remainingAccounts(): RemainingAccounts;
169
175
 
170
176
  /**
171
- * Read a struct of this program from a remaining account: owner, discriminator and size are checked.
177
+ * Read a struct of this program from a remaining account or an `AccountInfo` parameter (Anchor's
178
+ * `AccountLoader::try_from` on an `UncheckedAccount`): owner, discriminator and size are checked.
172
179
  * `loadAccountMut` also requires it to be writable, not aliased by another writable account, and not already loaded
173
- * with `loadAccountMut` in this instruction (2040).
180
+ * with `loadAccountMut` in this instruction (2040). The result is also the account (`.key`, `.lamports`), and passes
181
+ * to a helper that takes `Account<T>` / `Mut<T>`.
174
182
  */
175
- export declare function loadAccount<T>(account: RemainingAccount): Readonly<T>;
176
- export declare function loadAccountMut<T>(account: RemainingAccount): T;
183
+ export declare function loadAccount<T>(account: RemainingAccount | AccountInfo): Account<T>;
184
+ export declare function loadAccountMut<T>(account: RemainingAccount | AccountInfo): Mut<T>;
177
185
 
178
186
  /** 32 bytes of hash output (sha256 / keccak256, Merkle roots). Compare with `===`. */
179
187
  export interface Hash {
@@ -293,7 +301,27 @@ export declare function switchboardValue(
293
301
  minSamples?: u8,
294
302
  ): i64;
295
303
 
296
- /** The Associated Token Account program (ATokenGP…). Its address is checked. */
304
+ /**
305
+ * A Switchboard On-Demand randomness account (RandomnessAccountData): verifiable randomness by commit and reveal.
306
+ * Blockchains have no secret randomness of their own (slot hashes and timestamps are known to, or chosen by, the
307
+ * block producer), so the value comes from an oracle that commits to a future slot hash and reveals the result in a
308
+ * later transaction. Mainnet program unless `SwitchboardRandomness<"devnet">`; owner, discriminator and size checked.
309
+ *
310
+ * The pattern: in a commit instruction, require `seedSlot === currentSlot() - 1n` (committed just now, not yet
311
+ * revealed) and store the account's key and seedSlot; in the settle instruction, check both again and read `value()`.
312
+ */
313
+ export interface SwitchboardRandomness<Cluster extends "mainnet" | "devnet" = "mainnet"> extends AccountMeta {
314
+ readonly __switchboard_randomness: Cluster;
315
+ /** The slot whose hash seeds this randomness (set when it is committed). */
316
+ readonly seedSlot: u64;
317
+ /** The slot it was revealed in (0 before). */
318
+ readonly revealSlot: u64;
319
+ /** The revealed 32 bytes. Fails (5111) unless revealed in this slot, as Switchboard's `get_value`. */
320
+ value(): Hash;
321
+ /** The first 8 bytes of value() as a little-endian u64 (`value % 6n` for a die). */
322
+ valueU64(): u64;
323
+ }
324
+
297
325
  /** The SPL Memo program (v2, `MemoSq4g…`). Its address is checked. */
298
326
  export interface MemoProgram extends AccountMeta {
299
327
  readonly __memo_program: void;
@@ -343,6 +371,24 @@ export interface StakeProgram extends AccountMeta {
343
371
  ): void;
344
372
  }
345
373
 
374
+ /**
375
+ * The StakeHistory sysvar (`SysvarStakeHistory…`), read as an account: the cluster's stake per recent epoch (up to
376
+ * 512, newest first), as the Stake program uses it for warmup and cooldown. The address is checked.
377
+ */
378
+ export interface StakeHistorySysvar extends AccountMeta {
379
+ readonly __stake_history: void;
380
+ /** How many epochs it lists. */
381
+ readonly count: u64;
382
+ /** The newest epoch it lists (0 when empty). */
383
+ readonly latestEpoch: u64;
384
+ /** Whether `epoch` is listed; the readers below give 0 for an epoch that is not. */
385
+ has(epoch: u64): boolean;
386
+ /** Lamports of effective, activating and deactivating stake in `epoch`. */
387
+ effective(epoch: u64): u64;
388
+ activating(epoch: u64): u64;
389
+ deactivating(epoch: u64): u64;
390
+ }
391
+
346
392
  /**
347
393
  * The SlotHashes sysvar (`SysvarS1otHashes…`), read as an account: it is too large for the sysvar syscalls (Anchor's
348
394
  * `Sysvar<SlotHashes>` cannot load it either). The address is checked, and that it holds an entry (5103).
@@ -356,6 +402,7 @@ export interface SlotHashesSysvar extends AccountMeta {
356
402
  readonly latestHash: Hash;
357
403
  }
358
404
 
405
+ /** The Associated Token Account program (ATokenGP…). Its address is checked. */
359
406
  export interface AssociatedTokenProgram extends AccountMeta {
360
407
  readonly __ata_program: void;
361
408
  create(
@@ -496,6 +543,11 @@ export interface TokenMetadataProgram extends AccountMeta {
496
543
  /** The Metaplex master edition address of a mint (PDA ["metadata", program, mint, "edition"]). */
497
544
  export declare function masterEditionAddress(mint: Pubkey): Pubkey;
498
545
  /** The canonical program address of `seeds` under `program` (Rust `Pubkey::find_program_address(seeds, program).0`). */
546
+ /**
547
+ * An integer seed with its bytes most significant first (Rust's `x.to_be_bytes()`), inside a seeds list:
548
+ * `["tick_array", pool.key, bigEndian(startIndex)]`. In `Seeds<…>` write `"@startIndex:be"`.
549
+ */
550
+ export declare function bigEndian(value: bigint): Seed;
499
551
  export declare function findProgramAddress(seeds: Seed[], program: Pubkey): Pubkey;
500
552
  /** The bump of that canonical program address (`Pubkey::find_program_address(seeds, program).1`), to sign with it. */
501
553
  export declare function findProgramAddressBump(seeds: Seed[], program: Pubkey): u8;
@@ -1592,7 +1644,8 @@ interface TokenOps {
1592
1644
  transfer(from: TokenAccount, to: TokenAccount, authority: AccountMeta, amount: u64, signerSeeds?: Seed[] | SignerSeeds): void;
1593
1645
  transferChecked(from: TokenAccount, mint: Mint, to: TokenAccount, authority: AccountMeta, amount: u64, signerSeeds?: Seed[] | SignerSeeds): void;
1594
1646
  /** `mint`: a Mut<Mint>, or a mint this instruction created and initialized (Mut<Signer> / Mut<AccountInfo>). */
1595
- mintTo(mint: Mint | AccountMeta, to: TokenAccount, authority: AccountMeta, amount: u64, signerSeeds?: Seed[] | SignerSeeds): void;
1647
+ /** `to`: a Mut<TokenAccount>, or a Mut<AccountInfo> token account this instruction created (e.g. with `ata.create`). */
1648
+ mintTo(mint: Mint | AccountMeta, to: TokenAccount | AccountInfo, authority: AccountMeta, amount: u64, signerSeeds?: Seed[] | SignerSeeds): void;
1596
1649
  burn(from: TokenAccount, mint: Mint, authority: AccountMeta, amount: u64, signerSeeds?: Seed[] | SignerSeeds): void;
1597
1650
  /** Close a token account (empty), or under Token-2022 a mint with no supply left (its MintCloseAuthority signs). */
1598
1651
  closeAccount(account: TokenAccount | Mint, destination: AccountMeta, authority: AccountMeta, signerSeeds?: Seed[] | SignerSeeds): void;
@@ -1605,7 +1658,8 @@ interface TokenOps {
1605
1658
  burnChecked(from: TokenAccount, mint: Mint, authority: AccountMeta, amount: u64, signerSeeds?: Seed[] | SignerSeeds): void;
1606
1659
  /** Change or remove (null) an authority of a mint or token account: `AuthorityType.MintTokens`, …. */
1607
1660
  setAuthority(target: AccountMeta, currentAuthority: AccountMeta, authorityType: u8, newAuthority: Pubkey | null, signerSeeds?: Seed[] | SignerSeeds): void;
1608
- freezeAccount(account: TokenAccount, mint: AccountMeta, freezeAuthority: AccountMeta, signerSeeds?: Seed[] | SignerSeeds): void;
1661
+ /** `account`: a Mut<TokenAccount>, or (freezeAccount) a Mut<AccountInfo> token account this instruction created. */
1662
+ freezeAccount(account: TokenAccount | AccountInfo, mint: AccountMeta, freezeAuthority: AccountMeta, signerSeeds?: Seed[] | SignerSeeds): void;
1609
1663
  thawAccount(account: TokenAccount, mint: AccountMeta, freezeAuthority: AccountMeta, signerSeeds?: Seed[] | SignerSeeds): void;
1610
1664
  /** Update a wrapped-SOL account's amount after lamports were sent to it. */
1611
1665
  syncNative(account: TokenAccount): void;
@@ -1970,12 +2024,13 @@ export declare function u32(value: bigint): u32;
1970
2024
  export declare function u64(value: bigint): u64;
1971
2025
  export declare function i64(value: bigint): i64;
1972
2026
  export declare function u128(value: bigint): u128;
2027
+ export declare function u256(value: bigint): u256;
1973
2028
  export declare function i8(value: bigint): i8;
1974
2029
  export declare function i16(value: bigint): i16;
1975
2030
  export declare function i32(value: bigint): i32;
1976
2031
  export declare function i128(value: bigint): i128;
1977
2032
 
1978
- /** floor(a * b / c) with a double-width intermediate (all u64, or all u128); aborts if c is 0 or the result does not fit. */
2033
+ /** floor(a * b / c) with a double-width intermediate (all u64, all u128, or all u256); aborts if c is 0 or the result does not fit. */
1979
2034
  export declare function mulDiv(a: u64, b: u64, c: u64): u64;
1980
2035
  /** ceil(a * b / c), like mulDiv. */
1981
2036
  export declare function mulDivCeil(a: u64, b: u64, c: u64): u64;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typesol/cli",
3
- "version": "0.35.0",
3
+ "version": "0.37.0",
4
4
  "description": "Compile a TypeScript-syntax language directly to Solana SBF programs.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -186,6 +186,219 @@ static inline __attribute__((always_inline)) unsigned __int128 ts_rem_u128(unsig
186
186
  return a % b;
187
187
  }
188
188
 
189
+ /*
190
+ * u256: a computation type (locals, helper parameters and results; never stored), for math such as concentrated
191
+ * liquidity's sqrt-price formulas. Four little-endian 64-bit limbs. Additions, subtractions and comparisons work on
192
+ * the 64-bit limbs; multiplication and division on 32-bit limbs with 64-bit products (SBF has no 128-bit instructions,
193
+ * and the __int128 library routines cost hundreds of CU each). Division is Knuth's algorithm D, as Hacker's Delight
194
+ * writes it (divmnu), also used for mulDiv's 512-bit intermediate.
195
+ */
196
+ typedef struct {
197
+ uint64_t w[4];
198
+ } TsU256;
199
+
200
+ static inline TsU256 ts_u256_lit(uint64_t a, uint64_t b, uint64_t c, uint64_t d) {
201
+ TsU256 r = {{a, b, c, d}};
202
+ return r;
203
+ }
204
+ static inline TsU256 ts_u256_from_u128(unsigned __int128 v) { return ts_u256_lit((uint64_t)v, (uint64_t)(v >> 64), 0, 0); }
205
+ static inline bool ts_u256_is_zero(TsU256 a) { return (a.w[0] | a.w[1] | a.w[2] | a.w[3]) == 0; }
206
+ static inline int ts_u256_cmp(TsU256 a, TsU256 b) {
207
+ for (int i = 3; i >= 0; i--)
208
+ if (a.w[i] != b.w[i]) return a.w[i] < b.w[i] ? -1 : 1;
209
+ return 0;
210
+ }
211
+ /* Whether the value fits in `bits` (64 or 128) bits. */
212
+ static inline bool ts_u256_fits(TsU256 a, int bits) { return bits >= 128 ? (a.w[2] | a.w[3]) == 0 : (a.w[1] | a.w[2] | a.w[3]) == 0; }
213
+ static inline unsigned __int128 ts_u256_lo128(TsU256 a) { return ((unsigned __int128)a.w[1] << 64) | a.w[0]; }
214
+
215
+ static inline bool ts_u256_add(TsU256 a, TsU256 b, TsU256 *out) {
216
+ uint64_t carry = 0;
217
+ for (int i = 0; i < 4; i++) {
218
+ uint64_t s = a.w[i] + b.w[i];
219
+ uint64_t c1 = s < a.w[i];
220
+ uint64_t s2 = s + carry;
221
+ carry = c1 | (s2 < s);
222
+ out->w[i] = s2;
223
+ }
224
+ return carry != 0;
225
+ }
226
+ static inline bool ts_u256_sub(TsU256 a, TsU256 b, TsU256 *out) {
227
+ uint64_t borrow = 0;
228
+ for (int i = 0; i < 4; i++) {
229
+ uint64_t d = a.w[i] - b.w[i];
230
+ uint64_t b1 = a.w[i] < b.w[i];
231
+ uint64_t d2 = d - borrow;
232
+ borrow = b1 | (d < borrow);
233
+ out->w[i] = d2;
234
+ }
235
+ return borrow != 0;
236
+ }
237
+ static inline TsU256 ts_u256_bit(TsU256 a, TsU256 b, int op) {
238
+ TsU256 r;
239
+ for (int i = 0; i < 4; i++) r.w[i] = op == 0 ? a.w[i] & b.w[i] : op == 1 ? a.w[i] | b.w[i] : a.w[i] ^ b.w[i];
240
+ return r;
241
+ }
242
+ static inline TsU256 ts_u256_shl(TsU256 a, uint64_t s) {
243
+ TsU256 r = {{0, 0, 0, 0}};
244
+ int limbs = (int)(s / 64), bits = (int)(s % 64);
245
+ for (int i = 3; i >= limbs; i--) {
246
+ r.w[i] = a.w[i - limbs] << bits;
247
+ if (bits && i - limbs - 1 >= 0) r.w[i] |= a.w[i - limbs - 1] >> (64 - bits);
248
+ }
249
+ return r;
250
+ }
251
+ static inline TsU256 ts_u256_shr(TsU256 a, uint64_t s) {
252
+ TsU256 r = {{0, 0, 0, 0}};
253
+ int limbs = (int)(s / 64), bits = (int)(s % 64);
254
+ for (int i = 0; i + limbs < 4; i++) {
255
+ r.w[i] = a.w[i + limbs] >> bits;
256
+ if (bits && i + limbs + 1 < 4) r.w[i] |= a.w[i + limbs + 1] << (64 - bits);
257
+ }
258
+ return r;
259
+ }
260
+
261
+ static inline void ts_u256_to32(TsU256 a, uint32_t *x) {
262
+ for (int i = 0; i < 4; i++) {
263
+ x[2 * i] = (uint32_t)a.w[i];
264
+ x[2 * i + 1] = (uint32_t)(a.w[i] >> 32);
265
+ }
266
+ }
267
+ static inline TsU256 ts_u256_from32(const uint32_t *x) {
268
+ TsU256 r;
269
+ for (int i = 0; i < 4; i++) r.w[i] = (uint64_t)x[2 * i] | ((uint64_t)x[2 * i + 1] << 32);
270
+ return r;
271
+ }
272
+ /* r[0 .. n+m) = a[0 .. n) * b[0 .. m), 32-bit limbs. */
273
+ static __attribute__((noinline)) void ts_mul32(const uint32_t *a, int n, const uint32_t *b, int m, uint32_t *r) {
274
+ for (int i = 0; i < n + m; i++) r[i] = 0;
275
+ for (int j = 0; j < m; j++) {
276
+ uint64_t k = 0;
277
+ for (int i = 0; i < n; i++) {
278
+ uint64_t t = (uint64_t)a[i] * b[j] + r[i + j] + k;
279
+ r[i + j] = (uint32_t)t;
280
+ k = t >> 32;
281
+ }
282
+ r[j + n] = (uint32_t)k;
283
+ }
284
+ }
285
+ /*
286
+ * Knuth's algorithm D (Hacker's Delight, divmnu): q[0 .. m-n] = u / v and r[0 .. n) = u % v for an m-limb u and an
287
+ * n-limb v whose top limb is not zero, m >= n, 32-bit limbs, m <= 16.
288
+ */
289
+ static __attribute__((noinline)) void ts_divmnu(uint32_t *q, uint32_t *r, const uint32_t *u, const uint32_t *v, int m, int n) {
290
+ const uint64_t b = 4294967296ULL;
291
+ if (n == 1) {
292
+ uint64_t k = 0;
293
+ for (int j = m - 1; j >= 0; j--) {
294
+ uint64_t cur = (k << 32) | u[j];
295
+ q[j] = (uint32_t)(cur / v[0]);
296
+ k = cur - (uint64_t)q[j] * v[0];
297
+ }
298
+ r[0] = (uint32_t)k;
299
+ return;
300
+ }
301
+ uint32_t vn[8], un[17];
302
+ int s = __builtin_clz(v[n - 1]);
303
+ for (int i = n - 1; i > 0; i--) vn[i] = (v[i] << s) | (s ? v[i - 1] >> (32 - s) : 0);
304
+ vn[0] = v[0] << s;
305
+ un[m] = s ? u[m - 1] >> (32 - s) : 0;
306
+ for (int i = m - 1; i > 0; i--) un[i] = (u[i] << s) | (s ? u[i - 1] >> (32 - s) : 0);
307
+ un[0] = u[0] << s;
308
+ for (int j = m - n; j >= 0; j--) {
309
+ uint64_t num = ((uint64_t)un[j + n] << 32) | un[j + n - 1];
310
+ uint64_t qhat = num / vn[n - 1];
311
+ uint64_t rhat = num - qhat * vn[n - 1];
312
+ while (qhat >= b || qhat * vn[n - 2] > ((rhat << 32) | un[j + n - 2])) {
313
+ qhat--;
314
+ rhat += vn[n - 1];
315
+ if (rhat >= b) break;
316
+ }
317
+ int64_t t;
318
+ int64_t k = 0;
319
+ for (int i = 0; i < n; i++) {
320
+ uint64_t p = qhat * vn[i];
321
+ t = (int64_t)un[i + j] - k - (int64_t)(p & 0xFFFFFFFFULL);
322
+ un[i + j] = (uint32_t)t;
323
+ k = (int64_t)(p >> 32) - (t >> 32);
324
+ }
325
+ t = (int64_t)un[j + n] - k;
326
+ un[j + n] = (uint32_t)t;
327
+ q[j] = (uint32_t)qhat;
328
+ if (t < 0) {
329
+ q[j]--;
330
+ uint64_t c = 0;
331
+ for (int i = 0; i < n; i++) {
332
+ uint64_t sum = (uint64_t)un[i + j] + vn[i] + c;
333
+ un[i + j] = (uint32_t)sum;
334
+ c = sum >> 32;
335
+ }
336
+ un[j + n] += (uint32_t)c;
337
+ }
338
+ }
339
+ for (int i = 0; i < n; i++) r[i] = (un[i] >> s) | (s ? un[i + 1] << (32 - s) : 0);
340
+ }
341
+ static inline int ts_len32(const uint32_t *x, int n) {
342
+ while (n > 0 && x[n - 1] == 0) n--;
343
+ return n;
344
+ }
345
+ /* u / v (div) or u % v (!div), for an m-limb u (up to 16 limbs) and a 256-bit v, not zero. */
346
+ static __attribute__((noinline)) void ts_divmod32(const uint32_t *u, int mmax, TsU256 v, uint32_t *q, TsU256 *rem) {
347
+ uint32_t vv[8], r[8] = {0};
348
+ ts_u256_to32(v, vv);
349
+ int n = ts_len32(vv, 8), m = ts_len32(u, mmax);
350
+ for (int i = 0; i < mmax; i++) q[i] = 0;
351
+ if (m < n) {
352
+ for (int i = 0; i < 8 && i < mmax; i++) r[i] = u[i];
353
+ } else {
354
+ ts_divmnu(q, r, u, vv, m, n);
355
+ }
356
+ if (rem) *rem = ts_u256_from32(r);
357
+ }
358
+ static inline bool ts_u256_mul(TsU256 a, TsU256 b, TsU256 *out) {
359
+ if (ts_u256_fits(a, 128) && ts_u256_fits(b, 128) && ((a.w[1] | b.w[1]) == 0)) {
360
+ *out = ts_u256_from_u128((unsigned __int128)a.w[0] * b.w[0]);
361
+ return false;
362
+ }
363
+ uint32_t x[8], y[8], r[16];
364
+ ts_u256_to32(a, x);
365
+ ts_u256_to32(b, y);
366
+ ts_mul32(x, 8, y, 8, r);
367
+ *out = ts_u256_from32(r);
368
+ for (int i = 8; i < 16; i++)
369
+ if (r[i]) return true;
370
+ return false;
371
+ }
372
+ static inline TsU256 ts_u256_divmod(TsU256 a, TsU256 b, bool div) {
373
+ if (ts_u256_fits(a, 128) && ts_u256_fits(b, 128)) {
374
+ unsigned __int128 x = ts_u256_lo128(a), y = ts_u256_lo128(b);
375
+ return ts_u256_from_u128(div ? ts_div_u128(x, y) : ts_rem_u128(x, y));
376
+ }
377
+ uint32_t u[8], q[8];
378
+ TsU256 rem;
379
+ ts_u256_to32(a, u);
380
+ ts_divmod32(u, 8, b, q, &rem);
381
+ return div ? ts_u256_from32(q) : rem;
382
+ }
383
+ /* floor or ceil(a * b / c) with a 512-bit product; false when c is zero or the result needs more than 256 bits. */
384
+ static __attribute__((noinline)) bool ts_u256_muldiv(TsU256 a, TsU256 b, TsU256 c, bool ceil, TsU256 *out) {
385
+ if (ts_u256_is_zero(c)) return false;
386
+ uint32_t x[8], y[8], p[16], q[16];
387
+ ts_u256_to32(a, x);
388
+ ts_u256_to32(b, y);
389
+ ts_mul32(x, 8, y, 8, p);
390
+ TsU256 rem;
391
+ ts_divmod32(p, 16, c, q, &rem);
392
+ for (int i = 8; i < 16; i++)
393
+ if (q[i]) return false;
394
+ *out = ts_u256_from32(q);
395
+ if (ceil && !ts_u256_is_zero(rem)) {
396
+ TsU256 one = {{1, 0, 0, 0}};
397
+ if (ts_u256_add(*out, one, out)) return false;
398
+ }
399
+ return true;
400
+ }
401
+
189
402
  /* ---- Well-known program ids --------------------------------------------- */
190
403
  static const SolPubkey TS_SYSTEM_PROGRAM_ID = {{0}};
191
404
  /* TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA */
@@ -531,6 +744,18 @@ static inline uint64_t ts_claim_mut(TsParams *params, const TsAccount *a) {
531
744
  return SUCCESS;
532
745
  }
533
746
 
747
+ /* loadAccountMut of the account parameter at index `at`: its aliasing was checked on entry, a second load is refused. */
748
+ static inline uint64_t ts_claim_mut_at(TsParams *params, uint64_t at) {
749
+ #if TS_MAX_ACCOUNTS > 64
750
+ if (params->loaded_mut[at >> 6] >> (at & 63) & 1) TS_FAIL(TS_ERR_DUPLICATE_MUT, "TypeSOL: account already loaded with loadAccountMut");
751
+ params->loaded_mut[at >> 6] |= (uint64_t)1 << (at & 63);
752
+ #else
753
+ if (params->loaded_mut >> at & 1) TS_FAIL(TS_ERR_DUPLICATE_MUT, "TypeSOL: account already loaded with loadAccountMut");
754
+ params->loaded_mut |= (uint64_t)1 << at;
755
+ #endif
756
+ return SUCCESS;
757
+ }
758
+
534
759
  static inline uint64_t ts_check_address(const TsAccount *a, const SolPubkey *expected) {
535
760
  if (!ts_key_eq(&a->key, expected)) TS_FAIL(TS_ERR_WRONG_PROGRAM, "TypeSOL: unexpected program address");
536
761
  return SUCCESS;
@@ -1819,6 +2044,31 @@ static const uint8_t TS_SWITCHBOARD_FEED_DISC[8] = {196, 27, 108, 196, 10, 215,
1819
2044
  #define TS_SB_RESULT_SAMPLES (8 + 2256 + 96)
1820
2045
  #define TS_SB_RESULT_SLOT (8 + 2256 + 104)
1821
2046
 
2047
+ /* RandomnessAccountData: discriminator, authority, queue, seed slot hash, seed_slot @104, oracle, reveal_slot @144,
2048
+ value[32] @152, reserved; 384 bytes. */
2049
+ static const uint8_t TS_SWITCHBOARD_RANDOMNESS_DISC[8] = {10, 66, 229, 135, 220, 239, 217, 114};
2050
+ #define TS_SB_RAND_SEED_SLOT 104
2051
+ #define TS_SB_RAND_REVEAL_SLOT 144
2052
+ #define TS_SB_RAND_VALUE 152
2053
+
2054
+ static inline uint64_t ts_check_sb_randomness(const TsAccount *a, bool devnet) {
2055
+ if (!ts_key_eq(&a->owner, devnet ? &TS_SWITCHBOARD_DEVNET_ID : &TS_SWITCHBOARD_MAINNET_ID))
2056
+ TS_FAIL(TS_ERR_WRONG_OWNER, "TypeSOL: not a Switchboard randomness account (owner)");
2057
+ if (a->data_len < 384 || !ts_mem_eq(a->data, TS_SWITCHBOARD_RANDOMNESS_DISC, 8))
2058
+ TS_FAIL(TS_ERR_DISCRIMINATOR, "TypeSOL: not a Switchboard RandomnessAccountData account");
2059
+ return SUCCESS;
2060
+ }
2061
+
2062
+ /* The revealed value, only in the slot it was revealed in (Switchboard's get_value). */
2063
+ static inline uint64_t ts_sb_randomness_value(const TsAccount *a, const uint8_t **out) {
2064
+ uint64_t clock[5];
2065
+ if (sol_get_clock_sysvar(clock) != SUCCESS) TS_FAIL(TS_ERR_SYSVAR, "TypeSOL: clock sysvar unavailable");
2066
+ if (ts_read_le(a->data + TS_SB_RAND_REVEAL_SLOT, 8) != clock[0])
2067
+ TS_FAIL(TS_ERR_ORACLE, "TypeSOL: the randomness was not revealed in this slot");
2068
+ *out = a->data + TS_SB_RAND_VALUE;
2069
+ return SUCCESS;
2070
+ }
2071
+
1822
2072
  static inline uint64_t ts_check_switchboard(const TsAccount *a, bool devnet) {
1823
2073
  if (!ts_key_eq(&a->owner, devnet ? &TS_SWITCHBOARD_DEVNET_ID : &TS_SWITCHBOARD_MAINNET_ID))
1824
2074
  TS_FAIL(TS_ERR_WRONG_OWNER, "TypeSOL: not a Switchboard feed (owner)");
@@ -1854,6 +2104,22 @@ static inline uint64_t ts_switchboard_value(const TsAccount *a, uint64_t max_sta
1854
2104
  /* ---- Associated token accounts, token metadata, transfer hooks ------------------- */
1855
2105
  static const SolPubkey TS_STAKE_PROGRAM_ID = {{6,161,216,23,145,55,84,42,152,52,55,189,254,42,122,178,85,127,83,92,138,120,114,43,104,164,157,192,0,0,0,0}};
1856
2106
  static const SolPubkey TS_MEMO_PROGRAM_ID = {{5,74,83,90,153,41,33,6,77,36,232,113,96,218,56,124,124,53,181,221,188,146,187,129,228,31,168,64,65,5,68,141}};
2107
+ static const SolPubkey TS_STAKE_HISTORY_ID = {{6,167,213,23,25,53,132,208,254,237,155,179,67,29,19,32,107,229,68,40,27,87,184,86,108,197,55,95,244,0,0,0}};
2108
+ /* StakeHistory: u64 count, then (epoch, effective, activating, deactivating) u64 entries, newest first. The entry for
2109
+ `epoch` (field 1..3 of it), or 0 when it is not listed; `found` says which. */
2110
+ static inline uint64_t ts_stake_history(const TsAccount *a, uint64_t epoch, int field, bool *found) {
2111
+ uint64_t n = ts_read_le(a->data, 8);
2112
+ if (n > (a->data_len - 8) / 32) n = (a->data_len - 8) / 32;
2113
+ for (uint64_t i = 0; i < n; i++) {
2114
+ const uint8_t *e = a->data + 8 + 32 * i;
2115
+ if (ts_read_le(e, 8) == epoch) {
2116
+ *found = true;
2117
+ return ts_read_le(e + 8 * field, 8);
2118
+ }
2119
+ }
2120
+ *found = false;
2121
+ return 0;
2122
+ }
1857
2123
  static const SolPubkey TS_SLOT_HASHES_ID = {{6,167,213,23,25,47,10,175,198,242,101,227,251,119,204,122,218,130,197,41,208,190,59,19,110,45,0,85,32,0,0,0}};
1858
2124
  static const SolPubkey TS_ATA_PROGRAM_ID = {{140,151,37,143,78,36,137,241,187,61,16,41,20,142,13,131,11,90,19,153,218,255,16,132,4,142,123,216,219,233,248,89}};
1859
2125
  static const SolPubkey TS_MPL_METADATA_ID = {{11,112,101,177,227,209,124,69,56,157,82,127,107,4,195,205,88,184,108,115,26,160,253,181,73,182,209,188,3,248,41,70}};