zellic-audit
Initial commit
f998fcd
raw
history blame
27.4 kB
{
"language": "Solidity",
"sources": {
"yBribe.sol": {
"content": "pragma solidity 0.8.6;\n\nimport \"EnumerableSet.sol\";\n\ninterface GaugeController {\n struct VotedSlope {\n uint slope;\n uint power;\n uint end;\n }\n \n struct Point {\n uint bias;\n uint slope;\n }\n \n function vote_user_slopes(address, address) external view returns (VotedSlope memory);\n function last_user_vote(address, address) external view returns (uint);\n function points_weight(address, uint) external view returns (Point memory);\n function checkpoint_gauge(address) external;\n function time_total() external view returns (uint);\n function gauge_types(address) external view returns (int128);\n}\n\ninterface erc20 { \n function transfer(address recipient, uint amount) external returns (bool);\n function decimals() external view returns (uint8);\n function balanceOf(address) external view returns (uint);\n function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);\n function approve(address spender, uint amount) external returns (bool);\n}\n\ncontract yBribe {\n using EnumerableSet for EnumerableSet.AddressSet;\n\n event RewardAdded(address indexed briber, address indexed gauge, address indexed reward_token, uint amount, uint fee);\n event NewTokenReward(address indexed gauge, address indexed reward_token); // Specifies unique token added for first time to gauge\n event RewardClaimed(address indexed user, address indexed gauge, address indexed reward_token, uint amount);\n event Blacklisted(address indexed user);\n event RemovedFromBlacklist(address indexed user);\n event SetRewardRecipient(address indexed user, address recipient);\n event ClearRewardRecipient(address indexed user, address recipient);\n event ChangeOwner(address owner);\n event PeriodUpdated(address indexed gauge, uint indexed period, uint bias, uint blacklisted_bias);\n event FeeUpdated(uint fee);\n\n uint constant WEEK = 86400 * 7;\n uint constant PRECISION = 10**18;\n uint constant BPS = 10_000;\n GaugeController constant GAUGE = GaugeController(0x2F50D538606Fa9EDD2B11E2446BEb18C9D5846bB);\n \n mapping(address => mapping(address => uint)) public claims_per_gauge;\n mapping(address => mapping(address => uint)) public reward_per_gauge;\n \n mapping(address => mapping(address => uint)) public reward_per_token;\n mapping(address => mapping(address => uint)) public active_period;\n mapping(address => mapping(address => mapping(address => uint))) public last_user_claim;\n mapping(address => uint) public next_claim_time;\n \n mapping(address => address[]) public _rewards_per_gauge;\n mapping(address => address[]) public _gauges_per_reward;\n mapping(address => mapping(address => bool)) public _rewards_in_gauge;\n\n address public owner = 0xFEB4acf3df3cDEA7399794D0869ef76A6EfAff52;\n address public fee_recipient = 0x93A62dA5a14C80f265DAbC077fCEE437B1a0Efde;\n address public pending_owner;\n uint public fee_percent = 100; // Expressed in BPS\n mapping(address => address) public reward_recipient;\n EnumerableSet.AddressSet private blacklist;\n \n function _add(address gauge, address reward) internal {\n if (!_rewards_in_gauge[gauge][reward]) {\n _rewards_per_gauge[gauge].push(reward);\n _gauges_per_reward[reward].push(gauge);\n _rewards_in_gauge[gauge][reward] = true;\n emit NewTokenReward(gauge, reward);\n }\n }\n \n function rewards_per_gauge(address gauge) external view returns (address[] memory) {\n return _rewards_per_gauge[gauge];\n }\n \n function gauges_per_reward(address reward) external view returns (address[] memory) {\n return _gauges_per_reward[reward];\n }\n \n /// @dev Required to sync each gauge/token pair to new week.\n /// @dev Can be triggered either by claiming or adding bribes to gauge/token pair.\n function _update_period(address gauge, address reward_token) internal returns (uint) {\n uint _period = active_period[gauge][reward_token];\n if (block.timestamp >= _period + WEEK) {\n _period = current_period();\n GAUGE.checkpoint_gauge(gauge);\n uint _bias = GAUGE.points_weight(gauge, _period).bias;\n uint blacklisted_bias = get_blacklisted_bias(gauge);\n _bias -= blacklisted_bias;\n emit PeriodUpdated(gauge, _period, _bias, blacklisted_bias);\n uint _amount = reward_per_gauge[gauge][reward_token] - claims_per_gauge[gauge][reward_token];\n if (_bias > 0){\n reward_per_token[gauge][reward_token] = _amount * PRECISION / _bias;\n }\n active_period[gauge][reward_token] = _period;\n }\n return _period;\n }\n \n function add_reward_amount(address gauge, address reward_token, uint amount) external returns (bool) {\n require(GAUGE.gauge_types(gauge) >= 0); // @dev: reverts on invalid gauge\n _safeTransferFrom(reward_token, msg.sender, address(this), amount);\n uint fee_take = fee_percent * amount / BPS;\n uint reward_amount = amount - fee_take;\n if (fee_take > 0){\n _safeTransfer(reward_token, fee_recipient, fee_take);\n }\n _update_period(gauge, reward_token);\n reward_per_gauge[gauge][reward_token] += reward_amount;\n _add(gauge, reward_token);\n emit RewardAdded(msg.sender, gauge, reward_token, reward_amount, fee_take);\n return true;\n }\n \n /// @notice Estimate pending bribe amount for any user\n /// @dev This function returns zero if active_period has not yet been updated.\n /// @dev Should not rely on this function for any user case where precision is required.\n function claimable(address user, address gauge, address reward_token) external view returns (uint) {\n uint _period = current_period();\n if(blacklist.contains(user) || next_claim_time[user] > _period) {\n return 0;\n }\n if (last_user_claim[user][gauge][reward_token] >= _period) {\n return 0;\n }\n uint last_user_vote = GAUGE.last_user_vote(user, gauge);\n if (last_user_vote >= _period) {\n return 0;\n }\n if (_period != active_period[gauge][reward_token]) {\n return 0;\n }\n GaugeController.VotedSlope memory vs = GAUGE.vote_user_slopes(user, gauge);\n uint _user_bias = _calc_bias(vs.slope, vs.end);\n return _user_bias * reward_per_token[gauge][reward_token] / PRECISION;\n }\n\n function claim_reward(address gauge, address reward_token) external returns (uint) {\n return _claim_reward(msg.sender, gauge, reward_token);\n }\n\n function claim_reward_for_many(address[] calldata _users, address[] calldata _gauges, address[] calldata _reward_tokens) external returns (uint[] memory amounts) {\n require(_users.length == _gauges.length && _users.length == _reward_tokens.length, \"!lengths\");\n uint length = _users.length;\n amounts = new uint[](length);\n for (uint i = 0; i < length; i++) {\n amounts[i] = _claim_reward(_users[i], _gauges[i], _reward_tokens[i]);\n }\n return amounts;\n }\n\n function claim_reward_for(address user, address gauge, address reward_token) external returns (uint) {\n return _claim_reward(user, gauge, reward_token);\n }\n \n function _claim_reward(address user, address gauge, address reward_token) internal returns (uint) {\n if(blacklist.contains(user) || next_claim_time[user] > current_period()){\n return 0;\n }\n uint _period = _update_period(gauge, reward_token);\n uint _amount = 0;\n if (last_user_claim[user][gauge][reward_token] < _period) {\n last_user_claim[user][gauge][reward_token] = _period;\n if (GAUGE.last_user_vote(user, gauge) < _period) {\n GaugeController.VotedSlope memory vs = GAUGE.vote_user_slopes(user, gauge);\n uint _user_bias = _calc_bias(vs.slope, vs.end);\n _amount = _user_bias * reward_per_token[gauge][reward_token] / PRECISION;\n if (_amount > 0) {\n claims_per_gauge[gauge][reward_token] += _amount;\n address recipient = reward_recipient[user];\n recipient = recipient == address(0) ? user : recipient;\n _safeTransfer(reward_token, recipient, _amount);\n emit RewardClaimed(user, gauge, user, _amount);\n }\n }\n }\n return _amount;\n }\n\n /// @dev Compute bias from slope and lock end\n /// @param _slope User's slope\n /// @param _end Timestamp of user's lock end\n function _calc_bias(uint _slope, uint _end) internal view returns (uint) {\n uint current = current_period();\n if (current + WEEK >= _end) return 0;\n return _slope * (_end - current);\n }\n\n /// @dev Sum all blacklisted bias for any gauge in current period.\n function get_blacklisted_bias(address gauge) public view returns (uint) {\n uint bias;\n uint length = blacklist.length();\n for (uint i = 0; i < length; i++) {\n address user = blacklist.at(i);\n GaugeController.VotedSlope memory vs = GAUGE.vote_user_slopes(user, gauge);\n bias += _calc_bias(vs.slope, vs.end);\n }\n return bias;\n }\n\n /// @notice Allow owner to add address to blacklist, preventing them from claiming\n /// @dev Any vote weight address added\n function add_to_blacklist(address _user) external {\n require(msg.sender == owner, \"!owner\");\n if(blacklist.add(_user)) emit Blacklisted(_user);\n }\n\n /// @notice Allow owner to remove address from blacklist\n /// @dev We set a next_claim_time to prevent access to current period's bribes\n function remove_from_blacklist(address _user) external {\n require(msg.sender == owner, \"!owner\");\n if(blacklist.remove(_user)){\n next_claim_time[_user] = current_period() + WEEK;\n emit RemovedFromBlacklist(_user);\n }\n }\n\n /// @notice Check if address is blacklisted\n function is_blacklisted(address address_to_check) public view returns (bool) {\n return blacklist.contains(address_to_check);\n }\n\n /// @dev Helper function, if possible, avoid using on-chain as list can grow unbounded\n function get_blacklist() public view returns (address[] memory _blacklist) {\n _blacklist = new address[](blacklist.length());\n for (uint i; i < blacklist.length(); i++) {\n _blacklist[i] = blacklist.at(i);\n }\n }\n\n /// @dev Helper function to determine current period globally. Not specific to any gauges or internal state.\n function current_period() public view returns (uint) {\n return block.timestamp / WEEK * WEEK;\n }\n\n /// @notice Allow any user to route claimed rewards to a specified recipient address\n function set_recipient(address _recipient) external {\n require (_recipient != msg.sender, \"self\");\n require (_recipient != address(0), \"0x0\");\n address current_recipient = reward_recipient[msg.sender];\n require (_recipient != current_recipient, \"Already set\");\n \n // Update delegation mapping\n reward_recipient[msg.sender] = _recipient;\n \n if (current_recipient != address(0)) {\n emit ClearRewardRecipient(msg.sender, current_recipient);\n }\n\n emit SetRewardRecipient(msg.sender, _recipient);\n }\n\n /// @notice Allow any user to clear any previously specified reward recipient\n function clear_recipient() external {\n address current_recipient = reward_recipient[msg.sender];\n require (current_recipient != address(0), \"No recipient set\");\n // update delegation mapping\n reward_recipient[msg.sender]= address(0);\n emit ClearRewardRecipient(msg.sender, current_recipient);\n }\n\n /// @notice Allow owner to set fees of up to 4% of bribes upon deposit\n function set_fee_percent(uint _percent) external {\n require(msg.sender == owner, \"!owner\");\n require(_percent <= 400);\n fee_percent = _percent;\n }\n\n function set_fee_recipient(address _recipient) external {\n require(msg.sender == owner, \"!owner\");\n fee_recipient = _recipient;\n }\n\n function set_owner(address _new_owner) external {\n require(msg.sender == owner, \"!owner\");\n pending_owner = _new_owner;\n }\n\n function accept_owner() external {\n address _pending_owner = pending_owner;\n require(msg.sender == _pending_owner, \"!pending_owner\");\n owner = _pending_owner;\n emit ChangeOwner(_pending_owner);\n pending_owner = address(0);\n }\n\n function _safeTransfer(\n address token,\n address to,\n uint value\n ) internal {\n (bool success, bytes memory data) =\n token.call(abi.encodeWithSelector(erc20.transfer.selector, to, value));\n require(success && (data.length == 0 || abi.decode(data, (bool))));\n }\n \n function _safeTransferFrom(\n address token,\n address from,\n address to,\n uint value\n ) internal {\n (bool success, bytes memory data) =\n token.call(abi.encodeWithSelector(erc20.transferFrom.selector, from, to, value));\n require(success && (data.length == 0 || abi.decode(data, (bool))));\n }\n}"
},
"EnumerableSet.sol": {
"content": "// SPDX-License-Identifier: MIT\n// OpenZeppelin Contracts (last updated v4.7.0) (utils/structs/EnumerableSet.sol)\n\npragma solidity ^0.8.0;\n\n/**\n * @dev Library for managing\n * https://en.wikipedia.org/wiki/Set_(abstract_data_type)[sets] of primitive\n * types.\n *\n * Sets have the following properties:\n *\n * - Elements are added, removed, and checked for existence in constant time\n * (O(1)).\n * - Elements are enumerated in O(n). No guarantees are made on the ordering.\n *\n * ```\n * contract Example {\n * // Add the library methods\n * using EnumerableSet for EnumerableSet.AddressSet;\n *\n * // Declare a set state variable\n * EnumerableSet.AddressSet private mySet;\n * }\n * ```\n *\n * As of v3.3.0, sets of type `bytes32` (`Bytes32Set`), `address` (`AddressSet`)\n * and `uint256` (`UintSet`) are supported.\n *\n * [WARNING]\n * ====\n * Trying to delete such a structure from storage will likely result in data corruption, rendering the structure unusable.\n * See https://github.com/ethereum/solidity/pull/11843[ethereum/solidity#11843] for more info.\n *\n * In order to clean an EnumerableSet, you can either remove all elements one by one or create a fresh instance using an array of EnumerableSet.\n * ====\n */\nlibrary EnumerableSet {\n // To implement this library for multiple types with as little code\n // repetition as possible, we write it in terms of a generic Set type with\n // bytes32 values.\n // The Set implementation uses private functions, and user-facing\n // implementations (such as AddressSet) are just wrappers around the\n // underlying Set.\n // This means that we can only create new EnumerableSets for types that fit\n // in bytes32.\n\n struct Set {\n // Storage of set values\n bytes32[] _values;\n // Position of the value in the `values` array, plus 1 because index 0\n // means a value is not in the set.\n mapping(bytes32 => uint256) _indexes;\n }\n\n /**\n * @dev Add a value to a set. O(1).\n *\n * Returns true if the value was added to the set, that is if it was not\n * already present.\n */\n function _add(Set storage set, bytes32 value) private returns (bool) {\n if (!_contains(set, value)) {\n set._values.push(value);\n // The value is stored at length-1, but we add 1 to all indexes\n // and use 0 as a sentinel value\n set._indexes[value] = set._values.length;\n return true;\n } else {\n return false;\n }\n }\n\n /**\n * @dev Removes a value from a set. O(1).\n *\n * Returns true if the value was removed from the set, that is if it was\n * present.\n */\n function _remove(Set storage set, bytes32 value) private returns (bool) {\n // We read and store the value's index to prevent multiple reads from the same storage slot\n uint256 valueIndex = set._indexes[value];\n\n if (valueIndex != 0) {\n // Equivalent to contains(set, value)\n // To delete an element from the _values array in O(1), we swap the element to delete with the last one in\n // the array, and then remove the last element (sometimes called as 'swap and pop').\n // This modifies the order of the array, as noted in {at}.\n\n uint256 toDeleteIndex = valueIndex - 1;\n uint256 lastIndex = set._values.length - 1;\n\n if (lastIndex != toDeleteIndex) {\n bytes32 lastValue = set._values[lastIndex];\n\n // Move the last value to the index where the value to delete is\n set._values[toDeleteIndex] = lastValue;\n // Update the index for the moved value\n set._indexes[lastValue] = valueIndex; // Replace lastValue's index to valueIndex\n }\n\n // Delete the slot where the moved value was stored\n set._values.pop();\n\n // Delete the index for the deleted slot\n delete set._indexes[value];\n\n return true;\n } else {\n return false;\n }\n }\n\n /**\n * @dev Returns true if the value is in the set. O(1).\n */\n function _contains(Set storage set, bytes32 value) private view returns (bool) {\n return set._indexes[value] != 0;\n }\n\n /**\n * @dev Returns the number of values on the set. O(1).\n */\n function _length(Set storage set) private view returns (uint256) {\n return set._values.length;\n }\n\n /**\n * @dev Returns the value stored at position `index` in the set. O(1).\n *\n * Note that there are no guarantees on the ordering of values inside the\n * array, and it may change when more values are added or removed.\n *\n * Requirements:\n *\n * - `index` must be strictly less than {length}.\n */\n function _at(Set storage set, uint256 index) private view returns (bytes32) {\n return set._values[index];\n }\n\n /**\n * @dev Return the entire set in an array\n *\n * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed\n * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that\n * this function has an unbounded cost, and using it as part of a state-changing function may render the function\n * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.\n */\n function _values(Set storage set) private view returns (bytes32[] memory) {\n return set._values;\n }\n\n // Bytes32Set\n\n struct Bytes32Set {\n Set _inner;\n }\n\n /**\n * @dev Add a value to a set. O(1).\n *\n * Returns true if the value was added to the set, that is if it was not\n * already present.\n */\n function add(Bytes32Set storage set, bytes32 value) internal returns (bool) {\n return _add(set._inner, value);\n }\n\n /**\n * @dev Removes a value from a set. O(1).\n *\n * Returns true if the value was removed from the set, that is if it was\n * present.\n */\n function remove(Bytes32Set storage set, bytes32 value) internal returns (bool) {\n return _remove(set._inner, value);\n }\n\n /**\n * @dev Returns true if the value is in the set. O(1).\n */\n function contains(Bytes32Set storage set, bytes32 value) internal view returns (bool) {\n return _contains(set._inner, value);\n }\n\n /**\n * @dev Returns the number of values in the set. O(1).\n */\n function length(Bytes32Set storage set) internal view returns (uint256) {\n return _length(set._inner);\n }\n\n /**\n * @dev Returns the value stored at position `index` in the set. O(1).\n *\n * Note that there are no guarantees on the ordering of values inside the\n * array, and it may change when more values are added or removed.\n *\n * Requirements:\n *\n * - `index` must be strictly less than {length}.\n */\n function at(Bytes32Set storage set, uint256 index) internal view returns (bytes32) {\n return _at(set._inner, index);\n }\n\n /**\n * @dev Return the entire set in an array\n *\n * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed\n * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that\n * this function has an unbounded cost, and using it as part of a state-changing function may render the function\n * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.\n */\n function values(Bytes32Set storage set) internal view returns (bytes32[] memory) {\n return _values(set._inner);\n }\n\n // AddressSet\n\n struct AddressSet {\n Set _inner;\n }\n\n /**\n * @dev Add a value to a set. O(1).\n *\n * Returns true if the value was added to the set, that is if it was not\n * already present.\n */\n function add(AddressSet storage set, address value) internal returns (bool) {\n return _add(set._inner, bytes32(uint256(uint160(value))));\n }\n\n /**\n * @dev Removes a value from a set. O(1).\n *\n * Returns true if the value was removed from the set, that is if it was\n * present.\n */\n function remove(AddressSet storage set, address value) internal returns (bool) {\n return _remove(set._inner, bytes32(uint256(uint160(value))));\n }\n\n /**\n * @dev Returns true if the value is in the set. O(1).\n */\n function contains(AddressSet storage set, address value) internal view returns (bool) {\n return _contains(set._inner, bytes32(uint256(uint160(value))));\n }\n\n /**\n * @dev Returns the number of values in the set. O(1).\n */\n function length(AddressSet storage set) internal view returns (uint256) {\n return _length(set._inner);\n }\n\n /**\n * @dev Returns the value stored at position `index` in the set. O(1).\n *\n * Note that there are no guarantees on the ordering of values inside the\n * array, and it may change when more values are added or removed.\n *\n * Requirements:\n *\n * - `index` must be strictly less than {length}.\n */\n function at(AddressSet storage set, uint256 index) internal view returns (address) {\n return address(uint160(uint256(_at(set._inner, index))));\n }\n\n /**\n * @dev Return the entire set in an array\n *\n * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed\n * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that\n * this function has an unbounded cost, and using it as part of a state-changing function may render the function\n * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.\n */\n function values(AddressSet storage set) internal view returns (address[] memory) {\n bytes32[] memory store = _values(set._inner);\n address[] memory result;\n\n /// @solidity memory-safe-assembly\n assembly {\n result := store\n }\n\n return result;\n }\n\n // UintSet\n\n struct UintSet {\n Set _inner;\n }\n\n /**\n * @dev Add a value to a set. O(1).\n *\n * Returns true if the value was added to the set, that is if it was not\n * already present.\n */\n function add(UintSet storage set, uint256 value) internal returns (bool) {\n return _add(set._inner, bytes32(value));\n }\n\n /**\n * @dev Removes a value from a set. O(1).\n *\n * Returns true if the value was removed from the set, that is if it was\n * present.\n */\n function remove(UintSet storage set, uint256 value) internal returns (bool) {\n return _remove(set._inner, bytes32(value));\n }\n\n /**\n * @dev Returns true if the value is in the set. O(1).\n */\n function contains(UintSet storage set, uint256 value) internal view returns (bool) {\n return _contains(set._inner, bytes32(value));\n }\n\n /**\n * @dev Returns the number of values on the set. O(1).\n */\n function length(UintSet storage set) internal view returns (uint256) {\n return _length(set._inner);\n }\n\n /**\n * @dev Returns the value stored at position `index` in the set. O(1).\n *\n * Note that there are no guarantees on the ordering of values inside the\n * array, and it may change when more values are added or removed.\n *\n * Requirements:\n *\n * - `index` must be strictly less than {length}.\n */\n function at(UintSet storage set, uint256 index) internal view returns (uint256) {\n return uint256(_at(set._inner, index));\n }\n\n /**\n * @dev Return the entire set in an array\n *\n * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed\n * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that\n * this function has an unbounded cost, and using it as part of a state-changing function may render the function\n * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.\n */\n function values(UintSet storage set) internal view returns (uint256[] memory) {\n bytes32[] memory store = _values(set._inner);\n uint256[] memory result;\n\n /// @solidity memory-safe-assembly\n assembly {\n result := store\n }\n\n return result;\n }\n}\n"
}
},
"settings": {
"evmVersion": "istanbul",
"optimizer": {
"enabled": true,
"runs": 200
},
"libraries": {
"yBribe.sol": {}
},
"outputSelection": {
"*": {
"*": [
"evm.bytecode",
"evm.deployedBytecode",
"devdoc",
"userdoc",
"metadata",
"abi"
]
}
}
}
}