godprotocol 1.1.32 → 1.2.0
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/Datatypes/arr.js +559 -254
- package/Datatypes/bool.js +8 -5
- package/Datatypes/callable.js +1 -1
- package/Datatypes/cls.js +193 -24
- package/Datatypes/conf.js +2 -2
- package/Datatypes/err.js +25 -0
- package/Datatypes/func.js +89 -13
- package/Datatypes/functions/storage.js +572 -12
- package/Datatypes/functions/wallet.js +49 -0
- package/Datatypes/instance.js +87 -122
- package/Datatypes/num.js +192 -153
- package/Datatypes/str.js +188 -122
- package/Datatypes/twa.js +346 -71
- package/Datatypes/voi.js +9 -4
- package/Index.js +1 -1
- package/Instances/account.js +0 -0
- package/Instances/event.js +0 -0
- package/Instances/fold.js +103 -0
- package/Instances/folder_queries.js +46 -0
- package/Instances/response.js +31 -0
- package/Objects/Account.js +160 -110
- package/Objects/Blockweb.js +6 -11
- package/Objects/Callable.js +223 -188
- package/Objects/Chain.js +127 -142
- package/Objects/Datatypes.js +114 -588
- package/Objects/Manager.js +349 -39
- package/Objects/Oracle.js +283 -210
- package/Objects/Repository.js +12 -33
- package/Objects/Trinity.js +9 -7
- package/Objects/Utils.js +8 -8
- package/Objects/Virtual_machine.js +320 -320
- package/README.md +0 -0
- package/callables/functions/assert.js +10 -0
- package/callables/functions/display.js +2 -3
- package/callables/functions/env.js +5 -0
- package/callables/functions/event.js +14 -0
- package/callables/functions/exit.js +3 -2
- package/callables/functions/folder.js +15 -0
- package/callables/functions/get_uid.js +6 -0
- package/callables/functions/hash.js +7 -0
- package/callables/functions/import_.js +62 -0
- package/callables/functions/local.js +9 -0
- package/callables/functions/net.js +33 -18
- package/callables/functions/parse.js +35 -0
- package/callables/functions/query.js +12 -0
- package/callables/functions/render.js +9 -13
- package/callables/functions/servers.js +10 -0
- package/callables/functions/spawn.js +7 -0
- package/callables/functions/store.js +75 -0
- package/callables/functions/super_.js +24 -0
- package/callables/functions/typeof.js +32 -0
- package/callables/functions/wait.js +9 -0
- package/callables/register.js +193 -8
- package/html/create.js +110 -0
- package/html/index.js +114 -0
- package/html/load.js +165 -0
- package/html/parse.js +171 -0
- package/html/run.js +151 -0
- package/html/util.js +5 -0
- package/package.json +5 -3
- package/repos/fs.js +114 -0
- package/repos/github.js +73 -0
- package/repos/ram.js +47 -0
- package/utils/create_server.js +66 -26
- package/utils/hash.js +3 -2
- package/utils/ops.js +2 -0
- package/utils/oracle_handlers.js +7 -0
- package/utils/services.js +18 -51
- package/utils/socket_server.js +36 -0
- package/utils/vm_utils.js +241 -137
- package/callables/functions/assign.js +0 -6
|
@@ -1,23 +1,583 @@
|
|
|
1
|
+
import { generate_random_string } from "generalised-datastore/utils/functions.js";
|
|
2
|
+
|
|
1
3
|
class Storage {
|
|
2
|
-
constructor(
|
|
3
|
-
this.
|
|
4
|
-
|
|
4
|
+
constructor(account) {
|
|
5
|
+
this.account = account
|
|
6
|
+
|
|
7
|
+
this.methods = {
|
|
8
|
+
'get_id': {args:[], ret: ['_id', 'string']},
|
|
9
|
+
'folder': {args:[['address', 'string'], ['options', 'twain']], ret: ['folder', 'folder']},
|
|
10
|
+
|
|
11
|
+
'__and__': {args: [['other', 'object']], ret: ['result', 'object']},
|
|
12
|
+
'__sub__': {args: [['other', 'object']], ret: ['result', 'object']},
|
|
13
|
+
'__or__': {args: [['other', 'object']], ret: ['result', 'object']},
|
|
14
|
+
'__add__': {args: [['other', 'object']], ret: ['result', 'object']},
|
|
15
|
+
'__mul__': {args: [['other', 'object']], ret: ['result', 'object']},
|
|
16
|
+
'__div__': {args: [['other', 'object']], ret: ['result', 'object']},
|
|
17
|
+
'__gt__': {args: [['other', 'object']], ret: ['result', 'object']},
|
|
18
|
+
'__lt__': {args: [['other', 'object']], ret: ['result', 'object']},
|
|
19
|
+
'__gte__': {args: [['other', 'object']], ret: ['result', 'object']},
|
|
20
|
+
'__lte__': {args: [['other', 'object']], ret: ['result', 'object']},
|
|
21
|
+
'__ne__': {args: [['other', 'object']], ret: ['result', 'object']},
|
|
22
|
+
'__eq__': {args: [['other', 'object']], ret: ['result', 'object']},
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
__or__ = async(other)=>{
|
|
27
|
+
let result;
|
|
28
|
+
switch(this.type){
|
|
29
|
+
case 'number':
|
|
30
|
+
if (typeof other === 'number')
|
|
31
|
+
result = this.value || other
|
|
32
|
+
break
|
|
33
|
+
case 'string':
|
|
34
|
+
result = this.value || other
|
|
35
|
+
break
|
|
36
|
+
case 'void':
|
|
37
|
+
result = await other.to_address()
|
|
38
|
+
break
|
|
39
|
+
case 'function':
|
|
40
|
+
result = await this.to_address()
|
|
41
|
+
break
|
|
42
|
+
case 'instance':
|
|
43
|
+
result = await this.run('__or__', {config: {arguments: [other], location: this.spawn_address()}, chain: this.account.chain})
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
if (!result) result = await this.account.vm.voided()
|
|
47
|
+
|
|
48
|
+
return result
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
__and__ = async(other)=>{
|
|
52
|
+
let result;
|
|
53
|
+
|
|
54
|
+
switch(this.type){
|
|
55
|
+
case 'number':
|
|
56
|
+
if (typeof other === 'number')
|
|
57
|
+
result = this.value && other
|
|
58
|
+
break
|
|
59
|
+
case 'instance':
|
|
60
|
+
result = await this.run('__and__', {config: {arguments: [other], location: this.spawn_address()}, chain: this.account.chain})
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
if (!result) result = await this.account.vm.voided()
|
|
64
|
+
|
|
65
|
+
return result
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
__gt__ = async(other)=>{
|
|
69
|
+
let result;
|
|
70
|
+
|
|
71
|
+
switch(this.type){
|
|
72
|
+
case 'number':
|
|
73
|
+
if (typeof other === 'number')
|
|
74
|
+
result = this.value > other
|
|
75
|
+
break
|
|
76
|
+
case 'instance':
|
|
77
|
+
result = await this.run('__gt__', {config: {arguments: [other], location: this.spawn_address()}, chain: this.account.chain})
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
if (!result) result = await this.account.vm.voided()
|
|
81
|
+
|
|
82
|
+
return result
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
__gte__ = async(other)=>{
|
|
86
|
+
let result;
|
|
87
|
+
|
|
88
|
+
switch(this.type){
|
|
89
|
+
case 'number':
|
|
90
|
+
if (typeof other === 'number')
|
|
91
|
+
result = this.value >= other
|
|
92
|
+
break
|
|
93
|
+
case 'instance':
|
|
94
|
+
result = await this.run('__gte__', {config: {arguments: [other], location: this.spawn_address()}, chain: this.account.chain})
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
if (!result) result = await this.account.vm.voided()
|
|
98
|
+
|
|
99
|
+
return result
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
__lt__ = async(other)=>{
|
|
103
|
+
let result;
|
|
104
|
+
|
|
105
|
+
switch(this.type){
|
|
106
|
+
case 'number':
|
|
107
|
+
if (typeof other === 'number')
|
|
108
|
+
result = this.value < other
|
|
109
|
+
break
|
|
110
|
+
case 'instance':
|
|
111
|
+
result = await this.run('__lt__', {config: {arguments: [other], location: this.spawn_address()}, chain: this.account.chain})
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
if (!result) result = await this.account.vm.voided()
|
|
115
|
+
|
|
116
|
+
return result
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
__lte__ = async(other)=>{
|
|
120
|
+
let result;
|
|
121
|
+
|
|
122
|
+
switch(this.type){
|
|
123
|
+
case 'number':
|
|
124
|
+
if (typeof other === 'number')
|
|
125
|
+
result = this.value <= other
|
|
126
|
+
break
|
|
127
|
+
case 'instance':
|
|
128
|
+
result = await this.run('__lte__', {config: {arguments: [other], location: this.spawn_address()}, chain: this.account.chain})
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
if (!result) result = await this.account.vm.voided()
|
|
132
|
+
|
|
133
|
+
return result
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
__eq__ = async(other)=>{
|
|
137
|
+
let result;
|
|
138
|
+
|
|
139
|
+
switch(this.type){
|
|
140
|
+
case 'number':
|
|
141
|
+
if (typeof other === 'number')
|
|
142
|
+
result = this.value == other
|
|
143
|
+
break
|
|
144
|
+
case 'string':
|
|
145
|
+
result = this.value == other
|
|
146
|
+
break
|
|
147
|
+
case 'class':
|
|
148
|
+
result = this.ref() === other.ref()
|
|
149
|
+
break
|
|
150
|
+
case 'function':
|
|
151
|
+
result = this.ref() === other.ref()
|
|
152
|
+
break
|
|
153
|
+
case 'instance':
|
|
154
|
+
await this.run('__eq__', {config: {arguments: [other], location: this.spawn_address()}, chain: this.account.chain})
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
if (result == null) result = await this.account.vm.voided()
|
|
158
|
+
|
|
159
|
+
return result
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
__ne__ = async(other)=>{
|
|
163
|
+
let result;
|
|
164
|
+
|
|
165
|
+
switch(this.type){
|
|
166
|
+
case 'number':
|
|
167
|
+
if (typeof other === 'number')
|
|
168
|
+
result = this.value != other
|
|
169
|
+
break
|
|
170
|
+
case 'void':
|
|
171
|
+
result = other.type === 'void'
|
|
172
|
+
break
|
|
173
|
+
case 'class':
|
|
174
|
+
result = this.ref() !== other.ref()
|
|
175
|
+
break
|
|
176
|
+
case 'function':
|
|
177
|
+
result = this.ref() !== other.ref()
|
|
178
|
+
break
|
|
179
|
+
case 'instance':
|
|
180
|
+
result = await this.run('__ne__', {config: {arguments: [other], location: this.spawn_address()}, chain: this.account.chain})
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
if (result==null) result = await this.account.vm.voided()
|
|
184
|
+
|
|
185
|
+
return result
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
__div__ = async(other)=>{
|
|
189
|
+
let result;
|
|
190
|
+
|
|
191
|
+
switch(this.type){
|
|
192
|
+
case 'number':
|
|
193
|
+
if (typeof other === 'number')
|
|
194
|
+
result = this.value / other
|
|
195
|
+
break
|
|
196
|
+
case 'instance':
|
|
197
|
+
result = await this.run('__div__', {config: {arguments: [other], location: this.spawn_address()}, chain: this.account.chain})
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
if (!result) result = await this.account.vm.voided()
|
|
201
|
+
|
|
202
|
+
return result
|
|
5
203
|
}
|
|
6
204
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
205
|
+
__mul__ = async(other)=>{
|
|
206
|
+
let result;
|
|
207
|
+
|
|
208
|
+
switch(this.type){
|
|
209
|
+
case 'number':
|
|
210
|
+
if (typeof other === 'number')
|
|
211
|
+
result = this.value * other
|
|
212
|
+
break
|
|
213
|
+
case 'string':
|
|
214
|
+
if (typeof other === 'string')
|
|
215
|
+
result = await this.repeat(other)
|
|
216
|
+
break;
|
|
217
|
+
case 'instance':
|
|
218
|
+
result = await this.run('__mul__', {config: {arguments: [other], location: this.spawn_address()}, chain: this.account.chain})
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
if (!result) result = await this.account.vm.voided()
|
|
222
|
+
|
|
223
|
+
return result
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
__sub__ = async(other)=>{
|
|
227
|
+
let result;
|
|
228
|
+
|
|
229
|
+
switch(this.type){
|
|
230
|
+
case 'number':
|
|
231
|
+
if (typeof other === 'number')
|
|
232
|
+
result = this.value - other
|
|
233
|
+
break
|
|
234
|
+
case 'instance':
|
|
235
|
+
result = await this.run('__sub__', {config: {arguments: [other], location: this.spawn_address()}, chain: this.account.chain})
|
|
236
|
+
break;
|
|
237
|
+
}
|
|
238
|
+
if (!result) result = await this.account.vm.voided()
|
|
239
|
+
|
|
240
|
+
return result
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
__add__ = async(other)=>{
|
|
244
|
+
let result;
|
|
245
|
+
|
|
246
|
+
switch(this.type){
|
|
247
|
+
case 'number':
|
|
248
|
+
if (typeof other === 'number')
|
|
249
|
+
result = this.value + other
|
|
250
|
+
break
|
|
251
|
+
case 'string':
|
|
252
|
+
if (typeof other === 'string')
|
|
253
|
+
result = await this.concat(other)
|
|
254
|
+
break;
|
|
255
|
+
case 'instance':
|
|
256
|
+
result = await this.run('__add__', {config: {arguments: [other], location: this.spawn_address()}, chain: this.account.chain})
|
|
257
|
+
break;
|
|
11
258
|
}
|
|
12
|
-
|
|
259
|
+
if (!result) result = await this.account.vm.voided()
|
|
260
|
+
|
|
261
|
+
return result
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
_folder = async(options={})=>{
|
|
265
|
+
let addr = this.type === 'folder'? this.config.value: this.address
|
|
266
|
+
let folder = await this.account.manager.ds.folder(addr, {...options})
|
|
267
|
+
|
|
268
|
+
return folder;
|
|
13
269
|
}
|
|
14
270
|
|
|
15
|
-
|
|
16
|
-
|
|
271
|
+
emit = async(name, args)=>{
|
|
272
|
+
name = await name.literal()
|
|
273
|
+
if(this.type === 'instance'){
|
|
274
|
+
name = `${name}:${this.config._id}`
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
let fold = await this._folder()
|
|
278
|
+
let res = await fold.emit(name, args[1]? [await args[1].to_address()]: [], {account: this.account})
|
|
279
|
+
let {vm, chain} = this.account
|
|
280
|
+
|
|
281
|
+
return await vm.instantiate({
|
|
282
|
+
type: 'array',
|
|
283
|
+
value: res,
|
|
284
|
+
location: vm.gen_location()
|
|
285
|
+
}, {chain})
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
listen = async(name, args)=>{
|
|
289
|
+
name = await name.literal()
|
|
290
|
+
let func = await args[1].to_address()
|
|
291
|
+
|
|
292
|
+
if (this.type === 'instance'){
|
|
293
|
+
name = `${name}:${this.config._id}`
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
let fold = await this._folder()
|
|
297
|
+
await fold.listen(name, func, {account: this.account})
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
folder = async(address, args, {call_config, chain})=>{
|
|
301
|
+
let parent = this.config.location
|
|
302
|
+
if (this.type === 'instance'){
|
|
303
|
+
parent = `${this.config.cls}/${this.config._id}`
|
|
304
|
+
}
|
|
305
|
+
let fold = await this.account.manager.ds.folder(parent, {account: this.account.physical_address})
|
|
306
|
+
address = await address.literal()
|
|
307
|
+
|
|
308
|
+
let subfolder = await fold.folder(address, {account: this.account.physical_address})
|
|
309
|
+
|
|
310
|
+
let obj = {
|
|
311
|
+
type: 'folder',
|
|
312
|
+
value: subfolder.address,
|
|
313
|
+
location: call_config.location
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
let folder = await chain.folder(call_config.location)
|
|
317
|
+
let res = await folder.write(obj)
|
|
318
|
+
|
|
319
|
+
return {
|
|
320
|
+
type: 'address',
|
|
321
|
+
value: folder.address,
|
|
322
|
+
_id: res._id
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
get_id = async()=>{
|
|
327
|
+
return this.config._id
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
call = async(method, options)=>{
|
|
331
|
+
let {chain, call_config, pure, thread} = options
|
|
332
|
+
let {arguments: args} = call_config, other, result;
|
|
333
|
+
let method_name = method;
|
|
334
|
+
|
|
335
|
+
let args_ = new Array()
|
|
336
|
+
for(let a=0; a < args.length; a++){
|
|
337
|
+
args_[a] = await this.account.vm.cloth(args[a])
|
|
338
|
+
}
|
|
339
|
+
args = args_
|
|
340
|
+
switch(this.type){
|
|
341
|
+
case 'number':
|
|
342
|
+
this.value = await this.literal()
|
|
343
|
+
other = args[0] && await args[0].literal()
|
|
344
|
+
method = this[method]
|
|
345
|
+
if (typeof method === 'function'){
|
|
346
|
+
result = await method(other, args, {call_config, thread, chain})
|
|
347
|
+
}
|
|
348
|
+
break;
|
|
349
|
+
case 'string':
|
|
350
|
+
this.value = await this.literal()
|
|
351
|
+
other = args[0] && await args[0].literal()
|
|
352
|
+
|
|
353
|
+
method = this[method]
|
|
354
|
+
if (typeof method === 'function'){
|
|
355
|
+
result = await method(other, args, {call_config})
|
|
356
|
+
}else {
|
|
357
|
+
result = await this.overloads(method_name, other, args, {call_config, thread, chain})
|
|
358
|
+
}
|
|
359
|
+
break;
|
|
360
|
+
case 'array':
|
|
361
|
+
if (method === 'call') method = 'call_'
|
|
362
|
+
method = this[method]
|
|
363
|
+
if(typeof method === 'function'){
|
|
364
|
+
result = await method(args[0], args, {call_config, thread, chain})
|
|
365
|
+
}
|
|
366
|
+
break;
|
|
367
|
+
case 'twain':
|
|
368
|
+
if(['set', 'get'].includes(method)){
|
|
369
|
+
method = `${method}_`
|
|
370
|
+
}
|
|
371
|
+
method = this[method]
|
|
372
|
+
if (typeof method === 'function'){
|
|
373
|
+
result = await method(args[0], args, {call_config, thread, chain})
|
|
374
|
+
}
|
|
375
|
+
break;
|
|
376
|
+
case 'function':
|
|
377
|
+
method = this[method]
|
|
378
|
+
if (typeof method === 'function'){
|
|
379
|
+
result = await method(args[0], args, {call_config, thread, chain})
|
|
380
|
+
}
|
|
381
|
+
break;
|
|
382
|
+
case 'instance':
|
|
383
|
+
method = this[method]
|
|
384
|
+
if(typeof method === 'function'){
|
|
385
|
+
result = await method(args[0], args, {call_config, chain})
|
|
386
|
+
}
|
|
387
|
+
break;
|
|
388
|
+
case 'class':
|
|
389
|
+
method = this[method]
|
|
390
|
+
if (typeof method === 'function'){
|
|
391
|
+
result = await method(args[0], args, {call_config, thread, chain})
|
|
392
|
+
}
|
|
393
|
+
break;
|
|
394
|
+
default:
|
|
395
|
+
method = this[method]
|
|
396
|
+
if (typeof method === 'function'){
|
|
397
|
+
result = await method(args[0], args, {call_config, thread, chain})
|
|
398
|
+
}
|
|
399
|
+
;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
if(pure){
|
|
403
|
+
return result;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
if (result && result.__interrupt__){}
|
|
407
|
+
else if (result && ['address', 'folder'].includes(result.type)){
|
|
408
|
+
}else result = await this.account.vm.parse_aircode(result, {
|
|
409
|
+
config: call_config,
|
|
410
|
+
chain
|
|
411
|
+
})
|
|
412
|
+
|
|
413
|
+
return result
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
overloads = async (name, other, args)=>{
|
|
417
|
+
let lit = await this.literal(), result;
|
|
418
|
+
|
|
419
|
+
switch(this.type){
|
|
420
|
+
case "string":
|
|
421
|
+
switch(name){
|
|
422
|
+
case '__eq__':
|
|
423
|
+
result = lit === other
|
|
424
|
+
break;
|
|
425
|
+
case '__ne__':
|
|
426
|
+
result = lit !== other
|
|
427
|
+
break;
|
|
428
|
+
case '__lt__':
|
|
429
|
+
result = lit < other;
|
|
430
|
+
break;
|
|
431
|
+
case '__lte__':
|
|
432
|
+
result = lit <= other;
|
|
433
|
+
break;
|
|
434
|
+
case '__gt__':
|
|
435
|
+
result = lit > other;
|
|
436
|
+
break;
|
|
437
|
+
case '__or__':
|
|
438
|
+
result = lit || other;
|
|
439
|
+
break;
|
|
440
|
+
case '__and__':
|
|
441
|
+
result = lit && other;
|
|
442
|
+
break;
|
|
443
|
+
case '__gte__':
|
|
444
|
+
result = lit >= other;
|
|
445
|
+
break;
|
|
446
|
+
case '__mul__':
|
|
447
|
+
result = await this.repeat(other, args)
|
|
448
|
+
break;
|
|
449
|
+
case '__add__':
|
|
450
|
+
result = await this.concat(other, args)
|
|
451
|
+
break
|
|
452
|
+
}
|
|
453
|
+
break;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
return result
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
ref = ()=>{
|
|
460
|
+
return `${this.address || this.config.address}/${this.config._id}`
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
get_ =async(prop)=>{
|
|
464
|
+
if (!this.methods[prop] && !this[prop]) return;
|
|
465
|
+
|
|
466
|
+
return {
|
|
467
|
+
type: 'address',
|
|
468
|
+
object: this.ref(),
|
|
469
|
+
value: prop
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
get = async(prop, options={})=>{
|
|
474
|
+
let {obj, raw} = options
|
|
475
|
+
let result;
|
|
476
|
+
|
|
477
|
+
if (this.type === 'twain' && this.is_literal){
|
|
478
|
+
if (Object.keys(this.methods).includes(prop)){
|
|
479
|
+
return {
|
|
480
|
+
type: 'address',
|
|
481
|
+
value: prop,
|
|
482
|
+
object: this.ref()
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
if (this.account.vm.datatypes.includes(this.type) || this.type === 'error' || ['function', 'class'].includes(this.type)){
|
|
488
|
+
if(this.type === 'void' && !Object.keys(this.methods).includes(prop)){
|
|
489
|
+
result = await this.account.vm.throw_error({type: "ValueError", message: "Cannot subset a void value"})
|
|
490
|
+
} else if(this.type === 'function'){
|
|
491
|
+
result = await this.get_(prop)
|
|
492
|
+
} else if (this.type === 'twain'){
|
|
493
|
+
result = await this.get_(prop)
|
|
494
|
+
}else if (options.obj && options.obj.type === 'number' && ['array', 'string'].includes(this.type)){
|
|
495
|
+
if (this.type === 'array')
|
|
496
|
+
result = await this.index(prop)
|
|
497
|
+
else {
|
|
498
|
+
result = await this.call('index', {
|
|
499
|
+
chain: this.account.chain,
|
|
500
|
+
call_config: {arguments: [options.config], location: this.spawn_address()}
|
|
501
|
+
})
|
|
502
|
+
}
|
|
503
|
+
} else if (!prop.includes('/')){
|
|
504
|
+
if (this.methods[prop])
|
|
505
|
+
result = {
|
|
506
|
+
type: 'address',
|
|
507
|
+
value: prop,
|
|
508
|
+
object: this.ref()
|
|
509
|
+
}
|
|
510
|
+
if (['function', 'class'].includes(this.type)){
|
|
511
|
+
result.config = true;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
}else if(this.type === 'instance'){
|
|
515
|
+
result = await this.get_(obj? await obj.literal() :prop)
|
|
516
|
+
} else if(this.type === 'folder'){
|
|
517
|
+
result = await this.get_(obj? await obj.literal() : prop)
|
|
518
|
+
}
|
|
519
|
+
if (!result && !raw){
|
|
520
|
+
result = await this.account.vm.statics({type:'void'})
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
return result
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
set = async(prop, value, options)=>{
|
|
527
|
+
let {chain} = options;
|
|
528
|
+
|
|
529
|
+
if (this.type === 'twain'){
|
|
530
|
+
await this.call('set', {call_config: {arguments: [prop, value], location: this.spawn_address()}, chain})
|
|
531
|
+
}else if(this.type === 'instance' || this.type === 'function'){
|
|
532
|
+
return await this.set_(prop, value)
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
chain =async ()=>{
|
|
537
|
+
if (this.chn || !this.address)return this.chn;
|
|
538
|
+
|
|
539
|
+
this.chn = await this.account.chain.chain(this.address)
|
|
540
|
+
|
|
541
|
+
return this.chn
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
sync = async()=>{
|
|
545
|
+
return this
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
spawn_address = (spw)=>{
|
|
549
|
+
return `${this.address}_${spw || Math.random().toString().slice(3)}`
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
_split_datapath = (datapath)=>{
|
|
553
|
+
datapath = datapath.split('/')
|
|
554
|
+
|
|
555
|
+
return [datapath.slice(0,-1).join('/'), datapath.slice(-1)[0]]
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
to_address = async()=>{
|
|
559
|
+
let o = {
|
|
560
|
+
type: 'address',
|
|
561
|
+
value: this.address || this.config.location,
|
|
562
|
+
_id: this.config._id,
|
|
563
|
+
}
|
|
564
|
+
if (this.value && this.value.object) o.object = this.value.object
|
|
565
|
+
|
|
566
|
+
if (['function', 'class'].includes(this.type)){
|
|
567
|
+
o.config = true
|
|
568
|
+
o.value = this.config.address
|
|
569
|
+
}
|
|
570
|
+
o.module = this.config.module
|
|
571
|
+
|
|
572
|
+
if (this.is_literal) {
|
|
573
|
+
o.is_literal = true
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
return o
|
|
17
577
|
}
|
|
18
578
|
|
|
19
|
-
|
|
20
|
-
return
|
|
579
|
+
sync = async()=>{
|
|
580
|
+
return this
|
|
21
581
|
}
|
|
22
582
|
}
|
|
23
583
|
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
class Wallet{
|
|
2
|
+
constructor(instance){
|
|
3
|
+
this.instance = instance;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
transfer = async(price, recipient)=>{
|
|
7
|
+
let {vm} = this.instance.account
|
|
8
|
+
let folder = await this.instance._folder()
|
|
9
|
+
let wallet_folder = await folder.child('wallet')
|
|
10
|
+
let tok = await (await wallet_folder.readone({_id: price.token})).literal()
|
|
11
|
+
if (!tok) return {successful: false, message:"Doesn't have token"}
|
|
12
|
+
if(tok.value< price.value) return {successful: false, message:"Insufficient balance"}
|
|
13
|
+
|
|
14
|
+
await wallet_folder.update({_id: tok._id}, {value: {$dec: price.value}})
|
|
15
|
+
|
|
16
|
+
let transactions = await wallet_folder.child(price.token)
|
|
17
|
+
let recent_tx = await (await transactions.readone()).literal()
|
|
18
|
+
let tx = {value: price.value, previous: await vm.hash(recent_tx), recipient: recipient.ref(), timestamp: Date.now()}
|
|
19
|
+
tx.hash = await vm.hash(tx)
|
|
20
|
+
|
|
21
|
+
let res = await transactions.write(tx)
|
|
22
|
+
await wallet_folder.child('transactions').write(await res.to_address())
|
|
23
|
+
|
|
24
|
+
let recipient_folder = await recipient._folder()
|
|
25
|
+
let recipient_wallet = await recipient_folder.child('wallet'), sender_ref = `${res.ref()}:${tx.hash}`;
|
|
26
|
+
let rec_tok = await recipient_wallet.readone({_id: price.token})
|
|
27
|
+
let rec_txs = await recipient_wallet.child(price.token), rec_tx;
|
|
28
|
+
|
|
29
|
+
if(!rec_tok){
|
|
30
|
+
await recipient_wallet.write({_id: price.token, value: price.value})
|
|
31
|
+
rec_tx = {
|
|
32
|
+
value: price.value, previous: 'genesis', sender_ref
|
|
33
|
+
}
|
|
34
|
+
rec_tx.hash = await vm.hash(rec_tx)
|
|
35
|
+
await rec_txs.write(rec_tx)
|
|
36
|
+
}else {
|
|
37
|
+
await recipient_wallet.update({_id: price.token}, {value: {$inc: price.value}})
|
|
38
|
+
let rec_prev_tx = await (await rec_txs.readone()).literal()
|
|
39
|
+
rec_tx = {value: price.value, previous: await vm.hash(rec_prev_tx), sender_ref}
|
|
40
|
+
rec_tx.hash = await vm.hash(rec_tx)
|
|
41
|
+
|
|
42
|
+
await rec_txs.write(rec_tx)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return {successful: true, message: 'Transfer completed.'}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export default Wallet;
|