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

144 lines
3.9 KiB
TypeScript

import { formatEther, parseEther, getAddress } from 'viem';
// Celo Mainnet Configuration
export const CELO_MAINNET = {
chainId: 42220,
name: 'Celo Mainnet',
rpcUrl: 'https://forno.celo.org',
blockExplorerUrl: 'https://explorer.celo.org',
};
// Celo Alfajores Testnet Configuration
export const CELO_ALFAJORES = {
chainId: 44787,
name: 'Celo Alfajores',
rpcUrl: 'https://alfajores-forno.celo-testnet.org',
blockExplorerUrl: 'https://alfajores-blockscout.celo-testnet.org',
};
// cUSD Contract Addresses
export const CUSD_ADDRESSES = {
mainnet: getAddress('0x765DE816845861e75A25fCA122bb6898B8B1282a'),
alfajores: getAddress('0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1'),
};
// Current environment configuration
const isMainnetEnv = process.env.NODE_ENV === 'production';
export const CUSD_ADDRESS = isMainnetEnv ? CUSD_ADDRESSES.mainnet : CUSD_ADDRESSES.alfajores;
export const CELO_RPC_URL = isMainnetEnv ? CELO_MAINNET.rpcUrl : CELO_ALFAJORES.rpcUrl;
export const BLOCK_EXPLORER_URL = isMainnetEnv ? CELO_MAINNET.blockExplorerUrl : CELO_ALFAJORES.blockExplorerUrl;
// cUSD ERC20 ABI
export const CUSD_ABI = [
{
constant: true,
inputs: [{ name: 'owner', type: 'address' }],
name: 'balanceOf',
outputs: [{ name: '', type: 'uint256' }],
payable: false,
stateMutability: 'view',
type: 'function',
},
{
constant: false,
inputs: [
{ name: 'spender', type: 'address' },
{ name: 'amount', type: 'uint256' }
],
name: 'approve',
outputs: [{ name: '', type: 'bool' }],
payable: false,
stateMutability: 'nonpayable',
type: 'function',
},
{
constant: true,
inputs: [
{ name: 'owner', type: 'address' },
{ name: 'spender', type: 'address' }
],
name: 'allowance',
outputs: [{ name: '', type: 'uint256' }],
payable: false,
stateMutability: 'view',
type: 'function',
},
{
constant: false,
inputs: [
{ name: 'to', type: 'address' },
{ name: 'amount', type: 'uint256' }
],
name: 'transfer',
outputs: [{ name: '', type: 'bool' }],
payable: false,
stateMutability: 'nonpayable',
type: 'function',
},
] as const;
// Utility functions
export function weiToCUSD(wei: bigint): number {
return parseFloat(formatEther(wei));
}
export function cUSDToWei(amount: string | number): bigint {
return parseEther(amount.toString());
}
export function formatCUSD(wei: bigint): string {
const amount = weiToCUSD(wei);
if (amount >= 1000) {
return `${(amount / 1000).toFixed(1)}K cUSD`;
}
if (amount >= 1) {
return `${amount.toFixed(2)} cUSD`;
}
return `${amount.toFixed(3)} cUSD`;
}
// Protocol configuration
export const PROTOCOL_CONFIG = {
// Protocol wallet address (to receive fees and distributions)
WALLET_ADDRESS: getAddress(process.env.PROTOCOL_WALLET_ADDRESS || '0x742d35Cc6634C0532925a3b844Bc454e4438f44e'),
// Vote amount options in cUSD
VOTE_OPTIONS: [
{ amount: 0.01, label: '0.01 cUSD', emoji: '馃獧' },
{ amount: 0.05, label: '0.05 cUSD', emoji: '馃挵' },
{ amount: 0.1, label: '0.1 cUSD', emoji: '馃挼' },
{ amount: 0.5, label: '0.5 cUSD', emoji: '馃捀' },
] as const,
// Protocol fees (as decimal percentages)
FEES: {
CREATION_FEE: 5.0, // 5 cUSD to create a poll
PROTOCOL_FEE_PERCENTAGE: 0.05, // 5% of total pot
WINNERS_PERCENTAGE: 0.95, // 95% to winners
},
// Minimum amounts (in cUSD)
MINIMUMS: {
VOTE: 0.01,
DISTRIBUTION: 0.001, // Minimum distribution to avoid dust
},
};
// Network detection
export function isMainnet(): boolean {
return isMainnetEnv;
}
export function getCurrentNetwork() {
return isMainnet() ? CELO_MAINNET : CELO_ALFAJORES;
}
// Transaction helpers
export function getExplorerUrl(txHash: string): string {
return `${BLOCK_EXPLORER_URL}/tx/${txHash}`;
}
export function getAddressUrl(address: string): string {
return `${BLOCK_EXPLORER_URL}/address/${address}`;
}