Building Full Stack DeFi Applications: A practical guide to creating your own decentralized finance projects on blockchain [1 ed.] 9781837634118

Enter the world of Decentralized Finance (DeFi) with Building Full Stack DeFi Applications. Understand how this blockcha

131 50 17MB

English Pages 629 Year 2024

Report DMCA / Copyright

DOWNLOAD EPUB FILE

Table of contents :
Building Full Stack DeFi Applications
Contributors
About the author
About the reviewer
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Conventions used
Get in touch
Share Your Thoughts
Download a free PDF copy of this book
Part 1: Introduction to DeFi Application Development
1
Introduction to DeFi
What is DeFi?
Characteristics of DeFi
Understanding terminologies
Overview of DeFi applications
Decentralized exchanges
Crypto loans
Staking, yield farming, and liquidity mining
Architecture of DeFi applications
DeFi application frontend
DeFi wallet
RPC endpoint
Interactions between blockchain and oracle
Vulnerabilities of DeFi applications
Reentrancy
Self-destruct operation
Gas overflow
Random number manipulation
Summary
Further reading
2
Getting Started with DeFi Application Development
Technical requirements
Creating a DeFi project
Installing Node.js
Creating a project with create-react-app
Installing and configuring Hardhat
Writing, compiling, and deploying a smart contract in a local environment
Writing and compiling a smart contract
Bytecode and ABI
Running a local EVM environment
Deploying the smart contract
Deploying a smart contract on Testnet
Acquiring the RPC endpoint of Testnet
Configuring the deployer account
Smart contract deployment and verification
Adding a deployment script to package.json
Testing and debugging the smart contract
Verifying smart contract with the Hardhat console
Writing and running automated tests for smart contracts
Debugging smart contracts with Hardhat
Summary
Further reading
3
Interacting with Smart Contracts and DeFi Wallets in the Frontend
Technical requirements
Overview of DeFi application frontend development
Running the UI wireframe
Deploying smart contract metadata to the frontend
Connecting to a DeFi wallet
The architecture of DeFi wallet connectivity with blockchain
Implementing a wallet connection in the DeFi project
Reading data from blockchain
Making transactions on blockchain
Summary
Part 2: Design and Implementation of a DeFi Application for Trading Cryptos
4
Introduction to Decentralized Exchanges
The three main types of DEXs
AMMs
Order book DEXs
DEX aggregators
The mathematics of AMMs
Relation functions
Constant Function Market Maker (CFMM)
Liquidity mining and burning
Impermanent loss
The architecture of AMM
Adding liquidity
Removing liquidity
Swapping
Summary
5
Building Crypto-Trading Smart Contracts
Implementing token pair smart contracts
Creating the skeleton of a token pair
Initializing token pairs
Storing retrieving token reserves
Transferring tokens safely
Minting LP tokens
Reward distribution for liquidity providers and DEX owners
Minting LP tokens for the DEX owner’s reward
Burning liquidity pool tokens
Swapping token
skim and sync
Implementing pair factory smart contracts
Introducing the smart contract source file
Creating token pairs
Retrieving addresses for token pairs
Verifying the token pair factory
Implementing AMM router smart contracts
Liquidity provisioning
Liquidity removal
Swapping
Verifying DEX smart contracts
Deploying AMMRouter with a script
Verifying smart contracts using the Hardhat console
Summary
6
Implementing a Liquidity Management Frontend with Web3
Implementing URL routes for liquidity management
Retrieving liquidity information
Checking whether the wallet is connected
Getting LP tokens owned by a connected account
Getting the pooled token amount and liquidity share percentage
Finishing up the liquidity listing page
Implementing the liquidity provisioning page
Overview of the liquidity provisioning page
Frontend workflow of liquidity provisioning
Loading the token pair information from the search parameter
Selecting tokens and providing token amounts
Checking the allowance and increasing the allowance
Interacting with smart contracts for adding liquidity
Implementing the liquidity removal page
Frontend workflow of liquidity removal
Inputting the LP token amount
Calculating pooled token amounts
Getting the allowance, increasing the allowance, and removing liquidity
Verifying liquidity management pages
Summary
7
Implementing a Token-Swapping Frontend with Web3
Overview of the token swapping frontend
Frontend workflow of token swapping
Preparing for the token swapping page
Generating token swapping paths
Building the graph for token pairs
Finding all paths given a pair of tokens
The default token pair
Identifying the best path, price, and price Impact
How does the best path change for different amounts?
Why the reserve can be insufficient
Calculating the receiving and spending amounts with code
Calculating the price impact
Swapping token – after a wallet is connected
Improving user experiences for token swapping
Summary
8
Working with Native Tokens
Diving into the WETH smart contract
Demystifying the WETH smart contract
Verifying a WETH smart contract with the Hardhat console
Refactoring smart contracts to support native tokens in a DEX
The router as the intermediary
Function definitions for native tokens in the router
Improving AMMRouter to support the native token
Implementing addLiquidityETH and removeLiquidityETH
Implementing token-swapping functions to support ETH
Implementing the DEX frontend for the native token
Supporting the native token in the liquidity management pages
Supporting the native token on the token-swapping page
Summary
Part 3: Building a DeFi Application for Staking and Yield Farming
9
Building Smart Contracts for Staking and Farming
Understanding the architectures of staking and farming
Two types of architectures for staking
Calculating the reward for staking and farming
Reward per block, starting block, and ending block
Share, reward per share, and reward debt
Implementing the staking pool smart contract
Defining smart contract variables and implementing a constructor
Updating the parameters for the staking pool
Implementing the deposit and withdraw functions
Implementing the utility functions of the staking pool
Implementing the smart contract for staking pool management
Verifying staking pool smart contracts
Implementing the command to mine blocks with Hardhat
Verifying staking pool smart contracts in the Hardhat console
Summary
10
Implementing a Frontend for Staking and Farming
Overview of frontend pages for staking and farming
Implementing a staking pool listing dashboard
Retrieve staking pools
Use the accordion component to show the list
Hide expired pools
Implementing pages for creating staking pools and supplying rewards
Improving the token selection modal component
Implementing a page to create a staking pool
Implementing a page for supplying rewards
Implementing frontend components for deposits, withdrawals, and harvesting
Deposit page for staking pools
Withdrawal page for staking pools
Implementing the harvest function
Implementing the farming frontend
Refactoring frontend code for farming
Summary
Part 4: Building a Crypto Loan App for Lending and Borrowing
11
An Introduction to Crypto Loans
Technical requirements
Exploring the characteristics of a crypto loan
Zero waiting time for approval
No credit checks
No term constraints
No requirement for selling out of crypto holdings
Designing crypto loan smart contracts
Crypto loan personas
The architecture of a crypto loan smart contract
Understanding interest rate model and pool configuration
The borrowing interest rate and lending interest rate
Demystifying the interest rate model
The collateral rate
The liquidation bonus rate
Implementing the pool configuration smart contract
Implementing an asset pool share and its deployer
Introducing an asset pool share
Implementing the asset pool share smart contract
Implementing the asset pool share deployer
Exploring a crypto loan system by example
An introduction to Aave
An Aave protocol version 2 overview
New features in the Aave protocol version 3
Summary
12
Implementing an Asset Pool Smart Contract for a Crypto Loan
Technical requirements
Implementing the code to manage the asset pools
Pool status
Pool management
Pool parameter calculation
Managing records in user ledgers
Amount conversion between shares and asset tokens
Retrieving user-lending and -borrowing information
User account healthiness
Implementing the functions for user requests
Depositing
Withdrawal
Borrowing
Repaying
Liquidation
Withdrawing the reserve
Deploying and testing the crypto loan smart contracts
Deploying crypto loan smart contracts
Configuring asset pools for crypto loans
Testing crypto loan smart contracts
Summary
13
Implementing a Price Oracle for Crypto Loans
How price manipulation attacks are carried out on crypto loan systems
Executing a crypto loan exploit
An example of attacking crypto loan systems with price manipulation
Building an on-chain price oracle with cumulative prices
Calculating cumulative prices
Calculating time-weighted average prices
Calculating prices with the moving window
Implementing a manipulation-resistant price oracle smart contract
Updating information when the reserve amount changes in a liquidity pool
Information update for the current period in the price oracle
Calculating the token price in the price oracle
Deploying, maintaining, and verifying the price oracle
Deploying the PriceOracleV2 smart contract
Price oracle maintenance
Verifying the manipulation-resistant price oracle
Summary
14
Implementing the Crypto Loan Frontend with Web3
Technical requirements
Implementing the account summary and asset pool listing page
Implementing the UI components in ListAssetPools.js
Retrieving the user summary information for the crypto loan system
Retrieving the information for all asset pools
Implementing the pages for deposit, withdrawal, borrowing, and repayment
Implementing the deposit page
Implementing the withdrawal page
Implementing the borrowing page
Implementing the repayment page
Best practices for decentralized crypto loan systems
Select blue chip assets
Liquidity utilization rate maintenance
Monitoring and liquidating unhealthy accounts
Closed asset pools
Disabling the use of assets as collateral
Summary
Index
Why subscribe?
Other Books You May Enjoy
Packt is searching for authors like you
Share Your Thoughts
Download a free PDF copy of this book

Building Full Stack DeFi Applications: A practical guide to creating your own decentralized finance projects on blockchain [1 ed.]
 9781837634118

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
Recommend Papers