1516 words
8 minutes
MeltyFi
VincenzoImp
/
meltyfi
Waiting for api.github.com...
00K
0K
0K
Waiting...

Making the illiquid liquid - An innovative peer-to-pool lending and borrowing protocol with NFT collateral through lottery mechanics

๐ŸŽฏ Overview#

MeltyFi is a revolutionary DeFi protocol that enables peer-to-pool lending and borrowing with NFT collateral through an innovative lottery-based system inspired by Charlie and the Chocolate Factory. Unlike traditional NFT-backed lending platforms, MeltyFi eliminates the risk of involuntary liquidation by operating independently of off-chain factors like floor prices.

๐Ÿซ The Chocolate Factory Analogy#

In MeltyFi, each NFT is like a chocolate bar. When borrowers need liquidity:

  1. ๐Ÿซ Breaking the Bar: The NFT (chocolate bar) is divided into WonkaBars (chocolate squares)
  2. ๐Ÿ’ฐ Selling Squares: Lenders buy WonkaBars to fund the loan
  3. ๐ŸŽซ Golden Ticket: If the loan isnโ€™t repaid, one random WonkaBar contains the โ€œgolden ticketโ€ to win the NFT
  4. ๐Ÿช ChocoChips: All participants earn governance tokens as rewards

This process โ€œmeltsโ€ the illiquid NFT into liquid capital while maintaining fairness for all parties.

โœจ Key Features#

  • ๐Ÿ”’ Zero Liquidation Risk: NFTs are never forcibly liquidated due to price fluctuations
  • ๐ŸŽฒ Lottery-Based Funding: Transparent, fair distribution mechanism
  • ๐Ÿ† Win-Win Design: Benefits for both borrowers and lenders
  • ๐ŸŽฎ Gamified Experience: Engaging chocolate factory theme
  • ๐ŸŒ Fully Decentralized: No dependence on external price oracles
  • โšก Chainlink Integration: Provably random winner selection

๐Ÿ—๏ธ Protocol Architecture#

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   MeltyFiNFT    โ”‚    โ”‚   MeltyFiDAO    โ”‚    โ”‚   ChocoChip     โ”‚
โ”‚  (Core Logic)   โ”‚โ—„โ”€โ”€โ–บโ”‚  (Governance)   โ”‚โ—„โ”€โ”€โ–บโ”‚ (ERC-20 Token)  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚                       โ”‚                       โ”‚
         โ–ผ                       โ–ผ                       โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ LogoCollection  โ”‚    โ”‚ VRF Consumer    โ”‚    โ”‚   WonkaBars     โ”‚
โ”‚ (ERC-1155 Meme) โ”‚    โ”‚ (Randomness)    โ”‚    โ”‚ (ERC-1155 Tickets)โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Smart Contracts#

๐ŸŽฏ Core Contracts#

  • MeltyFiNFT (0x6c1030B8BbE523671Bcfd774Ae59ef620f9f31b4) - Main protocol logic
  • MeltyFiDAO (0xC4AA65a48fd317070F1A5aC5eBAC70F9d022Fb1e) - Governance contract
  • ChocoChip - ERC-20 governance token with voting capabilities
  • LogoCollection - ERC-1155 meme token for protocol branding
  • VRFv2DirectFundingConsumer - Chainlink VRF for random winner selection

๐ŸŽซ Token Standards#

  • ERC-721: NFT collateral support
  • ERC-1155: WonkaBars (lottery tickets) and LogoCollection
  • ERC-20: ChocoChip governance token with EIP-2612 permit functionality

๐ŸŽฒ Lottery States & Behavior#

State Diagram#

    [Create Lottery]
           โ”‚
           โ–ผ
    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚   ACTIVE    โ”‚
    โ”‚ (Can buy    โ”‚
    โ”‚ WonkaBars)  โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
           โ”‚
           โ”œโ”€โ”€โ”€ Repay Loan โ”€โ”€โ”€โ”€โ–บ [CANCELLED] โ”€โ”€โ–บ Refund + ChocoChips
           โ”‚
           โ””โ”€โ”€โ”€ Time Expires โ”€โ”€โ–บ [CONCLUDED] โ”€โ”€โ–บ Random Winner + ChocoChips

๐ŸŽฎ Lottery States Explained#

๐ŸŸข ACTIVE State#

  • Duration: From creation until expiration or repayment
  • Actions: Lenders can purchase WonkaBars
  • Funds: 95% goes to borrower, 5% to DAO treasury
  • Transition: Can become CANCELLED (repayment) or CONCLUDED (expiration)

