@btc-vision/btc-runtime 1.10.10 → 1.10.11

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.
Files changed (44) hide show
  1. package/LICENSE +190 -0
  2. package/README.md +258 -137
  3. package/SECURITY.md +226 -0
  4. package/docs/README.md +614 -0
  5. package/docs/advanced/bitcoin-scripts.md +939 -0
  6. package/docs/advanced/cross-contract-calls.md +579 -0
  7. package/docs/advanced/plugins.md +1006 -0
  8. package/docs/advanced/quantum-resistance.md +660 -0
  9. package/docs/advanced/signature-verification.md +715 -0
  10. package/docs/api-reference/blockchain.md +729 -0
  11. package/docs/api-reference/events.md +642 -0
  12. package/docs/api-reference/op20.md +902 -0
  13. package/docs/api-reference/op721.md +819 -0
  14. package/docs/api-reference/safe-math.md +510 -0
  15. package/docs/api-reference/storage.md +840 -0
  16. package/docs/contracts/op-net-base.md +786 -0
  17. package/docs/contracts/op20-token.md +687 -0
  18. package/docs/contracts/op20s-signatures.md +614 -0
  19. package/docs/contracts/op721-nft.md +785 -0
  20. package/docs/contracts/reentrancy-guard.md +787 -0
  21. package/docs/core-concepts/blockchain-environment.md +724 -0
  22. package/docs/core-concepts/decorators.md +466 -0
  23. package/docs/core-concepts/events.md +652 -0
  24. package/docs/core-concepts/pointers.md +391 -0
  25. package/docs/core-concepts/security.md +473 -0
  26. package/docs/core-concepts/storage-system.md +969 -0
  27. package/docs/examples/basic-token.md +745 -0
  28. package/docs/examples/nft-with-reservations.md +1440 -0
  29. package/docs/examples/oracle-integration.md +1212 -0
  30. package/docs/examples/stablecoin.md +1180 -0
  31. package/docs/getting-started/first-contract.md +575 -0
  32. package/docs/getting-started/installation.md +384 -0
  33. package/docs/getting-started/project-structure.md +630 -0
  34. package/docs/storage/memory-maps.md +764 -0
  35. package/docs/storage/stored-arrays.md +778 -0
  36. package/docs/storage/stored-maps.md +758 -0
  37. package/docs/storage/stored-primitives.md +655 -0
  38. package/docs/types/address.md +773 -0
  39. package/docs/types/bytes-writer-reader.md +938 -0
  40. package/docs/types/calldata.md +744 -0
  41. package/docs/types/safe-math.md +446 -0
  42. package/package.json +51 -26
  43. package/runtime/memory/MapOfMap.ts +1 -0
  44. package/LICENSE.md +0 -21
