wao 0.4.0 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/esm/tgql.js ADDED
@@ -0,0 +1,283 @@
1
+ import { reverse, includes, map, is, isNil, last, clone, pick } from "ramda"
2
+
3
+ const subs = {
4
+ owner: ["address", "key"],
5
+ fee: ["winston", "ar"],
6
+ quantity: ["winston", "ar"],
7
+ data: ["size", "type"],
8
+ tags: ["name", "value"],
9
+ block: ["id", "timestamp", "height", "previous"],
10
+ parent: ["id"],
11
+ }
12
+ const field = (key, val = true) => {
13
+ if (includes(key, ["id", "anchor", "signature", "recipient"])) {
14
+ return { key }
15
+ } else if (subs[key]) {
16
+ let _subs = []
17
+ if (val === true) val = subs[key]
18
+ else if (is(Object, val) && !is(Array, val)) {
19
+ let _val = []
20
+ let isTrue = false
21
+ let isFalse = false
22
+ for (const k in val)
23
+ if (val[k] === true) isTrue = true
24
+ else if (val[k] === false) isFalse = true
25
+ if (!isTrue && isFalse) {
26
+ for (const k of subs[key]) if (val[k] !== false) _val.push(k)
27
+ } else {
28
+ for (const k in val) if (val[k] === true) _val.push(k)
29
+ }
30
+ val = _val
31
+ } else if (!is(Array, val)) val = subs[key]
32
+ for (const v2 of val) {
33
+ if (is(String, v2) && includes(v2, subs[key])) _subs.push(v2)
34
+ }
35
+ if (_subs.length === 0) return null
36
+ return { key, subs: _subs }
37
+ }
38
+ return null
39
+ }
40
+
41
+ const field_blocks = (key, val = true) => {
42
+ if (includes(key, ["id", "timestamp", "height", "previous"])) {
43
+ return { key }
44
+ }
45
+ return null
46
+ }
47
+
48
+ export default class GQL {
49
+ constructor({ mem }) {
50
+ this.mem = mem
51
+ }
52
+ async txs(opt = {}) {
53
+ let block_max = null
54
+ let block_min = null
55
+ let _block = {}
56
+ if (is(Number, opt.block)) {
57
+ _block = { min: opt.block, max: opt.block }
58
+ } else if (is(Array, opt.block) && opt.block.length > 0) {
59
+ _block = {}
60
+ if (!isNil(opt.block[0])) _block.min = opt.block[0]
61
+ if (!isNil(opt.block[1])) _block.max = opt.block[1]
62
+ }
63
+ let first = opt.first ?? 20
64
+ let tags = []
65
+ for (const k in opt.tags ?? {}) {
66
+ if (is(String, opt.tags[k])) {
67
+ tags.push({ name: k, values: [opt.tags[k]] })
68
+ } else if (is(Array, opt.tags[k])) {
69
+ tags.push({ name: k, values: opt.tags[k] })
70
+ }
71
+ }
72
+ let recipients = null
73
+ if (is(Array, opt.recipients)) recipients = opt.recipients
74
+ else if (is(String, opt.recipient)) recipients = [opt.recipient]
75
+
76
+ let owners = null
77
+ if (is(Array, opt.owners)) owners = opt.owners
78
+ else if (is(String, opt.owner)) owners = [opt.owner]
79
+
80
+ let ids = null
81
+ if (is(Array, opt.ids)) ids = opt.ids
82
+ else if (is(String, opt.id)) ids = [opt.id]
83
+
84
+ let data = []
85
+ let blocks = this.mem.blocks
86
+ if (opt.asc !== true) blocks = reverse(blocks)
87
+ let count = 0
88
+ let after = false
89
+ for (const v of blocks) {
90
+ const block = clone(this.mem.blockmap[v])
91
+ if (!isNil(_block.min) && block.height < _block.min) continue
92
+ if (!isNil(_block.max) && block.height > _block.max) continue
93
+ let txs = block.txs
94
+ if (opt.asc !== true) txs = reverse(txs)
95
+ for (const v2 of txs) {
96
+ if (!isNil(opt.after) && opt.after === v2) {
97
+ after = true
98
+ break
99
+ }
100
+ if (!isNil(opt.after) && count === 0 && !after) continue
101
+ let tx = this.mem.txs[v2]
102
+ if (!isNil(ids) && ids.length > 0) {
103
+ if (!includes(tx.id, ids)) continue
104
+ }
105
+ if (!isNil(owners) && owners.length > 0) {
106
+ if (!includes(tx.owner, owners)) continue
107
+ }
108
+
109
+ if (!isNil(recipients) && recipients.length > 0) {
110
+ if (!includes(tx.recipient, recipients)) continue
111
+ }
112
+ let tag_unmatch = false
113
+ for (const v of tags) {
114
+ let ex = false
115
+ for (const v2 of tx.tags) {
116
+ if (v2.name === v.name && includes(v2.value, v.values)) {
117
+ ex = true
118
+ break
119
+ }
120
+ }
121
+ if (!ex) {
122
+ tag_unmatch = true
123
+ break
124
+ }
125
+ }
126
+ if (tag_unmatch) continue
127
+ let _tx = {
128
+ cursor: tx.id,
129
+ ...tx,
130
+ data: tx._data,
131
+ block: pick(["id", "timestamp", "height", "previous"], block),
132
+ anchor: tx.anchor ?? "",
133
+ signature: tx.signature ?? "",
134
+ owner: { address: tx.owner, key: this.mem.addrmap[tx.owner] },
135
+ fee: { ar: "0", winston: "0" },
136
+ quantity: { ar: "0", winston: "0" },
137
+ data: { size: "0", type: "" },
138
+ }
139
+ if (!isNil(opt.fields)) {
140
+ let _tx2 = { cursor: tx.id }
141
+ let fields = []
142
+ if (is(Array, opt.fields) && opt.fields.length > 0) {
143
+ for (const v of opt.fields) {
144
+ if (is(String, v)) {
145
+ const fld = field(v)
146
+ if (fld) fields.push(fld)
147
+ } else if (is(Object, v) && !is(Array, v)) {
148
+ for (const k in v) {
149
+ const fld = field(k, v[k])
150
+ if (fld) fields.push(fld)
151
+ }
152
+ }
153
+ }
154
+ } else if (is(Object, opt.fields) && !is(Array, opt.fields)) {
155
+ for (const k in opt.fields) {
156
+ const fld = field(k, opt.fields[k])
157
+ if (fld) fields.push(fld)
158
+ }
159
+ }
160
+ for (const f of fields) {
161
+ if (isNil(f.subs)) {
162
+ _tx2[f.key] = _tx[f.key] ?? ""
163
+ } else {
164
+ let subs = {}
165
+ _tx2[f.key] = {}
166
+ if (is(Array, _tx[f.key])) {
167
+ _tx2[f.key] = map(pick(f.subs))(_tx[f.key])
168
+ } else {
169
+ for (const f2 of f.subs) {
170
+ _tx2[f.key][f2] = _tx[f.key][f2] ?? ""
171
+ }
172
+ }
173
+ }
174
+ }
175
+ _tx = _tx2
176
+ }
177
+ data.push(_tx)
178
+ count += 1
179
+ if (count >= first) break
180
+ }
181
+ if (count >= first) break
182
+ }
183
+ if (opt.next === true) {
184
+ let cursor = null
185
+ if (data.length > 0) cursor = last(data).cursor
186
+ const next = !cursor
187
+ ? null
188
+ : async () => {
189
+ let _opt = clone(opt)
190
+ _opt.after = cursor
191
+ return await this.txs(_opt)
192
+ }
193
+ return { data, next }
194
+ } else {
195
+ return data
196
+ }
197
+ }
198
+ async blocks(opt = {}) {
199
+ let block_max = null
200
+ let block_min = null
201
+ let _block = {}
202
+ if (is(Number, opt.block)) {
203
+ _block = { min: opt.block, max: opt.block }
204
+ } else if (is(Array, opt.block) && opt.block.length > 0) {
205
+ _block = {}
206
+ if (!isNil(opt.block[0])) _block.min = opt.block[0]
207
+ if (!isNil(opt.block[1])) _block.max = opt.block[1]
208
+ }
209
+ let first = opt.first ?? 20
210
+
211
+ let ids = null
212
+ if (is(Array, opt.ids)) ids = opt.ids
213
+ else if (is(String, opt.id)) ids = [opt.id]
214
+
215
+ let data = []
216
+ let blocks = this.mem.blocks
217
+ if (opt.asc !== true) blocks = reverse(blocks)
218
+ let count = 0
219
+ let after = false
220
+ for (const v of blocks) {
221
+ let block = clone({ ...this.mem.blockmap[v], cursor: v })
222
+ delete block.txs
223
+ if (!isNil(_block.min) && block.height < _block.min) continue
224
+ if (!isNil(_block.max) && block.height > _block.max) continue
225
+ if (!isNil(ids) && ids.length > 0) if (!includes(v.id, ids)) continue
226
+ if (!isNil(opt.fields)) {
227
+ let block2 = { cursor: block.id }
228
+ let fields = []
229
+ if (is(Array, opt.fields) && opt.fields.length > 0) {
230
+ for (const v of opt.fields) {
231
+ if (is(String, v)) {
232
+ const fld = field_blocks(v)
233
+ if (fld) fields.push(fld)
234
+ } else if (is(Object, v) && !is(Array, v)) {
235
+ for (const k in v) {
236
+ const fld = field_blocks(k, v[k])
237
+ if (fld) fields.push(fld)
238
+ }
239
+ }
240
+ }
241
+ } else if (is(Object, opt.fields) && !is(Array, opt.fields)) {
242
+ for (const k in opt.fields) {
243
+ const fld = field_blocks(k, opt.fields[k])
244
+ if (fld) fields.push(fld)
245
+ }
246
+ }
247
+ for (const f of fields) {
248
+ if (isNil(f.subs)) {
249
+ block2[f.key] = block[f.key] ?? ""
250
+ } else {
251
+ let subs = {}
252
+ block2[f.key] = {}
253
+ if (is(Array, block[f.key])) {
254
+ block2[f.key] = map(pick(f.subs))(block[f.key])
255
+ } else {
256
+ for (const f2 of f.subs) {
257
+ block2[f.key][f2] = block[f.key][f2] ?? ""
258
+ }
259
+ }
260
+ }
261
+ }
262
+ block = block2
263
+ }
264
+ data.push(block)
265
+ count += 1
266
+ if (count >= first) break
267
+ }
268
+ if (opt.next === true) {
269
+ let cursor = null
270
+ if (data.length > 0) cursor = last(data).cursor
271
+ const next = !cursor
272
+ ? null
273
+ : async () => {
274
+ let _opt = clone(opt)
275
+ _opt.after = cursor
276
+ return await this.txs(_opt)
277
+ }
278
+ return { data, next }
279
+ } else {
280
+ return data
281
+ }
282
+ }
283
+ }
package/esm/utils.js CHANGED
@@ -91,18 +91,6 @@ const isData = (data, res) => {
91
91
  return false
92
92
  }
93
93
 
94
- const query = txid => `query {
95
- transactions(ids: ["${txid}"]) {
96
- edges { node { id tags { name value } owner { address } } }
97
- }
98
- }`
99
-
100
- const queries = to => `query {
101
- transactions (recipients: ["${to}"]){
102
- edges { node { id recipient tags { name value } owner { address } } }
103
- }
104
- }`
105
-
106
94
  const isLocalhost = v => includes(v, ["localhost", "127.0.0.1"])
107
95
 
108
96
  const udl = ({ payment, access, derivations, commercial, training }) => {
@@ -315,8 +303,6 @@ export {
315
303
  srcs,
316
304
  getTagVal,
317
305
  isData,
318
- query,
319
- queries,
320
306
  getTag,
321
307
  tagEq,
322
308
  searchTag,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wao",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "description": "",
5
5
  "main": "cjs/index.js",
6
6
  "module": "esm/index.js",