@btc-vision/btc-runtime 1.0.28 → 1.0.30

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 (43) hide show
  1. package/package.json +1 -1
  2. package/runtime/buffer/BytesReader.ts +200 -200
  3. package/runtime/buffer/BytesWriter.ts +346 -342
  4. package/runtime/contracts/OP_20.ts +341 -341
  5. package/runtime/contracts/OP_NET.ts +73 -73
  6. package/runtime/contracts/interfaces/IOP_20.ts +21 -21
  7. package/runtime/env/BTCEnvironment.ts +319 -314
  8. package/runtime/env/index.ts +3 -3
  9. package/runtime/events/NetEvent.ts +27 -27
  10. package/runtime/events/predefined/ApproveEvent.ts +16 -16
  11. package/runtime/events/predefined/BurnEvent.ts +13 -13
  12. package/runtime/events/predefined/ClaimEvent.ts +13 -13
  13. package/runtime/events/predefined/MintEvent.ts +15 -15
  14. package/runtime/events/predefined/StakeEvent.ts +13 -13
  15. package/runtime/events/predefined/TransferEvent.ts +16 -16
  16. package/runtime/events/predefined/UnstakeEvent.ts +13 -13
  17. package/runtime/events/predefined/index.ts +7 -7
  18. package/runtime/exports/index.ts +37 -37
  19. package/runtime/generic/Map.ts +65 -65
  20. package/runtime/generic/MapU256.ts +57 -57
  21. package/runtime/index.ts +53 -52
  22. package/runtime/interfaces/DeployContractResponse.ts +12 -12
  23. package/runtime/interfaces/IBTC.ts +6 -6
  24. package/runtime/lang/Definitions.ts +1 -1
  25. package/runtime/math/abi.ts +37 -37
  26. package/runtime/math/bytes.ts +34 -34
  27. package/runtime/math/cyrb53.ts +46 -46
  28. package/runtime/math/rnd.ts +51 -51
  29. package/runtime/math/sha256.ts +12 -12
  30. package/runtime/memory/AddressMemoryMap.ts +44 -44
  31. package/runtime/memory/KeyMerger.ts +53 -53
  32. package/runtime/memory/MemorySlot.ts +1 -1
  33. package/runtime/memory/MemorySlotPointer.ts +3 -3
  34. package/runtime/memory/MultiAddressMemoryMap.ts +62 -62
  35. package/runtime/shared-libraries/TransferHelper.ts +64 -64
  36. package/runtime/storage/Serializable.ts +75 -0
  37. package/runtime/storage/StoredString.ts +145 -145
  38. package/runtime/storage/StoredU256.ts +246 -246
  39. package/runtime/types/Address.ts +5 -5
  40. package/runtime/types/Revert.ts +5 -5
  41. package/runtime/types/SafeMath.ts +197 -197
  42. package/runtime/types/index.ts +8 -8
  43. package/runtime/universal/ABIRegistry.ts +72 -72
