From 813f937b5d56c5f84bcb2ddf84fa036345a0f058 Mon Sep 17 00:00:00 2001 From: speedx <807088414@qq.com> Date: Mon, 1 Apr 2024 20:07:26 +0800 Subject: [PATCH] upgrade ethers to v6 --- basic/21-aave-uni-loan/README-CN.md | 6 +- basic/21-aave-uni-loan/README.md | 5 +- basic/21-aave-uni-loan/hardhat.config.js | 9 +- .../ignition/modules/AaveApe.js | 22 ++++ basic/21-aave-uni-loan/package.json | 24 +--- basic/21-aave-uni-loan/scripts/deploy.js | 120 ------------------ basic/21-aave-uni-loan/scripts/loan.js | 26 ++-- basic/21-aave-uni-loan/scripts/query.js | 24 ++-- basic/21-aave-uni-loan/test/test.js | 15 +-- 9 files changed, 63 insertions(+), 188 deletions(-) create mode 100644 basic/21-aave-uni-loan/ignition/modules/AaveApe.js delete mode 100644 basic/21-aave-uni-loan/scripts/deploy.js diff --git a/basic/21-aave-uni-loan/README-CN.md b/basic/21-aave-uni-loan/README-CN.md index ba5883c4e..4d322fbce 100644 --- a/basic/21-aave-uni-loan/README-CN.md +++ b/basic/21-aave-uni-loan/README-CN.md @@ -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 ``` - 验证合约 @@ -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 ``` diff --git a/basic/21-aave-uni-loan/README.md b/basic/21-aave-uni-loan/README.md index 4cc1692b5..7ae8b4315 100644 --- a/basic/21-aave-uni-loan/README.md +++ b/basic/21-aave-uni-loan/README.md @@ -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 ``` diff --git a/basic/21-aave-uni-loan/hardhat.config.js b/basic/21-aave-uni-loan/hardhat.config.js index ca737f8eb..27420e3da 100644 --- a/basic/21-aave-uni-loan/hardhat.config.js +++ b/basic/21-aave-uni-loan/hardhat.config.js @@ -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 diff --git a/basic/21-aave-uni-loan/ignition/modules/AaveApe.js b/basic/21-aave-uni-loan/ignition/modules/AaveApe.js new file mode 100644 index 000000000..004e8df61 --- /dev/null +++ b/basic/21-aave-uni-loan/ignition/modules/AaveApe.js @@ -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 }; +}); \ No newline at end of file diff --git a/basic/21-aave-uni-loan/package.json b/basic/21-aave-uni-loan/package.json index 80a7897d4..6edc2f1e8 100644 --- a/basic/21-aave-uni-loan/package.json +++ b/basic/21-aave-uni-loan/package.json @@ -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" } } diff --git a/basic/21-aave-uni-loan/scripts/deploy.js b/basic/21-aave-uni-loan/scripts/deploy.js deleted file mode 100644 index 7d503ea8a..000000000 --- a/basic/21-aave-uni-loan/scripts/deploy.js +++ /dev/null @@ -1,120 +0,0 @@ -/* eslint no-use-before-define: "warn" */ -const fs = require("fs"); -const chalk = require("chalk"); -const { config, ethers, tenderly } = require("hardhat"); -const R = require("ramda"); - -const main = async () => { - - console.log("\n\n 📡 Deploying...\n"); - - const [deployer] = await ethers.getSigners(); - - console.log( - "Deploying contracts with the account:", - deployer.address - ); - - let mainnetConfig = { - lendingPoolAddressesProvider: "0x2f39d218133AFaB8F2B819B1066c7E434Ad94E9e", - uniswapRouterAddress: "0xE592427A0AEce92De3Edee1F18E0157C05861564" - } - - let maticConfig = { - lendingPoolAddressesProvider: "0xa97684ead0e402dC232d5A977953DF7ECBaB3CDb", - //uniswap - uniswapRouterAddress: "0xE592427A0AEce92De3Edee1F18E0157C05861564" - } - - // Kovan Aave has a dedicated mock Uniswap contract... https://kovan.etherscan.io/address/0xC18451d36aA370fDACe8d45839bF975F48f7AEa1#readContract - let kovanConfig = { - lendingPoolAddressesProvider: "0x652B2937Efd0B5beA1c8d54293FC1289672AFC6b", - uniswapRouterAddress: "0xfcd87315f0e4067070ade8682fcdbc3006631441" - } - - let deployConfig = maticConfig - console.log("lendingPoolAddressesProvider ", deployConfig.lendingPoolAddressesProvider) - - const aaveApe = await deploy("AaveApe",[deployConfig.lendingPoolAddressesProvider, deployConfig.uniswapRouterAddress]) - - console.log( - " 💾 Artifacts (address, abi, and args) saved to: ", - chalk.blue("packages/hardhat/artifacts/"), - "\n\n" - ); - - // Lengding pool deposite eth - - // borrow - - // use aaveape to flashloan to repay borrow - -}; - -const deploy = async (contractName, _args) => { - console.log(` 🛰 Deploying: ${contractName}`); - - const contractArgs = _args || []; - const contractArtifacts = await ethers.getContractFactory(contractName); - const deployed = await contractArtifacts.deploy(...contractArgs); - const encoded = abiEncodeArgs(deployed, contractArgs); - fs.writeFileSync(`artifacts/${contractName}.address`, deployed.address); - - console.log( - " 📄", - chalk.cyan(contractName), - "deployed to:", - chalk.magenta(deployed.address), - ); - - if (!encoded || encoded.length <= 2) return deployed; - fs.writeFileSync(`artifacts/${contractName}.args`, encoded.slice(2)); - - return deployed; -}; - - -// ------ utils ------- - -// abi encodes contract arguments -// useful when you want to manually verify the contracts -// for example, on Etherscan -const abiEncodeArgs = (deployed, contractArgs) => { - // not writing abi encoded args if this does not pass - if ( - !contractArgs || - !deployed || - !R.hasPath(["interface", "deploy"], deployed) - ) { - return ""; - } - - const encoded = ethers.utils.defaultAbiCoder.encode( - deployed.interface.deploy.inputs, - contractArgs - ); - return encoded; -}; - -// checks if it is a Solidity file -const isSolidity = (fileName) => - fileName.indexOf(".sol") >= 0 && fileName.indexOf(".swp") < 0; - -const readArgsFile = (contractName) => { - let args = []; - try { - const argsFile = `./contracts/${contractName}.args`; - if (!fs.existsSync(argsFile)) return args; - args = JSON.parse(fs.readFileSync(argsFile)); - } catch (e) { - console.log(e); - } - return args; -}; - -main() - .then(() => process.exit(0)) - .catch((error) => { - console.error(error); - process.exit(1); - }); diff --git a/basic/21-aave-uni-loan/scripts/loan.js b/basic/21-aave-uni-loan/scripts/loan.js index cb9ad254f..6eb04e0ca 100644 --- a/basic/21-aave-uni-loan/scripts/loan.js +++ b/basic/21-aave-uni-loan/scripts/loan.js @@ -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 @@ -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'; @@ -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(); @@ -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'); @@ -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. '); @@ -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() diff --git a/basic/21-aave-uni-loan/scripts/query.js b/basic/21-aave-uni-loan/scripts/query.js index 7632744eb..7ca95d474 100644 --- a/basic/21-aave-uni-loan/scripts/query.js +++ b/basic/21-aave-uni-loan/scripts/query.js @@ -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 @@ -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 ) @@ -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); diff --git a/basic/21-aave-uni-loan/test/test.js b/basic/21-aave-uni-loan/test/test.js index 314056eb3..9f5b54268 100644 --- a/basic/21-aave-uni-loan/test/test.js +++ b/basic/21-aave-uni-loan/test/test.js @@ -1,5 +1,4 @@ -require("@nomiclabs/hardhat-waffle"); -const { use, expect } = require("chai"); +const { expect } = require("chai"); // ethereum mainnet addresses let daiAddress = "0x6B175474E89094C44Da98b954EedeAC495271d0F" @@ -12,10 +11,10 @@ let uniswapRouterAddress = "0xE592427A0AEce92De3Edee1F18E0157C05861564" let wethGatewayAddress = "0x893411580e590D62dDBca8a703d61Cc4A8c7b2b9" const depositEthInAave = async (_poolAddress, _userAddress, _amount) => { - + // console.log("isAddressable", _poolAddress, _userAddress, ethers.isAddressable(_poolAddress), ethers.isAddressable(_userAddress)) 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) @@ -61,7 +60,7 @@ const getDebtToken = async (_asset, _interestRateMode, erc20=false) => { const delegateCreditToTheApe = async (_asset, _interestRateMode = 2) => { let assetDebtToken = await getDebtToken(_asset, _interestRateMode) - let assetDebtApproval = await assetDebtToken['approveDelegation'](aaveApe.address, ethers.constants.MaxUint256) + let assetDebtApproval = await assetDebtToken['approveDelegation'](aaveApe.target, ethers.MaxUint256) } describe("AaveApe", function () { @@ -73,8 +72,8 @@ describe("AaveApe", function () { const [user] = await ethers.getSigners(); userAddress = user.address; const lendingpool = await getLendingPool(); - pooladdress = lendingpool.address; - }); + pooladdress = lendingpool.target; + }); describe("Address verification", function () { @@ -169,7 +168,7 @@ describe("AaveApe", function () { let aToken = await getAToken(wethAddress) let debtToken = await getDebtToken(daiAddress, interestRateMode, true) - await aToken.approve(aaveApe.address, ethers.constants.MaxUint256) + await aToken.approve(aaveApe.target, ethers.MaxUint256) let aBalanceBefore = await aToken.balanceOf(userAddress) let debtBalanceBefore = await debtToken.balanceOf(userAddress)