|
1 | 1 | import '../lib/instrument.js'
|
| 2 | +import { ethers } from 'ethers' |
2 | 3 | import http from 'node:http'
|
3 | 4 | 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' |
4 | 8 | import pg from 'pg'
|
5 | 9 | 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' |
7 | 12 |
|
8 | 13 | const {
|
9 | 14 | PORT = 8080,
|
@@ -50,3 +55,40 @@ console.log('Starting the http server on host %j port %s', HOST, PORT)
|
50 | 55 | server.listen(PORT, HOST)
|
51 | 56 | await once(server, 'listening')
|
52 | 57 | 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