From 31673ca1860391a456ea2f45f920d36de6cce962 Mon Sep 17 00:00:00 2001 From: Daksh Miglani Date: Wed, 26 May 2021 20:58:01 +0530 Subject: [PATCH] =?UTF-8?q?test:=20=F0=9F=92=8D=20aggregate=20resolvers=20?= =?UTF-8?q?such=20as=20comp,=20aave=20and=20maker?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contracts/aggregate/aaveV2.sol | 49 +++++++++-------- .../{ => aggregate}/common/interfaces.sol | 0 contracts/{ => aggregate}/common/math.sol | 0 .../{ => aggregate}/common/stores-polygon.sol | 0 contracts/{ => aggregate}/common/stores.sol | 0 contracts/aggregate/compound.sol | 51 +++++++++++++----- contracts/aggregate/maker.sol | 52 ++++++++++++------- hardhat.config.js | 18 ++++--- package-lock.json | 11 ++++ package.json | 1 + test/aggregate-resolvers.js | 36 +++++++++++++ 11 files changed, 155 insertions(+), 63 deletions(-) rename contracts/{ => aggregate}/common/interfaces.sol (100%) rename contracts/{ => aggregate}/common/math.sol (100%) rename contracts/{ => aggregate}/common/stores-polygon.sol (100%) rename contracts/{ => aggregate}/common/stores.sol (100%) create mode 100644 test/aggregate-resolvers.js diff --git a/contracts/aggregate/aaveV2.sol b/contracts/aggregate/aaveV2.sol index 00d4486..05f2dbf 100644 --- a/contracts/aggregate/aaveV2.sol +++ b/contracts/aggregate/aaveV2.sol @@ -1,39 +1,43 @@ pragma solidity ^0.7.0; pragma experimental ABIEncoderV2; -import { DSMath } from "../../common/math.sol"; +import {DSMath} from "./common/math.sol"; interface AaveProtocolDataProvider { - function getUserAccountData(address user) external view returns ( - uint256 totalCollateralETH, - uint256 totalDebtETH, - uint256 availableBorrowsETH, - uint256 currentLiquidationThreshold, - uint256 ltv, - uint256 healthFactor - ); + function getUserAccountData(address user) + external + view + returns ( + uint256 totalCollateralETH, + uint256 totalDebtETH, + uint256 availableBorrowsETH, + uint256 currentLiquidationThreshold, + uint256 ltv, + uint256 healthFactor + ); } + interface ChainLinkInterface { function latestAnswer() external view returns (int256); } contract Variables { - ChainLinkInterface public constant ethPriceFeed = ChainLinkInterface(0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419); - AaveProtocolDataProvider public constant aaveDataProvider = AaveProtocolDataProvider(0x7d2768dE32b0b80b7a3454c06BdAc94A69DDc7A9); + ChainLinkInterface public constant ethPriceFeed = + ChainLinkInterface(0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419); + AaveProtocolDataProvider public constant aaveDataProvider = + AaveProtocolDataProvider(0x7d2768dE32b0b80b7a3454c06BdAc94A69DDc7A9); } contract Resolver is Variables, DSMath { - function getPosition(address account) external view returns (uint256 networthInUsd) { - ( - uint256 totalCollateralETH, - uint256 totalDebtETH, - , - , - , - - ) = aaveDataProvider.getUserAccountData(account); - - uint256 ethPrice = mul(uint256(ethPriceFeed.latestAnswer()), 10 ** 10); + function getPosition(address account) + external + view + returns (uint256 networthInUsd) + { + (uint256 totalCollateralETH, uint256 totalDebtETH, , , , ) = + aaveDataProvider.getUserAccountData(account); + + uint256 ethPrice = mul(uint256(ethPriceFeed.latestAnswer()), 10**10); networthInUsd = sub(totalCollateralETH, totalDebtETH); networthInUsd = wmul(networthInUsd, ethPrice); @@ -43,4 +47,3 @@ contract Resolver is Variables, DSMath { contract InstaAaveV2AggregateResolver is Resolver { string public constant name = "AaveV2-Aggregate-Resolver-v1.0"; } - diff --git a/contracts/common/interfaces.sol b/contracts/aggregate/common/interfaces.sol similarity index 100% rename from contracts/common/interfaces.sol rename to contracts/aggregate/common/interfaces.sol diff --git a/contracts/common/math.sol b/contracts/aggregate/common/math.sol similarity index 100% rename from contracts/common/math.sol rename to contracts/aggregate/common/math.sol diff --git a/contracts/common/stores-polygon.sol b/contracts/aggregate/common/stores-polygon.sol similarity index 100% rename from contracts/common/stores-polygon.sol rename to contracts/aggregate/common/stores-polygon.sol diff --git a/contracts/common/stores.sol b/contracts/aggregate/common/stores.sol similarity index 100% rename from contracts/common/stores.sol rename to contracts/aggregate/common/stores.sol diff --git a/contracts/aggregate/compound.sol b/contracts/aggregate/compound.sol index 8449379..902de97 100644 --- a/contracts/aggregate/compound.sol +++ b/contracts/aggregate/compound.sol @@ -1,33 +1,51 @@ pragma solidity ^0.7.0; pragma experimental ABIEncoderV2; -import { DSMath } from "../../common/math.sol"; +import {DSMath} from "./common/math.sol"; interface CTokenInterface { - function exchangeRateCurrent() external returns (uint); - function borrowBalanceCurrent(address account) external returns (uint); - function balanceOfUnderlying(address account) external returns (uint); + function exchangeRateCurrent() external returns (uint256); + + function borrowBalanceCurrent(address account) external returns (uint256); + + function balanceOfUnderlying(address account) external returns (uint256); function underlying() external view returns (address); - function balanceOf(address) external view returns (uint); + + function balanceOf(address) external view returns (uint256); } interface OracleCompInterface { - function getUnderlyingPrice(address) external view returns (uint); + function getUnderlyingPrice(address) external view returns (uint256); } interface ComptrollerLensInterface { - function markets(address) external view returns (bool, uint, bool); + function markets(address) + external + view + returns ( + bool, + uint256, + bool + ); + function oracle() external view returns (address); } contract Variables { - ComptrollerLensInterface public constant comptroller = ComptrollerLensInterface(0x3d9819210A31b4961b30EF54bE2aeD79B9c9Cd3B); + ComptrollerLensInterface public constant comptroller = + ComptrollerLensInterface(0x3d9819210A31b4961b30EF54bE2aeD79B9c9Cd3B); - address public constant cethAddr = 0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5; + address public constant cethAddr = + 0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5; uint256 public constant markets = 12; - function getAllMarkets() public pure returns (bytes20[markets] memory _markets) { + + function getAllMarkets() + public + pure + returns (bytes20[markets] memory _markets) + { _markets = [ bytes20(0x6C8c6b02E7b2BE14d4fA6022Dfd6d75921D90E4E), // cBAT bytes20(0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643), // cDAI @@ -48,13 +66,16 @@ contract Variables { } contract Resolver is Variables, DSMath { - function getCompoundNetworth(address account) internal returns (uint256 networth) { + function getCompoundNetworth(address account) + internal + returns (uint256 networth) + { bytes20[markets] memory allMarkets = getAllMarkets(); OracleCompInterface oracle = OracleCompInterface(comptroller.oracle()); uint256 totalBorrowInUsd = 0; uint256 totalSupplyInUsd = 0; - for (uint i = 0; i < markets; i++) { + for (uint256 i = 0; i < markets; i++) { CTokenInterface cToken = CTokenInterface(address(allMarkets[i])); uint256 priceInUSD = oracle.getUnderlyingPrice(address(cToken)); uint256 supply = cToken.balanceOfUnderlying(account); @@ -69,10 +90,12 @@ contract Resolver is Variables, DSMath { networth = sub(totalSupplyInUsd, totalBorrowInUsd); } - function getPosition(address account) external returns (uint256 networthInUsd) { + function getPosition(address account) + external + returns (uint256 networthInUsd) + { return getCompoundNetworth(account); } - } contract InstaCompoundAggregateResolver is Resolver { diff --git a/contracts/aggregate/maker.sol b/contracts/aggregate/maker.sol index c42c8ca..bc23272 100644 --- a/contracts/aggregate/maker.sol +++ b/contracts/aggregate/maker.sol @@ -1,18 +1,34 @@ pragma solidity ^0.7.0; pragma experimental ABIEncoderV2; +import "hardhat/console.sol"; + +import {DSMath} from "./common/math.sol"; -import { DSMath } from "../../common/math.sol"; interface ManagerLike { - function ilks(uint) external view returns (bytes32); - function owns(uint) external view returns (address); - function urns(uint) external view returns (address); + function ilks(uint256) external view returns (bytes32); + + function owns(uint256) external view returns (address); + + function urns(uint256) external view returns (address); } interface VatLike { - function ilks(bytes32) external view returns (uint, uint, uint, uint, uint); - function dai(address) external view returns (uint); - function urns(bytes32, address) external view returns (uint, uint); - function gem(bytes32, address) external view returns (uint); + function ilks(bytes32) + external + view + returns ( + uint256, + uint256, + uint256, + uint256, + uint256 + ); + + function dai(address) external view returns (uint256); + + function urns(bytes32, address) external view returns (uint256, uint256); + + function gem(bytes32, address) external view returns (uint256); } interface PipLike { @@ -20,17 +36,19 @@ interface PipLike { } interface SpotLike { - function ilks(bytes32) external view returns (PipLike, uint); + function ilks(bytes32) external view returns (PipLike, uint256); } contract Variables { - ManagerLike public constant managerContract = ManagerLike(0x5ef30b9986345249bc32d8928B7ee64DE9435E39); - SpotLike public constant spotContract = SpotLike(0x65C79fcB50Ca1594B025960e539eD7A9a6D434A3); - VatLike public constant vatContract = VatLike(0x35D1b3F3D7966A1DFe207aa4514C12a259A0492B); + ManagerLike public constant managerContract = + ManagerLike(0x5ef30b9986345249bc32d8928B7ee64DE9435E39); + SpotLike public constant spotContract = + SpotLike(0x65C79fcB50Ca1594B025960e539eD7A9a6D434A3); + VatLike public constant vatContract = + VatLike(0x35D1b3F3D7966A1DFe207aa4514C12a259A0492B); } contract VaultResolver is Variables, DSMath { - function getOwner(uint256 id) external view returns (address owner) { owner = managerContract.owns(id); } @@ -40,22 +58,20 @@ contract VaultResolver is Variables, DSMath { bytes32 ilk = managerContract.ilks(id); (uint256 ink, uint256 art) = vatContract.urns(ilk, urn); - (,uint256 rate, uint256 priceMargin,,) = vatContract.ilks(ilk); + (, uint256 rate, uint256 priceMargin, , ) = vatContract.ilks(ilk); (, uint256 mat) = spotContract.ilks(ilk); uint256 price = rmul(priceMargin, mat); - price = price / 1e18; + price = price / 1e9; uint256 supply = wmul(ink, price); uint256 borrow = rmul(art, rate); + borrow = borrow / 1e9; networth = sub(supply, borrow); } - } contract InstaMakerDAOAggregateResolver is VaultResolver { string public constant name = "MakerDAO-Aggregate-Resolver-v1.0"; } - - diff --git a/hardhat.config.js b/hardhat.config.js index b2e46f7..45760a5 100644 --- a/hardhat.config.js +++ b/hardhat.config.js @@ -23,14 +23,17 @@ task("accounts", "Prints the list of accounts", async () => { * @type import('hardhat/config').HardhatUserConfig */ module.exports = { + paths: { + sources: "./contracts/aggregate", + }, solidity: { compilers: [ { - version: "0.6.10" + version: "0.6.10", }, { - version: "0.7.3" - } + version: "0.7.3", + }, ], optimizer: { enabled: true, @@ -41,12 +44,11 @@ module.exports = { hardhat: { forking: { url: "https://eth-mainnet.alchemyapi.io/v2/" + ALCHEMY_API_KEY, - blockNumber: 12386345 - } - } + blockNumber: 12510580, + }, + }, }, mocha: { timeout: 100000, - } + }, }; - diff --git a/package-lock.json b/package-lock.json index bef2810..45fadfd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,7 @@ "version": "1.0.0", "license": "ISC", "dependencies": { + "@openzeppelin/contracts": "^3.4.1-solc-0.7-2", "solc": "^0.6.0" }, "devDependencies": { @@ -1378,6 +1379,11 @@ "hardhat": "^2.0.0" } }, + "node_modules/@openzeppelin/contracts": { + "version": "3.4.1-solc-0.7-2", + "resolved": "https://registry.npmjs.org/@openzeppelin/contracts/-/contracts-3.4.1-solc-0.7-2.tgz", + "integrity": "sha512-tAG9LWg8+M2CMu7hIsqHPaTyG4uDzjr6mhvH96LvOpLZZj6tgzTluBt+LsCf1/QaYrlis6pITvpIaIhE+iZB+Q==" + }, "node_modules/@resolver-engine/core": { "version": "0.3.3", "resolved": "https://registry.npmjs.org/@resolver-engine/core/-/core-0.3.3.tgz", @@ -18687,6 +18693,11 @@ "@types/web3": "1.0.19" } }, + "@openzeppelin/contracts": { + "version": "3.4.1-solc-0.7-2", + "resolved": "https://registry.npmjs.org/@openzeppelin/contracts/-/contracts-3.4.1-solc-0.7-2.tgz", + "integrity": "sha512-tAG9LWg8+M2CMu7hIsqHPaTyG4uDzjr6mhvH96LvOpLZZj6tgzTluBt+LsCf1/QaYrlis6pITvpIaIhE+iZB+Q==" + }, "@resolver-engine/core": { "version": "0.3.3", "resolved": "https://registry.npmjs.org/@resolver-engine/core/-/core-0.3.3.tgz", diff --git a/package.json b/package.json index 6896ff6..93b3541 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ }, "homepage": "https://github.com/InstaDApp/dsa-resolvers#readme", "dependencies": { + "@openzeppelin/contracts": "^3.4.1-solc-0.7-2", "solc": "^0.6.0" }, "devDependencies": { diff --git a/test/aggregate-resolvers.js b/test/aggregate-resolvers.js new file mode 100644 index 0000000..cac22a3 --- /dev/null +++ b/test/aggregate-resolvers.js @@ -0,0 +1,36 @@ +const { expect } = require("chai"); +const { ethers } = require("hardhat"); + +describe("Resolver getPosition", function () { + let maker, compound, aaveV2; + before("Deploy Aggregate Resolvers", async function () { + maker = await ( + await ethers.getContractFactory("InstaMakerDAOAggregateResolver") + ).deploy(); + compound = await ( + await ethers.getContractFactory("InstaCompoundAggregateResolver") + ).deploy(); + aaveV2 = await ( + await ethers.getContractFactory("InstaAaveV2AggregateResolver") + ).deploy(); + }); + + it("should get position for maker", async function () { + const id = 2571; + const position = await maker.getPosition(id); + expect(position).gt(0); + }); + + it("should get position for compound", async function () { + const account = "0x005280119e7070fd1999703ec606c5e97b146e84"; + const tx = compound.getPosition(account); + expect(tx).to.satisfy; + }); + + it("should get position for aaveV2", async function () { + const account = "0x005280119e7070fd1999703ec606c5e97b146e84"; + const position = await aaveV2.getPosition(account); + console.log(position.toString()); + expect(position).gt(0); + }); +});