Skip to content

Commit 6de4ef6

Browse files
Poll Transfer events, populate daily_fil table, reformat endpoint handling
1 parent 91ac513 commit 6de4ef6

10 files changed

+1001
-50
lines changed

bin/spark-stats.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import '../lib/instrument.js'
2+
import { ethers } from 'ethers'
23
import http from 'node:http'
34
import { once } from 'node:events'
5+
import fs from 'node:fs/promises'
6+
import { fileURLToPath } from 'node:url'
7+
import { onContractEvent } from 'on-contract-event'
48
import pg from 'pg'
59
import { createHandler } from '../lib/handler.js'
6-
import { DATABASE_URL } from '../lib/config.js'
10+
import { DATABASE_URL, IE_CONTRACT_ADDRESS, RPC_URL, rpcHeaders } from '../lib/config.js'
11+
import { updateDailyFilStats } from '../lib/platform-stats-generator.js'
712

813
const {
914
PORT = 8080,
@@ -50,3 +55,40 @@ console.log('Starting the http server on host %j port %s', HOST, PORT)
5055
server.listen(PORT, HOST)
5156
await once(server, 'listening')
5257
console.log(`http://${HOST}:${PORT}`)
58+
59+
// Set up the Ethereum provider and contract
60+
const fetchRequest = new ethers.FetchRequest(RPC_URL)
61+
fetchRequest.setHeader('Authorization', rpcHeaders.Authorization || '')
62+
const provider = new ethers.JsonRpcProvider(
63+
fetchRequest,
64+
null,
65+
{ batchMaxCount: 1 }
66+
)
67+
const ieContract = new ethers.Contract(
68+
IE_CONTRACT_ADDRESS,
69+
JSON.parse(
70+
await fs.readFile(
71+
fileURLToPath(new URL('../lib/abi.json', import.meta.url)),
72+
'utf8'
73+
)
74+
),
75+
provider
76+
)
77+
78+
const onTransfer = async (to, amount) => {
79+
const transferEvent = { to_address: to, amount }
80+
updateDailyFilStats(pgPool, transferEvent)
81+
}
82+
83+
// Listen for Transfer events from the IE contract
84+
const it = onContractEvent({
85+
contract: ieContract,
86+
provider,
87+
rpcUrl: RPC_URL,
88+
rpcHeaders
89+
})
90+
for await (const event of it) {
91+
if (event.name === 'Transfer') {
92+
await onTransfer(...event.args)
93+
}
94+
}

0 commit comments

Comments
 (0)