@btc-vision/btc-runtime 1.1.0 → 1.1.2

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.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "Bitcoin Smart Contract Runtime",
5
5
  "main": "btc/index.ts",
6
6
  "types": "btc/index.ts",
@@ -14,48 +14,56 @@ import { MultiAddressMemoryMap } from '../memory/MultiAddressMemoryMap';
14
14
  import { StoredU256 } from '../storage/StoredU256';
15
15
  import { ApproveEvent, BurnEvent, MintEvent, TransferEvent } from '../events/predefined';
16
16
  import { StoredString } from '../storage/StoredString';
17
+ import { OP20InitParameters } from './interfaces/OP20InitParameters';
17
18
 
18
- export class OP20InitParameters {
19
- readonly maxSupply: u256;
20
- readonly decimals: u8;
21
- readonly name: string;
22
- readonly symbol: string;
23
-
24
- constructor(maxSupply: u256, decimals: u8, name: string, symbol: string) {
25
- this.maxSupply = maxSupply;
26
- this.decimals = decimals;
27
- this.name = name;
28
- this.symbol = symbol;
29
- }
30
- }
19
+ const maxSupplyPointer: u16 = Blockchain.nextPointer;
20
+ const decimalsPointer: u16 = Blockchain.nextPointer;
21
+ const namePointer: u16 = Blockchain.nextPointer;
22
+ const symbolPointer: u16 = Blockchain.nextPointer;
23
+ const totalSupplyPointer: u16 = Blockchain.nextPointer;
24
+ const allowanceMapPointer: u16 = Blockchain.nextPointer;
25
+ const balanceOfMapPointer: u16 = Blockchain.nextPointer;
31
26
 
