|
| 1 | +/** |
| 2 | +▓▓▌ ▓▓ ▐▓▓ ▓▓▓▓▓▓▓▓▓▓▌▐▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▄ |
| 3 | +▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▌▐▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ |
| 4 | + ▓▓▓▓▓▓ ▓▓▓▓▓▓▓▀ ▐▓▓▓▓▓▓ ▐▓▓▓▓▓ ▓▓▓▓▓▓ ▓▓▓▓▓ ▐▓▓▓▓▓▌ ▐▓▓▓▓▓▓ |
| 5 | + ▓▓▓▓▓▓▄▄▓▓▓▓▓▓▓▀ ▐▓▓▓▓▓▓▄▄▄▄ ▓▓▓▓▓▓▄▄▄▄ ▐▓▓▓▓▓▌ ▐▓▓▓▓▓▓ |
| 6 | + ▓▓▓▓▓▓▓▓▓▓▓▓▓▀ ▐▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▌ ▐▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ |
| 7 | + ▓▓▓▓▓▓▀▀▓▓▓▓▓▓▄ ▐▓▓▓▓▓▓▀▀▀▀ ▓▓▓▓▓▓▀▀▀▀ ▐▓▓▓▓▓▓▓▓▓▓▓▓▓▓▀ |
| 8 | + ▓▓▓▓▓▓ ▀▓▓▓▓▓▓▄ ▐▓▓▓▓▓▓ ▓▓▓▓▓ ▓▓▓▓▓▓ ▓▓▓▓▓ ▐▓▓▓▓▓▌ |
| 9 | +▓▓▓▓▓▓▓▓▓▓ █▓▓▓▓▓▓▓▓▓ ▐▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓ |
| 10 | +▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓ ▐▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓ |
| 11 | +
|
| 12 | + Trust math, not hardware. |
| 13 | +*/ |
| 14 | + |
| 15 | +pragma solidity 0.5.17; |
| 16 | + |
| 17 | +import "@keep-network/keep-core/contracts/KeepRegistry.sol"; |
| 18 | +import "@keep-network/keep-core/contracts/KeepStaking.sol"; |
| 19 | +import "@keep-network/sortition-pools/contracts/api/IBonding.sol"; |
| 20 | +import "openzeppelin-solidity/contracts/math/SafeMath.sol"; |
| 21 | + |
| 22 | + |
| 23 | +/// @title Keep Bonding |
| 24 | +/// @notice Contract holding deposits from keeps' operators. |
| 25 | +contract AbstractBonding is IBonding { |
| 26 | + using SafeMath for uint256; |
| 27 | + |
| 28 | + // Registry contract with a list of approved factories (operator contracts). |
| 29 | + KeepRegistry internal registry; |
| 30 | + |
| 31 | + // Staking contract. |
| 32 | + KeepStaking internal staking; |
| 33 | + |
| 34 | + // Unassigned value in wei deposited by operators. |
| 35 | + mapping(address => uint256) public unbondedValue; |
| 36 | + |
| 37 | + // References to created bonds. Bond identifier is built from operator's |
| 38 | + // address, holder's address and reference ID assigned on bond creation. |
| 39 | + mapping(bytes32 => uint256) internal lockedBonds; |
| 40 | + |
| 41 | + // Sortition pools authorized by operator's authorizer. |
| 42 | + // operator -> pool -> boolean |
| 43 | + mapping(address => mapping(address => bool)) internal authorizedPools; |
| 44 | + |
| 45 | + event UnbondedValueDeposited(address indexed operator, uint256 amount); |
| 46 | + event UnbondedValueWithdrawn( |
| 47 | + address indexed operator, |
| 48 | + address indexed beneficiary, |
| 49 | + uint256 amount |
| 50 | + ); |
| 51 | + event BondCreated( |
| 52 | + address indexed operator, |
| 53 | + address indexed holder, |
| 54 | + address indexed sortitionPool, |
| 55 | + uint256 referenceID, |
| 56 | + uint256 amount |
| 57 | + ); |
| 58 | + event BondReassigned( |
| 59 | + address indexed operator, |
| 60 | + uint256 indexed referenceID, |
| 61 | + address newHolder, |
| 62 | + uint256 newReferenceID |
| 63 | + ); |
| 64 | + event BondReleased(address indexed operator, uint256 indexed referenceID); |
| 65 | + event BondSeized( |
| 66 | + address indexed operator, |
| 67 | + uint256 indexed referenceID, |
| 68 | + address destination, |
| 69 | + uint256 amount |
| 70 | + ); |
| 71 | + |
| 72 | + /// @notice Initializes Keep Bonding contract. |
| 73 | + /// @param registryAddress Keep registry contract address. |
| 74 | + /// @param stakingContractAddress Keep staking contract address. |
| 75 | + constructor(address registryAddress, address stakingContractAddress) |
| 76 | + public |
| 77 | + { |
| 78 | + registry = KeepRegistry(registryAddress); |
| 79 | + staking = KeepStaking(stakingContractAddress); // Rename to: Staking |
| 80 | + } |
| 81 | + |
| 82 | + /// @notice Add the provided value to operator's pool available for bonding. |
| 83 | + /// @param operator Address of the operator. |
| 84 | + function deposit(address operator) public payable { |
| 85 | + unbondedValue[operator] = unbondedValue[operator].add(msg.value); |
| 86 | + emit UnbondedValueDeposited(operator, msg.value); |
| 87 | + } |
| 88 | + |
| 89 | + /// @notice Withdraws amount from operator's value available for bonding. |
| 90 | + /// @param amount Value to withdraw in wei. |
| 91 | + /// @param operator Address of the operator. |
| 92 | + function withdraw(uint256 amount, address operator) public; |
| 93 | + |
| 94 | + /// @notice Returns the amount of wei the operator has made available for |
| 95 | + /// bonding and that is still unbounded. If the operator doesn't exist or |
| 96 | + /// bond creator is not authorized as an operator contract or it is not |
| 97 | + /// authorized by the operator or there is no secondary authorization for |
| 98 | + /// the provided sortition pool, function returns 0. |
| 99 | + /// @dev Implements function expected by sortition pools' IBonding interface. |
| 100 | + /// @param operator Address of the operator. |
| 101 | + /// @param bondCreator Address authorized to create a bond. |
| 102 | + /// @param authorizedSortitionPool Address of authorized sortition pool. |
| 103 | + /// @return Amount of authorized wei deposit available for bonding. |
| 104 | + function availableUnbondedValue( |
| 105 | + address operator, |
| 106 | + address bondCreator, |
| 107 | + address authorizedSortitionPool |
| 108 | + ) public view returns (uint256) { |
| 109 | + // Sortition pools check this condition and skips operators that |
| 110 | + // are no longer eligible. We cannot revert here. |
| 111 | + if ( |
| 112 | + registry.isApprovedOperatorContract(bondCreator) && |
| 113 | + staking.isAuthorizedForOperator(operator, bondCreator) && |
| 114 | + hasSecondaryAuthorization(operator, authorizedSortitionPool) |
| 115 | + ) { |
| 116 | + return unbondedValue[operator]; |
| 117 | + } |
| 118 | + |
| 119 | + return 0; |
| 120 | + } |
| 121 | + |
| 122 | + /// @notice Create bond for the given operator, holder, reference and amount. |
| 123 | + /// @dev Function can be executed only by authorized contract. Reference ID |
| 124 | + /// should be unique for holder and operator. |
| 125 | + /// @param operator Address of the operator to bond. |
| 126 | + /// @param holder Address of the holder of the bond. |
| 127 | + /// @param referenceID Reference ID used to track the bond by holder. |
| 128 | + /// @param amount Value to bond in wei. |
| 129 | + /// @param authorizedSortitionPool Address of authorized sortition pool. |
| 130 | + function createBond( |
| 131 | + address operator, |
| 132 | + address holder, |
| 133 | + uint256 referenceID, |
| 134 | + uint256 amount, |
| 135 | + address authorizedSortitionPool |
| 136 | + ) public { |
| 137 | + require( |
| 138 | + availableUnbondedValue( |
| 139 | + operator, |
| 140 | + msg.sender, |
| 141 | + authorizedSortitionPool |
| 142 | + ) >= amount, |
| 143 | + "Insufficient unbonded value" |
| 144 | + ); |
| 145 | + |
| 146 | + bytes32 bondID = keccak256( |
| 147 | + abi.encodePacked(operator, holder, referenceID) |
| 148 | + ); |
| 149 | + |
| 150 | + require( |
| 151 | + lockedBonds[bondID] == 0, |
| 152 | + "Reference ID not unique for holder and operator" |
| 153 | + ); |
| 154 | + |
| 155 | + unbondedValue[operator] = unbondedValue[operator].sub(amount); |
| 156 | + lockedBonds[bondID] = lockedBonds[bondID].add(amount); |
| 157 | + |
| 158 | + emit BondCreated( |
| 159 | + operator, |
| 160 | + holder, |
| 161 | + authorizedSortitionPool, |
| 162 | + referenceID, |
| 163 | + amount |
| 164 | + ); |
| 165 | + } |
| 166 | + |
| 167 | + /// @notice Returns value of wei bonded for the operator. |
| 168 | + /// @param operator Address of the operator. |
| 169 | + /// @param holder Address of the holder of the bond. |
| 170 | + /// @param referenceID Reference ID of the bond. |
| 171 | + /// @return Amount of wei in the selected bond. |
| 172 | + function bondAmount(address operator, address holder, uint256 referenceID) |
| 173 | + public |
| 174 | + view |
| 175 | + returns (uint256) |
| 176 | + { |
| 177 | + bytes32 bondID = keccak256( |
| 178 | + abi.encodePacked(operator, holder, referenceID) |
| 179 | + ); |
| 180 | + |
| 181 | + return lockedBonds[bondID]; |
| 182 | + } |
| 183 | + |
| 184 | + /// @notice Reassigns a bond to a new holder under a new reference. |
| 185 | + /// @dev Function requires that a caller is the current holder of the bond |
| 186 | + /// which is being reassigned. |
| 187 | + /// @param operator Address of the bonded operator. |
| 188 | + /// @param referenceID Reference ID of the bond. |
| 189 | + /// @param newHolder Address of the new holder of the bond. |
| 190 | + /// @param newReferenceID New reference ID to register the bond. |
| 191 | + function reassignBond( |
| 192 | + address operator, |
| 193 | + uint256 referenceID, |
| 194 | + address newHolder, |
| 195 | + uint256 newReferenceID |
| 196 | + ) public { |
| 197 | + address holder = msg.sender; |
| 198 | + bytes32 bondID = keccak256( |
| 199 | + abi.encodePacked(operator, holder, referenceID) |
| 200 | + ); |
| 201 | + |
| 202 | + require(lockedBonds[bondID] > 0, "Bond not found"); |
| 203 | + |
| 204 | + bytes32 newBondID = keccak256( |
| 205 | + abi.encodePacked(operator, newHolder, newReferenceID) |
| 206 | + ); |
| 207 | + |
| 208 | + require( |
| 209 | + lockedBonds[newBondID] == 0, |
| 210 | + "Reference ID not unique for holder and operator" |
| 211 | + ); |
| 212 | + |
| 213 | + lockedBonds[newBondID] = lockedBonds[bondID]; |
| 214 | + lockedBonds[bondID] = 0; |
| 215 | + |
| 216 | + emit BondReassigned(operator, referenceID, newHolder, newReferenceID); |
| 217 | + } |
| 218 | + |
| 219 | + /// @notice Releases the bond and moves the bond value to the operator's |
| 220 | + /// unbounded value pool. |
| 221 | + /// @dev Function requires that caller is the holder of the bond which is |
| 222 | + /// being released. |
| 223 | + /// @param operator Address of the bonded operator. |
| 224 | + /// @param referenceID Reference ID of the bond. |
| 225 | + function freeBond(address operator, uint256 referenceID) public { |
| 226 | + address holder = msg.sender; |
| 227 | + bytes32 bondID = keccak256( |
| 228 | + abi.encodePacked(operator, holder, referenceID) |
| 229 | + ); |
| 230 | + |
| 231 | + require(lockedBonds[bondID] > 0, "Bond not found"); |
| 232 | + |
| 233 | + uint256 amount = lockedBonds[bondID]; |
| 234 | + lockedBonds[bondID] = 0; |
| 235 | + unbondedValue[operator] = unbondedValue[operator].add(amount); |
| 236 | + |
| 237 | + emit BondReleased(operator, referenceID); |
| 238 | + } |
| 239 | + |
| 240 | + /// @notice Seizes the bond by moving some or all of the locked bond to the |
| 241 | + /// provided destination address. |
| 242 | + /// @dev Function requires that a caller is the holder of the bond which is |
| 243 | + /// being seized. |
| 244 | + /// @param operator Address of the bonded operator. |
| 245 | + /// @param referenceID Reference ID of the bond. |
| 246 | + /// @param amount Amount to be seized. |
| 247 | + /// @param destination Address to send the amount to. |
| 248 | + function seizeBond( |
| 249 | + address operator, |
| 250 | + uint256 referenceID, |
| 251 | + uint256 amount, |
| 252 | + address payable destination |
| 253 | + ) public { |
| 254 | + require(amount > 0, "Requested amount should be greater than zero"); |
| 255 | + |
| 256 | + address payable holder = msg.sender; |
| 257 | + bytes32 bondID = keccak256( |
| 258 | + abi.encodePacked(operator, holder, referenceID) |
| 259 | + ); |
| 260 | + |
| 261 | + require( |
| 262 | + lockedBonds[bondID] >= amount, |
| 263 | + "Requested amount is greater than the bond" |
| 264 | + ); |
| 265 | + |
| 266 | + lockedBonds[bondID] = lockedBonds[bondID].sub(amount); |
| 267 | + |
| 268 | + (bool success, ) = destination.call.value(amount)(""); |
| 269 | + require(success, "Transfer failed"); |
| 270 | + |
| 271 | + emit BondSeized(operator, referenceID, destination, amount); |
| 272 | + } |
| 273 | + |
| 274 | + /// @notice Authorizes sortition pool for the provided operator. |
| 275 | + /// Operator's authorizers need to authorize individual sortition pools |
| 276 | + /// per application since they may be interested in participating only in |
| 277 | + /// a subset of keep types used by the given application. |
| 278 | + /// @dev Only operator's authorizer can call this function. |
| 279 | + function authorizeSortitionPoolContract( |
| 280 | + address _operator, |
| 281 | + address _poolAddress |
| 282 | + ) public { |
| 283 | + require( |
| 284 | + staking.authorizerOf(_operator) == msg.sender, |
| 285 | + "Not authorized" |
| 286 | + ); |
| 287 | + authorizedPools[_operator][_poolAddress] = true; |
| 288 | + } |
| 289 | + |
| 290 | + /// @notice Deauthorizes sortition pool for the provided operator. |
| 291 | + /// Authorizer may deauthorize individual sortition pool in case the |
| 292 | + /// operator should no longer be eligible for work selection and the |
| 293 | + /// application represented by the sortition pool should no longer be |
| 294 | + /// eligible to create bonds for the operator. |
| 295 | + /// @dev Only operator's authorizer can call this function. |
| 296 | + function deauthorizeSortitionPoolContract( |
| 297 | + address _operator, |
| 298 | + address _poolAddress |
| 299 | + ) public { |
| 300 | + require( |
| 301 | + staking.authorizerOf(_operator) == msg.sender, |
| 302 | + "Not authorized" |
| 303 | + ); |
| 304 | + authorizedPools[_operator][_poolAddress] = false; |
| 305 | + } |
| 306 | + |
| 307 | + /// @notice Checks if the sortition pool has been authorized for the |
| 308 | + /// provided operator by its authorizer. |
| 309 | + /// @dev See authorizeSortitionPoolContract. |
| 310 | + function hasSecondaryAuthorization(address _operator, address _poolAddress) |
| 311 | + public |
| 312 | + view |
| 313 | + returns (bool) |
| 314 | + { |
| 315 | + return authorizedPools[_operator][_poolAddress]; |
| 316 | + } |
| 317 | + |
| 318 | + /// @notice Withdraws the provided amount from unbonded value of the |
| 319 | + /// provided operator to operator's beneficiary. If there is no enough |
| 320 | + /// unbonded value or the transfer failed, function fails. |
| 321 | + function withdrawBond(uint256 amount, address operator) internal { |
| 322 | + require( |
| 323 | + unbondedValue[operator] >= amount, |
| 324 | + "Insufficient unbonded value" |
| 325 | + ); |
| 326 | + |
| 327 | + unbondedValue[operator] = unbondedValue[operator].sub(amount); |
| 328 | + |
| 329 | + address beneficiary = staking.beneficiaryOf(operator); |
| 330 | + require( |
| 331 | + beneficiary != address(0), |
| 332 | + "Beneficiary not defined for the operator" |
| 333 | + ); |
| 334 | + |
| 335 | + (bool success, ) = beneficiary.call.value(amount)(""); |
| 336 | + require(success, "Transfer failed"); |
| 337 | + |
| 338 | + emit UnbondedValueWithdrawn(operator, beneficiary, amount); |
| 339 | + } |
| 340 | +} |
0 commit comments