-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.js
430 lines (354 loc) · 13.9 KB
/
handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
'use strict'
const axios = require('axios')
const _ = require('lodash')
const MongoClient = require('mongodb').MongoClient
require('dotenv').config()
const url = process.env.MONGO_URI
const dev = 'https://api.better-call.dev/v1/contract/mainnet/KT1RJ6PbjHpwc3M5rw5s2Nbmefwbuwbdxton/tokens'
const client = new MongoClient(url)
// method to populate the DB with all owners via upsert. Takes around 60 mins (500k records).
const getOwners = async (arr, counter, owners) => {
/*
https://api.tzkt.io/v1/bigmaps/522/keys?sort.desc=id&select=key,value&offset=0&limit=10
limit can be up to 1000
[{"key":{"nat":"68596","address":"tz1d7i7nUREze4LCpfonUeaJgdfhcWnoy7p9"},"value":"1"},..]
owners collection: {"token_id": 999999, "owner_id": "tz123", "balance": 0 }
unique index on token_id, owner_id. Index on owner_id for search.
*/
let res = await axios.get("https://api.tzkt.io/v1/bigmaps/511/keys?sort.desc=id&select=key,value&limit=50&offset=" + counter)
.then(res => res.data)
res = await res.map(async e => {
try {
const query = { "token_id": parseInt(e.key.nat), "owner_id": e.key.address }
const update = { "$set": { "token_id": parseInt(e.key.nat), "owner_id": e.key.address, "balance": parseInt(e.value) } }
const options = { upsert: true };
console.log(e.key)
let r = await owners.updateOne(query, update, options)
if (r.modifiedCount === 1 || r.upsertedId !== null) {
return true //updated or inserted something new
} else {
return false //change from false to true to interate thru all results.
}
} catch (err) {
console.log('err', e.key, err)
return false
}
})
var promise = Promise.all(res.map(e => e))
promise.then(async (results) => {
if (!results.every(e => e === false)) { //looks at all results to see if there was a change
await getOwners(arr, counter + 50, owners) //fetch more records if some results were updated
}
})
console.log('end')
return [arr, ...res]
}
////////////////////////
// method to populate the DB with objkt curation hDAO balances via upsert.
// Full refresh takes around 10 mins (70k records).
const getTokenCurationBalance = async (arr, counter, objkts) => {
/*
https://api.tzkt.io/v1/bigmaps/519/keys?sort.desc=id&select=key,value&offset=0&limit=10
limit can be up to 1000
[{"key":"69976","value":{"issuer":"tz1ZM3gyiFnaU9itgTshE7Z2jgG3bTk4TF2z","hDAO_balance":"4471"}},..]
`token_curations` collection: {"token_id": 999999, "hdao_balance": 0 }
unique index on token_id.
*** API has `hDAO_balance` in objkt
Given the 1:1 relationship with objkt, this should be an object on `metadata` collection.
Iterate through bigmap. stop when no more updates after a `counter` updates no more records
Update only, if token_id missing, then will pick up in next call.
*/
let res = await axios.get("https://api.tzkt.io/v1/bigmaps/519/keys?sort.desc=id&select=key,value&limit=50&offset=" + counter)
.then(res => res.data)
res = await res.map(async e => {
try {
const query = { "token_id": parseInt(e.key), hDAO_balance: { "$ne": parseInt(e.value.hDAO_balance) } }
const update = { "$set": { "hDAO_balance": parseInt(e.value.hDAO_balance) } }
console.log(e.key, e.value)
let r = await objkts.findOneAndUpdate(query, update)
console.log(r.lastErrorObject.updatedExisting)
if (r.lastErrorObject.updatedExisting === true) {
return true //updated or inserted something new
} else {
return false //change from false to true to interate thru all results.
}
} catch (err) {
console.log('err', e.key, err)
return false
}
})
var promise = Promise.all(res.map(e => e))
promise.then(async (results) => {
if (!results.every(e => e === false)) { //looks at all results to see if there was a change
await getTokenCurationBalance(arr, counter + 50, objkts) //fetch more records if some results were updated
}
})
console.log('end')
return [arr, ...res]
}
////////////////////////
// method to populate the DB with swaps via upsert.
// Full refresh takes around 10 mins (150k records).
const getSwaps = async (arr, counter, swaps) => {
/*
https://api.tzkt.io/v1/bigmaps/523/keys?sort.desc=id&offset=0&limit=100
limit can be up to 1000
[{"id":1210695,"active":true,"hash":"expruPNtEb5v3PLGHEFrnQTNAL7TZX38TQkvWcj3KAYNZ8PMp3yxwQ",
"key":"150293","value":{"issuer":"tz1ViMzA17dRAZpCopuckBHdphMua26YDVgN","objkt_id":"46019",
"objkt_amount":"1","xtz_per_objkt":"4300000"},"firstLevel":1473980,"lastLevel":1473980,
"updates":1}, ...]
`swaps` collection: {"token_id": 999999, "swap_id": 12345, "objkt_amount": 1, "xtz_per_objkt": 100000,
"issuer": "tz123", "active": true}
unique index on swap_id. natural key on token_id, issuer. facts: active (status), price, amt.
Iterate through bigmap. stop when no more updates after a `counter` updates no more records
Update only, if token_id missing, then will pick up in next call.
*/
let res = await axios.get("https://api.tzkt.io/v1/bigmaps/523/keys?sort.desc=id&limit=100&offset=" + counter)
.then(res => res.data)
res = await res.map(async e => {
try {
const query = {
swap_id: parseInt(e.key),
"$or": [
{ status: { "$ne": e.value.active } },
{ objkt_amount: { "$ne": parseInt(e.value.objkt_amount) } },
{ xtz_per_objkt: { "$ne": parseInt(e.value.xtz_per_objkt) } }
]
}
const update = {
"$set": {
token_id: parseInt(e.value.objkt_id),
issuer: e.value.issuer,
status: e.active,
objkt_amount: parseInt(e.value.objkt_amount),
xtz_per_objkt: parseInt(e.value.xtz_per_objkt)
}
}
const options = { upsert: true };
console.log(e.id, "swap_id:", e.key) //, e.value, e.active)
let r = await swaps.updateOne(query, update, options)
if (r.modifiedCount === 1 || r.upsertedId !== null) {
//console.log("t", r.modifiedCount, r.upsertedId)
return true //updated or inserted something new
} else {
//console.log("f", r.modifiedCount, r.upsertedId)
return false //change from false to true to interate thru all results.
}
} catch (err) {
console.log('err', e.key, err)
return false
}
})
var promise = Promise.all(res.map(e => e))
promise.then(async (results) => {
if (!results.every(e => e === false)) { //looks at all results to see if there was a change
await getSwaps(arr, counter + 100, swaps) //fetch more records if some results were updated
}
})
console.log('end')
return [arr, ...res]
}
////////////////////////
// method to populate the DB with user hDAO via upsert.
// Full refresh takes around 2 mins (3k records).
const getHDAOBalances = async (arr, counter, hDAOBalances) => {
/*
https://api.tzkt.io/v1/bigmaps/515/keys?sort.desc=id&select=key,value&offset=0&limit=100
limit can be up to 1000
[{"key":{"nat":"0","address":"tz1gk59qsxEYJyq1owS6NqV61hA2nD8nihyH"},"value":"99651"}, ...]
`hdao_balances` collection: {"wallet": "tz123", balance: 12345}
unique index on wallet. facts: balance. Balances are set to 0 rather than active=false.
Iterate through bigmap. stop when no more updates after a `counter` updates no more records
Update only, if token_id missing, then will pick up in next call.
*/
let res = await axios.get("https://api.tzkt.io/v1/bigmaps/515/keys?sort.desc=id&select=key,value&limit=50&offset=" + counter)
.then(res => res.data)
res = await res.map(async e => {
try {
const query = {
wallet: e.key.address, balance: { "$ne": parseInt(e.value) }
}
const update = { "$set": { value: parseInt(e.value) } }
const options = { upsert: true }
console.log("wallet:", e.key.address)
let r = await hDAOBalances.updateOne(query, update, options)
if (r.modifiedCount === 1 || r.upsertedId !== null) {
//console.log("t", r.modifiedCount, r.upsertedId)
return true //updated or inserted something new
} else {
//console.log("f", r.modifiedCount, r.upsertedId)
return false //change from false to true to interate thru all results.
}
} catch (err) {
console.log('err', e.key, err)
return false
}
})
var promise = Promise.all(res.map(e => e))
promise.then(async (results) => {
if (!results.every(e => e === false)) { //looks at all results to see if there was a change
await getHDAOBalances(arr, counter + 50, hDAOBalances) //fetch more records if some results were updated
}
})
console.log('end')
return [arr, ...res]
}
//////////////
const getRoyalties = async (arr, counter, objkts) => {
// https://api.tzkt.io/v1/bigmaps/522/keys?sort.desc=id&select=key,value&offset=0&limit=10
// limit can be up to 1000
// default is in ascending order
// {"key":"152","value":{"issuer":"tz1UBZUkXpKGhYsP5KtzDNqLLchwF4uHrGjw","royalties":"100"}}
let res = await axios.get("https://api.tzkt.io/v1/bigmaps/522/keys?sort.desc=id&select=key,value&limit=20&offset=" + counter)
.then(res => res.data)
res = await res.map(async e => {
try {
const query = { "token_id": parseInt(e.key), royalties: { "$ne": parseInt(e.value.royalties) / 1000 } }
const update = { "$set": { "royalties": parseInt(e.value.royalties) / 1000 } }
console.log(e.key, e.value)
let r = await objkts.findOneAndUpdate(query, update)
console.log(r.lastErrorObject.updatedExisting)
if (r.lastErrorObject.updatedExisting === true) {
return true //updated or inserted something new
} else {
return false //change from false to true to interate thru all results.
}
} catch (err) {
console.log('err', e.key, err)
return false
}
})
var promise = Promise.all(res.map(e => e))
promise.then(async (results) => {
if (!results.every(e => e === false)) {
await getRoyalties(arr, counter + 20, objkts)
}
})
console.log('end')
return [arr, ...res]
}
const getFeed = async (arr, counter, objkts) => {
// gets latest objkts
let res = await axios
.get("https://api.better-call.dev/v1/contract/mainnet/KT1RJ6PbjHpwc3M5rw5s2Nbmefwbuwbdxton/tokens?offset=" + counter)
.then(res => res.data)
res = await res.map(async e => {
// fails on unique keys
try {
console.log(e.token_id)
await objkts.insertOne(e)
return true
} catch (err) {
//console.log('err', e.token_id, err)
return false
}
})
var promise = Promise.all(res.map(e => e))
promise.then(async (results) => {
if (!results.includes(false)) {
await getFeed(arr, counter + 10, objkts)
}
})
console.log('end')
return [arr, ...res]
}
///////
// subjkts methods
const registries = 3536
const subjkts = 3537
const subjkts_metadata = 3538
const kt = 'KT1P69B8exDGuqNysBweuZJSqAmaD4dU3gtU'
//https://api.better-call.dev/v1/contract/mainnet/KT1P69B8exDGuqNysBweuZJSqAmaD4dU3gtU/storage
const getRegistries = async () => {
let res = await axios.get('https://api.better-call.dev/v1/bigmap/mainnet/3536/keys').then(res => res.data)
return res.map(e => {
return { tz: e.data.key.value, subjkt: e.data.value !== null ? e.data.value.value : null }
})
}
const getSubjktsMetadata = async () => {
let res = await axios.get('https://api.better-call.dev/v1/bigmap/mainnet/3538/keys').then(res => res.data)
res = res.map(e => {
return { subjkt: e.data.key.value, ipfs: e.data.value !== null ? e.data.value.value : null }
})
return res.map(async e => {
if (e.ipfs !== null) {
e.metadata = await axios.get(`https://ipfs.io/ipfs/${(e.ipfs).split('//')[1]}`).then(res => res.data)
return e
} else {
return e
}
})
}
const merge = async (subjkt) => {
let promise = Promise.all((await getSubjktsMetadata()).map(e => e))
promise.then(async (metadata) => {
let result = _.merge(_.keyBy(await getRegistries(), 'subjkt'), _.keyBy(metadata, 'subjkt'))
await subjkt.insertMany(_.values(result))
})
}
const insertSubjkts = async () => {
await client.connect()
const database = client.db('OBJKTs-DB')
const subjkt = database.collection('subjkt')
await merge(subjkt)
// for string?
// await subjkt.createIndex( { 'subjkt' : 1 }, { unique : true } )
}
const insertFeed = async () => {
await client.connect()
const database = client.db('OBJKTs-DB')
const objkts = database.collection('metadata')
//await objkts.createIndex( { 'token_id' : 1 }, { unique: true } )
await getFeed([], 0, objkts)
}
const insertRoyalties = async () => {
await client.connect()
const database = client.db('OBJKTs-DB')
const objkts = database.collection('metadata')
await getRoyalties([], 0, objkts)
}
const insertTokenOwners = async () => {
await client.connect()
const database = client.db('OBJKTs-DB')
const owners = database.collection('owners')
//await owners.createIndex( { 'token_id' : 1, 'owner_id' : 1 }, { unique: true } )
//await owners.createIndex( { 'owner_id' : 1 } )
await getOwners([], 0, owners)
}
const insertTokenCurationBalance = async () => {
await client.connect()
const database = client.db('OBJKTs-DB')
const objkts = database.collection('metadata')
await getTokenCurationBalance([], 0, objkts)
}
const insertSwaps = async () => {
await client.connect()
const database = client.db('OBJKTs-DB')
const swaps = database.collection('swaps')
await getSwaps([], 0, swaps)
}
const insertHDAOBalances = async () => {
await client.connect()
const database = client.db('OBJKTs-DB')
const hDAOBalances = database.collection('hdao_balances')
await getHDAOBalances([], 0, hDAOBalances)
}
//insertFeed()
//insertRoyalties()
//insertTokenOwners()
//insertTokenCurationBalance()
//insertSwaps()
//insertHDAOBalances()
//insertSubjkts()
module.exports.insert = async (event) => {
await insertFeed()
await insertRoyalties()
await insertTokenOwners()
await insertTokenCurationBalance()
await insertSwaps()
await insertHDAOBalances()
//await insertSubjkts()
return {
status : 200
}
}