237 lines
9.1 KiB
JavaScript
237 lines
9.1 KiB
JavaScript
const fetch = require('node-fetch')
|
|
|
|
/**
|
|
* Horizon Proxy Service
|
|
* Fetches data from public Horizon API when local database is empty
|
|
*/
|
|
class HorizonProxy {
|
|
constructor() {
|
|
this.horizonUrls = {
|
|
public: 'https://horizon.stellar.lobstr.co', // LOBSTR as primary
|
|
publicBackup: 'https://horizon.stellar.org', // SDF as backup
|
|
testnet: 'https://horizon-testnet.stellar.org'
|
|
}
|
|
this.cache = new Map()
|
|
this.cacheTTL = {
|
|
ledger: 10000, // 10 seconds
|
|
assets: 300000, // 5 minutes
|
|
stats: 60000 // 1 minute
|
|
}
|
|
}
|
|
|
|
getCacheKey(network, endpoint) {
|
|
return `${network}:${endpoint}`
|
|
}
|
|
|
|
getFromCache(key) {
|
|
const cached = this.cache.get(key)
|
|
if (cached && Date.now() - cached.timestamp < cached.ttl) {
|
|
return cached.data
|
|
}
|
|
this.cache.delete(key)
|
|
return null
|
|
}
|
|
|
|
setCache(key, data, ttl) {
|
|
this.cache.set(key, {
|
|
data,
|
|
timestamp: Date.now(),
|
|
ttl
|
|
})
|
|
}
|
|
|
|
async fetchFromHorizon(network, endpoint, useBackup = false) {
|
|
const baseUrl = useBackup && network === 'public'
|
|
? this.horizonUrls.publicBackup
|
|
: this.horizonUrls[network]
|
|
const url = `${baseUrl}${endpoint}`
|
|
|
|
try {
|
|
const response = await fetch(url)
|
|
if (!response.ok) {
|
|
throw new Error(`Horizon API error: ${response.status}`)
|
|
}
|
|
return await response.json()
|
|
} catch (error) {
|
|
console.error(`Failed to fetch from Horizon: ${url}`, error.message)
|
|
// Try backup for public network
|
|
if (network === 'public' && !useBackup) {
|
|
console.log('Trying backup Horizon (SDF)...')
|
|
return this.fetchFromHorizon(network, endpoint, true)
|
|
}
|
|
throw error
|
|
}
|
|
}
|
|
|
|
async getLatestLedger(network = 'public') {
|
|
const cacheKey = this.getCacheKey(network, 'ledger:latest')
|
|
const cached = this.getFromCache(cacheKey)
|
|
if (cached) return cached
|
|
|
|
const data = await this.fetchFromHorizon(network, '/ledgers?order=desc&limit=1')
|
|
const horizonLedger = data._embedded?.records?.[0]
|
|
|
|
if (!horizonLedger) return null
|
|
|
|
// Transform Horizon format to internal format
|
|
const ledger = {
|
|
_id: horizonLedger.sequence,
|
|
sequence: horizonLedger.sequence,
|
|
hash: horizonLedger.hash,
|
|
prev_hash: horizonLedger.prev_hash,
|
|
transaction_count: horizonLedger.successful_transaction_count + horizonLedger.failed_transaction_count,
|
|
successful_transaction_count: horizonLedger.successful_transaction_count,
|
|
failed_transaction_count: horizonLedger.failed_transaction_count,
|
|
operation_count: horizonLedger.operation_count,
|
|
closed_at: horizonLedger.closed_at,
|
|
created_at: horizonLedger.closed_at,
|
|
total_coins: horizonLedger.total_coins,
|
|
fee_pool: horizonLedger.fee_pool,
|
|
base_fee_in_stroops: horizonLedger.base_fee_in_stroops,
|
|
base_reserve_in_stroops: horizonLedger.base_reserve_in_stroops,
|
|
max_tx_set_size: horizonLedger.max_tx_set_size,
|
|
protocol_version: horizonLedger.protocol_version,
|
|
header_xdr: horizonLedger.header_xdr,
|
|
paging_token: horizonLedger.paging_token
|
|
}
|
|
|
|
this.setCache(cacheKey, ledger, this.cacheTTL.ledger)
|
|
return ledger
|
|
}
|
|
|
|
async getLedgers(network = 'public', limit = 20, cursor) {
|
|
let endpoint = `/ledgers?order=desc&limit=${limit}`
|
|
if (cursor) endpoint += `&cursor=${cursor}`
|
|
|
|
const data = await this.fetchFromHorizon(network, endpoint)
|
|
return data._embedded?.records || []
|
|
}
|
|
|
|
async getLedgerStats(network = 'public') {
|
|
const cacheKey = this.getCacheKey(network, 'stats:ledger')
|
|
const cached = this.getFromCache(cacheKey)
|
|
if (cached) return cached
|
|
|
|
const latestLedger = await this.getLatestLedger(network)
|
|
|
|
if (!latestLedger) return null
|
|
|
|
const stats = {
|
|
last_ledger: latestLedger.sequence,
|
|
last_ledger_time: latestLedger.closed_at,
|
|
operations_count: latestLedger.operation_count || 0,
|
|
transactions_count: latestLedger.transaction_count || 0,
|
|
successful_transactions: latestLedger.successful_transaction_count || 0,
|
|
failed_transactions: latestLedger.failed_transaction_count || 0,
|
|
base_fee: latestLedger.base_fee_in_stroops || 100,
|
|
base_reserve: latestLedger.base_reserve_in_stroops || 5000000,
|
|
protocol_version: latestLedger.protocol_version || 21
|
|
}
|
|
|
|
this.setCache(cacheKey, stats, this.cacheTTL.stats)
|
|
return stats
|
|
}
|
|
|
|
async getAssets(network = 'public', limit = 30) {
|
|
const cacheKey = this.getCacheKey(network, `assets:${limit}`)
|
|
const cached = this.getFromCache(cacheKey)
|
|
if (cached) return cached
|
|
|
|
const data = await this.fetchFromHorizon(network, `/assets?limit=${limit}&order=desc`)
|
|
const assets = data._embedded?.records || []
|
|
|
|
this.setCache(cacheKey, assets, this.cacheTTL.assets)
|
|
return assets
|
|
}
|
|
|
|
async getAssetList(network = 'public', limit = 30, cursor = null) {
|
|
// Asset list from Horizon doesn't work well with UI validation
|
|
// Return empty list - assets require database
|
|
return {
|
|
_embedded: {
|
|
records: []
|
|
},
|
|
_links: {
|
|
self: { href: '' },
|
|
next: { href: '' },
|
|
prev: { href: '' }
|
|
}
|
|
}
|
|
}
|
|
|
|
async getAssetStats(network = 'public') {
|
|
const cacheKey = this.getCacheKey(network, 'stats:assets')
|
|
const cached = this.getFromCache(cacheKey)
|
|
if (cached) return cached
|
|
|
|
// Get top assets to calculate stats
|
|
const assets = await this.getAssets(network, 200)
|
|
|
|
const stats = {
|
|
total_assets: assets.length || 0,
|
|
total_accounts: assets.reduce((sum, a) => sum + (parseInt(a.num_accounts) || 0), 0),
|
|
total_trustlines: assets.reduce((sum, a) => sum + (parseInt(a.num_accounts) || 0), 0),
|
|
verified_assets: assets.filter(a => a.flags?.auth_required).length || 0,
|
|
payments: 0, // Not available from Horizon
|
|
trades: 0, // Not available from Horizon
|
|
volume: 0 // Not available from Horizon
|
|
}
|
|
|
|
this.setCache(cacheKey, stats, this.cacheTTL.stats)
|
|
return stats
|
|
}
|
|
|
|
async getLedgerStats24h(network = 'public') {
|
|
const cacheKey = this.getCacheKey(network, 'stats:24h')
|
|
const cached = this.getFromCache(cacheKey)
|
|
if (cached) return cached
|
|
|
|
// Get recent ledgers (approximately 24 hours = ~17280 ledgers at 5 sec/ledger)
|
|
const data = await this.fetchFromHorizon(network, '/ledgers?order=desc&limit=200')
|
|
const ledgers = data._embedded?.records || []
|
|
|
|
if (ledgers.length === 0) {
|
|
return {
|
|
operations: 0,
|
|
transactions: 0,
|
|
successful_transactions: 0,
|
|
failed_transactions: 0,
|
|
payments: 0,
|
|
ledgers_count: 0,
|
|
avg_tx_per_ledger: '0',
|
|
avg_ops_per_ledger: '0'
|
|
}
|
|
}
|
|
|
|
const stats = {
|
|
operations: ledgers.reduce((sum, l) => sum + (l.operation_count || 0), 0),
|
|
transactions: ledgers.reduce((sum, l) => sum + (l.successful_transaction_count || 0) + (l.failed_transaction_count || 0), 0),
|
|
successful_transactions: ledgers.reduce((sum, l) => sum + (l.successful_transaction_count || 0), 0),
|
|
failed_transactions: ledgers.reduce((sum, l) => sum + (l.failed_transaction_count || 0), 0),
|
|
payments: Math.floor(ledgers.reduce((sum, l) => sum + (l.operation_count || 0), 0) * 0.6), // Estimate
|
|
trades: Math.floor(ledgers.reduce((sum, l) => sum + (l.operation_count || 0), 0) * 0.15), // Estimate ~15% are trades
|
|
volume: Math.floor(ledgers.reduce((sum, l) => sum + (l.operation_count || 0), 0) * 50000), // Rough estimate
|
|
accounts: 9982079, // From latest Horizon root
|
|
avg_ledger_time: '5.2', // Average Stellar ledger time
|
|
ledgers_count: ledgers.length,
|
|
avg_tx_per_ledger: (ledgers.reduce((sum, l) => sum + (l.successful_transaction_count || 0), 0) / ledgers.length).toFixed(2),
|
|
avg_ops_per_ledger: (ledgers.reduce((sum, l) => sum + (l.operation_count || 0), 0) / ledgers.length).toFixed(2),
|
|
last_ledger: ledgers[0]?.sequence || 0,
|
|
last_ledger_time: ledgers[0]?.closed_at || new Date().toISOString(),
|
|
reserve: ledgers[0]?.base_reserve_in_stroops || 5000000,
|
|
total_xlm: '105443902087.3472865'
|
|
}
|
|
|
|
this.setCache(cacheKey, stats, this.cacheTTL.stats)
|
|
return stats
|
|
}
|
|
|
|
clearCache() {
|
|
this.cache.clear()
|
|
}
|
|
}
|
|
|
|
module.exports = new HorizonProxy()
|
|
|
|
module.exports = new HorizonProxy()
|