Undercollateralized lending powered by FairScale reputation scores
FairLend is a trust-based P2P lending protocol on Solana that uses FairScale's on-chain reputation scoring to dynamically adjust lending terms. Higher FairScores unlock better loan amounts and lower interest rates, reducing default risk while rewarding active, trustworthy participants in the Solana ecosystem.
Built for the FairScale Challenge | Demo Video | Live App
FairLend meaningfully integrates FairScale's reputation API as the primary risk primitive:
| Tier | FairScore Range | Loan Multiplier | Interest Rate | Status |
|---|---|---|---|---|
| π₯ Gold | 800+ | 2x | 5% APR | β Approved |
| π₯ Silver | 400-800 | 1.5x | 10% APR | β Approved |
| π₯ Bronze | < 400 | N/A | N/A | β Denied |
- Wallet Connection: User connects Phantom/Solflare wallet
- FairScore Fetch: Backend API proxies FairScale API to retrieve score
- Dynamic Terms: UI displays tier, multiplier, and interest rate
- On-Chain Enforcement: Anchor program validates score >= 400, applies multipliers
- Loan Execution: USDC transferred from pool to borrower with PDA-based escrow
- Repayment: Interest calculated on-chain (amount Γ rate Γ days / 365)
Key Differentiator: FairScore isn't just UI decorationβit's enforced on-chain in the smart contract, preventing low-score loans at the protocol level.
βββββββββββββββββββ
β User Wallet β (Phantom, Solflare)
ββββββββββ¬βββββββββ
β
ββββββββββββββββββββ
β β
v v
ββββββββββββββββββ βββββββββββββββββββββββ
β Next.js App β β FairScale API β
β (Frontend) ββββ€ (Reputation Score) β
βββββββββ¬βββββββββ βββββββββββββββββββββββ
β
β RPC Calls
v
βββββββββββββββββββββββββββββββ
β Solana Blockchain β
β (Devnet) β
β β
β ββββββββββββββββββββββ β
β β FairLend Program β β
β β - Pool PDA β β
β β - Loan PDAs β β
β β - FairScore Logic β β
β ββββββββββββ¬ββββββββββ β
β β β
β v β
β ββββββββββββββββββββββ β
β β SPL Token Program β β
β β (USDC Transfers) β β
β ββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββ
- Node.js 18+ and npm
- Rust 1.93+ and Cargo
- Solana CLI 1.18+
- Anchor CLI 0.32+
- Phantom or Solflare wallet (with Devnet SOL)
- FairScale API key (get it here)
# Clone the repository
git clone https://github.com/yourusername/fairlend-prototype.git
cd fairlend-prototype
# Install Anchor program dependencies
cd program/fairlend
anchor build
# Install frontend dependencies
cd ../../frontend
npm install
# Configure environment variables
cp .env.local.example .env.local
# Edit .env.local and add your FairScale API keyEdit frontend/.env.local:
NEXT_PUBLIC_SOLANA_RPC=https://api.devnet.solana.com
NEXT_PUBLIC_PROGRAM_ID=<your_deployed_program_id>
FAIRSCALE_API_KEY=<your_fairscale_api_key>cd program/fairlend
# Set Solana to devnet
solana config set --url devnet
# Airdrop SOL for deployment (if needed)
solana airdrop 2
# Build and deploy
anchor build
anchor deploy
# Copy the program ID and update .env.localcd frontend
npm run devVisit http://localhost:3000 and connect your wallet!
fairlend-prototype/
βββ program/
β βββ fairlend/
β βββ programs/fairlend/src/
β β βββ lib.rs # Anchor smart contract
β βββ tests/ # Anchor tests
β βββ Anchor.toml # Anchor config (devnet)
β βββ Cargo.toml
β
βββ frontend/
β βββ app/
β β βββ api/
β β β βββ getFairScore/route.ts # FairScale API proxy
β β β βββ getWalletScore/route.ts # Detailed signals
β β βββ loan/page.tsx # Loan request page
β β βββ repay/page.tsx # Loan repayment page
β β βββ lender/page.tsx # Lender deposit page
β β βββ layout.tsx # Root layout with wallet provider
β β βββ page.tsx # Dashboard
β βββ components/
β β βββ WalletContextProvider.tsx # Solana wallet adapter
β βββ .env.local # Environment config
β βββ package.json
β
βββ README.md
- Purpose: One-time setup of lending pool
- Accounts: Pool PDA, authority
- Logic: Creates pool account with bump seed
- Purpose: Lenders deposit USDC to earn yield
- Accounts: Pool, lender token account, pool token account
- Logic: Transfers USDC to pool, updates total_deposited
- Purpose: Borrowers request loans with FairScore validation
- Accounts: Pool, borrower, loan PDA, token accounts
- Parameters:
amount(requested),fairscore(from API) - Logic:
- Validate
fairscore >= 400(on-chain check) - Calculate multiplier: 2x (score >= 800) or 1.5x (score >= 400)
- Calculate interest rate: 5% (Gold) or 10% (Silver)
- Check pool liquidity
- Transfer adjusted amount using PDA signer
- Create Loan account to track state
- Validate
- Purpose: Borrowers repay principal + interest
- Accounts: Pool, borrower, loan PDA, token accounts
- Logic:
- Calculate interest:
amount Γ rate Γ days / 365 - Transfer total_repayment to pool
- Mark loan as repaid
- Update pool state
- Calculate interest:
#[account]
pub struct Pool {
pub authority: Pubkey,
pub bump: u8,
pub total_deposited: u64,
pub total_loaned: u64,
}
#[account]
pub struct Loan {
pub borrower: Pubkey,
pub amount: u64,
pub start_time: i64,
pub interest_rate: u8, // 5 or 10
pub fairscore: u64,
pub is_repaid: bool,
pub bump: u8,
}LowFairScore: Score below 400 minimumInsufficientPoolFunds: Not enough liquidityLoanAlreadyRepaid: Duplicate repayment attemptInvalidAmount: Amount <= 0
- Wallet connection via Solana Wallet Adapter
- Real-time FairScore display with tier badge
- Dynamic color-coding (Gold/Silver/Bronze)
- Navigation cards to loan/repay/lender panels
- Glassmorphism UI with gradient backgrounds
- FairScore-based term preview
- Live calculation of adjusted amount
- Tier benefits explanation
- Transaction status tracking
- Solana Explorer links (coming soon)
- Active loan details
- Real-time interest accrual calculation
- Days borrowed counter
- One-click repayment
- Educational tooltips
- Pool statistics (size, utilization, APY)
- User position tracking
- Estimated earnings calculator
- Deposit interface
- Yield breakdown
/api/getFairScore?wallet=<address>
- Proxies FairScale main score API
- Hides API key from client
- Returns:
{ fairscore: number }
/api/getWalletScore?wallet=<address>
- Fetches detailed wallet signals
- Used for badge explanations
- Returns: Activity breakdown (trades, socials, etc.)
cd program/fairlend
anchor testTest Coverage:
- Pool initialization
- Lender deposits
- Loan requests with various FairScores (>800, 400-800, <400)
- Multiplier calculations
- Interest rate assignments
- Repayment with interest
- Error cases (low score, insufficient funds)
-
Wallet Connection
- Connect Phantom wallet on devnet
- View FairScore on dashboard
- See correct tier badge (Gold/Silver/Bronze)
-
Loan Flow
- Navigate to
/loan - Enter loan amount
- See adjusted amount based on tier
- Submit transaction (demo mode shows success)
- Verify transaction on Solana Explorer
- Navigate to
-
Repayment Flow
- Navigate to
/repay - View active loan details
- See interest calculation
- Complete repayment
- Navigate to
-
Lender Flow
- Navigate to
/lender - View pool statistics
- Deposit USDC to pool
- See updated position
- Navigate to
- FairScore validated on-chain (prevents tampering)
- PDA-based escrow (no authority risks)
- API key hidden from client (Next.js API routes)
- Checked arithmetic (no overflows)
- Demo mode: No real USDC transfers yet (requires token setup)
- π Implement oracle for real-time FairScore updates
- π Add multisig for pool authority
- π Conduct full smart contract audit
- π Implement liquidation mechanism for score drops
- π Add rate limiting to API routes (10 req/min per IP)
- π Use verifiable FairScore proofs (if FairScale supports)
- Risk Primitive: FairScore is the core risk assessment tool, replacing traditional collateral requirements
- Dynamic Terms: Loan amounts and interest rates automatically adjust based on score
- On-Chain Enforcement: Smart contract validates scores, not just UI display
- User Education: UI explains how on-chain activity (trades, social links) affects terms
- Continuous Impact: Repaying loans on time improves FairScore for future larger loans
- Borrowers: Access capital without locking up assets, better terms for good behavior
- Lenders: Lower default risk via reputation gating, transparent APY
- Ecosystem: Incentivizes positive on-chain activity, builds trust on Solana
- FairScore distribution of borrowers
- Default rates by tier (expect Gold < Silver < Bronze denial)
- Avg loan size increase for repeat borrowers with improving scores
- Pool utilization vs. interest rate correlation
- Deploy to mainnet
- Integrate with Marginfi or Solend for real liquidity
- Add liquidation triggers for score drops
- Implement governance token (FAIR)
- Multi-token support (SOL, USDT, RAY)
- Credit limits based on historical repayment
- Referral system (lenders refer borrowers)
- Mobile app (React Native)
- NFT collateral boost (Tensor/Magic Eden)
- DeFi reputation aggregation (FairScale + other protocols)
- Cross-chain bridges (Wormhole)
Anchor build fails with edition2024 error
# Solution: Update Rust to latest nightly
rustup update nightly
rustup override set nightly
anchor buildWallet won't connect
- Ensure you're on Solana devnet
- Check wallet has devnet SOL (use faucet)
- Clear browser cache
FairScore shows "Failed to fetch"
- Verify
FAIRSCALE_API_KEYin.env.local - Check API key is valid (test in Postman)
- Ensure Next.js dev server is running
Transaction fails
- Currently in demo modeβconnect to deployed program for real txs
- Check wallet has sufficient SOL for gas
- Verify program is deployed to devnet
This is a prototype for the FairScale Challenge. For production use:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
MIT License - feel free to use this as a template for your own reputation-based lending protocols!
- FairScale for the reputation API and challenge inspiration
- Solana Foundation for the blazing-fast blockchain
- Anchor for making Solana development accessible
- Phantom & Solflare for seamless wallet experiences
FairLend: Where reputation meets opportunity π