/** * Copyright 2017–2019, LaborX PTY * Licensed under the AGPL version 3 license. */ pragma solidity ^0.4.25; import "../common/initializable/InitializableOwned.sol"; import "./LaborContractCore.sol"; import "./LaborContractBase.sol"; import "../libs/DataParser.sol"; import "./LaborLib.sol"; contract LaborContractCompletable is LaborContractBaseCompletable, InitializableOwned, LaborContractAbstract { /// @dev Indices for contract completion details values uint constant internal CONTRACT_COMPLETION_DETAILS_PAID_AMOUNT = 0; uint constant internal CONTRACT_COMPLETION_DETAILS_COMPLETED_AMOUNT = 1; uint constant internal CONTRACT_COMPLETION_DETAILS_UNPAID_AMOUNT = 2; uint constant internal CONTRACT_COMPLETION_DETAILS_ESCROW_BALANCE = 3; uint constant internal CONTRACT_COMPLETION_DETAILS_NEED_TO_DEPOSIT_AMOUNT = 4; uint constant internal CONTRACT_COMPLETION_DETAILS_DEPOSIT_FEE_AMOUNT = 5; uint constant internal CONTRACT_COMPLETION_DETAILS_EMPLOYE_AMOUNT = 6; uint constant internal CONTRACT_COMPLETION_DETAILS_ASSIGNEE_AMOUNT = 7; uint constant internal CONTRACT_COMPLETION_DETAILS_LOCKED_TERMINATION_AMOUNT = 8; uint constant internal CONTRACT_COMPLETION_DETAILS_WITHDRAW_FEE_AMOUNT = 9; uint constant internal CONTRACT_COMPLETION_DETAILS_LENGTH = 10; constructor() StorageAdapter(Storage(0x0), "") public { // NOTE: don't need to initialize anything } function version() public pure returns (string) { return "0.2.1"; } function getCompletionContractDepositBalance( bytes32 _contractId ) external view returns ( uint _depositValue, uint _employerPaybackValue, uint _assigneeResolveValue ) { DataParser.ContractData memory _contractInfo = DataParser.parseContractDataCleanInput(_getContractInfoBytes(_contractId)); return _getCompletionAmounts(_contractId, _contractInfo); } function getCompletionContractDetails(bytes32 _contractId) external view returns (uint[] _values) { _values = new uint[](CONTRACT_COMPLETION_DETAILS_LENGTH); DataParser.ContractData memory _contractInfo = DataParser.parseContractDataCleanInput(_getContractInfoBytes(_contractId)); uint _accumulatedBalance = _getLeftToPayAmount(_contractId); if (_contractInfo.paymentTimeline == uint8(DataParser.PaymentTimeline.PAY_BY_TASK)) { _values[CONTRACT_COMPLETION_DETAILS_PAID_AMOUNT] = _accumulatedBalance; _values[CONTRACT_COMPLETION_DETAILS_COMPLETED_AMOUNT] = 0; } else if (_contractInfo.paymentTimeline == uint8(DataParser.PaymentTimeline.PAY_BY_PROJECT)) { _values[CONTRACT_COMPLETION_DETAILS_PAID_AMOUNT] = 0; _values[CONTRACT_COMPLETION_DETAILS_COMPLETED_AMOUNT] = _accumulatedBalance; } uint _totalBudget = _getContractTotalBudgetAmount(_contractId); _values[CONTRACT_COMPLETION_DETAILS_UNPAID_AMOUNT] = _totalBudget.sub(_accumulatedBalance); EscrowBaseInterface _escrow = getEscrow(_contractId); (,uint _escrowBalance) = _escrow.getBalanceOf(_contractId, _contractInfo.employer, _contractInfo.assignee); _values[CONTRACT_COMPLETION_DETAILS_ESCROW_BALANCE] = _escrowBalance; ( uint _depositedValue, uint _paybackEmployerValue, uint _releaseAssigneeValue ) = LaborLib._calculateContractCompletion( _contractInfo, _escrowBalance, _accumulatedBalance ); _values[CONTRACT_COMPLETION_DETAILS_DEPOSIT_FEE_AMOUNT] = 0; _values[CONTRACT_COMPLETION_DETAILS_NEED_TO_DEPOSIT_AMOUNT] = _depositedValue; _values[CONTRACT_COMPLETION_DETAILS_EMPLOYE_AMOUNT] = _paybackEmployerValue; _values[CONTRACT_COMPLETION_DETAILS_ASSIGNEE_AMOUNT] = _releaseAssigneeValue; _values[CONTRACT_COMPLETION_DETAILS_LOCKED_TERMINATION_AMOUNT] = 0; (, uint _assigneePercent, uint _percentPrecision) = getServiceSurchargePercent(_contractId); _values[CONTRACT_COMPLETION_DETAILS_WITHDRAW_FEE_AMOUNT] = PercentCalculator.getPercent( _releaseAssigneeValue, _assigneePercent, _percentPrecision ); } function completeContract( bytes32 _contractId, uint _expireAtBlock, uint _salt, bytes _signatures ) external payable onlyInState(_contractId, State.STARTED) returns (uint) { DataParser.ContractData memory _contractInfo = DataParser.parseContractDataCleanInput(_getContractInfoBytes(_contractId)); require(_contractInfo.contractType == uint8(DataParser.ContractType.JOB), "C_CJ"); require(msg.sender == _contractInfo.employer, "C_IC"); _completeContractToEscrow(_contractId, _contractInfo, _expireAtBlock, _salt, _signatures); _setContractStateTo(_contractId, State.COMPLETED); return OK; } /** PRIVATE */ function _completeContractToEscrow( bytes32 _contractId, DataParser.ContractData memory _contractInfo, uint _expireAtBlock, uint _salt, bytes _signatures ) private { (uint _depositedValue, uint _paybackEmployerValue, uint _releaseAssigneeValue) = _getCompletionAmounts(_contractId, _contractInfo); EscrowBaseInterface _escrow = getEscrow(_contractId); if (_depositedValue > 0) { require( OK == _escrow.deposit.value(msg.value) ( _contractId, _contractInfo.employer, _contractInfo.assignee, _depositedValue, 0, _getNoFeeFlag() ), "C_DE" ); } if (_paybackEmployerValue.add(_releaseAssigneeValue) > 0) { require( OK == _escrow.releaseNegotiatedPayment( _contractId, _contractInfo.employer, _contractInfo.assignee, _paybackEmployerValue, _releaseAssigneeValue, _expireAtBlock, _salt, _signatures, _getAllFeeFlag() ), "" ); } require( OK == _escrow.deleteEscrow(_contractId, _contractInfo.employer, _contractInfo.assignee), "C_DCE" ); } /** PRIVATE GETTERS */ function _getCompletionAmounts( bytes32 _contractId, DataParser.ContractData memory _contractInfo ) private view returns (uint _depositValue, uint _employerValue, uint _assigneeValue) { EscrowBaseInterface _escrow = getEscrow(_contractId); (,uint _escrowBalance) = _escrow.getBalanceOf(_contractId, _contractInfo.employer, _contractInfo.assignee); uint _accumulatedPaymentValue = _getLeftToPayAmount(_contractId); return LaborLib._calculateContractCompletion(_contractInfo, _escrowBalance, _accumulatedPaymentValue); } }