@typesol/cli 0.45.0 → 0.46.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 +227 -0
- package/README.md +64 -17
- package/SECURITY.md +9 -7
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +9 -0
- package/dist/cli.js +33 -15
- package/dist/compiler/analyze.js +57 -6
- package/dist/compiler/diagnostics.d.ts +4 -3
- package/dist/compiler/diagnostics.js +10 -6
- package/dist/compiler/emit/base.d.ts +1 -1
- package/dist/compiler/emit/calls.js +18 -5
- package/dist/compiler/emit/cpi-core.d.ts +3 -1
- package/dist/compiler/emit/cpi-core.js +8 -4
- package/dist/compiler/emit/expressions.d.ts +14 -0
- package/dist/compiler/emit/expressions.js +52 -4
- package/dist/compiler/emit/shared.d.ts +8 -1
- package/dist/compiler/emit/shared.js +8 -1
- package/dist/compiler/emit/statements.d.ts +8 -3
- package/dist/compiler/emit/statements.js +68 -3
- package/dist/compiler/emit/storage.js +5 -1
- package/dist/compiler/hints.d.ts +7 -0
- package/dist/compiler/hints.js +58 -9
- package/dist/compiler/index.d.ts +5 -0
- package/dist/compiler/index.js +10 -1
- package/dist/compiler/model.js +3 -2
- package/dist/compiler/toolchain.d.ts +50 -2
- package/dist/compiler/toolchain.js +96 -11
- package/dist/node-version.d.ts +4 -0
- package/dist/node-version.js +12 -0
- package/dist/tools/doctor.d.ts +41 -0
- package/dist/tools/doctor.js +96 -14
- package/dist/workspace.d.ts +9 -2
- package/dist/workspace.js +13 -3
- package/docker/deps/0.46.0.lock.json +1206 -0
- package/docs/ai.md +2 -1
- package/docs/architecture.md +7 -3
- package/docs/audit.md +6 -6
- package/docs/case-study-phoenix.md +1 -1
- package/docs/case-study-raydium-amm.md +1 -1
- package/docs/case-study-spl-stake-pool.md +1 -1
- package/docs/cli.md +42 -4
- package/docs/errors.md +22 -8
- package/docs/glossary.md +210 -0
- package/docs/language-accounts.md +499 -0
- package/docs/language-basics.md +389 -0
- package/docs/language-builtins.md +411 -0
- package/docs/language-chain.md +198 -0
- package/docs/language-cpi.md +470 -0
- package/docs/language-errors-events.md +184 -0
- package/docs/language-native.md +493 -0
- package/docs/language-pdas.md +198 -0
- package/docs/language-statements.md +620 -0
- package/docs/language-types.md +601 -0
- package/docs/language.md +190 -467
- package/docs/mainnet.md +18 -3
- package/docs/migrate-from-anchor.md +7 -6
- package/docs/security-model.md +3 -3
- package/docs/stability.md +3 -1
- package/docs/test-your-program.md +52 -15
- package/docs/troubleshooting.md +222 -100
- package/lib/typesol.d.ts +67 -22
- package/llms-full.txt +4915 -634
- package/llms.txt +12 -1
- package/package.json +2 -2
- package/releases/0.45.0.json +732 -0
- package/ts-plugin/index.js +16 -5
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
# Language basics
|
|
2
|
+
|
|
3
|
+
This page explains what a TypeSOL program is: the file, its imports, its instructions and account data, and what the
|
|
4
|
+
compiler derives from them. It is the first page of the [language guide](language.md). It assumes you know
|
|
5
|
+
TypeScript; it does not assume you know [Anchor](glossary.md#anchor) or Rust, and [Solana in a dozen
|
|
6
|
+
lines](#solana-in-a-dozen-lines) introduces the chain's terms.
|
|
7
|
+
|
|
8
|
+
## A program is a TypeScript file
|
|
9
|
+
|
|
10
|
+
A TypeSOL program is a `.ts` file, the entry file, and the TypeSOL files it imports. The TypeScript compiler
|
|
11
|
+
type-checks it against `lib/typesol.d.ts` (the package's declarations), then TypeSOL checks that it stays inside the
|
|
12
|
+
language, lowers it to C and builds a Solana SBF program with clang. Nothing runs on chain but the compiled code: there
|
|
13
|
+
is no interpreter and no JavaScript runtime.
|
|
14
|
+
|
|
15
|
+
The file is made of:
|
|
16
|
+
|
|
17
|
+
| Top-level declaration | What it becomes |
|
|
18
|
+
| --- | --- |
|
|
19
|
+
| `export function name(…)` | an [instruction](#instructions): an entry point clients call |
|
|
20
|
+
| `export interface Name { … }` | a struct: [account data](#account-data), an argument, an event or a field of another struct |
|
|
21
|
+
| `export enum Name { … }` | a unit enum, or an error enum when its members are strings ([errors](language-errors-events.md)) |
|
|
22
|
+
| `export type Name = { kind: "A"; … } \| { kind: "B"; … }` | a data enum ([types](language-types.md#data-enums)) |
|
|
23
|
+
| `function name(…)` (not exported) | a [helper](language-statements.md#helpers), called by instructions and other helpers |
|
|
24
|
+
| `const NAME = …` | a compile-time [constant](#constants) |
|
|
25
|
+
| `type Name = …` | a [type alias](#type-aliases) |
|
|
26
|
+
| `interface Name { … }` (not exported) | a [file-local struct](language-types.md#file-local-structs), left out of the IDL |
|
|
27
|
+
| `import { … } from "typesol"` or `"./file"` | the language's built-ins, or another TypeSOL file |
|
|
28
|
+
|
|
29
|
+
Every other top-level statement is refused: `class`, `namespace`, `let` and `var`, and expressions with effects (see
|
|
30
|
+
[what is refused](language-statements.md#what-is-refused)).
|
|
31
|
+
|
|
32
|
+
## Solana in a dozen lines
|
|
33
|
+
|
|
34
|
+
What the rest of the guide assumes about the chain. Each term is in the [glossary](glossary.md); Solana's own
|
|
35
|
+
documentation is at [solana.com/docs/core](https://solana.com/docs/core).
|
|
36
|
+
|
|
37
|
+
- An [account](glossary.md#account) holds [lamports](glossary.md#lamports) (1 SOL is 10^9 of them) and bytes of
|
|
38
|
+
data, and has an [owner](glossary.md#owner) program. Only the owner changes the data or takes lamports out; any
|
|
39
|
+
program can add lamports to a writable account.
|
|
40
|
+
- A program is an account holding code. It keeps no state of its own: what it remembers is in accounts it owns, and
|
|
41
|
+
the [checks on its parameters](language-accounts.md) make sure an account is one of them.
|
|
42
|
+
- Every account holds the [rent](glossary.md#rent)-exempt minimum for its size; closing it returns that
|
|
43
|
+
([limits and costs](#limits-and-costs)).
|
|
44
|
+
- A [transaction](glossary.md#transaction) holds [instructions](glossary.md#instruction). It lists every account they
|
|
45
|
+
use, which are [writable](glossary.md#writable) and which [sign](glossary.md#signer); a program can reach no other
|
|
46
|
+
account. If one instruction fails, the whole transaction fails.
|
|
47
|
+
- A [PDA](language-pdas.md) is an address derived from [seeds](glossary.md#seeds) and a program id, with a
|
|
48
|
+
[bump](glossary.md#bump) byte; no key can sign for it but that program's own calls.
|
|
49
|
+
- One program calls another with a [CPI](language-cpi.md); the [System Program](glossary.md#system-program) creates
|
|
50
|
+
accounts and moves SOL, and [sysvars](language-chain.md#sysvars) hold the clock and other cluster state.
|
|
51
|
+
- Work is metered in [compute units](glossary.md#compute-units), with a budget per transaction.
|
|
52
|
+
- A build writes an [IDL](glossary.md#idl), the JSON that clients encode calls from; arguments are
|
|
53
|
+
[Borsh](glossary.md#borsh)-encoded, the program itself is [SBF](glossary.md#sbf) bytecode.
|
|
54
|
+
|
|
55
|
+
## A first program
|
|
56
|
+
|
|
57
|
+
A poll: anyone opens one at an address derived from their key and a number, anyone votes, and the creator ends it and
|
|
58
|
+
gets the number of votes back as the instruction's result.
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import { type Account, type Init, type Mut, type Pubkey, type Signer, type SystemProgram, type u64, type u8, emit, require } from "typesol";
|
|
62
|
+
|
|
63
|
+
/** One poll: the votes for and against. */
|
|
64
|
+
export interface Poll {
|
|
65
|
+
creator: Pubkey;
|
|
66
|
+
/** Votes for yes. */
|
|
67
|
+
yes: u64;
|
|
68
|
+
no: u64;
|
|
69
|
+
closed: boolean;
|
|
70
|
+
bump: u8;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface Voted {
|
|
74
|
+
poll: Pubkey;
|
|
75
|
+
yes: boolean;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export enum PollError {
|
|
79
|
+
Closed = "The poll is closed",
|
|
80
|
+
NotCreator = "Only the creator can end the poll",
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const SEED = "poll";
|
|
84
|
+
|
|
85
|
+
/** Open a poll at the address ["poll", creator, id]. */
|
|
86
|
+
export function open(creator: Mut<Signer>, poll: Init<Poll>, systemProgram: SystemProgram, id: u64): void {
|
|
87
|
+
poll.bump = poll.create(creator, [SEED, creator.key, id]);
|
|
88
|
+
poll.creator = creator.key;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function vote(voter: Signer, poll: Mut<Poll>, yes: boolean): void {
|
|
92
|
+
require(!poll.closed, PollError.Closed);
|
|
93
|
+
if (yes) poll.yes += 1n;
|
|
94
|
+
else poll.no += 1n;
|
|
95
|
+
emit<Voted>({ poll: poll.key, yes });
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function end(creator: Signer, poll: Mut<Poll>): u64 {
|
|
99
|
+
require(poll.creator === creator.key, PollError.NotCreator);
|
|
100
|
+
poll.closed = true;
|
|
101
|
+
return total(poll);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function total(poll: Account<Poll>): u64 {
|
|
105
|
+
return poll.yes + poll.no;
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Line by line:
|
|
110
|
+
|
|
111
|
+
- **The import.** Everything the program uses comes from `"typesol"`. Types (`u64`, `Mut`, `Signer`) are imported
|
|
112
|
+
with `type`; functions (`require`, `emit`) without it. There is no other module to import from: no Node.js, no npm
|
|
113
|
+
packages, no `console`.
|
|
114
|
+
- **`export interface Poll`.** An exported interface is a struct. Used as an account's type (`Init<Poll>`,
|
|
115
|
+
`Mut<Poll>`), it is account data: an 8-byte discriminator, then the fields in order, 8 + 32 + 8 + 8 + 1 + 1 = 58
|
|
116
|
+
bytes.
|
|
117
|
+
The JSDoc comments become `docs` in the IDL.
|
|
118
|
+
- **`export interface Voted`.** Another struct; this one is only used by `emit<Voted>(…)`, so the IDL lists it under
|
|
119
|
+
`events`.
|
|
120
|
+
- **`export enum PollError`.** An enum whose members are strings is an error enum. `PollError.Closed` is error 6000
|
|
121
|
+
and `PollError.NotCreator` 6001, with these messages in the IDL, as Anchor's `#[error_code]`.
|
|
122
|
+
- **`const SEED = "poll"`.** A constant, inlined where it is used. Here it is a seed.
|
|
123
|
+
- **`open`.** An exported function is an instruction. Its parameters are the accounts the client passes (`creator`,
|
|
124
|
+
`poll`, `systemProgram`) and the arguments it sends (`id`). Each account type says what is checked before the body
|
|
125
|
+
runs: `Mut<Signer>` must sign and be writable, `Init<Poll>` must be writable and is created by the body,
|
|
126
|
+
`SystemProgram` must be the System Program. `poll.create(creator, [SEED, creator.key, id])` derives the address
|
|
127
|
+
from the seeds, checks that `poll` is that address, creates the account with `creator` paying the rent, writes the
|
|
128
|
+
discriminator and returns the bump. The new account's fields start at zero.
|
|
129
|
+
- **`vote`.** `Mut<Poll>` is an existing account of this program holding a `Poll` (owner, discriminator and size
|
|
130
|
+
checked), writable. `require(condition, error)` fails the instruction when the condition is false, and with it
|
|
131
|
+
the whole transaction: nothing it wrote is kept. `poll.yes += 1n` is checked: an overflow fails the instruction
|
|
132
|
+
too. Integer literals are bigints, written `1n`.
|
|
133
|
+
- **`end`.** Only the creator may end the poll: `require` compares the stored `creator` with the signer. It sets
|
|
134
|
+
`closed`, so later votes fail with 6000, and returns the total. An instruction may return a value: the client reads
|
|
135
|
+
it as return data, and the IDL lists it under `returns`. (The account stays open; `close(account, destination)`
|
|
136
|
+
would delete it and return its rent: [closing and resizing](language-accounts.md#closing-and-resizing).)
|
|
137
|
+
- **`function total`.** A function that is not exported is a helper. It takes the account as `Account<Poll>`
|
|
138
|
+
(read-only) and returns a `u64`. Helpers are not entry points and are not in the IDL.
|
|
139
|
+
|
|
140
|
+
The IDL the build writes describes all of this: three instructions with their accounts (with `poll`'s seeds under
|
|
141
|
+
`pda`, so clients derive its address), arguments and return type, the `Poll` account, the `Voted` event and the two
|
|
142
|
+
errors.
|
|
143
|
+
|
|
144
|
+
## Imports
|
|
145
|
+
|
|
146
|
+
```ts
|
|
147
|
+
import { type Mut, type u64, require, u8 } from "typesol";
|
|
148
|
+
|
|
149
|
+
export interface Gauge {
|
|
150
|
+
level: u8;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function set(gauge: Mut<Gauge>, value: u64): void {
|
|
154
|
+
require(value <= 100n, "level is 0 to 100");
|
|
155
|
+
gauge.level = u8(value);
|
|
156
|
+
}
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
- `"typesol"` is the language's library. Import types with `type`, and functions and constants without it.
|
|
160
|
+
- A cast is a function as well as a type: `u8(x)` converts, `x: u8` declares. Import `u8` without `type` when the
|
|
161
|
+
code calls it; one import serves as both.
|
|
162
|
+
- `import { … } from "./math"` imports another TypeSOL file of the program ([multiple files](#multiple-files)).
|
|
163
|
+
- Nothing else can be imported: `only imports from "typesol" and relative TypeSOL files ("./math") are allowed`.
|
|
164
|
+
Imports must be named imports (`use named imports: import { ... } from "typesol"`), not a default or namespace
|
|
165
|
+
import. Names from `"typesol"` may be renamed (`import { log as say }`); names from TypeSOL files may not.
|
|
166
|
+
|
|
167
|
+
## Instructions
|
|
168
|
+
|
|
169
|
+
An exported function is an instruction. The client calls it by name; the program finds it by its discriminator.
|
|
170
|
+
`export default function bump(…)` is an instruction too, named `bump` like any other: the program has no default
|
|
171
|
+
entry point.
|
|
172
|
+
|
|
173
|
+
- **Discriminator.** The first 8 bytes of the instruction data: sha256("global:<name>")[..8], where `<name>` is the
|
|
174
|
+
function's name in snake_case (`relayIncrement` is `global:relay_increment`), as Anchor computes it. Two functions
|
|
175
|
+
whose names meet in snake_case are refused. `/** @discriminator … */` replaces it
|
|
176
|
+
([interfaces](language-cpi.md#implementing-another-programs-interface-transfer-hooks)).
|
|
177
|
+
- **Parameters.** Each parameter is an account (its type is an account type: `Signer`, `Account<T>`, `Mut<T>`, …,
|
|
178
|
+
see [accounts](language-accounts.md)) or an argument (an integer, `boolean`, `Pubkey`, `string`, a struct, an enum,
|
|
179
|
+
an `Option`, a list). The client passes the accounts in the order they are declared, and sends the arguments,
|
|
180
|
+
Borsh-encoded, after the discriminator.
|
|
181
|
+
- **Body.** Before the first statement the accounts are checked and the arguments decoded: a transaction that fails a
|
|
182
|
+
check never reaches your code ([which error a transaction gets](errors.md#which-error-a-transaction-gets)).
|
|
183
|
+
- **Result.** `void`, or one integer, `boolean`, `Pubkey`, `string` (a Borsh String) or fixed-size struct, sent as
|
|
184
|
+
return data (at most 1024 bytes), listed as `returns` in the IDL and decoded by the generated client.
|
|
185
|
+
- **Failure.** There are no exceptions. A failed `require`, a failed account check, an overflow or a failed call to
|
|
186
|
+
another program ends the instruction with an error code. The whole transaction fails: the runtime discards every
|
|
187
|
+
change it made, in every one of its instructions.
|
|
188
|
+
|
|
189
|
+
Unlike TypeScript, an instruction cannot be `async` or a generator, and its parameters cannot be optional (`x?: u64`),
|
|
190
|
+
have defaults or be a rest parameter. An argument that may be absent is an `Option<T>` the client passes as `null`.
|
|
191
|
+
|
|
192
|
+
How a transaction reaches an instruction:
|
|
193
|
+
|
|
194
|
+
1. Instruction data shorter than 8 bytes fails with 100 (InstructionMissing).
|
|
195
|
+
2. The discriminator selects the instruction; an unknown one fails with 101 (InstructionFallbackNotFound), unless the
|
|
196
|
+
program has a [`@fallback`](language-native.md#the-fallback-entry) entry.
|
|
197
|
+
3. The accounts are checked in parameter order, then the arguments are decoded (malformed or short data: 102,
|
|
198
|
+
InstructionDidNotDeserialize), then the constraints run ([accounts](language-accounts.md)).
|
|
199
|
+
4. The body runs.
|
|
200
|
+
|
|
201
|
+
## Account data
|
|
202
|
+
|
|
203
|
+
An exported interface whose fields have a fixed size is a struct. As the type of an account parameter it is that
|
|
204
|
+
account's data:
|
|
205
|
+
|
|
206
|
+
```ts
|
|
207
|
+
import { type Mut, type Pubkey, type u64, type FixedArray } from "typesol";
|
|
208
|
+
|
|
209
|
+
export interface Point {
|
|
210
|
+
x: u64;
|
|
211
|
+
y: u64;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export interface Player {
|
|
215
|
+
owner: Pubkey;
|
|
216
|
+
home: Point;
|
|
217
|
+
trail: FixedArray<Point, 4>;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export function move(player: Mut<Player>, x: u64, y: u64): void {
|
|
221
|
+
player.home = { x, y };
|
|
222
|
+
}
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
- An account of this program starts with an 8-byte discriminator, sha256("account:<Name>")[..8] (Anchor's), then the
|
|
226
|
+
fields in declaration order, back to back with no padding, integers little-endian. `Player` takes 8 + 32 + 16 +
|
|
227
|
+
4 × 16 = 120 bytes.
|
|
228
|
+
- Interfaces nest: `home: Point`, `trail: FixedArray<Point, 4>`.
|
|
229
|
+
- Every field has a fixed size, except `Vec<T>` fields, which make the account grow
|
|
230
|
+
([Vec](language-types.md#vec)). Each type's size is in [types](language-types.md).
|
|
231
|
+
- Unlike TypeScript, fields cannot be optional (`v?: u64`): `optional fields are not supported; use v: Option<T> (null
|
|
232
|
+
for none)`. Interfaces hold plain fields only; methods and getters are refused.
|
|
233
|
+
- What the IDL describes (account data, arguments, results, events, an exported struct's field, a data enum's
|
|
234
|
+
variant) must be exported: a struct used there without `export` is an error saying to export it.
|
|
235
|
+
|
|
236
|
+
Anchor programs store the same bytes as long as the struct has only fixed-size types. Borsh strings, vectors and options
|
|
237
|
+
in account data (Anchor's default) need [`@layout borsh`](language-accounts.md#anchors-account-bytes-layout-borsh).
|
|
238
|
+
|
|
239
|
+
## Helpers
|
|
240
|
+
|
|
241
|
+
A function that is not exported is a helper: it can be called from instructions and other helpers, is not an entry
|
|
242
|
+
point and is not in the IDL. Every function of an imported file is a helper too. Helpers take values, structs, lists
|
|
243
|
+
and accounts the instruction has already checked, and return a value or `void`. They need an explicit return type
|
|
244
|
+
and cannot call themselves. See [functions and helpers](language-statements.md#helpers).
|
|
245
|
+
|
|
246
|
+
## Constants
|
|
247
|
+
|
|
248
|
+
```ts
|
|
249
|
+
import { type Mut, type u16, type u64, log } from "typesol";
|
|
250
|
+
|
|
251
|
+
const SEED = "vault";
|
|
252
|
+
const FEE_BPS: u16 = 30n;
|
|
253
|
+
const ROOM = 1280n - 34n;
|
|
254
|
+
const ON = true;
|
|
255
|
+
|
|
256
|
+
export interface Config {
|
|
257
|
+
fee: u16;
|
|
258
|
+
room: u64;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export function setup(config: Mut<Config>): void {
|
|
262
|
+
config.fee = FEE_BPS;
|
|
263
|
+
config.room = ROOM;
|
|
264
|
+
if (ON) log(SEED);
|
|
265
|
+
}
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
- A top-level `const` is a compile-time constant: a bigint, boolean or string literal, `pubkey("…")` (a base58
|
|
269
|
+
address, checked when the program compiles), or an integer expression over literals and constants declared before
|
|
270
|
+
it, in this file or an imported one (`+ - * / % ** & | ^ << >>`, unary `-` and `~`).
|
|
271
|
+
- Constants are inlined where they are used, and may be seeds; when a seed is a constant, the IDL records its value.
|
|
272
|
+
- A constant with an integer type is computed in that type, every step checked: `const X: u64 = 1n - 2n + 5n` is an
|
|
273
|
+
error (`1n - 2n is -1, which does not fit in u64`), and `const M: u8 = ~1n` is 254 (`~` within the width).
|
|
274
|
+
Division by zero, a negative exponent or a shift past the width are compile errors.
|
|
275
|
+
- An untyped integer constant is inlined as its value and takes the type of the place it is used in, as a literal
|
|
276
|
+
does: `const FEE = 30n` stores into a `u16` field, and `const FEE = 300n` into a `u8` one is `300 does not fit in u8`.
|
|
277
|
+
- Top-level `let` and `var` are refused: `top-level variables must be const literals`. A program has no global state
|
|
278
|
+
other than its accounts.
|
|
279
|
+
- A few names configure the compiler: `HEAP_SIZE_KIB` ([below](#memory-stack-and-heap)), `MAX_ACCOUNTS`
|
|
280
|
+
([accounts](language-accounts.md#how-many-accounts)), `NATIVE_ACCOUNT_WRITES`, `PANIC_ON_OVERFLOW`, `PANIC_STYLE`,
|
|
281
|
+
`ERROR_LOG_PREFIX`, `PROGRAM_ERROR_LOG_PREFIX` ([native programs](language-native.md)).
|
|
282
|
+
|
|
283
|
+
## Type aliases
|
|
284
|
+
|
|
285
|
+
```ts
|
|
286
|
+
import { type FixedVec, type Mut, type Pubkey, type u64, type u8 } from "typesol";
|
|
287
|
+
|
|
288
|
+
type Price = u64;
|
|
289
|
+
type Key = Pubkey;
|
|
290
|
+
type Buf = FixedVec<u8, 32>;
|
|
291
|
+
type Ask = Price;
|
|
292
|
+
|
|
293
|
+
export interface Book {
|
|
294
|
+
best: Ask;
|
|
295
|
+
maker: Key;
|
|
296
|
+
memo: Buf;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export function quote(book: Mut<Book>, price: Price): void {
|
|
300
|
+
book.best = price;
|
|
301
|
+
}
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
A type alias is another name for one type, replaced by it wherever it is used: layouts, arguments and the IDL see the
|
|
305
|
+
aliased type. Aliases can be exported or not, imported like any name, and name other aliases (the aliased type keeps
|
|
306
|
+
the names of the file declaring it). They cannot be generic (`type aliases cannot be generic`). A union of named
|
|
307
|
+
types (`type Event = Fill | Place`) is neither an alias nor a data enum: write the variants as object types
|
|
308
|
+
([data enums](language-types.md#data-enums)).
|
|
309
|
+
|
|
310
|
+
## Multiple files
|
|
311
|
+
|
|
312
|
+
```ts file=math.ts
|
|
313
|
+
import { type u64 } from "typesol";
|
|
314
|
+
|
|
315
|
+
export const BPS = 10000n;
|
|
316
|
+
|
|
317
|
+
export function fee(amount: u64, bps: u64): u64 {
|
|
318
|
+
return (amount * bps) / BPS;
|
|
319
|
+
}
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
```ts
|
|
323
|
+
import { type Mut, type u64 } from "typesol";
|
|
324
|
+
import { fee } from "./math";
|
|
325
|
+
|
|
326
|
+
export interface Vault {
|
|
327
|
+
fees: u64;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
export function charge(vault: Mut<Vault>, amount: u64): void {
|
|
331
|
+
vault.fees += fee(amount, 30n);
|
|
332
|
+
}
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
- Structs, enums, constants and helpers can live in other files. Only the entry file's exported functions are
|
|
336
|
+
instructions; every function of an imported file is a helper.
|
|
337
|
+
- Names are program-wide, so imports from TypeSOL files cannot be renamed (`import { a as b }` is refused).
|
|
338
|
+
- Error codes number the imported files' error enums first.
|
|
339
|
+
|
|
340
|
+
## What the compiler derives
|
|
341
|
+
|
|
342
|
+
From the source alone, a build writes:
|
|
343
|
+
|
|
344
|
+
- `<name>.so`: the program.
|
|
345
|
+
- `<name>.json`: an Anchor IDL (spec 0.1.0). It lists every instruction (name, discriminator, accounts with their
|
|
346
|
+
`writable` / `signer` flags, `pda` seeds and fixed `address`es, `optional` accounts, `relations`, arguments,
|
|
347
|
+
`returns`), every account struct, type, event and error, and the JSDoc of instructions, `@param` tags, structs,
|
|
348
|
+
fields and data enums as `docs`. Anchor and Codama clients read it unchanged.
|
|
349
|
+
- `<name>.client.ts`: a typed client ([client.md](client.md)).
|
|
350
|
+
- `<name>.c` and `<name>.build.json`: the generated C and the build's record ([cli.md](cli.md)).
|
|
351
|
+
|
|
352
|
+
## Memory: stack and heap
|
|
353
|
+
|
|
354
|
+
A program runs with a 4 KiB stack frame per function and a heap of 32 KiB. Locals live on the stack: a list local
|
|
355
|
+
holds at most 2 KiB, and a frame that overflows is a compile error that names its biggest locals. Strings built at run
|
|
356
|
+
time and a few other values live on the heap ([heap values](language-types.md#heap-values)), which is never freed
|
|
357
|
+
during an instruction.
|
|
358
|
+
|
|
359
|
+
`const HEAP_SIZE_KIB = 64n` (32 to 256) lets the program use a bigger heap; the client must then add a ComputeBudget
|
|
360
|
+
`requestHeapFrame` instruction with that size to the transaction.
|
|
361
|
+
|
|
362
|
+
## Limits and costs
|
|
363
|
+
|
|
364
|
+
The cluster sets these; the numbers are those at the time of writing.
|
|
365
|
+
|
|
366
|
+
- **Rent.** An account holds the rent-exempt minimum for its size, (128 + data bytes) × 6,960 lamports: 1,294,560
|
|
367
|
+
lamports (about 0.0013 SOL) for the 58-byte poll above. `create` takes it from the payer, `rentExempt(space)` computes
|
|
368
|
+
it in the program, and `close(account, destination)` moves every lamport, the rent included, to `destination`.
|
|
369
|
+
- **Compute.** A transaction gets 200,000 compute units per instruction by default and at most 1,400,000, which the
|
|
370
|
+
client asks for with a ComputeBudget `SetComputeUnitLimit` instruction; past its budget the transaction fails. To
|
|
371
|
+
see where a program spends them, build with `typesol build --profile` and pass the transaction's logs to `typesol
|
|
372
|
+
profile` ([cli.md](cli.md)), which gives compute units per TypeScript line.
|
|
373
|
+
- **Transaction size.** At most 1,232 bytes, everything included: the signatures (64 bytes each), the message
|
|
374
|
+
header, every account's 32-byte address (one byte for an account read from an address lookup table), a recent
|
|
375
|
+
blockhash and each instruction's accounts and data. It bounds how many accounts and how many bytes of arguments one
|
|
376
|
+
transaction can carry.
|
|
377
|
+
- **Account size.** At most 10 MiB. `create` allocates at most 10 KiB, and an account grows by at most 10 KiB per
|
|
378
|
+
instruction (3016): grow a larger one over several instructions.
|
|
379
|
+
|
|
380
|
+
TypeSOL sets one more: at most 32 accounts per instruction (5113), unless the program raises it with
|
|
381
|
+
[`MAX_ACCOUNTS`](language-accounts.md#how-many-accounts).
|
|
382
|
+
|
|
383
|
+
## Next
|
|
384
|
+
|
|
385
|
+
- [Types](language-types.md): integers, strings, enums, structs, options and lists.
|
|
386
|
+
- [Statements and functions](language-statements.md): which TypeScript is TypeSOL.
|
|
387
|
+
- [Accounts](language-accounts.md): account parameters and the checks they emit.
|
|
388
|
+
- A complete program that moves tokens: [`examples/escrow.ts`](https://github.com/typesol-lang/typesol/blob/main/examples/escrow.ts), which `typesol init my-app
|
|
389
|
+
--template escrow` scaffolds with its tests ([tokens](language-cpi.md#tokens)).
|