⚡Smart Contract Flow
Trust scores are calculated off-chain using the algorithm in the previous section. The smart contracts handle money movement, not social graph analysis. This keeps gas costs low (Base L2 fees are ~$0.01 per transaction).
Contract Interfaces
IMicroLoanFactory.sol
interface IMicroLoanFactory {
function createLoan(
address borrower,
string calldata metadataURI,
uint256 principal,
uint256 loanDuration,
uint256 fundraisingDeadline
) external returns (address loanAddress);
}Deploys individual loan contracts with validation constraints
IMicroLoan.sol
interface IMicroLoan {
function contribute(uint256 amount) external;
function disburse() external;
function repay(uint256 amount) external;
function claimableAmount(address contributor)
external view returns (uint256);
function claim() external;
}Core loan lifecycle: fundraising → disbursement → repayment → claims
Loan Lifecycle
1. Loan Deployed via Factory
function createLoan(
address borrower,
string calldata metadataURI,
uint256 principal,
uint256 loanDuration,
uint256 fundraisingDeadline
) external returns (address loanAddress)Constraints:
Minimum principal: P_min = $100
Loan duration: 7 days ≤ D ≤ 365 days
Disbursement window: 14 days after funding
Restriction: One active loan per borrower
Each loan gets its own smart contract with parameters: principal P, maturity date T_maturity, borrower address, and verified Farcaster ID.
2. Lenders Contribute
function contribute(uint256 amount) externalLenders send funds to the contract. Each contribution updates:
C_lender ← C_lender + amount
R_total ← R_total + amountWhere:
C_lender = cumulative contribution from this lender
R_total = total raised across all lenders
Trust scores are calculated off-chain and displayed in the UI. The contract only tracks capital flow.
3. Funds Disbursed to Borrower
function disburse() externalOnce fully funded, borrower can claim funds within the disbursement window:
Conditions:
R_total ≥ P (fully funded)
T_current ≤ T_deadline + 14 days
Funds not yet disbursed
Action: Transfer R_total to borrower
4. Repayment & Claims
function repay(uint256 amount) external
function claimableAmount(address contributor)
external view returns (uint256)
function claim() externalBorrower repays flexibly. Lenders claim their pro-rata share using an accumulator pattern for gas efficiency.
On Each Repayment:
A ← A + (r × k) / PWhere:
A = accumulator (tracks total repaid per $1 of principal)
r = repayment amount
k = precision constant (10^18)
P = original principal
Claimable Amount per Lender:
Claimable = (C_lender × A) / k − D_lenderWhere:
C_lender = lender's contribution
D_lender = amount already claimed by lender
Result is proportional to contribution share
This approach calculates pro-rata shares in O(1) time per lender, rather than iterating over all lenders. Overpayments automatically distribute as bonuses.
Tipping on Repayment
Borrowers can express gratitude by repaying more than the principal amount. The smart contract automatically distributes tips proportionally among all lenders.
How it works:
// Borrower repays $110 on $100 loan
repay(110); // $10 tip automatically distributed
// Each lender receives: (contribution / principal) × total_repaid
// Lender who gave $30 receives: ($30 / $100) × $110 = $33 ($3 tip)
// Lender who gave $70 receives: ($70 / $100) × $110 = $77 ($7 tip)The accumulator pattern handles this automatically—any repayment amount distributes proportionally, whether it's 50%, 100%, or 110% of principal.
Pro Tip: Tips strengthen your reputation and show gratitude to supporters. Even small tips (5-10%) signal reliability and appreciation.
Gas Optimization
The accumulator pattern is crucial for scalability:
Accumulator (Our Method)
~40,000 gas
~40,000 gas
Iterate All Lenders
~40,000 gas
~4,000,000 gas
With Base L2 gas at ~$0.01 per 40,000 gas, our method keeps costs constant regardless of lender count.
Security Considerations
Reentrancy Protection: All external calls use
nonReentrantmodifierAccess Control: Only borrower can disburse funds
Overflow Protection: Solidity 0.8.20 has built-in overflow checks
Fixed-Point Math: 10^18 precision prevents rounding errors
Contract Addresses
Base Mainnet
MicroLoanFactory:
[TO BE DEPLOYED]USDC Token:
0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913
Base Sepolia (Testnet)
MicroLoanFactory:
[CHECK REPO]Test USDC:
[CHECK REPO]
Next: Learn how Social Trust Scoring enables uncollateralized lending
Last updated