-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprizeApiToDb.js
68 lines (60 loc) · 2.53 KB
/
prizeApiToDb.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
// import {ProcessPrizeApiDraw} from "./testApiSort.js"
const { ProcessPrizeApiDraw } = require("./processPrizeApiDraw.js")
const pgp = require("pg-promise")(/* initialization options */);
const dotenv = require("dotenv");
const fetch = require("cross-fetch")
dotenv.config();
const cn = {
host: "localhost", // server name or IP address;
port: 5432,
database: "prizeapi",
user: "pooltogether",
password: process.env.PASSWORD,
};
const db = pgp(cn);
// let currentDraw = 281
// let startDraw = 281
async function go() {
let getCurrentDraw = await fetch("https://poolexplorer.xyz/recent")
let currentDraw = await getCurrentDraw.json()
let newDraw = currentDraw.id + 1
let apiUpdated = false
try {
let fetchApiTest = await fetch("https://api.pooltogether.com/prizes/137/0x8141bcfbcee654c5de17c4e2b2af26b67f9b9056/draw/" + newDraw + "/prizes.json")
// console.log(fetchApiTest)
if (fetchApiTest.status !== 404 || currentDraw !== undefined) {
fetchApiTest = await fetchApiTest.json()
let totalPrizeCount = 0
let totalPrizeCountClaimable = 0
let totalValueClaimable = 0
let totalValueDropped = 0
// for (let draw = startDraw; draw <= currentDraw; draw++) {
for (let draw = newDraw ; draw <= newDraw; draw++) {
let result = await ProcessPrizeApiDraw(draw);
let winners = result.result
for (let x of winners) {
let network = ""
if (x.n == 3) { network = "polygon" }
if (x.n == 4) { network = "avalanche" }
if (x.n == 6) { network = "optimism" }
if (x.n == 1) { network = "ethereum" }
let address = "\\" + x.a.substring(1)
let claimable = x.c.map(x => x * 1e14)
let dropped = x.u.map(x => x * 1e14)
let newWinner = "INSERT into prizes (network,address,draw_id,claimable_prizes,dropped_prizes) values('" + network + "','" + address + "','" + draw + "','{" + claimable + "}','{" + dropped + "}')";
// console.log(newWinner)
await db.any(newWinner);
}
totalPrizeCount += result.totalPrizeCount
totalPrizeCountClaimable += result.totalPrizeCountClaimable
totalValueClaimable += result.totalValueClaimable
totalValueDropped += result.totalValueDropped
}
console.log("\n\ntotal prize count ", totalPrizeCount,
"\ntotal prize count claimable ", totalPrizeCountClaimable,
"\ntotal value claimable ", totalValueClaimable,
"\ntotal value dropped ", totalValueDropped)
}
} catch (error) { console.log(error) }
}
go()