๐ŸŸก CANCELLED State#

  • Trigger: Borrower repays loan before expiration
  • Outcome: NFT returned to borrower
  • Rewards:
    • Lenders get full refund + ChocoChips
    • Borrower gets ChocoChips for good behavior

๐Ÿ”ด CONCLUDED State#

  • Trigger: Expiration date reached without repayment
  • Winner Selection: Chainlink VRF randomly selects winner
  • Rewards:
    • Winner gets NFT + ChocoChips
    • Other lenders get ChocoChips only

โšซ TRASHED State#

  • Condition: No WonkaBars were ever purchased
  • Result: Lottery automatically cleaned up

๐Ÿš€ Protocol Workflows#

๐Ÿ“‹ Use Cases Overview#

๐Ÿฆ BORROWER                    ๐ŸŽฏ LENDER                    ๐Ÿค– ORACLE
     โ”‚                            โ”‚                            โ”‚
     โ”œโ”€ Create Lottery            โ”œโ”€ Buy WonkaBars            โ”œโ”€ Draw Winner
     โ””โ”€ Repay Loan               โ””โ”€ Melt WonkaBars           โ””โ”€ (Automation)

๐Ÿ”„ Detailed Workflows#

1๏ธโƒฃ Create Lottery (Borrower)#

sequenceDiagram
    participant B as Borrower
    participant M as MeltyFiNFT
    participant N as NFT Contract
    
    B->>N: approve(MeltyFiNFT, tokenId)
    B->>M: createLottery(nftContract, tokenId, price, expiry, maxSupply)
    M->>N: transferFrom(borrower, MeltyFiNFT, tokenId)
    M->>M: mint WonkaBars for lottery
    M-->>B: Lottery created โœ…

2๏ธโƒฃ Buy WonkaBars (Lender)#

sequenceDiagram
    participant L as Lender
    participant M as MeltyFiNFT
    participant B as Borrower
    participant D as DAO
    
    L->>M: buyWonkaBars(lotteryId, quantity) + ETH
    M->>M: mint WonkaBars to lender
    M->>B: transfer 95% of payment
    M->>D: transfer 5% fee
    M-->>L: WonkaBars received ๐ŸŽซ

3๏ธโƒฃ Repay Loan (Borrower)#

sequenceDiagram
    participant B as Borrower
    participant M as MeltyFiNFT
    participant C as ChocoChip
    
    B->>M: repayLoan(lotteryId)
    M->>M: set lottery state to CANCELLED
    M->>M: transfer NFT back to borrower
    M->>C: mint ChocoChips to borrower
    M-->>B: Loan repaid, NFT returned ๐ŸŽ‰

4๏ธโƒฃ Melt WonkaBars (Lender)#

sequenceDiagram
    participant L as Lender
    participant M as MeltyFiNFT
    participant C as ChocoChip
    
    alt Lottery CANCELLED
        L->>M: meltWonkaBars(lotteryId, quantity)
        M->>L: refund ETH investment
        M->>C: mint ChocoChips to lender
        M-->>L: Refund + rewards received ๐Ÿ’ฐ
    else Lottery CONCLUDED (Winner)
        L->>M: meltWonkaBars(lotteryId, quantity)
        M->>L: transfer NFT prize
        M->>C: mint ChocoChips to lender
        M-->>L: NFT + rewards received ๐Ÿ†
    else Lottery CONCLUDED (Non-winner)
        L->>M: meltWonkaBars(lotteryId, quantity)
        M->>C: mint ChocoChips to lender
        M-->>L: ChocoChips received ๐Ÿช
    end

5๏ธโƒฃ Draw Winner (Oracle Automation)#

sequenceDiagram
    participant O as Oracle
    participant M as MeltyFiNFT
    participant V as VRF Consumer
    
    O->>M: checkUpkeep() [expired lotteries?]
    M-->>O: performData [lottery IDs to conclude]
    O->>M: performUpkeep(performData)
    M->>V: requestRandomWords()
    V-->>M: fulfillRandomWords(requestId, randomWords)
    M->>M: select winner based on random number
    M->>M: set lottery state to CONCLUDED
    M-->>O: Winners drawn for expired lotteries ๐ŸŽฒ

๐ŸŽฏ Protocol Scenarios#