32
27
  export abstract class DeployableOP_20 extends OP_NET implements IOP_20 {
33
28
  protected readonly allowanceMap: MultiAddressMemoryMap<Address, Address, MemorySlotData<u256>>;
34
29
  protected readonly balanceOfMap: AddressMemoryMap<Address, MemorySlotData<u256>>;
35
30
 
31
+ protected readonly _maxSupply: StoredU256;
32
+ protected readonly _decimals: StoredU256;
33
+ protected readonly _name: StoredString;
34
+ protected readonly _symbol: StoredString;
35
+
36
36
  protected constructor(params?: OP20InitParameters) {
37
37
  super();
38
38
 
39
- if (params) {
40
- this.instantiate(params);
41
- } else {
42
- this.instantiate(new OP20InitParameters(u256.Zero, 0, '', ''));
43
- }
44
-
45
39
  this.allowanceMap = new MultiAddressMemoryMap<Address, Address, MemorySlotData<u256>>(
46
- Blockchain.nextPointer,
40
+ allowanceMapPointer,
47
41
  u256.Zero,
48
42
  );
49
43
 
50
44
  this.balanceOfMap = new AddressMemoryMap<Address, MemorySlotData<u256>>(
51
- Blockchain.nextPointer,
45
+ balanceOfMapPointer,
52
46
  u256.Zero,
53
47
  );
54
48
 
55
- this._totalSupply = new StoredU256(Blockchain.nextPointer, u256.Zero, u256.Zero);
49
+ this._totalSupply = new StoredU256(totalSupplyPointer, u256.Zero, u256.Zero);
50
+
51
+ this._maxSupply = new StoredU256(maxSupplyPointer, u256.Zero, u256.Zero);
52
+ this._decimals = new StoredU256(decimalsPointer, u256.Zero, u256.Zero);
53
+
54
+ this._name = new StoredString(namePointer, '');
55
+ this._symbol = new StoredString(symbolPointer, '');
56
+
57
+ if (params && this._maxSupply.value.isZero()) {
58
+ this.instantiate(params, true);
59
+ }
56
60
  }
57
61
 
58
- private _maxSupply: StoredU256 | undefined;
62
+ public _totalSupply: StoredU256;
63
+
64
+ public get totalSupply(): u256 {
65
+ return this._totalSupply.value;
66
+ }
59
67
 
60
68
  public get maxSupply(): u256 {
61
69
  if (!this._maxSupply) throw new Revert('Max supply not set');
@@ -63,54 +71,39 @@ export abstract class DeployableOP_20 extends OP_NET implements IOP_20 {
63
71
  return this._maxSupply.value;
64
72
  }
65
73
 
66
- private _decimals: StoredU256 | undefined;
67
-
68
74
  public get decimals(): u8 {
69
75
  if (!this._decimals) throw new Revert('Decimals not set');
70
76
 
71
- return u8(this._decimals.value);
77
+ return u8(this._decimals.value.toU32());
72
78
  }
73
79
 
74
- private _name: StoredString | undefined;
75
-
76
80
  public get name(): string {
77
81
  if (!this._name) throw new Revert('Name not set');
78
82
 
79
83
  return this._name.value;
80
84
  }
81
85
 
82
- private _symbol: StoredString | undefined;
83
-
84
86
  public get symbol(): string {
85
87
  if (!this._symbol) throw new Revert('Symbol not set');
86
88
 
87
89
  return this._symbol.value;
88
90
  }
89
91
 
90
- public _totalSupply: StoredU256;
91
-
92
- public get totalSupply(): u256 {
93
- return this._totalSupply.value;
94
- }
92
+ public instantiate(params: OP20InitParameters, skipOwnerVerification: boolean = false): void {
93
+ if (!skipOwnerVerification) this.onlyOwner(Blockchain.origin());
95
94
 
96
- public instantiate(params: OP20InitParameters): void {
97
- this.onlyOwner(Blockchain.from());
98
-
99
- this._maxSupply = new StoredU256(Blockchain.nextPointer, u256.Zero, u256.Zero);
100
- if (u256.ne(this._maxSupply.value, u256.Zero)) {
95
+ if (!this._maxSupply.value.isZero()) {
101
96
  throw new Revert('Already initialized');
102
97
  }
103
98
 
104
- this._maxSupply.value = params.maxSupply;
105
-
106
- this._decimals = new StoredU256(
107
- Blockchain.nextPointer,
108
- u256.Zero,
109
- u256.fromU32(u32(params.decimals)),
110
- );
99
+ if (params.decimals > 32) {
100
+ throw new Revert('Decimals can not be more than 32');
101
+ }
111
102
 
112
- this._name = new StoredString(Blockchain.nextPointer, params.name);
113
- this._symbol = new StoredString(Blockchain.nextPointer, params.symbol);
103
+ this._maxSupply.value = params.maxSupply;
104
+ this._decimals.value = u256.fromU32(u32(params.decimals));
105
+ this._name.value = params.name;
106
+ this._symbol.value = params.symbol;
114
107
  }
115
108
 
116
109
  /** METHODS */
@@ -132,7 +125,7 @@ export abstract class DeployableOP_20 extends OP_NET implements IOP_20 {
132
125
  const resp = this._approve(spender, value);
133
126
  response.writeBoolean(resp);
134
127
 
135
- this.createApproveEvent(Blockchain.from(), spender, value);
128
+ this.createApproveEvent(Blockchain.origin(), spender, value);
136
129
 
137
130
  return response;
138
131
  }
@@ -241,7 +234,7 @@ export abstract class DeployableOP_20 extends OP_NET implements IOP_20 {
241
234
  }
242
235
 
243
236
  protected _approve(spender: Address, value: u256): boolean {
244
- const callee = Blockchain.from();
237
+ const callee = Blockchain.origin();
245
238
 
246
239
  const senderMap = this.allowanceMap.get(callee);
247
240
  senderMap.set(spender, value);
@@ -261,7 +254,7 @@ export abstract class DeployableOP_20 extends OP_NET implements IOP_20 {
261
254
  throw new Revert(`No tokens`);
262
255
  }
263
256
 
264
- const callee = Blockchain.from();
257
+ const callee = Blockchain.origin();
265
258
  const caller = Blockchain.sender();
266
259
 
267
260
  if (onlyOwner) this.onlyOwner(callee); // only indexers can burn tokens
@@ -283,7 +276,7 @@ export abstract class DeployableOP_20 extends OP_NET implements IOP_20 {
283
276
  }
284
277
 
285
278
  protected _mint(to: Address, value: u256, onlyOwner: boolean = true): boolean {
286
- const callee = Blockchain.from();
279
+ const callee = Blockchain.origin();
287
280
 
288
281
  if (onlyOwner) this.onlyOwner(callee);
289
282
 
@@ -306,7 +299,7 @@ export abstract class DeployableOP_20 extends OP_NET implements IOP_20 {
306
299
  }
307
300
 
308
301
  protected _transfer(to: string, value: u256): boolean {
309
- const caller = Blockchain.from();
302
+ const caller = Blockchain.origin();
310
303
 
311
304
  if (!this.balanceOfMap.has(caller)) throw new Revert();
312
305
  if (this.isSelf(caller)) throw new Revert('Can not transfer from self account');
@@ -362,7 +355,7 @@ export abstract class DeployableOP_20 extends OP_NET implements IOP_20 {
362
355
  }
363
356
 
364
357
  protected _transferFrom(from: Address, to: Address, value: u256): boolean {
365
- const spender = Blockchain.from();
358
+ const spender = Blockchain.origin();
366
359
  if (Blockchain.sender() !== from) {
367
360
  throw new Revert('Not caller.');
368
361
  }
@@ -1,5 +1,6 @@
1
- import { DeployableOP_20, OP20InitParameters } from './DeployableOP_20';
2
- import { u256 } from 'as-bignum';
1
+ import { DeployableOP_20 } from './DeployableOP_20';
2
+ import { u256 } from 'as-bignum/assembly';
3
+ import { OP20InitParameters } from './interfaces/OP20InitParameters';
3
4
 
4
5
  export abstract class OP_20 extends DeployableOP_20 {
5
6
  protected constructor(maxSupply: u256, decimals: u8, name: string, symbol: string) {
@@ -0,0 +1,15 @@
1
+ import { u256 } from 'as-bignum/assembly';
2
+
3
+ export class OP20InitParameters {
4
+ readonly maxSupply: u256;
5
+ readonly decimals: u8;
6
+ readonly name: string;
7
+ readonly symbol: string;
8
+
9
+ constructor(maxSupply: u256, decimals: u8, name: string, symbol: string) {
10
+ this.maxSupply = maxSupply;
11
+ this.decimals = decimals;
12
+ this.name = name;
13
+ this.symbol = symbol;
14
+ }
15
+ }
@@ -32,7 +32,7 @@ export class BlockchainEnvironment {
32
32
  private storage: PointerStorage = new MapU256();
33
33
  private events: NetEvent[] = [];
34
34
 
35
- private _from: PotentialAddress = null;
35
+ private _origin: PotentialAddress = null;
36
36
  private _sender: PotentialAddress = null;
37
37
  private currentBlock: u256 = u256.Zero;
38
38
 
@@ -98,12 +98,12 @@ export class BlockchainEnvironment {
98
98
  return this.currentBlock.toU64();
99
99
  }
100
100
 
101
- public from(): Address {
102
- if (!this._from) {
101
+ public origin(): Address {
102
+ if (!this._origin) {
103
103
  throw this.error('Callee is required');
104
104
  }
105
105
 
106
- return this._from as Address;
106
+ return this._origin as Address;
107
107
  }
108
108
 
109
109
  public sender(): Address {
@@ -118,7 +118,7 @@ export class BlockchainEnvironment {
118
118
  const reader: BytesReader = new BytesReader(data);
119
119
 
120
120
  this._sender = reader.readAddress();
121
- this._from = reader.readAddress();
121
+ this._origin = reader.readAddress();
122
122
  this.currentBlock = reader.readU256();
123
123
 
124
124
  this._owner = reader.readAddress();
@@ -128,7 +128,7 @@ export class BlockchainEnvironment {
128
128
  }
129
129
 
130
130
  public call(destinationContract: Address, calldata: BytesWriter): BytesReader {
131
- if (destinationContract === this._from) {
131
+ if (destinationContract === this._origin) {
132
132
  throw this.error('Cannot call self');
133
133
  }
134
134
 
package/runtime/index.ts CHANGED
@@ -6,6 +6,7 @@ export * from './contracts/interfaces/IOP_20';
6
6
  export * from './contracts/OP_20';
7
7
  export * from './contracts/DeployableOP_20';
8
8
  export * from './contracts/OP_NET';
9
+ export * from './contracts/interfaces/OP20InitParameters';
9
10
 
10
11
  /** Buffer */
11
12
  export * from './buffer/BytesReader';
@@ -1,4 +1,4 @@
1
- import { u256 } from 'as-bignum/assembly/integer/u256';
1
+ import { u256 } from 'as-bignum/assembly';
2
2
  import { encodeSelector, Selector } from '../math/abi';
3
3
  import { Address } from '../types/Address';
4
4
  import { BytesWriter } from '../buffer/BytesWriter';