@leofcoin/chain 1.3.4 → 1.3.5

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/dist/chain.js CHANGED
@@ -81,27 +81,27 @@ class Machine {
81
81
  case 'contractError': {
82
82
  console.warn(`removing contract ${await data.hash}`);
83
83
  await contractStore.delete(await data.hash);
84
- break
84
+ break
85
85
  }
86
86
 
87
87
  case 'executionError': {
88
88
  // console.warn(`error executing transaction ${data.message}`);
89
89
  pubsub.publish(data.id, {error: data.message});
90
- break
90
+ break
91
91
  }
92
92
 
93
93
  case 'debug': {
94
94
  for (const message of data.messages) debug(message);
95
- break
95
+ break
96
96
  }
97
97
  case 'machine-ready': {
98
98
  this.lastBlock = data.lastBlock;
99
99
  pubsub.publish('machine.ready', true);
100
- break
100
+ break
101
101
  }
102
102
  case 'response': {
103
103
  pubsub.publish(data.id, data.value);
104
- break
104
+ break
105
105
  }
106
106
  }
107
107
 
@@ -159,12 +159,11 @@ class Machine {
159
159
  * @params {ContractMessage} - contractMessage
160
160
  */
161
161
  async addContract(contractMessage) {
162
- if (!await contractStore.has(contractMessage.hash)) {
163
- await contractStore.put(contractMessage.hash, contractMessage.encoded);
164
- await this.#runContract(contractMessage);
165
- return contractMessage.hash
166
- }
167
- throw new Error('duplicate contract')
162
+ if (await contractStore.has(await contractMessage.hash)) throw new Error('duplicate contract')
163
+
164
+ await contractStore.put(await contractMessage.hash, contractMessage.encoded);
165
+ await this.#runContract(contractMessage);
166
+ return contractMessage.hash
168
167
  }
169
168
 
170
169
  async execute(contract, method, parameters) {
@@ -1163,20 +1162,19 @@ async #signTransaction (transaction, wallet) {
1163
1162
  }
1164
1163
 
1165
1164
  /**
1166
- *
1167
- * @param {String} contract - a contract string (see plugins/deployContract)
1165
+ *
1166
+ * @param {String} contract
1167
+ * @param {Array} parameters
1168
+ * @returns
1168
1169
  */
1169
1170
  async deployContract(contract, parameters = []) {
1170
- globalThis.msg = {sender: peernet.selectedAccount, call: this.call};
1171
-
1172
- const hash = await this.createContractAddress(creator, contract, parameters);
1173
- console.log(hash);
1171
+ const message = await createContractMessage(peernet.selectedAccount, contract, parameters);
1174
1172
  try {
1175
- const tx = await this.createTransactionFrom(peernet.selectedAccount, addresses.contractFactory, 'deployContract', [hash, creator, contract, constructorParameters]);
1173
+ await this.#machine.addContract(message);
1176
1174
  } catch (error) {
1177
1175
  throw error
1178
1176
  }
1179
- return this.#machine.addContract(message)
1177
+ return this.createTransactionFrom(peernet.selectedAccount, addresses.contractFactory, 'registerContract', [await message.hash])
1180
1178
  }
1181
1179
 
1182
1180
  #createMessage(sender = peernet.selectedAccount) {
@@ -1230,6 +1228,19 @@ console.log(hash);
1230
1228
  return this.staticCall(addresses.nativeToken, 'balances')
1231
1229
  }
1232
1230
 
1231
+ get contracts() {
1232
+ return this.staticCall(addresses.contractFactory, 'contracts')
1233
+ }
1234
+ /**
1235
+ *
1236
+ * @param {Address} address old contract address
1237
+ * @param {Address} newAddress new contract address
1238
+ * @returns
1239
+ */
1240
+ async updateImplementation(address, newAddress) {
1241
+ return this.call(addresses.contractFactory, 'updateImplementation', [address, newAddress])
1242
+ }
1243
+
1233
1244
  deleteAll() {
1234
1245
  return this.#machine.deleteAll()
1235
1246
  }