๐Ÿ“ˆ Scenario 1: Successful Repayment#

  1. Borrower creates lottery with valuable NFT
  2. Multiple lenders buy WonkaBars, funding the loan
  3. Borrower receives 95% of funds immediately
  4. Before expiration, borrower repays the loan
  5. Results:
    • โœ… Borrower gets NFT back + ChocoChips
    • โœ… Lenders get full refund + ChocoChips
    • โœ… Everyone wins!

๐Ÿ“‰ Scenario 2: Loan Default#

  1. Borrower creates lottery but cannot repay
  2. Lottery expires and transitions to CONCLUDED
  3. Chainlink VRF randomly selects winner
  4. Results:
    • ๐Ÿ† One lucky lender wins the NFT + ChocoChips
    • ๐Ÿช Other lenders receive ChocoChips as consolation
    • ๐Ÿ’” Borrower loses NFT but keeps the borrowed funds

๐Ÿ’ป Frontend Architecture#

๐ŸŽจ User Interface#

Built with modern React ecosystem for optimal user experience:

  • React 18 - Component-based UI framework
  • React-Bootstrap - Responsive design components
  • Ethers.js - Ethereum blockchain interaction
  • thirdweb - Web3 authentication and wallet connection
  • MetaMask - Primary wallet integration
  • OpenSea API - NFT metadata retrieval

๐Ÿ“ฑ Pages & Features#

๐Ÿ  Home Page#

  • Protocol introduction and chocolate factory explanation
  • Key features and benefits overview
  • How-to-use guide for new users

๐ŸŽฐ Lotteries Page#

  • Browse Lotteries: View all active lotteries with filtering
  • Create Lottery: Select NFTs and set lottery parameters
  • Real-time lottery status and participation data
  • Interactive lottery cards with NFT previews

๐Ÿ‘ค Profile Page#

  • View owned lotteries and their current status
  • Check WonkaBar holdings and win probabilities
  • Monitor ChocoChip balance and transaction history
  • Manage active positions and claim rewards

๐Ÿ› ๏ธ Installation & Setup#

Prerequisites#

# Required software
- Node.js (v16 or higher)
- MetaMask browser extension
- Git

# Required tokens  
- Goerli testnet ETH (from faucet)
- LINK tokens (for oracle funding)

๐Ÿ”ง Backend Setup#

# Clone the repository
git clone https://github.com/VincenzoImp/MeltyFi.NFT
cd MeltyFi.NFT/src/back-end/MeltyFiProtocol

# Install dependencies
npm install

# Environment configuration
cp .env.example .env
# Add your keys:
# ALCHEMY_API_KEY=your_alchemy_key
# GOERLI_PRIVATE_KEY=your_private_key  
# ETHERSCAN_API_KEY=your_etherscan_key

# Compile smart contracts
npx hardhat compile

# Run comprehensive test suite
npx hardhat test

# Deploy to Goerli (if needed)
npx hardhat run scripts/deploy.js --network goerli

# Verify contracts on Etherscan
npx hardhat verify --network goerli CONTRACT_ADDRESS

๐ŸŽจ Frontend Setup#

# Navigate to frontend directory
cd src/front-end/meltyfi.nft

# Install React dependencies
npm install

# Start development server
npm start

# Build for production
npm run build

The application will be available at http://localhost:3000

๐Ÿ“Š Contract Interactions#

๐Ÿ” Reading Contract State#

// Get lottery information
function getLotteryInfo(uint256 lotteryId) 
    external view returns (Lottery memory);

// Check WonkaBar balance
function balanceOf(address account, uint256 lotteryId) 
    external view returns (uint256);

// Get ChocoChip balance
function balanceOf(address account) 
    external view returns (uint256);

// Check if address won lottery
function isWinner(address account, uint256 lotteryId) 
    external view returns (bool);

โœ๏ธ Writing Contract State#

// Create new lottery
function createLottery(
    IERC721 _prizeContract,
    uint256 _prizeTokenId, 
    uint256 _wonkaBarPrice,
    uint256 _expirationDate,
    uint256 _wonkaBarsMaxSupply
) external;

// Purchase lottery tickets
function buyWonkaBars(uint256 _lotteryId, uint256 _amount) 
    external payable;

// Repay loan and cancel lottery  
function repayLoan(uint256 _lotteryId) external;

// Claim rewards and prizes
function meltWonkaBars(uint256 _lotteryId, uint256 _amount) 
    external;

// Manually trigger winner selection
function drawWinner(uint256 _lotteryId) external;

๐Ÿ” Security Features#

