A Clarity smart contract for managing shared utility expenses among roommates on the Stacks blockchain. This contract enables transparent tracking of bills, payments, and balances in shared living arrangements.
- Create houses with unique identifiers
- Each house has an owner who manages members
- Track active/inactive status of houses
- Add roommates to houses with custom names
- Remove roommates when they move out
- Track join dates and member status
- Owner has administrative control
- Create bills with descriptions (e.g., "Electric", "Water", "Internet")
- Automatically split bills among specified number of roommates
- Track who paid the bill initially
- Record individual payment status
- Real-time balance tracking for each member
- Automatic balance updates when payments are recorded
- Manual balance adjustments by house owner
- Track who owes money and who is owed
(create-house (name (string-ascii 50)))Creates a new house and automatically adds the creator as owner and first member.
- Returns: House ID
- Access: Anyone
(add-roommate (house uint) (member principal) (name (string-ascii 50)))Adds a new roommate to the house.
- Returns:
trueon success - Access: House owner only
(remove-roommate (house uint) (member principal))Removes a roommate from the house (sets status to inactive).
- Returns:
trueon success - Access: House owner only
(create-bill (house uint) (description (string-ascii 100)) (total uint) (split uint))Creates a new bill to be split among roommates.
- Parameters:
house: House IDdescription: Bill description (e.g., "Electric - January")total: Total amount in microSTXsplit: Number of people to split among
- Returns: Bill ID
- Access: House members only
(record-payment (bill uint) (member principal))Records that a member has paid their share of a bill. Automatically updates balances.
- Returns:
trueon success - Access: House members only
(settle-bill (bill uint))Marks a bill as fully settled.
- Returns:
trueon success - Access: Bill creator only
(update-balance (house uint) (member principal) (amount uint) (add bool))Manually adjusts a member's balance.
- Parameters:
add:trueto add to balance,falseto subtract
- Returns:
trueon success - Access: House owner only
get-house- Retrieve house informationget-roommate- Get roommate detailsget-bill- Retrieve bill informationget-payment-status- Check if a member paid a specific billget-member-balance- Get member's current balanceget-bill-counter- Get total number of bills createdis-roommate- Check if address is an active member
(contract-call? .roommate-splitter create-house "Apartment 5B")
;; Returns: (ok u1)(contract-call? .roommate-splitter add-roommate u1 'SP2... "Alice")
(contract-call? .roommate-splitter add-roommate u1 'SP3... "Bob");; Electric bill of 300 STX split 3 ways
(contract-call? .roommate-splitter create-bill u1 "Electric - Oct 2025" u300000000 u3)
;; Returns: (ok u1) - Bill ID 1, each person owes 100 STX(contract-call? .roommate-splitter record-payment u1 'SP2...)
(contract-call? .roommate-splitter record-payment u1 'SP3...)(contract-call? .roommate-splitter get-member-balance u1 'SP2...)
;; Returns balance (negative = owes money, positive = is owed money)| Code | Constant | Description |
|---|---|---|
| u100 | err-owner-only |
Only house owner can perform this action |
| u101 | err-not-member |
Address is not a house member |
| u102 | err-already-member |
Member already exists in house |
| u103 | err-invalid-amount |
Bill amount must be greater than 0 |
| u104 | err-already-paid |
Payment already recorded |
| u105 | err-bill-not-found |
Bill ID doesn't exist |
| u106 | err-insufficient-balance |
Insufficient balance for operation |
| u107 | err-no-roommates |
No roommates in house |
| u108 | err-invalid-split |
Split count must be greater than 0 |
- Access Control: Owner-only functions for sensitive operations
- Validation: Input validation on all parameters
- Member Verification: Only active members can create bills and record payments
- Duplicate Prevention: Prevents duplicate payment records
- Safe Math: Balance calculations prevent underflow (minimum balance is 0)
- Bill Creation: Always specify accurate split counts
- Payment Recording: Record payments promptly to keep balances accurate
- Regular Settlements: Use
settle-billto mark completed bills - Balance Checks: Regularly check balances to ensure fairness
- Member Management: Remove inactive roommates to keep data clean
Deploy this contract to testnet before mainnet:
clarinet contract deploy roommate-splitterRun tests:
clarinet test