Skip to content

feat: implement family wallet contract#31

Merged
Baskarayelu merged 2 commits intoRemitwise-Org:mainfrom
davedumto:feat-implement-family-wallet-contract
Jan 29, 2026
Merged

feat: implement family wallet contract#31
Baskarayelu merged 2 commits intoRemitwise-Org:mainfrom
davedumto:feat-implement-family-wallet-contract

Conversation

@davedumto
Copy link
Contributor

@davedumto davedumto commented Jan 23, 2026

Implement Family Wallet Smart Contract

Summary

This PR implements the Family Wallet smart contract that manages family members, roles, and spending limits for the RemitWise platform.

Closes #5

Changes

New Files

  • family_wallet/Cargo.toml - Package configuration
  • family_wallet/src/lib.rs - Contract implementation (320 lines)
  • family_wallet/src/test.rs - Comprehensive test suite (513 lines)

Modified Files

  • Cargo.toml - Added family_wallet to workspace members

Implementation Details

Core Functionality ✅

All required functions implemented:

  1. initialize(owner: Address) → bool

    • Initializes wallet with owner
    • Prevents re-initialization
  2. add_member(owner, address, name, spending_limit, role) → bool

    • Creates new family members
    • Updates existing members (no duplicates)
    • Validates spending_limit > 0
    • Tracks addresses for iteration
  3. get_member(address: Address) → Option<FamilyMember>

    • Retrieves member by address
    • Returns None if not found
  4. get_all_members() → Vec<FamilyMember>

    • Returns all family members
    • Uses efficient address tracking
  5. update_spending_limit(owner, address, new_limit) → bool

    • Updates member's spending limit
    • Returns false if member not found
    • Validates new_limit > 0
  6. check_spending_limit(address, amount) → bool

    • Checks if amount is within limit
    • Returns false if member not found or exceeds limit

Data Structures ✅

pub struct FamilyMember {
    pub address: Address,
    pub name: String,
    pub spending_limit: i128,
    pub role: String,
}

Storage: Map<Address, FamilyMember> + Vec<Address> for iteration

Security Features ✅

  • Access Control: Owner-based authorization using require_auth()
  • Input Validation: Rejects zero and negative spending limits
  • Storage TTL: Proper TTL management (1-day threshold, 30-day bump)
  • Audit Trail: Events emitted for all state changes (MemberAdded, MemberUpdated, SpendingLimitUpdated)
  • Error Handling: Explicit panic! with descriptive messages

Edge Cases Handled ✅

  • ✅ Member not found
  • ✅ Duplicate address (updates existing)
  • ✅ Zero spending limits (rejected)
  • ✅ Negative spending limits (rejected)
  • ✅ Large limits (tested with i128::MAX)
  • ✅ Unauthorized access (non-owner attempts)

Testing

Test Coverage: 20 tests, 100% passing ✅

Required Tests (8):

  • ✅ Test add_member creates new member
  • ✅ Test add_member updates existing member
  • ✅ Test get_member retrieves correct member
  • ✅ Test update_spending_limit updates correctly
  • ✅ Test check_spending_limit validates correctly
  • ✅ Test check_spending_limit with non-existent member
  • ✅ Test multiple members management
  • ✅ Test edge cases (zero limits, large limits)

Additional Tests for 100% Coverage (12):

  • ✅ Initialization tests (2)
  • ✅ Get member not found
  • ✅ Get all members (empty case)
  • ✅ Update spending limit (member not found, zero/negative)
  • ✅ Check spending limit (exceeds limit)
  • ✅ Authorization tests (non-owner cannot add/update)

Test Results

cargo test --package family_wallet
running 20 tests
test test::test_add_member_creates_new ... ok
test test::test_add_member_updates_existing ... ok
test test::test_get_member_found ... ok
test test::test_get_member_not_found ... ok
test test::test_get_all_members ... ok
test test::test_get_all_members_empty ... ok
test test::test_update_spending_limit_success ... ok
test test::test_update_spending_limit_member_not_found ... ok
test test::test_check_spending_limit_within_limit ... ok
test test::test_check_spending_limit_exceeds_limit ... ok
test test::test_check_spending_limit_member_not_found ... ok
test test::test_large_spending_limit ... ok
test test::test_add_member_zero_limit_fails - should panic ... ok
test test::test_add_member_negative_limit_fails - should panic ... ok
test test::test_update_spending_limit_zero_fails - should panic ... ok
test test::test_update_spending_limit_negative_fails - should panic ... ok
test test::test_non_owner_cannot_add_member - should panic ... ok
test test::test_non_owner_cannot_update_limit - should panic ... ok
test test::test_initialize ... ok
test test::test_initialize_twice_fails - should panic ... ok

test result: ok. 20 passed; 0 failed; 0 ignored; 0 measured

WASM Build ✅

cargo build --release --target wasm32-unknown-unknown --package family_wallet

Result: Build successful, artifact at target/wasm32-unknown-unknown/release/family_wallet.wasm (6.3KB)

Acceptance Criteria

All requirements met:

  • ✅ Contract compiles without errors
  • ✅ All functions implemented as specified
  • ✅ Comprehensive test suite (100% coverage)
  • ✅ Documentation comments for all functions
  • ✅ Address-based storage works correctly
  • ✅ Edge cases handled properly
  • ✅ Gas optimization considered (efficient Map storage)
  • ✅ Code follows Soroban best practices (matches existing contracts)

Pattern Consistency

This implementation follows the exact same patterns as existing contracts in the repository:

Pattern Implementation
Storage TTL Same constants and helper function
Authorization require_auth() + owner verification
Input Validation Explicit checks with descriptive panics
Event Emissions Events for all state-changing operations
Error Handling panic! with clear messages
Data Structures #[contracttype] with owner tracking

Notes

  • Added 12 extra tests beyond the 8 required to achieve true 100% code coverage
  • Extra tests cover authorization failures, all edge cases in update functions, and initialization logic
  • This ensures production-ready code with no untested paths
  • Contract is ready for deployment to Stellar testnet/mainnet

##Screenshots
Screenshot 2026-01-23 at 1 14 28 PM

How to Test

# Run tests
cargo test --package family_wallet

# Build WASM
cargo build --release --target wasm32-unknown-unknown --package family_wallet

# Run all workspace tests
cargo test

@Baskarayelu
Copy link
Contributor

@davedumto Please fix the pipeline

@davedumto
Copy link
Contributor Author

hey @Baskarayelu I have fixed it

@davedumto
Copy link
Contributor Author

hi @Baskarayelu I am still waiting

@Baskarayelu Baskarayelu merged commit 3c9e991 into Remitwise-Org:main Jan 29, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Family Wallet Contract

2 participants