@@ -1,342 +1,346 @@
1
- import { u256 } from 'as-bignum/assembly';
2
- import { Address, ADDRESS_BYTE_LENGTH } from '../types/Address';
3
- import { Selector } from '../math/abi';
4
- import { BytesReader } from './BytesReader';
5
- import { SelectorsMap } from '../universal/ABIRegistry';
6
- import { MemorySlotPointer } from '../memory/MemorySlotPointer';
7
- import { MemorySlotData } from '../memory/MemorySlot';
8
- import { cyrb53a } from '../math/cyrb53';
9
- import { Revert } from '../types/Revert';
10
- import { Map } from '../generic/Map';
11
- import { BlockchainStorage, PointerStorage } from '../types';
12
-
13
- export enum BufferDataType {
14
- U8 = 0,
15
- U16 = 1,
16
- U32 = 2,
17
- U64 = 3,
18
- U256 = 4,
19
- ADDRESS = 5,
20
- STRING = 6,
21
- BOOLEAN = 7,
22
- }
23
-
24
- @final
25
- export class BytesWriter {
26
- private currentOffset: u32 = 0;
27
- private buffer: DataView;
28
-
29
- private selectorDatatype: u8[] = [];
30
-
31
- constructor(
32
- length: i32 = 1,
33
- private readonly trackDataTypes: boolean = false,
34
- ) {
35
- this.buffer = new DataView(new ArrayBuffer(length));
36
- }
37
-
38
- public bufferLength(): u32 {
39
- return this.buffer.byteLength;
40
- }
41
-
42
- public writeU8(value: u8): void {
43
- if (this.trackDataTypes) this.selectorDatatype.push(u8(BufferDataType.U8));
44
-
45
- this.allocSafe(1);
46
- this.buffer.setUint8(this.currentOffset++, value);
47
- }
48
-
49
- public writeU16(value: u16): void {
50
- if (this.trackDataTypes) this.selectorDatatype.push(u8(BufferDataType.U16));
51
-
52
- this.allocSafe(2);
53
- this.buffer.setUint16(this.currentOffset, value, true);
54
- this.currentOffset += 2;
55
- }
56
-
57
- public writeU32(value: u32, le: boolean = true): void {
58
- if (this.trackDataTypes) this.selectorDatatype.push(u8(BufferDataType.U32));
59
-
60
- this.allocSafe(4);
61
- this.buffer.setUint32(this.currentOffset, value, le);
62
- this.currentOffset += 4;
63
- }
64
-
65
- public writeU64(value: u64): void {
66
- if (this.trackDataTypes) this.selectorDatatype.push(u8(BufferDataType.U64));
67
-
68
- this.allocSafe(8);
69
- this.buffer.setUint64(this.currentOffset, value || 0, true);
70
- this.currentOffset += 8;
71
- }
72
-
73
- public writeAddressArray(value: Address[]): void {
74
- if (value.length > 65535) throw new Revert('Array size is too large');
75
-
76
- this.writeU16(value.length);
77
-
78
- for (let i: i32 = 0; i < value.length; i++) {
79
- this.writeAddress(value[i]);
80
- }
81
- }
82
-
83
- public writeStorage(storage: BlockchainStorage): void {
84
- this.writeU32(storage.size);
85
-
86
- const keys: Address[] = storage.keys();
87
- const values: PointerStorage[] = storage.values();
88
-
89
- for (let i: i32 = 0; i < keys.length; i++) {
90
- const address: Address = keys[i];
91
- const storage: PointerStorage = values[i];
92
-
93
- this.writeAddress(address);
94
-
95
- const subKeys: MemorySlotPointer[] = storage.keys();
96
- const subValues: MemorySlotData<u256>[] = storage.values();
97
-
98
- this.writeU32(subKeys.length);
99
-
100
- for (let j: i32 = 0; j < subKeys.length; j++) {
101
- const pointer: MemorySlotPointer = subKeys[j];
102
- const value: MemorySlotData<u256> = subValues[j];
103
-
104
- this.writeU256(pointer);
105
- this.writeU256(value);
106
- }
107
- }
108
- }
109
-
110
- public writeSelector(value: Selector): void {
111
- this.writeU32(value, false);
112
- }
113
-
114
- public writeBoolean(value: boolean): void {
115
- if (this.trackDataTypes) this.selectorDatatype.push(u8(BufferDataType.BOOLEAN));
116
-
117
- this.writeU8(value ? 1 : 0);
118
- }
119
-
120
- public writeU256(value: u256): void {
121
- if (this.trackDataTypes) this.selectorDatatype.push(u8(BufferDataType.U256));
122
- this.allocSafe(32);
123
-
124
- const bytes = value.toUint8Array(true);
125
- for (let i: i32 = 0; i < 32; i++) {
126
- this.writeU8(bytes[i] || 0);
127
- }
128
- }
129
-
130
- public writeTuple(value: u256[]): void {
131
- this.allocSafe(4 + value.length * 32);
132
- this.writeU32(u32(value.length));
133
-
134
- for (let i = 0; i < value.length; i++) {
135
- this.writeU256(value[i]);
136
- }
137
- }
138
-
139
- public writeBytes(value: Uint8Array): void {
140
- this.allocSafe(value.length);
141
-
142
- for (let i = 0; i < value.length; i++) {
143
- this.writeU8(value[i]);
144
- }
145
- }
146
-
147
- public writeBytesU8Array(value: u8[]): void {
148
- this.allocSafe(value.length);
149
-
150
- for (let i = 0; i < value.length; i++) {
151
- this.writeU8(value[i]);
152
- }
153
- }
154
-
155
- public writeBytesWithLength(value: Uint8Array): void {
156
- const length: u32 = u32(value.byteLength);
157
-
158
- this.allocSafe(length + 4);
159
- this.writeU32(length);
160
-
161
- for (let i: u32 = 0; i < length; i++) {
162
- this.writeU8(value[i]);
163
- }
164
- }
165
-
166
- public writeString(value: string): void {
167
- if (this.trackDataTypes) this.selectorDatatype.push(u8(BufferDataType.STRING));
168
-
169
- for (let i: i32 = 0; i < value.length; i++) {
170
- this.writeU8(u8(value.charCodeAt(i)));
171
- }
172
- }
173
-
174
- public writeAddress(value: Address): void {
175
- if (this.trackDataTypes) this.selectorDatatype.push(u8(BufferDataType.ADDRESS));
176
-
177
- const bytes = this.fromAddress(value);
178
- this.writeBytes(bytes);
179
- }
180
-
181
- public writeStringWithLength(value: string): void {
182
- this.writeU16(u16(value.length));
183
-
184
- this.writeString(value);
185
- }
186
-
187
- public writeViewSelectorMap(map: SelectorsMap): void {
188
- this.writeU16(u16(map.size));
189
-
190
- const keys = map.keys();
191
- for (let i = 0; i < keys.length; i++) {
192
- const key: u32 = keys[i] as u32;
193
- const value = map.get(key);
194
-
195
- this.writeBytes(value);
196
- }
197
- }
198
-
199
- public writeAddressValueTupleMap(map: Map<Address, u256>): void {
200
- if (map.size > 65535) throw new Revert('Map size is too large');
201
- this.writeU16(u16(map.size));
202
-
203
- const keys = map.keys();
204
- for (let i = 0; i < keys.length; i++) {
205
- const key: Address = keys[i];
206
- const value: u256 = map.get(key) || u256.Zero;
207
-
208
- this.writeAddress(key);
209
- this.writeU256(value);
210
- }
211
- }
212
-
213
- public writeLimitedAddressBytesMap(map: Map<Address, Uint8Array[]>): void {
214
- if (map.size > 8) throw new Revert('Too many contract called.'); // no more than 8 different contracts.
215
-
216
- this.writeU8(u8(map.size));
217
-
218
- const keys: Address[] = map.keys();
219
- for (let i: i32 = 0; i < keys.length; i++) {
220
- const address: Address = keys[i];
221
- const calls: Uint8Array[] = map.get(address) || [];
222
-
223
- if (calls.length > 10) throw new Revert('Too many calls.'); // no more than 16 different calls.
224
-
225
- this.writeAddress(address);
226
- this.writeU8(u8(calls.length));
227
-
228
- for (let j: i32 = 0; j < calls.length; j++) {
229
- this.writeBytesWithLength(calls[j]);
230
- }
231
- }
232
- }
233
-
234
- public writeMethodSelectorsMap(map: Selector[]): void {
235
- this.writeU16(u16(map.length));
236
-
237
- for (let i = 0; i < map.length; i++) {
238
- this.writeSelector(map[i]);
239
- }
240
- }
241
-
242
- public getBuffer(clear: boolean = true): Uint8Array {
243
- const buf = new Uint8Array(this.buffer.byteLength);
244
- for (let i: u32 = 0; i < u32(this.buffer.byteLength); i++) {
245
- buf[i] = this.buffer.getUint8(i);
246
- }
247
-
248
- if (clear) this.clear();
249
-
250
- return buf;
251
- }
252
-
253
- public toBytesReader(): BytesReader {
254
- return new BytesReader(this.getBuffer());
255
- }
256
-
257
- public getOffset(): u32 {
258
- return this.currentOffset;
259
- }
260
-
261
- public setOffset(offset: u32): void {
262
- this.currentOffset = offset;
263
- }
264
-
265
- public clear(): void {
266
- this.currentOffset = 0;
267
- this.buffer = this.getDefaultBuffer();
268
- this.selectorDatatype = [];
269
- }
270
-
271
- public allocSafe(size: u32): void {
272
- if (this.currentOffset + size > u32(this.buffer.byteLength)) {
273
- const sizeDiff: u32 = size - (u32(this.buffer.byteLength) - this.currentOffset);
274
-
275
- this.resize(sizeDiff);
276
- }
277
- }
278
-
279
- public writeABISelector(name: string, selector: Selector): void {
280
- this.writeStringWithLength(name);
281
- this.writeSelector(selector);
282
- }
283
-
284
- public getSelectorDataType(): u64 {
285
- let hash: u64 = 0;
286
- if (this.selectorDatatype.length === 0) return hash;
287
-
288
- return cyrb53a(this.selectorDatatype);
289
- }
290
-
291
- private getChecksum(): u32 {
292
- let checksum: u32 = 0;
293
- for (let i = 0; i < this.buffer.byteLength; i++) {
294
- checksum += this.buffer.getUint8(i);
295
- }
296
-
297
- return checksum % 2 ** 32;
298
- }
299
-
300
- private writeMethodSelectorMap(value: Set<Selector>): void {
301
- this.writeU16(u16(value.size));
302
-
303
- const keys = value.values();
304
- for (let i = 0; i < keys.length; i++) {
305
- const key = keys[i];
306
-
307
- this.writeSelector(key);
308
- }
309
- }
310
-
311
- private fromAddress(value: Address): Uint8Array {
312
- if (value.length > i32(ADDRESS_BYTE_LENGTH)) {
313
- throw new Revert(`Address is too long ${value.length} > ${ADDRESS_BYTE_LENGTH} bytes`);
314
- }
315
-
316
- const length: i32 = Math.min(value.length + 1, ADDRESS_BYTE_LENGTH);
317
- const bytes: Uint8Array = new Uint8Array(length);
318
- for (let i: i32 = 0; i < value.length; i++) {
319
- bytes[i] = value.charCodeAt(i);
320
- }
321
-
322
- if (value.length < i32(ADDRESS_BYTE_LENGTH)) {
323
- bytes[value.length] = 0;
324
- }
325
-
326
- return bytes;
327
- }
328
-
329
- private resize(size: u32): void {
330
- const buf: Uint8Array = new Uint8Array(u32(this.buffer.byteLength) + size);
331
-
332
- for (let i: i32 = 0; i < this.buffer.byteLength; i++) {
333
- buf[i] = this.buffer.getUint8(i);
334
- }
335
-
336
- this.buffer = new DataView(buf.buffer);
337
- }
338
-
339
- private getDefaultBuffer(length: i32 = 1): DataView {
340
- return new DataView(new ArrayBuffer(length));
341
- }
342
- }
1
+ import { u256 } from 'as-bignum/assembly';
2
+ import { Address, ADDRESS_BYTE_LENGTH } from '../types/Address';
3
+ import { Selector } from '../math/abi';
4
+ import { BytesReader } from './BytesReader';
5
+ import { SelectorsMap } from '../universal/ABIRegistry';
6
+ import { MemorySlotPointer } from '../memory/MemorySlotPointer';
7
+ import { MemorySlotData } from '../memory/MemorySlot';
8
+ import { cyrb53a } from '../math/cyrb53';
9
+ import { Revert } from '../types/Revert';
10
+ import { Map } from '../generic/Map';
11
+ import { BlockchainStorage, PointerStorage } from '../types';
12
+
13
+ export enum BufferDataType {
14
+ U8 = 0,
15
+ U16 = 1,
16
+ U32 = 2,
17
+ U64 = 3,
18
+ U256 = 4,
19
+ ADDRESS = 5,
20
+ STRING = 6,
21
+ BOOLEAN = 7,
22
+ }
23
+
24
+ @final
25
+ export class BytesWriter {
26
+ private currentOffset: u32 = 0;
27
+ private buffer: DataView;
28
+
29
+ private selectorDatatype: u8[] = [];
30
+
31
+ constructor(
32
+ length: i32 = 1,
33
+ private readonly trackDataTypes: boolean = false,
34
+ ) {
35
+ this.buffer = new DataView(new ArrayBuffer(length));
36
+ }
37
+
38
+ public bufferLength(): u32 {
39
+ return this.buffer.byteLength;
40
+ }
41
+
42
+ public writeU8(value: u8): void {
43
+ if (this.trackDataTypes) this.selectorDatatype.push(u8(BufferDataType.U8));
44
+
45
+ this.allocSafe(1);
46
+ this.buffer.setUint8(this.currentOffset++, value);
47
+ }
48
+
49
+ public writeU16(value: u16): void {
50
+ if (this.trackDataTypes) this.selectorDatatype.push(u8(BufferDataType.U16));
51
+
52
+ this.allocSafe(2);
53
+ this.buffer.setUint16(this.currentOffset, value, true);
54
+ this.currentOffset += 2;
55
+ }
56
+
57
+ public writeU32(value: u32, le: boolean = true): void {
58
+ if (this.trackDataTypes) this.selectorDatatype.push(u8(BufferDataType.U32));
59
+
60
+ this.allocSafe(4);
61
+ this.buffer.setUint32(this.currentOffset, value, le);
62
+ this.currentOffset += 4;
63
+ }
64
+
65
+ public writeU64(value: u64): void {
66
+ if (this.trackDataTypes) this.selectorDatatype.push(u8(BufferDataType.U64));
67
+
68
+ this.allocSafe(8);
69
+ this.buffer.setUint64(this.currentOffset, value || 0, true);
70
+ this.currentOffset += 8;
71
+ }
72
+
73
+ public writeAddressArray(value: Address[]): void {
74
+ if (value.length > 65535) throw new Revert('Array size is too large');
75
+
76
+ this.writeU16(value.length);
77
+
78
+ for (let i: i32 = 0; i < value.length; i++) {
79
+ this.writeAddress(value[i]);
80
+ }
81
+ }
82
+
83
+ public writeStorage(storage: BlockchainStorage): void {
84
+ this.writeU32(storage.size);
85
+
86
+ const keys: Address[] = storage.keys();
87
+ const values: PointerStorage[] = storage.values();
88
+
89
+ for (let i: i32 = 0; i < keys.length; i++) {
90
+ const address: Address = keys[i];
91
+ const storage: PointerStorage = values[i];
92
+
93
+ this.writeAddress(address);
94
+
95
+ const subKeys: MemorySlotPointer[] = storage.keys();
96
+ const subValues: MemorySlotData<u256>[] = storage.values();
97
+
98
+ this.writeU32(subKeys.length);
99
+
100
+ for (let j: i32 = 0; j < subKeys.length; j++) {
101
+ const pointer: MemorySlotPointer = subKeys[j];
102
+ const value: MemorySlotData<u256> = subValues[j];
103
+
104
+ this.writeU256(pointer);
105
+ this.writeU256(value);
106
+ }
107
+ }
108
+ }
109
+
110
+ public writeSelector(value: Selector): void {
111
+ this.writeU32(value, false);
112
+ }
113
+
114
+ public writeBoolean(value: boolean): void {
115
+ if (this.trackDataTypes) this.selectorDatatype.push(u8(BufferDataType.BOOLEAN));
116
+
117
+ this.writeU8(value ? 1 : 0);
118
+ }
119
+
120
+ public writeU256(value: u256): void {
121
+ if (this.trackDataTypes) this.selectorDatatype.push(u8(BufferDataType.U256));
122
+ this.allocSafe(32);
123
+
124
+ const bytes = value.toUint8Array(true);
125
+ for (let i: i32 = 0; i < 32; i++) {
126
+ this.writeU8(bytes[i] || 0);
127
+ }
128
+ }
129
+
130
+ public writeTuple(value: u256[]): void {
131
+ this.allocSafe(4 + value.length * 32);
132
+ this.writeU32(u32(value.length));
133
+
134
+ for (let i = 0; i < value.length; i++) {
135
+ this.writeU256(value[i]);
136
+ }
137
+ }
138
+
139
+ public writeBytes(value: Uint8Array): void {
140
+ this.allocSafe(value.length);
141
+
142
+ for (let i = 0; i < value.length; i++) {
143
+ this.writeU8(value[i]);
144
+ }
145
+ }
146
+
147
+ public writeBytesU8Array(value: u8[]): void {
148
+ this.allocSafe(value.length);
149
+
150
+ for (let i = 0; i < value.length; i++) {
151
+ this.writeU8(value[i]);
152
+ }
153
+ }
154
+
155
+ public writeBytesWithLength(value: Uint8Array): void {
156
+ const length: u32 = u32(value.byteLength);
157
+
158
+ this.allocSafe(length + 4);
159
+ this.writeU32(length);
160
+
161
+ for (let i: u32 = 0; i < length; i++) {
162
+ this.writeU8(value[i]);
163
+ }
164
+ }
165
+
166
+ public writeString(value: string): void {
167
+ if (this.trackDataTypes) this.selectorDatatype.push(u8(BufferDataType.STRING));
168
+
169
+ for (let i: i32 = 0; i < value.length; i++) {
170
+ this.writeU8(u8(value.charCodeAt(i)));
171
+ }
172
+ }
173
+
174
+ public writeAddress(value: Address): void {
175
+ if (this.trackDataTypes) this.selectorDatatype.push(u8(BufferDataType.ADDRESS));
176
+
177
+ const bytes = this.fromAddress(value);
178
+ this.writeBytes(bytes);
179
+ }
180
+
181
+ public writeStringWithLength(value: string): void {
182
+ this.writeU16(u16(value.length));
183
+
184
+ this.writeString(value);
185
+ }
186
+
187
+ public writeViewSelectorMap(map: SelectorsMap): void {
188
+ this.writeU16(u16(map.size));
189
+
190
+ const keys = map.keys();
191
+ for (let i = 0; i < keys.length; i++) {
192
+ const key: u32 = keys[i] as u32;
193
+ const value = map.get(key);
194
+
195
+ this.writeBytes(value);
196
+ }
197
+ }
198
+
199
+ public writeAddressValueTupleMap(map: Map<Address, u256>): void {
200
+ if (map.size > 65535) throw new Revert('Map size is too large');
201
+ this.writeU16(u16(map.size));
202
+
203
+ const keys = map.keys();
204
+ for (let i = 0; i < keys.length; i++) {
205
+ const key: Address = keys[i];
206
+ const value: u256 = map.get(key) || u256.Zero;
207
+
208
+ this.writeAddress(key);
209
+ this.writeU256(value);
210
+ }
211
+ }
212
+
213
+ public writeLimitedAddressBytesMap(map: Map<Address, Uint8Array[]>): void {
214
+ if (map.size > 8) throw new Revert('Too many contract called.'); // no more than 8 different contracts.
215
+
216
+ this.writeU8(u8(map.size));
217
+
218
+ const keys: Address[] = map.keys();
219
+ for (let i: i32 = 0; i < keys.length; i++) {
220
+ const address: Address = keys[i];
221
+ const calls: Uint8Array[] = map.get(address) || [];
222
+
223
+ if (calls.length > 10) throw new Revert('Too many calls.'); // no more than 16 different calls.
224
+
225
+ this.writeAddress(address);
226
+ this.writeU8(u8(calls.length));
227
+
228
+ for (let j: i32 = 0; j < calls.length; j++) {
229
+ this.writeBytesWithLength(calls[j]);
230
+ }
231
+ }
232
+ }
233
+
234
+ public writeMethodSelectorsMap(map: Selector[]): void {
235
+ this.writeU16(u16(map.length));
236
+
237
+ for (let i = 0; i < map.length; i++) {
238
+ this.writeSelector(map[i]);
239
+ }
240
+ }
241
+
242
+ public getBuffer(clear: boolean = true): Uint8Array {
243
+ const buf = new Uint8Array(this.buffer.byteLength);
244
+ for (let i: u32 = 0; i < u32(this.buffer.byteLength); i++) {
245
+ buf[i] = this.buffer.getUint8(i);
246
+ }
247
+
248
+ if (clear) this.clear();
249
+
250
+ return buf;
251
+ }
252
+
253
+ public toBytesReader(): BytesReader {
254
+ return new BytesReader(this.getBuffer());
255
+ }
256
+
257
+ public getOffset(): u32 {
258
+ return this.currentOffset;
259
+ }
260
+
261
+ public setOffset(offset: u32): void {
262
+ this.currentOffset = offset;
263
+ }
264
+
265
+ public clear(): void {
266
+ this.currentOffset = 0;
267
+ this.buffer = this.getDefaultBuffer();
268
+ this.selectorDatatype = [];
269
+ }
270
+
271
+ public allocSafe(size: u32): void {
272
+ if (this.currentOffset + size > u32(this.buffer.byteLength)) {
273
+ const sizeDiff: u32 = size - (u32(this.buffer.byteLength) - this.currentOffset);
274
+
275
+ this.resize(sizeDiff);
276
+ }
277
+ }
278
+
279
+ public writeABISelector(name: string, selector: Selector): void {
280
+ this.writeStringWithLength(name);
281
+ this.writeSelector(selector);
282
+ }
283
+
284
+ public getSelectorDataType(): u64 {
285
+ let hash: u64 = 0;
286
+ if (this.selectorDatatype.length === 0) return hash;
287
+
288
+ return cyrb53a(this.selectorDatatype);
289
+ }
290
+
291
+ private getChecksum(): u32 {
292
+ let checksum: u32 = 0;
293
+ for (let i = 0; i < this.buffer.byteLength; i++) {
294
+ checksum += this.buffer.getUint8(i);
295
+ }
296
+
297
+ return checksum % 2 ** 32;
298
+ }
299
+
300
+ private writeMethodSelectorMap(value: Set<Selector>): void {
301
+ this.writeU16(u16(value.size));
302
+
303
+ const keys = value.values();
304
+ for (let i = 0; i < keys.length; i++) {
305
+ const key = keys[i];
306
+
307
+ this.writeSelector(key);
308
+ }
309
+ }
310
+
311
+ private min(value1: i32, value2: i32): i32 {
312
+ return value1 < value2 ? value1 : value2;
313
+ }
314
+
315
+ private fromAddress(value: Address): Uint8Array {
316
+ if (value.length > i32(ADDRESS_BYTE_LENGTH)) {
317
+ throw new Revert(`Address is too long ${value.length} > ${ADDRESS_BYTE_LENGTH} bytes`);
318
+ }
319
+
320
+ const length: i32 = this.min(value.length + 1, ADDRESS_BYTE_LENGTH);
321
+ const bytes: Uint8Array = new Uint8Array(length);
322
+ for (let i: i32 = 0; i < value.length; i++) {
323
+ bytes[i] = value.charCodeAt(i);
324
+ }
325
+
326
+ if (value.length < i32(ADDRESS_BYTE_LENGTH)) {
327
+ bytes[value.length] = 0;
328
+ }
329
+
330
+ return bytes;
331
+ }
332
+
333
+ private resize(size: u32): void {
334
+ const buf: Uint8Array = new Uint8Array(u32(this.buffer.byteLength) + size);
335
+
336
+ for (let i: i32 = 0; i < this.buffer.byteLength; i++) {
337
+ buf[i] = this.buffer.getUint8(i);
338
+ }
339
+
340
+ this.buffer = new DataView(buf.buffer);
341
+ }
342
+
343
+ private getDefaultBuffer(length: i32 = 1): DataView {
344
+ return new DataView(new ArrayBuffer(length));
345
+ }
346
+ }