Successfully implemented a comprehensive fund distribution system for organizations that allows equal distribution of funds to all members when the organization ends.
Added to OrganisationModel:
distributionCompleted(boolean) - Tracks if funds have been distributeddistributionDate(timestamp) - When distribution occurreddistributionTxHash(varchar) - Blockchain transaction hash(es)
DistributionHandler (server/handlers/DistributionHandler.js):
distributeOrganisationFunds()- Distributes funds equally to all membersgetDistributionStatus()- Returns distribution status for an organizationcanDistribute()- Checks if user has permission to distributecheckAndDistributeExpired()- Finds expired orgs for cron job
API Endpoints (/api/distributions):
POST /organisation/:id/distribute- Trigger distribution (owner only)GET /organisation/:id/status- Get distribution statusGET /organisation/:id/can-distribute- Check distribution permission
OrganisationPage Updates:
- "End Organization" Button - Visible only to owners when:
- User is the organization owner
- Funds haven't been distributed yet
- Organization has funds to distribute
- Distribution Status Badge - Shows "✓ FUNDS DISTRIBUTED" when complete
- Confirmation Modal - Shows:
- Total funds to distribute
- Number of recipients
- Amount per member
- Warning that action cannot be undone
DistributionAPI (client/src/ApiFactory/DistributionAPI.js):
distributeOrganisationFunds(orgId)- Trigger distributiongetDistributionStatus(orgId)- Get statuscanDistribute(orgId)- Check permission
// Equal distribution calculation
const totalFunds = BigInt(organisation.addedFunds);
const sharePerMember = totalFunds / BigInt(members.length);
// Transfer to each member
for (const member of members) {
await ctkToken.transfer(member.userAddress, sharePerMember);
}For Organization Owners:
- Navigate to organization page
- See "END ORGANIZATION" button (if funds exist and not distributed)
- Click button → Confirmation modal appears
- Modal shows:
- Total funds: e.g., "100 CTK"
- Recipients: e.g., "5 members"
- Amount per member: e.g., "20 CTK"
- Click "Confirm & Distribute"
- Funds transferred to all members
- Button changes to "✓ FUNDS DISTRIBUTED"
For Members:
- See distribution status badge when funds are distributed
- Receive equal share of organization funds
- Can verify transaction on blockchain
- Permission Control: Only owner can trigger distribution
- Idempotency:
distributionCompletedflag prevents double distribution - Transaction Safety: All transfers wrapped in try-catch
- Balance Verification: Checks owner has sufficient balance before distributing
- Audit Trail: Records all transaction hashes and timestamps
- All funds in
organisation.addedFunds - Includes investments from all investors
- Split equally among ALL members (regardless of role)
- All active organization members
- Includes owners, users, and any other roles
- Members who left (leftDate !== null) are excluded
- Uses CTKToken contract's
transferfunction - Oracle wallet executes transfers
- All transactions recorded on-chain
- Transaction hashes stored in database
- Set up cron job to run
checkAndDistributeExpired()daily - Automatically distribute funds when
endDatepasses - Requires cron service setup
- Weighted Distribution: Distribute based on contribution or role
- Partial Distribution: Allow distributing specific amounts
- Distribution History: Track all distributions per organization
- Email Notifications: Notify members when funds are distributed
server/models/OrganisationModel.js- Added distribution fieldsserver/handlers/DistributionHandler.js- Distribution logic (NEW)server/routes/DistributionRouter.js- API routes (NEW)server/routes/IndexRouter.js- Added distribution routerserver/apply_distribution_migration.js- Migration script (NEW)
client/src/ApiFactory/DistributionAPI.js- API client (NEW)client/src/pages/OrganisationPage.jsx- UI updates
- Create test organization with funds
- Add multiple members
- Click "End Organization" button
- Verify modal shows correct calculations
- Confirm distribution
- Verify all members received equal amounts
- Check distribution status updates
- Verify button changes to "FUNDS DISTRIBUTED"
- Test permission (non-owner cannot distribute)
- Test duplicate prevention (cannot distribute twice)
The organization fund distribution system is fully implemented and ready for use. Owners can now easily end their organizations and distribute funds equally to all members with a single click. The system includes comprehensive security measures, blockchain integration, and a user-friendly interface.