๐Ÿ›ก๏ธ Smart Contract Security#

  • OpenZeppelin Standards: Battle-tested contract implementations
  • Reentrancy Guards: Protection against common attack vectors
  • Access Control: Role-based permissions and ownership
  • Input Validation: Comprehensive parameter checking
  • Emergency Controls: Circuit breakers for critical functions

๐Ÿ”’ Oracle Security#

  • Chainlink VRF: Provably random and tamper-proof randomness
  • Direct Funding: Anyone can fund oracles to ensure availability
  • Fallback Mechanisms: Manual winner drawing if automation fails
  • No External Dependencies: Floor price independence eliminates oracle manipulation

๐Ÿšจ Protocol Safety#

  • No Liquidation Risk: Borrowers cannot lose NFTs unexpectedly
  • Fair Distribution: Proportional lottery chances based on investment
  • Transparent Operations: All actions recorded on-chain
  • Automated Processes: Reduced human error through smart contracts

๐ŸŒ Live Deployment#

๐Ÿ“ Contract Addresses (Goerli Testnet)#

ContractAddressEtherscan
MeltyFiNFT0x6c1030B8BbE523671Bcfd774Ae59ef620f9f31b4View
MeltyFiDAO0xC4AA65a48fd317070F1A5aC5eBAC70F9d022Fb1eView

๐Ÿš€ Live Applications#

๐Ÿ“ Example Transactions#

โš ๏ธ Known Limitations#

๐ŸŒ Protocol Limitations#

  • Slow Funding: Loans require multiple lenders to participate
  • Uncertainty: Three possible outcomes create complexity for lenders
  • Gas Costs: Complex lottery mechanics increase transaction costs
  • Testnet Only: Currently deployed on Goerli for testing

๐Ÿ”ง Technical Limitations#

  • Oracle Dependency: Relies on Chainlink for automation and randomness
  • Frontend Gaps: DAO interface not yet implemented
  • Scalability: May face congestion on mainnet deployment

๐Ÿš€ Future Roadmap#

๐ŸŽฏ Phase 1: Production Ready#

  • โœ… Mainnet deployment with audited contracts
  • โœ… Layer 2 integration (Polygon, Arbitrum, Optimism)
  • โœ… Enhanced security audits and bug bounties
  • โœ… Mobile-responsive interface improvements

๐ŸŽฏ Phase 2: Advanced Features#

  • ๐Ÿ”„ Dutch auction lottery mechanisms
  • ๐ŸŽฏ Multi-NFT collateral lotteries
  • ๐Ÿ’Ž Fractional NFT support
  • ๐Ÿช NFT marketplace integration

๐ŸŽฏ Phase 3: Ecosystem Expansion#

  • ๐Ÿ“ฑ Native mobile application
  • ๐ŸŒ‰ Cross-chain bridge functionality
  • ๐Ÿ›๏ธ Fully functional DAO governance
  • ๐ŸŽฎ Gamification enhancements

๐Ÿ‘ฅ Team#

Team MemberRoleResponsibilities
Vincenzo ImperatiProject Manager & Backend DeveloperSmart contract development, system architecture, project coordination
Benigno AnsanelliFrontend DeveloperLottery interface, user experience, documentation
Andrea PrincicFrontend Developer & UX DesignerProfile management, sequence diagrams, use case design

๐Ÿค Contributing#

We welcome contributions from the community! Hereโ€™s how you can help:

๐Ÿ”ง Development Guidelines#

  1. Fork the repository and create a feature branch
  2. Follow existing code style and conventions
  3. Add comprehensive tests for new functionality
  4. Update documentation as needed
  5. Test thoroughly on Goerli testnet

๐Ÿ“‹ Contribution Areas#

  • ๐Ÿ› Bug fixes and security improvements
  • โœจ New features and enhancements
  • ๐Ÿ“š Documentation and tutorials
  • ๐ŸŽจ UI/UX improvements
  • ๐Ÿงช Testing and quality assurance

๐Ÿ“„ License#

This project is open source. Please refer to the repository for specific license details.

๐ŸŒ Project Resources#

๐Ÿ”ง Development Tools#

๐Ÿ“š Educational Resources#


MeltyFi Protocol - Where Charlieโ€™s chocolate factory meets DeFi innovation! ๐Ÿซโœจ

Making illiquid NFTs liquid through the magic of lottery mechanics and community-driven finance.

MeltyFi
https://vincenzo.imperati.dev/posts/meltyfi/
Author
Vincenzo Imperati
Published at
2023-01-15