|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +import {EnumerableSet} from '@openzeppelin/contracts/utils/structs/EnumerableSet.sol'; |
| 5 | +import {AccessControl} from '@openzeppelin/contracts/access/AccessControl.sol'; |
| 6 | +import {VersionedInitializable} from '@aave/core-v3/contracts/protocol/libraries/aave-upgradeability/VersionedInitializable.sol'; |
| 7 | +import {UpgradeableERC20} from './UpgradeableERC20.sol'; |
| 8 | +import {IGhoToken} from './interfaces/IGhoToken.sol'; |
| 9 | + |
| 10 | +/** |
| 11 | + * @title Upgradeable GHO Token |
| 12 | + * @author Aave Labs |
| 13 | + */ |
| 14 | +contract UpgradeableGhoToken is VersionedInitializable, UpgradeableERC20, AccessControl, IGhoToken { |
| 15 | + using EnumerableSet for EnumerableSet.AddressSet; |
| 16 | + |
| 17 | + mapping(address => Facilitator) internal _facilitators; |
| 18 | + EnumerableSet.AddressSet internal _facilitatorsList; |
| 19 | + |
| 20 | + /// @inheritdoc IGhoToken |
| 21 | + bytes32 public constant FACILITATOR_MANAGER_ROLE = keccak256('FACILITATOR_MANAGER_ROLE'); |
| 22 | + |
| 23 | + /// @inheritdoc IGhoToken |
| 24 | + bytes32 public constant BUCKET_MANAGER_ROLE = keccak256('BUCKET_MANAGER_ROLE'); |
| 25 | + |
| 26 | + /** |
| 27 | + * @dev Constructor |
| 28 | + */ |
| 29 | + constructor() UpgradeableERC20(18) { |
| 30 | + // Intentionally left bank |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @dev Initializer |
| 35 | + * @param admin This is the initial holder of the default admin role |
| 36 | + */ |
| 37 | + function initialize(address admin) public virtual initializer { |
| 38 | + _ERC20_init('Gho Token', 'GHO'); |
| 39 | + |
| 40 | + _setupRole(DEFAULT_ADMIN_ROLE, admin); |
| 41 | + } |
| 42 | + |
| 43 | + /// @inheritdoc IGhoToken |
| 44 | + function mint(address account, uint256 amount) external { |
| 45 | + require(amount > 0, 'INVALID_MINT_AMOUNT'); |
| 46 | + Facilitator storage f = _facilitators[msg.sender]; |
| 47 | + |
| 48 | + uint256 currentBucketLevel = f.bucketLevel; |
| 49 | + uint256 newBucketLevel = currentBucketLevel + amount; |
| 50 | + require(f.bucketCapacity >= newBucketLevel, 'FACILITATOR_BUCKET_CAPACITY_EXCEEDED'); |
| 51 | + f.bucketLevel = uint128(newBucketLevel); |
| 52 | + |
| 53 | + _mint(account, amount); |
| 54 | + |
| 55 | + emit FacilitatorBucketLevelUpdated(msg.sender, currentBucketLevel, newBucketLevel); |
| 56 | + } |
| 57 | + |
| 58 | + /// @inheritdoc IGhoToken |
| 59 | + function burn(uint256 amount) external { |
| 60 | + require(amount > 0, 'INVALID_BURN_AMOUNT'); |
| 61 | + |
| 62 | + Facilitator storage f = _facilitators[msg.sender]; |
| 63 | + uint256 currentBucketLevel = f.bucketLevel; |
| 64 | + uint256 newBucketLevel = currentBucketLevel - amount; |
| 65 | + f.bucketLevel = uint128(newBucketLevel); |
| 66 | + |
| 67 | + _burn(msg.sender, amount); |
| 68 | + |
| 69 | + emit FacilitatorBucketLevelUpdated(msg.sender, currentBucketLevel, newBucketLevel); |
| 70 | + } |
| 71 | + |
| 72 | + /// @inheritdoc IGhoToken |
| 73 | + function addFacilitator( |
| 74 | + address facilitatorAddress, |
| 75 | + string calldata facilitatorLabel, |
| 76 | + uint128 bucketCapacity |
| 77 | + ) external onlyRole(FACILITATOR_MANAGER_ROLE) { |
| 78 | + Facilitator storage facilitator = _facilitators[facilitatorAddress]; |
| 79 | + require(bytes(facilitator.label).length == 0, 'FACILITATOR_ALREADY_EXISTS'); |
| 80 | + require(bytes(facilitatorLabel).length > 0, 'INVALID_LABEL'); |
| 81 | + |
| 82 | + facilitator.label = facilitatorLabel; |
| 83 | + facilitator.bucketCapacity = bucketCapacity; |
| 84 | + |
| 85 | + _facilitatorsList.add(facilitatorAddress); |
| 86 | + |
| 87 | + emit FacilitatorAdded( |
| 88 | + facilitatorAddress, |
| 89 | + keccak256(abi.encodePacked(facilitatorLabel)), |
| 90 | + bucketCapacity |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + /// @inheritdoc IGhoToken |
| 95 | + function removeFacilitator( |
| 96 | + address facilitatorAddress |
| 97 | + ) external onlyRole(FACILITATOR_MANAGER_ROLE) { |
| 98 | + require( |
| 99 | + bytes(_facilitators[facilitatorAddress].label).length > 0, |
| 100 | + 'FACILITATOR_DOES_NOT_EXIST' |
| 101 | + ); |
| 102 | + require( |
| 103 | + _facilitators[facilitatorAddress].bucketLevel == 0, |
| 104 | + 'FACILITATOR_BUCKET_LEVEL_NOT_ZERO' |
| 105 | + ); |
| 106 | + |
| 107 | + delete _facilitators[facilitatorAddress]; |
| 108 | + _facilitatorsList.remove(facilitatorAddress); |
| 109 | + |
| 110 | + emit FacilitatorRemoved(facilitatorAddress); |
| 111 | + } |
| 112 | + |
| 113 | + /// @inheritdoc IGhoToken |
| 114 | + function setFacilitatorBucketCapacity( |
| 115 | + address facilitator, |
| 116 | + uint128 newCapacity |
| 117 | + ) external onlyRole(BUCKET_MANAGER_ROLE) { |
| 118 | + require(bytes(_facilitators[facilitator].label).length > 0, 'FACILITATOR_DOES_NOT_EXIST'); |
| 119 | + |
| 120 | + uint256 oldCapacity = _facilitators[facilitator].bucketCapacity; |
| 121 | + _facilitators[facilitator].bucketCapacity = newCapacity; |
| 122 | + |
| 123 | + emit FacilitatorBucketCapacityUpdated(facilitator, oldCapacity, newCapacity); |
| 124 | + } |
| 125 | + |
| 126 | + /// @inheritdoc IGhoToken |
| 127 | + function getFacilitator(address facilitator) external view returns (Facilitator memory) { |
| 128 | + return _facilitators[facilitator]; |
| 129 | + } |
| 130 | + |
| 131 | + /// @inheritdoc IGhoToken |
| 132 | + function getFacilitatorBucket(address facilitator) external view returns (uint256, uint256) { |
| 133 | + return (_facilitators[facilitator].bucketCapacity, _facilitators[facilitator].bucketLevel); |
| 134 | + } |
| 135 | + |
| 136 | + /// @inheritdoc IGhoToken |
| 137 | + function getFacilitatorsList() external view returns (address[] memory) { |
| 138 | + return _facilitatorsList.values(); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * @notice Returns the revision number |
| 143 | + * @return The revision number |
| 144 | + */ |
| 145 | + function REVISION() public pure virtual returns (uint256) { |
| 146 | + return 1; |
| 147 | + } |
| 148 | + |
| 149 | + /// @inheritdoc VersionedInitializable |
| 150 | + function getRevision() internal pure virtual override returns (uint256) { |
| 151 | + return REVISION(); |
| 152 | + } |
| 153 | +} |
0 commit comments