This project implements a decentralized kindness pool system using Hardhat and Solidity smart contracts. Users can contribute to or receive from a daily pool of Ether, promoting a culture of giving and receiving kindness.
For detailed business logic, please refer to BUSINESS_LOGIC.md.
Key features:
- 24-hour cycles for contributions and distributions
- User can contribute between 0.001 and 1 ETH
- Users can enter a receiver pool to potentially receive funds
- Automatic distribution at the end of each cycle
- Daily contribution and receiver limits
- Rate limiting and DoS protection
- Failed transfer handling and retry mechanisms
- Comprehensive security features
- Node.js (v14.0.0 or later)
- npm (v6.0.0 or later)
- Git
-
Clone the repository:
git clone https://github.com/your-repo/kindness-pool.git cd kindness-pool -
Install dependencies:
npm install
npm run compilenpm testnpm test -- test/Pool.test.ts
npm test -- test/DoSAttacks.test.ts
npm test -- test/DailyTracking.test.tsnpm run format
npm run format:checknpm run clean- Pool.sol - Main contract managing contributions, receivers, and distributions
- UserRegistry.sol - Manages user statistics and receiver pool status
- TimeBasedDistributor.sol - Handles time-based distribution logic
- Errors.sol - Custom error definitions for gas-efficient error handling
- Reentrancy Protection: Uses checks-effects-interactions pattern
- Rate Limiting: Action cooldowns and transaction limits
- DoS Protection: Gas limit protections and receiver limits
- Failed Transfer Handling: Robust retry mechanisms with exponential backoff
- Access Control: Role-based permissions for admin functions
- Contribution Limits: 5 ETH maximum per user per day
- Receiver Pool Limits: 1 entry/exit per user per day
- Transaction Limits: 10 transactions per user per day (Note: Currently has a reset bug)
- Action Cooldowns: 1 hour between general actions, 30 minutes between receiver pool actions
- Uses custom errors for gas efficiency
- Comprehensive error messages for debugging
- Failed transfer tracking and retry mechanisms
- Description: Contribute ETH to the daily pool
- Parameters:
amount: Amount to contribute (0.001 to 1 ETH)
- Requirements:
- Amount must be between MIN_KINDNESS_AMOUNT and MAX_KINDNESS_AMOUNT
- User must not exceed daily contribution limit (5 ETH)
- Respects transaction limits and cooldowns
- Description: Enter the receiver pool to potentially receive funds
- Requirements:
- User must not have contributed today
- User must not already be in receiver pool
- Respects daily entry limits and cooldowns
- Description: Exit the receiver pool
- Requirements:
- User must be in receiver pool
- Respects daily exit limits and cooldowns
- Description: Distribute the daily pool to receivers (admin only)
- Requirements:
- Must be within distribution window
- Must not have distributed today
- Pool must have minimum balance
- Returns:
bool- Whether current time is within distribution window
- Returns:
bool- Whether distribution has occurred today
- Returns:
uint256- Current number of receivers
- Returns:
uint256- Amount of unclaimed funds from failed transfers
MIN_KINDNESS_AMOUNT: 0.001 ETHMAX_KINDNESS_AMOUNT: 1 ETHMAX_DAILY_CONTRIBUTION: 5 ETHMAX_DAILY_RECEIVER_ENTRIES: 1MAX_DAILY_RECEIVER_EXITS: 1MAX_TRANSACTIONS_PER_DAY: 10ACTION_COOLDOWN: 1 hourRECEIVER_POOL_COOLDOWN: 30 minutesMAX_RECEIVERS: 100
KindnessGiven(address indexed giver, uint256 amount)KindnessReceived(address indexed receiver, uint256 amount)PoolDistributed(uint256 totalAmount, uint256 receiverCount)EnteredReceiverPool(address indexed receiver)LeftReceiverPool(address indexed receiver)TransferFailed(address indexed receiver, uint256 amount)TransferRetried(address indexed receiver, uint256 amount, bool success)
UserStatsUpdated(address indexed user, bool isGiving, uint256 amount, uint256 totalGiven, uint256 totalReceived, int256 netAmount)UserNameUpdated(address indexed user, string name)ReceiverPoolStatusUpdated(address indexed user, bool isInReceiverPool)
-
Start a local Hardhat node:
npx hardhat node
-
Deploy contracts to local network:
npx hardhat run scripts/deploy.ts --network localhost
-
Configure your environment variables in
.env:PRIVATE_KEY=your_private_key INFURA_API_KEY=your_infura_key ETHERSCAN_API_KEY=your_etherscan_key -
Deploy to testnet (e.g., Sepolia):
npx hardhat run scripts/deploy.ts --network sepolia
-
Verify contracts:
npx hardhat verify --network sepolia DEPLOYED_ADDRESS
- Use the same process as testnet but with mainnet network configuration
- Consider using a multisig wallet for admin functions
- Implement timelocks for critical parameter changes
- Transaction Count Reset Bug: The
transactionCountmapping is not reset in the daily reset mechanism, causing permanent lockout after 10 transactions - Gas Limit Considerations: Large receiver pools may approach gas limits during distribution
- Failed Transfer Handling: Failed transfers are tracked but require manual intervention for resolution
- Admin Role Management: Use multisig wallets for admin roles
- Emergency Procedures: Implement emergency pause mechanisms
- Monitoring: Monitor failed transfers and retry attempts
- Gas Price Management: Consider gas price fluctuations for distribution operations
The project includes comprehensive test suites:
- Pool.test.ts: Core functionality tests
- DoSAttacks.test.ts: Denial of service attack resistance tests
- DailyTracking.test.ts: Daily reset and limit tests
- UserRegistry.test.ts: User statistics and registry tests
- ReceiverPoolExit.test.ts: Receiver pool exit functionality tests
# Run all tests
npm test
# Run with gas reporting
npx hardhat test --gas-report
# Run specific test file
npm test -- test/DoSAttacks.test.ts
# Run tests with coverage
npx hardhat coverage- Fork the repository
- Create a feature branch
- Implement your changes with tests
- Ensure all tests pass
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
For questions and support, please open an issue on the GitHub repository.