@typesol/cli 0.30.0 → 0.32.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 +52 -0
- package/README.md +11 -7
- package/dist/cli.js +50 -5
- package/dist/compiler/analyze.js +14 -5
- package/dist/compiler/emit/base.d.ts +1 -0
- package/dist/compiler/emit/calls.d.ts +2 -0
- package/dist/compiler/emit/calls.js +125 -11
- package/dist/compiler/emit/cpi-core.d.ts +8 -0
- package/dist/compiler/emit/cpi-core.js +15 -0
- package/dist/compiler/emit/cpi.js +10 -4
- package/dist/compiler/emit/create.d.ts +2 -0
- package/dist/compiler/emit/create.js +56 -13
- package/dist/compiler/emit/expressions.js +6 -2
- package/dist/compiler/emit/statements.js +15 -0
- package/dist/compiler/emit/token.js +43 -0
- package/dist/templates.js +13 -36
- package/dist/testing.d.ts +30 -0
- package/dist/testing.js +64 -2
- package/dist/workspace.d.ts +16 -1
- package/dist/workspace.js +49 -5
- package/docs/audit.md +1 -1
- package/docs/case-study-escrow.md +3 -3
- package/docs/case-study-multisig.md +3 -3
- package/docs/case-study-raydium-cp-swap.md +152 -0
- package/docs/cli.md +5 -3
- package/docs/errors.md +3 -1
- package/docs/language.md +13 -6
- package/docs/migrate-from-anchor.md +3 -0
- package/docs/test-your-program.md +12 -3
- package/docs/testing.md +1 -1
- package/lib/typesol.d.ts +43 -5
- package/package.json +1 -1
- package/runtime/typesol_rt.h +121 -7
package/runtime/typesol_rt.h
CHANGED
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
#define TS_ERR_MINT_TOKEN_PROGRAM 2022 /* ConstraintMintTokenProgram */
|
|
38
38
|
#define TS_ERR_DUPLICATE_MUT 2040 /* ConstraintDuplicateMutableAccount */
|
|
39
39
|
#define TS_ERR_ALREADY_INITIALIZED 3000 /* AccountDiscriminatorAlreadySet */
|
|
40
|
+
#define TS_ERR_INIT_PAYER 4101 /* TryingToInitPayerAsProgramAccount */
|
|
40
41
|
#define TS_ERR_DISCRIMINATOR 3002 /* AccountDiscriminatorMismatch */
|
|
41
42
|
#define TS_ERR_ACCOUNT_TOO_SMALL 3003 /* AccountDidNotDeserialize */
|
|
42
43
|
#define TS_ERR_DID_NOT_SERIALIZE 3004 /* AccountDidNotSerialize */
|
|
@@ -160,6 +161,31 @@ static inline __attribute__((always_inline)) bool ts_mul_overflow_u64(uint64_t a
|
|
|
160
161
|
return __builtin_mul_overflow(a, b, out);
|
|
161
162
|
}
|
|
162
163
|
|
|
164
|
+
/*
|
|
165
|
+
* u128 arithmetic: the 128-bit library routines (__muloti4, __udivti3) cost hundreds of CU on SBF. Values that fit in
|
|
166
|
+
* 64 bits (most amounts and rates) take the native 64-bit instructions instead; the results are the same.
|
|
167
|
+
*/
|
|
168
|
+
static inline __attribute__((always_inline)) bool ts_mul_overflow_u128(unsigned __int128 a, unsigned __int128 b, unsigned __int128 *out) {
|
|
169
|
+
if (((a | b) >> 32) == 0) {
|
|
170
|
+
const uint64_t product = (uint64_t)a * (uint64_t)b; /* both below 2^32: the product fits in 64 bits */
|
|
171
|
+
*out = product;
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
if (((a | b) >> 64) == 0) {
|
|
175
|
+
*out = (unsigned __int128)(uint64_t)a * (uint64_t)b; /* below 2^128: cannot overflow */
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
return __builtin_mul_overflow(a, b, out);
|
|
179
|
+
}
|
|
180
|
+
static inline __attribute__((always_inline)) unsigned __int128 ts_div_u128(unsigned __int128 a, unsigned __int128 b) {
|
|
181
|
+
if (((a | b) >> 64) == 0) return (uint64_t)a / (uint64_t)b;
|
|
182
|
+
return a / b;
|
|
183
|
+
}
|
|
184
|
+
static inline __attribute__((always_inline)) unsigned __int128 ts_rem_u128(unsigned __int128 a, unsigned __int128 b) {
|
|
185
|
+
if (((a | b) >> 64) == 0) return (uint64_t)a % (uint64_t)b;
|
|
186
|
+
return a % b;
|
|
187
|
+
}
|
|
188
|
+
|
|
163
189
|
/* ---- Well-known program ids --------------------------------------------- */
|
|
164
190
|
static const SolPubkey TS_SYSTEM_PROGRAM_ID = {{0}};
|
|
165
191
|
/* TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA */
|
|
@@ -687,6 +713,12 @@ static inline uint64_t ts_event_dispatch(TsParams *params) {
|
|
|
687
713
|
return ts_check_event_authority(params->ka[0], params->program_id, &bump);
|
|
688
714
|
}
|
|
689
715
|
|
|
716
|
+
/* The Clock sysvar into `clock` (an instruction keeps it: the Clock does not change within a transaction). */
|
|
717
|
+
static inline uint64_t ts_read_clock(uint64_t clock[5]) {
|
|
718
|
+
if (sol_get_clock_sysvar(clock) != SUCCESS) TS_FAIL(TS_ERR_SYSVAR, "TypeSOL: clock sysvar unavailable");
|
|
719
|
+
return SUCCESS;
|
|
720
|
+
}
|
|
721
|
+
|
|
690
722
|
static inline uint64_t ts_now(int64_t *out) {
|
|
691
723
|
/* Clock { slot, epoch_start_timestamp, epoch, leader_schedule_epoch, unix_timestamp } */
|
|
692
724
|
uint64_t clock[5] = {0};
|
|
@@ -716,6 +748,23 @@ static inline uint64_t ts_init_zeroed(TsAccount *a, const uint8_t disc[8]) {
|
|
|
716
748
|
return SUCCESS;
|
|
717
749
|
}
|
|
718
750
|
|
|
751
|
+
/* initAccount<T>: Anchor's AccountLoader::load_init on an account the body created. */
|
|
752
|
+
static inline uint64_t ts_init_account(TsAccount *a, const SolPubkey *program_id, const uint8_t disc[8], uint64_t size) {
|
|
753
|
+
if (!ts_key_eq(&a->owner, program_id)) TS_FAIL(TS_ERR_WRONG_OWNER, "TypeSOL: account not owned by this program");
|
|
754
|
+
if (!a->is_writable) TS_FAIL(TS_ERR_NOT_WRITABLE, "TypeSOL: account is not writable");
|
|
755
|
+
if (a->data_len < 8 + size) TS_FAIL(TS_ERR_ACCOUNT_TOO_SMALL, "TypeSOL: account data too small");
|
|
756
|
+
if (ts_ld64(a->data) != 0) TS_FAIL(TS_ERR_ALREADY_INITIALIZED, "TypeSOL: account already initialized");
|
|
757
|
+
ts_memcpy(a->data, disc, 8);
|
|
758
|
+
return SUCCESS;
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
/* Pubkey order: Rust's Ord on [u8; 32], lexicographic. */
|
|
762
|
+
static inline int64_t ts_key_cmp(const SolPubkey *a, const SolPubkey *b) {
|
|
763
|
+
for (int i = 0; i < 32; i++)
|
|
764
|
+
if (a->x[i] != b->x[i]) return a->x[i] < b->x[i] ? -1 : 1;
|
|
765
|
+
return 0;
|
|
766
|
+
}
|
|
767
|
+
|
|
719
768
|
/* ---- PDA ----------------------------------------------------------------- */
|
|
720
769
|
static inline uint64_t ts_require_pda(const TsAccount *a, const SolSignerSeed *seeds, int n,
|
|
721
770
|
const SolPubkey *program_id) {
|
|
@@ -726,15 +775,22 @@ static inline uint64_t ts_require_pda(const TsAccount *a, const SolSignerSeed *s
|
|
|
726
775
|
}
|
|
727
776
|
|
|
728
777
|
/* ---- System program CPIs ------------------------------------------------- */
|
|
729
|
-
static inline uint64_t
|
|
730
|
-
|
|
778
|
+
static inline uint64_t ts_system_transfer_signed(TsParams *params, TsAccount *from, TsAccount *to,
|
|
779
|
+
uint64_t lamports, const SolSignerSeed *seeds, int nseeds) {
|
|
731
780
|
uint8_t data[12];
|
|
732
781
|
uint32_t tag = 2;
|
|
733
782
|
ts_memcpy(data, &tag, 4);
|
|
734
783
|
ts_memcpy(data + 4, &lamports, 8);
|
|
735
784
|
SolAccountMeta metas[2] = {{&from->key, true, true}, {&to->key, true, false}};
|
|
736
785
|
SolInstruction ix = {(SolPubkey *)&TS_SYSTEM_PROGRAM_ID, metas, 2, data, sizeof(data)};
|
|
737
|
-
return ts_invoke(params, &ix, NULL, 0);
|
|
786
|
+
if (nseeds == 0) return ts_invoke(params, &ix, NULL, 0);
|
|
787
|
+
SolSignerSeeds signer = {seeds, (uint64_t)nseeds};
|
|
788
|
+
return ts_invoke(params, &ix, &signer, 1);
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
static inline uint64_t ts_system_transfer(TsParams *params, TsAccount *from, TsAccount *to,
|
|
792
|
+
uint64_t lamports) {
|
|
793
|
+
return ts_system_transfer_signed(params, from, to, lamports, NULL, 0);
|
|
738
794
|
}
|
|
739
795
|
|
|
740
796
|
/*
|
|
@@ -749,8 +805,8 @@ static inline uint64_t ts_create_pda(TsParams *params, TsAccount *payer, TsAccou
|
|
|
749
805
|
if (ts_find_pda(seeds, n, params->program_id, &derived, &bump) != SUCCESS ||
|
|
750
806
|
!ts_key_eq(&derived, &acct->key))
|
|
751
807
|
TS_FAIL(TS_ERR_SEEDS, "TypeSOL: PDA seeds do not match account");
|
|
752
|
-
|
|
753
|
-
|
|
808
|
+
/* No check that the address is free: as Anchor's init, the System Program refuses one in use (AccountAlreadyInUse,
|
|
809
|
+
custom 0), so a second create fails with the same error under both. */
|
|
754
810
|
uint64_t lamports;
|
|
755
811
|
TS_TRY(ts_minimum_balance(space, &lamports));
|
|
756
812
|
seeds[n].addr = ≎
|
|
@@ -769,6 +825,7 @@ static inline uint64_t ts_create_pda(TsParams *params, TsAccount *payer, TsAccou
|
|
|
769
825
|
TS_TRY(ts_invoke(params, &ix, &signer, 1));
|
|
770
826
|
} else {
|
|
771
827
|
/* Someone pre-funded the address (CreateAccount would fail): top up, allocate, assign. */
|
|
828
|
+
if (ts_key_eq(&payer->key, &acct->key)) TS_FAIL(TS_ERR_INIT_PAYER, "TypeSOL: the payer cannot be the account created");
|
|
772
829
|
if (acct->lamports < lamports) TS_TRY(ts_system_transfer(params, payer, acct, lamports - acct->lamports));
|
|
773
830
|
uint8_t alloc[12];
|
|
774
831
|
uint32_t alloc_tag = 8; /* Allocate */
|
|
@@ -808,8 +865,8 @@ static inline uint64_t ts_create_owned(TsParams *params, TsAccount *payer, TsAcc
|
|
|
808
865
|
} else if (!acct->is_signer) {
|
|
809
866
|
TS_FAIL(TS_ERR_NOT_SIGNER, "TypeSOL: a new keypair account must sign");
|
|
810
867
|
}
|
|
811
|
-
|
|
812
|
-
|
|
868
|
+
/* No check that the address is free: as Anchor's init, the System Program refuses one in use (AccountAlreadyInUse,
|
|
869
|
+
custom 0), so a second create fails with the same error under both. */
|
|
813
870
|
uint64_t lamports;
|
|
814
871
|
TS_TRY(ts_minimum_balance(space, &lamports));
|
|
815
872
|
SolSignerSeeds signer = {seeds, (uint64_t)(n + 1)};
|
|
@@ -826,6 +883,7 @@ static inline uint64_t ts_create_owned(TsParams *params, TsAccount *payer, TsAcc
|
|
|
826
883
|
SolInstruction ix = {(SolPubkey *)&TS_SYSTEM_PROGRAM_ID, metas, 2, data, sizeof(data)};
|
|
827
884
|
TS_TRY(ts_invoke(params, &ix, signers, nsigners));
|
|
828
885
|
} else {
|
|
886
|
+
if (ts_key_eq(&payer->key, &acct->key)) TS_FAIL(TS_ERR_INIT_PAYER, "TypeSOL: the payer cannot be the account created");
|
|
829
887
|
if (acct->lamports < lamports) TS_TRY(ts_system_transfer(params, payer, acct, lamports - acct->lamports));
|
|
830
888
|
uint8_t alloc[12];
|
|
831
889
|
uint32_t alloc_tag = 8; /* Allocate */
|
|
@@ -1058,6 +1116,62 @@ static inline uint64_t ts_t22_transfer_fee(const TsAccount *mint, uint64_t amoun
|
|
|
1058
1116
|
return SUCCESS;
|
|
1059
1117
|
}
|
|
1060
1118
|
|
|
1119
|
+
/* The TransferFee in force this epoch (TransferFeeConfig, extension 1): basis points and maximum, both 0 without one. */
|
|
1120
|
+
static inline uint64_t ts_t22_epoch_fee(const TsAccount *mint, uint64_t *bps, uint64_t *maximum) {
|
|
1121
|
+
uint64_t off, len;
|
|
1122
|
+
*bps = 0;
|
|
1123
|
+
*maximum = 0;
|
|
1124
|
+
if (!ts_t22_ext(mint, 1, &off, &len) || len < 108) return SUCCESS;
|
|
1125
|
+
uint64_t epoch;
|
|
1126
|
+
TS_TRY(ts_clock_u64(2, &epoch));
|
|
1127
|
+
const uint8_t *newer = mint->data + off + 90;
|
|
1128
|
+
const uint8_t *fee = epoch >= ts_read_le(newer, 8) ? newer : mint->data + off + 72;
|
|
1129
|
+
*maximum = ts_read_le(fee + 8, 8);
|
|
1130
|
+
*bps = ts_read_le(fee + 16, 2);
|
|
1131
|
+
return SUCCESS;
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
/* The fee withheld when the recipient is to get `post` (spl-token-2022's calculate_inverse_epoch_fee): the amount to
|
|
1135
|
+
send, as calculate_pre_fee_amount finds it, then the fee on it. Fails (5100) where spl-token-2022 returns None. */
|
|
1136
|
+
static inline uint64_t ts_t22_transfer_fee_inverse(const TsAccount *mint, uint64_t post, uint64_t *out) {
|
|
1137
|
+
uint64_t bps, maximum, pre;
|
|
1138
|
+
*out = 0;
|
|
1139
|
+
TS_TRY(ts_t22_epoch_fee(mint, &bps, &maximum));
|
|
1140
|
+
if (bps == 0) pre = post;
|
|
1141
|
+
else if (post == 0) pre = 0;
|
|
1142
|
+
else if (bps == 10000) {
|
|
1143
|
+
if (__builtin_add_overflow(maximum, post, &pre)) TS_FAIL(TS_ERR_OVERFLOW, "TypeSOL: transfer fee overflow");
|
|
1144
|
+
} else {
|
|
1145
|
+
unsigned __int128 raw = ((unsigned __int128)post * 10000 + (10000 - bps) - 1) / (10000 - bps);
|
|
1146
|
+
if (raw - post >= maximum) {
|
|
1147
|
+
if (__builtin_add_overflow(post, maximum, &pre)) TS_FAIL(TS_ERR_OVERFLOW, "TypeSOL: transfer fee overflow");
|
|
1148
|
+
} else {
|
|
1149
|
+
if (raw > UINT64_MAX) TS_FAIL(TS_ERR_OVERFLOW, "TypeSOL: transfer fee overflow");
|
|
1150
|
+
pre = (uint64_t)raw;
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
if (bps == 0 || pre == 0) return SUCCESS;
|
|
1154
|
+
unsigned __int128 fee = ((unsigned __int128)pre * bps + 9999) / 10000;
|
|
1155
|
+
*out = fee > maximum ? maximum : (uint64_t)fee;
|
|
1156
|
+
return SUCCESS;
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
/* Whether every extension of a Token-2022 mint or account is one of `allowed` (true for legacy SPL Token ones). */
|
|
1160
|
+
static inline bool ts_t22_extensions_within(const TsAccount *a, const uint16_t *allowed, uint64_t n) {
|
|
1161
|
+
if (!ts_key_eq(&a->owner, &TS_TOKEN_2022_PROGRAM_ID) || a->data_len <= TS_TOKEN_ACCOUNT_LEN + 1) return true;
|
|
1162
|
+
uint64_t at = TS_TOKEN_ACCOUNT_LEN + 1;
|
|
1163
|
+
while (at + 4 <= a->data_len) {
|
|
1164
|
+
uint16_t t = (uint16_t)ts_read_le(a->data + at, 2);
|
|
1165
|
+
uint16_t len = (uint16_t)ts_read_le(a->data + at + 2, 2);
|
|
1166
|
+
if (t == 0) return true; /* uninitialized: end of the entries */
|
|
1167
|
+
bool ok = false;
|
|
1168
|
+
for (uint64_t i = 0; i < n; i++) ok = ok || allowed[i] == t;
|
|
1169
|
+
if (!ok) return false;
|
|
1170
|
+
at += 4 + (uint64_t)len;
|
|
1171
|
+
}
|
|
1172
|
+
return true;
|
|
1173
|
+
}
|
|
1174
|
+
|
|
1061
1175
|
/* ---- Metaplex Core and Bubblegum ----------------------------------------- */
|
|
1062
1176
|
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
1177
|
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}};
|