|
| 1 | +import { Framework } from "@superfluid-finance/sdk-core"; |
| 2 | +import { ethers } from "ethers"; |
| 3 | + |
| 4 | +const CONFIG = { |
| 5 | + chainId: 10, |
| 6 | + wsUrl: `wss://opt-mainnet.g.alchemy.com/v2/${process.env.ALCHEMY_WS_KEY}`, |
| 7 | + audityzerAddr: process.env.AUDITYZER_ADDR!.toLowerCase(), |
| 8 | + pollIntervalMs: 120_000, |
| 9 | +}; |
| 10 | + |
| 11 | +let provider: ethers.WebSocketProvider; |
| 12 | +let sf: Framework; |
| 13 | + |
| 14 | +async function initializeMonitoring() { |
| 15 | + try { |
| 16 | + provider = new ethers.WebSocketProvider(CONFIG.wsUrl); |
| 17 | + sf = await Framework.create({ chainId: CONFIG.chainId, provider }); |
| 18 | + const usdcx = await sf.loadSuperToken("USDCx"); |
| 19 | + |
| 20 | + setInterval(async () => { |
| 21 | + const [rtb, netFlow] = await Promise.all([ |
| 22 | + usdcx.realtimeBalanceOf({ account: CONFIG.audityzerAddr, providerOrSigner: provider }), |
| 23 | + sf.cfaV1.getNetFlow({ superToken: usdcx.address, account: CONFIG.audityzerAddr, providerOrSigner: provider }), |
| 24 | + ]); |
| 25 | + console.log({ |
| 26 | + availableBalance: ethers.formatEther(rtb.availableBalance), |
| 27 | + deposit: ethers.formatEther(rtb.deposit), |
| 28 | + netFlowRateWeiPerSec: netFlow, |
| 29 | + }); |
| 30 | + }, CONFIG.pollIntervalMs); |
| 31 | + |
| 32 | + const cfaContract = new ethers.Contract( |
| 33 | + sf.settings.config.cfaV1Address, |
| 34 | + ["event FlowUpdated(address indexed token, address indexed sender, address indexed receiver, int96 flowRate, int256 totalSenderFlowRate, int256 totalReceiverFlowRate, bytes userData)"], |
| 35 | + provider |
| 36 | + ); |
| 37 | + |
| 38 | + cfaContract.on("FlowUpdated", (token: string, sender: string, receiver: string, flowRate: bigint) => { |
| 39 | + if (receiver.toLowerCase() === CONFIG.audityzerAddr) { |
| 40 | + console.log(`Stream update: ${sender} → rate ${flowRate}`); |
| 41 | + } |
| 42 | + }); |
| 43 | + |
| 44 | + provider.websocket.on("close", () => { |
| 45 | + console.warn("WS disconnected — reconnecting in 5s..."); |
| 46 | + setTimeout(initializeMonitoring, 5000); |
| 47 | + }); |
| 48 | + } catch (err) { |
| 49 | + console.error("Monitor init failed:", err); |
| 50 | + setTimeout(initializeMonitoring, 10_000); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +initializeMonitoring(); |
0 commit comments