@btc-vision/btc-runtime 1.3.16 → 1.3.18

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 (32) hide show
  1. package/package.json +1 -1
  2. package/runtime/buffer/BytesReader.ts +37 -22
  3. package/runtime/buffer/BytesWriter.ts +42 -28
  4. package/runtime/contracts/DeployableOP_20.ts +10 -9
  5. package/runtime/contracts/OP_NET.ts +6 -5
  6. package/runtime/env/BlockchainEnvironment.ts +15 -14
  7. package/runtime/events/predefined/ApproveEvent.ts +4 -3
  8. package/runtime/events/predefined/BurnEvent.ts +3 -2
  9. package/runtime/events/predefined/ClaimEvent.ts +3 -2
  10. package/runtime/events/predefined/MintEvent.ts +4 -3
  11. package/runtime/events/predefined/StakeEvent.ts +3 -2
  12. package/runtime/events/predefined/TransferEvent.ts +4 -3
  13. package/runtime/events/predefined/UnstakeEvent.ts +3 -2
  14. package/runtime/exports/index.ts +1 -0
  15. package/runtime/index.ts +1 -0
  16. package/runtime/shared-libraries/OP20Utils.ts +3 -2
  17. package/runtime/shared-libraries/TransferHelper.ts +12 -6
  18. package/runtime/storage/Serializable.ts +5 -4
  19. package/runtime/storage/StoredAddress.ts +2 -5
  20. package/runtime/storage/StoredAddressArray.ts +3 -7
  21. package/runtime/storage/StoredBooleanArray.ts +3 -7
  22. package/runtime/storage/StoredString.ts +5 -7
  23. package/runtime/storage/StoredU128Array.ts +3 -5
  24. package/runtime/storage/StoredU16Array.ts +4 -6
  25. package/runtime/storage/StoredU256.ts +5 -4
  26. package/runtime/storage/StoredU256Array.ts +3 -5
  27. package/runtime/storage/StoredU32Array.ts +470 -0
  28. package/runtime/storage/StoredU64.ts +5 -4
  29. package/runtime/tests/tests.ts +2 -1
  30. package/runtime/types/Address.ts +2 -3
  31. package/runtime/utils/index.ts +2 -1
  32. package/runtime/utils/lengths.ts +18 -0
@@ -1,9 +1,10 @@
1
1
  import { u256 } from '@btc-vision/as-bignum/assembly';
2
- import { encodeSelector, Selector } from '../math/abi';
3
- import { Address, ADDRESS_BYTE_LENGTH } from '../types/Address';
4
2
  import { BytesWriter } from '../buffer/BytesWriter';
5
3
  import { Blockchain } from '../env';
4
+ import { encodeSelector, Selector } from '../math/abi';
5
+ import { Address } from '../types/Address';
6
6
  import { Revert } from '../types/Revert';
7
+ import { ADDRESS_BYTE_LENGTH, SELECTOR_BYTE_LENGTH, U256_BYTE_LENGTH } from '../utils/lengths';
7
8
 
