@btc-vision/btc-runtime 1.1.7 → 1.1.8

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 +211 -197
  49. package/runtime/types/index.ts +8 -8
  50. package/runtime/universal/ABIRegistry.ts +72 -72
@@ -1,48 +1,48 @@
1
- import { u256 } from 'as-bignum/assembly';
2
- import { Blockchain } from '../env';
3
-
4
- @final
5
- export class StoredBoolean {
6
- constructor(
7
- public pointer: u16,
8
- private defaultValue: bool,
9
- ) {}
10
-
11
- private _value: u256 = u256.Zero;
12
-
13
- @inline
14
- public get value(): bool {
15
- this.ensureValue();
16
-
17
- return this._value.toBool();
18
- }
19
-
20
- @inline
21
- public set value(value: bool) {
22
- this._value = value ? u256.One : u256.Zero;
23
-
24
- Blockchain.setStorageAt(this.pointer, u256.Zero, this._value);
25
- }
26
-
27
- @inline
28
- public set(value: u256): this {
29
- this._value = value;
30
-
31
- Blockchain.setStorageAt(this.pointer, u256.Zero, this._value);
32
-
33
- return this;
34
- }
35
-
36
- @inline
37
- public toUint8Array(): Uint8Array {
38
- return this._value.toUint8Array(true);
39
- }
40
-
41
- private ensureValue(): void {
42
- this._value = Blockchain.getStorageAt(
43
- this.pointer,
44
- u256.Zero,
45
- this.defaultValue ? u256.One : u256.Zero,
46
- );
47
- }
48
- }
1
+ import { u256 } from 'as-bignum/assembly';
2
+ import { Blockchain } from '../env';
3
+
4
+ @final
5
+ export class StoredBoolean {
6
+ constructor(
7
+ public pointer: u16,
8
+ private defaultValue: bool,
9
+ ) {}
10
+
11
+ private _value: u256 = u256.Zero;
12
+
13
+ @inline
14
+ public get value(): bool {
15
+ this.ensureValue();
16
+
17
+ return this._value.toBool();
18
+ }
19
+
20
+ @inline
21
+ public set value(value: bool) {
22
+ this._value = value ? u256.One : u256.Zero;
23
+
24
+ Blockchain.setStorageAt(this.pointer, u256.Zero, this._value);
25
+ }
26
+
27
+ @inline
28
+ public set(value: u256): this {
29
+ this._value = value;
30
+
31
+ Blockchain.setStorageAt(this.pointer, u256.Zero, this._value);
32
+
33
+ return this;
34
+ }
35
+
36
+ @inline
37
+ public toUint8Array(): Uint8Array {
38
+ return this._value.toUint8Array(true);
39
+ }
40
+
41
+ private ensureValue(): void {
42
+ this._value = Blockchain.getStorageAt(
43
+ this.pointer,
44
+ u256.Zero,
45
+ this.defaultValue ? u256.One : u256.Zero,
46
+ );
47
+ }
48
+ }
@@ -1,145 +1,145 @@
1
- import { u256 } from 'as-bignum/assembly';
2
- import { Blockchain } from '../env';
3
- import { SafeMath } from '../types/SafeMath';
4
-
5
- @final
6
- export class StoredString {
7
- constructor(
8
- public pointer: u16,
9
- private defaultValue?: string,
10
- ) {}
11
-
12
- private _value: string = '';
13
-
14
- @inline
15
- public get value(): string {
16
- if (!this._value) {
17
- this.load();
18
- }
19
-
20
- return this._value;
21
- }
22
-
23
- @inline
24
- public set value(value: string) {
25
- this._value = value;
26
- this.save();
27
- }
28
-
29
- private min(a: u32, b: u32): u32 {
30
- return a < b ? a : b;
31
- }
32
-
33
- private max(a: u32, b: u32): u32 {
34
- return a > b ? a : b;
35
- }
36
-
37
- private save(): void {
38
- const length: u32 = this._value.length;
39
- if (length == 0) {
40
- return;
41
- }
42
-
43
- if (length > 2048) {
44
- throw new Error('StoredString: value is too long');
45
- }
46
-
47
- // Prepare the header with the length of the string in the first 4 bytes
48
- let header: u256 = u256.fromU32(length);
49
- header = SafeMath.shl(header, 224);
50
-
51
- let currentPointer: u256 = u256.Zero;
52
- let remainingLength: u32 = length;
53
- let offset: u32 = 0;
54
-
55
- // Save the initial chunk (first 28 bytes) in the header
56
- let bytesToWrite: u32 = this.min(remainingLength, 28);
57
- header = this.saveChunk(header, this._value, offset, bytesToWrite, 4);
58
- Blockchain.setStorageAt(this.pointer, currentPointer, header);
59
-
60
- remainingLength -= bytesToWrite;
61
- offset += bytesToWrite;
62
-
63
- // Save the remaining chunks in subsequent storage slots
64
- while (remainingLength > 0) {
65
- bytesToWrite = this.min(remainingLength, 32);
66
- const storageValue: u256 = this.saveChunk(
67
- u256.Zero,
68
- this._value,
69
- offset,
70
- bytesToWrite,
71
- 0,
72
- );
73
- currentPointer = u256.add(currentPointer, u256.One);
74
- Blockchain.setStorageAt(this.pointer, currentPointer, storageValue);
75
-
76
- remainingLength -= bytesToWrite;
77
- offset += bytesToWrite;
78
- }
79
- }
80
-
81
- // Helper method to save a chunk of the string into the storage slot
82
- private saveChunk(
83
- storage: u256,
84
- value: string,
85
- offset: u32,
86
- length: u32,
87
- storageOffset: u32,
88
- ): u256 {
89
- const bytes = storage.toBytes(true);
90
- for (let i: u32 = 0; i < length; i++) {
91
- const index: i32 = i32(offset + i);
92
- bytes[i + storageOffset] = u8(value.charCodeAt(index));
93
- }
94
- return u256.fromBytes(bytes, true);
95
- }
96
-
97
- private load(): void {
98
- const header: u256 = Blockchain.getStorageAt(this.pointer, u256.Zero, u256.Zero);
99
- if (u256.eq(header, u256.Zero)) {
100
- if (this.defaultValue) {
101
- this.value = this.defaultValue;
102
- }
103
-
104
- return;
105
- }
106
-
107
- // the length of the string is stored in the first 4 bytes of the header
108
- const bits: u256 = u256.shr(header, 224);
109
- const length: u32 = bits.toU32();
110
-
111
- // the rest contains the string itself
112
- let currentPointer: u256 = u256.Zero;
113
- let remainingLength: u32 = length;
114
- let currentStorage: u256 = header;
115
-
116
- const bytesToRead: u32 = this.min(remainingLength, 28);
117
- let str: string = this.loadChunk(currentStorage, 4, bytesToRead);
118
- remainingLength -= bytesToRead;
119
-
120
- while (remainingLength > 0) {
121
- // Move to the next storage slot
122
- currentPointer = u256.add(currentPointer, u256.One);
123
- currentStorage = Blockchain.getStorageAt(this.pointer, currentPointer, u256.Zero);
124
-
125
- // Extract the relevant portion of the string from the current storage slot
126
- const bytesToRead: u32 = this.min(remainingLength, 32);
127
- str += this.loadChunk(currentStorage, 0, bytesToRead);
128
-
129
- remainingLength -= bytesToRead;
130
- }
131
-
132
- this._value = str;
133
- }
134
-
135
- private loadChunk(value: u256, offset: u32, length: u32): string {
136
- const bytes = value.toBytes(true);
137
-
138
- let str: string = '';
139
- for (let i: u32 = 0; i < length; i++) {
140
- str += String.fromCharCode(bytes[i + offset]);
141
- }
142
-
143
- return str;
144
- }
145
- }
1
+ import { u256 } from 'as-bignum/assembly';
2
+ import { Blockchain } from '../env';
3
+ import { SafeMath } from '../types/SafeMath';
4
+
5
+ @final
6
+ export class StoredString {
7
+ constructor(
8
+ public pointer: u16,
9
+ private defaultValue?: string,
10
+ ) {}
11
+
12
+ private _value: string = '';
13
+
14
+ @inline
15
+ public get value(): string {
16
+ if (!this._value) {
17
+ this.load();
18
+ }
19
+
20
+ return this._value;
21
+ }
22
+
23
+ @inline
24
+ public set value(value: string) {
25
+ this._value = value;
26
+ this.save();
27
+ }
28
+
29
+ private min(a: u32, b: u32): u32 {
30
+ return a < b ? a : b;
31
+ }
32
+
33
+ private max(a: u32, b: u32): u32 {
34
+ return a > b ? a : b;
35
+ }
36
+
37
+ private save(): void {
38
+ const length: u32 = this._value.length;
39
+ if (length == 0) {
40
+ return;
41
+ }
42
+
43
+ if (length > 2048) {
44
+ throw new Error('StoredString: value is too long');
45
+ }
46
+
47
+ // Prepare the header with the length of the string in the first 4 bytes
48
+ let header: u256 = u256.fromU32(length);
49
+ header = SafeMath.shl(header, 224);
50
+
51
+ let currentPointer: u256 = u256.Zero;
52
+ let remainingLength: u32 = length;
53
+ let offset: u32 = 0;
54
+
55
+ // Save the initial chunk (first 28 bytes) in the header
56
+ let bytesToWrite: u32 = this.min(remainingLength, 28);
57
+ header = this.saveChunk(header, this._value, offset, bytesToWrite, 4);
58
+ Blockchain.setStorageAt(this.pointer, currentPointer, header);
59
+
60
+ remainingLength -= bytesToWrite;
61
+ offset += bytesToWrite;
62
+
63
+ // Save the remaining chunks in subsequent storage slots
64
+ while (remainingLength > 0) {
65
+ bytesToWrite = this.min(remainingLength, 32);
66
+ const storageValue: u256 = this.saveChunk(
67
+ u256.Zero,
68
+ this._value,
69
+ offset,
70
+ bytesToWrite,
71
+ 0,
72
+ );
73
+ currentPointer = u256.add(currentPointer, u256.One);
74
+ Blockchain.setStorageAt(this.pointer, currentPointer, storageValue);
75
+
76
+ remainingLength -= bytesToWrite;
77
+ offset += bytesToWrite;
78
+ }
79
+ }
80
+
81
+ // Helper method to save a chunk of the string into the storage slot
82
+ private saveChunk(
83
+ storage: u256,
84
+ value: string,
85
+ offset: u32,
86
+ length: u32,
87
+ storageOffset: u32,
88
+ ): u256 {
89
+ const bytes = storage.toBytes(true);
90
+ for (let i: u32 = 0; i < length; i++) {
91
+ const index: i32 = i32(offset + i);
92
+ bytes[i + storageOffset] = u8(value.charCodeAt(index));
93
+ }
94
+ return u256.fromBytes(bytes, true);
95
+ }
96
+
97
+ private load(): void {
98
+ const header: u256 = Blockchain.getStorageAt(this.pointer, u256.Zero, u256.Zero);
99
+ if (u256.eq(header, u256.Zero)) {
100
+ if (this.defaultValue) {
101
+ this.value = this.defaultValue;
102
+ }
103
+
104
+ return;
105
+ }
106
+
107
+ // the length of the string is stored in the first 4 bytes of the header
108
+ const bits: u256 = u256.shr(header, 224);
109
+ const length: u32 = bits.toU32();
110
+
111
+ // the rest contains the string itself
112
+ let currentPointer: u256 = u256.Zero;
113
+ let remainingLength: u32 = length;
114
+ let currentStorage: u256 = header;
115
+
116
+ const bytesToRead: u32 = this.min(remainingLength, 28);
117
+ let str: string = this.loadChunk(currentStorage, 4, bytesToRead);
118
+ remainingLength -= bytesToRead;
119
+
120
+ while (remainingLength > 0) {
121
+ // Move to the next storage slot
122
+ currentPointer = u256.add(currentPointer, u256.One);
123
+ currentStorage = Blockchain.getStorageAt(this.pointer, currentPointer, u256.Zero);
124
+
125
+ // Extract the relevant portion of the string from the current storage slot
126
+ const bytesToRead: u32 = this.min(remainingLength, 32);
127
+ str += this.loadChunk(currentStorage, 0, bytesToRead);
128
+
129
+ remainingLength -= bytesToRead;
130
+ }
131
+
132
+ this._value = str;
133
+ }
134
+
135
+ private loadChunk(value: u256, offset: u32, length: u32): string {
136
+ const bytes = value.toBytes(true);
137
+
138
+ let str: string = '';
139
+ for (let i: u32 = 0; i < length; i++) {
140
+ str += String.fromCharCode(bytes[i + offset]);
141
+ }
142
+
143
+ return str;
144
+ }
145
+ }