@@ -0,0 +1,384 @@
1
+ # Installation
2
+
3
+ This guide walks you through setting up your development environment for building OPNet smart contracts.
4
+
5
+ ## Quick Start - Clone Example Project
6
+
7
+ The fastest way to get started is to clone the official example-tokens repository:
8
+
9
+ ```bash
10
+ git clone https://github.com/btc-vision/example-tokens.git
11
+ cd example-tokens
12
+ npm install
13
+ npm run build:token
14
+ ```
15
+
16
+ This repository contains working examples of:
17
+ - Basic OP20 token
18
+ - Stablecoin with roles and pausability
19
+ - Pegged token
20
+ - Multi-oracle stablecoin
21
+ - NFT (OP721)
22
+
23
+ ## Prerequisites
24
+
25
+ Before you begin, ensure you have:
26
+
27
+ - **Node.js 22+** - [Download Node.js](https://nodejs.org/)
28
+ - **npm** - Comes with Node.js
29
+ - **Git** - [Download Git](https://git-scm.com/)
30
+
31
+ Verify your installation:
32
+
33
+ ```bash
34
+ node --version # Should be v22.0.0 or higher
35
+ npm --version # Should be v9.0.0 or higher
36
+ ```
37
+
38
+ ## Manual Setup
39
+
40
+ If you prefer to set up from scratch:
41
+
42
+ ### 1. Create Project
43
+
44
+ ```bash
45
+ mkdir my-opnet-contract
46
+ cd my-opnet-contract
47
+ npm init -y
48
+ ```
49
+
50
+ ### 2. Install Dependencies
51
+
52
+ ```bash
53
+ npm install @btc-vision/btc-runtime @btc-vision/as-bignum @btc-vision/opnet-transform
54
+ npm install --save-dev assemblyscript prettier typescript
55
+ ```
56
+
57
+ ### 3. Create Configuration Files
58
+
59
+ #### `asconfig.json`
60
+
61
+ ```json
62
+ {
63
+ "targets": {
64
+ "token": {
65
+ "outFile": "build/MyToken.wasm",
66
+ "use": ["abort=src/token/index/abort"]
67
+ }
68
+ },
69
+ "options": {
70
+ "sourceMap": false,
71
+ "optimizeLevel": 3,
72
+ "shrinkLevel": 1,
73
+ "converge": true,
74
+ "noAssert": false,
75
+ "enable": [
76
+ "sign-extension",
77
+ "mutable-globals",
78
+ "nontrapping-f2i",
79
+ "bulk-memory",
80
+ "simd",
81
+ "reference-types",
82
+ "multi-value"
83
+ ],
84
+ "runtime": "stub",
85
+ "memoryBase": 0,
86
+ "initialMemory": 1,
87
+ "exportStart": "start",
88
+ "transform": "@btc-vision/opnet-transform"
89
+ }
90
+ }
91
+ ```
92
+
93
+ #### `package.json`
94
+
95
+ ```json
96
+ {
97
+ "name": "my-opnet-contract",
98
+ "version": "1.0.0",
99
+ "type": "module",
100
+ "scripts": {
101
+ "build:token": "asc src/token/index.ts --target token --measure --uncheckedBehavior never"
102
+ },
103
+ "dependencies": {
104
+ "@btc-vision/as-bignum": "^0.0.6",
105
+ "@btc-vision/btc-runtime": "^1.10.8",
106
+ "@btc-vision/opnet-transform": "^0.1.12"
107
+ },
108
+ "devDependencies": {
109
+ "assemblyscript": "^0.28.9",
110
+ "prettier": "^3.7.4"
111
+ }
112
+ }
113
+ ```
114
+
115
+ #### `tsconfig.json`
116
+
117
+ ```json
118
+ {
119
+ "compilerOptions": {
120
+ "module": "esnext",
121
+ "declaration": true,
122
+ "target": "esnext",
123
+ "noImplicitAny": true,
124
+ "removeComments": true,
125
+ "suppressImplicitAnyIndexErrors": false,
126
+ "preserveConstEnums": true,
127
+ "resolveJsonModule": true,
128
+ "skipLibCheck": false,
129
+ "sourceMap": false,
130
+ "moduleDetection": "force",
131
+ "experimentalDecorators": true,
132
+ "lib": ["es6"],
133
+ "strict": true,
134
+ "strictNullChecks": true,
135
+ "strictFunctionTypes": true,
136
+ "strictBindCallApply": true,
137
+ "strictPropertyInitialization": true,
138
+ "alwaysStrict": true,
139
+ "moduleResolution": "node",
140
+ "allowJs": false,
141
+ "incremental": true,
142
+ "allowSyntheticDefaultImports": true,
143
+ "outDir": "build"
144
+ },
145
+ "include": [
146
+ "./tests/*.ts",
147
+ "./tests/**/*.ts"
148
+ ]
149
+ }
150
+ ```
151
+
152
+ #### `src/tsconfig.json`
153
+
154
+ ```json
155
+ {
156
+ "extends": "@btc-vision/opnet-transform/std/assembly.json",
157
+ "include": [
158
+ "./**/*.ts"
159
+ ]
160
+ }
161
+ ```
162
+
163
+ #### `.prettierrc.json`
164
+
165
+ ```json
166
+ {
167
+ "printWidth": 100,
168
+ "trailingComma": "all",
169
+ "tabWidth": 4,
170
+ "semi": true,
171
+ "singleQuote": true,
172
+ "quoteProps": "as-needed",
173
+ "bracketSpacing": true,
174
+ "bracketSameLine": true,
175
+ "arrowParens": "always",
176
+ "singleAttributePerLine": true
177
+ }
178
+ ```
179
+
180
+ #### `.vscode/settings.json`
181
+
182
+ ```json
183
+ {
184
+ "eslint.validate": ["javascript", "typescript"],
185
+ "prettier.useEditorConfig": false,
186
+ "prettier.useTabs": false,
187
+ "prettier.configPath": ".prettierrc.json",
188
+ "prettier.requireConfig": true,
189
+ "prettier.tabWidth": 4,
190
+ "prettier.singleQuote": true,
191
+ "editor.formatOnSave": true
192
+ }
193
+ ```
194
+
195
+ #### `.gitignore`
196
+
197
+ ```gitignore
198
+ # Dependencies
199
+ node_modules/
200
+ package-lock.json
201
+
202
+ # Build output
203
+ build/
204
+ debug/
205
+
206
+ # IDE
207
+ .idea/
208
+ .vs/
209
+ .vscode-test/
210
+
211
+ # Logs
212
+ *.log
213
+ npm-debug.log*
214
+
215
+ # TypeScript
216
+ *.tsbuildinfo
217
+
218
+ # Environment
219
+ .env
220
+ .env.local
221
+
222
+ # Keys
223
+ *.key
224
+ *.wallet
225
+ ```
226
+
227
+ ### 4. Create Project Structure
228
+
229
+ ```bash
230
+ mkdir -p src/token
231
+ ```
232
+
233
+ #### `src/token/MyToken.ts`
234
+
235
+ ```typescript
236
+ import { u256 } from '@btc-vision/as-bignum/assembly';
237
+ import {
238
+ Blockchain,
239
+ BytesWriter,
240
+ Calldata,
241
+ OP20,
242
+ OP20InitParameters,
243
+ } from '@btc-vision/btc-runtime/runtime';
244
+
245
+ @final
246
+ export class MyToken extends OP20 {
247
+ public constructor() {
248
+ super();
249
+ }
250
+
251
+ public override onDeployment(_calldata: Calldata): void {
252
+ const maxSupply: u256 = u256.fromString('1000000000000000000000000'); // 1 million tokens with 18 decimals
253
+ const decimals: u8 = 18;
254
+ const name: string = 'MyToken';
255
+ const symbol: string = 'MTK';
256
+
257
+ this.instantiate(new OP20InitParameters(maxSupply, decimals, name, symbol));
258
+
259
+ // Mint initial supply to deployer
260
+ this._mint(Blockchain.tx.origin, maxSupply);
261
+ }
262
+ }
263
+ ```
264
+
265
+ #### `src/token/index.ts`
266
+
267
+ ```typescript
268
+ import { Blockchain } from '@btc-vision/btc-runtime/runtime';
269
+ import { revertOnError } from '@btc-vision/btc-runtime/runtime/abort/abort';
270
+ import { MyToken } from './MyToken';
271
+
272
+ // DO NOT TOUCH TO THIS.
273
+ Blockchain.contract = () => {
274
+ // ONLY CHANGE THE CONTRACT CLASS NAME.
275
+ // DO NOT ADD CUSTOM LOGIC HERE.
276
+
277
+ return new MyToken();
278
+ };
279
+
280
+ // VERY IMPORTANT
281
+ export * from '@btc-vision/btc-runtime/runtime/exports';
282
+
283
+ // VERY IMPORTANT
284
+ export function abort(message: string, fileName: string, line: u32, column: u32): void {
285
+ revertOnError(message, fileName, line, column);
286
+ }
287
+ ```
288
+
289
+ ### 5. Build
290
+
291
+ ```bash
292
+ npm run build:token
293
+ ```
294
+
295
+ If successful, you'll see `build/MyToken.wasm` generated.
296
+
297
+ ## Adding More Contracts
298
+
299
+ To add more contracts (e.g., NFT), add a new target to `asconfig.json`:
300
+
301
+ ```json
302
+ {
303
+ "targets": {
304
+ "token": {
305
+ "outFile": "build/MyToken.wasm",
306
+ "use": ["abort=src/token/index/abort"]
307
+ },
308
+ "nft": {
309
+ "outFile": "build/MyNFT.wasm",
310
+ "use": ["abort=src/nft/index/abort"]
311
+ }
312
+ },
313
+ ...
314
+ }
315
+ ```
316
+
317
+ And add a build script to `package.json`:
318
+
319
+ ```json
320
+ {
321
+ "scripts": {
322
+ "build:token": "asc src/token/index.ts --target token --measure --uncheckedBehavior never",
323
+ "build:nft": "asc src/nft/index.ts --target nft --measure --uncheckedBehavior never"
324
+ }
325
+ }
326
+ ```
327
+
328
+ ## Package Dependencies
329
+
330
+ | Package | Version | Purpose |
331
+ |---------|---------|---------|
332
+ | `@btc-vision/btc-runtime` | ^1.10.8 | Core runtime - contracts, storage, events |
333
+ | `@btc-vision/as-bignum` | ^0.0.6 | 128-bit and 256-bit integer types |
334
+ | `@btc-vision/opnet-transform` | ^0.1.12 | AssemblyScript transform for OPNet |
335
+ | `assemblyscript` | ^0.28.9 | AssemblyScript compiler |
336
+
337
+ ## Troubleshooting
338
+
339
+ ### "Module not found" Errors
340
+
341
+ Ensure your imports use the correct paths:
342
+
343
+ ```typescript
344
+ // Correct
345
+ import { OP_NET } from '@btc-vision/btc-runtime/runtime';
346
+ import { u256 } from '@btc-vision/as-bignum/assembly';
347
+
348
+ // Wrong
349
+ import { OP_NET } from 'btc-runtime'; // Missing @btc-vision scope
350
+ ```
351
+
352
+ ### Missing `src/tsconfig.json`
353
+
354
+ The `src/tsconfig.json` is required and must extend the opnet-transform config:
355
+
356
+ ```json
357
+ {
358
+ "extends": "@btc-vision/opnet-transform/std/assembly.json",
359
+ "include": ["./**/*.ts"]
360
+ }
361
+ ```
362
+
363
+ ### AssemblyScript Version Mismatch
364
+
365
+ Ensure you're using AssemblyScript 0.28.9:
366
+
367
+ ```bash
368
+ npm ls assemblyscript
369
+ # Should show ^0.28.9
370
+ ```
371
+
372
+ ## Next Steps
373
+
374
+ Now that your environment is set up:
375
+
376
+ 1. [Create your first contract](./first-contract.md)
377
+ 2. [Understand the project structure](./project-structure.md)
378
+ 3. [Learn about the blockchain environment](../core-concepts/blockchain-environment.md)
379
+
380
+ ---
381
+
382
+ **Navigation:**
383
+ - Previous: [Documentation Index](../README.md)
384
+ - Next: [First Contract](./first-contract.md)