story-research-zapwall/lib/fundingCalculation.ts
Nicolas Cantu 2a191f35f4 Fix all TypeScript errors and warnings
- Fix unused function warnings by renaming to _unusedExtractTags
- Fix type errors in nostrTagSystem.ts for includes() calls
- Fix type errors in reviews.ts for filter kinds array
- Fix ArrayBuffer type errors in articleEncryption.ts
- Remove unused imports (DecryptionKey, decryptArticleContent, extractTagsFromEvent)
- All TypeScript checks now pass without disabling any controls
2025-12-27 22:26:13 +01:00

96 lines
3.4 KiB
TypeScript

/**
* Calculate total platform funds collected
* Aggregates all commission types: articles, reviews, sponsoring
*/
import { PLATFORM_COMMISSIONS } from './platformCommissions'
import { aggregateZapSats } from './zapAggregation'
const FUNDING_TARGET_BTC = 0.27
const FUNDING_TARGET_SATS = FUNDING_TARGET_BTC * 100_000_000
export interface FundingStats {
totalSats: number
totalBTC: number
targetBTC: number
targetSats: number
progressPercent: number
}
/**
* Calculate total platform funds from all sources
* This is an approximation based on commission rates
* Actual calculation would require querying all transactions
*/
export async function calculatePlatformFunds(_authorPubkeys: string[]): Promise<FundingStats> {
let totalSats = 0
// Calculate article commissions (from zap receipts with kind_type: purchase)
// Each article payment is 800 sats, platform gets 100 sats
// This is an approximation - in reality we'd query all zap receipts
try {
const articleCommissions = await aggregateZapSats({
authorPubkey: '', // Empty to get all
kindType: 'purchase',
})
// Estimate: assume 100 sats commission per purchase (800 total, 700 author, 100 platform)
// This is simplified - actual calculation would need to track each payment
totalSats += Math.floor(articleCommissions * (PLATFORM_COMMISSIONS.article.platform / PLATFORM_COMMISSIONS.article.total))
} catch (e) {
console.error('Error calculating article commissions:', e)
}
// Calculate review commissions (from zap receipts with kind_type: review_tip)
// Each review tip is 70 sats, platform gets 21 sats
try {
const reviewCommissions = await aggregateZapSats({
authorPubkey: '', // Empty to get all
kindType: 'review_tip',
})
// Estimate: assume 21 sats commission per review tip (70 total, 49 reviewer, 21 platform)
totalSats += Math.floor(reviewCommissions * (PLATFORM_COMMISSIONS.review.platform / PLATFORM_COMMISSIONS.review.total))
} catch (e) {
console.error('Error calculating review commissions:', e)
}
// Calculate sponsoring commissions (from zap receipts with kind_type: sponsoring)
// Each sponsoring is 0.046 BTC, platform gets 0.004 BTC (400,000 sats)
try {
const sponsoringCommissions = await aggregateZapSats({
authorPubkey: '', // Empty to get all
kindType: 'sponsoring',
})
// Estimate: assume 400,000 sats commission per sponsoring (4,600,000 total, 4,200,000 author, 400,000 platform)
totalSats += Math.floor(sponsoringCommissions * (PLATFORM_COMMISSIONS.sponsoring.platformSats / PLATFORM_COMMISSIONS.sponsoring.totalSats))
} catch (e) {
console.error('Error calculating sponsoring commissions:', e)
}
const totalBTC = totalSats / 100_000_000
const progressPercent = Math.min(100, (totalBTC / FUNDING_TARGET_BTC) * 100)
return {
totalSats,
totalBTC,
targetBTC: FUNDING_TARGET_BTC,
targetSats: FUNDING_TARGET_SATS,
progressPercent,
}
}
/**
* Simplified version that estimates based on known commission rates
* For a more accurate calculation, we'd need to track all transactions
*/
export function estimatePlatformFunds(): FundingStats {
// This is a placeholder - actual implementation would query all transactions
// For now, return 0 as we need to implement proper tracking
return {
totalSats: 0,
totalBTC: 0,
targetBTC: FUNDING_TARGET_BTC,
targetSats: FUNDING_TARGET_SATS,
progressPercent: 0,
}
}