Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "pi-door-unified",
"version": "1.0.0",
"type": "module",
"scripts": {
"start": "node dist/server.js",
"build": "tsc"
},
"dependencies": {
"express": "^4.18.2",
"ws": "^8.16.0",
"node-fetch": "^3.3.2"
}
}
29 changes: 29 additions & 0 deletions src/src/monitor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import fetch from 'node-fetch'
import { LedgerEvent } from './types.js'

const HORIZON = 'https://api.mainnet.minepi.com'

export function startMonitor(
onUpdate: (e: LedgerEvent) => void
) {
let last = 0

setInterval(async () => {
try {
const r = await fetch(`${HORIZON}/ledgers?order=desc&limit=1`)
const j = await r.json()
const l = j._embedded.records[0]

if (l.sequence !== last) {
last = l.sequence
onUpdate({
ledger: l.sequence,
txCount: l.transaction_count,
closeTime: Date.parse(l.closed_at)
})
}
} catch (e) {
console.error('monitor error', e)
}
}, 3000)
}
32 changes: 32 additions & 0 deletions src/src/src/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import express from 'express'
import { createServer } from 'http'
import { WebSocketServer } from 'ws'
import { startMonitor } from './monitor.js'

const app = express()
const server = createServer(app)
const wss = new WebSocketServer({ server })

// Serve frontend
app.use(express.static('web'))

// WebSocket
function broadcast(data: any) {
wss.clients.forEach(c => {
if (c.readyState === 1) {
c.send(JSON.stringify(data))
}
})
}

// Start monitor
startMonitor(event => {
broadcast({
type: 'LEDGER_UPDATE',
payload: event
})
})

server.listen(8080, () =>
console.log('🚪 Pi Door Unified running :8080')
)
36 changes: 36 additions & 0 deletions src/src/src/web/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<html>
<head>
<title>Pi Door Live</title>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<body>
<div id="app">
<h2>Pi Network Live Monitor</h2>
<p>Ledger: {{ ledger }}</p>
<p>Tx Count: {{ tx }}</p>
<p>Last Close: {{ time }}</p>
</div>

<script>
const { createApp } = Vue

createApp({
data() {
return { ledger: 0, tx: 0, time: '' }
},
mounted() {
const ws = new WebSocket(`ws://${location.host}`)
ws.onmessage = e => {
const d = JSON.parse(e.data)
if (d.type === 'LEDGER_UPDATE') {
this.ledger = d.payload.ledger
this.tx = d.payload.txCount
this.time = new Date(d.payload.closeTime).toLocaleTimeString()
}
}
}
}).mount('#app')
</script>
</body>
</html>
11 changes: 11 additions & 0 deletions src/src/src/web/refactor: migrate to unified monorepo architecture
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
door-unified/
├── src/
│ ├── server.ts # HTTP + WS server
│ ├── monitor.ts # Ledger polling
│ ├── analyzer.ts # TPS & anomaly
│ └── types.ts
├── web/
│ ├── index.html
│ └── main.js # Vue 3 app (CDN / Vite optional)
├── package.json
└── tsconfig.json
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface LedgerEvent {
ledger: number
txCount: number
closeTime: number
}