@leofcoin/chain 1.0.4

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 (82) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/LICENSE +88 -0
  3. package/README.md +4 -0
  4. package/_node.js +1 -0
  5. package/chain.js +1468 -0
  6. package/demo/index.html +18 -0
  7. package/dist/browser/peernet.js +101516 -0
  8. package/dist/chain.browser.js +19879 -0
  9. package/dist/chain.browser.js.tmp-browserify-65536332430764332457 +0 -0
  10. package/dist/chain.browser.js.tmp-browserify-77283953363424018335 +0 -0
  11. package/dist/chain.browser.min.js +2 -0
  12. package/dist/chain.browser.min.js.LICENSE.txt +10 -0
  13. package/dist/chain.js +1468 -0
  14. package/dist/contracts/factory.js +1 -0
  15. package/dist/contracts/nameService.js +1 -0
  16. package/dist/contracts/nativeToken.js +1 -0
  17. package/dist/contracts/validators.js +1 -0
  18. package/dist/lib.browser.js +16966 -0
  19. package/dist/native.js +1 -0
  20. package/dist/node.browser.js +68961 -0
  21. package/dist/node.browser.min.js +2 -0
  22. package/dist/node.browser.min.js.LICENSE.txt +12 -0
  23. package/dist/node.js +1 -0
  24. package/dist/standards/token.js +1 -0
  25. package/dist/token/native.js +1 -0
  26. package/dist/token.js +1 -0
  27. package/examples/contracts/token.js +7 -0
  28. package/lib.js +1 -0
  29. package/package.json +46 -0
  30. package/plugins/bundle.js +18 -0
  31. package/rollup.config.js +151 -0
  32. package/src/addresses.json +6 -0
  33. package/src/apis/token.lfcc +17 -0
  34. package/src/bytecodes.json +6 -0
  35. package/src/chain.js +593 -0
  36. package/src/config/config.js +15 -0
  37. package/src/config/main.js +4 -0
  38. package/src/config/protocol.js +5 -0
  39. package/src/contracts/factory.js +59 -0
  40. package/src/contracts/nameService.js +108 -0
  41. package/src/contracts/nativeToken.js +7 -0
  42. package/src/contracts/powerToken.js +7 -0
  43. package/src/contracts/proxies/factoryProxy.js +12 -0
  44. package/src/contracts/proxies/nameServiceProxy.js +12 -0
  45. package/src/contracts/proxies/nativeTokenProxy.js +12 -0
  46. package/src/contracts/proxies/validatorsProxy.js +12 -0
  47. package/src/contracts/proxies/votingProxy.js +12 -0
  48. package/src/contracts/proxyManager.js +7 -0
  49. package/src/contracts/validators.js +119 -0
  50. package/src/fee/config.js +3 -0
  51. package/src/lib.js +67 -0
  52. package/src/machine.js +130 -0
  53. package/src/messages/block.js +14 -0
  54. package/src/messages/bw-request.js +14 -0
  55. package/src/messages/bw.js +14 -0
  56. package/src/messages/contract.js +13 -0
  57. package/src/messages/last-block-request.js +14 -0
  58. package/src/messages/last-block.js +14 -0
  59. package/src/messages/transaction.js +14 -0
  60. package/src/node.js +55 -0
  61. package/src/protos/block.proto.js +26 -0
  62. package/src/protos/bw-request.proto.js +5 -0
  63. package/src/protos/bw.proto.js +7 -0
  64. package/src/protos/contract.proto.js +7 -0
  65. package/src/protos/last-block-request.proto.js +5 -0
  66. package/src/protos/last-block.proto.js +7 -0
  67. package/src/protos/transaction.proto.js +11 -0
  68. package/src/standards/Proxy.js +42 -0
  69. package/src/standards/Voting.js +3 -0
  70. package/src/standards/initializer.js +10 -0
  71. package/src/standards/proxyManager.js +66 -0
  72. package/src/standards/roles.js +65 -0
  73. package/src/standards/token.js +137 -0
  74. package/src/state.js +26 -0
  75. package/src/transactions/validator.js +29 -0
  76. package/src/typer.js +19 -0
  77. package/src/utils/utils.js +16 -0
  78. package/test/chain.js +119 -0
  79. package/test/contracts/token.js +40 -0
  80. package/test/create-genesis.js +58 -0
  81. package/test/index.js +3 -0
  82. package/webpack.config.js +167 -0
