@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.
- package/CHANGELOG.md +373 -2
- package/README.md +43 -26
- package/SECURITY.md +1 -1
- package/dist/cli.js +37 -3
- package/dist/compiler/analyze.d.ts +6 -11
- package/dist/compiler/analyze.js +331 -34
- package/dist/compiler/emit/alias.js +3 -2
- package/dist/compiler/emit/base.d.ts +7 -1
- package/dist/compiler/emit/base.js +2 -0
- package/dist/compiler/emit/borsh.js +63 -2
- package/dist/compiler/emit/calls.d.ts +14 -1
- package/dist/compiler/emit/calls.js +311 -21
- package/dist/compiler/emit/cpi-core.js +17 -5
- package/dist/compiler/emit/cpi.js +81 -27
- package/dist/compiler/emit/create.d.ts +7 -0
- package/dist/compiler/emit/create.js +27 -0
- package/dist/compiler/emit/expressions.d.ts +6 -0
- package/dist/compiler/emit/expressions.js +102 -6
- package/dist/compiler/emit/function.d.ts +7 -0
- package/dist/compiler/emit/function.js +164 -29
- package/dist/compiler/emit/program.js +27 -2
- package/dist/compiler/emit/shared.d.ts +15 -5
- package/dist/compiler/emit/shared.js +5 -0
- package/dist/compiler/emit/statements.d.ts +28 -1
- package/dist/compiler/emit/statements.js +270 -13
- package/dist/compiler/emit/storage.d.ts +10 -1
- package/dist/compiler/emit/storage.js +50 -11
- package/dist/compiler/hints.d.ts +5 -0
- package/dist/compiler/hints.js +88 -0
- package/dist/compiler/idl.d.ts +1 -1
- package/dist/compiler/idl.js +42 -11
- package/dist/compiler/index.d.ts +3 -0
- package/dist/compiler/index.js +6 -1
- package/dist/compiler/model.d.ts +47 -10
- package/dist/compiler/model.js +14 -4
- package/dist/testing.js +8 -0
- package/dist/tools/clientgen.js +10 -1
- package/dist/tools/portgen.d.ts +6 -0
- package/dist/tools/portgen.js +68 -17
- package/docs/architecture.md +5 -4
- package/docs/audit.md +31 -19
- package/docs/case-study-escrow.md +5 -5
- package/docs/case-study-jito-mev.md +9 -5
- package/docs/case-study-marginfi.md +797 -0
- package/docs/case-study-multisig.md +3 -3
- package/docs/case-study-raydium-amm.md +80 -0
- package/docs/case-study-raydium-clmm.md +44 -33
- package/docs/case-study-raydium-cp-swap.md +3 -3
- package/docs/cli.md +5 -5
- package/docs/client.md +1 -1
- package/docs/errors.md +13 -5
- package/docs/language.md +65 -32
- package/docs/migrate-from-anchor.md +4 -1
- package/docs/runtime.md +1 -1
- package/docs/security-model.md +2 -1
- package/docs/testing.md +34 -1
- package/docs/troubleshooting.md +6 -0
- package/lib/typesol.d.ts +118 -10
- package/package.json +9 -7
- package/runtime/typesol_rt.h +158 -17
- package/ts-plugin/index.js +24 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typesol/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.40.0",
|
|
4
4
|
"description": "Compile a TypeScript-syntax language directly to Solana SBF programs.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -63,13 +63,15 @@
|
|
|
63
63
|
"test": "vitest run",
|
|
64
64
|
"typesol": "node dist/cli.js",
|
|
65
65
|
"smoke": "node scripts/smoke.ts",
|
|
66
|
-
"fuzz": "FUZZ_RUNS=2000 FUZZ_STEPS=600 FUZZ_VERBOSE=1 vitest run test/verification/fuzz-arith.test.ts test/verification/fuzz-state.test.ts test/verification/fuzz-layout.test.ts test/verification/differential.test.ts --silent=false",
|
|
66
|
+
"fuzz": "FUZZ_RUNS=2000 FUZZ_STEPS=600 FUZZ_VERBOSE=1 vitest run test/verification/fuzz-arith.test.ts test/verification/fuzz-state.test.ts test/verification/fuzz-layout.test.ts test/verification/differential.test.ts showcase/test/fuzz-jito.test.ts showcase/test/fuzz-marginfi.test.ts --silent=false",
|
|
67
67
|
"build:twins": "cd test/anchor-twins && cargo-build-sbf",
|
|
68
68
|
"prepack": "npm run build",
|
|
69
69
|
"fetch:programs": "bash scripts/fetch-programs.sh",
|
|
70
|
-
"format": "prettier --write \"src/**/*.ts\" \"test/*.ts\" \"test/*/*.test.ts\" \"scripts/**/*.ts\" \"lib/**/*.ts\"",
|
|
71
|
-
"lint": "prettier --check \"src/**/*.ts\" \"test/*.ts\" \"test/*/*.test.ts\" \"scripts/**/*.ts\" \"lib/**/*.ts\" && pnpm typecheck",
|
|
72
|
-
"prepublishOnly": "pnpm lint && pnpm test"
|
|
70
|
+
"format": "prettier --write \"src/**/*.ts\" \"test/*.ts\" \"test/*/*.test.ts\" \"showcase/test/*.ts\" \"showcase/test/*/*.test.ts\" \"scripts/**/*.ts\" \"lib/**/*.ts\"",
|
|
71
|
+
"lint": "prettier --check \"src/**/*.ts\" \"test/*.ts\" \"test/*/*.test.ts\" \"showcase/test/*.ts\" \"showcase/test/*/*.test.ts\" \"scripts/**/*.ts\" \"lib/**/*.ts\" && pnpm typecheck",
|
|
72
|
+
"prepublishOnly": "pnpm lint && pnpm test",
|
|
73
|
+
"test:core": "vitest run --exclude 'showcase/**'",
|
|
74
|
+
"test:showcase": "vitest run showcase"
|
|
73
75
|
},
|
|
74
76
|
"engines": {
|
|
75
77
|
"node": ">=22.12"
|
|
@@ -115,10 +117,10 @@
|
|
|
115
117
|
"packageManager": "pnpm@11.9.0",
|
|
116
118
|
"repository": {
|
|
117
119
|
"type": "git",
|
|
118
|
-
"url": "git+https://github.com/typesol/typesol.git"
|
|
120
|
+
"url": "git+https://github.com/typesol-lang/typesol.git"
|
|
119
121
|
},
|
|
120
122
|
"bugs": {
|
|
121
|
-
"url": "https://github.com/typesol/typesol/issues"
|
|
123
|
+
"url": "https://github.com/typesol-lang/typesol/issues"
|
|
122
124
|
},
|
|
123
125
|
"homepage": "https://typesol.com",
|
|
124
126
|
"publishConfig": {
|
package/runtime/typesol_rt.h
CHANGED
|
@@ -38,6 +38,7 @@
|
|
|
38
38
|
#define TS_ERR_DUPLICATE_MUT 2040 /* ConstraintDuplicateMutableAccount */
|
|
39
39
|
#define TS_ERR_ALREADY_INITIALIZED 3000 /* AccountDiscriminatorAlreadySet */
|
|
40
40
|
#define TS_ERR_INIT_PAYER 4101 /* TryingToInitPayerAsProgramAccount */
|
|
41
|
+
#define TS_ERR_DISC_NOT_FOUND 3001 /* AccountDiscriminatorNotFound */
|
|
41
42
|
#define TS_ERR_DISCRIMINATOR 3002 /* AccountDiscriminatorMismatch */
|
|
42
43
|
#define TS_ERR_ACCOUNT_TOO_SMALL 3003 /* AccountDidNotDeserialize */
|
|
43
44
|
#define TS_ERR_DID_NOT_SERIALIZE 3004 /* AccountDidNotSerialize */
|
|
@@ -70,9 +71,22 @@
|
|
|
70
71
|
/* Every failure logs through this one sol_log_ call: each syscall call site costs a 16-byte relocation in the binary. */
|
|
71
72
|
static __attribute__((noinline)) uint64_t ts_fail(uint64_t code, const char *msg, uint64_t len) {
|
|
72
73
|
sol_log_(msg, len);
|
|
74
|
+
#ifdef TS_PANIC_ON_OVERFLOW
|
|
75
|
+
/* PANIC_ON_OVERFLOW: arithmetic fails as a Rust `checked_*().unwrap()` does, with a panic (ProgramFailedToComplete). */
|
|
76
|
+
if (code == TS_ERR_OVERFLOW || code == TS_ERR_DIV_ZERO) sol_panic_("", 0, 0, 0);
|
|
77
|
+
#endif
|
|
78
|
+
#ifdef TS_CUSTOM_ZERO
|
|
79
|
+
/* An error with code 0 (a native program's first error) is returned as solana_program returns
|
|
80
|
+
* ProgramError::Custom(0): the built-in CUSTOM_ZERO, since 0 itself means success. */
|
|
81
|
+
if (code == 0) return (uint64_t)1 << 32;
|
|
82
|
+
#endif
|
|
73
83
|
return code;
|
|
74
84
|
}
|
|
75
85
|
#define TS_FAIL(code, msg) return ts_fail((code), (msg), sol_strlen(msg))
|
|
86
|
+
/* require(condition, code) with a code computed at run time: 0 would read as success, so it fails as an overflow. */
|
|
87
|
+
static __attribute__((noinline, unused)) uint64_t ts_fail_code(uint32_t code, const char *msg) {
|
|
88
|
+
return ts_fail(code ? code : TS_ERR_OVERFLOW, msg, sol_strlen(msg));
|
|
89
|
+
}
|
|
76
90
|
|
|
77
91
|
#define TS_TRY(expr) \
|
|
78
92
|
do { \
|
|
@@ -355,17 +369,25 @@ static __attribute__((noinline)) void ts_divmod32(const uint32_t *u, int mmax, T
|
|
|
355
369
|
}
|
|
356
370
|
if (rem) *rem = ts_u256_from32(r);
|
|
357
371
|
}
|
|
372
|
+
/* Two 64-bit values: one 128-bit product. Otherwise schoolbook over the limbs that are not zero (two 88-bit values
|
|
373
|
+
take 9 products, not 64). */
|
|
358
374
|
static inline bool ts_u256_mul(TsU256 a, TsU256 b, TsU256 *out) {
|
|
359
|
-
if (
|
|
375
|
+
if (((a.w[1] | a.w[2] | a.w[3]) | (b.w[1] | b.w[2] | b.w[3])) == 0) {
|
|
360
376
|
*out = ts_u256_from_u128((unsigned __int128)a.w[0] * b.w[0]);
|
|
361
377
|
return false;
|
|
362
378
|
}
|
|
363
379
|
uint32_t x[8], y[8], r[16];
|
|
364
380
|
ts_u256_to32(a, x);
|
|
365
381
|
ts_u256_to32(b, y);
|
|
366
|
-
|
|
382
|
+
int n = ts_len32(x, 8), m = ts_len32(y, 8);
|
|
383
|
+
if (n == 0 || m == 0) {
|
|
384
|
+
*out = ts_u256_lit(0, 0, 0, 0);
|
|
385
|
+
return false;
|
|
386
|
+
}
|
|
387
|
+
ts_mul32(x, n, y, m, r);
|
|
388
|
+
for (int i = n + m; i < 16; i++) r[i] = 0;
|
|
367
389
|
*out = ts_u256_from32(r);
|
|
368
|
-
for (int i = 8; i <
|
|
390
|
+
for (int i = 8; i < n + m; i++)
|
|
369
391
|
if (r[i]) return true;
|
|
370
392
|
return false;
|
|
371
393
|
}
|
|
@@ -651,6 +673,28 @@ static inline uint64_t ts_create_pda_address(const SolSignerSeed *seeds, int n,
|
|
|
651
673
|
return SUCCESS;
|
|
652
674
|
}
|
|
653
675
|
|
|
676
|
+
/* msg!("{prefix}{base64(bytes)}") with standard padding, as Raydium's ray_log writes it (at most 300 bytes in). */
|
|
677
|
+
static __attribute__((noinline, unused)) void ts_log_base64(const char *prefix, uint64_t plen, const uint8_t *data, uint64_t len) {
|
|
678
|
+
static const char tbl[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
679
|
+
char out[64 + 4 * ((300 + 2) / 3)];
|
|
680
|
+
if (plen > 64 || len > 300) return;
|
|
681
|
+
uint64_t o = 0;
|
|
682
|
+
for (uint64_t i = 0; i < plen; i++) out[o++] = prefix[i];
|
|
683
|
+
uint64_t i = 0;
|
|
684
|
+
for (; i + 3 <= len; i += 3) {
|
|
685
|
+
uint32_t v = ((uint32_t)data[i] << 16) | ((uint32_t)data[i + 1] << 8) | data[i + 2];
|
|
686
|
+
out[o++] = tbl[(v >> 18) & 63]; out[o++] = tbl[(v >> 12) & 63]; out[o++] = tbl[(v >> 6) & 63]; out[o++] = tbl[v & 63];
|
|
687
|
+
}
|
|
688
|
+
if (len - i == 1) {
|
|
689
|
+
uint32_t v = (uint32_t)data[i] << 16;
|
|
690
|
+
out[o++] = tbl[(v >> 18) & 63]; out[o++] = tbl[(v >> 12) & 63]; out[o++] = '='; out[o++] = '=';
|
|
691
|
+
} else if (len - i == 2) {
|
|
692
|
+
uint32_t v = ((uint32_t)data[i] << 16) | ((uint32_t)data[i + 1] << 8);
|
|
693
|
+
out[o++] = tbl[(v >> 18) & 63]; out[o++] = tbl[(v >> 12) & 63]; out[o++] = tbl[(v >> 6) & 63]; out[o++] = '=';
|
|
694
|
+
}
|
|
695
|
+
sol_log_(out, o);
|
|
696
|
+
}
|
|
697
|
+
|
|
654
698
|
/* sol_try_find_program_address: the canonical (highest) bump. */
|
|
655
699
|
static inline uint64_t ts_find_pda(const SolSignerSeed *seeds, int n, const SolPubkey *program, SolPubkey *out, uint8_t *bump_out) {
|
|
656
700
|
if (!ts_pda_seeds_ok(seeds, n, TS_MAX_PDA_SEEDS - 1)) return 1; /* not SUCCESS: the PDA syscalls fail the same inputs */
|
|
@@ -711,6 +755,18 @@ static inline uint64_t ts_check_program_account(const TsAccount *a, const SolPub
|
|
|
711
755
|
return SUCCESS;
|
|
712
756
|
}
|
|
713
757
|
|
|
758
|
+
/* A zero-copy (`@serialization bytemuck`) account, as Anchor's AccountLoader checks one: owner, then a discriminator to
|
|
759
|
+
read and match, with no "not initialized" case. Anchor's load() then panics on data shorter than the struct; this
|
|
760
|
+
refuses it (3003). */
|
|
761
|
+
static inline uint64_t ts_check_loader_account(const TsAccount *a, const SolPubkey *program_id, const uint8_t disc[8],
|
|
762
|
+
uint64_t size) {
|
|
763
|
+
if (!ts_key_eq(&a->owner, program_id)) TS_FAIL(TS_ERR_WRONG_OWNER, "TypeSOL: account not owned by this program");
|
|
764
|
+
if (a->data_len < 8) TS_FAIL(TS_ERR_DISC_NOT_FOUND, "TypeSOL: account has no discriminator");
|
|
765
|
+
if (!ts_mem_eq(a->data, disc, 8)) TS_FAIL(TS_ERR_DISCRIMINATOR, "TypeSOL: account discriminator mismatch");
|
|
766
|
+
if (a->data_len < 8 + size) TS_FAIL(TS_ERR_ACCOUNT_TOO_SMALL, "TypeSOL: account data too small");
|
|
767
|
+
return SUCCESS;
|
|
768
|
+
}
|
|
769
|
+
|
|
714
770
|
/* An account ending in Vec<T>: data must be exactly fixed part + len * element size. */
|
|
715
771
|
static inline uint64_t ts_check_vec_len(const TsAccount *a, uint64_t fixed, uint32_t len, uint64_t elem) {
|
|
716
772
|
if (a->data_len != 8 + fixed + (uint64_t)len * elem)
|
|
@@ -756,6 +812,44 @@ static inline uint64_t ts_claim_mut_at(TsParams *params, uint64_t at) {
|
|
|
756
812
|
return SUCCESS;
|
|
757
813
|
}
|
|
758
814
|
|
|
815
|
+
/* A loadAccountMut inside a loop whose view cannot outlive the iteration: the claim's index is kept in `*slot`, so the
|
|
816
|
+
iteration's end can drop it (ts_release_mut) and the next iteration may load the same account again.
|
|
817
|
+
A remaining account may be passed more than once here (a route through one pool twice): its slots share one record,
|
|
818
|
+
and the claim bit is the last slot's, so two live views of it are still refused. It may not also be one of the
|
|
819
|
+
instruction's own accounts (`declared`: their count), whose views last the whole instruction. */
|
|
820
|
+
static inline uint64_t ts_claim_mut_rec(TsParams *params, const TsAccount *a, uint64_t *slot, uint64_t declared) {
|
|
821
|
+
uint64_t seen = 0, at = 0, first = (uint64_t)-1;
|
|
822
|
+
for (uint64_t i = 0; i < params->ka_num; i++)
|
|
823
|
+
if (params->ka[i] == a) {
|
|
824
|
+
seen++, at = i;
|
|
825
|
+
if (first == (uint64_t)-1) first = i;
|
|
826
|
+
}
|
|
827
|
+
if (seen > 1 && first < declared) TS_FAIL(TS_ERR_DUPLICATE_MUT, "TypeSOL: account passed more than once");
|
|
828
|
+
#if TS_MAX_ACCOUNTS > 64
|
|
829
|
+
if (params->loaded_mut[at >> 6] >> (at & 63) & 1) TS_FAIL(TS_ERR_DUPLICATE_MUT, "TypeSOL: account already loaded with loadAccountMut");
|
|
830
|
+
params->loaded_mut[at >> 6] |= (uint64_t)1 << (at & 63);
|
|
831
|
+
#else
|
|
832
|
+
if (params->loaded_mut >> at & 1) TS_FAIL(TS_ERR_DUPLICATE_MUT, "TypeSOL: account already loaded with loadAccountMut");
|
|
833
|
+
params->loaded_mut |= (uint64_t)1 << at;
|
|
834
|
+
#endif
|
|
835
|
+
*slot = at;
|
|
836
|
+
return SUCCESS;
|
|
837
|
+
}
|
|
838
|
+
static inline uint64_t ts_claim_mut_at_rec(TsParams *params, uint64_t at, uint64_t *slot) {
|
|
839
|
+
TS_TRY(ts_claim_mut_at(params, at));
|
|
840
|
+
*slot = at;
|
|
841
|
+
return SUCCESS;
|
|
842
|
+
}
|
|
843
|
+
static inline void ts_release_mut(TsParams *params, uint64_t *slot) {
|
|
844
|
+
if (*slot == (uint64_t)-1) return;
|
|
845
|
+
#if TS_MAX_ACCOUNTS > 64
|
|
846
|
+
params->loaded_mut[*slot >> 6] &= ~((uint64_t)1 << (*slot & 63));
|
|
847
|
+
#else
|
|
848
|
+
params->loaded_mut &= ~((uint64_t)1 << *slot);
|
|
849
|
+
#endif
|
|
850
|
+
*slot = (uint64_t)-1;
|
|
851
|
+
}
|
|
852
|
+
|
|
759
853
|
static inline uint64_t ts_check_address(const TsAccount *a, const SolPubkey *expected) {
|
|
760
854
|
if (!ts_key_eq(&a->key, expected)) TS_FAIL(TS_ERR_WRONG_PROGRAM, "TypeSOL: unexpected program address");
|
|
761
855
|
return SUCCESS;
|
|
@@ -778,13 +872,19 @@ static inline bool ts_is_token_program(const SolPubkey *k) {
|
|
|
778
872
|
}
|
|
779
873
|
/* A Token-2022 Multisig is 355 bytes and its signer bytes are attacker-chosen: never accept that length. */
|
|
780
874
|
#define TS_MULTISIG_LEN 355
|
|
875
|
+
/* A token account or mint not owned by a token program. As Anchor's (Interface)Account: an empty system account (never
|
|
876
|
+
created) is "not initialized", anything else a wrong owner. Out of line: the checks that pass stay as small as before. */
|
|
877
|
+
static __attribute__((noinline)) uint64_t ts_token_owner_fail(const TsAccount *a) {
|
|
878
|
+
if (a->lamports == 0 && ts_key_eq(&a->owner, &TS_SYSTEM_PROGRAM_ID)) TS_FAIL(TS_ERR_NOT_INITIALIZED, "TypeSOL: account is not initialized");
|
|
879
|
+
TS_FAIL(TS_ERR_WRONG_OWNER, "TypeSOL: not owned by a token program");
|
|
880
|
+
}
|
|
781
881
|
static inline uint64_t ts_check_token_account(const TsAccount *a) {
|
|
782
882
|
bool ok = false;
|
|
783
883
|
if (ts_key_eq(&a->owner, &TS_TOKEN_PROGRAM_ID)) ok = a->data_len == TS_TOKEN_ACCOUNT_LEN;
|
|
784
884
|
else if (ts_key_eq(&a->owner, &TS_TOKEN_2022_PROGRAM_ID))
|
|
785
885
|
ok = a->data_len == TS_TOKEN_ACCOUNT_LEN ||
|
|
786
886
|
(a->data_len > TS_TOKEN_ACCOUNT_LEN && a->data_len != TS_MULTISIG_LEN && a->data[TS_TOKEN_ACCOUNT_LEN] == 2);
|
|
787
|
-
else
|
|
887
|
+
else return ts_token_owner_fail(a);
|
|
788
888
|
if (!ok || a->data[108] == 0) TS_FAIL(TS_ERR_TOKEN_ACCOUNT, "TypeSOL: not an initialized token account");
|
|
789
889
|
return SUCCESS;
|
|
790
890
|
}
|
|
@@ -795,7 +895,7 @@ static inline uint64_t ts_check_mint(const TsAccount *a) {
|
|
|
795
895
|
else if (ts_key_eq(&a->owner, &TS_TOKEN_2022_PROGRAM_ID))
|
|
796
896
|
ok = a->data_len == TS_MINT_LEN ||
|
|
797
897
|
(a->data_len > TS_TOKEN_ACCOUNT_LEN && a->data_len != TS_MULTISIG_LEN && a->data[TS_TOKEN_ACCOUNT_LEN] == 1);
|
|
798
|
-
else
|
|
898
|
+
else return ts_token_owner_fail(a);
|
|
799
899
|
if (!ok || a->data[45] != 1) TS_FAIL(TS_ERR_MINT, "TypeSOL: not an initialized mint");
|
|
800
900
|
return SUCCESS;
|
|
801
901
|
}
|
|
@@ -998,10 +1098,13 @@ static inline uint64_t ts_init_account(TsAccount *a, const SolPubkey *program_id
|
|
|
998
1098
|
return SUCCESS;
|
|
999
1099
|
}
|
|
1000
1100
|
|
|
1001
|
-
/* Pubkey order: Rust's Ord on [u8; 32], lexicographic.
|
|
1101
|
+
/* Pubkey order: Rust's Ord on [u8; 32], lexicographic. Eight bytes at a time: a word read big-endian orders as its
|
|
1102
|
+
bytes do, so the first word that differs decides. */
|
|
1002
1103
|
static inline int64_t ts_key_cmp(const SolPubkey *a, const SolPubkey *b) {
|
|
1003
|
-
for (int i = 0; i < 32; i
|
|
1004
|
-
|
|
1104
|
+
for (int i = 0; i < 32; i += 8) {
|
|
1105
|
+
uint64_t x = ts_ld64(a->x + i), y = ts_ld64(b->x + i);
|
|
1106
|
+
if (x != y) return __builtin_bswap64(x) < __builtin_bswap64(y) ? -1 : 1;
|
|
1107
|
+
}
|
|
1005
1108
|
return 0;
|
|
1006
1109
|
}
|
|
1007
1110
|
|
|
@@ -1034,12 +1137,12 @@ static inline uint64_t ts_system_transfer(TsParams *params, TsAccount *from, TsA
|
|
|
1034
1137
|
}
|
|
1035
1138
|
|
|
1036
1139
|
/*
|
|
1037
|
-
* Create `acct` as a PDA of this program. `seeds` must have room
|
|
1038
|
-
* entry: the bump is appended there.
|
|
1140
|
+
* Create `acct` as a PDA of this program, owned by `owner` (Anchor's `init` with `owner = …`). `seeds` must have room
|
|
1141
|
+
* for one extra entry: the bump is appended there.
|
|
1039
1142
|
*/
|
|
1040
|
-
static inline uint64_t
|
|
1041
|
-
|
|
1042
|
-
|
|
1143
|
+
static inline uint64_t ts_create_pda_owned(TsParams *params, TsAccount *payer, TsAccount *acct,
|
|
1144
|
+
SolSignerSeed *seeds, int n, const SolPubkey *owner, uint64_t space,
|
|
1145
|
+
uint8_t *bump_out) {
|
|
1043
1146
|
SolPubkey derived;
|
|
1044
1147
|
uint8_t bump;
|
|
1045
1148
|
if (ts_find_pda(seeds, n, params->program_id, &derived, &bump) != SUCCESS ||
|
|
@@ -1059,7 +1162,7 @@ static inline uint64_t ts_create_pda(TsParams *params, TsAccount *payer, TsAccou
|
|
|
1059
1162
|
ts_memcpy(data, &tag, 4);
|
|
1060
1163
|
ts_memcpy(data + 4, &lamports, 8);
|
|
1061
1164
|
ts_memcpy(data + 12, &space, 8);
|
|
1062
|
-
ts_memcpy(data + 20,
|
|
1165
|
+
ts_memcpy(data + 20, owner, 32);
|
|
1063
1166
|
SolAccountMeta metas[2] = {{&payer->key, true, true}, {&acct->key, true, true}};
|
|
1064
1167
|
SolInstruction ix = {(SolPubkey *)&TS_SYSTEM_PROGRAM_ID, metas, 2, data, sizeof(data)};
|
|
1065
1168
|
TS_TRY(ts_invoke(params, &ix, &signer, 1));
|
|
@@ -1077,17 +1180,25 @@ static inline uint64_t ts_create_pda(TsParams *params, TsAccount *payer, TsAccou
|
|
|
1077
1180
|
uint8_t assign[36];
|
|
1078
1181
|
uint32_t assign_tag = 1; /* Assign */
|
|
1079
1182
|
ts_memcpy(assign, &assign_tag, 4);
|
|
1080
|
-
ts_memcpy(assign + 4,
|
|
1183
|
+
ts_memcpy(assign + 4, owner, 32);
|
|
1081
1184
|
SolInstruction assign_ix = {(SolPubkey *)&TS_SYSTEM_PROGRAM_ID, one, 1, assign, sizeof(assign)};
|
|
1082
1185
|
TS_TRY(ts_invoke(params, &assign_ix, &signer, 1));
|
|
1083
1186
|
}
|
|
1084
1187
|
|
|
1085
1188
|
acct->data_len = space;
|
|
1086
|
-
ts_memcpy(acct->data, disc, 8);
|
|
1087
1189
|
*bump_out = bump;
|
|
1088
1190
|
return SUCCESS;
|
|
1089
1191
|
}
|
|
1090
1192
|
|
|
1193
|
+
/* Create `acct` as a PDA of this program, owned by it; writes the discriminator on success. */
|
|
1194
|
+
static inline uint64_t ts_create_pda(TsParams *params, TsAccount *payer, TsAccount *acct,
|
|
1195
|
+
SolSignerSeed *seeds, int n, const uint8_t disc[8], uint64_t space,
|
|
1196
|
+
uint8_t *bump_out) {
|
|
1197
|
+
TS_TRY(ts_create_pda_owned(params, payer, acct, seeds, n, params->program_id, space, bump_out));
|
|
1198
|
+
ts_memcpy(acct->data, disc, 8);
|
|
1199
|
+
return SUCCESS;
|
|
1200
|
+
}
|
|
1201
|
+
|
|
1091
1202
|
/*
|
|
1092
1203
|
* Create `acct` with `space` bytes owned by `owner` (a token program), funded by `payer`: a PDA of this program when
|
|
1093
1204
|
* `n` > 0 (seeds without the bump; a pre-funded address is topped up, allocated and assigned), otherwise a new keypair
|
|
@@ -1239,6 +1350,15 @@ static inline uint64_t ts_resize(TsParams *params, TsAccount *payer, TsAccount *
|
|
|
1239
1350
|
return SUCCESS;
|
|
1240
1351
|
}
|
|
1241
1352
|
|
|
1353
|
+
/* resizeAccount(): the data length only (AccountInfo::resize), new bytes zeroed; no rent moves. */
|
|
1354
|
+
static inline uint64_t ts_resize_raw(TsAccount *acct, uint64_t new_len, uint64_t entry_len) {
|
|
1355
|
+
if (new_len > entry_len + MAX_PERMITTED_DATA_INCREASE)
|
|
1356
|
+
TS_FAIL(TS_ERR_REALLOC, "TypeSOL: account would grow more than 10 KiB in one instruction");
|
|
1357
|
+
if (new_len > acct->data_len) ts_memzero(acct->data + acct->data_len, new_len - acct->data_len);
|
|
1358
|
+
acct->data_len = new_len;
|
|
1359
|
+
return SUCCESS;
|
|
1360
|
+
}
|
|
1361
|
+
|
|
1242
1362
|
/*
|
|
1243
1363
|
* InitIfNeeded: create the PDA like ts_create_pda, or accept it when it already exists as this program's
|
|
1244
1364
|
* account of the right type (owner, discriminator, size). The PDA is re-derived either way.
|
|
@@ -1511,7 +1631,12 @@ static const SolPubkey TS_INSTRUCTIONS_SYSVAR_ID = {{6,167,213,23,24,123,209,102
|
|
|
1511
1631
|
* difference as uint8_t, so any difference is positive.)
|
|
1512
1632
|
*/
|
|
1513
1633
|
static inline int ts_bytes_cmp(const uint8_t *a, const uint8_t *b, uint64_t n) {
|
|
1514
|
-
|
|
1634
|
+
uint64_t i = 0;
|
|
1635
|
+
for (; i + 8 <= n; i += 8) {
|
|
1636
|
+
uint64_t x = ts_ld64(a + i), y = ts_ld64(b + i);
|
|
1637
|
+
if (x != y) return __builtin_bswap64(x) < __builtin_bswap64(y) ? -1 : 1;
|
|
1638
|
+
}
|
|
1639
|
+
for (; i < n; i++)
|
|
1515
1640
|
if (a[i] != b[i]) return a[i] < b[i] ? -1 : 1;
|
|
1516
1641
|
return 0;
|
|
1517
1642
|
}
|
|
@@ -1931,6 +2056,22 @@ static inline unsigned __int128 ts_isqrt_u128(unsigned __int128 x) {
|
|
|
1931
2056
|
}
|
|
1932
2057
|
}
|
|
1933
2058
|
|
|
2059
|
+
/* floor(sqrt(x)) of a u256 (the `uint` crate's U256::integer_sqrt): Newton's method from a power of two above it. */
|
|
2060
|
+
static inline TsU256 ts_isqrt_u256(TsU256 x) {
|
|
2061
|
+
int top = 3;
|
|
2062
|
+
while (top > 0 && x.w[top] == 0) top--;
|
|
2063
|
+
if (top == 0 && x.w[0] < 2) return x;
|
|
2064
|
+
int bits = 64 * top + (64 - __builtin_clzll(x.w[top]));
|
|
2065
|
+
TsU256 r = ts_u256_shl(ts_u256_lit(1, 0, 0, 0), (uint64_t)((bits + 1) / 2));
|
|
2066
|
+
while (1) {
|
|
2067
|
+
TsU256 q = ts_u256_divmod(x, r, true), s;
|
|
2068
|
+
ts_u256_add(r, q, &s); /* r + x / r stays below 2^256: r <= 2^128 and x / r < 2^256 / r */
|
|
2069
|
+
TsU256 n = ts_u256_shr(s, 1);
|
|
2070
|
+
if (ts_u256_cmp(n, r) >= 0) return r;
|
|
2071
|
+
r = n;
|
|
2072
|
+
}
|
|
2073
|
+
}
|
|
2074
|
+
|
|
1934
2075
|
/* Decimal text of a 128-bit integer on the heap. */
|
|
1935
2076
|
static inline uint64_t ts_str_u128(unsigned __int128 v, bool negative, TS_Str *out) {
|
|
1936
2077
|
char tmp[40];
|
package/ts-plugin/index.js
CHANGED
|
@@ -99,10 +99,33 @@ function init({ typescript: ts }) {
|
|
|
99
99
|
return found;
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
+
/**
|
|
103
|
+
* TypeScript's errors in a TypeSOL project's files, each with the hint the CLI prints under it (TypeScript's own
|
|
104
|
+
* advice, such as installing @types/node, points the wrong way in a program). Needs the in-process compiler.
|
|
105
|
+
*/
|
|
106
|
+
function withHints(fileName, diagnostics) {
|
|
107
|
+
if (!compiler || !compiler.typescriptHint || !entriesNear(path.dirname(path.resolve(fileName))).size) return diagnostics;
|
|
108
|
+
return diagnostics.map((d) => {
|
|
109
|
+
if (d.category !== ts.DiagnosticCategory.Error) return d;
|
|
110
|
+
let hint;
|
|
111
|
+
try {
|
|
112
|
+
hint = compiler.typescriptHint(d);
|
|
113
|
+
} catch {
|
|
114
|
+
return d;
|
|
115
|
+
}
|
|
116
|
+
if (!hint) return d;
|
|
117
|
+
const text = `hint: ${hint}`;
|
|
118
|
+
const messageText =
|
|
119
|
+
typeof d.messageText === "string"
|
|
120
|
+
? `${d.messageText}\n${text}`
|
|
121
|
+
: { ...d.messageText, next: [...(d.messageText.next || []), { messageText: text, category: d.category, code: d.code, next: undefined }] };
|
|
122
|
+
return { ...d, messageText };
|
|
123
|
+
});
|
|
124
|
+
}
|
|
102
125
|
const proxy = Object.create(null);
|
|
103
126
|
for (const k of Object.keys(ls)) proxy[k] = (...args) => ls[k](...args);
|
|
104
127
|
proxy.getSemanticDiagnostics = (fileName) => {
|
|
105
|
-
const prior = ls.getSemanticDiagnostics(fileName);
|
|
128
|
+
const prior = withHints(fileName, ls.getSemanticDiagnostics(fileName));
|
|
106
129
|
if (prior.some((d) => d.category === ts.DiagnosticCategory.Error)) return prior; // TypeScript's errors first
|
|
107
130
|
const target = path.resolve(fileName);
|
|
108
131
|
const entries = entriesNear(path.dirname(target));
|