Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX(basic): upgrade basic task 21 to Aave V3, Uniswap V3 #1073

Merged
merged 6 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions basic/21-aave-uni-loan/README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
基于 Hardhat 测试框架,通过样例合约的演示,了解如何在 AAVE 上进行借贷,在 Uniswap 上进行兑换,并通过 AAVE 和 Uniswap 进行做多和做空。

## 合约交互
### AAVE 合约
Lending Pool Addresses Provider 是 AAVE 交互的入口,通过它可以和 AAVE 进行交互。
同时因为 AAVE 会更新 LendingPool / ProtocolDataProvider / PriceOracle 合约的地址以便修复发现的 Bug ,而通过 Lending Pool Addresses Provider 可以获取这三个合约的最新的地址,而不必修改代码:
- LendingPool: 负责 存入/借贷 功能
- ProtocolDataProvider: 提供 用户/池子 中相关的借贷信息
### AAVE V3 合约
Pool Addresses Provider 是 AAVE V3 交互的入口,通过它可以和 AAVE V3 进行交互。
同时因为 AAVE 会更新 Pool / PoolDataProvider / PriceOracle 合约的地址以便修复发现的 Bug ,而通过 Lending Pool Addresses Provider 可以获取这三个合约的最新的地址,而不必修改代码:
- Pool 负责 存入/借贷 功能
- PoolDataProvider: 提供 用户/池子 中相关的借贷信息
- PriceOracle: 价格预言机

### uniswapv2 合约
UniswapV2 的交互入口为 Router02,通过它可以完成和 UniswapV2 的所有交互
### uniswap V3 合约
Uniswap V3 的交互入口为 SwapRouter,通过它可以完成和 Uniswap V3 的所有交互


## 功能要点
Expand All @@ -30,12 +30,12 @@ AaveApe 合约主要有四个功能,分别是
### 做空
从 AAVE 借出资产 B 后,Ape 进行如下操作做空 B。

- 通过 Uniswap V2, 把借出的资产 B 全部兑换为 资产 A
- 通过 Uniswap V3, 把借出的资产 B 全部兑换为 资产 A
- 把兑换出的资产 A 继续存入 AAVE


