@btc-vision/btc-runtime 1.10.8 → 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.
- package/LICENSE +190 -0
- package/README.md +258 -137
- package/SECURITY.md +226 -0
- package/docs/README.md +614 -0
- package/docs/advanced/bitcoin-scripts.md +939 -0
- package/docs/advanced/cross-contract-calls.md +579 -0
- package/docs/advanced/plugins.md +1006 -0
- package/docs/advanced/quantum-resistance.md +660 -0
- package/docs/advanced/signature-verification.md +715 -0
- package/docs/api-reference/blockchain.md +729 -0
- package/docs/api-reference/events.md +642 -0
- package/docs/api-reference/op20.md +902 -0
- package/docs/api-reference/op721.md +819 -0
- package/docs/api-reference/safe-math.md +510 -0
- package/docs/api-reference/storage.md +840 -0
- package/docs/contracts/op-net-base.md +786 -0
- package/docs/contracts/op20-token.md +687 -0
- package/docs/contracts/op20s-signatures.md +614 -0
- package/docs/contracts/op721-nft.md +785 -0
- package/docs/contracts/reentrancy-guard.md +787 -0
- package/docs/core-concepts/blockchain-environment.md +724 -0
- package/docs/core-concepts/decorators.md +466 -0
- package/docs/core-concepts/events.md +652 -0
- package/docs/core-concepts/pointers.md +391 -0
- package/docs/core-concepts/security.md +473 -0
- package/docs/core-concepts/storage-system.md +969 -0
- package/docs/examples/basic-token.md +745 -0
- package/docs/examples/nft-with-reservations.md +1440 -0
- package/docs/examples/oracle-integration.md +1212 -0
- package/docs/examples/stablecoin.md +1180 -0
- package/docs/getting-started/first-contract.md +575 -0
- package/docs/getting-started/installation.md +384 -0
- package/docs/getting-started/project-structure.md +630 -0
- package/docs/storage/memory-maps.md +764 -0
- package/docs/storage/stored-arrays.md +778 -0
- package/docs/storage/stored-maps.md +758 -0
- package/docs/storage/stored-primitives.md +655 -0
- package/docs/types/address.md +773 -0
- package/docs/types/bytes-writer-reader.md +938 -0
- package/docs/types/calldata.md +744 -0
- package/docs/types/safe-math.md +446 -0
- package/package.json +52 -27
- package/runtime/memory/MapOfMap.ts +1 -0
- package/LICENSE.md +0 -21
package/LICENSE
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright 2025 Orange Pill Labs Holding Ltd.
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -9,192 +9,313 @@
|
|
|
9
9
|
|
|
10
10
|
[](https://github.com/prettier/prettier)
|
|
11
11
|
|
|
12
|
-
##
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
- **
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
-
|
|
40
|
-
|
|
12
|
+
## Overview
|
|
13
|
+
|
|
14
|
+
The **OPNet Smart Contract Runtime** is the foundational framework for building decentralized applications directly on
|
|
15
|
+
Bitcoin Layer 1 (L1). Written in AssemblyScript and compiled to WebAssembly, btc-runtime enables developers to create,
|
|
16
|
+
deploy, and execute smart contracts on the Bitcoin network with the same expressiveness as Ethereum's Solidity.
|
|
17
|
+
|
|
18
|
+
Unlike Bitcoin Layer 2 solutions, OPNet operates directly on Bitcoin's base layer, inheriting Bitcoin's security
|
|
19
|
+
guarantees and decentralization properties while adding programmable smart contract capabilities.
|
|
20
|
+
|
|
21
|
+
> **What is OPNet?**
|
|
22
|
+
>
|
|
23
|
+
> OPNet (Open Protocol Network) is a consensus-layer built on Bitcoin L1. It allows developers to write smart
|
|
24
|
+
> contracts in AssemblyScript or similar that compile to WebAssembly (WASM) and execute deterministically across all
|
|
25
|
+
> network nodes. Think of it as "Solidity for Bitcoin" - you get the programmability of Ethereum with the security of
|
|
26
|
+
> Bitcoin.
|
|
27
|
+
|
|
28
|
+
> **Why AssemblyScript?**
|
|
29
|
+
>
|
|
30
|
+
> AssemblyScript compiles to WebAssembly, which provides:
|
|
31
|
+
> - **Deterministic execution** across all platforms and nodes
|
|
32
|
+
> - **Near-native performance** for compute-intensive operations
|
|
33
|
+
> - **Memory safety** through WASM's sandboxed environment
|
|
34
|
+
> - **Familiar syntax** for TypeScript/JavaScript developers
|
|
35
|
+
|
|
36
|
+
> **IMPORTANT: Floating-Point Arithmetic is Prohibited**
|
|
37
|
+
>
|
|
38
|
+
> Floating-point arithmetic (`f32`, `f64`) is **strictly prohibited** in blockchain and smart contract environments.
|
|
39
|
+
> Floating-point operations are **non-deterministic** across different CPU architectures, compilers, and platforms due
|
|
40
|
+
> to differences in rounding, precision, and IEEE 754 implementation details.
|
|
41
|
+
>
|
|
42
|
+
> **Always use integer arithmetic** (`u128`, `u256`) for all blockchain computations. For decimal values, use
|
|
43
|
+
> fixed-point representation (e.g., store currency as smallest units like satoshis). This library provides full support
|
|
44
|
+
> for 128-bit and 256-bit integer operations through [@btc-vision/as-bignum](https://github.com/btc-vision/as-bignum).
|
|
45
|
+
|
|
46
|
+
## Security Audit
|
|
47
|
+
|
|
48
|
+
<p align="center">
|
|
49
|
+
<a href="https://verichains.io">
|
|
50
|
+
<img src="https://raw.githubusercontent.com/btc-vision/contract-logo/refs/heads/main/public-assets/verichains.png" alt="Verichains" width="300"/>
|
|
51
|
+
</a>
|
|
52
|
+
</p>
|
|
53
|
+
|
|
54
|
+
<p align="center">
|
|
55
|
+
<a href="https://verichains.io">
|
|
56
|
+
<img src="https://img.shields.io/badge/Security%20Audit-Verichains-4C35E0?style=for-the-badge&logo=data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjQiIGhlaWdodD0iMjQiIHZpZXdCb3g9IjAgMCAyNCAyNCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48cGF0aCBkPSJNMTIgMkw0IDV2Ni41YzAgNS4yNSAzLjQgMTAuMiA4IDExLjUgNC42LTEuMyA4LTYuMjUgOC0xMS41VjVsLTgtM3ptMCAxMC45OVYxOS41Yy0zLjQ1LTEuMTctNS45My00LjgtNi02LjVWNi4zTDEyIDRsMCA4Ljk5eiIgZmlsbD0id2hpdGUiLz48L3N2Zz4=" alt="Audited by Verichains"/>
|
|
57
|
+
</a>
|
|
58
|
+
<a href="./SECURITY.md">
|
|
59
|
+
<img src="https://img.shields.io/badge/Security-Report-22C55E?style=for-the-badge&logo=data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjQiIGhlaWdodD0iMjQiIHZpZXdCb3g9IjAgMCAyNCAyNCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48cGF0aCBkPSJNOSAxNi4xN0w0LjgzIDEybC0xLjQyIDEuNDFMOSAxOSAyMSA3bC0xLjQxLTEuNDFMOSAxNi4xN3oiIGZpbGw9IndoaXRlIi8+PC9zdmc+" alt="Security Report"/>
|
|
60
|
+
</a>
|
|
61
|
+
</p>
|
|
62
|
+
|
|
63
|
+
This runtime has been professionally audited by [**Verichains**](https://verichains.io), a leading blockchain security
|
|
64
|
+
firm. The audit covered all core components including contract standards (OP20, OP721), storage systems, cryptographic
|
|
65
|
+
operations, and security mechanisms.
|
|
66
|
+
|
|
67
|
+
For full details, see [SECURITY.md](./SECURITY.md).
|
|
68
|
+
|
|
69
|
+
## Features
|
|
70
|
+
|
|
71
|
+
| Feature | Description |
|
|
72
|
+
|------------------------------|------------------------------------------------------------------|
|
|
73
|
+
| **Contract Standards** | OP20 (fungible tokens), OP721 (NFTs), OP20S (gasless signatures) |
|
|
74
|
+
| **Storage System** | Pointer-based persistent storage with SHA256 key hashing |
|
|
75
|
+
| **SafeMath** | Overflow/underflow protection for all arithmetic operations |
|
|
76
|
+
| **Reentrancy Protection** | Built-in guards with STANDARD and CALLBACK modes |
|
|
77
|
+
| **Cryptographic Operations** | Schnorr signatures, ML-DSA (quantum-resistant), SHA256 |
|
|
78
|
+
| **Bitcoin Integration** | Transaction parsing, address validation, script building |
|
|
79
|
+
| **Event System** | 352-byte events for state change notifications |
|
|
80
|
+
| **Cross-Contract Calls** | Inter-contract communication with configurable failure handling |
|
|
41
81
|
|
|
42
82
|
## Installation
|
|
43
83
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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.
|
|
84
|
+
```bash
|
|
85
|
+
npm install @btc-vision/btc-runtime
|
|
86
|
+
```
|
|
89
87
|
|
|
90
|
-
##
|
|
88
|
+
## Quick Start
|
|
91
89
|
|
|
92
|
-
###
|
|
90
|
+
### Your First Contract
|
|
93
91
|
|
|
94
|
-
Here
|
|
95
|
-
contract follows the OP20 standard.
|
|
92
|
+
Here's a minimal OP20 token contract to get you started:
|
|
96
93
|
|
|
97
94
|
```typescript
|
|
98
95
|
import { u256 } from '@btc-vision/as-bignum/assembly';
|
|
99
96
|
import {
|
|
100
|
-
Address,
|
|
101
|
-
AddressMap,
|
|
102
97
|
Blockchain,
|
|
103
98
|
BytesWriter,
|
|
104
99
|
Calldata,
|
|
105
100
|
OP20,
|
|
106
101
|
OP20InitParameters,
|
|
107
|
-
SafeMath,
|
|
108
102
|
} from '@btc-vision/btc-runtime/runtime';
|
|
109
103
|
|
|
110
104
|
@final
|
|
111
105
|
export class MyToken extends OP20 {
|
|
112
106
|
public constructor() {
|
|
113
107
|
super();
|
|
114
|
-
|
|
115
|
-
// IMPORTANT. THIS WILL RUN EVERYTIME THE CONTRACT IS INTERACTED WITH. FOR SPECIFIC INITIALIZATION, USE "onDeployment" METHOD.
|
|
108
|
+
// NOTE: Constructor runs on EVERY interaction, not just deployment!
|
|
116
109
|
}
|
|
117
110
|
|
|
118
|
-
//
|
|
111
|
+
// This runs ONCE when the contract is deployed (like Solidity's constructor)
|
|
119
112
|
public override onDeployment(_calldata: Calldata): void {
|
|
120
|
-
const maxSupply: u256 = u256.fromString('
|
|
121
|
-
const decimals: u8 = 18;
|
|
122
|
-
const name: string = '
|
|
123
|
-
const symbol: string = '
|
|
113
|
+
const maxSupply: u256 = u256.fromString('1000000000000000000000000'); // 1 million tokens
|
|
114
|
+
const decimals: u8 = 18;
|
|
115
|
+
const name: string = 'MyToken';
|
|
116
|
+
const symbol: string = 'MTK';
|
|
124
117
|
|
|
125
118
|
this.instantiate(new OP20InitParameters(maxSupply, decimals, name, symbol));
|
|
126
119
|
|
|
127
|
-
//
|
|
128
|
-
|
|
120
|
+
// Mint initial supply to deployer
|
|
121
|
+
this._mint(Blockchain.tx.origin, maxSupply);
|
|
129
122
|
}
|
|
130
123
|
|
|
131
|
-
|
|
132
|
-
{
|
|
133
|
-
name: 'address',
|
|
134
|
-
type: ABIDataTypes.ADDRESS,
|
|
135
|
-
},
|
|
136
|
-
{
|
|
137
|
-
name: 'amount',
|
|
138
|
-
type: ABIDataTypes.UINT256,
|
|
139
|
-
},
|
|
140
|
-
)
|
|
141
|
-
@emit('Minted')
|
|
124
|
+
// Custom mint function (deployer only)
|
|
142
125
|
public mint(calldata: Calldata): BytesWriter {
|
|
143
126
|
this.onlyDeployer(Blockchain.tx.sender);
|
|
144
|
-
|
|
127
|
+
|
|
128
|
+
const to = calldata.readAddress();
|
|
129
|
+
const amount = calldata.readU256();
|
|
130
|
+
this._mint(to, amount);
|
|
131
|
+
|
|
145
132
|
return new BytesWriter(0);
|
|
146
133
|
}
|
|
134
|
+
}
|
|
135
|
+
```
|
|
147
136
|
|
|
148
|
-
|
|
149
|
-
* Mints tokens to the specified addresses.
|
|
150
|
-
*
|
|
151
|
-
* @param calldata Calldata containing an `AddressMap<Address, u256>` to mint to.
|
|
152
|
-
*/
|
|
153
|
-
@method({
|
|
154
|
-
name: 'addressAndAmount',
|
|
155
|
-
type: ABIDataTypes.ADDRESS_UINT256_TUPLE,
|
|
156
|
-
})
|
|
157
|
-
@emit('Minted')
|
|
158
|
-
public airdrop(calldata: Calldata): BytesWriter {
|
|
159
|
-
this.onlyDeployer(Blockchain.tx.sender);
|
|
137
|
+
### Solidity Comparison
|
|
160
138
|
|
|
161
|
-
|
|
162
|
-
const addresses: Address[] = addressAndAmount.keys();
|
|
139
|
+
If you're coming from Solidity/EVM development, here's how OPNet concepts map:
|
|
163
140
|
|
|
164
|
-
|
|
141
|
+
| Solidity/EVM | OPNet/btc-runtime | Notes |
|
|
142
|
+
|-----------------------------|-------------------------------------------|-----------------------------|
|
|
143
|
+
| `contract MyContract` | `class MyContract extends OP_NET` | Base class inheritance |
|
|
144
|
+
| `constructor()` | `onDeployment(calldata)` | Runs once at deployment |
|
|
145
|
+
| `msg.sender` | `Blockchain.tx.sender` | Immediate caller |
|
|
146
|
+
| `tx.origin` | `Blockchain.tx.origin` | Original transaction signer |
|
|
147
|
+
| `block.number` | `Blockchain.block.number` | Current block height |
|
|
148
|
+
| `mapping(address => uint)` | `AddressMemoryMap` + `StoredU256` | Pointer-based storage |
|
|
149
|
+
| `emit Transfer(...)` | `this.emitEvent(new TransferEvent(...))` | Event emission |
|
|
150
|
+
| `ERC20` | `OP20` | Fungible token standard |
|
|
151
|
+
| `ERC721` | `OP721` | Non-fungible token standard |
|
|
152
|
+
| `uint256` | `u256` | 256-bit unsigned integer |
|
|
153
|
+
| `require(condition, "msg")` | `if (!condition) throw new Revert("msg")` | Error handling |
|
|
154
|
+
| `modifier onlyOwner` | `this.onlyDeployer(sender)` | Access control |
|
|
165
155
|
|
|
166
|
-
|
|
167
|
-
const address = addresses[i];
|
|
168
|
-
const amount = addressAndAmount.get(address);
|
|
156
|
+
## Documentation
|
|
169
157
|
|
|
170
|
-
|
|
158
|
+
Comprehensive documentation is available in the [docs/](./docs/) directory:
|
|
171
159
|
|
|
172
|
-
|
|
173
|
-
this.balanceOfMap.set(address, SafeMath.add(currentBalance, amount));
|
|
174
|
-
} else {
|
|
175
|
-
this.balanceOfMap.set(address, amount);
|
|
176
|
-
}
|
|
160
|
+
### Getting Started
|
|
177
161
|
|
|
178
|
-
|
|
162
|
+
- [Installation](./docs/getting-started/installation.md) - Setup and configuration
|
|
163
|
+
- [First Contract](./docs/getting-started/first-contract.md) - Step-by-step tutorial
|
|
164
|
+
- [Project Structure](./docs/getting-started/project-structure.md) - Directory layout
|
|
179
165
|
|
|
180
|
-
|
|
181
|
-
}
|
|
166
|
+
### Core Concepts
|
|
182
167
|
|
|
183
|
-
|
|
168
|
+
- [Blockchain Environment](./docs/core-concepts/blockchain-environment.md) - Runtime context
|
|
169
|
+
- [Storage System](./docs/core-concepts/storage-system.md) - How data persistence works
|
|
170
|
+
- [Pointers](./docs/core-concepts/pointers.md) - Storage key management
|
|
171
|
+
- [Events](./docs/core-concepts/events.md) - State change notifications
|
|
172
|
+
- [Security](./docs/core-concepts/security.md) - Protection mechanisms
|
|
184
173
|
|
|
185
|
-
|
|
174
|
+
### Contract Standards
|
|
175
|
+
|
|
176
|
+
- [OP_NET Base](./docs/contracts/op-net-base.md) - Abstract contract class
|
|
177
|
+
- [OP20 Token](./docs/contracts/op20-token.md) - Fungible token standard
|
|
178
|
+
- [OP20S Signatures](./docs/contracts/op20s-signatures.md) - Gasless approvals
|
|
179
|
+
- [OP721 NFT](./docs/contracts/op721-nft.md) - Non-fungible tokens
|
|
180
|
+
- [ReentrancyGuard](./docs/contracts/reentrancy-guard.md) - Reentrancy protection
|
|
181
|
+
|
|
182
|
+
### Types & Utilities
|
|
183
|
+
|
|
184
|
+
- [Address](./docs/types/address.md) - 32-byte address handling
|
|
185
|
+
- [SafeMath](./docs/types/safe-math.md) - Overflow-safe arithmetic
|
|
186
|
+
- [Calldata](./docs/types/calldata.md) - Input parsing
|
|
187
|
+
- [BytesWriter/Reader](./docs/types/bytes-writer-reader.md) - Serialization
|
|
188
|
+
|
|
189
|
+
### Storage Types
|
|
190
|
+
|
|
191
|
+
- [Stored Primitives](./docs/storage/stored-primitives.md) - Basic value storage
|
|
192
|
+
- [Stored Arrays](./docs/storage/stored-arrays.md) - Array storage
|
|
193
|
+
- [Stored Maps](./docs/storage/stored-maps.md) - Key-value storage
|
|
194
|
+
- [Memory Maps](./docs/storage/memory-maps.md) - In-memory mappings
|
|
195
|
+
|
|
196
|
+
### Advanced Topics
|
|
197
|
+
|
|
198
|
+
- [Cross-Contract Calls](./docs/advanced/cross-contract-calls.md) - Inter-contract communication
|
|
199
|
+
- [Signature Verification](./docs/advanced/signature-verification.md) - Cryptographic operations
|
|
200
|
+
- [Quantum Resistance](./docs/advanced/quantum-resistance.md) - ML-DSA support
|
|
201
|
+
- [Bitcoin Scripts](./docs/advanced/bitcoin-scripts.md) - Script building
|
|
202
|
+
- [Plugins](./docs/advanced/plugins.md) - Extending functionality
|
|
203
|
+
|
|
204
|
+
### Examples
|
|
205
|
+
|
|
206
|
+
- [Basic Token](./docs/examples/basic-token.md) - Simple OP20 implementation
|
|
207
|
+
- [NFT with Reservations](./docs/examples/nft-with-reservations.md) - Advanced NFT
|
|
208
|
+
- [Stablecoin](./docs/examples/stablecoin.md) - Role-based token
|
|
209
|
+
- [Oracle Integration](./docs/examples/oracle-integration.md) - Price feeds
|
|
210
|
+
|
|
211
|
+
### API Reference
|
|
212
|
+
|
|
213
|
+
- [Blockchain](./docs/api-reference/blockchain.md) - Environment methods
|
|
214
|
+
- [OP20](./docs/api-reference/op20.md) - Token standard API
|
|
215
|
+
- [OP721](./docs/api-reference/op721.md) - NFT standard API
|
|
216
|
+
- [SafeMath](./docs/api-reference/safe-math.md) - Math operations
|
|
217
|
+
- [Storage](./docs/api-reference/storage.md) - Storage classes
|
|
218
|
+
- [Events](./docs/api-reference/events.md) - Event classes
|
|
219
|
+
|
|
220
|
+
## Running Tests
|
|
221
|
+
|
|
222
|
+
```bash
|
|
223
|
+
# Run all tests with verbose output
|
|
224
|
+
npm test
|
|
225
|
+
|
|
226
|
+
# Run tests with summary only
|
|
227
|
+
npm run test:ci
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
## Project Structure
|
|
231
|
+
|
|
232
|
+
```
|
|
233
|
+
btc-runtime/
|
|
234
|
+
├── runtime/ # Core runtime library
|
|
235
|
+
│ ├── contracts/ # Contract base classes (OP_NET, OP20, OP721)
|
|
236
|
+
│ ├── storage/ # Storage types (Stored*, Maps)
|
|
237
|
+
│ ├── math/ # SafeMath operations
|
|
238
|
+
│ ├── types/ # Core types (Address, Calldata)
|
|
239
|
+
│ ├── events/ # Event system
|
|
240
|
+
│ └── env/ # Blockchain environment
|
|
241
|
+
├── docs/ # Documentation
|
|
242
|
+
└── tests/ # Test suite
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
## Key Differences from Solidity
|
|
246
|
+
|
|
247
|
+
### 1. Constructor Behavior
|
|
248
|
+
|
|
249
|
+
```typescript
|
|
250
|
+
// OPNet: Constructor runs EVERY time
|
|
251
|
+
export class MyContract extends OP_NET {
|
|
252
|
+
constructor() {
|
|
253
|
+
super();
|
|
254
|
+
// DON'T put initialization here - it runs on every call!
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// Use this for one-time initialization (like Solidity constructor)
|
|
258
|
+
public override onDeployment(calldata: Calldata): void {
|
|
259
|
+
// Initialize storage here
|
|
186
260
|
}
|
|
187
261
|
}
|
|
188
262
|
```
|
|
189
263
|
|
|
190
|
-
|
|
264
|
+
### 2. Storage is Explicit
|
|
191
265
|
|
|
192
|
-
|
|
266
|
+
```typescript
|
|
267
|
+
// Solidity: Implicit storage
|
|
268
|
+
// mapping(address => uint256) balances;
|
|
269
|
+
|
|
270
|
+
// OPNet: Explicit pointer allocation
|
|
271
|
+
class Test {
|
|
272
|
+
private balancePointer: u16 = Blockchain.nextPointer;
|
|
273
|
+
private balances: AddressMemoryMap<Address, StoredU256> = new AddressMemoryMap(
|
|
274
|
+
this.balancePointer,
|
|
275
|
+
u256.Zero
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
```
|
|
193
279
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
280
|
+
### 3. Integer Types
|
|
281
|
+
|
|
282
|
+
```typescript
|
|
283
|
+
// OPNet uses u256 from as-bignum (NOT native BigInt)
|
|
284
|
+
import { u256 } from '@btc-vision/as-bignum/assembly';
|
|
285
|
+
|
|
286
|
+
const a = u256.from(100);
|
|
287
|
+
const b = u256.from(50);
|
|
288
|
+
const sum = SafeMath.add(a, b); // Always use SafeMath!
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
### 4. No Floating Point
|
|
292
|
+
|
|
293
|
+
```typescript
|
|
294
|
+
// WRONG - Non-deterministic!
|
|
295
|
+
// const price: f64 = 1.5;
|
|
296
|
+
|
|
297
|
+
// CORRECT - Fixed-point with integers
|
|
298
|
+
const PRECISION: u256 = u256.fromU64(1_000_000); // 6 decimals
|
|
299
|
+
const price: u256 = SafeMath.mul(amount, PRECISION);
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
## Contributing
|
|
303
|
+
|
|
304
|
+
Contributions are welcome! Please ensure all tests pass before submitting a pull request.
|
|
305
|
+
|
|
306
|
+
```bash
|
|
307
|
+
npm test
|
|
308
|
+
```
|
|
197
309
|
|
|
198
310
|
## License
|
|
199
311
|
|
|
200
|
-
This project is licensed under the
|
|
312
|
+
This project is licensed under the Apache-2.0 License. See [LICENSE](./LICENSE) for details.
|
|
313
|
+
|
|
314
|
+
## Links
|
|
315
|
+
|
|
316
|
+
- **Website**: [OPNet](https://opnet.org)
|
|
317
|
+
- **Documentation**: [docs/](./docs/)
|
|
318
|
+
- **Security**: [SECURITY.md](./SECURITY.md)
|
|
319
|
+
- **GitHub**: [btc-vision/btc-runtime](https://github.com/btc-vision/btc-runtime)
|
|
320
|
+
- **Issues**: [GitHub Issues](https://github.com/btc-vision/btc-runtime/issues)
|
|
321
|
+
- **Auditor**: [Verichains](https://verichains.io)
|