@btc-vision/btc-runtime 1.1.7 → 1.1.9

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 (50) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +186 -186
  3. package/package.json +2 -2
  4. package/runtime/buffer/BytesReader.ts +200 -200
  5. package/runtime/buffer/BytesWriter.ts +262 -350
  6. package/runtime/contracts/DeployableOP_20.ts +407 -402
  7. package/runtime/contracts/OP_20.ts +9 -9
  8. package/runtime/contracts/OP_NET.ts +78 -76
  9. package/runtime/contracts/interfaces/IOP_20.ts +21 -21
  10. package/runtime/contracts/interfaces/OP20InitParameters.ts +15 -15
  11. package/runtime/env/BTCEnvironment.ts +330 -330
  12. package/runtime/env/global.ts +32 -32
  13. package/runtime/env/index.ts +3 -3
  14. package/runtime/events/NetEvent.ts +23 -27
  15. package/runtime/events/predefined/ApproveEvent.ts +16 -16
  16. package/runtime/events/predefined/BurnEvent.ts +13 -13
  17. package/runtime/events/predefined/ClaimEvent.ts +13 -13
  18. package/runtime/events/predefined/MintEvent.ts +15 -15
  19. package/runtime/events/predefined/StakeEvent.ts +13 -13
  20. package/runtime/events/predefined/TransferEvent.ts +16 -16
  21. package/runtime/events/predefined/UnstakeEvent.ts +13 -13
  22. package/runtime/events/predefined/index.ts +7 -7
  23. package/runtime/exports/index.ts +37 -37
  24. package/runtime/generic/Map.ts +65 -65
  25. package/runtime/generic/MapU256.ts +57 -57
  26. package/runtime/index.ts +57 -57
  27. package/runtime/interfaces/DeployContractResponse.ts +12 -12
  28. package/runtime/interfaces/IBTC.ts +6 -6
  29. package/runtime/lang/Definitions.ts +1 -1
  30. package/runtime/math/abi.ts +37 -37
  31. package/runtime/math/bytes.ts +34 -34
  32. package/runtime/math/cyrb53.ts +48 -48
  33. package/runtime/math/rnd.ts +55 -55
  34. package/runtime/math/sha256.ts +12 -12
  35. package/runtime/memory/AddressMemoryMap.ts +44 -44
  36. package/runtime/memory/KeyMerger.ts +53 -53
  37. package/runtime/memory/MemorySlot.ts +1 -1
  38. package/runtime/memory/MemorySlotPointer.ts +3 -3
  39. package/runtime/memory/MultiAddressMemoryMap.ts +62 -62
  40. package/runtime/shared-libraries/OP20Utils.ts +21 -21
  41. package/runtime/shared-libraries/TransferHelper.ts +64 -64
  42. package/runtime/storage/Serializable.ts +79 -79
  43. package/runtime/storage/StoredBoolean.ts +48 -48
  44. package/runtime/storage/StoredString.ts +145 -145
  45. package/runtime/storage/StoredU256.ts +225 -250
  46. package/runtime/types/Address.ts +5 -5
  47. package/runtime/types/Revert.ts +5 -5
  48. package/runtime/types/SafeMath.ts +208 -197
  49. package/runtime/types/index.ts +8 -8
  50. package/runtime/universal/ABIRegistry.ts +72 -72
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2024 btc-vision
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2024 [ORANGE PILLS INC, 2140 S DUPONT HWY CAMDEN, DE 19934]
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,186 +1,186 @@
1
- # OPNet Smart Contract Runtime
2
-
3
- ![Bitcoin](https://img.shields.io/badge/Bitcoin-000?style=for-the-badge&logo=bitcoin&logoColor=white)
4
- ![AssemblyScript](https://img.shields.io/badge/assembly%20script-%23000000.svg?style=for-the-badge&logo=assemblyscript&logoColor=white)
5
- ![TypeScript](https://img.shields.io/badge/TypeScript-007ACC?style=for-the-badge&logo=typescript&logoColor=white)
6
- ![NodeJS](https://img.shields.io/badge/Node%20js-339933?style=for-the-badge&logo=nodedotjs&logoColor=white)
7
- ![WebAssembly](https://img.shields.io/badge/WebAssembly-654FF0?style=for-the-badge&logo=webassembly&logoColor=white)
8
- ![NPM](https://img.shields.io/badge/npm-CB3837?style=for-the-badge&logo=npm&logoColor=white)
9
-
10
- [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
11
-
12
- ## Table of Contents
13
-
14
- 1. [Introduction](#introduction)
15
- 2. [Installation](#installation)
16
- 3. [Core Concepts](#core-concepts)
17
- - [Blockchain Environment](#blockchain-environment)
18
- - [Contracts](#contracts)
19
- - [Events](#events)
20
- - [Pointers and Storage Management](#pointers-and-storage-management)
21
- 4. [Usage Examples](#usage-examples)
22
- 5. [Advanced Topics](#advanced-topics)
23
- 6. [Additional Documentation](#additional-documentation)
24
- 7. [License](#license)
25
-
26
- ## Introduction
27
-
28
- The OPNet Smart Contract Runtime provides the foundational components required for creating smart contracts on Bitcoin
29
- Layer 1 (L1). Written in AssemblyScript, this runtime allows developers to leverage WebAssembly for efficient contract
30
- execution while integrating deeply with Bitcoin's decentralized architecture.
31
-
32
- ### Features
33
-
34
- - **AssemblyScript and WebAssembly:** Efficient and high-performance contract execution using WebAssembly.
35
- - **Bitcoin Integration:** Direct interaction with Bitcoin L1, enabling the creation of decentralized applications that
36
- operate on the Bitcoin network.
37
- - **Comprehensive Storage Management:** Flexible and secure storage management using primary pointers and sub-pointers,
38
- ensuring data integrity through cryptographic proofs.
39
- - **Event Handling:** Sophisticated event system for contract state changes, allowing easy tracking and logging of
40
- contract activities.
41
-
42
- ## Installation
43
-
44
- 1. Clone the repository:
45
- ```bash
46
- git clone https://github.com/btc-vision/btc-runtime.git
47
- ```
48
- 2. Navigate to the repository directory:
49
- ```bash
50
- cd btc-runtime
51
- ```
52
- 3. Install the necessary dependencies:
53
- ```bash
54
- npm install
55
- ```
56
-
57
- ## Core Concepts
58
-
59
- ### Blockchain Environment
60
-
61
- The `Blockchain` object environment is the backbone of the OPNet runtime, providing essential functionality for
62
- interacting with the blockchain, such as managing contract states, handling transactions, and emitting events.
63
-
64
- For more detailed information, see the [Blockchain.md](docs/Blockchain.md) documentation.
65
-
66
- ### Contracts
67
-
68
- Contracts in OPNet are AssemblyScript classes that extend the `OP_NET` base class. The constructor pattern differs from
69
- Solidity's, as it runs every time a contract is instantiated, so developers should not use the constructor for
70
- persistent initialization.
71
-
72
- For a detailed guide on how to structure contracts, refer to the [Contract.md](docs/Contract.md) documentation.
73
-
74
- ### Events
75
-
76
- Events in OPNet allow contracts to emit signals that external observers can listen to. They are crucial for tracking
77
- state changes and interactions within the contract.
78
-
79
- For a comprehensive explanation on how to define and use events, refer to the [Events.md](docs/Events.md) documentation.
80
-
81
- ### Pointers and Storage Management
82
-
83
- Storage in OPNet is managed using a combination of pointers (`u16`) and sub-pointers (`u256`). These are encoded and
84
- hashed to generate unique storage locations that are secure and verifiable. This approach ensures that the data stored
85
- is tamper-proof and can be efficiently accessed.
86
-
87
- For more details on pointers and storage management, see the [Pointers.md](docs/Pointers.md)
88
- and [Storage.md](docs/Storage.md) documentation.
89
-
90
- ## Usage Examples
91
-
92
- ### Creating a Basic Token Contract
93
-
94
- Here is a real-world example of how to create a basic token contract using the OPNet Smart Contract Runtime. This
95
- contract follows the OP20 standard.
96
-
97
- ```typescript
98
- import { u128, u256 } from 'as-bignum/assembly';
99
- import {
100
- Address, Blockchain,
101
- BytesWriter,
102
- Calldata,
103
- encodeSelector,
104
- Map,
105
- OP20InitParameters,
106
- Selector,
107
- } from '@btc-vision/btc-runtime/runtime';
108
- import { DeployableOP_20 } from '@btc-vision/btc-runtime/runtime/contracts/DeployableOP_20';
109
-
110
- @final
111
- export class MyToken extends DeployableOP_20 {
112
- constructor() {
113
- super();
114
-
115
- // DO NOT USE TO DEFINE VARIABLE THAT ARE NOT CONSTANT. SEE "solidityLikeConstructor" BELOW.
116
- }
117
-
118
- // "solidityLikeConstructor" This is a solidity-like constructor. This method will only run once.
119
- public onInstantiated(): void {
120
- if (!this.isInstantiated) {
121
- super.onInstantiated(); // IMPORTANT.
122
-
123
- const maxSupply: u256 = u128.fromString('100000000000000000000000000').toU256(); // Your max supply.
124
- const decimals: u8 = 18; // Your decimals.
125
- const name: string = 'MyToken'; // Your token name.
126
- const symbol: string = 'TOKEN'; // Your token symbol.
127
-
128
- this.instantiate(new OP20InitParameters(maxSupply, decimals, name, symbol));
129
-
130
- // Add your logic here. Eg, minting the initial supply:
131
- // this._mint(Blockchain.origin, maxSupply);
132
- }
133
- }
134
-
135
- public override callMethod(method: Selector, calldata: Calldata): BytesWriter {
136
- switch (method) {
137
- default:
138
- return super.callMethod(method, calldata);
139
- }
140
- }
141
- }
142
- ```
143
-
144
- ### Emitting Events
145
-
146
- ```typescript
147
- class MyContract extends OP_NET {
148
- public someAction(): void {
149
- const event = new CustomEvent(Blockchain.sender, u256.fromU32(100));
150
- this.emitEvent(event);
151
- }
152
- }
153
- ```
154
-
155
- ## Advanced Topics
156
-
157
- ### Storage Management with Cryptographic Proofs
158
-
159
- Storage pointers and sub-pointers are encoded and hashed to create unique and secure storage locations. These storage
160
- locations are managed using the `Blockchain` class's `setStorageAt` and `getStorageAt` methods, ensuring data integrity
161
- and preventing tampering.
162
-
163
- ### Using Serializable Data Structures
164
-
165
- For complex data types, the `Serializable` class allows you to manage and persist data structures across multiple
166
- storage slots.
167
-
168
- ```typescript
169
- class ComplexData extends Serializable {
170
- // Implementation
171
- }
172
- ```
173
-
174
- ## Additional Documentation
175
-
176
- For more detailed explanations on specific topics, refer to the individual documentation files:
177
-
178
- - [Blockchain.md](docs/Blockchain.md)
179
- - [Contract.md](docs/Contract.md)
180
- - [Events.md](docs/Events.md)
181
- - [Pointers.md](docs/Pointers.md)
182
- - [Storage.md](docs/Storage.md)
183
-
184
- ## License
185
-
186
- This project is licensed under the MIT License. View the full license [here](LICENSE.md).
1
+ # OPNet Smart Contract Runtime
2
+
3
+ ![Bitcoin](https://img.shields.io/badge/Bitcoin-000?style=for-the-badge&logo=bitcoin&logoColor=white)
4
+ ![AssemblyScript](https://img.shields.io/badge/assembly%20script-%23000000.svg?style=for-the-badge&logo=assemblyscript&logoColor=white)
5
+ ![TypeScript](https://img.shields.io/badge/TypeScript-007ACC?style=for-the-badge&logo=typescript&logoColor=white)
6
+ ![NodeJS](https://img.shields.io/badge/Node%20js-339933?style=for-the-badge&logo=nodedotjs&logoColor=white)
7
+ ![WebAssembly](https://img.shields.io/badge/WebAssembly-654FF0?style=for-the-badge&logo=webassembly&logoColor=white)
8
+ ![NPM](https://img.shields.io/badge/npm-CB3837?style=for-the-badge&logo=npm&logoColor=white)
9
+
10
+ [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
11
+
12
+ ## Table of Contents
13
+
14
+ 1. [Introduction](#introduction)
15
+ 2. [Installation](#installation)
16
+ 3. [Core Concepts](#core-concepts)
17
+ - [Blockchain Environment](#blockchain-environment)
18
+ - [Contracts](#contracts)
19
+ - [Events](#events)
20
+ - [Pointers and Storage Management](#pointers-and-storage-management)
21
+ 4. [Usage Examples](#usage-examples)
22
+ 5. [Advanced Topics](#advanced-topics)
23
+ 6. [Additional Documentation](#additional-documentation)
24
+ 7. [License](#license)
25
+
26
+ ## Introduction
27
+
28
+ The OPNet Smart Contract Runtime provides the foundational components required for creating smart contracts on Bitcoin
29
+ Layer 1 (L1). Written in AssemblyScript, this runtime allows developers to leverage WebAssembly for efficient contract
30
+ execution while integrating deeply with Bitcoin's decentralized architecture.
31
+
32
+ ### Features
33
+
34
+ - **AssemblyScript and WebAssembly:** Efficient and high-performance contract execution using WebAssembly.
35
+ - **Bitcoin Integration:** Direct interaction with Bitcoin L1, enabling the creation of decentralized applications that
36
+ operate on the Bitcoin network.
37
+ - **Comprehensive Storage Management:** Flexible and secure storage management using primary pointers and sub-pointers,
38
+ ensuring data integrity through cryptographic proofs.
39
+ - **Event Handling:** Sophisticated event system for contract state changes, allowing easy tracking and logging of
40
+ contract activities.
41
+
42
+ ## Installation
43
+
44
+ 1. Clone the repository:
45
+ ```bash
46
+ git clone https://github.com/btc-vision/btc-runtime.git
47
+ ```
48
+ 2. Navigate to the repository directory:
49
+ ```bash
50
+ cd btc-runtime
51
+ ```
52
+ 3. Install the necessary dependencies:
53
+ ```bash
54
+ npm install
55
+ ```
56
+
57
+ ## Core Concepts
58
+
59
+ ### Blockchain Environment
60
+
61
+ The `Blockchain` object environment is the backbone of the OPNet runtime, providing essential functionality for
62
+ interacting with the blockchain, such as managing contract states, handling transactions, and emitting events.
63
+
64
+ For more detailed information, see the [Blockchain.md](docs/Blockchain.md) documentation.
65
+
66
+ ### Contracts
67
+
68
+ Contracts in OPNet are AssemblyScript classes that extend the `OP_NET` base class. The constructor pattern differs from
69
+ Solidity's, as it runs every time a contract is instantiated, so developers should not use the constructor for
70
+ persistent initialization.
71
+
72
+ For a detailed guide on how to structure contracts, refer to the [Contract.md](docs/Contract.md) documentation.
73
+
74
+ ### Events
75
+
76
+ Events in OPNet allow contracts to emit signals that external observers can listen to. They are crucial for tracking
77
+ state changes and interactions within the contract.
78
+
79
+ For a comprehensive explanation on how to define and use events, refer to the [Events.md](docs/Events.md) documentation.
80
+
81
+ ### Pointers and Storage Management
82
+
83
+ Storage in OPNet is managed using a combination of pointers (`u16`) and sub-pointers (`u256`). These are encoded and
84
+ hashed to generate unique storage locations that are secure and verifiable. This approach ensures that the data stored
85
+ is tamper-proof and can be efficiently accessed.
86
+
87
+ For more details on pointers and storage management, see the [Pointers.md](docs/Pointers.md)
88
+ and [Storage.md](docs/Storage.md) documentation.
89
+
90
+ ## Usage Examples
91
+
92
+ ### Creating a Basic Token Contract
93
+
94
+ Here is a real-world example of how to create a basic token contract using the OPNet Smart Contract Runtime. This
95
+ contract follows the OP20 standard.
96
+
97
+ ```typescript
98
+ import { u128, u256 } from 'as-bignum/assembly';
99
+ import {
100
+ Address, Blockchain,
101
+ BytesWriter,
102
+ Calldata,
103
+ encodeSelector,
104
+ Map,
105
+ OP20InitParameters,
106
+ Selector,
107
+ } from '@btc-vision/btc-runtime/runtime';
108
+ import { DeployableOP_20 } from '@btc-vision/btc-runtime/runtime/contracts/DeployableOP_20';
109
+
110
+ @final
111
+ export class MyToken extends DeployableOP_20 {
112
+ constructor() {
113
+ super();
114
+
115
+ // DO NOT USE TO DEFINE VARIABLE THAT ARE NOT CONSTANT. SEE "solidityLikeConstructor" BELOW.
116
+ }
117
+
118
+ // "solidityLikeConstructor" This is a solidity-like constructor. This method will only run once.
119
+ public onInstantiated(): void {
120
+ if (!this.isInstantiated) {
121
+ super.onInstantiated(); // IMPORTANT.
122
+
123
+ const maxSupply: u256 = u128.fromString('100000000000000000000000000').toU256(); // Your max supply.
124
+ const decimals: u8 = 18; // Your decimals.
125
+ const name: string = 'MyToken'; // Your token name.
126
+ const symbol: string = 'TOKEN'; // Your token symbol.
127
+
128
+ this.instantiate(new OP20InitParameters(maxSupply, decimals, name, symbol));
129
+
130
+ // Add your logic here. Eg, minting the initial supply:
131
+ // this._mint(Blockchain.origin, maxSupply);
132
+ }
133
+ }
134
+
135
+ public override callMethod(method: Selector, calldata: Calldata): BytesWriter {
136
+ switch (method) {
137
+ default:
138
+ return super.callMethod(method, calldata);
139
+ }
140
+ }
141
+ }
142
+ ```
143
+
144
+ ### Emitting Events
145
+
146
+ ```typescript
147
+ class MyContract extends OP_NET {
148
+ public someAction(): void {
149
+ const event = new CustomEvent(Blockchain.sender, u256.fromU32(100));
150
+ this.emitEvent(event);
151
+ }
152
+ }
153
+ ```
154
+
155
+ ## Advanced Topics
156
+
157
+ ### Storage Management with Cryptographic Proofs
158
+
159
+ Storage pointers and sub-pointers are encoded and hashed to create unique and secure storage locations. These storage
160
+ locations are managed using the `Blockchain` class's `setStorageAt` and `getStorageAt` methods, ensuring data integrity
161
+ and preventing tampering.
162
+
163
+ ### Using Serializable Data Structures
164
+
165
+ For complex data types, the `Serializable` class allows you to manage and persist data structures across multiple
166
+ storage slots.
167
+
168
+ ```typescript
169
+ class ComplexData extends Serializable {
170
+ // Implementation
171
+ }
172
+ ```
173
+
174
+ ## Additional Documentation
175
+
176
+ For more detailed explanations on specific topics, refer to the individual documentation files:
177
+
178
+ - [Blockchain.md](docs/Blockchain.md)
179
+ - [Contract.md](docs/Contract.md)
180
+ - [Events.md](docs/Events.md)
181
+ - [Pointers.md](docs/Pointers.md)
182
+ - [Storage.md](docs/Storage.md)
183
+
184
+ ## License
185
+
186
+ This project is licensed under the MIT License. View the full license [here](LICENSE.md).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@btc-vision/btc-runtime",
3
- "version": "1.1.7",
3
+ "version": "1.1.9",
4
4
  "description": "Bitcoin Smart Contract Runtime",
5
5
  "main": "btc/index.ts",
6
6
  "types": "btc/index.ts",
@@ -23,7 +23,7 @@
23
23
  },
24
24
  "repository": {
25
25
  "type": "git",
26
- "url": "https://github.com/BlobMaster41/btc-runtime.git"
26
+ "url": "https://github.com/btc-vision/btc-runtime"
27
27
  },
28
28
  "type": "module",
29
29
  "files": [