Skip to content

Commit

Permalink
Functioning deposits
Browse files Browse the repository at this point in the history
  • Loading branch information
xhad committed Oct 18, 2024
1 parent 9596f38 commit 9dfc084
Show file tree
Hide file tree
Showing 8 changed files with 1,010 additions and 298 deletions.
13 changes: 13 additions & 0 deletions script/storage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

echo "ERC20:"
# keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.ERC20")) - 1)) & ~bytes32(uint256(0xff))

echo "vault:"
cast keccak "yieldnest.storage.vault"

echo "asset:"
cast keccak "yieldnest.storage.asset"

echo "strat:"
cast keccak "yieldnest.storage.strat"

6 changes: 3 additions & 3 deletions src/Common.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import {Address} from "lib/openzeppelin-contracts/contracts/utils/Address.sol";
import {ERC20} from "lib/openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
import {ERC20PermitUpgradeable} from
"lib/openzeppelin-contracts-upgradeable/contracts/token/ERC20/extensions/ERC20PermitUpgradeable.sol";
import {ERC20Upgradeable} from
"lib/openzeppelin-contracts-upgradeable/contracts/token/ERC20/ERC20Upgradeable.sol";
import {ETHRateProvider} from "src/module/ETHRateProvider.sol";
import {IAccessControl} from "lib/openzeppelin-contracts/contracts/access/IAccessControl.sol";
import {IERC20} from "lib/openzeppelin-contracts/contracts/interfaces/IERC20.sol";
import {IERC20Metadata} from "lib/openzeppelin-contracts/contracts/interfaces/IERC20Metadata.sol";
import {IERC20Permit} from "lib/openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Permit.sol";
import {IStakeManager} from "lib/synclub-contracts/contracts/interfaces/IStakeManager.sol";
import {IRateProvider} from "src/interface/IRateProvider.sol";
import {IVault} from "src/interface/IVault.sol";
import {Math} from "lib/openzeppelin-contracts/contracts/utils/math/Math.sol";
import {ProxyAdmin} from "lib/openzeppelin-contracts/contracts/proxy/transparent/ProxyAdmin.sol";
import {ReentrancyGuardUpgradeable} from
Expand All @@ -25,7 +25,7 @@ import {TimelockControllerUpgradeable} from
"lib/openzeppelin-contracts-upgradeable/contracts/governance/TimelockControllerUpgradeable.sol";
import {TransparentUpgradeableProxy} from
"lib/openzeppelin-contracts/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";
import {IERC165} from "lib/openzeppelin-contracts/contracts/interfaces/IERC165.sol";

import {Storage} from "src/Storage.sol";

contract Common {}
114 changes: 100 additions & 14 deletions src/Storage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,122 @@
pragma solidity ^0.8.24;

import {IVault} from "src/interface/IVault.sol";
import {IRateProvider} from "src/interface/IRateProvider.sol";
import {Math, ERC20Upgradeable} from "src/Common.sol";

