A professional and secure lending smart contract built with Rust using the ink! framework for Substrate-based blockchains.
- Loan Creation: Users can create loan requests with specified amounts, interest rates, and collateral
- Loan Funding: Lenders can fund pending loans and earn interest
- Loan Repayment: Borrowers can repay loans with interest
- Early Repayment with Discount: Save 1-5% on interest by repaying early
- Partial Repayment: Pay off loans in multiple smaller installments
- Loan Extension: Extend loan duration with configurable fees
- Late Payment Penalties: Automatic late fees for overdue loans
- Loan Refinancing: Refinance loans with better terms and lower rates
- Variable Interest Rates: Dynamic rates with risk-based pricing
- Collateral Management: Secure collateral system with configurable ratios
- User Profiles: Track user borrowing/lending history and credit scores
- Event System: Comprehensive event logging for all operations
- Security Features: Input validation, access control, and error handling
For a comprehensive overview of planned features and development phases, see FEATURE_ROADMAP.md.
Current Phase: Core contract implementation ✅
Next Phase: Enhanced lending features (early repayment, partial payments, loan extensions)
The roadmap includes 8 development phases covering:
- Core lending enhancements
- Advanced financial features
- Liquidity pool management
- Risk management & security
- DeFi integrations
- Performance optimizations
lending-smart-contract/
├── src/
│ ├── lib.rs # Main library entry point
│ ├── lending_contract.rs # Core lending contract implementation
│ ├── types.rs # Data structures and types
│ └── errors.rs # Custom error definitions
├── tests/
│ └── lending_contract_tests.rs # Test suite (requires updates for ink! 5.x)
├── examples/
│ ├── basic_usage.rs # Basic usage examples
│ └── advanced_features.rs # Advanced features demonstration
├── scripts/
│ └── build.sh # Build automation script
├── Cargo.toml # Project dependencies and configuration
├── README.md # This file
└── FEATURE_ROADMAP.md # Comprehensive feature development roadmap
- Rust 1.70.0 or later
- Cargo package manager
- ink! 5.x toolchain
- Clone the repository:
git clone https://github.com/tkmy401/lending-smart-contract.git
cd lending-smart-contract- Build the contract:
cargo build✅ Core Contract: Successfully compiles and builds
🔧 Dependencies: Updated to use ink! 5.1.1 with compatible crates
The project uses the following key dependencies:
ink = "5.1.1"- Core ink! frameworkparity-scale-codec = "3.6.0"- SCALE encoding/decodingscale-info = "2.11.6"- Type information for ink! contracts
After building, you can deploy the contract to a Substrate-based blockchain:
# Build the contract
cargo build --release
# The compiled contract will be available in target/ink/create_loan(
amount: Balance, // Loan amount
interest_rate: u16, // Interest rate in basis points (500 = 5%)
duration: u64, // Loan duration in blocks
collateral: Balance // Collateral amount
) -> Result<u64, LendingError>fund_loan(loan_id: u64) -> Result<(), LendingError>repay_loan(loan_id: u64) -> Result<(), LendingError>early_repay_loan(loan_id: u64) -> Result<(), LendingError>partial_repay_loan(loan_id: u64) -> Result<(), LendingError>extend_loan(loan_id: u64, extension_duration: u64) -> Result<(), LendingError>apply_late_fees(loan_id: u64) -> Result<(), LendingError>refinance_loan(loan_id: u64, new_interest_rate: u16, new_duration: u64) -> Result<(), LendingError>adjust_interest_rate(loan_id: u64, new_base_rate: u16, reason: RateAdjustmentReason) -> Result<(), LendingError>
update_risk_multiplier(loan_id: u64, new_risk_multiplier: u16) -> Result<(), LendingError>
convert_to_variable_rate(loan_id: u64, new_base_rate: u16) -> Result<(), LendingError>get_loan(loan_id: u64) -> Option<Loan>
get_user_profile(user: AccountId) -> Option<UserProfile>
get_total_loans() -> u64
get_total_liquidity() -> Balance
get_early_repayment_discount(loan_id: u64) -> Result<u16, LendingError>
get_loan_payment_info(loan_id: u64) -> Result<(Balance, Balance, Vec<PartialPayment>), LendingError>
get_partial_payment_count(loan_id: u64) -> Result<u32, LendingError>
get_loan_extension_info(loan_id: u64) -> Result<(u32, u32, u16), LendingError>
can_extend_loan(loan_id: u64) -> Result<bool, LendingError>
calculate_extension_fee(loan_id: u64) -> Result<Balance, LendingError>
get_late_fee_info(loan_id: u64) -> Result<(Balance, u16, u16, Option<u64>), LendingError>
calculate_current_late_fees(loan_id: u64) -> Result<Balance, LendingError>
is_loan_overdue(loan_id: u64) -> Result<bool, LendingError>
get_loan_refinance_info(loan_id: u64) -> Result<(u32, u32, u16), LendingError>
can_refinance_loan(loan_id: u64) -> Result<bool, LendingError>
calculate_refinance_fee(loan_id: u64) -> Result<Balance, LendingError>
get_refinance_history(loan_id: u64) -> Result<Vec<RefinanceRecord>, LendingError>Currently, the test suite requires updates for ink! 5.x compatibility. The main contract compiles successfully:
# Build the contract (works)
cargo build
# Run tests (requires updates)
cargo test- Input Validation: All inputs are validated before processing
- Access Control: Only authorized users can perform specific actions
- Collateral Requirements: Minimum collateral ratios enforced
- Error Handling: Comprehensive error handling with custom error types
- Event Logging: All operations are logged for transparency
The contract includes several configurable parameters:
protocol_fee: Protocol fee in basis points (default: 50 = 0.5%)min_collateral_ratio: Minimum collateral ratio (default: 150 = 150%)max_interest_rate: Maximum allowed interest rate (default: 10000 = 100%)
The following issues have been resolved:
- ✅ Fixed compilation errors with ink! 5.x compatibility
- ✅ Updated dependencies to use compatible versions
- ✅ Added missing
TypeInfoderive macros - ✅ Fixed type mismatches and import issues
- ✅ Corrected module structure and exports
To complete the project setup:
- Update test suite for ink! 5.x compatibility
- Update examples for ink! 5.x compatibility
- Add comprehensive integration tests
- Implement additional security features
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
This smart contract is for educational and development purposes. Please conduct thorough testing and security audits before using in production environments.