-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_block.ts
123 lines (106 loc) · 3.58 KB
/
index_block.ts
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
/* Polygon chain indexing
Copyright (C) 2024 Alexander Diemand
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { isBlock } from './data_Block'
import { isTransaction } from './data_Transaction'
import { connect, get_block, ankr_amoy_url, last_block_height } from './get_block'
import { chain } from './get_contract_tx'
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient({ log: ['warn'] })
// const selected_chain = chain.POLYGON_AMOY
// var startblock: bigint = 12496368n
let startblock: bigint = 0n
let numblocks: bigint = 100n
const args = process.argv
// skip first two arguments; usually the interpreter and the code file
for (let index = 2; index < args.length - 1; index++) {
if (args[index] == '--startblock') {
startblock = BigInt(args[++index])
} else if (args[index] == '--numblocks') {
numblocks = BigInt(args[++index])
}
}
const rpc = connect(ankr_amoy_url)
console.log("now: " + new Date().toLocaleString())
async function main(): Promise<void> {
const lastblock = await last_block_height(rpc)
console.log("current block height: " + String(lastblock))
// if no startblock argument, then use last block in db
if (startblock == 0n) {
const dbblock = await prisma.block.findFirst({
select: {
number: true
},
orderBy: {
number: 'desc'
}
})
if (dbblock) {
console.log(`restarting after latest database block ${dbblock.number}`)
startblock = dbblock.number + 1n
const toend = lastblock - startblock
if (toend < numblocks) {
numblocks = toend
}
} else {
startblock = lastblock - numblocks
}
}
console.log(`reading ${numblocks} blocks starting at ${startblock}`);
// inject BigInt serialisation to JSON
(BigInt.prototype as any).toJSON = function () {
return this.toString();
};
try {
let blocks = []
for (let num:bigint = 0n; num < numblocks; num++) {
const block = await get_block(rpc, startblock + num)
if (isBlock((block))) {
blocks.push(block)
// split JSON structure
const {transactions, ...blockheader} = block
await prisma.block.create({
data: {
...blockheader
},
})
for (let tx of transactions) {
tx.v = tx.v.toString()
if (tx.to) {} else {tx.to = ""}
if (isTransaction(tx)) {
await prisma.transaction.create({
data: tx
})
}
}
} else {
console.log(`failed to match block of JSON: ${JSON.stringify(block,null,2)}`)
}
}
console.log(`got ${blocks.length} blocks`)
// console.log(JSON.stringify(blocks,null,2))
} catch (e: unknown) {
console.log("error occured: " + String(e))
}
}
main().then(() => {
console.log("done.")
}).catch((e: unknown) => {
let m: string = 'unknown error'
if (typeof e == 'string') {
m = e
} else if (e instanceof Error) {
m = e.message
}
console.log("Error at toplevel: " + m)
})