library Storage {
bytes32 private constant VAULT_STORAGE_POSITION = keccak256("yieldnest.storage.vault");
bytes32 private constant ASSET_STORAGE_POSITION = keccak256("yieldnest.storage.asset");
bytes32 private constant STRAT_STORAGE_POSITION = keccak256("yieldnest.storage.strat");
using Math for uint256;

bytes32 private constant ERC20_STORAGE_POSITION =
keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.ERC20")) - 1)) & ~bytes32(uint256(0xff));

function _getVaultStorage() internal pure returns (IVault.VaultStorage storage $) {
function getVaultStorage() internal pure returns (IVault.VaultStorage storage $) {
assembly {
$.slot := VAULT_STORAGE_POSITION
$.slot := 0x22cdba5640455d74cb7564fb236bbbbaf66b93a0cc1bd221f1ee2a6b2d0a2427
}
}

function _getAssetStorage() internal pure returns (IVault.AssetStorage storage $) {
function getAssetStorage() internal pure returns (IVault.AssetStorage storage $) {
assembly {
$.slot := ASSET_STORAGE_POSITION
$.slot := 0x2dd192a2474c87efcf5ffda906a4b4f8a678b0e41f9245666251cfed8041e680
}
}

function _getStrategyStorage() internal pure returns (IVault.StrategyStorage storage $) {
function getStrategyStorage() internal pure returns (IVault.StrategyStorage storage $) {
assembly {
$.slot := STRAT_STORAGE_POSITION
$.slot := 0x36e313fea70c5f83d23dd12fc41865566e392cbac4c21baf7972d39f7af1774d
}
}

function _getERC20Storage() internal pure returns (IVault.ERC20Storage storage $) {
function getERC20Storage() internal pure returns (ERC20Upgradeable.ERC20Storage storage $) {
assembly {
$.slot := ERC20_STORAGE_POSITION
$.slot := 0x52c63247e1f47db19d5ce0460030c497f067ca4cebf71ba98eeadabe20bace00
}
}

function getBaseAsset() internal view returns (address) {
IVault.AssetStorage storage assetStorage = getAssetStorage();
return address(assetStorage.list[0]);
}

function getTotalAssets() internal view returns (uint256) {
IVault.VaultStorage storage vaultStorage = getVaultStorage();
return vaultStorage.totalAssets;
}

function getRateProvider() internal view returns (address) {
IVault.VaultStorage storage vaultStorage = getVaultStorage();
return vaultStorage.rateProvider;
}

function getPaused() internal view returns (bool) {
IVault.VaultStorage storage vaultStorage = getVaultStorage();
return vaultStorage.paused;
}

function getAsset(address asset_) internal view returns (IVault.AssetParams memory) {
IVault.AssetStorage storage assetStorage = getAssetStorage();
return assetStorage.assets[asset_];
}

function getBaseDecimals() internal view returns (uint8) {
IVault.AssetStorage storage assetStorage = getAssetStorage();
return assetStorage.assets[getBaseAsset()].decimals + decimalsOffset();
}

function getAllAssets() internal view returns (address[] memory assets_) {
IVault.AssetStorage storage assetStorage = Storage.getAssetStorage();
uint256 assetListLength = assetStorage.list.length;
assets_ = new address[](assetListLength);
for (uint256 i = 0; i < assetListLength; i++) {
assets_[i] = address(assetStorage.list[i]);
}
}

function getMaxDeposit() internal pure returns (uint256) {
return type(uint256).max;
}

function getMaxMint() internal pure returns (uint256) {
return type(uint256).max;
}

function getRate(address asset_) internal view returns (uint256) {
return IRateProvider(getRateProvider()).getRate(asset_);
}

function convertAssetsToBase(address asset_, uint256 assets_) internal view returns (uint256) {
return (assets_ * getRate(asset_)) / 1e18;
}

function convertBaseToAssets(address asset_, uint256 assets_) internal view returns (uint256) {
return (assets_ * 1e18) / getRate(asset_);
}

function convertToAssets(address asset, uint256 shares, Math.Rounding rounding)
internal
view
returns (uint256)
{
uint256 convertedShares = convertBaseToAssets(asset, shares);
return convertedShares.mulDiv(getTotalSupply() + 10 ** decimalsOffset(), getTotalAssets() + 1, rounding);
}

function convertToShares(address asset_, uint256 assets_, Math.Rounding rounding)
internal
view
returns (uint256)
{
uint256 convertedAssets = convertAssetsToBase(asset_, assets_);
return convertedAssets.mulDiv(getTotalSupply() + 10 ** decimalsOffset(), getTotalAssets() + 1, rounding);
}

function decimalsOffset() internal pure returns (uint8) {
return 0;
}

// ERC20

function getTotalSupply() internal view returns (uint256) {
ERC20Upgradeable.ERC20Storage storage erc20Storage = getERC20Storage();
return erc20Storage._totalSupply;
}

}
Loading

0 comments on commit 9dfc084

Please sign in to comment.