|
| 1 | +# DeFi Lend Borrow - Part II |
| 2 | + |
| 3 | +This is a comprehensive guide to the DeFi Lend Borrow DApp, a decentralized application built using React and the ethers library. The DApp allows users to lend and borrow cryptocurrency assets on Shimmer EVM Testnet. |
| 4 | + |
| 5 | + |
| 6 | +### Prerequisites |
| 7 | + |
| 8 | +- [Node.js](https://nodejs.org) = v18.0 |
| 9 | +- [React.js](https://react.dev/) >= v18.2.0 |
| 10 | +- [npx](https://www.npmjs.com/package/npx) >= v7.1.0. |
| 11 | +- [Metamask](https://metamask.io/) : Set up a Metamask wallet with some Shimmer EVM testnet tokens. |
| 12 | + |
| 13 | +## Set Up |
| 14 | + |
| 15 | +First, [bootsrap a new React project](https://create-react-app.dev/docs/getting-started/), by running: |
| 16 | + |
| 17 | +```bash |
| 18 | +npx create-react-app lend-borrow-ui |
| 19 | +cd lend-borrow-ui |
| 20 | +``` |
| 21 | + |
| 22 | +Secondly, install [ethers.js](https://docs.ethers.org/v5/) for interacting with the contracts through UI. |
| 23 | +```bash |
| 24 | +npm install ethers |
| 25 | +``` |
| 26 | + |
| 27 | +## Creating the UI |
| 28 | + |
| 29 | +### Code Structure |
| 30 | + |
| 31 | +- [`Context.js`](https://github.com/iota-community/Defi-lend-borrow/blob/main/lend-borrow-ui/src/context/Context.js): Defines the global context for web3 and other account data. |
| 32 | +- `Components`: Contains the components listed below for the UI : |
| 33 | + - [`NavigationBar`](https://github.com/iota-community/Defi-lend-borrow/blob/main/lend-borrow-ui/src/components/NavigationBar.js) |
| 34 | + - [`LendBorrowPlatformDetails`](https://github.com/iota-community/Defi-lend-borrow/blob/main/lend-borrow-ui/src/components/LendBorrowPlatformDetails/index.js) |
| 35 | + - [`AllAssetsList`](https://github.com/iota-community/Defi-lend-borrow/blob/main/lend-borrow-ui/src/components/LendBorrowPlatformDetails/AllAssetsList.js) |
| 36 | + - [`TransactionsCard`](https://github.com/iota-community/Defi-lend-borrow/blob/main/lend-borrow-ui/src/components/TransactionsCard/index.js) |
| 37 | + - [`Tabs`](https://github.com/iota-community/Defi-lend-borrow/blob/main/lend-borrow-ui/src/components/TransactionsCard/Tabs.js) |
| 38 | + - [`TransactionsForm`](https://github.com/iota-community/Defi-lend-borrow/blob/main/lend-borrow-ui/src/components/TransactionsCard/TransactionForm.js) |
| 39 | + - [`AccountDetails`](https://github.com/iota-community/Defi-lend-borrow/blob/main/lend-borrow-ui/src/components/AccountDetails.js) |
| 40 | + - [`ConnectWallet`](https://github.com/iota-community/Defi-lend-borrow/blob/main/lend-borrow-ui/src/components/ConnectWallet.js) |
| 41 | + - [`TransactionsAlert`](https://github.com/iota-community/Defi-lend-borrow/blob/main/lend-borrow-ui/src/components/TransactionAlert.js) |
| 42 | +- `Utils`: |
| 43 | + - [`etherUtils.js`](https://github.com/iota-community/Defi-lend-borrow/blob/main/lend-borrow-ui/src/utils/ethersUtils.js) |
| 44 | + - [`contractAbi.js`](https://github.com/iota-community/Defi-lend-borrow/blob/main/lend-borrow-ui/src/utils/contractAbi.js) |
| 45 | + - [`formats.js`](https://github.com/iota-community/Defi-lend-borrow/blob/main/lend-borrow-ui/src/utils/formats.js) |
| 46 | + - [`sendTransactions.js`](https://github.com/iota-community/Defi-lend-borrow/blob/main/lend-borrow-ui/src/utils/sendTransactions.js) |
| 47 | + |
| 48 | +### `Context` |
| 49 | + |
| 50 | +Create a directory named `context` and a file `Context.js`inside the app. |
| 51 | +This `Context.js` file sets up a context for managing wallet connection, balance information, and transaction details in a React application. It uses `createContext` to create a `Context` object, and the `WalletProvider` component provides this context to child components. |
| 52 | + |
| 53 | +The `WalletProvider` manages state for the wallet address, SMR balance, token balance, gas prices, and transaction history. It uses the `useEffect` hook to automatically detect if MetaMask is installed and connects to the Ethereum network. It also handles network switching, wallet connection, and balance fetching using utility functions from `ethersUtils`. Additionally, it includes methods to connect, disconnect, and refresh wallet balances when needed. |
| 54 | + |
| 55 | +This setup facilitates easy access to wallet-related data and actions across the app. |
| 56 | + |
| 57 | +```javascript reference |
| 58 | +https://github.com/iota-community/Defi-lend-borrow/blob/ee1dd2bd3f94ec594163c153886e4c0457654a5b/lend-borrow-ui/src/context/Context.js#L1-L82 |
| 59 | +``` |
| 60 | +## Components |
| 61 | + |
| 62 | +### `NavigationBar` |
| 63 | + |
| 64 | +The `Navbar` component manages wallet connection and user interactions. It displays the connected wallet address, balance, and provides a dropdown menu with options to view the balance or disconnect the wallet. It uses `context` to retrieve wallet details and fetches the user's native token balance. The component also includes a button to navigate to the accounts section of the app. |
| 65 | + |
| 66 | +```javascript reference |
| 67 | +https://github.com/iota-community/Defi-lend-borrow/blob/ee1dd2bd3f94ec594163c153886e4c0457654a5b/lend-borrow-ui/src/components/NavigationBar.js#L1-L76 |
| 68 | +``` |
| 69 | + |
| 70 | +### Dashboard |
| 71 | + |
| 72 | +the dashboard page consists of two components, `LendBorrowPlatformDetails` and `AllAssetsList` which shows details of all the supported tokens and details about the tokens and platform. |
| 73 | + |
| 74 | +#### `LendBorrowPlatformDetails` |
| 75 | + |
| 76 | +The `LendBorrowPlatformDetails` component displays a summary of the total supplies and total borrows on a lending/borrowing platform. It accepts two props: `totalSuppliesSum` and `totalBorrowsSum`, which represent the sum of all supplied and borrowed assets. The component formats and truncates these values to display the first six digits and shows them in a styled card layout. If no borrow amount is provided, it defaults to "$ 0". |
| 77 | + |
| 78 | +```javascript reference |
| 79 | +https://github.com/iota-community/Defi-lend-borrow/blob/ee1dd2bd3f94ec594163c153886e4c0457654a5b/lend-borrow-ui/src/components/LendBorrowPlatformDetails/index.js#L1-L27 |
| 80 | +``` |
| 81 | + |
| 82 | + |
| 83 | +#### `AllAssetsList` |
| 84 | + |
| 85 | +The AllAssetsList component fetches and displays a list of supported assets (tokens) in a decentralized finance (DeFi) application. It retrieves token data, including their supply, borrow amounts, collateral factor, and price, and renders them in a table format. Here's a breakdown: |
| 86 | + |
| 87 | +1. State Management: |
| 88 | + |
| 89 | + - allAssets: Stores the list of assets with their details. |
| 90 | + - isLoading: Indicates whether the asset data is being loaded. |
| 91 | + |
| 92 | +2. Fetching Data: |
| 93 | + |
| 94 | + - On component load, the useEffect hook calls the `getAllSupportedTokens` function to fetch a list of underlying and iToken addresses. |
| 95 | + - For each underlying token, details such as name, supply, borrow amounts, and price are fetched. |
| 96 | + - This data is combined and stored in `allAssets`. |
| 97 | + - It also calculates the total sum of supplies and borrows and updates the parent component's state using `setTotalSuppliesSum` and `setTotalBorrowsSum`. |
| 98 | + |
| 99 | +3. Rendering: |
| 100 | + - If data is loaded (`!isLoading`), it displays a table with the asset details. |
| 101 | + - Each row includes the asset name, address (with a link to the Shimmer EVM explorer), total borrow, total supply, collateral factor, and price. |
| 102 | + - Clicking on a row selects the asset by calling `setSelectedAsset`. |
| 103 | + |
| 104 | +4. Loading State: |
| 105 | + |
| 106 | +While data is being fetched, it shows a loading spinner with a message. |
| 107 | +This component serves as a dynamic asset list for users to view and interact with supported tokens in the platform. |
| 108 | + |
| 109 | +```javascript reference |
| 110 | +https://github.com/iota-community/Defi-lend-borrow/blob/ee1dd2bd3f94ec594163c153886e4c0457654a5b/lend-borrow-ui/src/components/LendBorrowPlatformDetails/AllAssetsList.js#L1-L126 |
| 111 | +``` |
| 112 | + |
| 113 | + |
| 114 | +### `TransactionsCard` |
| 115 | + |
| 116 | +This component manages and displays a card for performing different types of transactions (like `Supply` or `Borrow`) based on the selected asset. It uses state named `activeTab` to Keeps track of the currently active transaction type (e.g., "Supply", "Withdraw"). |
| 117 | + |
| 118 | + - `TabsPanel`: Allows users to switch between different transaction types. |
| 119 | + - `TransactionForm`: Displays the form for performing the selected transaction on the selectedAsset. |
| 120 | + |
| 121 | + #### `TransactionForm` |
| 122 | + |
| 123 | +This component facilitates token transactions (`Supply`, `Withdraw`, `Borrow` or `Repay`) based on the selected asset. It: |
| 124 | + |
| 125 | +Manages user input for transaction amounts and tracks token balances. |
| 126 | +Updates the UI dynamically based on the active transaction type. |
| 127 | +Connects to the wallet and opens a modal to confirm the transaction. |
| 128 | +Calls specific transaction functions like `mintItokens`, `borrowItokens`, `redeemItokens` or `repayItokens` based on the active tab. |
| 129 | +Displays transaction alerts to indicate success or failure after confirmation. |
| 130 | + |
| 131 | +```javascript reference |
| 132 | +https://github.com/iota-community/Defi-lend-borrow/blob/ee1dd2bd3f94ec594163c153886e4c0457654a5b/lend-borrow-ui/src/components/TransactionsCard/TransactionForm.js#L1-L154 |
| 133 | +``` |
| 134 | + |
| 135 | +:::tip |
| 136 | +You can refer this repository for full React UI code : [lend-borrow-ui](https://github.com/iota-community/Defi-lend-borrow/tree/main/lend-borrow-ui) |
| 137 | +::: |
| 138 | + |
| 139 | +### Usage Example |
| 140 | + |
| 141 | +- Connect your Metamask wallet: Click the "Connect Wallet" button on the app to open the below metamask popup: |
| 142 | + |
| 143 | +  |
| 144 | + |
| 145 | +- View your account balance: See your current token balance. |
| 146 | +  |
| 147 | +- View the Dashboard component: |
| 148 | +  |
| 149 | + - In the Dashboard component you can view `total supplied` funds and `total borrowed` funds and the list of all the supported tokens with their details. |
| 150 | + |
| 151 | +- `TransactionsCard` could be used by clicking any of the token mentioned on the list to `supply`, `borrow`, `repay` or `withdraw` funds. |
| 152 | +  |
| 153 | + - Example of Supplying tokens below : |
| 154 | + - Enter the amount you want to supply and click transact. |
| 155 | + - First you will need to approve the underlying token to the contract. |
| 156 | + |
| 157 | +  |
| 158 | + - And a new metamask popup will be shown to mint the eqvivalent amount of ITokens. |
| 159 | + |
| 160 | +  |
| 161 | + |
| 162 | +Similarly you can `borrow`, `repay` or `withdraw` funds. |
| 163 | + |
| 164 | + |
| 165 | +## Conclusion |
| 166 | + |
| 167 | +This is the last document of DeFi Lend Borrow tutorial, in which we learn how to setup and use the react application to interact with the contracts we created in Part I. |
0 commit comments