-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnon-functional.js
More file actions
110 lines (93 loc) · 3.68 KB
/
Copy pathnon-functional.js
File metadata and controls
110 lines (93 loc) · 3.68 KB
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
const pgp = require("pg-promise")({});
const cn = 'postgres://postgres:postgres@localhost:5432/moonbeans';
const db = pgp(cn);
const fs = require("fs").promises;
const Web3 = require("web3");
const provider = new Web3.providers.WebsocketProvider("wss://moonriver.api.onfinality.io/public-ws", {
clientConfig: {
maxReceivedFrameSize: 100000000,
maxReceivedMessageSize: 100000000,
keepalive: true,
keepaliveInterval: -1
},
reconnect: {
auto: true,
delay: 50000000,
maxAttempts: 5,
onTimeout: true
},
timeout: 30000000
})
const web3 = new Web3(provider);
async function main() {
let mnbeansAsks = [];
const collections = JSON.parse(await fs.readFile('./collections.json'));
let collectionAddresses = {};
for (let [key, val] of Object.entries(collections)) {
collectionAddresses[val['contractAddress']] = val['title'];
}
let oldAsks = [];
mnbeansAsks = await db.manyOrNone('SELECT "tokenId", "collectionId", "timestamp", "tokenNumber", "transactionHash" FROM "asks"');
console.log("Listed tokens: ", mnbeansAsks.length)
if (mnbeansAsks.length > 0) {
console.log("Processing token list..");
try {
while (mnbeansAsks.length > 0) {
ask = mnbeansAsks[0];
if (!(ask['collectionId'] in collectionAddresses)) {
mnbeansAsks.shift();
continue;
}
let holder = await db.oneOrNone('SELECT "currentOwner", "lastTransfer" FROM "holders" WHERE "id" = $1', [ask['tokenId']]);
if (holder === null) {
console.log("Skipping", ask['tokenId']);
mnbeansAsks.shift();
continue;
}
if (ask['timestamp'] < holder['lastTransfer']) {
let tx = await web3.eth.getTransaction(ask['transactionHash']);
if (tx['from'] != holder['currentOwner']) {
oldAsks.push(Object.assign({}, ask, holder));
}
}
mnbeansAsks.shift();
}
} catch (e) {
console.log(e);
console.log("Retrying...");
await sleep(120000);
}
}
console.log("Token Transfers after their listing: ", oldAsks.length);
let oldContracts = {};
let tokenIds = [];
for (let oldAsk of oldAsks) {
if (!(oldAsk['collectionId'] in oldContracts)) {
oldContracts[oldAsk['collectionId']] = {
title: collectionAddresses[oldAsk['collectionId']],
count: 0,
entities: []
}
}
oldContracts[oldAsk['collectionId']]['count']++;
oldContracts[oldAsk['collectionId']]['entities'].push(oldAsk['tokenNumber']);
tokenIds.push(oldAsk['tokenId']);
}
console.log(oldContracts);
if (tokenIds.length) {
console.log(`DELETE FROM "asks" WHERE "tokenId" IN ('${tokenIds.join("','")}');`);
console.log(`UPDATE "tokens" SET "currentAsk" = 0 WHERE "id" IN ('${tokenIds.join("','")}');`);
for (let collectionId in oldContracts) {
console.log(`UPDATE "collections" SET "ceilingPrice" = (SELECT MAX("value") FROM "asks" WHERE "collectionId" = '${collectionId}'), "floorPrice" = (SELECT MIN("value") FROM "asks" WHERE "collectionId" = '${collectionId}') WHERE "id" = '${collectionId}';`);
}
}
}
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
main().then((e => {
console.log('Done');
process.exit()
}))