From 0cf01d794bbad83c8e6eba1906c2095915b18c69 Mon Sep 17 00:00:00 2001 From: Shriya Tyagi Date: Wed, 31 Jan 2024 15:27:34 +0530 Subject: [PATCH] feat: add erc4626 resolver --- .../protocols/polygon/erc4626/interfaces.sol | 54 +++++++ contracts/protocols/polygon/erc4626/main.sol | 137 ++++++++++++++++++ 2 files changed, 191 insertions(+) create mode 100644 contracts/protocols/polygon/erc4626/interfaces.sol create mode 100644 contracts/protocols/polygon/erc4626/main.sol diff --git a/contracts/protocols/polygon/erc4626/interfaces.sol b/contracts/protocols/polygon/erc4626/interfaces.sol new file mode 100644 index 0000000..4c125a5 --- /dev/null +++ b/contracts/protocols/polygon/erc4626/interfaces.sol @@ -0,0 +1,54 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.8.19; + +interface TokenInterface { + function balanceOf(address) external view returns (uint256); + + function allowance(address owner, address spender) external view returns (uint256); + + function decimals() external view returns (uint256); + + function name() external view returns (string memory); + + function symbol() external view returns (string memory); +} + +interface VaultInterface { + function decimals() external view returns (uint256); + + function name() external view returns (string memory); + + function symbol() external view returns (string memory); + + function asset() external view returns (address); + + function totalAssets() external view returns (uint256); + + function totalSupply() external view returns (uint256); + + function balanceOf(address) external view returns (uint256); + + function allowance(address owner, address spender) external view returns (uint256); + + function nonces(address) external view returns (uint256); + + function convertToShares(uint256 assets) external view returns (uint256); + + function convertToAssets(uint256 shares) external view returns (uint256); + + function previewDeposit(uint256 assets) external view returns (uint256); + + function previewMint(uint256 shares) external view returns (uint256); + + function previewWithdraw(uint256 assets) external view returns (uint256); + + function previewRedeem(uint256 shares) external view returns (uint256); + + function maxDeposit(address) external view returns (uint256); + + function maxMint(address) external view returns (uint256); + + function maxWithdraw(address owner) external view returns (uint256); + + function maxRedeem(address owner) external view returns (uint256); +} diff --git a/contracts/protocols/polygon/erc4626/main.sol b/contracts/protocols/polygon/erc4626/main.sol new file mode 100644 index 0000000..306c028 --- /dev/null +++ b/contracts/protocols/polygon/erc4626/main.sol @@ -0,0 +1,137 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.8.19; +import "./interfaces.sol"; + +contract Resolver { + + struct VaultData { + bool isToken; + string name; + string symbol; + uint256 decimals; + address asset; + uint256 totalAssets; + uint256 totalSupply; + uint256 convertToShares; + uint256 convertToAssets; + } + + struct UserPosition { + uint256 underlyingBalance; + uint256 vaultBalance; + } + + struct VaultPreview { + uint256 previewDeposit; + uint256 previewMint; + uint256 previewWithdraw; + uint256 previewRedeem; + uint256 decimals; + uint256 underlyingDecimals; + } + + struct MetaMorphoDetails { + uint256 totalCap; + address loanToken; + address collateralToken; + uint256 lltv; + uint256 fee; + bool enabled; // Whether the market is in the withdraw queue + VaultData vaultData; + } + + function getVaultDetails(address[] memory vaultAddresses) public view returns (VaultData[] memory) { + VaultData[] memory _vaultData = new VaultData[](vaultAddresses.length); + for (uint256 i = 0; i < vaultAddresses.length; i++) { + VaultInterface vaultToken = VaultInterface(vaultAddresses[i]); + bool isToken = true; + + try vaultToken.symbol() {} catch { + isToken = false; + continue; + } + + try vaultToken.name() {} catch { + isToken = false; + continue; + } + + try vaultToken.decimals() {} catch { + isToken = false; + continue; + } + + try vaultToken.asset() {} catch { + isToken = false; + continue; + } + + TokenInterface _underlyingToken = TokenInterface(vaultToken.asset()); + + _vaultData[i] = VaultData( + isToken, + vaultToken.name(), + vaultToken.symbol(), + vaultToken.decimals(), + vaultToken.asset(), + vaultToken.totalAssets(), + vaultToken.totalSupply(), + vaultToken.convertToShares(10 ** _underlyingToken.decimals()), // example convertToShares + vaultToken.convertToAssets(10 ** vaultToken.decimals()) // example convertToAssets for 10 ** decimal + ); + } + + return _vaultData; + } + + function getPositions(address owner, address[] memory vaultAddress) public view returns (UserPosition[] memory) { + UserPosition[] memory _userPosition = new UserPosition[](vaultAddress.length); + for (uint256 i = 0; i < vaultAddress.length; i++) { + VaultInterface vaultToken = VaultInterface(vaultAddress[i]); + + address _underlyingAddress = vaultToken.asset(); + TokenInterface underlyingToken = TokenInterface(_underlyingAddress); + + _userPosition[i].underlyingBalance = underlyingToken.balanceOf(owner); + _userPosition[i].vaultBalance = vaultToken.balanceOf(owner); + } + + return _userPosition; + } + + function getAllowances(address owner, address[] memory vaultAddresses) public view returns (uint256[] memory) { + uint256[] memory _tokenAllowance = new uint256[](vaultAddresses.length); + + for (uint256 i = 0; i < vaultAddresses.length; i++) { + VaultInterface vaultToken = VaultInterface(vaultAddresses[i]); + _tokenAllowance[i] = vaultToken.allowance(owner, vaultAddresses[i]); + } + + return _tokenAllowance; + } + + function getVaultPreview( + uint256 amount, + address[] memory vaultAddresses + ) public view returns (VaultPreview[] memory) { + VaultPreview[] memory _vaultPreview = new VaultPreview[](vaultAddresses.length); + + for (uint256 i = 0; i < vaultAddresses.length; i++) { + VaultInterface vaultToken = VaultInterface(vaultAddresses[i]); + + _vaultPreview[i].previewDeposit = vaultToken.previewDeposit(amount); + _vaultPreview[i].previewMint = vaultToken.previewMint(amount); + _vaultPreview[i].previewWithdraw = vaultToken.previewWithdraw(amount); + _vaultPreview[i].previewRedeem = vaultToken.previewRedeem(amount); + _vaultPreview[i].decimals = vaultToken.decimals(); + TokenInterface _underlyingToken = TokenInterface(vaultToken.asset()); + _vaultPreview[i].underlyingDecimals = _underlyingToken.decimals(); + } + + return _vaultPreview; + } +} + +contract InstaERC4626ResolverPolygon is Resolver { + string public constant name = "ERC4626-Resolver-v1.0"; +}