package/src/chain.js ADDED
@@ -0,0 +1,593 @@
1
+ import { BigNumber, formatUnits, parseUnits, info } from './utils/utils'
2
+ import Machine from './machine'
3
+ import lib from './lib'
4
+
5
+ globalThis.BigNumber = BigNumber
6
+
7
+ const { ContractMessage, TransactionMessage, BlockMessage, BWMessage, BWRequestMessage } = lib
8
+ // check if browser or local
9
+ export default class Chain {
10
+ #validators = []
11
+ #blocks = []
12
+ #machine
13
+ #runningEpoch = false
14
+ #lastBlock = {index: 0, previousHash: '0x0'}
15
+
16
+ constructor() {
17
+ return this.#init()
18
+ }
19
+
20
+ get lib() {
21
+ return lib
22
+ }
23
+
24
+ get lastBlock() {
25
+ return this.#lastBlock
26
+ }
27
+
28
+ get nativeToken() {
29
+ return lib.nativeToken
30
+ }
31
+
32
+ get validators() {
33
+ return [...this.#validators]
34
+ }
35
+
36
+ get blocks() {
37
+ return [...this.#blocks]
38
+ }
39
+
40
+ async hasTransactionToHandle() {
41
+ const size = await transactionPoolStore.size()
42
+ if (size > 0) return true
43
+ return false
44
+ }
45
+
46
+ async #runEpoch() {
47
+ const validators = await this.staticCall(lib.validators, 'validators')
48
+ if (!validators[peernet.id]?.active) return
49
+
50
+ this.#runningEpoch = true
51
+ const start = new Date().getTime()
52
+ await this.#createBlock()
53
+ const end = new Date().getTime()
54
+ console.log((end - start) / 1000 + ' s');
55
+ if (await this.hasTransactionToHandle()) return this.#runEpoch()
56
+ this.#runningEpoch = false
57
+ }
58
+
59
+ async #setup() {
60
+ await contractStore.put(lib.contractFactory, lib.contractFactoryMessage)
61
+ await contractStore.put(lib.nativeToken, lib.nativeTokenMessage)
62
+ await contractStore.put(lib.validators, lib.validatorsMessage)
63
+ await contractStore.put(lib.nameService, lib.nameServiceMessage)
64
+ console.log('handle native contracts');
65
+ // handle native contracts
66
+ }
67
+
68
+ async #sync() {
69
+ let promises = []
70
+ for (const peer of peernet.connections) {
71
+ if (peer.peerId !== this.id) {
72
+ let data = new peernet.protos['peernet-request']({request: 'lastBlock'})
73
+
74
+ const node = await peernet.prepareMessage(id, data.encoded)
75
+ promises.push(peer.request(node.encoded))
76
+ }
77
+ }
78
+ // if (this.)
79
+
80
+ promises = await Promise.allSettled(promises)
81
+ promises = promises.reduce((set, c) => {
82
+ console.log({c});
83
+ if (c.index > set.index) {
84
+ set.index = c.index
85
+ set.hash = c.hash
86
+ }
87
+ return set
88
+ }, {index: 0, hash: '0x0'})
89
+ // get lastblock
90
+ }
91
+
92
+ async #init() {
93
+ // this.node = await new Node()
94
+ this.participants = []
95
+ const initialized = await contractStore.has(lib.contractFactory)
96
+ if (!initialized) await this.#setup()
97
+
98
+ this.#machine = await new Machine()
99
+ this.utils = { BigNumber, formatUnits, parseUnits }
100
+
101
+ try {
102
+ let localBlock = await chainStore.get('lastBlock')
103
+ localBlock = await peernet.get(new TextDecoder().decode(localBlock))
104
+ localBlock = new BlockMessage(localBlock)
105
+ this.#lastBlock = {...localBlock.decoded, hash: localBlock.hash}
106
+ // console.log(this.lastBlock.decoded.transactions);
107
+ } catch (e) {
108
+ await this.#sync()
109
+ // this.#setup()
110
+ }
111
+
112
+ await peernet.addRequestHandler('bw-request-message', () => {
113
+
114
+ return new BWMessage(peernet.client.bw) || { up: 0, down: 0 }
115
+ })
116
+
117
+ await peernet.addRequestHandler('lastBlock', this.#lastBlockHandler.bind(this))
118
+
119
+ peernet.subscribe('add-block', this.#addBlock.bind(this))
120
+
121
+ peernet.subscribe('add-transaction', async transaction => {
122
+ try {
123
+ transaction = new TransactionMessage(transaction)
124
+ await transactionPoolStore.put(transaction.hash, transaction.encoded)
125
+ if (await this.hasTransactionToHandle() && !this.#runningEpoch) return this.#runEpoch()
126
+ } catch (e) {
127
+ throw Error('invalid transaction')
128
+ }
129
+ })
130
+
131
+ pubsub.subscribe('peer:connected', this.#peerConnected.bind(this))
132
+
133
+ // load local blocks
134
+ await this.resolveBlocks()
135
+ return this
136
+ }
137
+
138
+ async #peerConnected(peer) {
139
+ let node = new peernet.protos['peernet-request']({request: 'lastBlock'})
140
+ node = await peernet.prepareMessage(peer.id, node.encoded)
141
+ let response = await peer.request(node.encoded)
142
+ response = new Uint8Array(Object.values(response))
143
+ const proto = new globalThis.peernet.protos['peernet-message'](response)
144
+ response = new globalThis.peernet.protos['peernet-response'](proto.decoded.data)
145
+ let lastBlock = JSON.parse(new TextDecoder().decode(response.decoded.response))
146
+
147
+ if (!this.lastBlock || this.lastBlock.index < lastBlock.index) {
148
+ // TODO: check if valid
149
+ const localIndex = this.lastBlock ? this.lastBlock.index : 0
150
+ const index = lastBlock.index
151
+ await this.resolveBlock(lastBlock.hash)
152
+ this.#lastBlock = this.#blocks[this.#blocks.length - 1]
153
+ console.log({lastBlock: this.#lastBlock});
154
+ console.log(this.#blocks);
155
+ let blocksSynced = localIndex > 0 ? localIndex - index : index
156
+ blocksSynced += 1
157
+ info(`synced ${blocksSynced} ${blocksSynced > 1 ? 'blocks' : 'block'}`)
158
+
159
+ const end = this.#blocks.length
160
+ const start = (this.#blocks.length) - blocksSynced
161
+ await this.#loadBlocks(this.#blocks)
162
+ const message = new BlockMessage(this.lastBlock)
163
+ await blockStore.put(message.hash, message.encoded)
164
+ await chainStore.put('lastBlock', new TextEncoder().encode(this.lastBlock.hash))
165
+ }
166
+ }
167
+
168
+ async #lastBlockHandler() {
169
+ return new peernet.protos['peernet-response']({response: new TextEncoder().encode(JSON.stringify({ hash: this.lastBlock?.hash, index: this.lastBlock?.index }))})
170
+ }
171
+
172
+ async resolveBlock(hash) {
173
+ let block = await peernet.get(hash, 'block')
174
+ if (!await peernet.has(hash, 'block')) await peernet.put(hash, block, 'block')
175
+ block = await new BlockMessage(block)
176
+ const size = block.encoded.length || block.encoded.byteLength
177
+ block = {...block.decoded, hash}
178
+ this.#blocks[block.index] = block
179
+ console.log(`loaded block: ${hash} @${block.index} ${Math.round((size / 1024 / 1024) * 100) / 100} mb`);
180
+ if (block.index !== 0) {
181
+ return this.resolveBlock(block.previousHash)
182
+ }
183
+ }
184
+
185
+ async resolveBlocks() {
186
+ try {
187
+
188
+ const localBlock = await chainStore.get('lastBlock')
189
+ const hash = new TextDecoder().decode(localBlock)
190
+ if (hash !== '0x0')
191
+ await this.resolveBlock(new TextDecoder().decode(localBlock))
192
+ this.#lastBlock = this.#blocks[this.#blocks.length - 1]
193
+ await this.#loadBlocks(this.#blocks)
194
+ } catch (e) {
195
+ await chainStore.put('lastBlock', new TextEncoder().encode('0x0'))
196
+ return this.resolveBlocks()
197
+ // console.log(e);
198
+ }
199
+ }
200
+
201
+ async #loadBlocks(blocks) {
202
+ for (const block of blocks) {
203
+ if (!block.loaded) {
204
+ for (const transaction of block.transactions) {
205
+ try {
206
+ await this.#machine.execute(transaction.to, transaction.method, transaction.params)
207
+
208
+ } catch (e) {
209
+ console.log(e);
210
+ }
211
+ }
212
+ block.loaded = true
213
+ // let message = await peernet.get(block.hash, 'block')
214
+
215
+ // const compressed = pako.deflate(message);
216
+ // const result = pako.inflate(compressed);
217
+ // console.log(result.length, compressed.length);
218
+ //
219
+ // console.log(result.length - compressed.length);
220
+
221
+ // message = new BlockMessage(message)
222
+ // for (const transaction of message.decoded.transactions) {
223
+ // try {
224
+ // await this.#machine.execute(transaction.to, transaction.method, transaction.params)
225
+ //
226
+ // } catch (e) {
227
+ // // console.log(e);
228
+ // }
229
+ // }
230
+ // block.loaded = true
231
+ }
232
+ }
233
+ }
234
+
235
+ async #executeTransaction({hash, from, to, method, params, nonce}) {
236
+ try {
237
+ let result = await this.#machine.execute(to, method, params, from, nonce)
238
+ // if (!result) result = this.#machine.state
239
+ pubsub.publish(`transaction.completed.${hash}`, {status: 'fulfilled', hash})
240
+ return result ? result : 'no state change'
241
+ } catch (e) {
242
+ pubsub.publish(`transaction.completed.${hash}`, {status: 'fail', hash, error: e})
243
+ throw e
244
+ }
245
+ }
246
+
247
+ async #addBlock(block) {
248
+ const blockMessage = new BlockMessage(block)
249
+ // if (!Buffer.isBuffer(block)) block = Buffer.from(block, 'hex')
250
+ // const transactionJob = async transaction => {
251
+ // try {
252
+ // transaction = await transactionPoolStore.get(transaction)
253
+ // } catch (e) {
254
+ // try {
255
+ // transaction = await peernet.get(transaction, 'transaction')
256
+ // } catch (e) {
257
+ // console.warn(`couldn't resolve ${transaction}`);
258
+ // }
259
+ // }
260
+ // transaction = new TransactionMessage(transaction)
261
+ // return transaction
262
+ // }
263
+ await Promise.all(blockMessage.decoded.transactions
264
+ .map(transaction => transactionPoolStore.delete(transaction.hash)))
265
+
266
+ // let transactions = blockMessage.decoded.transactions.map(tx => transactionJob(tx))
267
+ // transactions = await Promise.all(transactions)
268
+ this.#lastBlock = {hash: blockMessage.hash, ...blockMessage.decoded}
269
+ await blockStore.put(blockMessage.hash, blockMessage.encoded)
270
+ await chainStore.put('lastBlock', new TextEncoder().encode(blockMessage.hash))
271
+ info(`added block: ${blockMessage.hash}`)
272
+ let promises = []
273
+ let contracts = []
274
+ for (let transaction of blockMessage.decoded.transactions) {
275
+ // await transactionStore.put(transaction.hash, transaction.encoded)
276
+ const index = contracts.indexOf(transaction.to)
277
+ if (index === -1) contracts.push(transaction.to)
278
+ promises.push(this.#executeTransaction(transaction))
279
+ }
280
+ try {
281
+ promises = await Promise.allSettled(promises)
282
+
283
+ // todo finish state
284
+ for (const contract of contracts) {
285
+ const state = await this.#machine.get(contract, 'state')
286
+ // await stateStore.put(contract, state)
287
+ console.log(state);
288
+ }
289
+ } catch (e) {
290
+ console.log(e);
291
+ }
292
+
293
+ }
294
+
295
+
296
+
297
+ async participate() {
298
+ // TODO: validate participant
299
+ // hold min amount of 50k ART for 7 days
300
+ // lock the 50k
301
+ // introduce peer-reputation
302
+ // peerReputation(peerId)
303
+ // {bandwith: {up, down}, uptime}
304
+ if (!await this.staticCall(lib.validators, 'has', [peernet.id])) await this.createTransactionFrom(peernet.id, lib.validators, 'addValidator', [peernet.id])
305
+ if (await this.hasTransactionToHandle() && !this.#runningEpoch) return this.#runEpoch()
306
+ }
307
+
308
+ calculateFee(transaction) {
309
+ // excluded from fees
310
+ if (transaction.decoded.to === lib.validators) return 0
311
+ // fee per gb
312
+ return (transaction.encoded.length / 1024) / 1e-6
313
+ }
314
+
315
+ async #createBlock() {
316
+ let transactions = await transactionPoolStore.get()
317
+
318
+ if (Object.keys(transactions)?.length === 0 ) return
319
+ let block = {
320
+ transactions: [],
321
+ validators: [],
322
+ fees: 0
323
+ }
324
+
325
+ let _transactions = []
326
+
327
+ // exclude failing tx
328
+ for (let transaction of Object.values(transactions)) {
329
+ transaction = new TransactionMessage(new Uint8Array(transaction, transaction.byteOffset, transaction.byteLength))
330
+
331
+ block.fees += Number(lib.calculateFee(transaction))
332
+ _transactions.push({...transaction.decoded, hash: transaction.hash })
333
+ }
334
+
335
+ transactions = _transactions.sort((a, b) => a.nonce - b.nonce)
336
+ _transactions = []
337
+ for (let transaction of transactions) {
338
+ try {
339
+ await this.#executeTransaction(transaction)
340
+ _transactions.push(transaction)
341
+ } catch (e) {
342
+ transaction = new TransactionMessage(transaction)
343
+ block.fees -= Number(lib.calculateFee(transaction))
344
+ await transactionPoolStore.delete(transaction.hash)
345
+
346
+ }
347
+ }
348
+ transactions = _transactions
349
+ // don't add empty block
350
+ if (transactions.length === 0) return
351
+
352
+ block.transactions = transactions
353
+ const validators = await this.staticCall(lib.validators, 'validators')
354
+ console.log({validators});
355
+ // block.validators = Object.keys(block.validators).reduce((set, key) => {
356
+ // if (block.validators[key].active) {
357
+ // push({
358
+ // address: key
359
+ // })
360
+ // }
361
+ // }, [])
362
+ const peers = {}
363
+ for (const entry of peernet.peerEntries) {
364
+ peers[entry[0]] = entry[1]
365
+ }
366
+ for (const validator of Object.keys(validators)) {
367
+ if (validators[validator].active) {
368
+ const peer = peers[validator]
369
+ if (peer && peer.connected) {
370
+ let data = new BWRequestMessage()
371
+ const node = await peernet.prepareMessage(validator, data.encoded)
372
+ try {
373
+ const bw = await peer.request(node.encoded)
374
+ console.log(bw);
375
+ block.validators.push({
376
+ address: validator,
377
+ bw: bw.up + bw.down
378
+ })
379
+ } catch(e) {
380
+
381
+ }
382
+
383
+ } else if (peernet.id === validator) {
384
+ block.validators.push({
385
+ address: peernet.id,
386
+ bw: peernet.bw.up + peernet.bw.down
387
+ })
388
+
389
+ }
390
+ }
391
+
392
+ }
393
+
394
+ console.log(block.validators);
395
+
396
+ block.reward = 150
397
+ block.validators = block.validators.map(validator => {
398
+ validator.reward = String(Number(block.fees) + block.reward / block.validators.length)
399
+ delete validator.bw
400
+ return validator
401
+ })
402
+ // block.validators = lib.calculateValidatorReward(block.validators, block.fees)
403
+
404
+ block.index = this.lastBlock?.index
405
+ if (block.index === undefined) block.index = 0
406
+ else block.index += 1
407
+
408
+ block.previousHash = this.lastBlock?.hash || '0x0'
409
+ block.timestamp = new Date().getTime()
410
+
411
+ const parts = String(block.fees).split('.')
412
+ let decimals = 0
413
+ if (parts[1]) {
414
+ const potentional = parts[1].split('e')
415
+ if (potentional[0] !== parts[1]) {
416
+ parts[1] = potentional[0]
417
+ decimals = Number(potentional[1]?.replace(/\-|\+/g, '')) + Number(potentional[0].length)
418
+ } else {
419
+ decimals = parts[1].length
420
+ }
421
+
422
+ }
423
+ block.fees = Number.parseFloat(String(block.fees)).toFixed(decimals)
424
+
425
+ try {
426
+ let blockMessage = new BlockMessage(block)
427
+ this.#lastBlock = {...block, hash: blockMessage.hash}
428
+ peernet.publish('add-block', blockMessage.encoded)
429
+ this.#addBlock(blockMessage.encoded)
430
+ } catch (e) {
431
+ throw Error(`invalid block ${block}`)
432
+ }
433
+ // data = await this.#machine.execute(to, method, params)
434
+ // transactionStore.put(message.hash, message.encoded)
435
+ }
436
+
437
+ async getNonce(address) {
438
+ let transactions = await transactionPoolStore.get()
439
+ transactions = Object.keys(transactions).map(tx => new TransactionMessage(transactions[tx]))
440
+ transactions = transactions.filter(tx => tx.decoded.from === address)
441
+ transactions = transactions.map(tx => { return {...tx.decoded, hash: tx.hash} })
442
+ if (this.lastBlock && transactions.length === 0) {
443
+ let block = await peernet.get(this.lastBlock.hash)
444
+ block = new BlockMessage(block)
445
+
446
+ // for (let tx of block.decoded?.transactions) {
447
+ // tx = await peernet.get(tx, 'transaction')
448
+ // transactions.push(new TransactionMessage(tx))
449
+ // }
450
+ transactions = transactions.filter(tx => tx.from === address)
451
+ while (transactions.length === 0 && block.decoded.index !== 0) {
452
+ block = await blockStore.get(block.decoded.previousHash)
453
+ block = new BlockMessage(block)
454
+ transactions = block.decoded.transactions.filter(tx => tx.from === address)
455
+ }
456
+
457
+ }
458
+ if (transactions.length === 0) return 0
459
+
460
+ transactions = transactions.sort((a, b) => a.timestamp - b.timestamp)
461
+ return transactions[transactions.length - 1].nonce
462
+ }
463
+
464
+ /**
465
+ * whenever method = createContract params should hold the contract hash
466
+ *
467
+ * example: [hash]
468
+ * createTransaction('0x0', 'createContract', [hash])
469
+ */
470
+ createTransaction(to, method, params, nonce) {
471
+ return this.createTransactionFrom(peernet.id, to, method, params, nonce)
472
+ }
473
+ /**
474
+ * every tx done is trough contracts so no need for amount
475
+ * data is undefined when nothing is returned
476
+ * error is thrown on error so undefined data doesn't mean there is an error...
477
+ */
478
+ async createTransactionFrom(from, to, method, params, nonce) {
479
+ if (nonce === undefined) {
480
+ nonce = await this.getNonce(from)
481
+ nonce += 1
482
+ }
483
+
484
+ let data
485
+ let message = new TransactionMessage({timestamp: new Date().getTime(), from, to, nonce, method, params})
486
+ try {
487
+ // await transactionPoolStore.put(message.hash, new TextEncoder().encode(JSON.stringify({signature, message: message.encoded})))
488
+ await transactionPoolStore.put(message.hash, message.encoded)
489
+ peernet.publish('add-transaction', message.encoded)
490
+ } catch (e) {
491
+ throw e
492
+ }
493
+ const wait = () => new Promise((resolve, reject) => {
494
+ const completed = result => {
495
+ result.status === 'fulfilled' ? resolve(result.hash) : reject({hash: result.hash, error: result.error})
496
+
497
+ pubsub.unsubscribe(`transaction.completed.${message.hash}`, completed)
498
+ }
499
+ pubsub.subscribe(`transaction.completed.${message.hash}`, completed)
500
+ })
501
+
502
+ if (await this.hasTransactionToHandle() && !this.#runningEpoch) this.#runEpoch()
503
+ return {hash: message.hash, data, fee: lib.calculateFee(message), wait}
504
+ }
505
+
506
+ async createContractMessage(creator, contract, constructorParameters = []) {
507
+ return new ContractMessage({
508
+ creator,
509
+ contract,
510
+ constructorParameters
511
+ })
512
+ }
513
+
514
+ async createContractAddress(creator, contract, constructorParameters = []) {
515
+ return this.createContractMessage(creator, contract, constructorParameters)
516
+ .hash
517
+ }
518
+
519
+ /**
520
+ *
521
+ */
522
+ async deployContract(contract, params = []) {
523
+ globalThis.msg = {sender: peernet.id, call: this.call}
524
+
525
+ const hash = await this.createContractAddress(creator, contract, params)
526
+
527
+ try {
528
+ const tx = await this.createTransactionFrom(peernet.id, lib.contractFactory, 'deployContract', [hash, creator, contract, constructorParameters])
529
+ } catch (e) {
530
+ throw e
531
+ }
532
+ return this.#machine.addContract(message)
533
+ }
534
+
535
+ #createMessage(sender = peernet.id) {
536
+ return {
537
+ sender: peernet.id,
538
+ call: this.call,
539
+ staticCall: this.staticCall,
540
+ delegate: this.delegate,
541
+ staticDelegate: this.staticDelegate
542
+ }
543
+ }
544
+
545
+ internalCall(sender, contract, method, params) {
546
+ globalThis.msg = this.#createMessage(sender)
547
+
548
+ return this.#machine.execute(contract, method, params)
549
+ }
550
+
551
+ call(contract, method, params) {
552
+ globalThis.msg = this.#createMessage()
553
+
554
+ return this.#machine.execute(contract, method, params)
555
+ }
556
+
557
+ staticCall(contract, method, params) {
558
+ globalThis.msg = this.#createMessage()
559
+ return this.#machine.get(contract, method, params)
560
+ }
561
+
562
+ delegate(contract, method, params) {
563
+ globalThis.msg = this.#createMessage()
564
+
565
+ return this.#machine.execute(contract, method, params)
566
+ }
567
+
568
+ staticDelegate(contract, method, params) {
569
+ globalThis.msg = this.#createMessage()
570
+
571
+ return this.#machine.get(contract, method, params)
572
+ }
573
+
574
+ mint(to, amount) {
575
+ return this.call(lib.nativeToken, 'mint', [to, amount])
576
+ }
577
+
578
+ transfer(from, to, amount) {
579
+ return this.call(lib.nativeToken, 'transfer', [from, to, amount])
580
+ }
581
+
582
+ get balances() {
583
+ return this.staticCall(lib.nativeToken, 'balances')
584
+ }
585
+
586
+ deleteAll() {
587
+ return this.#machine.deleteAll()
588
+ }
589
+
590
+ lookup(name) {
591
+ return this.call(lib.nameService, 'lookup', [name])
592
+ }
593
+ }
@@ -0,0 +1,15 @@
1
+ import main from './main'
2
+ import protocol from './protocol'
3
+ import { read, write } from './../utils/utils'
4
+
5
+ export default async () => {
6
+ let config = { ...main, ...protocol }
7
+ try {
8
+ let data = await read(main.configPath)
9
+ data = JSON.parse(data.toString())
10
+ config = { ...config, ...data }
11
+ } catch (e) {
12
+ await write(main.configPath, JSON.stringify(config, null, '\t'))
13
+ }
14
+ return config
15
+ }
@@ -0,0 +1,4 @@
1
+ export default {
2
+ datadir: '',
3
+ configPath: ''
4
+ }
@@ -0,0 +1,5 @@
1
+ export default {
2
+ block: {
3
+ size: 1024
4
+ }
5
+ }
@@ -0,0 +1,59 @@
1
+ export default class Factory {
2
+ /**
3
+ * string
4
+ */
5
+ #name = 'ArtOnlineContractFactory'
6
+ /**
7
+ * uint
8
+ */
9
+ #totalContracts = 0
10
+ /**
11
+ * Array => string
12
+ */
13
+ #contracts = []
14
+
15
+ constructor(state) {
16
+ if (state) {
17
+ this.#contracts = state.contracts
18
+ this.#totalContracts = state.totalContracts
19
+ }
20
+ }
21
+
22
+ get state() {
23
+ return {
24
+ totalContracts: this.#totalContracts,
25
+ contracts: this.#contracts
26
+ }
27
+ }
28
+
29
+ get name() {
30
+ return this.#name
31
+ }
32
+
33
+ get contracts() {
34
+ return [...this.#contracts]
35
+ }
36
+
37
+ get totalContracts() {
38
+ return this.#totalContracts
39
+ }
40
+
41
+ isvalid(hash, creator, contract, constructorParameters = []) {
42
+ const message = new ContractMessage({
43
+ creator,
44
+ contract,
45
+ constructorParameters
46
+ })
47
+ return Boolean(message.hash === hash)
48
+ }
49
+
50
+ async deployContract(contractHash, creator, contract, constructorParameters = []) {
51
+ if (contract.creator !== msg.sender) throw new Error('only a contract creator can deploy a contract')
52
+ if (await contractStore.has(hash)) throw new Error('duplicate contract')
53
+ if (!this.isValid(contractHash, creator, contract, constructorParameters)) throw new Error('invalid contract')
54
+
55
+ await contractStore.put(hash, encoded)
56
+ this.#totalContracts += 1
57
+ this.#contracts.push(hash)
58
+ }
59
+ }