all files / contracts/legacy/ Escrow.sol

0% Statements 0/52
0% Branches 0/84
0% Functions 0/16
0% Lines 0/88
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
pragma solidity 0.6.2;
 
import './THXCTokenInterface.sol';
import './SafeMath.sol';
 
contract Escrow {
    using SafeMath for uint256;
    event IntermediateStorage(string _url, string _hash);
    event Pending(string manifest, string hash);
    event BulkTransfer(uint256 indexed _txId, uint256 _bulkCount);
 
    enum EscrowStatuses {
        Launched,
        Pending,
        Partial,
        Paid,
        Complete,
        Cancelled
    }
    EscrowStatuses public status;
 
    address public reputationOracle;
    address public recordingOracle;
    address public launcher;
    address payable public canceler;
 
    uint256 public reputationOracleStake;
    uint256 public recordingOracleStake;
    uint256 private constant BULK_MAX_VALUE = 1000000000 * (10 ** 18);
    uint32 private constant BULK_MAX_COUNT = 100;
 
    address public eip20;
 
    string public manifestUrl;
    string public manifestHash;
 
    string public finalResultsUrl;
    string public finalResultsHash;
 
    uint256 public duration;
 
    uint256[] public finalAmounts;
    bool public bulkPaid;
 
    mapping(address => bool) public areTrustedHandlers;
 
    constructor(
        address _eip20,
        address payable _canceler,
        uint256 _duration,
        address[] memory _handlers
    ) public {
        eip20 = _eip20;
        status = EscrowStatuses.Launched;
        duration = _duration.add(block.timestamp); // solhint-disable-line not-rely-on-time
        launcher = msg.sender;
        canceler = _canceler;
        areTrustedHandlers[_canceler] = true;
        areTrustedHandlers[msg.sender] = true;
        addTrustedHandlers(_handlers);
    }
 
    function getBalance() public view returns (uint256) {
        return THXCTokenInterface(eip20).balanceOf(address(this));
    }
 
    function addTrustedHandlers(address[] memory _handlers) public {
        require(
            areTrustedHandlers[msg.sender],
            'Address calling cannot add trusted handlers'
        );
        for (uint256 i = 0; i < _handlers.length; i++) {
            areTrustedHandlers[_handlers[i]] = true;
        }
    }
 
    // The escrower puts the Token in the contract without an agentless
    // and assigsn a reputation oracle to payout the bounty of size of the
    // amount specified
    function setup(
        address _reputationOracle,
        address _recordingOracle,
        uint256 _reputationOracleStake,
        uint256 _recordingOracleStake,
        string memory _url,
        string memory _hash
    ) public trusted notExpired {
        require(
            _reputationOracle != address(0),
            'Invalid or missing token spender'
        );
        require(
            _recordingOracle != address(0),
            'Invalid or missing token spender'
        );
        uint256 totalStake = _reputationOracleStake.add(_recordingOracleStake);
        require(totalStake >= 0 && totalStake <= 100, 'Stake out of bounds');
        require(
            status == EscrowStatuses.Launched,
            'Escrow not in Launched status state'
        );
 
        reputationOracle = _reputationOracle;
        recordingOracle = _recordingOracle;
        areTrustedHandlers[reputationOracle] = true;
        areTrustedHandlers[recordingOracle] = true;
 
        reputationOracleStake = _reputationOracleStake;
        recordingOracleStake = _recordingOracleStake;
 
        manifestUrl = _url;
        manifestHash = _hash;
        status = EscrowStatuses.Pending;
        emit Pending(manifestUrl, manifestHash);
    }
 
    function abort() public trusted notComplete notPaid {
        if (getBalance() != 0) {
            cancel();
        }
        selfdestruct(canceler);
    }
 
    function cancel()
        public
        trusted
        notBroke
        notComplete
        notPaid
        returns (bool)
    {
        bool success = THXCTokenInterface(eip20).transfer(canceler, getBalance());
        status = EscrowStatuses.Cancelled;
        return success;
    }
 
    function complete() public notExpired {
        require(
            msg.sender == reputationOracle || areTrustedHandlers[msg.sender],
            'Address calling is not trusted'
        );
        require(status == EscrowStatuses.Paid, 'Escrow not in Paid state');
        status = EscrowStatuses.Complete;
    }
 
    function storeResults(
        string memory _url,
        string memory _hash
    ) public trusted notExpired {
        require(
            status == EscrowStatuses.Pending ||
                status == EscrowStatuses.Partial,
            'Escrow not in Pending or Partial status state'
        );
        emit IntermediateStorage(_url, _hash);
    }
 
    function bulkPayOut(
        address[] memory _recipients,
        uint256[] memory _amounts,
        string memory _url,
        string memory _hash,
        uint256 _txId
    ) public trusted notBroke notLaunched notPaid notExpired returns (bool) {
        require(
            _recipients.length == _amounts.length,
            "Amount of recipients and values don't match"
        );
        require(_recipients.length < BULK_MAX_COUNT, 'Too many recipients');
 
        uint256 balance = getBalance();
        bulkPaid = false;
        uint256 aggregatedBulkAmount = 0;
        for (uint256 i; i < _amounts.length; i++) {
            aggregatedBulkAmount += _amounts[i];
        }
        require(aggregatedBulkAmount < BULK_MAX_VALUE, 'Bulk value too high');
 
        if (balance < aggregatedBulkAmount) {
            return bulkPaid;
        }
 
        bool writeOnchain = bytes(_hash).length != 0 || bytes(_url).length != 0;
        if (writeOnchain) {
            // Be sure they are both zero if one of them is
            finalResultsUrl = _url;
            finalResultsHash = _hash;
        }
 
        (
            uint256 reputationOracleFee,
            uint256 recordingOracleFee
        ) = finalizePayouts(_amounts);
        THXCTokenInterface token = THXCTokenInterface(eip20);
 
        for (uint i = 0; i < _recipients.length; ++i) {
            token.transfer(_recipients[i], finalAmounts[i]);
        }
 
        delete finalAmounts;
        bulkPaid =
            token.transfer(reputationOracle, reputationOracleFee) &&
            token.transfer(recordingOracle, recordingOracleFee);
 
        balance = getBalance();
        if (bulkPaid) {
            if (status == EscrowStatuses.Pending) {
                status = EscrowStatuses.Partial;
            }
            if (balance == 0 && status == EscrowStatuses.Partial) {
                status = EscrowStatuses.Paid;
            }
        }
        emit BulkTransfer(_txId, _recipients.length);
        return bulkPaid;
    }
 
    function finalizePayouts(
        uint256[] memory _amounts
    ) internal returns (uint256, uint256) {
        uint256 reputationOracleFee = 0;
        uint256 recordingOracleFee = 0;
        for (uint256 j; j < _amounts.length; j++) {
            uint256 singleReputationOracleFee = reputationOracleStake
                .mul(_amounts[j])
                .div(100);
            uint256 singleRecordingOracleFee = recordingOracleStake
                .mul(_amounts[j])
                .div(100);
            uint256 amount = _amounts[j].sub(singleReputationOracleFee).sub(
                singleRecordingOracleFee
            );
            reputationOracleFee = reputationOracleFee.add(
                singleReputationOracleFee
            );
            recordingOracleFee = recordingOracleFee.add(
                singleRecordingOracleFee
            );
            finalAmounts.push(amount);
        }
        return (reputationOracleFee, recordingOracleFee);
    }
 
    modifier trusted() {
        require(areTrustedHandlers[msg.sender], 'Address calling not trusted');
        _;
    }
 
    modifier notBroke() {
        require(getBalance() != 0, 'EIP20 contract out of funds');
        _;
    }
 
    modifier notComplete() {
        require(
            status != EscrowStatuses.Complete,
            'Escrow in Complete status state'
        );
        _;
    }
 
    modifier notPaid() {
        require(status != EscrowStatuses.Paid, 'Escrow in Paid status state');
        _;
    }
 
    modifier notLaunched() {
        require(
            status != EscrowStatuses.Launched,
            'Escrow in Launched status state'
        );
        _;
    }
 
    modifier notExpired() {
        require(duration > block.timestamp, 'Contract expired'); // solhint-disable-line not-rely-on-time
        _;
    }
}