Skip to content

Commit

Permalink
Merge pull request #121 from Instadapp/erc4626-polygon
Browse files Browse the repository at this point in the history
Add erc4626 resolver
  • Loading branch information
shriyatyagii authored Jan 31, 2024
2 parents 1d85540 + 0cf01d7 commit ef7e662
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 0 deletions.
54 changes: 54 additions & 0 deletions contracts/protocols/polygon/erc4626/interfaces.sol
Original file line number Diff line number Diff line change
@@ -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);
}
137 changes: 137 additions & 0 deletions contracts/protocols/polygon/erc4626/main.sol
Original file line number Diff line number Diff line change
@@ -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";
}

0 comments on commit ef7e662

Please sign in to comment.