import { Address } from 'viem' import { AssembleClientConfig, PlatformFeeStats, ReferrerStats } from '../types' import { ValidationError } from '../errors' import { validateAddress } from '../utils' /** * Manager for platform fee operations */ export class PlatformFeeManager { constructor(private config: AssembleClientConfig) {} /** * Calculate platform fee for a given amount */ calculatePlatformFee(amount: bigint, feeBps: number): bigint { if (feeBps < 0 || feeBps > 10000) { throw new ValidationError('Fee basis points must be between 0 and 10000') } return (amount * BigInt(feeBps)) / 10000n } /** * Calculate total price including platform fee */ calculateTotalPrice(basePrice: bigint, platformFeeBps?: number): bigint { if (!platformFeeBps || platformFeeBps === 0) { return basePrice } const platformFee = this.calculatePlatformFee(basePrice, platformFeeBps) return basePrice + platformFee } /** * Get platform fee earnings for a referrer (ETH) * Note: This requires tracking via event logs or off-chain indexing */ async getPlatformFeeEarnings(referrer: Address): Promise { validateAddress(referrer, 'referrer') // This would require: // 1. Filter all ticket purchase/tip events with platform fees // 2. Sum up fees earned by this referrer // 3. Account for any claimed fees console.warn('getPlatformFeeEarnings requires event log aggregation - returning 0 for now') return 0n } /** * Get platform fee earnings for a referrer for specific ERC20 token * Note: This requires tracking via event logs or off-chain indexing */ async getPlatformFeeEarningsERC20(referrer: Address, token: Address): Promise { validateAddress(referrer, 'referrer') validateAddress(token, 'token') // This would require: // 1. Filter all ERC20 ticket purchase/tip events with platform fees // 2. Filter by token address // 3. Sum up fees earned by this referrer // 4. Account for any claimed fees console.warn('getPlatformFeeEarningsERC20 requires event log aggregation - returning 0 for now') return 0n } /** * Get comprehensive platform fee statistics for a referrer */ async getTotalPlatformFeesGenerated(referrer: Address): Promise { validateAddress(referrer, 'referrer') // This would require comprehensive event log analysis // For now, returning zero stats console.warn('getTotalPlatformFeesGenerated requires event log aggregation - returning zero stats for now') return { totalEarnings: 0n, totalTransactions: 0, averageFee: 0n, } } /** * Get top referrers by platform fees earned */ async getTopReferrers(limit: number = 10): Promise { if (limit <= 0 || limit > 100) { throw new ValidationError('Limit must be between 1 and 100') } // This would require: // 1. Aggregate all platform fee events // 2. Group by referrer address // 3. Calculate totals and rank // 4. Return top N console.warn('getTopReferrers requires comprehensive event log aggregation - returning empty array for now') return [] } /** * Calculate platform fee percentage from basis points */ bpsToPercentage(bps: number): number { return bps / 100 } /** * Calculate basis points from percentage */ percentageToBps(percentage: number): number { if (percentage < 0 || percentage > 100) { throw new ValidationError('Percentage must be between 0 and 100') } return Math.round(percentage * 100) } /** * Validate platform fee basis points */ validatePlatformFeeBps(feeBps: number): boolean { return feeBps >= 0 && feeBps <= 500 // Max 5% platform fee } /** * Get default platform fee for the protocol * Note: This would typically be stored in the contract */ async getDefaultPlatformFee(): Promise { // For now, return a default value - could be enhanced to read from contract return 250 // 2.5% default platform fee } /** * Estimate total cost with platform fee */ estimateTotalCost(basePrice: bigint, quantity: number, platformFeeBps?: number): { baseTotal: bigint platformFee: bigint grandTotal: bigint } { const baseTotal = basePrice * BigInt(quantity) const platformFee = platformFeeBps ? this.calculatePlatformFee(baseTotal, platformFeeBps) : 0n const grandTotal = baseTotal + platformFee return { baseTotal, platformFee, grandTotal, } } /** * Check if a platform fee rate is reasonable */ isReasonableFeeRate(feeBps: number): boolean { // Consider fees between 0.5% and 5% as reasonable return feeBps >= 50 && feeBps <= 500 } }