celo-poll/scripts/distribute.ts
2025-12-08 16:31:30 +03:00

134 lines
3.9 KiB
TypeScript

/**
* Distribution Script for PollCelo Winners
*
* This script helps you manually distribute winnings to poll winners.
* Run this daily after a poll closes to pay out winners.
*
* Usage:
* npx tsx scripts/distribute.ts <pollId>
*
* Example:
* npx tsx scripts/distribute.ts poll-2025-01-15
*/
import { getPollById, getPollVotes } from '@/lib/db/polls'
import { formatCUSD } from '@/lib/format'
interface WinnerPayout {
userId: string
address?: string
voteAmount: number
winnings: number
percentage: number
}
async function calculateDistribution(pollId: string): Promise<WinnerPayout[]> {
// Get poll
const poll = await getPollById(pollId)
if (!poll) {
throw new Error(`Poll ${pollId} not found`)
}
if (poll.status !== 'closed') {
throw new Error(`Poll ${pollId} is not closed yet`)
}
if (!poll.correctOptionId) {
throw new Error(`Poll ${pollId} has no correct answer set`)
}
console.log(`\n๐Ÿ“Š Poll: ${poll.question}`)
console.log(`โœ… Correct answer: ${poll.options.find(o => o.id === poll.correctOptionId)?.text}`)
console.log(`๐Ÿ’ฐ Total pot: ${formatCUSD(poll.totalPot)}`)
console.log(`๐Ÿ† Winners pot (95%): ${formatCUSD(poll.totalPot * 0.95)}`)
console.log(`๐Ÿ’ธ Protocol fee (5%): ${formatCUSD(poll.totalPot * 0.05)}`)
console.log(`\n`)
// Get all votes
const votes = await getPollVotes(pollId)
// Filter winning votes
const winningVotes = votes.filter(v => v.optionId === poll.correctOptionId)
if (winningVotes.length === 0) {
console.log('โš ๏ธ No winners for this poll!')
return []
}
// Calculate total winning amount
const totalWinningAmount = winningVotes.reduce((sum, v) => sum + v.amount, 0)
const winnersPot = poll.totalPot * 0.95
// Calculate individual payouts
const payouts: WinnerPayout[] = winningVotes.map(vote => {
const percentage = (vote.amount / totalWinningAmount) * 100
const winnings = (vote.amount / totalWinningAmount) * winnersPot
return {
userId: vote.userId,
voteAmount: vote.amount,
winnings,
percentage,
}
})
// Sort by winnings descending
payouts.sort((a, b) => b.winnings - a.winnings)
return payouts
}
async function printDistribution(pollId: string) {
try {
const payouts = await calculateDistribution(pollId)
if (payouts.length === 0) {
console.log('No distribution needed.')
return
}
console.log(`๐Ÿ“‹ Distribution Summary (${payouts.length} winners):\n`)
console.log('โ”€'.repeat(80))
console.log('User ID'.padEnd(20) + 'Voted'.padEnd(15) + 'Share'.padEnd(10) + 'Winnings')
console.log('โ”€'.repeat(80))
let totalPayout = 0
payouts.forEach((payout, index) => {
const userDisplay = payout.userId.padEnd(20)
const voteDisplay = formatCUSD(payout.voteAmount).padEnd(15)
const shareDisplay = `${payout.percentage.toFixed(2)}%`.padEnd(10)
const winningsDisplay = formatCUSD(payout.winnings)
console.log(`${userDisplay}${voteDisplay}${shareDisplay}${winningsDisplay}`)
totalPayout += payout.winnings
})
console.log('โ”€'.repeat(80))
console.log(`Total payout: ${formatCUSD(totalPayout)}\n`)
console.log('๐Ÿ’ก Next Steps:')
console.log('1. Review the distribution above')
console.log('2. Get wallet addresses for each user (from Farcaster FID)')
console.log('3. Send cUSD to each winner using their wallet address')
console.log('4. Keep 5% protocol fee in your wallet')
console.log('\n๐Ÿ“ Example transaction (using Celo CLI):')
console.log(' celocli transfer:dollars --from $YOUR_ADDRESS --to $WINNER_ADDRESS --value 0.5')
console.log('\n')
} catch (error) {
console.error('โŒ Error:', error)
process.exit(1)
}
}
// Main execution
const pollId = process.argv[2]
if (!pollId) {
console.error('Usage: npx tsx scripts/distribute.ts <pollId>')
console.error('Example: npx tsx scripts/distribute.ts poll-2025-01-15')
process.exit(1)
}
printDistribution(pollId)