@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,53 +1,53 @@
1
- import { MemorySlotData } from './MemorySlot';
2
- import { u256 } from 'as-bignum/assembly';
3
- import { Blockchain } from '../env';
4
- import { MemorySlotPointer } from './MemorySlotPointer';
5
- import { encodePointer } from '../math/abi';
6
-
7
- @final
8
- export class KeyMerger<K extends string, K2 extends string, V extends MemorySlotData<u256>> {
9
- public parentKey: K;
10
-
11
- public pointer: u16;
12
-
13
- constructor(
14
- parent: K,
15
- pointer: u16,
16
- private readonly defaultValue: V,
17
- ) {
18
- this.pointer = pointer;
19
-
20
- this.parentKey = parent;
21
- }
22
-
23
- public set(key2: K2, value: V): this {
24
- const mergedKey: string = `${this.parentKey}${key2}`;
25
- const keyHash: MemorySlotPointer = encodePointer(mergedKey);
26
-
27
- Blockchain.setStorageAt(this.pointer, keyHash, value);
28
-
29
- return this;
30
- }
31
-
32
- public get(key: K): MemorySlotData<u256> {
33
- const mergedKey: string = `${this.parentKey}${key}`;
34
-
35
- return Blockchain.getStorageAt(this.pointer, encodePointer(mergedKey), this.defaultValue);
36
- }
37
-
38
- public has(key: K): bool {
39
- const mergedKey: string = `${this.parentKey}${key}`;
40
-
41
- return Blockchain.hasStorageAt(this.pointer, encodePointer(mergedKey));
42
- }
43
-
44
- @unsafe
45
- public delete(_key: K): bool {
46
- throw new Error('Method not implemented.');
47
- }
48
-
49
- @unsafe
50
- public clear(): void {
51
- throw new Error('Clear method not implemented.');
52
- }
53
- }
1
+ import { MemorySlotData } from './MemorySlot';
2
+ import { u256 } from 'as-bignum/assembly';
3
+ import { Blockchain } from '../env';
4
+ import { MemorySlotPointer } from './MemorySlotPointer';
5
+ import { encodePointer } from '../math/abi';
6
+
7
+ @final
8
+ export class KeyMerger<K extends string, K2 extends string, V extends MemorySlotData<u256>> {
9
+ public parentKey: K;
10
+
11
+ public pointer: u16;
12
+
13
+ constructor(
14
+ parent: K,
15
+ pointer: u16,
16
+ private readonly defaultValue: V,
17
+ ) {
18
+ this.pointer = pointer;
19
+
20
+ this.parentKey = parent;
21
+ }
22
+
23
+ public set(key2: K2, value: V): this {
24
+ const mergedKey: string = `${this.parentKey}${key2}`;
25
+ const keyHash: MemorySlotPointer = encodePointer(mergedKey);
26
+
27
+ Blockchain.setStorageAt(this.pointer, keyHash, value);
28
+
29
+ return this;
30
+ }
31
+
32
+ public get(key: K): MemorySlotData<u256> {
33
+ const mergedKey: string = `${this.parentKey}${key}`;
34
+
35
+ return Blockchain.getStorageAt(this.pointer, encodePointer(mergedKey), this.defaultValue);
36
+ }
37
+
38
+ public has(key: K): bool {
39
+ const mergedKey: string = `${this.parentKey}${key}`;
40
+
41
+ return Blockchain.hasStorageAt(this.pointer, encodePointer(mergedKey));
42
+ }
43
+
44
+ @unsafe
45
+ public delete(_key: K): bool {
46
+ throw new Error('Method not implemented.');
47
+ }
48
+
49
+ @unsafe
50
+ public clear(): void {
51
+ throw new Error('Clear method not implemented.');
52
+ }
53
+ }
@@ -1 +1 @@
1
- export type MemorySlotData<T> = T;
1
+ export type MemorySlotData<T> = T;
@@ -1,3 +1,3 @@
1
- import { u256 } from 'as-bignum/assembly';
2
-
3
- export type MemorySlotPointer = u256;
1
+ import { u256 } from 'as-bignum/assembly';
2
+
3
+ export type MemorySlotPointer = u256;
@@ -1,62 +1,62 @@
1
- import { MemorySlotData } from './MemorySlot';
2
- import { u256 } from 'as-bignum/assembly';
3
- import { KeyMerger } from './KeyMerger';
4
-
5
- @final
6
- export class MultiAddressMemoryMap<
7
- K extends string,
8
- K2 extends string,
9
- V extends MemorySlotData<u256>,
10
- > extends Map<K, KeyMerger<K, K2, V>> {
11
- public pointer: u16;
12
-
13
- constructor(
14
- pointer: u16,
15
- private readonly defaultValue: V,
16
- ) {
17
- super();
18
-
19
- this.pointer = pointer;
20
- }
21
-
22
- public get(key: K): KeyMerger<K, K2, V> {
23
- this.createKeyMerger(key);
24
-
25
- return super.get(key);
26
- }
27
-
28
- public setUpperKey(key: K, key2: K2, value: V): this {
29
- this.createKeyMerger(key);
30
-
31
- const subMap = super.get(key);
32
- if (subMap) {
33
- subMap.set(key2, value);
34
- }
35
-
36
- return this;
37
- }
38
-
39
- public set(key: K, value: KeyMerger<K, K2, V>): this {
40
- this.createKeyMerger(key);
41
-
42
- return <this>super.set(key, value);
43
- }
44
-
45
- public has(key: K): bool {
46
- return super.has(key);
47
- }
48
-
49
- public delete(key: K): bool {
50
- return super.delete(key);
51
- }
52
-
53
- public clear(): void {
54
- super.clear();
55
- }
56
-
57
- private createKeyMerger(key: K): void {
58
- if (!super.has(key)) {
59
- super.set(key, new KeyMerger<K, K2, V>(key, this.pointer, this.defaultValue));
60
- }
61
- }
62
- }
1
+ import { MemorySlotData } from './MemorySlot';
2
+ import { u256 } from 'as-bignum/assembly';
3
+ import { KeyMerger } from './KeyMerger';
4
+
5
+ @final
6
+ export class MultiAddressMemoryMap<
7
+ K extends string,
8
+ K2 extends string,
9
+ V extends MemorySlotData<u256>,
10
+ > extends Map<K, KeyMerger<K, K2, V>> {
11
+ public pointer: u16;
12
+
13
+ constructor(
14
+ pointer: u16,
15
+ private readonly defaultValue: V,
16
+ ) {
17
+ super();
18
+
19
+ this.pointer = pointer;
20
+ }
21
+
22
+ public get(key: K): KeyMerger<K, K2, V> {
23
+ this.createKeyMerger(key);
24
+
25
+ return super.get(key);
26
+ }
27
+
28
+ public setUpperKey(key: K, key2: K2, value: V): this {
29
+ this.createKeyMerger(key);
30
+
31
+ const subMap = super.get(key);
32
+ if (subMap) {
33
+ subMap.set(key2, value);
34
+ }
35
+
36
+ return this;
37
+ }
38
+
39
+ public set(key: K, value: KeyMerger<K, K2, V>): this {
40
+ this.createKeyMerger(key);
41
+
42
+ return <this>super.set(key, value);
43
+ }
44
+
45
+ public has(key: K): bool {
46
+ return super.has(key);
47
+ }
48
+
49
+ public delete(key: K): bool {
50
+ return super.delete(key);
51
+ }
52
+
53
+ public clear(): void {
54
+ super.clear();
55
+ }
56
+
57
+ private createKeyMerger(key: K): void {
58
+ if (!super.has(key)) {
59
+ super.set(key, new KeyMerger<K, K2, V>(key, this.pointer, this.defaultValue));
60
+ }
61
+ }
62
+ }
@@ -1,64 +1,64 @@
1
- import { u256 } from 'as-bignum/assembly/integer/u256';
2
- import { encodeSelector, Selector } from '../math/abi';
3
- import { Address } from '../types/Address';
4
- import { BytesWriter } from '../buffer/BytesWriter';
5
- import { Blockchain } from '../env';
6
- import { Revert } from '../types/Revert';
7
-
8
- export class TransferHelper {
9
- public static get APPROVE_SELECTOR(): Selector {
10
- return encodeSelector('approve');
11
- }
12
-
13
- public static get TRANSFER_SELECTOR(): Selector {
14
- return encodeSelector('transfer');
15
- }
16
-
17
- public static get TRANSFER_FROM_SELECTOR(): Selector {
18
- return encodeSelector('transferFrom');
19
- }
20
-
21
- public static safeApprove(token: Address, spender: Address, amount: u256): void {
22
- const calldata = new BytesWriter();
23
- calldata.writeSelector(this.APPROVE_SELECTOR);
24
- calldata.writeAddress(spender);
25
- calldata.writeU256(amount);
26
-
27
- const response = Blockchain.call(token, calldata);
28
- const isOk = response.readBoolean();
29
-
30
- if (!isOk) {
31
- throw new Revert(`TransferHelper: APPROVE_FAILED`);
32
- }
33
- }
34
-
35
- public static safeTransfer(token: Address, to: Address, amount: u256): void {
36
- const calldata = new BytesWriter();
37
- calldata.writeSelector(this.TRANSFER_SELECTOR);
38
- calldata.writeAddress(to);
39
- calldata.writeU256(amount);
40
-
41
- const response = Blockchain.call(token, calldata);
42
- const isOk = response.readBoolean();
43
-
44
- if (!isOk) {
45
- throw new Revert(`TransferHelper: TRANSFER_FAILED`);
46
- }
47
- }
48
-
49
- public static safeTransferFrom(token: Address, from: Address, to: Address, amount: u256): void {
50
- const calldata = new BytesWriter();
51
- calldata.writeSelector(this.TRANSFER_FROM_SELECTOR);
52
-
53
- calldata.writeAddress(from);
54
- calldata.writeAddress(to);
55
- calldata.writeU256(amount);
56
-
57
- const response = Blockchain.call(token, calldata);
58
- const isOk = response.readBoolean();
59
-
60
- if (!isOk) {
61
- throw new Revert(`TransferHelper: TRANSFER_FROM_FAILED`);
62
- }
63
- }
64
- }
1
+ import { u256 } from 'as-bignum/assembly/integer/u256';
2
+ import { encodeSelector, Selector } from '../math/abi';
3
+ import { Address } from '../types/Address';
4
+ import { BytesWriter } from '../buffer/BytesWriter';
5
+ import { Blockchain } from '../env';
6
+ import { Revert } from '../types/Revert';
7
+
8
+ export class TransferHelper {
9
+ public static get APPROVE_SELECTOR(): Selector {
10
+ return encodeSelector('approve');
11
+ }
12
+
13
+ public static get TRANSFER_SELECTOR(): Selector {
14
+ return encodeSelector('transfer');
15
+ }
16
+
17
+ public static get TRANSFER_FROM_SELECTOR(): Selector {
18
+ return encodeSelector('transferFrom');
19
+ }
20
+
21
+ public static safeApprove(token: Address, spender: Address, amount: u256): void {
22
+ const calldata = new BytesWriter();
23
+ calldata.writeSelector(this.APPROVE_SELECTOR);
24
+ calldata.writeAddress(spender);
25
+ calldata.writeU256(amount);
26
+
27
+ const response = Blockchain.call(token, calldata);
28
+ const isOk = response.readBoolean();
29
+
30
+ if (!isOk) {
31
+ throw new Revert(`TransferHelper: APPROVE_FAILED`);
32
+ }
33
+ }
34
+
35
+ public static safeTransfer(token: Address, to: Address, amount: u256): void {
36
+ const calldata = new BytesWriter();
37
+ calldata.writeSelector(this.TRANSFER_SELECTOR);
38
+ calldata.writeAddress(to);
39
+ calldata.writeU256(amount);
40
+
41
+ const response = Blockchain.call(token, calldata);
42
+ const isOk = response.readBoolean();
43
+
44
+ if (!isOk) {
45
+ throw new Revert(`TransferHelper: TRANSFER_FAILED`);
46
+ }
47
+ }
48
+
49
+ public static safeTransferFrom(token: Address, from: Address, to: Address, amount: u256): void {
50
+ const calldata = new BytesWriter();
51
+ calldata.writeSelector(this.TRANSFER_FROM_SELECTOR);
52
+
53
+ calldata.writeAddress(from);
54
+ calldata.writeAddress(to);
55
+ calldata.writeU256(amount);
56
+
57
+ const response = Blockchain.call(token, calldata);
58
+ const isOk = response.readBoolean();
59
+
60
+ if (!isOk) {
61
+ throw new Revert(`TransferHelper: TRANSFER_FROM_FAILED`);
62
+ }
63
+ }
64
+ }
@@ -0,0 +1,75 @@
1
+ import { u256 } from 'as-bignum/assembly';
2
+ import { Blockchain } from '../env';
3
+ import { MemorySlotPointer } from '../memory/MemorySlotPointer';
4
+ import { BytesWriter } from '../buffer/BytesWriter';
5
+ import { BytesReader } from '../buffer/BytesReader';
6
+
7
+ export abstract class Serializable {
8
+ protected pointer: u16;
9
+ protected subPointer:MemorySlotPointer;
10
+
11
+ protected constructor(pointer: u16,
12
+ subPointer:MemorySlotPointer) {
13
+ this.pointer = pointer;
14
+ this.subPointer = subPointer;
15
+ }
16
+
17
+ public abstract get chunkCount(): i32;
18
+ public abstract get length(): i32;
19
+ public abstract writeToBuffer(): BytesWriter;
20
+ public abstract readFromBuffer(reader: BytesReader): void;
21
+
22
+ public load() :void {
23
+ const chunks: u256[] = [];
24
+
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
+ chunks.push(chunk);
28
+ }
29
+
30
+ const reader = this.chunksToBytes(chunks);
31
+
32
+ this.readFromBuffer(reader);
33
+ }
34
+
35
+ public save(): void {
36
+ const writer: BytesWriter = this.writeToBuffer();
37
+
38
+ const buffer = writer.getBuffer();
39
+
40
+ const chunks: u256[] = this.bytesToChunks(buffer);
41
+
42
+ for (let index: i32 = 0; index < chunks.length; index++) {
43
+ Blockchain.setStorageAt(
44
+ this.pointer,
45
+ u256.add(this.subPointer, u256.fromU32(index)),
46
+ chunks[index],
47
+ );
48
+ }
49
+ }
50
+
51
+ protected bytesToChunks(buffer: Uint8Array): u256[] {
52
+ const chunks: u256[] = [];
53
+
54
+ for (let index: i32 = 0; index < buffer.byteLength; index += 32) {
55
+ const chunk = buffer.slice(index, index + 32);
56
+ chunks.push(u256.fromBytes(chunk, true));
57
+ }
58
+
59
+ return chunks;
60
+ }
61
+
62
+ protected chunksToBytes(chunks: u256[]): BytesReader {
63
+ const buffer: Uint8Array = new Uint8Array(this.length);
64
+ let offset: i32 = 0;
65
+
66
+ for (let indexChunk: i32 = 0; indexChunk < chunks.length; indexChunk++) {
67
+ const bytes: u8[] = chunks[indexChunk].toBytes(true);
68
+ for (let indexByte: i32 = 0; indexByte < bytes.length; indexByte++) {
69
+ buffer[offset++] = bytes[indexByte];
70
+ }
71
+ }
72
+
73
+ return new BytesReader(buffer);
74
+ }
75
+ }