Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 22ba7c2

Browse files
authored
feat(doc): create a tutorial for DeFi Lend Borrow UI (#1685)
* Add part I * Update doc permalinks * Update conclusion markdown * Update permalinks * Fix typo * Add part II * Add ui images * Update contract * Fix document * Fix sidebar * Update document * Fix document spacing
1 parent 48043ef commit 22ba7c2

12 files changed

+344
-5
lines changed

docs/build/isc/v1.3/docs/tutorials/defi-lend-borrow-tutorial-part-1.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
DeFi Lend Borrow is a decentralized finance (DeFi) application that enables users to lend and borrow assets on the Shimmer EVM testnet. The project is built using Solidity and Hardhat, with the core functionality provided by smart contracts.
44

5-
65
## Prerequisites
76

87
- [Node.js](https://nodejs.org) >= v18.0
@@ -150,6 +149,7 @@ The `ITokenManager` contract is designed to manage supported tokens and collater
150149
- `tokenCollateralFactor`: The collateral factor for the token.
151150
- **Modifiers:** `onlyOwner`
152151
- **Events:** Emits `TokenAdded` upon successful addition of the token.
152+
153153
```solidity reference
154154
https://github.com/iota-community/Defi-lend-borrow/blob/3a368391f4767d1decb209ad6bfdd20a2b08fa03/contracts/ITokenManager.sol#L57-L86
155155
```
@@ -160,7 +160,6 @@ https://github.com/iota-community/Defi-lend-borrow/blob/3a368391f4767d1decb209ad
160160
- **Modifiers:** `onlyOwner`
161161
- **Events:** Emits `TokenRemoved` upon successful removal of the token.
162162

163-
```solidity reference
164163
https://github.com/iota-community/Defi-lend-borrow/blob/3a368391f4767d1decb209ad6bfdd20a2b08fa03/contracts/ITokenManager.sol#L88-L111
165164
```
166165
@@ -186,6 +185,7 @@ https://github.com/iota-community/Defi-lend-borrow/blob/3a368391f4767d1decb209ad
186185
```
187186

188187
- `preBorrowChecks(address iTokenAddress, address redeemer, uint256 amount)`: Ensures that the redeemer has sufficient collateral to borrow tokens.
188+
189189
- **Parameters:**
190190
- `iTokenAddress`: The address of the `IToken` to be borrowed.
191191
- `redeemer`: The account attempting to borrow.
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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+
![connect metamask](/img/tutorials/defi-lend-borrow/defi-lend-borrow-connect-metamask.png "Connect to MetaMask")
144+
145+
- View your account balance: See your current token balance.
146+
![accoount balance](/img/tutorials/defi-lend-borrow/defi-lend-borrow-account-bal.png "You can view your account SMR balance here")
147+
- View the Dashboard component:
148+
![dashboard](/img/tutorials/defi-lend-borrow/defi-lend-borrow-dashboard.png "Dashboard UI")
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+
![transactions card](/img/tutorials/defi-lend-borrow/defi-lend-borrow-transaction-card.png "Dashboard UI")
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+
![approve underlying token](/img/tutorials/defi-lend-borrow/defi-lend-borrow-approve.png "Approve underlying token")
158+
- And a new metamask popup will be shown to mint the eqvivalent amount of ITokens.
159+
160+
![supply IToken](/img/tutorials/defi-lend-borrow/defi-lend-borrow-mint.png "Supply IToken")
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.

docs/build/isc/v1.3/sidebars.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,14 @@ module.exports = {
282282
items: [
283283
{
284284
type: 'doc',
285-
label: 'Part I',
285+
label: 'Contract',
286286
id: 'tutorials/defi-lend-borrow-tutorial-part-1',
287287
},
288+
{
289+
type: 'doc',
290+
label: 'React UI',
291+
id: 'tutorials/defi-lend-borrow-tutorial-part-2',
292+
},
288293
],
289294
},
290295
{

docs/build/isc/v1.4/docs/tutorials/defi-lend-borrow-tutorial-part-1.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
DeFi Lend Borrow is a decentralized finance (DeFi) application that enables users to lend and borrow assets on the Shimmer EVM testnet. The project is built using Solidity and Hardhat, with the core functionality provided by smart contracts.
44

5-
65
## Prerequisites
76

87
- [Node.js](https://nodejs.org) >= v18.0
@@ -150,6 +149,7 @@ The `ITokenManager` contract is designed to manage supported tokens and collater
150149
- `tokenCollateralFactor`: The collateral factor for the token.
151150
- **Modifiers:** `onlyOwner`
152151
- **Events:** Emits `TokenAdded` upon successful addition of the token.
152+
153153
```solidity reference
154154
https://github.com/iota-community/Defi-lend-borrow/blob/3a368391f4767d1decb209ad6bfdd20a2b08fa03/contracts/ITokenManager.sol#L57-L86
155155
```
@@ -160,7 +160,6 @@ https://github.com/iota-community/Defi-lend-borrow/blob/3a368391f4767d1decb209ad
160160
- **Modifiers:** `onlyOwner`
161161
- **Events:** Emits `TokenRemoved` upon successful removal of the token.
162162

163-
```solidity reference
164163
https://github.com/iota-community/Defi-lend-borrow/blob/3a368391f4767d1decb209ad6bfdd20a2b08fa03/contracts/ITokenManager.sol#L88-L111
165164
```
166165
@@ -186,6 +185,7 @@ https://github.com/iota-community/Defi-lend-borrow/blob/3a368391f4767d1decb209ad
186185
```
187186

188187
- `preBorrowChecks(address iTokenAddress, address redeemer, uint256 amount)`: Ensures that the redeemer has sufficient collateral to borrow tokens.
188+
189189
- **Parameters:**
190190
- `iTokenAddress`: The address of the `IToken` to be borrowed.
191191
- `redeemer`: The account attempting to borrow.

0 commit comments

Comments
 (0)