{ "language": "Solidity", "sources": { "src/VotingIlluvium.sol": { "content": "// SPDX-License-Identifier: MIT\npragma solidity 0.8.13;\n\nimport {IERC20} from \"./interfaces/staking/IERC20.sol\";\nimport {ICorePoolV1, V1Stake} from \"./interfaces/staking/ICorePoolV1.sol\";\nimport {ICorePoolV2, V2Stake, V2User} from \"./interfaces/staking/ICorePoolV2.sol\";\nimport {IVesting} from \"./interfaces/vesting/IVesting.sol\";\n\ncontract VotingIlluvium {\n string public constant name = \"Voting Illuvium\";\n string public constant symbol = \"vILV\";\n\n uint256 public constant decimals = 18;\n\n address public constant ILV = 0x767FE9EDC9E0dF98E07454847909b5E959D7ca0E;\n address public constant ILV_POOL =\n 0x25121EDDf746c884ddE4619b573A7B10714E2a36;\n address public constant ILV_POOL_V2 =\n 0x7f5f854FfB6b7701540a00C69c4AB2De2B34291D;\n address public constant LP_POOL =\n 0x8B4d8443a0229349A9892D4F7CbE89eF5f843F72;\n address public constant LP_POOL_V2 =\n 0xe98477bDc16126bB0877c6e3882e3Edd72571Cc2;\n address public constant VESTING =\n 0x6Bd2814426f9a6abaA427D2ad3FC898D2A57aDC6;\n\n uint256 internal constant WEIGHT_MULTIPLIER = 1e6;\n uint256 internal constant MAX_WEIGHT_MULTIPLIER = 2e6;\n uint256 internal constant BASE_WEIGHT = 1e6;\n uint256 internal constant MAX_STAKE_PERIOD = 365 days;\n\n function balanceOf(address _account)\n external\n view\n returns (uint256 balance)\n {\n // Get balance staked as deposits + yield in the v2 ilv pool\n uint256 ilvPoolV2Balance = ICorePoolV2(ILV_POOL_V2).balanceOf(_account);\n\n // Now we need to get deposits + yield in v1.\n // Copy the v2 user struct to memory and number of stakes in v2.\n V2User memory user = ICorePoolV2(ILV_POOL_V2).users(_account);\n // Get number of stakes in v2\n uint256 userStakesLength = ICorePoolV2(ILV_POOL_V2).getStakesLength(\n _account\n );\n\n // Loop over each stake, compute its weight and add to v2StakedWeight\n uint256 v2StakedWeight;\n for (uint256 i = 0; i < userStakesLength; i++) {\n // Read stake in ilv pool v2 contract\n V2Stake memory stake = ICorePoolV2(ILV_POOL_V2).getStake(\n _account,\n i\n );\n // Computes stake weight based on lock period and balance\n uint256 stakeWeight = _getStakeWeight(stake);\n v2StakedWeight += stakeWeight;\n }\n // V1 yield balance can be determined by the difference of\n // the user total weight and the v2 staked weight.\n // any extra weight that isn't coming from v2 = v1YieldWeight\n uint256 v1YieldBalance = user.totalWeight > v2StakedWeight\n ? (user.totalWeight - v2StakedWeight) / MAX_WEIGHT_MULTIPLIER\n : 0;\n\n // To finalize, we need to get the total amount of deposits\n // that are still in v1\n uint256 v1DepositBalance;\n // Loop over each v1StakeId stored in V2 contract.\n // Each stake id represents a deposit in v1\n for (uint256 i = 0; i < user.v1IdsLength; i++) {\n uint256 v1StakeId = ICorePoolV2(ILV_POOL_V2).getV1StakeId(\n _account,\n i\n );\n // Call v1 contract for deposit balance\n v1DepositBalance += (\n ICorePoolV1(ILV_POOL).getDeposit(_account, v1StakeId)\n ).tokenAmount;\n }\n\n // Now sum the queried ilv pool v2 balance with\n // the v1 yield balance and the v1 deposit balance\n // to have the final result\n uint256 totalILVPoolsBalance = ilvPoolV2Balance +\n v1YieldBalance +\n v1DepositBalance;\n\n // And simply query ILV normalized values from LP pools\n // V1 and V2\n uint256 lpPoolBalance = _lpToILV(\n ICorePoolV1(LP_POOL).balanceOf(_account)\n );\n uint256 lpPoolV2Balance = _lpToILV(\n ICorePoolV2(LP_POOL_V2).balanceOf(_account)\n );\n\n // We manually query index 0 because current vesting state in L1 is one position per address\n // If this changes we need to change the approach\n uint256 vestingBalance;\n\n try IVesting(VESTING).tokenOfOwnerByIndex(_account, 0) returns (\n uint256 vestingPositionId\n ) {\n vestingBalance = (IVesting(VESTING).positions(vestingPositionId))\n .balance;\n } catch Error(string memory) {}\n\n balance =\n totalILVPoolsBalance +\n lpPoolBalance +\n lpPoolV2Balance +\n vestingBalance;\n }\n\n function totalSupply() external view returns (uint256) {\n return IERC20(ILV).totalSupply();\n }\n\n function _lpToILV(uint256 _lpBalance)\n internal\n view\n returns (uint256 ilvAmount)\n {\n address _poolToken = ICorePoolV2(LP_POOL).poolToken();\n\n uint256 totalLP = IERC20(_poolToken).totalSupply();\n uint256 ilvInLP = IERC20(ILV).balanceOf(_poolToken);\n ilvAmount = (ilvInLP * _lpBalance) / totalLP;\n }\n\n function _getStakeWeight(V2Stake memory _stake)\n internal\n pure\n returns (uint256)\n {\n return\n uint256(\n (((_stake.lockedUntil - _stake.lockedFrom) *\n WEIGHT_MULTIPLIER) /\n MAX_STAKE_PERIOD +\n BASE_WEIGHT) * _stake.value\n );\n }\n}\n" }, "src/interfaces/staking/ICorePoolV1.sol": { "content": "// SPDX-License-Identifier: MIT\npragma solidity 0.8.13;\n\nstruct V1Stake {\n // @dev token amount staked\n uint256 tokenAmount;\n // @dev stake weight\n uint256 weight;\n // @dev locking period - from\n uint64 lockedFrom;\n // @dev locking period - until\n uint64 lockedUntil;\n // @dev indicates if the stake was created as a yield reward\n bool isYield;\n}\n\nstruct V1User {\n // @dev Total staked amount\n uint256 tokenAmount;\n // @dev Total weight\n uint256 totalWeight;\n // @dev Auxiliary variable for yield calculation\n uint256 subYieldRewards;\n // @dev Auxiliary variable for vault rewards calculation\n uint256 subVaultRewards;\n // @dev An array of holder's deposits\n V1Stake[] deposits;\n}\n\ninterface ICorePoolV1 {\n function users(address _who) external view returns (V1User memory);\n\n function balanceOf(address _user) external view returns (uint256);\n\n function getDeposit(address _from, uint256 _stakeId)\n external\n view\n returns (V1Stake memory);\n\n function poolToken() external view returns (address);\n\n function usersLockingWeight() external view returns (uint256);\n\n function poolTokenReserve() external view returns (uint256);\n}\n" }, "src/interfaces/staking/ICorePoolV2.sol": { "content": "// SPDX-License-Identifier: MIT\npragma solidity 0.8.13;\n\n/// @dev Data structure representing token holder using a pool.\nstruct V2User {\n /// @dev pending yield rewards to be claimed\n uint128 pendingYield;\n /// @dev pending revenue distribution to be claimed\n uint128 pendingRevDis;\n /// @dev Total weight\n uint248 totalWeight;\n /// @dev number of v1StakesIds\n uint8 v1IdsLength;\n /// @dev Checkpoint variable for yield calculation\n uint256 yieldRewardsPerWeightPaid;\n /// @dev Checkpoint variable for vault rewards calculation\n uint256 vaultRewardsPerWeightPaid;\n}\n\nstruct V2Stake {\n /// @dev token amount staked\n uint120 value;\n /// @dev locking period - from\n uint64 lockedFrom;\n /// @dev locking period - until\n uint64 lockedUntil;\n /// @dev indicates if the stake was created as a yield reward\n bool isYield;\n}\n\ninterface ICorePoolV2 {\n function users(address _user) external view returns (V2User memory);\n\n function poolToken() external view returns (address);\n\n function isFlashPool() external view returns (bool);\n\n function weight() external view returns (uint32);\n\n function lastYieldDistribution() external view returns (uint64);\n\n function yieldRewardsPerWeight() external view returns (uint256);\n\n function globalWeight() external view returns (uint256);\n\n function pendingRewards(address _user)\n external\n view\n returns (uint256, uint256);\n\n function poolTokenReserve() external view returns (uint256);\n\n function balanceOf(address _user) external view returns (uint256);\n\n function getTotalReserves() external view returns (uint256);\n\n function getStake(address _user, uint256 _stakeId)\n external\n view\n returns (V2Stake memory);\n\n function getV1StakeId(address _user, uint256 _position)\n external\n view\n returns (uint256);\n\n function getStakesLength(address _user) external view returns (uint256);\n\n function sync() external;\n\n function setWeight(uint32 _weight) external;\n\n function receiveVaultRewards(uint256 value) external;\n}\n" }, "src/interfaces/staking/IERC20.sol": { "content": "// SPDX-License-Identifier: MIT\npragma solidity 0.8.13;\n\n/**\n * @dev Interface of the ERC20 standard as defined in the EIP.\n */\ninterface IERC20 {\n /**\n * @dev Returns the amount of tokens in existence.\n */\n function totalSupply() external view returns (uint256);\n\n /**\n * @dev Returns the amount of tokens owned by `account`.\n */\n function balanceOf(address account) external view returns (uint256);\n\n /**\n * @dev Moves `amount` tokens from the caller's account to `recipient`.\n *\n * Returns a boolean value indicating whether the operation succeeded.\n *\n * Emits a {Transfer} event.\n */\n function transfer(address recipient, uint256 amount)\n external\n returns (bool);\n\n /**\n * @dev Returns the remaining number of tokens that `spender` will be\n * allowed to spend on behalf of `owner` through {transferFrom}. This is\n * zero by default.\n *\n * This value changes when {approve} or {transferFrom} are called.\n */\n function allowance(address owner, address spender)\n external\n view\n returns (uint256);\n\n /**\n * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.\n *\n * Returns a boolean value indicating whether the operation succeeded.\n *\n * IMPORTANT: Beware that changing an allowance with this method brings the risk\n * that someone may use both the old and the new allowance by unfortunate\n * transaction ordering. One possible solution to mitigate this race\n * condition is to first reduce the spender's allowance to 0 and set the\n * desired value afterwards:\n * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729\n *\n * Emits an {Approval} event.\n */\n function approve(address spender, uint256 amount) external returns (bool);\n\n /**\n * @dev Moves `amount` tokens from `sender` to `recipient` using the\n * allowance mechanism. `amount` is then deducted from the caller's\n * allowance.\n *\n * Returns a boolean value indicating whether the operation succeeded.\n *\n * Emits a {Transfer} event.\n */\n function transferFrom(\n address sender,\n address recipient,\n uint256 amount\n ) external returns (bool);\n\n /**\n * @dev Emitted when `value` tokens are moved from one account (`from`) to\n * another (`to`).\n *\n * Note that `value` may be zero.\n */\n event Transfer(address indexed from, address indexed to, uint256 value);\n\n /**\n * @dev Emitted when the allowance of a `spender` for an `owner` is set by\n * a call to {approve}. `value` is the new allowance.\n */\n event Approval(\n address indexed owner,\n address indexed spender,\n uint256 value\n );\n}\n" }, "src/interfaces/vesting/IVesting.sol": { "content": "// SPDX-License-Identifier: MIT\npragma solidity 0.8.13;\n\n/// @param balance total underlying balance\n/// @param unlocked underlying value already unlocked\n/// @param rate value unlocked per second, up to ~1.84e19 tokens per second\n/// @param start when position starts unlocking\n/// @param end when position unlocking ends\n/// @param pendingRevDis pending revenue distribution share to be claimed\n/// @param revDisPerTokenPaid last revDisPerToken applied to the position\nstruct Position {\n uint128 balance;\n uint128 unlocked;\n uint64 start;\n uint64 end;\n uint128 rate;\n uint128 pendingRevDis;\n uint256 revDisPerTokenPaid;\n}\n\ninterface IVesting {\n /// @notice Returns locked token holders vesting positions\n /// @param _tokenId position nft identifier\n /// @return position the vesting position stored data\n function positions(uint256 _tokenId)\n external\n view\n returns (Position memory);\n\n /**\n * @dev Returns a token ID owned by `owner` at a given `index` of its token list.\n * Use along with {balanceOf} to enumerate all of ``owner``'s tokens.\n */\n function tokenOfOwnerByIndex(address owner, uint256 index)\n external\n view\n returns (uint256);\n}\n" } }, "settings": { "remappings": [ "ds-test/=lib/forge-std/lib/ds-test/src/", "forge-std/=lib/forge-std/src/" ], "optimizer": { "enabled": true, "runs": 200 }, "metadata": { "bytecodeHash": "ipfs" }, "outputSelection": { "*": { "*": [ "evm.bytecode", "evm.deployedBytecode", "devdoc", "userdoc", "metadata", "abi" ] } }, "evmVersion": "london", "libraries": {} } }