Skip to content

Commit

Permalink
upgrade ethers to v6
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanaichi committed Apr 1, 2024
1 parent 2fc391e commit 813f937
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 188 deletions.
6 changes: 3 additions & 3 deletions basic/21-aave-uni-loan/README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ cp .env.example .env
- 部署合约
```shell
// depoly aaveape
hardhat run scripts/deploy.js --network matic
npx hardhat ignition deploy ./ignition/modules/AaveApe.js --network matic
```

- 验证合约
Expand All @@ -65,13 +65,13 @@ npx hardhat verify --network matic 0x4699f609F4FD97A3cf74CB63EFf5cd1200Dfe3dA "0
- 进行借贷
```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 Down
5 changes: 3 additions & 2 deletions basic/21-aave-uni-loan/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ For more detail , please visit [aave-ape](https://azfuller20.medium.com/aave-ape

```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 0x4699f609F4FD97A3cf74CB63EFf5cd1200Dfe3dA "0xa97684ead0e402dC232d5A977953DF7ECBaB3CDb" "0xE592427A0AEce92De3Edee1F18E0157C05861564"

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

```

Expand Down
9 changes: 1 addition & 8 deletions basic/21-aave-uni-loan/hardhat.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
require("dotenv").config();

require("@nomiclabs/hardhat-waffle");
require("hardhat-gas-reporter");
require("solidity-coverage");
const { utils } = require("ethers");
const fs = require("fs");

const { isAddress, getAddress, formatUnits, parseUnits } = utils;
require("@nomicfoundation/hardhat-toolbox");

// This is a sample Hardhat task. To learn how to create your own go to
// https://hardhat.org/guides/create-task.html
Expand Down
22 changes: 22 additions & 0 deletions basic/21-aave-uni-loan/ignition/modules/AaveApe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { buildModule } = require("@nomicfoundation/hardhat-ignition/modules");

let mainnetConfig = {
lendingPoolAddressesProvider: "0x2f39d218133AFaB8F2B819B1066c7E434Ad94E9e",
uniswapRouterAddress: "0xE592427A0AEce92De3Edee1F18E0157C05861564"
}

let maticConfig = {
lendingPoolAddressesProvider: "0xa97684ead0e402dC232d5A977953DF7ECBaB3CDb",
//uniswap
uniswapRouterAddress: "0xE592427A0AEce92De3Edee1F18E0157C05861564"
}

let contractParams = mainnetConfig;

module.exports = buildModule("AaveApe", (m) => {
const lendingPoolAddressesProviderAddress = m.getParameter("lendingPoolAddressesProviderAddress", contractParams.lendingPoolAddressesProvider);
const uniswapRouterAddress = m.getParameter("uniswapRouterAddress", contractParams.uniswapRouterAddress);

const aaveApe = m.contract("AaveApe", [lendingPoolAddressesProviderAddress, uniswapRouterAddress]);
return { aaveApe };
});
24 changes: 4 additions & 20 deletions basic/21-aave-uni-loan/package.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,14 @@
{
"name": "hardhat-project",
"devDependencies": {
"@nomiclabs/hardhat-ethers": "^2.2.3",
"@nomiclabs/hardhat-waffle": "^2.0.1",
"@uniswap/v3-core": "^1.0.1",
"chai": "^4.3.4",
"@nomicfoundation/hardhat-toolbox": "^5.0.0",
"dotenv": "^10.0.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-config-standard": "^16.0.3",
"eslint-plugin-import": "^2.24.2",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.4.1",
"eslint-plugin-promise": "^5.1.0",
"ethereum-waffle": "^3.4.0",
"fs": "0.0.1-security",
"hardhat": "^2.22.2",
"hardhat-gas-reporter": "^1.0.4",
"prettier": "^2.4.1",
"prettier-plugin-solidity": "^1.0.0-beta.18",
"ramda": "^0.27.1",
"solhint": "^3.3.6",
"solidity-coverage": "^0.7.17"
"hardhat": "^2.22.2"
},
"dependencies": {
"axios": "^0.26.1",
"graphql": "^16.3.0"
"axios": "^1.6.8",
"graphql": "^16.8.1"
}
}
120 changes: 0 additions & 120 deletions basic/21-aave-uni-loan/scripts/deploy.js

This file was deleted.

26 changes: 12 additions & 14 deletions basic/21-aave-uni-loan/scripts/loan.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
require('@nomiclabs/hardhat-waffle');
const { use, expect } = require('chai');

//https://docs.aave.com/developers/deployed-contracts/v3-mainnet/ethereum-mainnet
// let daiAddress = '0x6B175474E89094C44Da98b954EedeAC495271d0F';
// let wethAddress = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'; //weth
Expand All @@ -14,7 +11,7 @@ const { use, expect } = require('chai');
// let wethGatewayAddress = '0x893411580e590D62dDBca8a703d61Cc4A8c7b2b9'; //WrappedTokenGatewayV3

// // Fill in your address
// const aaveApeAddress = '0x4Dd5336F3C0D70893A7a86c6aEBe9B953E87c891';
// const aaveApeAddress = '0x90A3B384F62f43Ba07938EA43aEEc35c2aBfeCa2';

//https://docs.aave.com/developers/deployed-contracts/v3-mainnet/polygon
let daiAddress = '0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063';
Expand All @@ -34,7 +31,7 @@ const aaveApeAddress = '0x4699f609F4FD97A3cf74CB63EFf5cd1200Dfe3dA';
const depositEthInAave = async (pooladdress, _userAddress, _amount) => {
const ethGateway = await ethers.getContractAt('IWrappedTokenGatewayV3', wethGatewayAddress);
let metadata = {
value: ethers.utils.parseEther(_amount),
value: ethers.parseEther(_amount),
};
let ethDeposit = await ethGateway.depositETH(pooladdress, _userAddress, 0, metadata);
await ethDeposit.wait();
Expand Down Expand Up @@ -84,30 +81,31 @@ const getDebtToken = async (_asset, _interestRateMode, erc20 = false) => {

const delegateCreditToTheApe = async (_asset, _interestRateMode = 2) => {
let assetDebtToken = await getDebtToken(_asset, _interestRateMode);
console.log('assetDebtToken: ', assetDebtToken.address);
let assetDebtApproval = await assetDebtToken['approveDelegation'](aaveApeAddress, ethers.constants.MaxUint256);
console.log('assetDebtToken: ', assetDebtToken.target);
let assetDebtApproval = await assetDebtToken['approveDelegation'](aaveApeAddress, ethers.MaxUint256);
await assetDebtApproval.wait();
console.log('assetDebtApproval successfully ');
};

main = async () => {
const [deployer] = await ethers.getSigners()
console.log("user address:", deployer.address)

let userAddress = deployer.address;
console.log("user address:", userAddress)

const aaveApe = await ethers.getContractAt('AaveApe', aaveApeAddress);

const lendingpool = await getLendingPool();
console.log('lendingpool:', lendingpool.address);
console.log('lendingpool:', lendingpool.target);
let reserveData = await aaveApe.getAaveAssetReserveData(daiAddress);

// variable debt
let interestRateMode = 2;

await depositEthInAave(lendingpool.address, deployer.address, '0.1');
await depositEthInAave(lendingpool.target, userAddress, '0.1');
await delegateCreditToTheApe(daiAddress, interestRateMode);

let result = await aaveApe.getAvailableBorrowInAsset(daiAddress, deployer.address);
let result = await aaveApe.getAvailableBorrowInAsset(daiAddress, userAddress);
console.log('available borrow: ', result.toString());
console.log('begin ape');

Expand All @@ -120,7 +118,7 @@ main = async () => {
let debtToken = await getDebtToken(daiAddress, interestRateMode, true);
// console.log("debtToken: ", debtToken)

tx = await aToken.approve(aaveApe.address, ethers.constants.MaxUint256);
tx = await aToken.approve(aaveApe.target, ethers.MaxUint256);
await tx.wait();
console.log('atoken approve successfully. ');

Expand All @@ -137,10 +135,10 @@ main = async () => {
let debtBalanceAfter = await debtToken.balanceOf(userAddress);
console.log('debtBalanceAfter: ', debtBalanceAfter.toString());

await aToken.approve(wethGatewayAddress,ethers.constants.MaxUint256 )
await aToken.approve(wethGatewayAddress,ethers.MaxUint256 )
console.log('atoken approve successfully. ');

await withdrawEthInAave(lendingpool.address, userAddress, aBalanceAfter)
await withdrawEthInAave(lendingpool.target, userAddress, aBalanceAfter)
};

main()
Expand Down
24 changes: 11 additions & 13 deletions basic/21-aave-uni-loan/scripts/query.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
require('@nomiclabs/hardhat-waffle');
const { BigNumber } = require('@ethersproject/bignumber');
const axios = require('axios')
require("dotenv").config();

let exp = BigNumber.from("10").pow(18);
let exp1 = BigNumber.from("10").pow(27);
let exp = BigInt(10 ** 18);
let exp1 = BigInt(10 ** 27);

// matic address
//https://docs.aave.com/developers/deployed-contracts/v3-mainnet/polygon
Expand Down Expand Up @@ -186,7 +184,7 @@ main = async () => {
const aaveApe = await ethers.getContractAt('AaveApe', aaveApeAddress);

const lendingPool = await getLendingPool();
console.log('lendingPool:', lendingPool.address);
console.log('lendingPool:', lendingPool.target);
let reserveData = await aaveApe.getAaveAssetReserveData(daiAddress);
//console.log("dai reserveData: " , reserveData )

Expand All @@ -197,19 +195,19 @@ main = async () => {


// console.log("userdata: ", useraccount);
console.log("healthFactor: ", useraccount.healthFactor.mul(BigNumber.from("100")).div(exp).toString());
console.log("totalCollateralBase: %f USD", useraccount.totalCollateralBase.mul(BigNumber.from(100)).div(10**8).toNumber() / 100);
console.log("currentLiquidationThreshold: %f %", useraccount.currentLiquidationThreshold.toNumber() / 100);
console.log("ltv: %f %", useraccount.ltv.toNumber() / 100);
console.log("healthFactor: ", Number(useraccount.healthFactor * BigInt(100) / exp) / 100);
console.log("totalCollateralBase: %f USD", Number(useraccount.totalCollateralBase * BigInt(100) / BigInt(10**8)) / 100);
console.log("currentLiquidationThreshold: %f %", Number(useraccount.currentLiquidationThreshold) / 100);
console.log("ltv: %f %", Number(useraccount.ltv) / 100);

let reserveData1 = await lendingPool.getReserveData(daiAddress);
console.log("dai borrow variable rate: %f %", reserveData1.currentVariableBorrowRate.mul(BigNumber.from("10000")).div(exp1).toNumber() / 100);
console.log("dai borrow variable rate: %f %", Number(reserveData1.currentVariableBorrowRate * BigInt(10000) / exp1) / 100);
//console.log("usdc borrow stable rate: ", reserveData1.currentStableBorrowRate.mul(BigNumber.from("10000")).div(exp1).toString());
console.log("dai supply rate: %f %", reserveData1.currentLiquidityRate.mul(BigNumber.from("10000")).div(exp1).toNumber() / 100);
console.log("dai borrow index: ", reserveData1.variableBorrowIndex.mul(BigNumber.from("10000")).div(exp1).toNumber() / 100);
console.log("dai supply rate: %f %", Number(reserveData1.currentLiquidityRate * BigInt(10000) / exp1) / 100);
console.log("dai borrow index: ", Number(reserveData1.variableBorrowIndex * BigInt(10000) / exp1) / 100);

let result = await aaveApe.getAvailableBorrowInAsset(daiAddress, fish);
console.log('user available borrow dai: %f USD', result.mul(BigNumber.from(1000)).div(exp).toNumber() / 1000);
console.log('user available borrow dai: %f USD', Number(result * BigInt(1000) / exp) / 1000);


let aToken = await getAToken(wmaticAddress);
Expand Down
Loading

0 comments on commit 813f937

Please sign in to comment.