Tutorials and Examples: Building a Simple DeFi Lending dApp

This tutorial will walk you through developing a simple decentralized finance (DeFi) lending dApp on JuChain. Users can deposit JU (JuChain’s native token) as collateral, borrow a portion of JU based on that collateral, and retrieve their collateral after repaying the loan. We’ll use Solidity to write the smart contract, leverage JuChain’s EVM compatibility for deployment, and create a frontend for user interaction.

This example highlights JuChain’s near-instant transaction confirmations and ultra-low costs, making it ideal for DeFi use cases.


Tutorial: Simple DeFi Lending dApp

Prerequisites

Development Environment

  • Node.js: v16 or higher

  • Truffle Suite: Install via npm install -g truffle

  • MetaMask: Browser extension, configured for JuChain Testnet

  • Knowledge: Familiarity with Solidity and Web3.js

JuChain Testnet

  • Testnet RPC: https://testnet-rpc.juchain.org

  • Network ID: 202599 (assumed)

  • Test JU: Obtainable via the JuChain Testnet faucet


Step 1: Set Up the Project

  1. Create and Initialize the Project:

    mkdir JuChainLending
    cd JuChainLending
    truffle init
  2. Install Dependencies:

    npm install @openzeppelin/contracts web3

Step 2: Write the Lending Smart Contract

Create LendingPool.sol in the contracts/ folder:

Functionality Explained:

  • depositCollateral(): Users send JU (native token) as collateral.

  • borrow(uint256 amount): Borrow up to 50% of collateral’s value in JU.

  • repay(uint256 amount): Repay the loan with 5% interest; excess JU is refunded.

  • withdrawCollateral(uint256 amount): Retrieve collateral after full repayment.

  • getUserStatus(address user): Returns collateral, borrowed amount, and max borrowable JU.

  • Security: Uses ReentrancyGuard to prevent reentrancy attacks.

Note: JU is JuChain’s native token, handled via msg.value and call, not an ERC20 token.


Step 3: Configure Truffle

Edit truffle-config.js to connect to the JuChain Testnet:


Step 4: Deploy the Contract

  1. Create a Deployment Script: In migrations/, add 2_deploy_lending.js:

  2. Deploy to JuChain Testnet:

    • Note the deployed LendingPool contract address (e.g., 0x1234...5678).


Step 5: Test the Contract

Validate functionality using the Truffle console:

  1. Enter the Console:

  2. Test Commands:


Step 6: Create the Frontend Interface

Create index.html in the project root:

Configuration Notes:

  • ABI: Copy the full ABI from build/contracts/LendingPool.json and replace the abi variable.

  • Contract Address: Update contractAddress with the address from Step 4.

  • Run: Serve index.html with a local server (e.g., npx http-server) and connect via MetaMask on the JuChain Testnet.


Step 7: Run and Test

  1. Switch MetaMask to the JuChain Testnet and ensure sufficient test JU is available.

  2. Open index.html in a browser and test:

    • Deposit Collateral: Enter 100 JU and click "Deposit Collateral".

    • Borrow JU: Enter 50 JU and click "Borrow JU".

    • Repay Loan: Enter 50 JU and click "Repay Loan" (includes 5% interest, total 52.5 JU).

    • Withdraw Collateral: Enter 100 JU and click "Withdraw Collateral".

  3. JuChain’s near-instant confirmations ensure transactions complete in seconds, with fees typically below 0.001 JU.


Outcome

You’ve built and deployed a simple DeFi lending dApp:

  • Users can deposit JU as collateral.

  • Borrow up to 50% of their collateral’s value in JU.

  • Repay loans with 5% interest to reclaim collateral.

  • The frontend displays collateral, borrowed amounts, and max borrowable JU in real-time.

This dApp demonstrates JuChain’s high-performance infrastructure, perfect for scaling into more complex DeFi applications (e.g., multi-asset support or dynamic rates).


Notes

  1. Security:

    • For production, add liquidation mechanisms (to handle undercollateralization) and conduct a code audit.

    • The current contract doesn’t address edge cases (e.g., insufficient JU balance).

  2. Testnet:

    • Ensure your account has enough test JU for interactions.

  3. Scalability:

    • Enhance with time-based interest calculations or additional asset support as needed.


Last updated