/** * Copyright 2017–2019, LaborX PTY * Licensed under the AGPL Version 3 license. */ pragma solidity ^0.4.25; import "../common/initializable/InitializableStorageAdapter.sol"; import "../common/FeeConstants.sol"; import "../escrow/EscrowBaseInterface.sol"; import "../workflow/WorkflowBase.sol"; import "../libs/SafeMath.sol"; import "../libs/DataParser.sol"; contract LaborContractCore is InitializableStorageAdapter, FeeConstants { StorageInterface.Bytes32AddressMapping internal escrowStorage; StorageInterface.Bytes32AddressMapping internal workflowStorage; /// @dev keccak(contractId, signer address) => boolean StorageInterface.Bytes32BoolMapping internal signsStorage; /// @dev keccak(contractId, signer address) => salt (from #sign) StorageInterface.Bytes32UIntMapping internal signsSaltStorage; /// @dev keccak(contractId, signer address) => signature (from #sign) StorageInterface.BytesSequenceMapping internal signsSignatureStorage; /// @dev contractId => set of salts StorageInterface.UIntSetMapping internal signsInvalidatedSaltsStorage; // StorageInterface.Bytes32Bytes32Mapping internal baseContractStorage; // StorageInterface.Bytes32SetMapping internal linkedContractsStorage; StorageInterface.Bytes32UInt8Mapping internal contractStateStorage; StorageInterface.Bytes32Bytes32Mapping internal contractDocumentHashStorage; StorageInterface.BytesSequenceMapping internal contractInfoStorage; StorageInterface.BytesSequenceMapping internal terminationsStorage; StorageInterface.BytesSequenceMapping internal proposedTasksStorage; StorageInterface.Bytes32Bytes32Mapping internal proposedDocumentHashStorage; /// @dev proposition to store ID of termination with the highest termination budget /// contractId => max locked termination value StorageInterface.Bytes32UIntMapping internal terminationLockedBalanceStorage; /// @dev contractId => upfront balance StorageInterface.Bytes32UIntMapping internal budgetUpfrontPaymentAmountStorage; // StorageInterface.Bytes32UIntMapping internal totalLockBudgetAmountStorage; StorageInterface.Bytes32UIntMapping internal totalBudgetAmountStorage; /// @dev contractId => sum of total completed (of future) payments StorageInterface.Bytes32UIntMapping internal completedPaymentsBalanceStorage; /// @dev contractId => sum of total completed and transferred payments StorageInterface.Bytes32UIntMapping internal transferredPaymentsBalanceStorage; StorageInterface.Bytes32UIntMapping internal terminationRequestIdStorage; StorageInterface.StringMapping internal terminationReasonStorage; StorageInterface.Address internal terminatableStorage; StorageInterface.Address internal disputableStorage; StorageInterface.Address internal signableStorage; StorageInterface.Address internal completableStorage; StorageInterface.Address internal tasksProposableStorage; function _initCore() internal { escrowStorage.init("escrow"); workflowStorage.init("workflow"); signsStorage.init("signs"); signsSaltStorage.init("signsSalt"); signsSignatureStorage.init("signsSignature"); signsInvalidatedSaltsStorage.init("signsInvalidatedSalts"); // baseContractStorage.init("baseContract"); // linkedContractsStorage.init("linkedContracts"); contractStateStorage.init("contractState"); contractDocumentHashStorage.init("contractDocumentHash"); contractInfoStorage.init("contractInfo"); terminationsStorage.init("terminations"); proposedTasksStorage.init("proposedTasks"); proposedDocumentHashStorage.init("proposedDocumentHash"); terminationLockedBalanceStorage.init("terminationLockedBalance"); budgetUpfrontPaymentAmountStorage.init("budgetUpfrontPaymentAmount"); // totalLockBudgetAmountStorage.init("totalLockBudgetAmount"); totalBudgetAmountStorage.init("totalBudgetAmount"); terminationRequestIdStorage.init("terminationRequestId"); terminationReasonStorage.init("terminationReason"); completedPaymentsBalanceStorage.init("completedPaymentsBalance"); transferredPaymentsBalanceStorage.init("transferredPaymentsBalance"); terminatableStorage.init("terminatable"); disputableStorage.init("disputable"); signableStorage.init("signable"); completableStorage.init("completable"); tasksProposableStorage.init("tasksProposable"); } } contract LaborContractEventEmitter { event ContractCreated(bytes32 indexed contractId, bytes32 documentHash); event ContractSigned(bytes32 indexed contractId, bytes32 documentHash, address signer, uint expireAtBlock, uint salt); event ContractSignRevoked(bytes32 indexed contractId, address signer); event ContractStarted(bytes32 indexed contractId, uint _depositValue, uint employerFee, uint assigneeFee); event ContractTasksProposed(bytes32 indexed contractId, bytes32 documentHash); event ContractTasksAccepted(bytes32 indexed contractId, bytes32 documentHash); event ContractDisputeResolved(bytes32 indexed contractId); event ContractTerminationRequested(bytes32 indexed contractId, uint terminationId, address initiator, string comment); event ContractTerminationRequestCancelled(bytes32 indexed contractId, uint terminationId); event ContractTerminated(bytes32 indexed contractId, uint terminationId); event ContractStateTransitioned(bytes32 indexed contractId, uint8 indexed previousState, uint8 indexed currentState); event ContractOperationLogged(bytes32 indexed contractId, string indexed operationAction); event ContractWithdrawOperationLogged(bytes32 indexed contractId, string indexed operationAction, uint depositValue, uint paymentAmount); } contract LaborContractAbstract is LaborContractCore, LaborContractEventEmitter { using SafeMath for uint; enum State { NOT_INITIALIZED, CREATED, SIGNED, STARTED, TERMINATION_REQUESTED, TERMINATED, UNDER_DISPUTE, COMPLETED } uint constant internal OK = 1; uint constant internal TERMINATION_PERCENT_PRECISION = 10000; modifier onlyInState(bytes32 _contractId, State _state) { require(_getContractState(_contractId) == _state, "C_S"); _; } function getEscrow(bytes32 _contractId) public view returns (EscrowBaseInterface) { return EscrowBaseInterface(store.get(escrowStorage, _contractId)); } function getWorkflow(bytes32 _contractId) public view returns (WorkflowBase) { return WorkflowBase(store.get(workflowStorage, _contractId)); } function getServiceSurchargePercent(bytes32 _contractId) public view returns (uint _employerPercent, uint _assigneePercent, uint _precision) { DataParser.ContractData memory _contractInfo = DataParser.parseContractDataCleanInput(_getContractInfoBytes(_contractId)); (_employerPercent, _assigneePercent, _precision) = getEscrow(_contractId).getFeeInfo( _contractId, _contractInfo.employer, _contractInfo.assignee ); } function version() public pure returns (string); /** INTERNAL */ function _setContractStateTo(bytes32 _contractId, State _newState) internal { require(_newState != State.NOT_INITIALIZED, "C_NI"); State _currentState = _getContractState(_contractId); if (_currentState == _newState) { return; } store.set(contractStateStorage, _contractId, uint8(_newState)); emit ContractStateTransitioned(_contractId, uint8(_currentState), uint8(_newState)); } function _getContractState(bytes32 _contractId) internal view returns (State) { return State(store.get(contractStateStorage, _contractId)); } function _getContractDocumentHash( bytes32 _contractId, DataParser.ContractData memory _contractInfo ) internal view returns (bytes32 _documentHash) { _documentHash = store.get(contractDocumentHashStorage, _contractId); if (_documentHash == bytes32(0)) { _documentHash = _contractInfo.documentHash; } } function _getContractInfoBytes(bytes32 _contractId) internal view returns (bytes memory _data) { return store.get(contractInfoStorage, _contractId); } function _getTerminationsInfoBytes(bytes32 _contractId) internal view returns (bytes memory _data) { return store.get(terminationsStorage, _contractId); } function _getContractAccumulatedPaymentAmount(bytes32 _contractId) internal view returns (uint) { return store.get(completedPaymentsBalanceStorage, _contractId); } function _getContractTransferredPaymentAmount(bytes32 _contractId) internal view returns (uint) { return store.get(transferredPaymentsBalanceStorage, _contractId); } function _getContractTotalBudgetAmount(bytes32 _contractId) internal view returns (uint) { return store.get(totalBudgetAmountStorage, _contractId); } function _getContractLockedTerminationAmount(bytes32 _contractId) internal view returns (uint) { return store.get(terminationLockedBalanceStorage, _contractId); } function _getLeftToPayAmount(bytes32 _contractId) internal view returns (uint) { return _getContractAccumulatedPaymentAmount(_contractId).sub(_getContractTransferredPaymentAmount(_contractId)); } function _incrementTransferredBalance(bytes32 _contractId, uint _value) internal { store.set(transferredPaymentsBalanceStorage, _contractId, _getContractTransferredPaymentAmount(_contractId).add(_value)); } function _decrementTransferredBalance(bytes32 _contractId, uint _value) internal { store.set(transferredPaymentsBalanceStorage, _contractId, _getContractTransferredPaymentAmount(_contractId).sub(_value)); } function _incrementLockedTerminationAmount(bytes32 _contractId, uint _value) internal { store.set(terminationLockedBalanceStorage, _contractId, _getContractLockedTerminationAmount(_contractId).add(_value)); } function _incrementUpfrontPaymentAmount(bytes32 _contractId, uint _value) internal { if (_value > 0) { uint _currentUpfrontPaymentAmount = store.get(budgetUpfrontPaymentAmountStorage, _contractId); store.set(budgetUpfrontPaymentAmountStorage, _contractId, _currentUpfrontPaymentAmount.add(_value)); } } function _decrementLockedTerminationAmount(bytes32 _contractId, uint _value) internal { store.set(terminationLockedBalanceStorage, _contractId, _getContractLockedTerminationAmount(_contractId).sub(_value)); } function _incrementCompletedPaymentValue(bytes32 _contractId, uint _value) internal { uint _completedPaymentsBalance = _getContractAccumulatedPaymentAmount(_contractId); store.set(completedPaymentsBalanceStorage, _contractId, _completedPaymentsBalance.add(_value)); } function _decrementCompletedPaymentValue(bytes32 _contractId, uint _value) internal { uint _completedPaymentsBalance = _getContractAccumulatedPaymentAmount(_contractId); store.set(completedPaymentsBalanceStorage, _contractId, _completedPaymentsBalance.sub(_value)); } }