story-research-zapwall/lib/fundingCalculation.ts

99 lines
3.0 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
*/
async function calculateArticleCommissions(): Promise<number> {
try {
const articleCommissions = await aggregateZapSats({
authorPubkey: '', // Empty to get all
kindType: 'purchase',
})
return Math.floor(articleCommissions * (PLATFORM_COMMISSIONS.article.platform / PLATFORM_COMMISSIONS.article.total))
} catch (e) {
console.error('Error calculating article commissions:', e)
return 0
}
}
async function calculateReviewCommissions(): Promise<number> {
try {
const reviewCommissions = await aggregateZapSats({
authorPubkey: '', // Empty to get all
kindType: 'review_tip',
})
return Math.floor(reviewCommissions * (PLATFORM_COMMISSIONS.review.platform / PLATFORM_COMMISSIONS.review.total))
} catch (e) {
console.error('Error calculating review commissions:', e)
return 0
}
}
async function calculateSponsoringCommissions(): Promise<number> {
try {
const sponsoringCommissions = await aggregateZapSats({
authorPubkey: '', // Empty to get all
kindType: 'sponsoring',
})
return Math.floor(sponsoringCommissions * (PLATFORM_COMMISSIONS.sponsoring.platformSats / PLATFORM_COMMISSIONS.sponsoring.totalSats))
} catch (e) {
console.error('Error calculating sponsoring commissions:', e)
return 0
}
}
export async function calculatePlatformFunds(_authorPubkeys: string[]): Promise<FundingStats> {
const [articleCommissions, reviewCommissions, sponsoringCommissions] = await Promise.all([
calculateArticleCommissions(),
calculateReviewCommissions(),
calculateSponsoringCommissions(),
])
const totalSats = articleCommissions + reviewCommissions + sponsoringCommissions
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,
}
}