This project is a decentralized peer-2-peer lending platform that allows users to collateralize NFTs in exchange for cryptocurrency loans. Built using the Diamond Standard (EIP-2535), this contract system offers modular, upgradable, and gas-efficient smart contracts, enabling easy addition and modification of features.
- NFT Collateralization: Borrowers can lock up NFTs as collateral for loans.
- Lenders: Lenders select any loan term they like and grant loan while waiting for interest.
- Diamond Contract Structure: Using facets to modularize functionality, including
LoanFacet,LenderFacet, andRepaymentFacet. - Flexible Loan Terms: Customizable loan amounts, durations, and interest rates.
- Escrow Management: The platform acts as an escrow, transferring collateralized NFTs to lenders if loans are not repaid.
- Time Simulation (for Testing): Loan expiry and repayment scenarios can be tested with time manipulation.
- Diamond.sol: Implements the core Diamond Proxy logic, delegating calls to facets based on function selectors.
- LoanFacet.sol: Contains functionality for creating loan terms, transferring NFT collateral, and loan state management.
- LenderFacet.sol: Handles lender interactions, such as accepting loans and force-closing on defaults.
- RepaymentFacet.sol: Manages repayments, interest calculation, and updating loan status.
- LibDiamond.sol: Manages shared state variables (loan data, mappings) and stores diamond-related data.
Tests are written using Foundry to simulate different lending scenarios. Tests include:
- Loan creation and validation.
- Time-based tests for loan expiration.
- Forced loan closures and collateral transfers.
- Foundry: For testing, debugging, and simulating blockchain environments.
- OpenZeppelin Contracts: For ERC20 and ERC721 token standards.
- Solidity Console: Used in debugging to log state changes.
-
Clone the repository:
git clone git@github.com:Guzbyte-tech/NFTcollateral_Lending_Contract_Diamond.git cd NFTcollateral_Lending_Contract_Diamond -
Install Foundry: Follow the Foundry installation guide here.
-
Install dependencies:
forge install
-
Compile the contracts:
forge build
Tests are located in the test directory and use the Foundry testing framework. Run all tests using:
forge testTo simulate time in tests, Foundry’s vm.warp is used to control block.timestamp.
-
Create a Loan:
- Borrowers call
createLoanTermson theLoanFacet, specifying currency, amount, interest, and collateral NFT details.
- Borrowers call
-
Lender Acceptance:
- Lenders can accept a loan, initiating the loan duration and transferring tokens to the borrower.
-
Repayment:
- Borrowers repay the loan within the duration specified, paying principal plus interest.
-
Force Closure:
- If the borrower fails to repay, the lender can call
forceCloseLoanto transfer the NFT collateral to their address.
- If the borrower fails to repay, the lender can call
- Fork the project
- Create a feature branch:
git checkout -b feature-name - Commit your changes:
git commit -m 'Add some feature' - Push to the branch:
git push origin feature-name - Open a pull request
This project is licensed under the MIT License.