/** * 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 LaborContractTasksProposable is LaborContractBaseTasksProposable, InitializableOwned, LaborContractAbstract { uint constant internal CONTRACT_PROPOSABLE_TASKS_DETAILS_AMOUNT = 0; uint constant internal CONTRACT_PROPOSABLE_TASKS_DETAILS_DEPOSIT_AMOUNT = 1; uint constant internal CONTRACT_PROPOSABLE_TASKS_DETAILS_DEPOSIT_FEE_AMOUNT = 2; uint constant internal CONTRACT_PROPOSABLE_TASKS_DETAILS_TERMINATION_PART_AMOUNT = 3; uint constant internal CONTRACT_PROPOSABLE_TASKS_DETAILS_UPFRONT_AMOUNT = 4; uint constant internal CONTRACT_PROPOSABLE_TASKS_DETAILS_WITHDRAW_FEE_AMOUNT = 5; uint constant internal CONTRACT_PROPOSABLE_TASKS_DETAILS_LENGTH = 6; modifier onlyWithProposedTasks(bytes32 _contractId) { require(store.get(proposedDocumentHashStorage, _contractId) != bytes32(0), "C_TNP"); // C_TNP == tasks are not proposed _; } constructor() StorageAdapter(Storage(0x0), "") public { // NOTE: don't need to initialize anything } function version() public pure returns (string) { return "0.2.1"; } function getContractProposedTasksDetails(bytes32 _contractId) external view returns (uint[] _values) { _values = new uint[](CONTRACT_PROPOSABLE_TASKS_DETAILS_LENGTH); DataParser.ContractData memory _contractInfo = DataParser.parseContractDataCleanInput(_getContractInfoBytes(_contractId)); (uint _proposedTasksBudget, uint _upfrontAmount, uint _totalBudget) = _getPrecalculatedTasksBudgetForProposedTasks(_contractId); ( uint _depositAmount, uint _initialTransferAmount, uint _serviceFee, uint _lockTerminationAmount ) = _getProposedTasksAcceptanceAmount( _contractId, _contractInfo, _totalBudget, _proposedTasksBudget, _upfrontAmount ); _values[CONTRACT_PROPOSABLE_TASKS_DETAILS_AMOUNT] = _proposedTasksBudget; _values[CONTRACT_PROPOSABLE_TASKS_DETAILS_DEPOSIT_AMOUNT] = _depositAmount; _values[CONTRACT_PROPOSABLE_TASKS_DETAILS_DEPOSIT_FEE_AMOUNT] = _serviceFee; _values[CONTRACT_PROPOSABLE_TASKS_DETAILS_TERMINATION_PART_AMOUNT] = _lockTerminationAmount; _values[CONTRACT_PROPOSABLE_TASKS_DETAILS_UPFRONT_AMOUNT] = _initialTransferAmount; (, uint _assigneeFeeValue, uint _feePrecision) = getServiceSurchargePercent(_contractId); _values[CONTRACT_PROPOSABLE_TASKS_DETAILS_WITHDRAW_FEE_AMOUNT] = PercentCalculator.getPercent( _values[CONTRACT_PROPOSABLE_TASKS_DETAILS_UPFRONT_AMOUNT], _assigneeFeeValue, _feePrecision ); } function _getPrecalculatedTasksBudgetForProposedTasks(bytes32 _contractId) private view returns ( uint _proposedTasksBudget, uint _upfrontAmount, uint _totalBudget ) { bytes memory _data = store.get(proposedTasksStorage, _contractId); (_proposedTasksBudget, _upfrontAmount) = getWorkflow(_contractId).precalculateWorkflowData(_contractId, address(this), _data); _totalBudget = _getContractTotalBudgetAmount(_contractId).add(_proposedTasksBudget); } function proposeTasks( bytes32 _contractId, bytes32 _updatedDocumentHash, bytes _data ) external onlyInState(_contractId, State.STARTED) returns (uint) { require(_updatedDocumentHash != bytes32(0), "C_IV"); // C_IV == invalid value DataParser.ContractData memory _contractInfo = DataParser.parseContractDataCleanInput(_getContractInfoBytes(_contractId)); require(_contractInfo.allowDynamicTasks, "C_ADT"); // C_ADT == dynamic tasks should be allowed require(msg.sender == _contractInfo.assignee, "C_IC"); // C_IC == invalid caller require(_getContractDocumentHash(_contractId, _contractInfo) != _updatedDocumentHash, "C_H"); // C_H == invalid updated document hash _validateContractProposedTasks(_contractId, _data); store.set(proposedTasksStorage, _contractId, _data); store.set(proposedDocumentHashStorage, _contractId, _updatedDocumentHash); emit ContractTasksProposed(_contractId, _updatedDocumentHash); return OK; } function acceptProposedTasksAndDeposit( bytes32 _contractId, bytes32 _updatedDocumentHash, uint _value ) external payable onlyInState(_contractId, State.STARTED) onlyWithProposedTasks(_contractId) returns (uint) { require(store.get(proposedDocumentHashStorage, _contractId) == _updatedDocumentHash, "C_H"); // C_H == invalid updated document hash DataParser.ContractData memory _contractInfo = DataParser.parseContractDataCleanInput(_getContractInfoBytes(_contractId)); require(msg.sender == _contractInfo.employer, "C_IC"); // C_IC == invalid caller bytes memory _data = store.get(proposedTasksStorage, _contractId); (uint _tasksBudget, uint _upfrontAmount) = getWorkflow(_contractId).addWorkflowData(_contractId, address(this), _data); require(_tasksBudget > 0, "C_B"); // C_B == invalid tasks budget _incrementTotalContractBudget(_contractId, _tasksBudget); _incrementUpfrontPaymentAmount(_contractId, _upfrontAmount); _handleTasksPropositionToEscrow(_contractId, _contractInfo, _tasksBudget, _upfrontAmount, _value); store.set(contractDocumentHashStorage, _contractId, _updatedDocumentHash); store.set(proposedDocumentHashStorage, _contractId, bytes32(0)); store.set(proposedTasksStorage, _contractId, new bytes(0)); emit ContractTasksAccepted(_contractId, _updatedDocumentHash); return OK; } /** PRIVATE */ function _incrementTotalContractBudget(bytes32 _contractId, uint _value) private { uint _currentBudget = _getContractTotalBudgetAmount(_contractId); store.set(totalBudgetAmountStorage, _contractId, _currentBudget.add(_value)); } function _handleTasksPropositionToEscrow( bytes32 _contractId, DataParser.ContractData memory _contractInfo, uint _proposedTasksBudget, uint _upfrontAmount, uint _inValue ) private { ( uint _requiredDepositAmount, uint _initialTransferAmount, uint _serviceFee, uint _lockTerminationAmount ) = _getProposedTasksAcceptanceAmount( _contractId, _contractInfo, _getContractTotalBudgetAmount(_contractId), _proposedTasksBudget, _upfrontAmount ); require(_requiredDepositAmount == _inValue, "C_A"); // C_A == invalid amount provided _proceedDepositToEscrow( _contractId, _contractInfo, _requiredDepositAmount, _initialTransferAmount, _lockTerminationAmount, _serviceFee ); } function _proceedDepositToEscrow( bytes32 _contractId, DataParser.ContractData memory _contractInfo, uint _value, uint _initialTransferAmount, uint _additionalLockTerminationAmount, uint _serviceFee ) private { uint _depositAmount = _value.sub(_additionalLockTerminationAmount).sub(_serviceFee); EscrowBaseInterface _escrow = getEscrow(_contractId); /** 1. For contract budget we take fee only from upfront value for a reason that fee is taken from full budget but a deposited value could differ from that value. */ require( OK == _escrow.deposit.value(SafeMath.min(_depositAmount, msg.value)) ( _contractId, _contractInfo.employer, _contractInfo.assignee, _depositAmount, _initialTransferAmount, _getBuyerFeeFlag() ), "C_DE" // C_DE == invalid escrow deposit operation ); _incrementCompletedPaymentValue(_contractId, _initialTransferAmount); _incrementTransferredBalance(_contractId, _initialTransferAmount); /** 2. For termination budget we take no deposit fee according to specification */ if (_additionalLockTerminationAmount > 0) { require( OK == _escrow.deposit.value(SafeMath.min(_additionalLockTerminationAmount, msg.value)) ( _contractId, _contractInfo.employer, _contractInfo.assignee, _additionalLockTerminationAmount, 0, _getNoFeeFlag() ), "C_DE" // C_DE == invalid escrow deposit operation ); _incrementLockedTerminationAmount(_contractId, _additionalLockTerminationAmount); } /** 3. Transfer service fee directly to fee recepient */ if (_serviceFee > 0) { require( OK == _escrow.retranslateToFeeRecipient.value(SafeMath.min(_serviceFee, msg.value)) ( _contractInfo.paymentCurrencySymbol, _contractInfo.employer, _serviceFee ), "C_FE" // C_FE == invalid fee escrow retranslation ); } } function _validateContractProposedTasks( bytes32 _contractId, bytes _data ) private view { (uint _tasksBudget,) = getWorkflow(_contractId).precalculateWorkflowData(_contractId, address(this), _data); require(_tasksBudget > 0, "C_B"); // C_B == invalid tasks budget } /** PRIVATE GETTERS */ function _getProposedTasksAcceptanceAmount( bytes32 _contractId, DataParser.ContractData memory _contractInfo, uint _totalBudget, uint _proposedTasksBudget, uint _upfrontAmount ) private view returns ( uint _requiredDepositAmount, uint _initialTransferAmount, uint _serviceFee, uint _lockTerminationAmount ) { DataParser.TerminationData[] memory _terminations = DataParser.parseTerminationDataCleanInput(_getTerminationsInfoBytes(_contractId)); uint _lockedTerminationAmount = _getContractLockedTerminationAmount(_contractId); return _calculateProposedTasksAcceptanceAmount( _contractId, _contractInfo, _terminations, _totalBudget, _lockedTerminationAmount, _proposedTasksBudget, _upfrontAmount ); } function _calculateProposedTasksAcceptanceAmount( bytes32 _contractId, DataParser.ContractData memory _contractInfo, DataParser.TerminationData[] memory _terminations, uint _totalBudget, uint _terminationLockedAmount, uint _proposedTasksBudget, uint _upfrontAmount ) private view returns ( uint _requiredDepositAmount, uint _initialTransferAmount, uint _serviceFee, uint _lockTerminationAmount ) { uint _updatedMaxTerminationAmount = LaborLib._findMaxTerminationValue(_terminations, _totalBudget, TERMINATION_PERCENT_PRECISION); _lockTerminationAmount = _updatedMaxTerminationAmount.sub(_terminationLockedAmount); (uint _employerFeeValue,, uint _feePrecision) = getServiceSurchargePercent(_contractId); (_requiredDepositAmount, _initialTransferAmount, _serviceFee) = LaborLib._calculateContractSignDepositAmount( _contractInfo, _proposedTasksBudget, _upfrontAmount, _lockTerminationAmount, uint16(_employerFeeValue), _feePrecision ); } }