|
| 1 | +// SPDX-License-Identifier: SEE LICENSE IN LICENSE |
| 2 | +pragma solidity 0.8.13; |
| 3 | + |
| 4 | +import { MerkleProof } from "@openzeppelin/contracts-4.6.0/utils/cryptography/MerkleProof.sol"; |
| 5 | +import { IERC20 } from "@openzeppelin/contracts-4.6.0/token/ERC20/IERC20.sol"; |
| 6 | + |
| 7 | +interface ITokenGovernance { |
| 8 | + function token() external view returns (IERC20); |
| 9 | + |
| 10 | + function mint(address to, uint256 amount) external; |
| 11 | +} |
| 12 | + |
| 13 | +interface IBancorNetworkV3 { |
| 14 | + function depositFor( |
| 15 | + address provider, |
| 16 | + address pool, |
| 17 | + uint256 tokenAmount |
| 18 | + ) external payable returns (uint256); |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * @dev this contract allows claiming/staking V2.1 pending rewards |
| 23 | + */ |
| 24 | +contract StakingRewardsClaim { |
| 25 | + error AccessDenied(); |
| 26 | + error AlreadyClaimed(); |
| 27 | + error InvalidAddress(); |
| 28 | + error InvalidClaim(); |
| 29 | + error ZeroValue(); |
| 30 | + |
| 31 | + // the V3 network contract |
| 32 | + IBancorNetworkV3 private immutable _networkV3; |
| 33 | + |
| 34 | + // the address of the BNT token governance |
| 35 | + ITokenGovernance private immutable _bntGovernance; |
| 36 | + |
| 37 | + // the address of the BNT token |
| 38 | + IERC20 private immutable _bnt; |
| 39 | + |
| 40 | + // the merkle root of the pending rewards merkle tree |
| 41 | + bytes32 private immutable _merkleRoot; |
| 42 | + |
| 43 | + // the total claimed amount |
| 44 | + uint256 private _totalClaimed; |
| 45 | + |
| 46 | + // a mapping of providers which have already claimed their rewards |
| 47 | + mapping(address => bool) private _claimed; |
| 48 | + |
| 49 | + /** |
| 50 | + * @dev triggered when rewards are claimed |
| 51 | + */ |
| 52 | + event RewardsClaimed(address indexed provider, uint256 amount); |
| 53 | + |
| 54 | + /** |
| 55 | + * @dev triggered when rewards are staked |
| 56 | + */ |
| 57 | + event RewardsStaked(address indexed provider, uint256 amount); |
| 58 | + |
| 59 | + modifier validAddress(address addr) { |
| 60 | + _validAddress(addr); |
| 61 | + |
| 62 | + _; |
| 63 | + } |
| 64 | + |
| 65 | + modifier greaterThanZero(uint256 value) { |
| 66 | + _greaterThanZero(value); |
| 67 | + |
| 68 | + _; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @dev initializes the merkle-tree rewards airdrop contract |
| 73 | + */ |
| 74 | + constructor( |
| 75 | + IBancorNetworkV3 initNetworkV3, |
| 76 | + ITokenGovernance initBNTGovernance, |
| 77 | + bytes32 initMerkleRoot |
| 78 | + ) validAddress(address(initNetworkV3)) validAddress(address(initBNTGovernance)) { |
| 79 | + _networkV3 = initNetworkV3; |
| 80 | + _bntGovernance = initBNTGovernance; |
| 81 | + _bnt = initBNTGovernance.token(); |
| 82 | + |
| 83 | + _merkleRoot = initMerkleRoot; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @dev returns the merkle root of the pending rewards merkle tree |
| 88 | + */ |
| 89 | + function merkleRoot() external view returns (bytes32) { |
| 90 | + return _merkleRoot; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @dev returns the total claimed amount |
| 95 | + */ |
| 96 | + function totalClaimed() external view returns (uint256) { |
| 97 | + return _totalClaimed; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @dev returns whether providers have already claimed their rewards |
| 102 | + */ |
| 103 | + function hasClaimed(address account) external view returns (bool) { |
| 104 | + return _claimed[account]; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @dev claims rewards by providing a merkle proof (a { provider, amount } leaf and a merkle path) |
| 109 | + * |
| 110 | + * requirements: |
| 111 | + * |
| 112 | + * - the claim can be only made by the beneficiary of the reward |
| 113 | + */ |
| 114 | + function claimRewards( |
| 115 | + address provider, |
| 116 | + uint256 fullAmount, |
| 117 | + bytes32[] calldata proof |
| 118 | + ) external greaterThanZero(fullAmount) { |
| 119 | + _claimRewards(msg.sender, provider, fullAmount, proof, false); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @dev claims rewards by providing a merkle proof (a { provider, amount } leaf and a merkle path) and stakes them |
| 124 | + * in V3 |
| 125 | + * |
| 126 | + * requirements: |
| 127 | + * |
| 128 | + * - the claim can be only made by the beneficiary of the reward |
| 129 | + */ |
| 130 | + function stakeRewards( |
| 131 | + address provider, |
| 132 | + uint256 fullAmount, |
| 133 | + bytes32[] calldata proof |
| 134 | + ) external greaterThanZero(fullAmount) { |
| 135 | + _claimRewards(msg.sender, provider, fullAmount, proof, true); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * @dev claims or stakes rewards |
| 140 | + */ |
| 141 | + function _claimRewards( |
| 142 | + address caller, |
| 143 | + address provider, |
| 144 | + uint256 fullAmount, |
| 145 | + bytes32[] calldata proof, |
| 146 | + bool stake |
| 147 | + ) private { |
| 148 | + // allow users to opt-it for receiving their rewards |
| 149 | + if (caller != provider) { |
| 150 | + revert AccessDenied(); |
| 151 | + } |
| 152 | + |
| 153 | + // ensure that the user can't claim or stake rewards twice |
| 154 | + if (_claimed[provider]) { |
| 155 | + revert AlreadyClaimed(); |
| 156 | + } |
| 157 | + |
| 158 | + // ensure that the claim is valid |
| 159 | + bytes32 leaf = keccak256(abi.encodePacked(provider, fullAmount)); |
| 160 | + if (!MerkleProof.verify(proof, _merkleRoot, leaf)) { |
| 161 | + revert InvalidClaim(); |
| 162 | + } |
| 163 | + |
| 164 | + _claimed[provider] = true; |
| 165 | + _totalClaimed += fullAmount; |
| 166 | + |
| 167 | + if (stake) { |
| 168 | + // mint the full rewards to the contract itself and deposit them on behalf of the provider |
| 169 | + _bntGovernance.mint(address(this), fullAmount); |
| 170 | + |
| 171 | + _bnt.approve(address(_networkV3), fullAmount); |
| 172 | + _networkV3.depositFor(provider, address(_bnt), fullAmount); |
| 173 | + |
| 174 | + emit RewardsStaked(provider, fullAmount); |
| 175 | + } else { |
| 176 | + // mint the rewards directly to the provider |
| 177 | + _bntGovernance.mint(provider, fullAmount); |
| 178 | + |
| 179 | + emit RewardsClaimed(provider, fullAmount); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * @dev verifies that a given address is valid |
| 185 | + */ |
| 186 | + function _validAddress(address addr) internal pure { |
| 187 | + if (addr == address(0)) { |
| 188 | + revert InvalidAddress(); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * @dev verifies that a given amount is greater than zero |
| 194 | + */ |
| 195 | + function _greaterThanZero(uint256 value) internal pure { |
| 196 | + if (value == 0) { |
| 197 | + revert ZeroValue(); |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments