@btc-vision/btc-runtime 1.0.30 → 1.1.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.
Files changed (46) hide show
  1. package/package.json +1 -1
  2. package/runtime/buffer/BytesReader.ts +200 -200
  3. package/runtime/buffer/BytesWriter.ts +346 -346
  4. package/runtime/contracts/DeployableOP_20.ts +409 -0
  5. package/runtime/contracts/OP_20.ts +8 -341
  6. package/runtime/contracts/OP_NET.ts +73 -73
  7. package/runtime/contracts/interfaces/IOP_20.ts +21 -21
  8. package/runtime/env/BTCEnvironment.ts +319 -319
  9. package/runtime/env/index.ts +3 -3
  10. package/runtime/events/NetEvent.ts +27 -27
  11. package/runtime/events/predefined/ApproveEvent.ts +16 -16
  12. package/runtime/events/predefined/BurnEvent.ts +13 -13
  13. package/runtime/events/predefined/ClaimEvent.ts +13 -13
  14. package/runtime/events/predefined/MintEvent.ts +15 -15
  15. package/runtime/events/predefined/StakeEvent.ts +13 -13
  16. package/runtime/events/predefined/TransferEvent.ts +16 -16
  17. package/runtime/events/predefined/UnstakeEvent.ts +13 -13
  18. package/runtime/events/predefined/index.ts +7 -7
  19. package/runtime/exports/index.ts +37 -37
  20. package/runtime/generic/Map.ts +65 -65
  21. package/runtime/generic/MapU256.ts +57 -57
  22. package/runtime/index.ts +56 -53
  23. package/runtime/interfaces/DeployContractResponse.ts +12 -12
  24. package/runtime/interfaces/IBTC.ts +6 -6
  25. package/runtime/lang/Definitions.ts +1 -1
  26. package/runtime/math/abi.ts +37 -37
  27. package/runtime/math/bytes.ts +34 -34
  28. package/runtime/math/cyrb53.ts +46 -46
  29. package/runtime/math/rnd.ts +51 -51
  30. package/runtime/math/sha256.ts +12 -12
  31. package/runtime/memory/AddressMemoryMap.ts +44 -44
  32. package/runtime/memory/KeyMerger.ts +53 -53
  33. package/runtime/memory/MemorySlot.ts +1 -1
  34. package/runtime/memory/MemorySlotPointer.ts +3 -3
  35. package/runtime/memory/MultiAddressMemoryMap.ts +62 -62
  36. package/runtime/shared-libraries/OP20Utils.ts +21 -0
  37. package/runtime/shared-libraries/TransferHelper.ts +64 -64
  38. package/runtime/storage/Serializable.ts +6 -2
  39. package/runtime/storage/StoredBoolean.ts +48 -0
  40. package/runtime/storage/StoredString.ts +145 -145
  41. package/runtime/storage/StoredU256.ts +248 -246
  42. package/runtime/types/Address.ts +5 -5
  43. package/runtime/types/Revert.ts +5 -5
  44. package/runtime/types/SafeMath.ts +197 -197
  45. package/runtime/types/index.ts +8 -8
  46. package/runtime/universal/ABIRegistry.ts +72 -72
@@ -1,246 +1,248 @@
1
- import { u256 } from 'as-bignum/assembly';
2
- import { SafeMath } from '../types/SafeMath';
3
- import { MemorySlotPointer } from '../memory/MemorySlotPointer';
4
- import { Blockchain } from '../env';
5
-
6
- @final
7
- export class StoredU256 {
8
- constructor(
9
- public pointer: u16,
10
- public subPointer: MemorySlotPointer,
11
- private defaultValue: u256,
12
- ) {}
13
-
14
- private _value: u256 = u256.Zero;
15
-
16
- @inline
17
- public get value(): u256 {
18
- this.ensureValue();
19
-
20
- return this._value;
21
- }
22
-
23
- @inline
24
- public set value(value: u256) {
25
- this._value = value;
26
-
27
- Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
28
- }
29
-
30
- @inline
31
- public get toBytes(): Uint8Array {
32
- return this._value.toUint8Array(false);
33
- }
34
-
35
- @inline
36
- @operator('+')
37
- public add(value: u256): this {
38
- this.ensureValue();
39
-
40
- this._value = SafeMath.add(this._value, value);
41
- Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
42
-
43
- return this;
44
- }
45
-
46
- @inline
47
- @operator('-')
48
- public sub(value: u256): this {
49
- this.ensureValue();
50
-
51
- this._value = SafeMath.sub(this._value, value);
52
- Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
53
-
54
- return this;
55
- }
56
-
57
- @inline
58
- @operator('*')
59
- public mul(value: u256): this {
60
- this.ensureValue();
61
-
62
- this._value = SafeMath.mul(this._value, value);
63
- Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
64
-
65
- return this;
66
- }
67
-
68
- @inline
69
- @operator('==')
70
- public eq(value: u256): boolean {
71
- this.ensureValue();
72
-
73
- return this._value === value;
74
- }
75
-
76
- @inline
77
- @operator('!=')
78
- public ne(value: u256): boolean {
79
- this.ensureValue();
80
-
81
- return this._value !== value;
82
- }
83
-
84
- @inline
85
- @operator('<')
86
- public lt(value: u256): boolean {
87
- this.ensureValue();
88
-
89
- return this._value < value;
90
- }
91
-
92
- @inline
93
- @operator('>')
94
- public gt(value: u256): boolean {
95
- this.ensureValue();
96
-
97
- return this._value > value;
98
- }
99
-
100
- @inline
101
- @operator('<=')
102
- public le(value: u256): boolean {
103
- this.ensureValue();
104
-
105
- return this._value <= value;
106
- }
107
-
108
- @inline
109
- @operator('>=')
110
- public ge(value: u256): boolean {
111
- this.ensureValue();
112
-
113
- return this._value >= value;
114
- }
115
-
116
- @inline
117
- @operator('>>')
118
- public shr(value: i32): this {
119
- this.ensureValue();
120
-
121
- this._value = u256.shr(this._value, value);
122
- Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
123
-
124
- return this;
125
- }
126
-
127
- @inline
128
- @operator('&')
129
- public and(value: u256): this {
130
- this.ensureValue();
131
-
132
- this._value = u256.and(this._value, value);
133
- Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
134
-
135
- return this;
136
- }
137
-
138
- @inline
139
- @operator('|')
140
- public or(value: u256): this {
141
- this.ensureValue();
142
-
143
- this._value = u256.or(this._value, value);
144
- Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
145
-
146
- return this;
147
- }
148
-
149
- @inline
150
- @operator('^')
151
- public xor(value: u256): this {
152
- this.ensureValue();
153
-
154
- this._value = u256.xor(this._value, value);
155
- Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
156
-
157
- return this;
158
- }
159
-
160
- @inline
161
- @operator('**')
162
- public pow(value: u256): this {
163
- this.ensureValue();
164
-
165
- // code pow from scratch
166
- let result: u256 = u256.One;
167
-
168
- while (value > u256.Zero) {
169
- if (u256.and(value, u256.One)) {
170
- result = SafeMath.mul(result, this._value);
171
- }
172
-
173
- this._value = SafeMath.mul(this._value, this._value);
174
- value = u256.shr(value, 1);
175
- }
176
-
177
- Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
178
-
179
- return this;
180
- }
181
-
182
- @inline
183
- @operator('%')
184
- public mod(value: u256): this {
185
- this.ensureValue();
186
-
187
- // code mod from scratch
188
- let result: u256 = u256.Zero;
189
- let base: u256 = this._value;
190
- let exp: u256 = value;
191
-
192
- while (exp > u256.Zero) {
193
- if (u256.and(exp, u256.One)) {
194
- result = SafeMath.add(result, base);
195
- }
196
-
197
- base = SafeMath.add(base, base);
198
- exp = u256.shr(exp, 1);
199
- }
200
-
201
- this._value = result;
202
- Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
203
-
204
- return this;
205
- }
206
-
207
- @inline
208
- @operator.postfix('++')
209
- public inc(): this {
210
- this.ensureValue();
211
-
212
- this._value = SafeMath.add(this._value, u256.One);
213
- Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
214
-
215
- return this;
216
- }
217
-
218
- @inline
219
- @operator.postfix('--')
220
- public dec(): this {
221
- this.ensureValue();
222
-
223
- this._value = SafeMath.sub(this._value, u256.One);
224
- Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
225
-
226
- return this;
227
- }
228
-
229
- @inline
230
- public set(value: u256): this {
231
- this._value = value;
232
-
233
- Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
234
-
235
- return this;
236
- }
237
-
238
- @inline
239
- public toUint8Array(): Uint8Array {
240
- return this._value.toUint8Array(true);
241
- }
242
-
243
- private ensureValue(): void {
244
- this._value = Blockchain.getStorageAt(this.pointer, this.subPointer, this.defaultValue);
245
- }
246
- }
1
+ import { u256 } from 'as-bignum/assembly';
2
+ import { SafeMath } from '../types/SafeMath';
3
+ import { MemorySlotPointer } from '../memory/MemorySlotPointer';
4
+ import { Blockchain } from '../env';
5
+
6
+ @final
7
+ export class StoredU256 {
8
+ constructor(
9
+ public pointer: u16,
10
+ public subPointer: MemorySlotPointer,
11
+ private defaultValue: u256,
12
+ ) {}
13
+
14
+ private _value: u256 = u256.Zero;
15
+
16
+ @inline
17
+ public get value(): u256 {
18
+ this.ensureValue();
19
+
20
+ return this._value;
21
+ }
22
+
23
+ @inline
24
+ public set value(value: u256) {
25
+ if (u256.eq(value, this._value)) return;
26
+
27
+ this._value = value;
28
+
29
+ Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
30
+ }
31
+
32
+ @inline
33
+ public get toBytes(): Uint8Array {
34
+ return this._value.toUint8Array(false);
35
+ }
36
+
37
+ @inline
38
+ @operator('+')
39
+ public add(value: u256): this {
40
+ this.ensureValue();
41
+
42
+ this._value = SafeMath.add(this._value, value);
43
+ Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
44
+
45
+ return this;
46
+ }
47
+
48
+ @inline
49
+ @operator('-')
50
+ public sub(value: u256): this {
51
+ this.ensureValue();
52
+
53
+ this._value = SafeMath.sub(this._value, value);
54
+ Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
55
+
56
+ return this;
57
+ }
58
+
59
+ @inline
60
+ @operator('*')
61
+ public mul(value: u256): this {
62
+ this.ensureValue();
63
+
64
+ this._value = SafeMath.mul(this._value, value);
65
+ Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
66
+
67
+ return this;
68
+ }
69
+
70
+ @inline
71
+ @operator('==')
72
+ public eq(value: u256): boolean {
73
+ this.ensureValue();
74
+
75
+ return this._value === value;
76
+ }
77
+
78
+ @inline
79
+ @operator('!=')
80
+ public ne(value: u256): boolean {
81
+ this.ensureValue();
82
+
83
+ return this._value !== value;
84
+ }
85
+
86
+ @inline
87
+ @operator('<')
88
+ public lt(value: u256): boolean {
89
+ this.ensureValue();
90
+
91
+ return this._value < value;
92
+ }
93
+
94
+ @inline
95
+ @operator('>')
96
+ public gt(value: u256): boolean {
97
+ this.ensureValue();
98
+
99
+ return this._value > value;
100
+ }
101
+
102
+ @inline
103
+ @operator('<=')
104
+ public le(value: u256): boolean {
105
+ this.ensureValue();
106
+
107
+ return this._value <= value;
108
+ }
109
+
110
+ @inline
111
+ @operator('>=')
112
+ public ge(value: u256): boolean {
113
+ this.ensureValue();
114
+
115
+ return this._value >= value;
116
+ }
117
+
118
+ @inline
119
+ @operator('>>')
120
+ public shr(value: i32): this {
121
+ this.ensureValue();
122
+
123
+ this._value = u256.shr(this._value, value);
124
+ Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
125
+
126
+ return this;
127
+ }
128
+
129
+ @inline
130
+ @operator('&')
131
+ public and(value: u256): this {
132
+ this.ensureValue();
133
+
134
+ this._value = u256.and(this._value, value);
135
+ Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
136
+
137
+ return this;
138
+ }
139
+
140
+ @inline
141
+ @operator('|')
142
+ public or(value: u256): this {
143
+ this.ensureValue();
144
+
145
+ this._value = u256.or(this._value, value);
146
+ Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
147
+
148
+ return this;
149
+ }
150
+
151
+ @inline
152
+ @operator('^')
153
+ public xor(value: u256): this {
154
+ this.ensureValue();
155
+
156
+ this._value = u256.xor(this._value, value);
157
+ Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
158
+
159
+ return this;
160
+ }
161
+
162
+ @inline
163
+ @operator('**')
164
+ public pow(value: u256): this {
165
+ this.ensureValue();
166
+
167
+ // code pow from scratch
168
+ let result: u256 = u256.One;
169
+
170
+ while (value > u256.Zero) {
171
+ if (u256.and(value, u256.One)) {
172
+ result = SafeMath.mul(result, this._value);
173
+ }
174
+
175
+ this._value = SafeMath.mul(this._value, this._value);
176
+ value = u256.shr(value, 1);
177
+ }
178
+
179
+ Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
180
+
181
+ return this;
182
+ }
183
+
184
+ @inline
185
+ @operator('%')
186
+ public mod(value: u256): this {
187
+ this.ensureValue();
188
+
189
+ // code mod from scratch
190
+ let result: u256 = u256.Zero;
191
+ let base: u256 = this._value;
192
+ let exp: u256 = value;
193
+
194
+ while (exp > u256.Zero) {
195
+ if (u256.and(exp, u256.One)) {
196
+ result = SafeMath.add(result, base);
197
+ }
198
+
199
+ base = SafeMath.add(base, base);
200
+ exp = u256.shr(exp, 1);
201
+ }
202
+
203
+ this._value = result;
204
+ Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
205
+
206
+ return this;
207
+ }
208
+
209
+ @inline
210
+ @operator.postfix('++')
211
+ public inc(): this {
212
+ this.ensureValue();
213
+
214
+ this._value = SafeMath.add(this._value, u256.One);
215
+ Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
216
+
217
+ return this;
218
+ }
219
+
220
+ @inline
221
+ @operator.postfix('--')
222
+ public dec(): this {
223
+ this.ensureValue();
224
+
225
+ this._value = SafeMath.sub(this._value, u256.One);
226
+ Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
227
+
228
+ return this;
229
+ }
230
+
231
+ @inline
232
+ public set(value: u256): this {
233
+ this._value = value;
234
+
235
+ Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
236
+
237
+ return this;
238
+ }
239
+
240
+ @inline
241
+ public toUint8Array(): Uint8Array {
242
+ return this._value.toUint8Array(true);
243
+ }
244
+
245
+ private ensureValue(): void {
246
+ this._value = Blockchain.getStorageAt(this.pointer, this.subPointer, this.defaultValue);
247
+ }
248
+ }
@@ -1,5 +1,5 @@
1
- import { Potential } from '../lang/Definitions';
2
-
3
- export const ADDRESS_BYTE_LENGTH: u8 = 66;
4
- export declare type Address = string;
5
- export declare type PotentialAddress = Potential<Address>;
1
+ import { Potential } from '../lang/Definitions';
2
+
3
+ export const ADDRESS_BYTE_LENGTH: u8 = 66;
4
+ export declare type Address = string;
5
+ export declare type PotentialAddress = Potential<Address>;
@@ -1,5 +1,5 @@
1
- export class Revert extends Error {
2
- constructor(msg: string = '') {
3
- super(`Execution reverted ${msg}`);
4
- }
5
- }
1
+ export class Revert extends Error {
2
+ constructor(msg: string = '') {
3
+ super(`Execution reverted ${msg}`);
4
+ }
5
+ }