### 赎回
通过 AAVE V2 的闪电贷偿还从 AAVE 借出的资产.
通过 AAVE V3 的闪电贷偿还从 AAVE 借出的资产.
详细逻辑, 参考 [aave-ape](https://azfuller20.medium.com/aave-ape-with-%EF%B8%8F-scaffold-eth-c687874c079e )

## 操作步骤
Expand All @@ -53,25 +53,25 @@ cp .env.example .env
- 部署合约
```shell
// depoly aaveape
hardhat run scripts/deploy.js --network matic
npx hardhat ignition deploy ./ignition/modules/AaveApe.js --network matic
```

- 验证合约
```shell
// contract verify
npx hardhat verify --network matic 0xddb2d92d5a0EDcb03c013322c7BAe92734AA4597 "0xd05e3E715d945B59290df0ae8eF85c1BdB684744" "0x1b02da8cb0d097eb8d57a175b88c7d8b47997506"
npx hardhat verify --network matic 0x4699f609F4FD97A3cf74CB63EFf5cd1200Dfe3dA "0xa97684ead0e402dC232d5A977953DF7ECBaB3CDb" "0xE592427A0AEce92De3Edee1F18E0157C05861564"
```

- 进行借贷
```shell
// open maxposition on aave
hardhat run scripts/loan.js --network matic
npx hardhat run scripts/loan.js --network matic
```

- 查询用户支付利息总额
需要注意的是,应该在 matic 上,所有的可借贷币种的 APY 使用的都是浮动 APY,如果使用其他网络的话需要修改下代码中 “计算 interest” 部分传入 getDebtToken 接口的参数
```shell
hardhat run scripts/query.js --network matic
npx hardhat run scripts/query.js --network matic
```


Expand All @@ -81,10 +81,13 @@ hardhat run scripts/query.js --network matic
- aave-ape 借贷 : https://azfuller20.medium.com/lend-with-aave-v2-20bacceedade
- hardhat fork 主网 : https://hardhat.org/hardhat-network/guides/mainnet-forking.html
- quick swap: https://github.com/QuickSwap
- uniswapper 接口调用 : https://azfuller20.medium.com/swap-with-uniswap-wip-f15923349b3d
- uniswapper : https://azfuller20.medium.com/swap-with-uniswap-wip-f15923349b3d
- uniswap v3 接口调用 : https://solidity-by-example.org/defi/uniswap-v3-swap/
- ds-math 安全数学库: https://medium.com/dapphub/introducing-ds-math-an-innovative-safe-math-library-d58bc88313da
- aave 官方文档: https://docs.aave.com/developers/the-core-protocol/protocol-overview#main-contracts
- aave polygon graph: https://thegraph.com/legacy-explorer/subgraph/aave/aave-v2-matic
- aave 官方文档: https://docs.aave.com/developers/getting-started/contracts-overview
- aave polygon graph: https://thegraph.com/hosted-service/subgraph/aave/protocol-v3-polygon
- scaffold 挑战: https://medium.com/@austin_48503/%EF%B8%8Fethereum-dev-speed-run-bd72bcba6a4c
- scaffold-eth 任务: https://speedrunethereum.com/
- aave 闪电贷: https://github.com/johngrantuk/aaveFlashLoan
- aave 闪电贷: https://github.com/johngrantuk/aaveFlashLoan
- aave 合约地址: https://docs.aave.com/developers/deployed-contracts/v3-mainnet
- uniswap 合约地址: https://docs.uniswap.org/contracts/v3/reference/deployments
32 changes: 18 additions & 14 deletions basic/21-aave-uni-loan/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,46 @@ a simple contract that lets you create and unwind leveraged positions on Aave
The Aave Ape combines two Defi protocols, Aave (for lending) and Uniswap (for swapping).

**AAVE**
Interacting with Aave starts with the Lending Pool Addresses Provider — this is the source of truth for Aave V2’s latest contract addresses:
- LendingPool: where the depositing & borrowing happens 🏦
- ProtocolDataProvider: provides easy-to-access information about reserves & users 📊
Interacting with Aave starts with the Pool Addresses Provider — this is the source of truth for Aave V3’s latest contract addresses:
- Pool: where the depositing & borrowing happens 🏦
- PoolDataProvider: provides easy-to-access information about reserves & users 📊
- PriceOracle: provides secure price feeds 💱

**uniswapv2**
Interacting with Uniswap V2 all happens through the Uniswap Router02.
**uniswap V3**
Interacting with Uniswap V3 all happens through the Uniswap SwapRouter.


**key point**

We can create a constructor that lets us set the two addresses we need (the AddressesProvider and the Router). Once we then have the addresses and the interfaces, we can create helper functions to instantiate contracts so that we can interact with them, and fetch Aave reserve data from the ProtocolDataProvider.
We can create a constructor that lets us set the two addresses we need (the PoolAddressesProvider and the Router). Once we then have the addresses and the interfaces, we can create helper functions to instantiate contracts so that we can interact with them, and fetch Aave reserve data from the PoolDataProvider.

Assuming you have an apeAsset you want to go long and a borrowAsset you want to go short
this ape function lets a user carry out the following steps in one transaction
- Calculate the maximum amount the user is able to borrow in the borrowAsset, based on their collateral (this relies on getAvailableBorrowInAsset)
- Borrow that amount of borrowAsset from Aave V2 on behalf of the user
- Borrow that amount of borrowAsset from Aave V3 on behalf of the user

This requires the user to have delegated credit to the Aave Ape contract, so that it can borrow from Aave on the user’s behalf — see more about Credit Delegation

- Approve the borrowAsset for trading on Uniswap
- Swap the borrowAsset for the maximum available amount of the apeAsset via Uniswap V2
- Swap the borrowAsset for the maximum available amount of the apeAsset via Uniswap V3
- Deposit the apeAsset back into Aave on behalf of the user


**unwindApe**
Borrow the amount needed to repay their borrowAsset debt via a flashloan from Aave V2.
Borrow the amount needed to repay their borrowAsset debt via a flashloan from Aave V3.
For more detail , please visit [aave-ape](https://azfuller20.medium.com/aave-ape-with-%EF%B8%8F-scaffold-eth-c687874c079e )
## operating steps

```shell
// depoly aaveape
hardhat run --network matic scripts/deploy.js
npx hardhat ignition deploy ./ignition/modules/AaveApe.js --network matic


// contract verify
npx hardhat verify --network matic 0xddb2d92d5a0EDcb03c013322c7BAe92734AA4597 "0xd05e3E715d945B59290df0ae8eF85c1BdB684744" "0x1b02da8cb0d097eb8d57a175b88c7d8b47997506"
npx hardhat verify --network matic 0x4699f609F4FD97A3cf74CB63EFf5cd1200Dfe3dA "0xa97684ead0e402dC232d5A977953DF7ECBaB3CDb" "0xE592427A0AEce92De3Edee1F18E0157C05861564"

// open maxposition on aave
hardhat run --network matic scripts/loan.js
npx hardhat run --network matic scripts/loan.js

```

Expand All @@ -56,13 +57,16 @@ hardhat run --network matic scripts/loan.js
- hardhat forkmainnet : https://hardhat.org/hardhat-network/guides/mainnet-forking.html
- quick swap: https://github.com/QuickSwap
- uniswapper : https://azfuller20.medium.com/swap-with-uniswap-wip-f15923349b3d
yanyanho marked this conversation as resolved.
Show resolved Hide resolved
- uniswap v3 swap : https://solidity-by-example.org/defi/uniswap-v3-swap/
- https://github.com/austintgriffith/scaffold-eth/tree/defi-rtokens
- https://github.com/austintgriffith/scaffold-eth/tree/unifactory
- https://github.com/austintgriffith/scaffold-eth/tree/clr-dev
- https://medium.com/dapphub/introducing-ds-math-an-innovative-safe-math-library-d58bc88313da
- aave official doc: https://docs.aave.com/developers/the-core-protocol/protocol-overview#main-contracts
- aave polygon graph: https://thegraph.com/legacy-explorer/subgraph/aave/aave-v2-matic
- aave official doc: https://docs.aave.com/developers/getting-started/contracts-overview
- aave polygon graph: https://thegraph.com/hosted-service/subgraph/aave/protocol-v3-polygon
- scaffold chanllenge https://medium.com/@austin_48503/%EF%B8%8Fethereum-dev-speed-run-bd72bcba6a4c
- scaffold-eth task: https://speedrunethereum.com/
- aave flashloan: https://github.com/johngrantuk/aaveFlashLoan
- axios query subgraph: https://gist.github.com/alejoacosta74/55044445dec594f33c10c432b39f1116
- aave deployed contract addresses: https://docs.aave.com/developers/deployed-contracts/v3-mainnet
- uniswap deployed contract addresses: https://docs.uniswap.org/contracts/v3/reference/deployments
Loading
Loading