@@ -1 +1 @@
1
- class Roles{#roles={IMPLEMENTATION_MANAGER:[],OWNER:[],MINT:[],BURN:[]};constructor(roles){if(roles){if(!(roles instanceof Object))throw new TypeError("expected roles to be an object");this.#roles={...roles,...this.#roles}}else this.#grantRole(msg.sender,"OWNER"),this.#grantRole(msg.sender,"IMPLEMENTATION_MANAGER")}get state(){return{roles:this.roles}}get roles(){return{...this.#roles}}hasRole(address,role){return!!this.#roles[role]&&this.#roles[role].includes(address)}#grantRole(address,role){if(this.hasRole(address,role))throw new Error(`${role} role already granted for ${address}`);this.#roles[role].push(address)}#revokeRole(address,role){if(!this.hasRole(address,role))throw new Error(`${role} role already revoked for ${address}`);if("OWNER"===role&&1===this.#roles[role].length)throw new Error("atleast one owner is needed!");this.#roles[role].splice(this.#roles[role].indexOf(address))}grantRole(address,role){if(!this.hasRole(address,"OWNER"))throw new Error("Not allowed");this.#grantRole(address,role)}revokeRole(address,role){if(!this.hasRole(address,"OWNER"))throw new Error("Not allowed");this.#revokeRole(address,role)}}class Token extends Roles{#name;#symbol;#holders=0;#balances={};#approvals={};#decimals=18;#totalSupply=BigNumber.from(0);constructor(name,symbol,decimals=18,state){if(!name)throw new Error("name undefined");if(!symbol)throw new Error("symbol undefined");super(state?.roles),this.#name=name,this.#symbol=symbol,this.#decimals=decimals}get state(){return{...super.state,holders:this.holders,balances:this.balances,approvals:{...this.#approvals},totalSupply:this.totalSupply}}get totalSupply(){return this.#totalSupply}get name(){return this.#name}get symbol(){return this.#symbol}get holders(){return this.#holders}get balances(){return{...this.#balances}}mint(to,amount){if(!this.hasRole(msg.sender,"MINT"))throw new Error("not allowed");this.#totalSupply=this.#totalSupply.add(amount),this.#increaseBalance(to,amount)}burn(to,amount){if(!this.hasRole(msg.sender,"BURN"))throw new Error("not allowed");this.#totalSupply=this.#totalSupply.sub(amount),this.#decreaseBalance(to,amount)}#beforeTransfer(from,to,amount){if(!this.#balances[from]||this.#balances[from]<amount)throw new Error("amount exceeds balance")}#updateHolders(address,previousBalance){"0x00"===this.#balances[address].toHexString()?this.#holders-=1:"0x00"!==this.#balances[address].toHexString()&&"0x00"===previousBalance.toHexString()&&(this.#holders+=1)}#increaseBalance(address,amount){this.#balances[address]||(this.#balances[address]=BigNumber.from(0));const previousBalance=this.#balances[address];this.#balances[address]=this.#balances[address].add(amount),this.#updateHolders(address,previousBalance)}#decreaseBalance(address,amount){const previousBalance=this.#balances[address];this.#balances[address]=this.#balances[address].sub(amount),this.#updateHolders(address,previousBalance)}balanceOf(address){return this.#balances[address]}setApproval(operator,amount){const owner=globalThis.msg.sender;this.#approvals[owner]||(this.#approvals[owner]={}),this.#approvals[owner][operator]=amount}approved(owner,operator,amount){return this.#approvals[owner][operator]===amount}transfer(from,to,amount){amount=BigNumber.from(amount),this.#beforeTransfer(from,to,amount),this.#decreaseBalance(from,amount),this.#increaseBalance(to,amount)}}class ArtOnline extends Token{constructor(state){super("ArtOnline","ART",18,state)}}export{ArtOnline as default};
1
+ class Roles{#roles={IMPLEMENTATION_MANAGER:[],OWNER:[],MINT:[],BURN:[]};constructor(roles){if(roles){if(!(roles instanceof Object))throw new TypeError("expected roles to be an object");this.#roles={...roles,...this.#roles}}else this.#grantRole(msg.sender,"OWNER"),this.#grantRole(msg.sender,"IMPLEMENTATION_MANAGER")}get state(){return{roles:this.roles}}get roles(){return{...this.#roles}}hasRole(address,role){return!!this.#roles[role]&&this.#roles[role].includes(address)}#grantRole(address,role){if(this.hasRole(address,role))throw new Error(`${role} role already granted for ${address}`);this.#roles[role].push(address)}#revokeRole(address,role){if(!this.hasRole(address,role))throw new Error(`${role} role already revoked for ${address}`);if("OWNER"===role&&1===this.#roles[role].length)throw new Error("atleast one owner is needed!");this.#roles[role].splice(this.#roles[role].indexOf(address))}grantRole(address,role){if(!this.hasRole(address,"OWNER"))throw new Error("Not allowed");this.#grantRole(address,role)}revokeRole(address,role){if(!this.hasRole(address,"OWNER"))throw new Error("Not allowed");this.#revokeRole(address,role)}}class Token extends Roles{#name;#symbol;#holders=0;#balances={};#approvals={};#decimals=18;#totalSupply=BigNumber.from(0);constructor(name,symbol,decimals=18,state){if(!name)throw new Error("name undefined");if(!symbol)throw new Error("symbol undefined");super(state?.roles),this.#name=name,this.#symbol=symbol,this.#decimals=decimals}get state(){return{...super.state,holders:this.holders,balances:this.balances,approvals:{...this.#approvals},totalSupply:this.totalSupply}}get totalSupply(){return this.#totalSupply}get name(){return this.#name}get symbol(){return this.#symbol}get holders(){return this.#holders}get balances(){return{...this.#balances}}mint(to,amount){if(!this.hasRole(msg.sender,"MINT"))throw new Error("not allowed");this.#totalSupply=this.#totalSupply.add(amount),this.#increaseBalance(to,amount)}burn(from,amount){if(!this.hasRole(msg.sender,"BURN"))throw new Error("not allowed");this.#totalSupply=this.#totalSupply.sub(amount),this.#decreaseBalance(from,amount)}#beforeTransfer(from,to,amount){if(!this.#balances[from]||this.#balances[from]<amount)throw new Error("amount exceeds balance")}#updateHolders(address,previousBalance){"0x00"===this.#balances[address].toHexString()?this.#holders-=1:"0x00"!==this.#balances[address].toHexString()&&"0x00"===previousBalance.toHexString()&&(this.#holders+=1)}#increaseBalance(address,amount){this.#balances[address]||(this.#balances[address]=BigNumber.from(0));const previousBalance=this.#balances[address];this.#balances[address]=this.#balances[address].add(amount),this.#updateHolders(address,previousBalance)}#decreaseBalance(address,amount){const previousBalance=this.#balances[address];this.#balances[address]=this.#balances[address].sub(amount),this.#updateHolders(address,previousBalance)}balanceOf(address){return this.#balances[address]}setApproval(operator,amount){const owner=globalThis.msg.sender;this.#approvals[owner]||(this.#approvals[owner]={}),this.#approvals[owner][operator]=amount}approved(owner,operator,amount){return this.#approvals[owner][operator]===amount}transfer(from,to,amount){amount=BigNumber.from(amount),this.#beforeTransfer(from,to,amount),this.#decreaseBalance(from,amount),this.#increaseBalance(to,amount)}}class ArtOnline extends Token{constructor(state){super("ArtOnline","ART",18,state)}}export{ArtOnline as default};
@@ -72,27 +72,27 @@ class Machine {
72
72
  case 'contractError': {
73
73
  console.warn(`removing contract ${await data.hash}`);
74
74
  await contractStore.delete(await data.hash);
75
- break
75
+ break
76
76
  }
77
77
 
78
78
  case 'executionError': {
79
79
  // console.warn(`error executing transaction ${data.message}`);
80
80
  pubsub.publish(data.id, {error: data.message});
81
- break
81
+ break
82
82
  }
83
83
 
84
84
  case 'debug': {
85
85
  for (const message of data.messages) debug(message);
86
- break
86
+ break
87
87
  }
88
88
  case 'machine-ready': {
89
89
  this.lastBlock = data.lastBlock;
90
90
  pubsub.publish('machine.ready', true);
91
- break
91
+ break
92
92
  }
93
93
  case 'response': {
94
94
  pubsub.publish(data.id, data.value);
95
- break
95
+ break
96
96
  }
97
97
  }
98
98
 
@@ -150,12 +150,11 @@ class Machine {
150
150
  * @params {ContractMessage} - contractMessage
151
151
  */
152
152
  async addContract(contractMessage) {
153
- if (!await contractStore.has(contractMessage.hash)) {
154
- await contractStore.put(contractMessage.hash, contractMessage.encoded);
155
- await this.#runContract(contractMessage);
156
- return contractMessage.hash
157
- }
158
- throw new Error('duplicate contract')
153
+ if (await contractStore.has(await contractMessage.hash)) throw new Error('duplicate contract')
154
+
155
+ await contractStore.put(await contractMessage.hash, contractMessage.encoded);
156
+ await this.#runContract(contractMessage);
157
+ return contractMessage.hash
159
158
  }
160
159
 
161
160
  async execute(contract, method, parameters) {
@@ -1154,20 +1153,19 @@ async #signTransaction (transaction, wallet) {
1154
1153
  }
1155
1154
 
1156
1155
  /**
1157
- *
1158
- * @param {String} contract - a contract string (see plugins/deployContract)
1156
+ *
1157
+ * @param {String} contract
1158
+ * @param {Array} parameters
1159
+ * @returns
1159
1160
  */
1160
1161
  async deployContract(contract, parameters = []) {
1161
- globalThis.msg = {sender: peernet.selectedAccount, call: this.call};
1162
-
1163
- const hash = await this.createContractAddress(creator, contract, parameters);
1164
- console.log(hash);
1162
+ const message = await createContractMessage(peernet.selectedAccount, contract, parameters);
1165
1163
  try {
1166
- const tx = await this.createTransactionFrom(peernet.selectedAccount, addresses.contractFactory, 'deployContract', [hash, creator, contract, constructorParameters]);
1164
+ await this.#machine.addContract(message);
1167
1165
  } catch (error) {
1168
1166
  throw error
1169
1167
  }
1170
- return this.#machine.addContract(message)
1168
+ return this.createTransactionFrom(peernet.selectedAccount, addresses.contractFactory, 'registerContract', [await message.hash])
1171
1169
  }
1172
1170
 
1173
1171
  #createMessage(sender = peernet.selectedAccount) {
@@ -1221,6 +1219,19 @@ console.log(hash);
1221
1219
  return this.staticCall(addresses.nativeToken, 'balances')
1222
1220
  }
1223
1221
 
1222
+ get contracts() {
1223
+ return this.staticCall(addresses.contractFactory, 'contracts')
1224
+ }
1225
+ /**
1226
+ *
1227
+ * @param {Address} address old contract address
1228
+ * @param {Address} newAddress new contract address
1229
+ * @returns
1230
+ */
1231
+ async updateImplementation(address, newAddress) {
1232
+ return this.call(addresses.contractFactory, 'updateImplementation', [address, newAddress])
1233
+ }
1234
+
1224
1235
  deleteAll() {
1225
1236
  return this.#machine.deleteAll()
1226
1237
  }
@@ -1 +1 @@
1
- class Roles{#roles={IMPLEMENTATION_MANAGER:[],OWNER:[],MINT:[],BURN:[]};constructor(roles){if(roles){if(!(roles instanceof Object))throw new TypeError("expected roles to be an object");this.#roles={...roles,...this.#roles}}else this.#grantRole(msg.sender,"OWNER"),this.#grantRole(msg.sender,"IMPLEMENTATION_MANAGER")}get state(){return{roles:this.roles}}get roles(){return{...this.#roles}}hasRole(address,role){return!!this.#roles[role]&&this.#roles[role].includes(address)}#grantRole(address,role){if(this.hasRole(address,role))throw new Error(`${role} role already granted for ${address}`);this.#roles[role].push(address)}#revokeRole(address,role){if(!this.hasRole(address,role))throw new Error(`${role} role already revoked for ${address}`);if("OWNER"===role&&1===this.#roles[role].length)throw new Error("atleast one owner is needed!");this.#roles[role].splice(this.#roles[role].indexOf(address))}grantRole(address,role){if(!this.hasRole(address,"OWNER"))throw new Error("Not allowed");this.#grantRole(address,role)}revokeRole(address,role){if(!this.hasRole(address,"OWNER"))throw new Error("Not allowed");this.#revokeRole(address,role)}}class Token extends Roles{#name;#symbol;#holders=0;#balances={};#approvals={};#decimals=18;#totalSupply=BigNumber.from(0);constructor(name,symbol,decimals=18,state){if(!name)throw new Error("name undefined");if(!symbol)throw new Error("symbol undefined");super(state?.roles),this.#name=name,this.#symbol=symbol,this.#decimals=decimals}get state(){return{...super.state,holders:this.holders,balances:this.balances,approvals:{...this.#approvals},totalSupply:this.totalSupply}}get totalSupply(){return this.#totalSupply}get name(){return this.#name}get symbol(){return this.#symbol}get holders(){return this.#holders}get balances(){return{...this.#balances}}mint(to,amount){if(!this.hasRole(msg.sender,"MINT"))throw new Error("not allowed");this.#totalSupply=this.#totalSupply.add(amount),this.#increaseBalance(to,amount)}burn(to,amount){if(!this.hasRole(msg.sender,"BURN"))throw new Error("not allowed");this.#totalSupply=this.#totalSupply.sub(amount),this.#decreaseBalance(to,amount)}#beforeTransfer(from,to,amount){if(!this.#balances[from]||this.#balances[from]<amount)throw new Error("amount exceeds balance")}#updateHolders(address,previousBalance){"0x00"===this.#balances[address].toHexString()?this.#holders-=1:"0x00"!==this.#balances[address].toHexString()&&"0x00"===previousBalance.toHexString()&&(this.#holders+=1)}#increaseBalance(address,amount){this.#balances[address]||(this.#balances[address]=BigNumber.from(0));const previousBalance=this.#balances[address];this.#balances[address]=this.#balances[address].add(amount),this.#updateHolders(address,previousBalance)}#decreaseBalance(address,amount){const previousBalance=this.#balances[address];this.#balances[address]=this.#balances[address].sub(amount),this.#updateHolders(address,previousBalance)}balanceOf(address){return this.#balances[address]}setApproval(operator,amount){const owner=globalThis.msg.sender;this.#approvals[owner]||(this.#approvals[owner]={}),this.#approvals[owner][operator]=amount}approved(owner,operator,amount){return this.#approvals[owner][operator]===amount}transfer(from,to,amount){amount=BigNumber.from(amount),this.#beforeTransfer(from,to,amount),this.#decreaseBalance(from,amount),this.#increaseBalance(to,amount)}}export{Token as default};
1
+ class Roles{#roles={IMPLEMENTATION_MANAGER:[],OWNER:[],MINT:[],BURN:[]};constructor(roles){if(roles){if(!(roles instanceof Object))throw new TypeError("expected roles to be an object");this.#roles={...roles,...this.#roles}}else this.#grantRole(msg.sender,"OWNER"),this.#grantRole(msg.sender,"IMPLEMENTATION_MANAGER")}get state(){return{roles:this.roles}}get roles(){return{...this.#roles}}hasRole(address,role){return!!this.#roles[role]&&this.#roles[role].includes(address)}#grantRole(address,role){if(this.hasRole(address,role))throw new Error(`${role} role already granted for ${address}`);this.#roles[role].push(address)}#revokeRole(address,role){if(!this.hasRole(address,role))throw new Error(`${role} role already revoked for ${address}`);if("OWNER"===role&&1===this.#roles[role].length)throw new Error("atleast one owner is needed!");this.#roles[role].splice(this.#roles[role].indexOf(address))}grantRole(address,role){if(!this.hasRole(address,"OWNER"))throw new Error("Not allowed");this.#grantRole(address,role)}revokeRole(address,role){if(!this.hasRole(address,"OWNER"))throw new Error("Not allowed");this.#revokeRole(address,role)}}class Token extends Roles{#name;#symbol;#holders=0;#balances={};#approvals={};#decimals=18;#totalSupply=BigNumber.from(0);constructor(name,symbol,decimals=18,state){if(!name)throw new Error("name undefined");if(!symbol)throw new Error("symbol undefined");super(state?.roles),this.#name=name,this.#symbol=symbol,this.#decimals=decimals}get state(){return{...super.state,holders:this.holders,balances:this.balances,approvals:{...this.#approvals},totalSupply:this.totalSupply}}get totalSupply(){return this.#totalSupply}get name(){return this.#name}get symbol(){return this.#symbol}get holders(){return this.#holders}get balances(){return{...this.#balances}}mint(to,amount){if(!this.hasRole(msg.sender,"MINT"))throw new Error("not allowed");this.#totalSupply=this.#totalSupply.add(amount),this.#increaseBalance(to,amount)}burn(from,amount){if(!this.hasRole(msg.sender,"BURN"))throw new Error("not allowed");this.#totalSupply=this.#totalSupply.sub(amount),this.#decreaseBalance(from,amount)}#beforeTransfer(from,to,amount){if(!this.#balances[from]||this.#balances[from]<amount)throw new Error("amount exceeds balance")}#updateHolders(address,previousBalance){"0x00"===this.#balances[address].toHexString()?this.#holders-=1:"0x00"!==this.#balances[address].toHexString()&&"0x00"===previousBalance.toHexString()&&(this.#holders+=1)}#increaseBalance(address,amount){this.#balances[address]||(this.#balances[address]=BigNumber.from(0));const previousBalance=this.#balances[address];this.#balances[address]=this.#balances[address].add(amount),this.#updateHolders(address,previousBalance)}#decreaseBalance(address,amount){const previousBalance=this.#balances[address];this.#balances[address]=this.#balances[address].sub(amount),this.#updateHolders(address,previousBalance)}balanceOf(address){return this.#balances[address]}setApproval(operator,amount){const owner=globalThis.msg.sender;this.#approvals[owner]||(this.#approvals[owner]={}),this.#approvals[owner][operator]=amount}approved(owner,operator,amount){return this.#approvals[owner][operator]===amount}transfer(from,to,amount){amount=BigNumber.from(amount),this.#beforeTransfer(from,to,amount),this.#decreaseBalance(from,amount),this.#increaseBalance(to,amount)}}export{Token as default};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/chain",
3
- "version": "1.3.4",
3
+ "version": "1.3.5",
4
4
  "description": "Official javascript implementation",
5
5
  "main": "./dist/node.js",
6
6
  "module": "./dist/chain.esm",
package/src/chain.js CHANGED
@@ -764,20 +764,19 @@ async #signTransaction (transaction, wallet) {
764
764
  }
765
765
 
766
766
  /**
767
- *
768
- * @param {String} contract - a contract string (see plugins/deployContract)
767
+ *
768
+ * @param {String} contract
769
+ * @param {Array} parameters
770
+ * @returns
769
771
  */
770
772
  async deployContract(contract, parameters = []) {
771
- globalThis.msg = {sender: peernet.selectedAccount, call: this.call}
772
-
773
- const hash = await this.createContractAddress(creator, contract, parameters)
774
- console.log(hash);
773
+ const message = await createContractMessage(peernet.selectedAccount, contract, parameters)
775
774
  try {
776
- const tx = await this.createTransactionFrom(peernet.selectedAccount, addresses.contractFactory, 'deployContract', [hash, creator, contract, constructorParameters])
775
+ await this.#machine.addContract(message)
777
776
  } catch (error) {
778
777
  throw error
779
778
  }
780
- return this.#machine.addContract(message)
779
+ return this.createTransactionFrom(peernet.selectedAccount, addresses.contractFactory, 'registerContract', [await message.hash])
781
780
  }
782
781
 
783
782
  #createMessage(sender = peernet.selectedAccount) {
@@ -831,6 +830,19 @@ console.log(hash);
831
830
  return this.staticCall(addresses.nativeToken, 'balances')
832
831
  }
833
832
 
833
+ get contracts() {
834
+ return this.staticCall(addresses.contractFactory, 'contracts')
835
+ }
836
+ /**
837
+ *
838
+ * @param {Address} address old contract address
839
+ * @param {Address} newAddress new contract address
840
+ * @returns
841
+ */
842
+ async updateImplementation(address, newAddress) {
843
+ return this.call(addresses.contractFactory, 'updateImplementation', [address, newAddress])
844
+ }
845
+
834
846
  deleteAll() {
835
847
  return this.#machine.deleteAll()
836
848
  }
package/src/machine.js CHANGED
@@ -28,27 +28,27 @@ export default class Machine {
28
28
  case 'contractError': {
29
29
  console.warn(`removing contract ${await data.hash}`);
30
30
  await contractStore.delete(await data.hash)
31
- break
31
+ break
32
32
  }
33
33
 
34
34
  case 'executionError': {
35
35
  // console.warn(`error executing transaction ${data.message}`);
36
36
  pubsub.publish(data.id, {error: data.message})
37
- break
37
+ break
38
38
  }
39
39
 
40
40
  case 'debug': {
41
41
  for (const message of data.messages) debug(message)
42
- break
42
+ break
43
43
  }
44
44
  case 'machine-ready': {
45
45
  this.lastBlock = data.lastBlock
46
46
  pubsub.publish('machine.ready', true)
47
- break
47
+ break
48
48
  }
49
49
  case 'response': {
50
50
  pubsub.publish(data.id, data.value)
51
- break
51
+ break
52
52
  }
53
53
  }
54
54
 
@@ -106,12 +106,11 @@ export default class Machine {
106
106
  * @params {ContractMessage} - contractMessage
107
107
  */
108
108
  async addContract(contractMessage) {
109
- if (!await contractStore.has(contractMessage.hash)) {
110
- await contractStore.put(contractMessage.hash, contractMessage.encoded)
111
- await this.#runContract(contractMessage)
112
- return contractMessage.hash
113
- }
114
- throw new Error('duplicate contract')
109
+ if (await contractStore.has(await contractMessage.hash)) throw new Error('duplicate contract')
110
+
111
+ await contractStore.put(await contractMessage.hash, contractMessage.encoded)
112
+ await this.#runContract(contractMessage)
113
+ return contractMessage.hash
115
114
  }
116
115
 
117
116
  async execute(contract, method, parameters) {
@@ -81,11 +81,11 @@ export default class Token extends Roles {
81
81
  this.#increaseBalance(to, amount)
82
82
  }
83
83
 
84
- burn(to, amount) {
84
+ burn(from, amount) {
85
85
  if (!this.hasRole(msg.sender, 'BURN')) throw new Error('not allowed')
86
86
 
87
87
  this.#totalSupply = this.#totalSupply.sub(amount)
88
- this.#decreaseBalance(to, amount)
88
+ this.#decreaseBalance(from, amount)
89
89
  }
90
90
 
91
91
  #beforeTransfer(from, to, amount) {