@btc-vision/btc-runtime 1.2.4 → 1.2.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@btc-vision/btc-runtime",
3
- "version": "1.2.4",
3
+ "version": "1.2.6",
4
4
  "description": "Bitcoin Smart Contract Runtime",
5
5
  "main": "btc/index.ts",
6
6
  "scripts": {
@@ -1,10 +1,11 @@
1
1
  import { Address, ADDRESS_BYTE_LENGTH } from '../types/Address';
2
2
  import { Selector } from '../math/abi';
3
- import { u256 } from 'as-bignum/assembly';
3
+ import { i128, u128, u256 } from 'as-bignum/assembly';
4
4
  import { Revert } from '../types/Revert';
5
5
  import { Map } from '../generic/Map';
6
6
  import { TransactionInput, TransactionOutput } from '../env/classes/UTXO';
7
7
  import { StaticArray } from 'staticarray';
8
+ import { i256 } from '../math/i256';
8
9
 
9
10
  @final
10
11
  export class BytesReader {
@@ -49,12 +50,46 @@ export class BytesReader {
49
50
  }
50
51
 
51
52
  public readU256(): u256 {
53
+ this.verifyEnd(this.currentOffset + 32);
54
+
55
+ const next32Bytes: u8[] = this.readBytesBE(32);
56
+
57
+ return u256.fromBytesBE(next32Bytes);
58
+ }
59
+
60
+ @inline
61
+ public readBytesBE(count: i32): u8[] {
52
62
  const next32Bytes: u8[] = [];
53
- for (let i = 0; i < 32; i++) {
63
+ for (let i = 0; i < count; i++) {
54
64
  next32Bytes[i] = this.readU8();
55
65
  }
56
66
 
57
- return u256.fromBytesBE(next32Bytes);
67
+ return next32Bytes;
68
+ }
69
+
70
+ public readI64(): i64 {
71
+ this.verifyEnd(this.currentOffset + 8);
72
+
73
+ const value = this.buffer.getInt64(this.currentOffset, true);
74
+ this.currentOffset += 8;
75
+
76
+ return value;
77
+ }
78
+
79
+ public readU128(): u128 {
80
+ this.verifyEnd(this.currentOffset + 16);
81
+
82
+ const next16Bytes: u8[] = this.readBytesBE(16);
83
+
84
+ return u128.fromBytesBE(next16Bytes);
85
+ }
86
+
87
+ public readI128(): i128 {
88
+ this.verifyEnd(this.currentOffset + 16);
89
+
90
+ const next16Bytes: u8[] = this.readBytesBE(16);
91
+
92
+ return i128.fromBytesBE(next16Bytes);
58
93
  }
59
94
 
60
95
  public readBytes(length: u32, zeroStop: boolean = false): Uint8Array {
@@ -138,6 +173,14 @@ export class BytesReader {
138
173
  return result;
139
174
  }
140
175
 
176
+ public readI256(): i256 {
177
+ this.verifyEnd(this.currentOffset + 32);
178
+
179
+ const next32Bytes: u8[] = this.readBytesBE(32);
180
+
181
+ return i256.fromBytesBE(next32Bytes);
182
+ }
183
+
141
184
  public readTuple(): u256[] {
142
185
  const length = this.readU32();
143
186
  const result: u256[] = new Array<u256>(length);
@@ -1,10 +1,11 @@
1
- import { u256 } from 'as-bignum/assembly';
1
+ import { i128, u128, u256 } from 'as-bignum/assembly';
2
2
  import { Address, ADDRESS_BYTE_LENGTH } from '../types/Address';
3
3
  import { Selector } from '../math/abi';
4
4
  import { BytesReader } from './BytesReader';
5
5
  import { Revert } from '../types/Revert';
6
6
  import { Map } from '../generic/Map';
7
7
  import { ArrayBuffer } from 'arraybuffer';
8
+ import { i256 } from '../math/i256';
8
9
 
9
10
  @final
10
11
  export class BytesWriter {
@@ -63,6 +64,15 @@ export class BytesWriter {
63
64
  this.writeU8(value ? 1 : 0);
64
65
  }
65
66
 
67
+ public writeI256(value: i256): void {
68
+ this.allocSafe(32);
69
+
70
+ const bytes = value.toUint8Array(true);
71
+ for (let i: i32 = 0; i < 32; i++) {
72
+ this.writeU8(bytes[i] || 0);
73
+ }
74
+ }
75
+
66
76
  public writeU256(value: u256): void {
67
77
  this.allocSafe(32);
68
78
 
@@ -72,6 +82,24 @@ export class BytesWriter {
72
82
  }
73
83
  }
74
84
 
85
+ public writeI128(value: i128): void {
86
+ this.allocSafe(32);
87
+
88
+ const bytes = value.toUint8Array(true);
89
+ for (let i: i32 = 0; i < 32; i++) {
90
+ this.writeU8(bytes[i] || 0);
91
+ }
92
+ }
93
+
94
+ public writeU128(value: u128): void {
95
+ this.allocSafe(32);
96
+
97
+ const bytes = value.toUint8Array(true);
98
+ for (let i: i32 = 0; i < 32; i++) {
99
+ this.writeU8(bytes[i] || 0);
100
+ }
101
+ }
102
+
75
103
  public writeTuple(value: u256[]): void {
76
104
  this.allocSafe(4 + value.length * 32);
77
105
  this.writeU32(u32(value.length));
@@ -3,7 +3,6 @@ import { MemorySlotPointer } from '../memory/MemorySlotPointer';
3
3
  import { MemorySlotData } from '../memory/MemorySlot';
4
4
  import { u256 } from 'as-bignum/assembly';
5
5
  import { BytesReader } from '../buffer/BytesReader';
6
- import { encodePointerHash } from '../math/abi';
7
6
  import { BytesWriter } from '../buffer/BytesWriter';
8
7
  import { NetEvent } from '../events/NetEvent';
9
8
  import { Potential } from '../lang/Definitions';
@@ -210,11 +209,9 @@ export class BlockchainEnvironment {
210
209
  }
211
210
 
212
211
  public getStorageAt(
213
- pointer: u16,
214
- subPointer: MemorySlotPointer,
212
+ pointerHash: MemorySlotPointer,
215
213
  defaultValue: MemorySlotData<u256>,
216
214
  ): MemorySlotData<u256> {
217
- const pointerHash: MemorySlotPointer = encodePointerHash(pointer, subPointer);
218
215
  this.ensureStorageAtPointer(pointerHash, defaultValue);
219
216
 
220
217
  if (this.storage.has(pointerHash)) {
@@ -224,20 +221,14 @@ export class BlockchainEnvironment {
224
221
  return defaultValue;
225
222
  }
226
223
 
227
- public hasStorageAt(pointer: u16, subPointer: MemorySlotPointer): bool {
224
+ public hasStorageAt(pointerHash: MemorySlotPointer): bool {
228
225
  // We mark zero as the default value for the storage, if something is 0, the storage slot get deleted or is non-existent
229
- const val: u256 = this.getStorageAt(pointer, subPointer, u256.Zero);
226
+ const val: u256 = this.getStorageAt(pointerHash, u256.Zero);
230
227
 
231
228
  return u256.ne(val, u256.Zero);
232
229
  }
233
230
 
234
- public setStorageAt(
235
- pointer: u16,
236
- keyPointer: MemorySlotPointer,
237
- value: MemorySlotData<u256>,
238
- ): void {
239
- const pointerHash: u256 = encodePointerHash(pointer, keyPointer);
240
-
231
+ public setStorageAt(pointerHash: MemorySlotPointer, value: MemorySlotData<u256>): void {
241
232
  this._internalSetStorageAt(pointerHash, value);
242
233
  }
243
234
 
package/runtime/index.ts CHANGED
@@ -41,6 +41,7 @@ export * from './math/bytes';
41
41
  export * from './math/cyrb53';
42
42
  export * from './math/sha256';
43
43
  export * from './math/rnd';
44
+ export * from './math/i256';
44
45
 
45
46
  /** Memory */
46
47
  export * from './memory/AddressMemoryMap';
@@ -1,7 +1,6 @@
1
1
  // SO IN TYPESCRIPT, WE CAN NOT USE TWO METHOD WITH THE SAME NAME. SO NOT ADDING THE TYPE TO THE HASH IS A DESIGN CHOICE.
2
2
  import { bytes32, bytes4 } from './bytes';
3
3
  import { MemorySlotPointer } from '../memory/MemorySlotPointer';
4
- import { u256 } from 'as-bignum/assembly';
5
4
  import { Sha256 } from './sha256';
6
5
 
7
6
  export type Selector = u32;
@@ -13,25 +12,8 @@ export function encodeSelector(name: string): Selector {
13
12
  return bytes4(hash);
14
13
  }
15
14
 
16
- export function encodePointer(str: string): MemorySlotPointer {
17
- const typed = Uint8Array.wrap(String.UTF8.encode(str));
15
+ export function encodePointer(typed: Uint8Array): MemorySlotPointer {
18
16
  const hash = Sha256.hash(typed);
19
17
 
20
18
  return bytes32(hash);
21
19
  }
22
-
23
- export function encodePointerHash(pointer: u16, sub: u256): MemorySlotPointer {
24
- const finalBuffer: Uint8Array = new Uint8Array(34);
25
- const mergedKey: u8[] = [u8(pointer & u16(0xff)), u8((pointer >> u16(8)) & u16(0xff))];
26
-
27
- for (let i: i32 = 0; i < mergedKey.length; i++) {
28
- finalBuffer[i] = mergedKey[i];
29
- }
30
-
31
- const subKey = sub.toUint8Array();
32
- for (let i: i32 = 0; i < subKey.length; i++) {
33
- finalBuffer[mergedKey.length + i] = subKey[i];
34
- }
35
-
36
- return bytes32(Sha256.hash(finalBuffer));
37
- }
@@ -0,0 +1,606 @@
1
+ import { i128, u128, u256 } from 'as-bignum/assembly';
2
+
3
+ export class i256 {
4
+
5
+ @inline static get Zero(): i256 { return new i256(); }
6
+ @inline static get One(): i256 { return new i256(1); }
7
+ @inline static get Min(): i256 { return new i256(0, 0, 0, 0x8000000000000000); }
8
+ @inline static get Max(): i256 { return new i256(-1, -1, -1, 0x7FFFFFFFFFFFFFFF); }
9
+
10
+ @inline
11
+ static fromI256(value: i256): i256 {
12
+ return new i256(value.lo1, value.lo2, value.hi1, value.hi2);
13
+ }
14
+
15
+ @inline
16
+ static fromU256(value: u256): i256 {
17
+ return new i256(<i64>value.lo1, <i64>value.lo2, <i64>value.hi1, <i64>value.hi2);
18
+ }
19
+
20
+ @inline
21
+ static fromI128(value: i128): i256 {
22
+ var signExt = value.hi >> 63;
23
+ return new i256(<i64>value.lo, value.hi, signExt, signExt);
24
+ }
25
+
26
+ @inline
27
+ static fromU128(value: u128): i256 {
28
+ return new i256(<i64>value.lo, <i64>value.hi, 0, 0);
29
+ }
30
+
31
+ @inline
32
+ static fromI64(value: i64): i256 {
33
+ var signExt = value >> 63;
34
+ return new i256(value, signExt, signExt, signExt);
35
+ }
36
+
37
+ @inline
38
+ static fromU64(value: u64): i256 {
39
+ return new i256(<i64>value, 0, 0, 0);
40
+ }
41
+
42
+ @inline
43
+ static fromI32(value: i32): i256 {
44
+ var signExt = value >> 31;
45
+ var signExt64 = <i64>signExt;
46
+ return new i256(<i64>value, signExt64, signExt64, signExt64);
47
+ }
48
+
49
+ @inline
50
+ static fromU32(value: u32): i256 {
51
+ return new i256(<i64>value, 0, 0, 0);
52
+ }
53
+
54
+ @inline
55
+ static fromF64(value: f64): i256 {
56
+ var signExt = reinterpret<i64>(value) >> 63;
57
+ return new i256(<i64>value, signExt, signExt, signExt);
58
+ }
59
+
60
+ @inline
61
+ static fromF32(value: f32): i256 {
62
+ var signExt = reinterpret<i32>(value) >> 31;
63
+ var signExt64 = <i64>signExt;
64
+ return new i256(<i64>value, signExt64, signExt64, signExt64);
65
+ }
66
+
67
+ @inline
68
+ static fromBits(
69
+ l0: i32, l1: i32, l2: i32, l3: i32,
70
+ h0: i32, h1: i32, h2: i32, h3: i32,
71
+ ): i256 {
72
+ return new i256(
73
+ <i64>l0 | ((<i64>l1) << 32),
74
+ <i64>l2 | ((<i64>l3) << 32),
75
+ <i64>h0 | ((<i64>h1) << 32),
76
+ <i64>h2 | ((<i64>h3) << 32),
77
+ );
78
+ }
79
+
80
+ @inline
81
+ static fromBytes<T>(array: T, bigEndian: bool = false): i256 {
82
+ if (array instanceof u8[]) {
83
+ return bigEndian
84
+ ? i256.fromBytesBE(<u8[]>array)
85
+ : i256.fromBytesLE(<u8[]>array);
86
+ } else if (array instanceof Uint8Array) {
87
+ return bigEndian
88
+ ? i256.fromUint8ArrayBE(<Uint8Array>array)
89
+ : i256.fromUint8ArrayLE(<Uint8Array>array);
90
+ } else {
91
+ throw new TypeError("Unsupported generic type");
92
+ }
93
+ }
94
+
95
+ @inline
96
+ static fromBytesLE(array: u8[]): i256 {
97
+ assert(array.length && (array.length & 31) == 0);
98
+ var buffer = array.dataStart;
99
+ return new i256(
100
+ load<i64>(buffer, 0 * sizeof<i64>()),
101
+ load<i64>(buffer, 1 * sizeof<i64>()),
102
+ load<i64>(buffer, 2 * sizeof<i64>()),
103
+ load<i64>(buffer, 3 * sizeof<i64>()),
104
+ );
105
+ }
106
+
107
+ @inline
108
+ static fromBytesBE(array: u8[]): i256 {
109
+ assert(array.length && (array.length & 31) == 0);
110
+ var buffer = array.dataStart;
111
+ return new i256(
112
+ bswap<i64>(load<i64>(buffer, 3 * sizeof<i64>())),
113
+ bswap<i64>(load<i64>(buffer, 2 * sizeof<i64>())),
114
+ bswap<i64>(load<i64>(buffer, 1 * sizeof<i64>())),
115
+ bswap<i64>(load<i64>(buffer, 0 * sizeof<i64>())),
116
+ );
117
+ }
118
+
119
+ @inline
120
+ static fromUint8ArrayLE(array: Uint8Array): i256 {
121
+ assert(array.length && (array.length & 31) == 0);
122
+ var buffer = array.dataStart;
123
+ return new i256(
124
+ load<i64>(buffer, 0 * sizeof<i64>()),
125
+ load<i64>(buffer, 1 * sizeof<i64>()),
126
+ load<i64>(buffer, 2 * sizeof<i64>()),
127
+ load<i64>(buffer, 3 * sizeof<i64>()),
128
+ );
129
+ }
130
+
131
+ @inline
132
+ static fromUint8ArrayBE(array: Uint8Array): i256 {
133
+ assert(array.length && (array.length & 31) == 0);
134
+ var buffer = array.dataStart;
135
+ return new i256(
136
+ bswap<i64>(load<i64>(buffer, 3 * sizeof<i64>())),
137
+ bswap<i64>(load<i64>(buffer, 2 * sizeof<i64>())),
138
+ bswap<i64>(load<i64>(buffer, 1 * sizeof<i64>())),
139
+ bswap<i64>(load<i64>(buffer, 0 * sizeof<i64>())),
140
+ );
141
+ }
142
+
143
+ @inline
144
+ static from<T>(value: T): i256 {
145
+ if (value instanceof bool) return i256.fromU64(<u64>value);
146
+ else if (value instanceof i8) return i256.fromI64(<i64>value);
147
+ else if (value instanceof u8) return i256.fromU64(<u64>value);
148
+ else if (value instanceof i16) return i256.fromI64(<i64>value);
149
+ else if (value instanceof u16) return i256.fromU64(<u64>value);
150
+ else if (value instanceof i32) return i256.fromI32(<i32>value);
151
+ else if (value instanceof u32) return i256.fromU32(<u32>value);
152
+ else if (value instanceof i64) return i256.fromI64(<i64>value);
153
+ else if (value instanceof u64) return i256.fromU64(<u64>value);
154
+ else if (value instanceof f32) return i256.fromF64(<f64>value);
155
+ else if (value instanceof f64) return i256.fromF64(<f64>value);
156
+ else if (value instanceof i128) return i256.fromI128(<i128>value);
157
+ else if (value instanceof u128) return i256.fromU128(<u128>value);
158
+ else if (value instanceof i256) return i256.fromI256(<i256>value);
159
+ else if (value instanceof u256) return i256.fromU256(<u256>value);
160
+ else if (value instanceof u8[]) return i256.fromBytes(<u8[]>value);
161
+ else if (value instanceof Uint8Array) return i256.fromBytes(<Uint8Array>value);
162
+ else throw new TypeError("Unsupported generic type");
163
+ }
164
+
165
+ constructor(
166
+ public lo1: i64 = 0,
167
+ public lo2: i64 = 0,
168
+ public hi1: i64 = 0,
169
+ public hi2: i64 = 0,
170
+ ) {}
171
+
172
+ @inline
173
+ set(value: i256): this {
174
+ this.lo1 = value.lo1;
175
+ this.lo2 = value.lo2;
176
+ this.hi1 = value.hi1;
177
+ this.hi2 = value.hi2;
178
+ return this;
179
+ }
180
+
181
+ @inline
182
+ isNeg(): bool {
183
+ return <bool>(this.hi2 >>> 63);
184
+ }
185
+
186
+ @inline
187
+ isZero(): bool {
188
+ return !(this.lo1 | this.lo2 | this.hi1 | this.hi2);
189
+ }
190
+
191
+ @inline @operator.prefix('!')
192
+ static isEmpty(value: i256): bool {
193
+ return value.isZero();
194
+ }
195
+
196
+ @operator.prefix('~')
197
+ not(): i256 {
198
+ return new i256(~this.lo1, ~this.lo2, ~this.hi1, ~this.hi2);
199
+ }
200
+
201
+ @operator.prefix('+')
202
+ pos(): i256 {
203
+ return this;
204
+ }
205
+
206
+ @operator.prefix('-')
207
+ neg(): i256 {
208
+ var lo1 = ~this.lo1;
209
+ var lo2 = ~this.lo2;
210
+ var hi1 = ~this.hi1;
211
+ var hi2 = ~this.hi2;
212
+
213
+ var lo1p = lo1 + 1;
214
+ var carry = lo1p == 0 ? 1 : 0;
215
+
216
+ var lo2p = lo2 + carry;
217
+ carry = (lo2p == 0 && carry == 1) ? 1 : 0;
218
+
219
+ var hi1p = hi1 + carry;
220
+ carry = (hi1p == 0 && carry == 1) ? 1 : 0;
221
+
222
+ var hi2p = hi2 + carry;
223
+
224
+ return new i256(lo1p, lo2p, hi1p, hi2p);
225
+ }
226
+
227
+ @operator('+')
228
+ static add(a: i256, b: i256): i256 {
229
+ var lo1 = a.lo1 + b.lo1;
230
+ var carry = lo1 < a.lo1 ? 1 : 0;
231
+
232
+ var lo2 = a.lo2 + b.lo2 + carry;
233
+ carry = (lo2 < a.lo2 || (carry == 1 && lo2 == a.lo2)) ? 1 : 0;
234
+
235
+ var hi1 = a.hi1 + b.hi1 + carry;
236
+ carry = (hi1 < a.hi1 || (carry == 1 && hi1 == a.hi1)) ? 1 : 0;
237
+
238
+ var hi2 = a.hi2 + b.hi2 + carry;
239
+
240
+ return new i256(lo1, lo2, hi1, hi2);
241
+ }
242
+
243
+ @operator('-')
244
+ static sub(a: i256, b: i256): i256 {
245
+ var lo1 = a.lo1 - b.lo1;
246
+ var borrow = a.lo1 < b.lo1 ? 1 : 0;
247
+
248
+ var lo2 = a.lo2 - b.lo2 - borrow;
249
+ borrow = a.lo2 < b.lo2 + borrow ? 1 : 0;
250
+
251
+ var hi1 = a.hi1 - b.hi1 - borrow;
252
+ borrow = hi1 < b.hi1 + borrow ? 1 : 0;
253
+
254
+ var hi2 = a.hi2 - b.hi2 - borrow;
255
+
256
+ return new i256(lo1, lo2, hi1, hi2);
257
+ }
258
+
259
+ @inline @operator('|')
260
+ static or(a: i256, b: i256): i256 {
261
+ return new i256(a.lo1 | b.lo1, a.lo2 | b.lo2, a.hi1 | b.hi1, a.hi2 | b.hi2);
262
+ }
263
+
264
+ @inline @operator('^')
265
+ static xor(a: i256, b: i256): i256 {
266
+ return new i256(a.lo1 ^ b.lo1, a.lo2 ^ b.lo2, a.hi1 ^ b.hi1, a.hi2 ^ b.hi2);
267
+ }
268
+
269
+ @inline @operator('&')
270
+ static and(a: i256, b: i256): i256 {
271
+ return new i256(a.lo1 & b.lo1, a.lo2 & b.lo2, a.hi1 & b.hi1, a.hi2 & b.hi2);
272
+ }
273
+
274
+ @operator('<')
275
+ static lt(a: i256, b: i256): bool {
276
+ if (a.hi2 == b.hi2) {
277
+ if (a.hi1 == b.hi1) {
278
+ if (a.lo2 == b.lo2) {
279
+ return a.lo1 < b.lo1;
280
+ } else {
281
+ return a.lo2 < b.lo2;
282
+ }
283
+ } else {
284
+ return a.hi1 < b.hi1;
285
+ }
286
+ } else {
287
+ return a.hi2 < b.hi2;
288
+ }
289
+ }
290
+
291
+ @inline @operator('>')
292
+ static gt(a: i256, b: i256): bool {
293
+ return b < a;
294
+ }
295
+
296
+ @inline @operator('==')
297
+ static eq(a: i256, b: i256): bool {
298
+ return a.lo1 == b.lo1 && a.lo2 == b.lo2 && a.hi1 == b.hi1 && a.hi2 == b.hi2;
299
+ }
300
+
301
+ @inline @operator('!=')
302
+ static ne(a: i256, b: i256): bool {
303
+ return !i256.eq(a, b);
304
+ }
305
+
306
+ @inline @operator('<=')
307
+ static le(a: i256, b: i256): bool {
308
+ return !i256.gt(a, b);
309
+ }
310
+
311
+ @inline @operator('>=')
312
+ static ge(a: i256, b: i256): bool {
313
+ return !i256.lt(a, b);
314
+ }
315
+
316
+ @operator('<<')
317
+ static shl(value: i256, shift: i32): i256 {
318
+ shift &= 255;
319
+ if (shift == 0) return value;
320
+ if (shift >= 256) return new i256();
321
+ var lo1 = value.lo1;
322
+ var lo2 = value.lo2;
323
+ var hi1 = value.hi1;
324
+ var hi2 = value.hi2;
325
+ if (shift >= 192) {
326
+ let s = shift - 192;
327
+ return new i256(0, 0, 0, lo1 << s);
328
+ } else if (shift >= 128) {
329
+ let s = shift - 128;
330
+ return new i256(0, 0, lo1 << s, (lo2 << s) | (lo1 >>> (64 - s)));
331
+ } else if (shift >= 64) {
332
+ let s = shift - 64;
333
+ return new i256(0, (lo1 << s), (lo2 << s) | (lo1 >>> (64 - s)), (hi1 << s) | (lo2 >>> (64 - s)));
334
+ } else {
335
+ let s = shift;
336
+ return new i256(
337
+ lo1 << s,
338
+ (lo2 << s) | (lo1 >>> (64 - s)),
339
+ (hi1 << s) | (lo2 >>> (64 - s)),
340
+ (hi2 << s) | (hi1 >>> (64 - s)),
341
+ );
342
+ }
343
+ }
344
+
345
+ @operator('>>')
346
+ static shr(value: i256, shift: i32): i256 {
347
+ shift &= 255;
348
+ if (shift == 0) return value;
349
+ if (shift >= 256) {
350
+ let sign = value.isNeg() ? -1 : 0;
351
+ return new i256(sign, sign, sign, sign);
352
+ }
353
+ var lo1 = value.lo1;
354
+ var lo2 = value.lo2;
355
+ var hi1 = value.hi1;
356
+ var hi2 = value.hi2;
357
+ if (shift >= 192) {
358
+ let s = shift - 192;
359
+ let sign = value.isNeg() ? -1 : 0;
360
+ return new i256((hi2 >> s) | (sign << (64 - s)), sign, sign, sign);
361
+ } else if (shift >= 128) {
362
+ let s = shift - 128;
363
+ return new i256((hi1 >> s) | (hi2 << (64 - s)), (hi2 >> s) | (hi2 >> 63) << (64 - s), hi2 >> 63, hi2 >> 63);
364
+ } else if (shift >= 64) {
365
+ let s = shift - 64;
366
+ return new i256(
367
+ (lo2 >> s) | (hi1 << (64 - s)),
368
+ (hi1 >> s) | (hi2 << (64 - s)),
369
+ (hi2 >> s) | (hi2 >> 63) << (64 - s),
370
+ hi2 >> 63,
371
+ );
372
+ } else {
373
+ let s = shift;
374
+ return new i256(
375
+ (lo1 >> s) | (lo2 << (64 - s)),
376
+ (lo2 >> s) | (hi1 << (64 - s)),
377
+ (hi1 >> s) | (hi2 << (64 - s)),
378
+ (hi2 >> s) | (hi2 >> 63) << (64 - s),
379
+ );
380
+ }
381
+ }
382
+
383
+ @operator('>>>')
384
+ static shr_u(value: i256, shift: i32): i256 {
385
+ shift &= 255;
386
+ if (shift == 0) return value;
387
+ if (shift >= 256) return new i256();
388
+ var lo1 = value.lo1;
389
+ var lo2 = value.lo2;
390
+ var hi1 = value.hi1;
391
+ var hi2 = value.hi2;
392
+ if (shift >= 192) {
393
+ let s = shift - 192;
394
+ return new i256((hi2 >>> s), 0, 0, 0);
395
+ } else if (shift >= 128) {
396
+ let s = shift - 128;
397
+ return new i256((hi1 >>> s) | (hi2 << (64 - s)), (hi2 >>> s), 0, 0);
398
+ } else if (shift >= 64) {
399
+ let s = shift - 64;
400
+ return new i256(
401
+ (lo2 >>> s) | (hi1 << (64 - s)),
402
+ (hi1 >>> s) | (hi2 << (64 - s)),
403
+ (hi2 >>> s),
404
+ 0,
405
+ );
406
+ } else {
407
+ let s = shift;
408
+ return new i256(
409
+ (lo1 >>> s) | (lo2 << (64 - s)),
410
+ (lo2 >>> s) | (hi1 << (64 - s)),
411
+ (hi1 >>> s) | (hi2 << (64 - s)),
412
+ hi2 >>> s,
413
+ );
414
+ }
415
+ }
416
+
417
+ clone(): i256 {
418
+ return new i256(this.lo1, this.lo2, this.hi1, this.hi2);
419
+ }
420
+
421
+ // Helper methods to write the internal representation to a buffer
422
+ @inline
423
+ private toArrayBufferLE(buffer: usize): void {
424
+ store<i64>(buffer, this.lo1, 0 * sizeof<i64>());
425
+ store<i64>(buffer, this.lo2, 1 * sizeof<i64>());
426
+ store<i64>(buffer, this.hi1, 2 * sizeof<i64>());
427
+ store<i64>(buffer, this.hi2, 3 * sizeof<i64>());
428
+ }
429
+
430
+ @inline
431
+ private toArrayBufferBE(buffer: usize): void {
432
+ store<i64>(buffer, bswap<i64>(this.hi2), 0 * sizeof<i64>());
433
+ store<i64>(buffer, bswap<i64>(this.hi1), 1 * sizeof<i64>());
434
+ store<i64>(buffer, bswap<i64>(this.lo2), 2 * sizeof<i64>());
435
+ store<i64>(buffer, bswap<i64>(this.lo1), 3 * sizeof<i64>());
436
+ }
437
+
438
+ @inline
439
+ private toArrayBuffer(buffer: usize, bigEndian: bool = false): void {
440
+ if (bigEndian) {
441
+ this.toArrayBufferBE(buffer);
442
+ } else {
443
+ this.toArrayBufferLE(buffer);
444
+ }
445
+ }
446
+
447
+ /**
448
+ * Convert to Uint8Array
449
+ * @param bigEndian Little or Big Endian? Default: false
450
+ * @returns Uint8Array
451
+ */
452
+ @inline
453
+ toUint8Array(bigEndian: bool = false): Uint8Array {
454
+ var result = new Uint8Array(32);
455
+ this.toArrayBuffer(result.dataStart, bigEndian);
456
+ return result;
457
+ }
458
+
459
+ /**
460
+ * Convert to byte array
461
+ * @param bigEndian Little or Big Endian? Default: false
462
+ * @returns Array of bytes
463
+ */
464
+ @inline
465
+ toBytes(bigEndian: bool = false): u8[] {
466
+ var result = new Array<u8>(32);
467
+ this.toArrayBuffer(result.dataStart, bigEndian);
468
+ return result;
469
+ }
470
+
471
+ /**
472
+ * Convert to StaticArray of bytes
473
+ * @param bigEndian Little or Big Endian? Default: false
474
+ * @returns StaticArray<u8>
475
+ */
476
+ @inline
477
+ toStaticBytes(bigEndian: bool = false): StaticArray<u8> {
478
+ var result = new StaticArray<u8>(32);
479
+ this.toArrayBuffer(changetype<usize>(result), bigEndian);
480
+ return result;
481
+ }
482
+
483
+ // Other conversion methods can be added as needed, following similar patterns.
484
+
485
+ /**
486
+ * Generic type conversion
487
+ */
488
+ @inline
489
+ as<T>(): T {
490
+ var dummy!: T;
491
+ if (dummy instanceof bool) return <T>this.toBool();
492
+ else if (dummy instanceof i8) return <T>this.toI64();
493
+ else if (dummy instanceof u8) return <T>this.toU64();
494
+ else if (dummy instanceof i16) return <T>this.toI64();
495
+ else if (dummy instanceof u16) return <T>this.toU64();
496
+ else if (dummy instanceof i32) return <T>this.toI64();
497
+ else if (dummy instanceof u32) return <T>this.toU64();
498
+ else if (dummy instanceof i64) return <T>this.toI64();
499
+ else if (dummy instanceof u64) return <T>this.toU64();
500
+ else if (dummy instanceof i128) return <T>this.toI128();
501
+ else if (dummy instanceof u128) return <T>this.toU128();
502
+ else if (dummy instanceof i256) return <T>this;
503
+ else if (dummy instanceof u256) return <T>this.toU256();
504
+ else if (dummy instanceof u8[]) return <T>this.toBytes();
505
+ else if (dummy instanceof Uint8Array) return <T>this.toUint8Array();
506
+ else if (dummy instanceof StaticArray<u8>) return <T>this.toStaticBytes();
507
+ else if (dummy instanceof String) return <T>this.toString();
508
+ else throw new TypeError('Unsupported generic type');
509
+ }
510
+
511
+ @inline
512
+ toBool(): bool {
513
+ return this.lo1 != 0 || this.lo2 != 0 || this.hi1 != 0 || this.hi2 != 0;
514
+ }
515
+
516
+ @inline
517
+ toI64(): i64 {
518
+ return this.lo1;
519
+ }
520
+
521
+ @inline
522
+ toU64(): u64 {
523
+ return <u64>this.lo1;
524
+ }
525
+
526
+ @inline
527
+ toI32(): i32 {
528
+ return <i32>this.lo1;
529
+ }
530
+
531
+ @inline
532
+ toU32(): u32 {
533
+ return <u32>this.lo1;
534
+ }
535
+
536
+ @inline
537
+ toI128(): i128 {
538
+ return new i128(<u64>this.lo1, this.lo2);
539
+ }
540
+
541
+ @inline
542
+ toU128(): u128 {
543
+ return new u128(<u64>this.lo1, <u64>this.lo2);
544
+ }
545
+
546
+ @inline
547
+ toU256(): u256 {
548
+ return new u256(<u64>this.lo1, <u64>this.lo2, <u64>this.hi1, <u64>this.hi2);
549
+ }
550
+
551
+ @inline
552
+ public toString(radix: i32 = 10): string {
553
+ assert(radix == 10 || radix == 16, 'radix argument must be 10 or 16');
554
+ if (this.isZero()) return '0';
555
+
556
+ const isNegative = this.isNeg();
557
+ const value = isNegative ? this.neg() : this.clone();
558
+ let result = '';
559
+
560
+ if (radix == 16) {
561
+ const HEX_CHARS = '0123456789abcdef';
562
+ let shift: i32 = 252 - (i256.clz(value) & ~3);
563
+ let started = false;
564
+ while (shift >= 0) {
565
+ let digit = (<u8>((value.shr_u(shift)).lo1 & 0xF));
566
+ if (digit != 0 || started) {
567
+ // @ts-ignore
568
+ result += HEX_CHARS.charAt(digit);
569
+ started = true;
570
+ }
571
+ shift -= 4;
572
+ }
573
+ if (isNegative) {
574
+ result = '-' + result;
575
+ }
576
+ return result;
577
+ } else {
578
+ // Radix 10 conversion requires division, which needs to be implemented.
579
+ // Placeholder implementation (throws an error).
580
+ throw new Error('Radix 10 conversion is not implemented yet.');
581
+ }
582
+ }
583
+
584
+ shr_u(value: i32): i256 {
585
+ return i256.shr_u(this, value);
586
+ }
587
+
588
+ @inline
589
+ static clz(value: i256): i32 {
590
+ // For signed integers, leading zeros are counted from the sign bit.
591
+ // If the value is negative, we take its two's complement to get the magnitude.
592
+ if (value.isNeg()) {
593
+ value = value.neg();
594
+ }
595
+ if (value.hi2 != 0) {
596
+ return clz<u64>(value.hi2);
597
+ } else if (value.hi1 != 0) {
598
+ return clz<u64>(value.hi1) + 64;
599
+ } else if (value.lo2 != 0) {
600
+ return clz<u64>(value.lo2) + 128;
601
+ } else {
602
+ return clz<u64>(value.lo1) + 192;
603
+ }
604
+ }
605
+
606
+ }
@@ -3,6 +3,7 @@ import { Blockchain } from '../env';
3
3
  import { encodePointer } from '../math/abi';
4
4
  import { MemorySlotData } from './MemorySlot';
5
5
  import { u256 } from 'as-bignum/assembly';
6
+ import { BytesWriter } from '../buffer/BytesWriter';
6
7
 
7
8
  @final
8
9
  export class AddressMemoryMap<K extends string, V extends MemorySlotData<u256>> {
@@ -16,18 +17,22 @@ export class AddressMemoryMap<K extends string, V extends MemorySlotData<u256>>
16
17
  }
17
18
 
18
19
  public set(key: K, value: V): this {
19
- const keyHash: MemorySlotPointer = encodePointer(key);
20
- Blockchain.setStorageAt(this.pointer, keyHash, value);
20
+ const keyHash: MemorySlotPointer = this.encodePointer(key);
21
+ Blockchain.setStorageAt(keyHash, value);
21
22
 
22
23
  return this;
23
24
  }
24
25
 
25
26
  public get(key: K): MemorySlotData<u256> {
26
- return Blockchain.getStorageAt(this.pointer, encodePointer(key), this.defaultValue);
27
+ const keyHash: MemorySlotPointer = this.encodePointer(key);
28
+
29
+ return Blockchain.getStorageAt(keyHash, this.defaultValue);
27
30
  }
28
31
 
29
32
  public has(key: K): bool {
30
- return Blockchain.hasStorageAt(this.pointer, encodePointer(key));
33
+ const keyHash: MemorySlotPointer = this.encodePointer(key);
34
+
35
+ return Blockchain.hasStorageAt(keyHash);
31
36
  }
32
37
 
33
38
  @unsafe
@@ -41,4 +46,12 @@ export class AddressMemoryMap<K extends string, V extends MemorySlotData<u256>>
41
46
  public clear(): void {
42
47
  throw new Error('Method not implemented.');
43
48
  }
49
+
50
+ private encodePointer(key: K): MemorySlotPointer {
51
+ const writer = new BytesWriter(key.length + 2);
52
+ writer.writeU16(this.pointer);
53
+ writer.writeString(key);
54
+
55
+ return encodePointer(writer.getBuffer());
56
+ }
44
57
  }
@@ -3,6 +3,7 @@ import { u256 } from 'as-bignum/assembly';
3
3
  import { Blockchain } from '../env';
4
4
  import { MemorySlotPointer } from './MemorySlotPointer';
5
5
  import { encodePointer } from '../math/abi';
6
+ import { BytesWriter } from '../buffer/BytesWriter';
6
7
 
7
8
  @final
8
9
  export class KeyMerger<K extends string, K2 extends string, V extends MemorySlotData<u256>> {
@@ -22,9 +23,9 @@ export class KeyMerger<K extends string, K2 extends string, V extends MemorySlot
22
23
 
23
24
  public set(key2: K2, value: V): this {
24
25
  const mergedKey: string = `${this.parentKey}${key2}`;
25
- const keyHash: MemorySlotPointer = encodePointer(mergedKey);
26
+ const keyHash: MemorySlotPointer = this.encodePointer(mergedKey);
26
27
 
27
- Blockchain.setStorageAt(this.pointer, keyHash, value);
28
+ Blockchain.setStorageAt(keyHash, value);
28
29
 
29
30
  return this;
30
31
  }
@@ -32,13 +33,13 @@ export class KeyMerger<K extends string, K2 extends string, V extends MemorySlot
32
33
  public get(key: K): MemorySlotData<u256> {
33
34
  const mergedKey: string = `${this.parentKey}${key}`;
34
35
 
35
- return Blockchain.getStorageAt(this.pointer, encodePointer(mergedKey), this.defaultValue);
36
+ return Blockchain.getStorageAt(this.encodePointer(mergedKey), this.defaultValue);
36
37
  }
37
38
 
38
39
  public has(key: K): bool {
39
40
  const mergedKey: string = `${this.parentKey}${key}`;
40
41
 
41
- return Blockchain.hasStorageAt(this.pointer, encodePointer(mergedKey));
42
+ return Blockchain.hasStorageAt(this.encodePointer(mergedKey));
42
43
  }
43
44
 
44
45
  @unsafe
@@ -50,4 +51,12 @@ export class KeyMerger<K extends string, K2 extends string, V extends MemorySlot
50
51
  public clear(): void {
51
52
  throw new Error('Clear method not implemented.');
52
53
  }
54
+
55
+ private encodePointer(key: string): MemorySlotPointer {
56
+ const writer = new BytesWriter(key.length + 2);
57
+ writer.writeU16(this.pointer);
58
+ writer.writeString(key);
59
+
60
+ return encodePointer(writer.getBuffer());
61
+ }
53
62
  }
@@ -4,26 +4,29 @@ import { MemorySlotPointer } from '../memory/MemorySlotPointer';
4
4
  import { BytesWriter } from '../buffer/BytesWriter';
5
5
  import { BytesReader } from '../buffer/BytesReader';
6
6
  import { Revert } from '../types/Revert';
7
+ import { encodePointer } from '../math/abi';
7
8
 
8
9
  export abstract class Serializable {
9
10
  protected pointer: u16;
10
- protected subPointer:MemorySlotPointer;
11
+ protected subPointer: MemorySlotPointer;
11
12
 
12
- protected constructor(pointer: u16,
13
- subPointer:MemorySlotPointer) {
13
+ protected constructor(pointer: u16, subPointer: MemorySlotPointer) {
14
14
  this.pointer = pointer;
15
15
  this.subPointer = subPointer;
16
16
  }
17
17
 
18
18
  public abstract get chunkCount(): i32;
19
+
19
20
  public abstract writeToBuffer(): BytesWriter;
21
+
20
22
  public abstract readFromBuffer(reader: BytesReader): void;
21
23
 
22
- public load() :void {
24
+ public load(): void {
23
25
  const chunks: u256[] = [];
24
26
 
25
- for(let index:i32 = 0; index < this.chunkCount; index++){
26
- const chunk: u256 = Blockchain.getStorageAt(this.pointer, u256.add(this.subPointer, u256.fromU32(index)), u256.Zero);
27
+ for (let index: i32 = 0; index < this.chunkCount; index++) {
28
+ const pointer = this.getPointer(u256.add(this.subPointer, u256.fromU32(index)));
29
+ const chunk: u256 = Blockchain.getStorageAt(pointer, u256.Zero);
27
30
  chunks.push(chunk);
28
31
  }
29
32
 
@@ -41,8 +44,7 @@ export abstract class Serializable {
41
44
 
42
45
  for (let index: i32 = 0; index < chunks.length; index++) {
43
46
  Blockchain.setStorageAt(
44
- this.pointer,
45
- u256.add(this.subPointer, u256.fromU32(index)),
47
+ this.getPointer(u256.add(this.subPointer, u256.fromU32(index))),
46
48
  chunks[index],
47
49
  );
48
50
  }
@@ -60,7 +62,7 @@ export abstract class Serializable {
60
62
  }
61
63
 
62
64
  protected chunksToBytes(chunks: u256[]): BytesReader {
63
- if(this.chunkCount >= 67108863) {
65
+ if (this.chunkCount >= 67108863) {
64
66
  throw new Revert('Too many chunks received');
65
67
  }
66
68
 
@@ -76,4 +78,12 @@ export abstract class Serializable {
76
78
 
77
79
  return new BytesReader(buffer);
78
80
  }
81
+
82
+ private getPointer(subPointer: u256): u256 {
83
+ const writer = new BytesWriter(34);
84
+ writer.writeU16(this.pointer);
85
+ writer.writeU256(subPointer);
86
+
87
+ return encodePointer(writer.getBuffer());
88
+ }
79
89
  }
@@ -3,10 +3,14 @@ import { Blockchain } from '../env';
3
3
 
4
4
  @final
5
5
  export class StoredBoolean {
6
+ private readonly u256Pointer: u256;
7
+
6
8
  constructor(
7
9
  public pointer: u16,
8
10
  private defaultValue: bool,
9
- ) {}
11
+ ) {
12
+ this.u256Pointer = u256.from(this.pointer);
13
+ }
10
14
 
11
15
  private _value: u256 = u256.Zero;
12
16
 
@@ -21,14 +25,14 @@ export class StoredBoolean {
21
25
  public set value(value: bool) {
22
26
  this._value = value ? u256.One : u256.Zero;
23
27
 
24
- Blockchain.setStorageAt(this.pointer, u256.Zero, this._value);
28
+ Blockchain.setStorageAt(this.u256Pointer, this._value);
25
29
  }
26
30
 
27
31
  @inline
28
32
  public set(value: u256): this {
29
33
  this._value = value;
30
34
 
31
- Blockchain.setStorageAt(this.pointer, u256.Zero, this._value);
35
+ Blockchain.setStorageAt(this.u256Pointer, this._value);
32
36
 
33
37
  return this;
34
38
  }
@@ -40,8 +44,7 @@ export class StoredBoolean {
40
44
 
41
45
  private ensureValue(): void {
42
46
  this._value = Blockchain.getStorageAt(
43
- this.pointer,
44
- u256.Zero,
47
+ this.u256Pointer,
45
48
  this.defaultValue ? u256.One : u256.Zero,
46
49
  );
47
50
  }
@@ -1,6 +1,8 @@
1
1
  import { u256 } from 'as-bignum/assembly';
2
2
  import { Blockchain } from '../env';
3
3
  import { SafeMath } from '../types/SafeMath';
4
+ import { encodePointer } from '../math/abi';
5
+ import { BytesWriter } from '../buffer/BytesWriter';
4
6
 
5
7
  @final
6
8
  export class StoredString {
@@ -30,8 +32,12 @@ export class StoredString {
30
32
  return a < b ? a : b;
31
33
  }
32
34
 
33
- private max(a: u32, b: u32): u32 {
34
- return a > b ? a : b;
35
+ private getPointer(key: u256): u256 {
36
+ const buf = new BytesWriter(34);
37
+ buf.writeU16(this.pointer);
38
+ buf.writeU256(key);
39
+
40
+ return encodePointer(buf.getBuffer());
35
41
  }
36
42
 
37
43
  private save(): void {
@@ -55,7 +61,7 @@ export class StoredString {
55
61
  // Save the initial chunk (first 28 bytes) in the header
56
62
  let bytesToWrite: u32 = this.min(remainingLength, 28);
57
63
  header = this.saveChunk(header, this._value, offset, bytesToWrite, 4);
58
- Blockchain.setStorageAt(this.pointer, currentPointer, header);
64
+ Blockchain.setStorageAt(this.getPointer(currentPointer), header);
59
65
 
60
66
  remainingLength -= bytesToWrite;
61
67
  offset += bytesToWrite;
@@ -71,7 +77,7 @@ export class StoredString {
71
77
  0,
72
78
  );
73
79
  currentPointer = u256.add(currentPointer, u256.One);
74
- Blockchain.setStorageAt(this.pointer, currentPointer, storageValue);
80
+ Blockchain.setStorageAt(this.getPointer(currentPointer), storageValue);
75
81
 
76
82
  remainingLength -= bytesToWrite;
77
83
  offset += bytesToWrite;
@@ -95,7 +101,7 @@ export class StoredString {
95
101
  }
96
102
 
97
103
  private load(): void {
98
- const header: u256 = Blockchain.getStorageAt(this.pointer, u256.Zero, u256.Zero);
104
+ const header: u256 = Blockchain.getStorageAt(this.getPointer(u256.Zero), u256.Zero);
99
105
  if (u256.eq(header, u256.Zero)) {
100
106
  if (this.defaultValue) {
101
107
  this.value = this.defaultValue;
@@ -120,7 +126,7 @@ export class StoredString {
120
126
  while (remainingLength > 0) {
121
127
  // Move to the next storage slot
122
128
  currentPointer = u256.add(currentPointer, u256.One);
123
- currentStorage = Blockchain.getStorageAt(this.pointer, currentPointer, u256.Zero);
129
+ currentStorage = Blockchain.getStorageAt(this.getPointer(currentPointer), u256.Zero);
124
130
 
125
131
  // Extract the relevant portion of the string from the current storage slot
126
132
  const bytesToRead: u32 = this.min(remainingLength, 32);
@@ -2,14 +2,24 @@ import { u256 } from 'as-bignum/assembly';
2
2
  import { SafeMath } from '../types/SafeMath';
3
3
  import { MemorySlotPointer } from '../memory/MemorySlotPointer';
4
4
  import { Blockchain } from '../env';
5
+ import { encodePointer } from '../math/abi';
6
+ import { BytesWriter } from '../buffer/BytesWriter';
5
7
 
6
8
  @final
7
9
  export class StoredU256 {
10
+ private readonly u256Pointer: u256;
11
+
8
12
  constructor(
9
13
  public pointer: u16,
10
14
  public subPointer: MemorySlotPointer,
11
15
  private defaultValue: u256,
12
- ) {}
16
+ ) {
17
+ const writer = new BytesWriter(34);
18
+ writer.writeU16(pointer);
19
+ writer.writeU256(subPointer);
20
+
21
+ this.u256Pointer = encodePointer(writer.getBuffer());
22
+ }
13
23
 
14
24
  private _value: u256 = u256.Zero;
15
25
 
@@ -28,7 +38,7 @@ export class StoredU256 {
28
38
 
29
39
  this._value = value;
30
40
 
31
- Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
41
+ Blockchain.setStorageAt(this.u256Pointer, this._value);
32
42
  }
33
43
 
34
44
  @inline
@@ -42,7 +52,28 @@ export class StoredU256 {
42
52
  this.ensureValue();
43
53
 
44
54
  this._value = SafeMath.add(this._value, value);
45
- Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
55
+ Blockchain.setStorageAt(this.u256Pointer, this._value);
56
+
57
+ return this;
58
+ }
59
+
60
+ @inline
61
+ public addNoCommit(value: u256): this {
62
+ this._value = SafeMath.add(this._value, value);
63
+
64
+ return this;
65
+ }
66
+
67
+ @inline
68
+ public subNoCommit(value: u256): this {
69
+ this._value = SafeMath.sub(this._value, value);
70
+
71
+ return this;
72
+ }
73
+
74
+ @inline
75
+ public commit(): this {
76
+ Blockchain.setStorageAt(this.u256Pointer, this._value);
46
77
 
47
78
  return this;
48
79
  }
@@ -53,7 +84,7 @@ export class StoredU256 {
53
84
  this.ensureValue();
54
85
 
55
86
  this._value = SafeMath.sub(this._value, value);
56
- Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
87
+ Blockchain.setStorageAt(this.u256Pointer, this._value);
57
88
 
58
89
  return this;
59
90
  }
@@ -64,7 +95,7 @@ export class StoredU256 {
64
95
  this.ensureValue();
65
96
 
66
97
  this._value = SafeMath.mul(this._value, value);
67
- Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
98
+ Blockchain.setStorageAt(this.u256Pointer, this._value);
68
99
 
69
100
  return this;
70
101
  }
@@ -123,7 +154,7 @@ export class StoredU256 {
123
154
  this.ensureValue();
124
155
 
125
156
  this._value = u256.shr(this._value, value);
126
- Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
157
+ Blockchain.setStorageAt(this.u256Pointer, this._value);
127
158
 
128
159
  return this;
129
160
  }
@@ -134,7 +165,7 @@ export class StoredU256 {
134
165
  this.ensureValue();
135
166
 
136
167
  this._value = u256.and(this._value, value);
137
- Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
168
+ Blockchain.setStorageAt(this.u256Pointer, this._value);
138
169
 
139
170
  return this;
140
171
  }
@@ -145,7 +176,7 @@ export class StoredU256 {
145
176
  this.ensureValue();
146
177
 
147
178
  this._value = u256.or(this._value, value);
148
- Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
179
+ Blockchain.setStorageAt(this.u256Pointer, this._value);
149
180
 
150
181
  return this;
151
182
  }
@@ -156,7 +187,7 @@ export class StoredU256 {
156
187
  this.ensureValue();
157
188
 
158
189
  this._value = u256.xor(this._value, value);
159
- Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
190
+ Blockchain.setStorageAt(this.u256Pointer, this._value);
160
191
 
161
192
  return this;
162
193
  }
@@ -167,7 +198,7 @@ export class StoredU256 {
167
198
  this.ensureValue();
168
199
 
169
200
  this._value = SafeMath.pow(this._value, exponent);
170
- Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
201
+ Blockchain.setStorageAt(this.u256Pointer, this._value);
171
202
 
172
203
  return this;
173
204
  }
@@ -178,7 +209,7 @@ export class StoredU256 {
178
209
  this.ensureValue();
179
210
 
180
211
  this._value = SafeMath.mod(this._value, value);
181
- Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
212
+ Blockchain.setStorageAt(this.u256Pointer, this._value);
182
213
 
183
214
  return this;
184
215
  }
@@ -189,7 +220,7 @@ export class StoredU256 {
189
220
  this.ensureValue();
190
221
 
191
222
  this._value = SafeMath.add(this._value, u256.One);
192
- Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
223
+ Blockchain.setStorageAt(this.u256Pointer, this._value);
193
224
 
194
225
  return this;
195
226
  }
@@ -200,7 +231,7 @@ export class StoredU256 {
200
231
  this.ensureValue();
201
232
 
202
233
  this._value = SafeMath.sub(this._value, u256.One);
203
- Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
234
+ Blockchain.setStorageAt(this.u256Pointer, this._value);
204
235
 
205
236
  return this;
206
237
  }
@@ -209,7 +240,7 @@ export class StoredU256 {
209
240
  public set(value: u256): this {
210
241
  this._value = value;
211
242
 
212
- Blockchain.setStorageAt(this.pointer, this.subPointer, this._value);
243
+ Blockchain.setStorageAt(this.u256Pointer, this._value);
213
244
 
214
245
  return this;
215
246
  }
@@ -220,6 +251,6 @@ export class StoredU256 {
220
251
  }
221
252
 
222
253
  private ensureValue(): void {
223
- this._value = Blockchain.getStorageAt(this.pointer, this.subPointer, this.defaultValue);
254
+ this._value = Blockchain.getStorageAt(this.u256Pointer, this.defaultValue);
224
255
  }
225
256
  }