Skip to content

Conversation

@davedumto
Copy link
Contributor

@davedumto davedumto commented May 30, 2025

🎯 Implement Standalone FundRequest Smart Contract

closes #99

📚 Overview

This PR implements a comprehensive standalone FundRequest smart contract in Cairo for the BudgetChain project. The contract manages fund requests tied to project milestones with robust validation, access controls, and automatic budget updates.


✅ Issue Requirements Satisfied

1. Data Structures & Storage

  • FundRequest struct with all required fields:

    • project_id: u64
    • milestone_id: u64
    • amount: u128
    • requester: ContractAddress
    • status: FundRequestStatus (Pending, Approved, Rejected)
  • Events defined:

    • FundsRequested(project_id, request_id, milestone_id)
    • FundsReleased(project_id, request_id, milestone_id, amount)
    • FundsReturned(project_id, amount, project_owner)
  • Storage setup includes:

    • Fund request mappings
    • Counters
    • Existence tracking

2. Function Implementation

  • create_fund_request: Project owners can create requests only for completed milestones
  • approve_fund_request: Authorized approvers can approve requests and release funds
  • reject_fund_request: Authorized approvers can reject pending requests
  • Automatic project budget updates upon approval

3. Validation & Access Control

  • Milestone completion verification
  • Multi-role access control:
    • Project owners: Create fund requests
    • Authorized approvers: Approve/reject requests
    • Contract owner: Manage approver permissions
  • Request status validation (only pending requests can be modified)
  • Input validation: Amount checks, zero address protection

4. Event Emission

  • FundsRequested: Emitted when a request is created
  • FundsReleased: Emitted when funds are released
  • FundsReturned: Defined and ready for future use

🏗️ Technical Implementation

Core Architecture

trait IFundRequest<TContractState> {
    fn create_fund_request(ref self: TContractState, project_id: u64, milestone_id: u64, amount: u128) -> u64;
    fn approve_fund_request(ref self: TContractState, request_id: u64);
    fn reject_fund_request(ref self: TContractState, request_id: u64);
    // ... additional admin and getter functions
}

trait IProjectContract<TContractState> {
    fn is_milestone_completed(self: @TContractState, project_id: u64, milestone_id: u64) -> bool;
    fn get_project_owner(self: @TContractState, project_id: u64) -> ContractAddress;
    fn update_project_budget(ref self: TContractState, project_id: u64, amount: u128);
}

Security Features

  • 🔒 Multi-layer authorization with role-based control
  • 🛡️ Storage safety via existence tracking
  • ⚡ Comprehensive input validation
  • 🔐 Status protection to prevent invalid transitions

Key Technical Decisions

  • Storage optimization via request_exists mapping
  • Modular access control system
  • Event-driven architecture
  • Interface segregation for clarity

🧪 Testing Coverage

Core Functionality

  • ✅ Contract deployment and initialization
  • ✅ Fund request creation with validation
  • ✅ Approval and rejection workflows
  • ✅ Status transitions

Security Tests

  • ✅ Zero address validation
  • ✅ Unauthorized access checks
  • ✅ Invalid amount handling
  • ✅ Non-existent request handling

Access Control

  • ✅ Authorized approver management
  • ✅ Owner-only function enforcement
  • ✅ Project owner verification
  • ✅ Multi-role permissions

Edge Cases

  • ✅ Request existence verification
  • ✅ Status transition validation
  • ✅ Uninitialized data protection

Test Results:

  • Tests run: 14
  • Passed: 14
  • Failed: 0
  • Skipped: 0

📁 Files Added/Modified

New Files:

  • src/fund_request.cairo: Main contract
  • src/lib.cairo: Interfaces & exports
  • tests/test_fund_request.cairo: Full test suite
  • scripts/deploy.sh: Deployment automation

Configuration:

  • Scarb.toml: Cairo project setup
  • snfoundry.toml: Test configuration

🚀 Integration Points

External Dependencies

  • Validates milestone completion using IProjectContract
  • Ensures only project owners create requests
  • Updates budgets upon approval

Event Integration

  • Emits events for state changes
  • Supports frontend, auditing, and notifications

🛡️ Security Considerations

Access Control Matrix

Function Project Owner Authorized Approver Contract Owner Public
create_fund_request
approve_fund_request
reject_fund_request
add_authorized_approver
Getters

Validation Layers

  • Input validation on all parameters
  • Authorization checks on all sensitive actions
  • Status & state validations
  • Business logic enforcement (milestone & ownership checks)

📋 Deployment Instructions

Build the contract:

cd budgetchain-app/onchain/budget_contract
scarb build

Run tests:

snforge test

🔄 Future Enhancements

  • Fund return functionality (FundsReturned event prepared)
  • Batch request processing
  • Multi-signature approval flows
  • Time-based request expiration handling

✅ Checklist

  • All issue requirements implemented
  • Comprehensive test coverage (14 passing tests)
  • Security considerations addressed
  • Documentation and comments added
  • Integration interfaces defined
  • Deployment scripts included
  • Error handling implemented
  • Events emitted for all state changes
  • Access control fully implemented
  • Cairo best practices followed

🎯 Impact

This implementation provides BudgetChain with:

  • Secure fund management tied to project milestones
  • Full audit trail via comprehensive events
  • Flexible, role-based authorization system
  • Production-ready smart contract with full test coverage
  • Seamless integration with project management systems

##Screenshots

Screenshot 2025-05-30 at 16 21 05

@vercel
Copy link

vercel bot commented May 30, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
budget-chain ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 30, 2025 4:32pm

@anonfedora anonfedora merged commit c507601 into BudgetChain:main Jun 3, 2025
3 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 Fund Request Contract for Project Milestones

2 participants