8
9
  export class TransferHelper {
9
10
  public static get APPROVE_SELECTOR(): Selector {
@@ -19,7 +20,9 @@ export class TransferHelper {
19
20
  }
20
21
 
21
22
  public static safeApprove(token: Address, spender: Address, amount: u256): void {
22
- const calldata = new BytesWriter(4 + ADDRESS_BYTE_LENGTH + 32);
23
+ const calldata = new BytesWriter(
24
+ SELECTOR_BYTE_LENGTH + ADDRESS_BYTE_LENGTH + U256_BYTE_LENGTH,
25
+ );
23
26
  calldata.writeSelector(this.APPROVE_SELECTOR);
24
27
  calldata.writeAddress(spender);
25
28
  calldata.writeU256(amount);
@@ -33,7 +36,9 @@ export class TransferHelper {
33
36
  }
34
37
 
35
38
  public static safeTransfer(token: Address, to: Address, amount: u256): void {
36
- const calldata = new BytesWriter(4 + ADDRESS_BYTE_LENGTH + 32);
39
+ const calldata = new BytesWriter(
40
+ SELECTOR_BYTE_LENGTH + ADDRESS_BYTE_LENGTH + U256_BYTE_LENGTH,
41
+ );
37
42
  calldata.writeSelector(this.TRANSFER_SELECTOR);
38
43
  calldata.writeAddress(to);
39
44
  calldata.writeU256(amount);
@@ -47,9 +52,10 @@ export class TransferHelper {
47
52
  }
48
53
 
49
54
  public static safeTransferFrom(token: Address, from: Address, to: Address, amount: u256): void {
50
- const calldata = new BytesWriter(4 + ADDRESS_BYTE_LENGTH + ADDRESS_BYTE_LENGTH + 32);
55
+ const calldata = new BytesWriter(
56
+ SELECTOR_BYTE_LENGTH + ADDRESS_BYTE_LENGTH * 2 + U256_BYTE_LENGTH,
57
+ );
51
58
  calldata.writeSelector(this.TRANSFER_FROM_SELECTOR);
52
-
53
59
  calldata.writeAddress(from);
54
60
  calldata.writeAddress(to);
55
61
  calldata.writeU256(amount);
@@ -1,10 +1,11 @@
1
1
  import { u256 } from '@btc-vision/as-bignum/assembly';
2
+ import { BytesReader } from '../buffer/BytesReader';
3
+ import { BytesWriter } from '../buffer/BytesWriter';
2
4
  import { Blockchain } from '../env';
5
+ import { encodePointer } from '../math/abi';
3
6
  import { MemorySlotPointer } from '../memory/MemorySlotPointer';
4
- import { BytesWriter } from '../buffer/BytesWriter';
5
- import { BytesReader } from '../buffer/BytesReader';
6
7
  import { Revert } from '../types/Revert';
7
- import { encodePointer } from '../math/abi';
8
+ import { U256_BYTE_LENGTH } from '../utils/lengths';
8
9
 
9
10
  // Similar to a struct in Solidity. (Use in worst case scenario, consume a lot of gas)
10
11
  export abstract class Serializable {
@@ -99,7 +100,7 @@ export abstract class Serializable {
99
100
  }
100
101
 
101
102
  protected getPointer(subPointer: u256, index: u8): u256 {
102
- const writer = new BytesWriter(32);
103
+ const writer = new BytesWriter(U256_BYTE_LENGTH);
103
104
  writer.writeU256(subPointer);
104
105
 
105
106
  // Discard the first byte for offset.
@@ -1,7 +1,7 @@
1
1
  import { u256 } from '@btc-vision/as-bignum/assembly';
2
+ import { BytesWriter } from '../buffer/BytesWriter';
2
3
  import { Blockchain } from '../env';
3
4
  import { encodePointer } from '../math/abi';
4
- import { BytesWriter } from '../buffer/BytesWriter';
5
5
  import { Address } from '../types/Address';
6
6
 
7
7
  @final
@@ -9,10 +9,7 @@ export class StoredAddress {
9
9
  private readonly addressPointer: u256;
10
10
  private readonly defaultValue: u256;
11
11
 
12
- constructor(
13
- public pointer: u16,
14
- defaultValue: Address,
15
- ) {
12
+ constructor(public pointer: u16, defaultValue: Address) {
16
13
  const writer = new BytesWriter(32);
17
14
 
18
15
  this.defaultValue = u256.fromBytes(defaultValue);
@@ -1,9 +1,9 @@
1
1
  import { u256 } from '@btc-vision/as-bignum/assembly';
2
- import { Blockchain } from '../env';
3
2
  import { BytesWriter } from '../buffer/BytesWriter';
4
- import { SafeMath } from '../types/SafeMath';
3
+ import { Blockchain } from '../env';
5
4
  import { Address } from '../types/Address';
6
5
  import { Revert } from '../types/Revert';
6
+ import { SafeMath } from '../types/SafeMath';
7
7
 
8
8
  /**
9
9
  * @class StoredAddressArray
@@ -35,11 +35,7 @@ export class StoredAddressArray {
35
35
  * @param {Uint8Array} subPointer - The sub-pointer for memory slot addressing.
36
36
  * @param {Address} defaultValue - The default Address value if storage is uninitialized.
37
37
  */
38
- constructor(
39
- public pointer: u16,
40
- public subPointer: Uint8Array,
41
- private defaultValue: Address,
42
- ) {
38
+ constructor(public pointer: u16, public subPointer: Uint8Array, private defaultValue: Address) {
43
39
  // Initialize the base pointer
44
40
  const writer = new BytesWriter(32);
45
41
  writer.writeU16(pointer);
@@ -1,8 +1,8 @@
1
1
  import { u256 } from '@btc-vision/as-bignum/assembly';
2
- import { Blockchain } from '../env';
3
2
  import { BytesWriter } from '../buffer/BytesWriter';
4
- import { SafeMath } from '../types/SafeMath';
3
+ import { Blockchain } from '../env';
5
4
  import { Revert } from '../types/Revert';
5
+ import { SafeMath } from '../types/SafeMath';
6
6
 
7
7
  /**
8
8
  * @class StoredBooleanArray
@@ -33,11 +33,7 @@ export class StoredBooleanArray {
33
33
  * @param {Uint8Array} subPointer - The sub-pointer for memory slot addressing.
34
34
  * @param {u256} defaultValue - The default u256 value if storage is uninitialized.
35
35
  */
36
- constructor(
37
- public pointer: u16,
38
- public subPointer: Uint8Array,
39
- private defaultValue: u256,
40
- ) {
36
+ constructor(public pointer: u16, public subPointer: Uint8Array, private defaultValue: u256) {
41
37
  // Initialize the base u256 pointer using the primary pointer and subPointer
42
38
  const writer = new BytesWriter(32);
43
39
  writer.writeU16(pointer);
@@ -1,15 +1,13 @@
1
1
  import { u256 } from '@btc-vision/as-bignum/assembly';
2
+ import { BytesWriter } from '../buffer/BytesWriter';
2
3
  import { Blockchain } from '../env';
3
- import { SafeMath } from '../types/SafeMath';
4
4
  import { encodePointer } from '../math/abi';
5
- import { BytesWriter } from '../buffer/BytesWriter';
5
+ import { SafeMath } from '../types/SafeMath';
6
+ import { U256_BYTE_LENGTH } from '../utils/lengths';
6
7
 
7
8
  @final
8
9
  export class StoredString {
9
- constructor(
10
- public pointer: u16,
11
- private defaultValue?: string,
12
- ) {}
10
+ constructor(public pointer: u16, private defaultValue?: string) {}
13
11
 
14
12
  private _value: string = '';
15
13
 
@@ -33,7 +31,7 @@ export class StoredString {
33
31
  }
34
32
 
35
33
  private getPointer(key: u256): u256 {
36
- const buf = new BytesWriter(32);
34
+ const buf = new BytesWriter(U256_BYTE_LENGTH);
37
35
  buf.writeU256(key);
38
36
 
39
37
  return encodePointer(this.pointer, buf.getBuffer());
@@ -363,11 +363,9 @@ export class StoredU128Array {
363
363
  throw new Revert('SetLength operation failed: Length exceeds maximum allowed value.');
364
364
  }
365
365
 
366
- if (newLength < this._length) {
367
- // Truncate the array if newLength is smaller
368
- for (let i: u64 = newLength; i < this._length; i++) {
369
- this.delete(i);
370
- }
366
+ if (newLength > this._startIndex) {
367
+ this._startIndex = newLength;
368
+ this._isChangedStartIndex = true;
371
369
  }
372
370
 
373
371
  this._length = newLength;
@@ -108,7 +108,7 @@ export class StoredU16Array {
108
108
  const newIndex: u64 = this._length;
109
109
  const wrappedIndex: u64 =
110
110
  newIndex < this.MAX_LENGTH ? newIndex : newIndex % this.MAX_LENGTH;
111
-
111
+
112
112
  const slotIndex: u64 = wrappedIndex / 16;
113
113
  const subIndex: u8 = <u8>(wrappedIndex % 16);
114
114
 
@@ -354,11 +354,9 @@ export class StoredU16Array {
354
354
  throw new Revert('SetLength operation failed: Length exceeds maximum allowed value.');
355
355
  }
356
356
 
357
- if (newLength < this._length) {
358
- // Truncate the array if newLength is smaller
359
- for (let i: u64 = newLength; i < this._length; i++) {
360
- this.delete(i);
361
- }
357
+ if (newLength > this._startIndex) {
358
+ this._startIndex = newLength;
359
+ this._isChangedStartIndex = true;
362
360
  }
363
361
 
364
362
  this._length = newLength;
@@ -1,9 +1,10 @@
1
1
  import { u256 } from '@btc-vision/as-bignum/assembly';
2
- import { SafeMath } from '../types/SafeMath';
3
- import { MemorySlotPointer } from '../memory/MemorySlotPointer';
2
+ import { BytesWriter } from '../buffer/BytesWriter';
4
3
  import { Blockchain } from '../env';
5
4
  import { encodePointer } from '../math/abi';
6
- import { BytesWriter } from '../buffer/BytesWriter';
5
+ import { MemorySlotPointer } from '../memory/MemorySlotPointer';
6
+ import { SafeMath } from '../types/SafeMath';
7
+ import { U256_BYTE_LENGTH } from '../utils/lengths';
7
8
 
8
9
  @final
9
10
  export class StoredU256 {
@@ -14,7 +15,7 @@ export class StoredU256 {
14
15
  public subPointer: MemorySlotPointer,
15
16
  private defaultValue: u256,
16
17
  ) {
17
- const writer = new BytesWriter(32);
18
+ const writer = new BytesWriter(U256_BYTE_LENGTH);
18
19
  writer.writeU256(subPointer);
19
20
 
20
21
  this.u256Pointer = encodePointer(pointer, writer.getBuffer());
@@ -364,11 +364,9 @@ export class StoredU256Array {
364
364
  throw new Revert('SetLength operation failed: Length exceeds maximum allowed value.');
365
365
  }
366
366
 
367
- if (newLength < this._length) {
368
- // Truncate the array if newLength is smaller
369
- for (let i: u64 = newLength; i < this._length; i++) {
370
- this.delete(i);
371
- }
367
+ if (newLength > this._startIndex) {
368
+ this._startIndex = newLength;
369
+ this._isChangedStartIndex = true;
372
370
  }
373
371
 
374
372
  this._length = newLength;