@trufnetwork/sdk-js 0.2.0 → 0.2.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.
Files changed (38) hide show
  1. package/dist/cjs/client/client.cjs +4 -2
  2. package/dist/cjs/client/client.cjs.map +2 -2
  3. package/dist/cjs/contracts/composed_stream_template_unix.json +2122 -0
  4. package/dist/cjs/contracts/contractsContent.cjs +13 -1
  5. package/dist/cjs/contracts/contractsContent.cjs.map +3 -3
  6. package/dist/cjs/contracts/primitive_stream_template_unix.json +1173 -0
  7. package/dist/cjs/contracts-api/composedStream.cjs.map +2 -2
  8. package/dist/cjs/contracts-api/deployStream.cjs +4 -4
  9. package/dist/cjs/contracts-api/deployStream.cjs.map +2 -2
  10. package/dist/cjs/contracts-api/primitiveStream.cjs.map +1 -1
  11. package/dist/cjs/contracts-api/stream.cjs +25 -0
  12. package/dist/cjs/contracts-api/stream.cjs.map +2 -2
  13. package/dist/esm/client/client.mjs +4 -2
  14. package/dist/esm/client/client.mjs.map +2 -2
  15. package/dist/esm/contracts/composed_stream_template_unix.json +2122 -0
  16. package/dist/esm/contracts/contractsContent.mjs +13 -1
  17. package/dist/esm/contracts/contractsContent.mjs.map +2 -2
  18. package/dist/esm/contracts/primitive_stream_template_unix.json +1173 -0
  19. package/dist/esm/contracts-api/composedStream.mjs.map +2 -2
  20. package/dist/esm/contracts-api/deployStream.mjs +7 -5
  21. package/dist/esm/contracts-api/deployStream.mjs.map +2 -2
  22. package/dist/esm/contracts-api/primitiveStream.mjs.map +1 -1
  23. package/dist/esm/contracts-api/stream.mjs +25 -0
  24. package/dist/esm/contracts-api/stream.mjs.map +2 -2
  25. package/dist/tsconfig.build.tsbuildinfo +1 -1
  26. package/dist/types/client/client.d.ts +2 -1
  27. package/dist/types/client/client.d.ts.map +1 -1
  28. package/dist/types/contracts/contractsContent.d.ts +3 -1
  29. package/dist/types/contracts/contractsContent.d.ts.map +1 -1
  30. package/dist/types/contracts-api/composedStream.d.ts +1 -1
  31. package/dist/types/contracts-api/composedStream.d.ts.map +1 -1
  32. package/dist/types/contracts-api/deployStream.d.ts +1 -0
  33. package/dist/types/contracts-api/deployStream.d.ts.map +1 -1
  34. package/dist/types/contracts-api/primitiveStream.d.ts +1 -1
  35. package/dist/types/contracts-api/primitiveStream.d.ts.map +1 -1
  36. package/dist/types/contracts-api/stream.d.ts +13 -6
  37. package/dist/types/contracts-api/stream.d.ts.map +1 -1
  38. package/package.json +9 -9
@@ -0,0 +1,1173 @@
1
+ {
2
+ "name": "primitive_stream_db_name",
3
+ "owner": "",
4
+ "extensions": null,
5
+ "tables": [
6
+ {
7
+ "name": "primitive_events",
8
+ "columns": [
9
+ {
10
+ "name": "date_value",
11
+ "type": {
12
+ "name": "int",
13
+ "is_array": false,
14
+ "metadata": [
15
+ 0,
16
+ 0
17
+ ]
18
+ },
19
+ "attributes": [
20
+ {
21
+ "type": "NOT_NULL",
22
+ "value": ""
23
+ }
24
+ ]
25
+ },
26
+ {
27
+ "name": "value",
28
+ "type": {
29
+ "name": "decimal",
30
+ "is_array": false,
31
+ "metadata": [
32
+ 36,
33
+ 18
34
+ ]
35
+ },
36
+ "attributes": [
37
+ {
38
+ "type": "NOT_NULL",
39
+ "value": ""
40
+ }
41
+ ]
42
+ },
43
+ {
44
+ "name": "created_at",
45
+ "type": {
46
+ "name": "int",
47
+ "is_array": false,
48
+ "metadata": [
49
+ 0,
50
+ 0
51
+ ]
52
+ },
53
+ "attributes": [
54
+ {
55
+ "type": "NOT_NULL",
56
+ "value": ""
57
+ }
58
+ ]
59
+ }
60
+ ],
61
+ "indexes": [
62
+ {
63
+ "name": "identifier_idx",
64
+ "columns": [
65
+ "date_value",
66
+ "created_at"
67
+ ],
68
+ "type": "PRIMARY"
69
+ }
70
+ ],
71
+ "foreign_keys": null
72
+ },
73
+ {
74
+ "name": "metadata",
75
+ "columns": [
76
+ {
77
+ "name": "row_id",
78
+ "type": {
79
+ "name": "uuid",
80
+ "is_array": false,
81
+ "metadata": [
82
+ 0,
83
+ 0
84
+ ]
85
+ },
86
+ "attributes": [
87
+ {
88
+ "type": "PRIMARY_KEY",
89
+ "value": ""
90
+ },
91
+ {
92
+ "type": "NOT_NULL",
93
+ "value": ""
94
+ }
95
+ ]
96
+ },
97
+ {
98
+ "name": "metadata_key",
99
+ "type": {
100
+ "name": "text",
101
+ "is_array": false,
102
+ "metadata": [
103
+ 0,
104
+ 0
105
+ ]
106
+ },
107
+ "attributes": [
108
+ {
109
+ "type": "NOT_NULL",
110
+ "value": ""
111
+ }
112
+ ]
113
+ },
114
+ {
115
+ "name": "value_i",
116
+ "type": {
117
+ "name": "int",
118
+ "is_array": false,
119
+ "metadata": [
120
+ 0,
121
+ 0
122
+ ]
123
+ },
124
+ "attributes": null
125
+ },
126
+ {
127
+ "name": "value_f",
128
+ "type": {
129
+ "name": "decimal",
130
+ "is_array": false,
131
+ "metadata": [
132
+ 36,
133
+ 18
134
+ ]
135
+ },
136
+ "attributes": null
137
+ },
138
+ {
139
+ "name": "value_b",
140
+ "type": {
141
+ "name": "bool",
142
+ "is_array": false,
143
+ "metadata": [
144
+ 0,
145
+ 0
146
+ ]
147
+ },
148
+ "attributes": null
149
+ },
150
+ {
151
+ "name": "value_s",
152
+ "type": {
153
+ "name": "text",
154
+ "is_array": false,
155
+ "metadata": [
156
+ 0,
157
+ 0
158
+ ]
159
+ },
160
+ "attributes": null
161
+ },
162
+ {
163
+ "name": "value_ref",
164
+ "type": {
165
+ "name": "text",
166
+ "is_array": false,
167
+ "metadata": [
168
+ 0,
169
+ 0
170
+ ]
171
+ },
172
+ "attributes": null
173
+ },
174
+ {
175
+ "name": "created_at",
176
+ "type": {
177
+ "name": "int",
178
+ "is_array": false,
179
+ "metadata": [
180
+ 0,
181
+ 0
182
+ ]
183
+ },
184
+ "attributes": [
185
+ {
186
+ "type": "NOT_NULL",
187
+ "value": ""
188
+ }
189
+ ]
190
+ },
191
+ {
192
+ "name": "disabled_at",
193
+ "type": {
194
+ "name": "int",
195
+ "is_array": false,
196
+ "metadata": [
197
+ 0,
198
+ 0
199
+ ]
200
+ },
201
+ "attributes": null
202
+ }
203
+ ],
204
+ "indexes": [
205
+ {
206
+ "name": "key_idx",
207
+ "columns": [
208
+ "metadata_key"
209
+ ],
210
+ "type": "BTREE"
211
+ },
212
+ {
213
+ "name": "ref_idx",
214
+ "columns": [
215
+ "value_ref"
216
+ ],
217
+ "type": "BTREE"
218
+ },
219
+ {
220
+ "name": "created_idx",
221
+ "columns": [
222
+ "created_at"
223
+ ],
224
+ "type": "BTREE"
225
+ }
226
+ ],
227
+ "foreign_keys": null
228
+ }
229
+ ],
230
+ "actions": null,
231
+ "procedures": [
232
+ {
233
+ "name": "is_initiated",
234
+ "parameters": null,
235
+ "public": false,
236
+ "modifiers": [
237
+ "VIEW"
238
+ ],
239
+ "body": "for $row in SELECT * FROM metadata WHERE metadata_key = 'type' LIMIT 1 {\n return true;\n }\n\n return false;",
240
+ "return_types": {
241
+ "is_table": false,
242
+ "fields": [
243
+ {
244
+ "name": "result",
245
+ "type": {
246
+ "name": "bool",
247
+ "is_array": false,
248
+ "metadata": [
249
+ 0,
250
+ 0
251
+ ]
252
+ }
253
+ }
254
+ ]
255
+ },
256
+ "annotations": null
257
+ },
258
+ {
259
+ "name": "is_stream_owner",
260
+ "parameters": [
261
+ {
262
+ "name": "$wallet",
263
+ "type": {
264
+ "name": "text",
265
+ "is_array": false,
266
+ "metadata": [
267
+ 0,
268
+ 0
269
+ ]
270
+ }
271
+ }
272
+ ],
273
+ "public": true,
274
+ "modifiers": [
275
+ "VIEW"
276
+ ],
277
+ "body": "for $row in SELECT * FROM metadata WHERE metadata_key = 'stream_owner' AND value_ref = LOWER($wallet) LIMIT 1 {\n return true;\n }\n return false;",
278
+ "return_types": {
279
+ "is_table": false,
280
+ "fields": [
281
+ {
282
+ "name": "result",
283
+ "type": {
284
+ "name": "bool",
285
+ "is_array": false,
286
+ "metadata": [
287
+ 0,
288
+ 0
289
+ ]
290
+ }
291
+ }
292
+ ]
293
+ },
294
+ "annotations": null
295
+ },
296
+ {
297
+ "name": "is_wallet_allowed_to_write",
298
+ "parameters": [
299
+ {
300
+ "name": "$wallet",
301
+ "type": {
302
+ "name": "text",
303
+ "is_array": false,
304
+ "metadata": [
305
+ 0,
306
+ 0
307
+ ]
308
+ }
309
+ }
310
+ ],
311
+ "public": true,
312
+ "modifiers": [
313
+ "VIEW"
314
+ ],
315
+ "body": "if is_stream_owner($wallet) {\n return true;\n }\n\n // if there's metadata allow_write_wallet -\u003e \u003cwallet\u003e, then its permitted\n for $row in SELECT * FROM get_metadata('allow_write_wallet', false, $wallet) {\n return true;\n }\n\n return false;",
316
+ "return_types": {
317
+ "is_table": false,
318
+ "fields": [
319
+ {
320
+ "name": "value",
321
+ "type": {
322
+ "name": "bool",
323
+ "is_array": false,
324
+ "metadata": [
325
+ 0,
326
+ 0
327
+ ]
328
+ }
329
+ }
330
+ ]
331
+ },
332
+ "annotations": null
333
+ },
334
+ {
335
+ "name": "is_wallet_allowed_to_read",
336
+ "parameters": [
337
+ {
338
+ "name": "$wallet",
339
+ "type": {
340
+ "name": "text",
341
+ "is_array": false,
342
+ "metadata": [
343
+ 0,
344
+ 0
345
+ ]
346
+ }
347
+ }
348
+ ],
349
+ "public": true,
350
+ "modifiers": [
351
+ "VIEW"
352
+ ],
353
+ "body": "$visibility int := 0;\n for $v_row in SELECT * FROM get_metadata('read_visibility', true, null) {\n $visibility := $v_row.value_i;\n }\n\n if $visibility == 0 {\n return true;\n }\n\n // if it's the owner, it's permitted\n if is_stream_owner($wallet) {\n return true;\n }\n\n // if there's metadata allow_read_wallet -\u003e \u003cwallet\u003e, then its permitted\n for $row in SELECT * FROM get_metadata('allow_read_wallet', false, $wallet) {\n return true;\n }\n\n return false;",
354
+ "return_types": {
355
+ "is_table": false,
356
+ "fields": [
357
+ {
358
+ "name": "value",
359
+ "type": {
360
+ "name": "bool",
361
+ "is_array": false,
362
+ "metadata": [
363
+ 0,
364
+ 0
365
+ ]
366
+ }
367
+ }
368
+ ]
369
+ },
370
+ "annotations": null
371
+ },
372
+ {
373
+ "name": "stream_owner_only",
374
+ "parameters": null,
375
+ "public": false,
376
+ "modifiers": [
377
+ "VIEW"
378
+ ],
379
+ "body": "if is_stream_owner(@caller) == false {\n error('Stream owner only procedure');\n }",
380
+ "return_types": null,
381
+ "annotations": null
382
+ },
383
+ {
384
+ "name": "init",
385
+ "parameters": null,
386
+ "public": true,
387
+ "modifiers": [
388
+ "OWNER"
389
+ ],
390
+ "body": "if is_initiated() {\n error('this contract was already initialized');\n }\n\n // check if caller is empty\n // this happens can happen in tests, but we should also protect for that on production\n if @caller == '' {\n error('caller is empty');\n }\n\n $current_block int := @height;\n\n // uuid's namespaces are any random generated uuid from https://www.uuidtools.com/v5\n // but each usage should be different to maintain determinism, so we reuse the previous result\n $current_uuid uuid := uuid_generate_v5('111bfa42-17a2-11ef-bf03-325096b39f47'::uuid, @txid);\n\n // type = primitive\n $current_uuid := uuid_generate_v5($current_uuid, @txid);\n INSERT INTO metadata (row_id, metadata_key, value_s, created_at)\n VALUES ($current_uuid, 'type', 'primitive', $current_block);\n\n // stream_owner = @caller\n $current_uuid := uuid_generate_v5($current_uuid, @txid);\n INSERT INTO metadata (row_id, metadata_key, value_ref, created_at)\n VALUES ($current_uuid, 'stream_owner', LOWER(@caller), 1);\n\n // compose_visibility = 0 (public)\n $current_uuid := uuid_generate_v5($current_uuid, @txid);\n INSERT INTO metadata (row_id, metadata_key, value_i, created_at)\n VALUES ($current_uuid, 'compose_visibility', 0, $current_block);\n\n // read_visibility = 0 (public)\n $current_uuid := uuid_generate_v5($current_uuid, @txid);\n INSERT INTO metadata (row_id, metadata_key, value_i, created_at)\n VALUES ($current_uuid, 'read_visibility', 0, $current_block);\n\n $readonly_keys text[] := [\n 'type',\n 'stream_owner',\n 'readonly_key'\n ];\n\n for $key in $readonly_keys {\n $current_uuid := uuid_generate_v5($current_uuid, @txid);\n INSERT INTO metadata (row_id, metadata_key, value_s, created_at)\n VALUES ($current_uuid, 'readonly_key', $key, $current_block);\n }",
391
+ "return_types": null,
392
+ "annotations": null
393
+ },
394
+ {
395
+ "name": "insert_metadata",
396
+ "parameters": [
397
+ {
398
+ "name": "$key",
399
+ "type": {
400
+ "name": "text",
401
+ "is_array": false,
402
+ "metadata": [
403
+ 0,
404
+ 0
405
+ ]
406
+ }
407
+ },
408
+ {
409
+ "name": "$value",
410
+ "type": {
411
+ "name": "text",
412
+ "is_array": false,
413
+ "metadata": [
414
+ 0,
415
+ 0
416
+ ]
417
+ }
418
+ },
419
+ {
420
+ "name": "$val_type",
421
+ "type": {
422
+ "name": "text",
423
+ "is_array": false,
424
+ "metadata": [
425
+ 0,
426
+ 0
427
+ ]
428
+ }
429
+ }
430
+ ],
431
+ "public": true,
432
+ "modifiers": null,
433
+ "body": "$value_i int;\n $value_s text;\n $value_f decimal(36,18);\n $value_b bool;\n $value_ref text;\n\n if $val_type == 'int' {\n $value_i := $value::int;\n } elseif $val_type == 'string' {\n $value_s := $value;\n } elseif $val_type == 'bool' {\n $value_b := $value::bool;\n } elseif $val_type == 'ref' {\n $value_ref := $value;\n } elseif $val_type == 'float' {\n $value_f := $value::decimal(36,18);\n } else {\n error(format('unknown type used \"%s\". valid types = \"float\" | \"bool\" | \"int\" | \"ref\" | \"string\"', $val_type));\n }\n\n stream_owner_only();\n\n if is_initiated() == false {\n error('contract must be initiated');\n }\n\n // check if it's read-only\n for $row in SELECT * FROM metadata WHERE metadata_key = 'readonly_key' AND value_s = $key LIMIT 1 {\n error('Cannot insert metadata for read-only key');\n }\n\n // we create one deterministic uuid for each metadata record\n // we can't use just @txid because a single transaction can insert multiple metadata records.\n // the result will be idempotency here too.\n $uuid_key := @txid || $key || $value;\n\n $uuid uuid := uuid_generate_v5('1361df5d-0230-47b3-b2c1-37950cf51fe9'::uuid, $uuid_key);\n $current_block int := @height;\n\n // insert data\n INSERT INTO metadata (row_id, metadata_key, value_i, value_f, value_s, value_b, value_ref, created_at)\n VALUES ($uuid, $key, $value_i, $value_f, $value_s, $value_b, LOWER($value_ref), $current_block);",
434
+ "return_types": null,
435
+ "annotations": null
436
+ },
437
+ {
438
+ "name": "get_metadata",
439
+ "parameters": [
440
+ {
441
+ "name": "$key",
442
+ "type": {
443
+ "name": "text",
444
+ "is_array": false,
445
+ "metadata": [
446
+ 0,
447
+ 0
448
+ ]
449
+ }
450
+ },
451
+ {
452
+ "name": "$only_latest",
453
+ "type": {
454
+ "name": "bool",
455
+ "is_array": false,
456
+ "metadata": [
457
+ 0,
458
+ 0
459
+ ]
460
+ }
461
+ },
462
+ {
463
+ "name": "$ref",
464
+ "type": {
465
+ "name": "text",
466
+ "is_array": false,
467
+ "metadata": [
468
+ 0,
469
+ 0
470
+ ]
471
+ }
472
+ }
473
+ ],
474
+ "public": true,
475
+ "modifiers": [
476
+ "VIEW"
477
+ ],
478
+ "body": "if $only_latest == true {\n if $ref is distinct from null {\n return SELECT\n row_id,\n null::int as value_i,\n null::decimal(36,18) as value_f,\n null::bool as value_b,\n null::text as value_s,\n value_ref,\n created_at\n FROM metadata\n WHERE metadata_key = $key AND disabled_at IS NULL AND value_ref = LOWER($ref)\n ORDER BY created_at DESC\n LIMIT 1;\n } else {\n return SELECT\n row_id,\n value_i,\n value_f,\n value_b,\n value_s,\n value_ref,\n created_at\n FROM metadata\n WHERE metadata_key = $key AND disabled_at IS NULL\n ORDER BY created_at DESC\n LIMIT 1;\n }\n } else {\n // SHOULD BE THE EXACT CODE AS ABOVE, BUT WITHOUT LIMIT\n if $ref is distinct from null {\n return SELECT\n row_id,\n null::int as value_i,\n null::decimal(36,18) as value_f,\n null::bool as value_b,\n null::text as value_s,\n value_ref,\n created_at\n FROM metadata\n WHERE metadata_key = $key AND disabled_at IS NULL AND value_ref = LOWER($ref)\n ORDER BY created_at DESC;\n } else {\n return SELECT\n row_id,\n value_i,\n value_f,\n value_b,\n value_s,\n value_ref,\n created_at\n FROM metadata\n WHERE metadata_key = $key AND disabled_at IS NULL\n ORDER BY created_at DESC;\n }\n }",
479
+ "return_types": {
480
+ "is_table": true,
481
+ "fields": [
482
+ {
483
+ "name": "row_id",
484
+ "type": {
485
+ "name": "uuid",
486
+ "is_array": false,
487
+ "metadata": [
488
+ 0,
489
+ 0
490
+ ]
491
+ }
492
+ },
493
+ {
494
+ "name": "value_i",
495
+ "type": {
496
+ "name": "int",
497
+ "is_array": false,
498
+ "metadata": [
499
+ 0,
500
+ 0
501
+ ]
502
+ }
503
+ },
504
+ {
505
+ "name": "value_f",
506
+ "type": {
507
+ "name": "decimal",
508
+ "is_array": false,
509
+ "metadata": [
510
+ 36,
511
+ 18
512
+ ]
513
+ }
514
+ },
515
+ {
516
+ "name": "value_b",
517
+ "type": {
518
+ "name": "bool",
519
+ "is_array": false,
520
+ "metadata": [
521
+ 0,
522
+ 0
523
+ ]
524
+ }
525
+ },
526
+ {
527
+ "name": "value_s",
528
+ "type": {
529
+ "name": "text",
530
+ "is_array": false,
531
+ "metadata": [
532
+ 0,
533
+ 0
534
+ ]
535
+ }
536
+ },
537
+ {
538
+ "name": "value_ref",
539
+ "type": {
540
+ "name": "text",
541
+ "is_array": false,
542
+ "metadata": [
543
+ 0,
544
+ 0
545
+ ]
546
+ }
547
+ },
548
+ {
549
+ "name": "created_at",
550
+ "type": {
551
+ "name": "int",
552
+ "is_array": false,
553
+ "metadata": [
554
+ 0,
555
+ 0
556
+ ]
557
+ }
558
+ }
559
+ ]
560
+ },
561
+ "annotations": null
562
+ },
563
+ {
564
+ "name": "disable_metadata",
565
+ "parameters": [
566
+ {
567
+ "name": "$row_id",
568
+ "type": {
569
+ "name": "uuid",
570
+ "is_array": false,
571
+ "metadata": [
572
+ 0,
573
+ 0
574
+ ]
575
+ }
576
+ }
577
+ ],
578
+ "public": true,
579
+ "modifiers": null,
580
+ "body": "stream_owner_only();\n\n $current_block int := @height;\n\n $found bool := false;\n\n // Check if the metadata is not read-only\n for $metadata_row in\n SELECT metadata_key\n FROM metadata\n WHERE row_id = $row_id AND disabled_at IS NULL\n LIMIT 1 {\n $found := true;\n $row_key text := $metadata_row.metadata_key;\n\n for $readonly_row in SELECT row_id FROM metadata WHERE metadata_key = 'readonly_key' AND value_s = $row_key LIMIT 1 {\n error('Cannot disable read-only metadata');\n }\n\n UPDATE metadata SET disabled_at = $current_block\n WHERE row_id = $row_id;\n }\n\n if $found == false {\n error('metadata record not found');\n }",
581
+ "return_types": null,
582
+ "annotations": null
583
+ },
584
+ {
585
+ "name": "insert_record",
586
+ "parameters": [
587
+ {
588
+ "name": "$date_value",
589
+ "type": {
590
+ "name": "int",
591
+ "is_array": false,
592
+ "metadata": [
593
+ 0,
594
+ 0
595
+ ]
596
+ }
597
+ },
598
+ {
599
+ "name": "$value",
600
+ "type": {
601
+ "name": "decimal",
602
+ "is_array": false,
603
+ "metadata": [
604
+ 36,
605
+ 18
606
+ ]
607
+ }
608
+ }
609
+ ],
610
+ "public": true,
611
+ "modifiers": null,
612
+ "body": "if is_wallet_allowed_to_write(@caller) == false {\n error('wallet not allowed to write');\n }\n\n if is_initiated() == false {\n error('contract must be initiated');\n }\n\n $current_block int := @height;\n\n // insert data\n INSERT INTO primitive_events (date_value, value, created_at)\n VALUES ($date_value, $value, $current_block);",
613
+ "return_types": null,
614
+ "annotations": null
615
+ },
616
+ {
617
+ "name": "get_index",
618
+ "parameters": [
619
+ {
620
+ "name": "$date_from",
621
+ "type": {
622
+ "name": "int",
623
+ "is_array": false,
624
+ "metadata": [
625
+ 0,
626
+ 0
627
+ ]
628
+ }
629
+ },
630
+ {
631
+ "name": "$date_to",
632
+ "type": {
633
+ "name": "int",
634
+ "is_array": false,
635
+ "metadata": [
636
+ 0,
637
+ 0
638
+ ]
639
+ }
640
+ },
641
+ {
642
+ "name": "$frozen_at",
643
+ "type": {
644
+ "name": "int",
645
+ "is_array": false,
646
+ "metadata": [
647
+ 0,
648
+ 0
649
+ ]
650
+ }
651
+ },
652
+ {
653
+ "name": "$base_date",
654
+ "type": {
655
+ "name": "int",
656
+ "is_array": false,
657
+ "metadata": [
658
+ 0,
659
+ 0
660
+ ]
661
+ }
662
+ }
663
+ ],
664
+ "public": true,
665
+ "modifiers": [
666
+ "VIEW"
667
+ ],
668
+ "body": "$effective_base_date int := $base_date;\n if ($effective_base_date == 0 OR $effective_base_date IS NULL) {\n for $v_row in SELECT * FROM get_metadata('default_base_date', true, null) ORDER BY created_at DESC LIMIT 1 {\n $effective_base_date := $v_row.value_i;\n }\n }\n\n $baseValue decimal(36,18) := get_base_value($effective_base_date, $frozen_at);\n if $baseValue == 0::decimal(36,18) {\n error('base value is 0');\n }\n\n return SELECT date_value, (value * 100::decimal(36,18)) / $baseValue as value FROM get_record($date_from, $date_to, $frozen_at);",
669
+ "return_types": {
670
+ "is_table": true,
671
+ "fields": [
672
+ {
673
+ "name": "date_value",
674
+ "type": {
675
+ "name": "int",
676
+ "is_array": false,
677
+ "metadata": [
678
+ 0,
679
+ 0
680
+ ]
681
+ }
682
+ },
683
+ {
684
+ "name": "value",
685
+ "type": {
686
+ "name": "decimal",
687
+ "is_array": false,
688
+ "metadata": [
689
+ 36,
690
+ 18
691
+ ]
692
+ }
693
+ }
694
+ ]
695
+ },
696
+ "annotations": null
697
+ },
698
+ {
699
+ "name": "get_base_value",
700
+ "parameters": [
701
+ {
702
+ "name": "$base_date",
703
+ "type": {
704
+ "name": "int",
705
+ "is_array": false,
706
+ "metadata": [
707
+ 0,
708
+ 0
709
+ ]
710
+ }
711
+ },
712
+ {
713
+ "name": "$frozen_at",
714
+ "type": {
715
+ "name": "int",
716
+ "is_array": false,
717
+ "metadata": [
718
+ 0,
719
+ 0
720
+ ]
721
+ }
722
+ }
723
+ ],
724
+ "public": false,
725
+ "modifiers": [
726
+ "VIEW"
727
+ ],
728
+ "body": "if $base_date is null OR $base_date = 0 {\n for $row in SELECT * FROM primitive_events WHERE (created_at \u003c= $frozen_at OR $frozen_at = 0 OR $frozen_at IS NULL) ORDER BY date_value ASC, created_at DESC LIMIT 1 {\n return $row.value;\n }\n }\n\n for $row2 in SELECT * FROM primitive_events WHERE date_value \u003c= $base_date AND (created_at \u003c= $frozen_at OR $frozen_at = 0 OR $frozen_at IS NULL) ORDER BY date_value DESC, created_at DESC LIMIT 1 {\n return $row2.value;\n }\n\n // if no value is found, we find the first value after the given date\n // This will raise a red flag in the system and the data will undergo the usual process for when a new data provider is added.\n for $row3 in SELECT * FROM primitive_events WHERE date_value \u003e $base_date AND (created_at \u003c= $frozen_at OR $frozen_at = 0 OR $frozen_at IS NULL) ORDER BY date_value ASC, created_at DESC LIMIT 1 {\n return $row3.value;\n }\n\n // if no value is found, we return an error\n error('no base value found');",
729
+ "return_types": {
730
+ "is_table": false,
731
+ "fields": [
732
+ {
733
+ "name": "value",
734
+ "type": {
735
+ "name": "decimal",
736
+ "is_array": false,
737
+ "metadata": [
738
+ 36,
739
+ 18
740
+ ]
741
+ }
742
+ }
743
+ ]
744
+ },
745
+ "annotations": null
746
+ },
747
+ {
748
+ "name": "get_first_record",
749
+ "parameters": [
750
+ {
751
+ "name": "$after_date",
752
+ "type": {
753
+ "name": "int",
754
+ "is_array": false,
755
+ "metadata": [
756
+ 0,
757
+ 0
758
+ ]
759
+ }
760
+ },
761
+ {
762
+ "name": "$frozen_at",
763
+ "type": {
764
+ "name": "int",
765
+ "is_array": false,
766
+ "metadata": [
767
+ 0,
768
+ 0
769
+ ]
770
+ }
771
+ }
772
+ ],
773
+ "public": true,
774
+ "modifiers": [
775
+ "VIEW"
776
+ ],
777
+ "body": "if is_wallet_allowed_to_read(@caller) == false {\n error('wallet not allowed to read');\n }\n\n // check compose access\n is_stream_allowed_to_compose(@foreign_caller);\n\n // let's coalesce after_date with ''\n // then, if it's empty, it will always be the first value\n if $after_date is null {\n $after_date := 0;\n }\n\n // coalesce frozen_at with 0\n if $frozen_at is null {\n $frozen_at := 0;\n }\n\n return SELECT date_value, value FROM primitive_events WHERE date_value \u003e= $after_date AND (created_at \u003c= $frozen_at OR $frozen_at = 0 OR $frozen_at IS NULL) ORDER BY date_value ASC, created_at DESC LIMIT 1;",
778
+ "return_types": {
779
+ "is_table": true,
780
+ "fields": [
781
+ {
782
+ "name": "date_value",
783
+ "type": {
784
+ "name": "int",
785
+ "is_array": false,
786
+ "metadata": [
787
+ 0,
788
+ 0
789
+ ]
790
+ }
791
+ },
792
+ {
793
+ "name": "value",
794
+ "type": {
795
+ "name": "decimal",
796
+ "is_array": false,
797
+ "metadata": [
798
+ 36,
799
+ 18
800
+ ]
801
+ }
802
+ }
803
+ ]
804
+ },
805
+ "annotations": null
806
+ },
807
+ {
808
+ "name": "get_original_record",
809
+ "parameters": [
810
+ {
811
+ "name": "$date_from",
812
+ "type": {
813
+ "name": "int",
814
+ "is_array": false,
815
+ "metadata": [
816
+ 0,
817
+ 0
818
+ ]
819
+ }
820
+ },
821
+ {
822
+ "name": "$date_to",
823
+ "type": {
824
+ "name": "int",
825
+ "is_array": false,
826
+ "metadata": [
827
+ 0,
828
+ 0
829
+ ]
830
+ }
831
+ },
832
+ {
833
+ "name": "$frozen_at",
834
+ "type": {
835
+ "name": "int",
836
+ "is_array": false,
837
+ "metadata": [
838
+ 0,
839
+ 0
840
+ ]
841
+ }
842
+ }
843
+ ],
844
+ "public": false,
845
+ "modifiers": [
846
+ "VIEW"
847
+ ],
848
+ "body": "if is_wallet_allowed_to_read(@caller) == false {\n error('wallet not allowed to read');\n }\n // check compose access\n is_stream_allowed_to_compose(@foreign_caller);\n\n $frozenValue int := 0;\n if $frozen_at IS DISTINCT FROM NULL {\n $frozenValue := $frozen_at::int;\n }\n\n // TODO: whereClause here is a placeholder only, not supported yet, but it will make things cleaner if it available\n //$whereClause text := 'WHERE 1=1 ';\n //if $date_from != '' {\n // $whereClause := $whereClause || 'AND date_value \u003e= $date_from ';\n //}\n\n //if $date_to != '' {\n // $whereClause := $whereClause || 'AND date_value \u003c= $date_to ';\n //}\n\n\n // TODO: Normally we would use the following query to get the latest value of each date\n // But it's not working for JOIN and MAX() function\n //for $row in SELECT date_value, value FROM primitive_events JOIN (SELECT date_value, MAX(created_at) as created_at FROM primitive_events GROUP BY date_value) as max_created\n //ON primitive_events.date_value = max_created.date_value AND primitive_events.created_at = max_created.created_at\n //$whereClause\n //ORDER BY date_value DESC {\n // return next $row.date_value, $row.value;\n //}\n\n // TODO: had to use this workaround because \u0026\u0026 operator is not working\n $last_result_date int := 0;\n if $date_from IS DISTINCT FROM NULL {\n if $date_to IS DISTINCT FROM NULL {\n // date_from and date_to are provided\n // we will fetch all records from date_from to date_to\n for $row in SELECT date_value, value FROM primitive_events\n WHERE date_value \u003e= $date_from AND date_value \u003c= $date_to\n AND (created_at \u003c= $frozenValue OR $frozenValue = 0)\n AND $last_result_date != date_value\n ORDER BY date_value DESC, created_at DESC {\n if $last_result_date != $row.date_value {\n $last_result_date := $row.date_value;\n return next $row.date_value, $row.value;\n }\n }\n } else {\n // only date_from is provided\n // we will fetch all records from date_from to the latest\n for $row2 in SELECT date_value, value FROM primitive_events\n WHERE date_value \u003e= $date_from\n AND (created_at \u003c= $frozenValue OR $frozenValue = 0)\n AND $last_result_date != date_value\n ORDER BY date_value DESC, created_at DESC {\n if $last_result_date != $row2.date_value {\n $last_result_date := $row2.date_value;\n return next $row2.date_value, $row2.value;\n }\n }\n }\n } else {\n if $date_to IS NOT DISTINCT FROM NULL {\n // no date_from and date_to provided\n // we fetch only the latest record\n return SELECT date_value, value FROM primitive_events\n WHERE created_at \u003c= $frozenValue OR $frozenValue = 0\n AND $last_result_date != date_value\n ORDER BY date_value DESC, created_at DESC LIMIT 1;\n } else {\n // date_to is provided but date_from is not\n error('date_from is required if date_to is provided');\n }\n }",
849
+ "return_types": {
850
+ "is_table": true,
851
+ "fields": [
852
+ {
853
+ "name": "date_value",
854
+ "type": {
855
+ "name": "int",
856
+ "is_array": false,
857
+ "metadata": [
858
+ 0,
859
+ 0
860
+ ]
861
+ }
862
+ },
863
+ {
864
+ "name": "value",
865
+ "type": {
866
+ "name": "decimal",
867
+ "is_array": false,
868
+ "metadata": [
869
+ 36,
870
+ 18
871
+ ]
872
+ }
873
+ }
874
+ ]
875
+ },
876
+ "annotations": null
877
+ },
878
+ {
879
+ "name": "get_record",
880
+ "parameters": [
881
+ {
882
+ "name": "$date_from",
883
+ "type": {
884
+ "name": "int",
885
+ "is_array": false,
886
+ "metadata": [
887
+ 0,
888
+ 0
889
+ ]
890
+ }
891
+ },
892
+ {
893
+ "name": "$date_to",
894
+ "type": {
895
+ "name": "int",
896
+ "is_array": false,
897
+ "metadata": [
898
+ 0,
899
+ 0
900
+ ]
901
+ }
902
+ },
903
+ {
904
+ "name": "$frozen_at",
905
+ "type": {
906
+ "name": "int",
907
+ "is_array": false,
908
+ "metadata": [
909
+ 0,
910
+ 0
911
+ ]
912
+ }
913
+ }
914
+ ],
915
+ "public": true,
916
+ "modifiers": [
917
+ "VIEW"
918
+ ],
919
+ "body": "$is_first_result bool := true;\n\n for $row in SELECT * FROM get_original_record($date_from, $date_to, $frozen_at) {\n // we will only fetch the last record before the first result\n // if the first result is not the same as the start date\n if $is_first_result == true {\n $first_result_date int := $row.date_value;\n\n // if the first result date is not the same as the start date, then we need to fetch the last record before it\n if $first_result_date != $date_from {\n for $last_row in SELECT * FROM get_last_record_before_date($first_result_date) {\n // Note: although the user requested a date_from, we are returning the previous date here\n // e.g., the user used date_from:2021-01-02, and we are returning 2021-01-01 as first value\n //\n // that happens because the accuracy is guaranteed with this behavior, otherwise the\n // user won't be able to know when a data point really exist in our database or not.\n\n return next $last_row.date_value, $last_row.value;\n }\n }\n\n $is_first_result := false;\n }\n\n return next $row.date_value, $row.value;\n }\n\n // it's still the first result? i.e. there were no results\n // so let's try finding the last record before the start date\n if $is_first_result == true {\n for $last_row2 in SELECT * FROM get_last_record_before_date($date_from) {\n return next $last_row2.date_value, $last_row2.value;\n }\n }",
920
+ "return_types": {
921
+ "is_table": true,
922
+ "fields": [
923
+ {
924
+ "name": "date_value",
925
+ "type": {
926
+ "name": "int",
927
+ "is_array": false,
928
+ "metadata": [
929
+ 0,
930
+ 0
931
+ ]
932
+ }
933
+ },
934
+ {
935
+ "name": "value",
936
+ "type": {
937
+ "name": "decimal",
938
+ "is_array": false,
939
+ "metadata": [
940
+ 36,
941
+ 18
942
+ ]
943
+ }
944
+ }
945
+ ]
946
+ },
947
+ "annotations": null
948
+ },
949
+ {
950
+ "name": "get_last_record_before_date",
951
+ "parameters": [
952
+ {
953
+ "name": "$date_from",
954
+ "type": {
955
+ "name": "int",
956
+ "is_array": false,
957
+ "metadata": [
958
+ 0,
959
+ 0
960
+ ]
961
+ }
962
+ }
963
+ ],
964
+ "public": true,
965
+ "modifiers": [
966
+ "VIEW"
967
+ ],
968
+ "body": "return SELECT date_value, value FROM primitive_events WHERE date_value \u003c $date_from ORDER BY date_value DESC, created_at DESC LIMIT 1;",
969
+ "return_types": {
970
+ "is_table": true,
971
+ "fields": [
972
+ {
973
+ "name": "date_value",
974
+ "type": {
975
+ "name": "int",
976
+ "is_array": false,
977
+ "metadata": [
978
+ 0,
979
+ 0
980
+ ]
981
+ }
982
+ },
983
+ {
984
+ "name": "value",
985
+ "type": {
986
+ "name": "decimal",
987
+ "is_array": false,
988
+ "metadata": [
989
+ 36,
990
+ 18
991
+ ]
992
+ }
993
+ }
994
+ ]
995
+ },
996
+ "annotations": null
997
+ },
998
+ {
999
+ "name": "transfer_stream_ownership",
1000
+ "parameters": [
1001
+ {
1002
+ "name": "$new_owner",
1003
+ "type": {
1004
+ "name": "text",
1005
+ "is_array": false,
1006
+ "metadata": [
1007
+ 0,
1008
+ 0
1009
+ ]
1010
+ }
1011
+ }
1012
+ ],
1013
+ "public": true,
1014
+ "modifiers": null,
1015
+ "body": "stream_owner_only();\n\n // fail if not a valid address\n check_eth_address($new_owner);\n\n UPDATE metadata SET value_ref = LOWER($new_owner)\n WHERE metadata_key = 'stream_owner';",
1016
+ "return_types": null,
1017
+ "annotations": null
1018
+ },
1019
+ {
1020
+ "name": "check_eth_address",
1021
+ "parameters": [
1022
+ {
1023
+ "name": "$address",
1024
+ "type": {
1025
+ "name": "text",
1026
+ "is_array": false,
1027
+ "metadata": [
1028
+ 0,
1029
+ 0
1030
+ ]
1031
+ }
1032
+ }
1033
+ ],
1034
+ "public": false,
1035
+ "modifiers": null,
1036
+ "body": "if (length($address) != 42) {\n error('invalid address length');\n }\n\n // check if starts with 0x\n for $row in SELECT $address LIKE '0x%' as a {\n if $row.a == false {\n error('address does not start with 0x');\n }\n }",
1037
+ "return_types": null,
1038
+ "annotations": null
1039
+ },
1040
+ {
1041
+ "name": "is_stream_allowed_to_compose",
1042
+ "parameters": [
1043
+ {
1044
+ "name": "$foreign_caller",
1045
+ "type": {
1046
+ "name": "text",
1047
+ "is_array": false,
1048
+ "metadata": [
1049
+ 0,
1050
+ 0
1051
+ ]
1052
+ }
1053
+ }
1054
+ ],
1055
+ "public": true,
1056
+ "modifiers": [
1057
+ "VIEW"
1058
+ ],
1059
+ "body": "if $foreign_caller == '' {\n return true;\n }\n\n // if public, anyone can always read\n // If there's no visibility metadata, it's public.\n $visibility int := 0;\n for $v_row in SELECT * FROM get_metadata('compose_visibility', true, null) {\n $visibility := $v_row.value_i;\n }\n\n if $visibility == 0 {\n return true;\n }\n\n // if there's metadata allow_compose_stream -\u003e \u003cforeign_caller\u003e, then its permitted\n for $row in SELECT * FROM get_metadata('allow_compose_stream', true, $foreign_caller) LIMIT 1 {\n return true;\n }\n\n error('stream not allowed to compose');",
1060
+ "return_types": {
1061
+ "is_table": false,
1062
+ "fields": [
1063
+ {
1064
+ "name": "value",
1065
+ "type": {
1066
+ "name": "bool",
1067
+ "is_array": false,
1068
+ "metadata": [
1069
+ 0,
1070
+ 0
1071
+ ]
1072
+ }
1073
+ }
1074
+ ]
1075
+ },
1076
+ "annotations": null
1077
+ },
1078
+ {
1079
+ "name": "get_index_change",
1080
+ "parameters": [
1081
+ {
1082
+ "name": "$date_from",
1083
+ "type": {
1084
+ "name": "int",
1085
+ "is_array": false,
1086
+ "metadata": [
1087
+ 0,
1088
+ 0
1089
+ ]
1090
+ }
1091
+ },
1092
+ {
1093
+ "name": "$date_to",
1094
+ "type": {
1095
+ "name": "int",
1096
+ "is_array": false,
1097
+ "metadata": [
1098
+ 0,
1099
+ 0
1100
+ ]
1101
+ }
1102
+ },
1103
+ {
1104
+ "name": "$frozen_at",
1105
+ "type": {
1106
+ "name": "int",
1107
+ "is_array": false,
1108
+ "metadata": [
1109
+ 0,
1110
+ 0
1111
+ ]
1112
+ }
1113
+ },
1114
+ {
1115
+ "name": "$base_date",
1116
+ "type": {
1117
+ "name": "int",
1118
+ "is_array": false,
1119
+ "metadata": [
1120
+ 0,
1121
+ 0
1122
+ ]
1123
+ }
1124
+ },
1125
+ {
1126
+ "name": "$days_interval",
1127
+ "type": {
1128
+ "name": "int",
1129
+ "is_array": false,
1130
+ "metadata": [
1131
+ 0,
1132
+ 0
1133
+ ]
1134
+ }
1135
+ }
1136
+ ],
1137
+ "public": true,
1138
+ "modifiers": [
1139
+ "VIEW"
1140
+ ],
1141
+ "body": "if $frozen_at == null {\n $frozen_at := 0;\n }\n\n if $days_interval == null {\n error('days_interval is required');\n }\n\n $current_values decimal(36,18)[];\n // example: [01-2001, 05-2001, 09-2001, 10-2001]\n $current_dates int[];\n // example: [01-2000, 05-2000, 09-2000, 10-2000]\n $expected_prev_dates int[];\n\n for $row_current in SELECT * FROM get_index($date_from, $date_to, $frozen_at, $base_date) {\n $prev_date := $row_current.date_value - ($days_interval * 86400);\n $expected_prev_dates := array_append($expected_prev_dates, $prev_date);\n $current_values := array_append($current_values, $row_current.value);\n $current_dates := array_append($current_dates, $row_current.date_value);\n }\n\n // example: 01-2000]\n $earliest_prev_date := $expected_prev_dates[1];\n // example: 09-2000\n $latest_prev_date := $expected_prev_dates[array_length($expected_prev_dates)];\n\n // real previous values doesn't match the same length as expected previous dates\n // because the interval can have much more values than the expected dates\n $real_prev_values decimal(36,18)[];\n $real_prev_dates int[];\n\n // now we query the prev dates\n for $row_prev in SELECT * FROM get_index($earliest_prev_date, $latest_prev_date, $frozen_at, $base_date) {\n $real_prev_values := array_append($real_prev_values, $row_prev.value);\n $real_prev_dates := array_append($real_prev_dates, $row_prev.date_value);\n }\n\n // now we calculate the matching dates for the real prev values\n $result_prev_dates int[];\n $result_prev_values decimal(36,18)[];\n\n $real_prev_date_idx int := 1;\n\n // for each expected prev date, we find the matching real prev date\n if array_length($expected_prev_dates) \u003e 0 {\n for $expected_prev_date_idx in 1..array_length($expected_prev_dates) {\n // we start from the last index of real prev dates. we don't need to check previous values\n for $selector in $real_prev_date_idx..array_length($real_prev_dates) {\n // if next real prev date is greater than expected prev date (or null), then we need to use the current real value\n if $real_prev_dates[$selector + 1] \u003e $expected_prev_dates[$expected_prev_date_idx]\n OR $real_prev_dates[$selector + 1] IS NULL {\n // if the current real prev date is already greater than expected prev date\n // we use NULL. We're probably before the first real prev date here\n if $real_prev_dates[$selector] \u003e $expected_prev_dates[$expected_prev_date_idx] {\n $result_prev_dates := array_append($result_prev_dates, null::int);\n $result_prev_values := array_append($result_prev_values, null::decimal(36,18));\n } else {\n $result_prev_dates := array_append($result_prev_dates, $real_prev_dates[$selector]);\n $result_prev_values := array_append($result_prev_values, $real_prev_values[$selector]);\n }\n // we already appended one for current $real_prev_date_idx, then we need to go to next\n $real_prev_date_idx := $selector;\n break;\n }\n }\n }\n }\n\n // check if we have the same number of values and dates\n if array_length($current_dates) != array_length($result_prev_dates) {\n error('we have different number of dates and values');\n }\n if array_length($current_values) != array_length($result_prev_values) {\n error('we have different number of dates and values');\n }\n\n // calculate the index change\n if array_length($result_prev_dates) \u003e 0 {\n for $row_result in 1..array_length($result_prev_dates) {\n // if the expected_prev_date is null, then we don't have a real prev date\n if $result_prev_dates[$row_result] IS DISTINCT FROM NULL {\n return next $current_dates[$row_result], ($current_values[$row_result] - $result_prev_values[$row_result]) * 100.00::decimal(36,18) / $result_prev_values[$row_result];\n }\n }\n }",
1142
+ "return_types": {
1143
+ "is_table": true,
1144
+ "fields": [
1145
+ {
1146
+ "name": "date_value",
1147
+ "type": {
1148
+ "name": "int",
1149
+ "is_array": false,
1150
+ "metadata": [
1151
+ 0,
1152
+ 0
1153
+ ]
1154
+ }
1155
+ },
1156
+ {
1157
+ "name": "value",
1158
+ "type": {
1159
+ "name": "decimal",
1160
+ "is_array": false,
1161
+ "metadata": [
1162
+ 36,
1163
+ 18
1164
+ ]
1165
+ }
1166
+ }
1167
+ ]
1168
+ },
1169
+ "annotations": null
1170
+ }
1171
+ ],
1172
+ "foreign_calls": null
1173
+ }