/** * Copyright 2017–2019, LaborX PTY * Licensed under the AGPL Version 3 license. */ pragma solidity ^0.4.25; import "@laborx/solidity-storage-contracts/contracts/Storage.sol"; import "../common/initializable/InitializableOwned.sol"; import "../common/Signatures.sol"; import "./LaborContractCore.sol"; import "./LaborContractBase.sol"; import "./LaborContractCreatable.sol"; import "./LaborLib.sol"; import "../libs/DataParser.sol"; import "../libs/PercentCalculator.sol"; import "../libs/SafeMath.sol"; import "../libs/BytesLib.sol"; /// @title Part of LaborContract functionality that provides signing implementation. /// @dev Should be used as a library extension contract LaborContractSignable is LaborContractBaseSignable, InitializableOwned, LaborContractAbstract, LaborContractCreatable { /// @dev Indices for contract signable (locked) details values uint constant internal CONTRACT_SIGNABLE_DETAILS_TASKS_BUDGET = 0; uint constant internal CONTRACT_SIGNABLE_DETAILS_DEPOSIT_FEE_AMOUNT = 1; uint constant internal CONTRACT_SIGNABLE_DETAILS_TERMINATION_AMOUNT = 2; uint constant internal CONTRACT_SIGNABLE_DETAILS_UPFRONT_AMOUNT = 3; uint constant internal CONTRACT_SIGNABLE_DETAILS_WITHDRAW_FEE_AMOUNT = 4; uint constant internal CONTRACT_SIGNABLE_DETAILS_LENGTH = 5; constructor() StorageAdapter(Storage(0x0), "") public { // NOTE: don't need to initialize anything } function version() public pure returns (string) { return "0.2.1"; } function getLockingDepositBalance( bytes32 _contractId ) external view returns (uint _requiredDepositValue) { DataParser.ContractData memory _contractInfo = DataParser.parseContractDataCleanInput(_getContractInfoBytes(_contractId)); (_requiredDepositValue,,) = _getSignDepositAmount(_contractId, _contractInfo); } function getLockingContractDetails(bytes32 _contractId) external view returns (uint[] _values) { _values = new uint[](CONTRACT_SIGNABLE_DETAILS_LENGTH); uint _tasksBudget = _getContractTotalBudgetAmount(_contractId); _values[CONTRACT_SIGNABLE_DETAILS_TASKS_BUDGET] = _tasksBudget; _values[CONTRACT_SIGNABLE_DETAILS_TERMINATION_AMOUNT] = _getContractLockedTerminationAmount(_contractId); DataParser.ContractData memory _contractInfo = DataParser.parseContractDataCleanInput(_getContractInfoBytes(_contractId)); (uint16 _employerFeeValue, uint _feePrecision) = getEscrow(_contractId).getSellerFeeInfo(_contractInfo.employer); _values[CONTRACT_SIGNABLE_DETAILS_DEPOSIT_FEE_AMOUNT] = PercentCalculator.getPercent( _tasksBudget, _employerFeeValue, _feePrecision ); _values[CONTRACT_SIGNABLE_DETAILS_UPFRONT_AMOUNT] = store.get(budgetUpfrontPaymentAmountStorage, _contractId); (uint16 _assigneeFeeValue,) = getEscrow(_contractId).getBuyerFeeInfo(_contractInfo.assignee); _values[CONTRACT_SIGNABLE_DETAILS_WITHDRAW_FEE_AMOUNT] = PercentCalculator.getPercent( _values[CONTRACT_SIGNABLE_DETAILS_UPFRONT_AMOUNT], _assigneeFeeValue, _feePrecision ); } function createContractAndSign( bytes32 _contractId, EscrowBaseInterface _escrow, WorkflowBase _workflow, bytes _data, uint _expireAtBlock, uint _salt, bytes _signature ) external onlyInState(_contractId, State.NOT_INITIALIZED) returns (uint) { DataParser.ContractData memory _contractInfo = createContract(_contractId, _escrow, _workflow, _data); _sign(_contractInfo, _contractId, _expireAtBlock, _salt, _signature); return OK; } function sign( bytes32 _contractId, bytes32 _documentHash, uint _expireAtBlock, uint _salt, bytes _signature ) external onlyInState(_contractId, State.CREATED) returns (uint) { DataParser.ContractData memory _contractInfo = DataParser.parseContractDataCleanInput(_getContractInfoBytes(_contractId)); require(_documentHash == _contractInfo.documentHash, "C_H"); _sign(_contractInfo, _contractId, _expireAtBlock, _salt, _signature); return OK; } function _sign( DataParser.ContractData memory _contractInfo, bytes32 _contractId, uint _expireAtBlock, uint _salt, bytes _signature ) private { require(msg.sender == _contractInfo.assignee, "C_IC"); _saveSignSignature(_contractId, _contractInfo, _salt, _signature); _setContractStateTo(_contractId, State.SIGNED); emit ContractSigned(_contractId, _contractInfo.documentHash, msg.sender, _expireAtBlock, _salt); } function revokeSign( bytes32 _contractId ) external onlyInState(_contractId, State.SIGNED) returns (uint) { require(_isContractSigned(_contractId, msg.sender), "C_SGN"); _revokeSignSignature(_contractId); _setContractStateTo(_contractId, State.CREATED); emit ContractSignRevoked(_contractId, msg.sender); return OK; } function signAndDeposit( bytes32 _contractId, bytes32 _documentHash, uint _value, uint _expireAtBlock, uint _salt, bytes _signature ) external payable onlyInState(_contractId, State.SIGNED) returns (uint) { DataParser.ContractData memory _contractInfo = DataParser.parseContractDataCleanInput(_getContractInfoBytes(_contractId)); require(_documentHash == _contractInfo.documentHash, "C_H"); require(msg.sender == _contractInfo.employer, "C_IC"); (uint _requiredDepositAmount, uint _initialTransferAmount, uint _serviceFee) = _getSignDepositAmount(_contractId, _contractInfo); require(_value == _requiredDepositAmount, "C_A"); // TODO: version 2: cache escrow address _makeInitialDepositToEscrow(_contractId, _value, _initialTransferAmount, _serviceFee, _contractInfo); if (_contractInfo.contractType == uint8(DataParser.ContractType.JOB)) { _setDisputeArbiterToEscrow(_contractId, _contractInfo, _expireAtBlock, _salt, _signature); _cleanInvalidatedSalts(_contractId); } else if (_contractInfo.contractType == uint8(DataParser.ContractType.DISPUTE)) { _initiateDisputeToEscrow(_contractInfo.linkedContractId, _expireAtBlock, _salt, _signature); _setContractStateTo(_contractInfo.linkedContractId, State.UNDER_DISPUTE); } _setContractStateTo(_contractId, State.STARTED); _emitContractCreated(_contractId, _contractInfo, _value); return OK; } /** PRIVATE */ function _saveSignSignature( bytes32 _contractId, DataParser.ContractData memory _contractInfo, uint _salt, bytes _signature ) private { bytes32 _key = keccak256(abi.encodePacked(_contractId, msg.sender)); if ( _contractInfo.contractType == uint8(DataParser.ContractType.JOB) && _contractInfo.arbiter != 0x0 ) { require(_salt != 0, "C_SS"); require(Signatures.validSignaturesLength(_signature), "C_SS"); require( !store.includes(signsInvalidatedSaltsStorage, _contractId, _salt), "C_SS" ); store.set(signsSaltStorage, _key, _salt); store.set(signsSignatureStorage, _key, _signature); } store.set(signsStorage, _key, true); } function _revokeSignSignature(bytes32 _contractId) private { bytes32 _key = keccak256(abi.encodePacked(_contractId, msg.sender)); uint _signSalt = store.get(signsSaltStorage, _key); store.set(signsStorage, _key, false); if (_signSalt != 0) { store.add(signsInvalidatedSaltsStorage, _contractId, _signSalt); store.set(signsSaltStorage, _key, 0); store.set(signsSignatureStorage, _key, new bytes(0)); } } function _cleanInvalidatedSalts(bytes32 _contractId) private { uint _length = store.count(signsInvalidatedSaltsStorage, _contractId); for (uint _idx = 0; _idx < _length; ++_idx) { uint _valueToRemove = store.get(signsInvalidatedSaltsStorage, _contractId, _idx); store.remove(signsInvalidatedSaltsStorage, _contractId, _valueToRemove); } } /** ESCROW */ function _makeInitialDepositToEscrow( bytes32 _contractId, uint _value, uint _initialTransferAmount, uint _serviceFee, DataParser.ContractData memory _contractInfo ) private { uint _terminationDepositBalance = _getContractLockedTerminationAmount(_contractId); uint _depositAmount = _value.sub(_terminationDepositBalance).sub(_serviceFee); EscrowBaseInterface _escrow = getEscrow(_contractId); /** 1. For contract budget we take fee from deposit value and transfer back upfront payment */ require( OK == _escrow.createEscrow.value(SafeMath.min(_depositAmount, msg.value)) ( _contractId, _contractInfo.employer, _contractInfo.assignee, _contractInfo.paymentCurrencySymbol, _depositAmount, _initialTransferAmount, _getBuyerFeeFlag() ), "C_CE" ); _incrementCompletedPaymentValue(_contractId, _initialTransferAmount); _incrementTransferredBalance(_contractId, _initialTransferAmount); /** 2. For termination budget we take no deposit fee */ if (_terminationDepositBalance > 0) { require( OK == _escrow.deposit.value(SafeMath.min(_terminationDepositBalance, msg.value)) ( _contractId, _contractInfo.employer, _contractInfo.assignee, _terminationDepositBalance, 0, _getNoFeeFlag() ), "C_DE" ); // _decrementLockedTerminationAmount(_contractId, _terminationDepositBalance); } /** 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" ); } } function _setDisputeArbiterToEscrow( bytes32 _contractId, DataParser.ContractData memory _contractInfo, uint _expireAtBlock, uint _salt, bytes _signature ) private { if (_contractInfo.arbiter == 0x0) { return; } bytes memory _assigneeSignature = _getSignSignature(_contractId, _contractInfo.assignee); bytes memory _signatures = BytesLib.concat(_signature, _assigneeSignature); require( OK == getEscrow(_contractId).setArbiter( _contractId, _contractInfo.employer, _contractInfo.assignee, _contractInfo.arbiter, _expireAtBlock, _salt, _signatures ), "C_SA" ); } function _initiateDisputeToEscrow( bytes32 _contractId, uint _expireAtBlock, uint _salt, bytes _signature ) private { DataParser.ContractData memory _contractInfo = DataParser.parseContractDataCleanInput(_getContractInfoBytes(_contractId)); if (_contractInfo.arbiter != 0x0) { require( OK == getEscrow(_contractId).initiateDispute( _contractId, _contractInfo.employer, _contractInfo.assignee, _expireAtBlock, _salt, _signature ), "C_IDE" ); } } /** CALCULATIONS */ /// @dev Makes calculation about initial deposit values. /// Deposited value includes full contract budget service fee /// independently from either 'lockFullBudget' is true or false. function _getSignDepositAmount( bytes32 _contractId, DataParser.ContractData memory _contractInfo ) private view returns ( uint _requiredDepositAmount, uint _initialTransferAmount, uint _serviceFee ) { // NOTE: since at this moment no record in escrow about a deal could be found, // we take exact fee value from an escrow fee records. (uint _employerFeeValue, uint _feePrecision) = getEscrow(_contractId).getSellerFeeInfo(_contractInfo.employer); return LaborLib._calculateContractSignDepositAmount( _contractInfo, _getContractTotalBudgetAmount(_contractId), store.get(budgetUpfrontPaymentAmountStorage, _contractId), _getContractLockedTerminationAmount(_contractId), uint16(_employerFeeValue), _feePrecision ); } /** PRIVATE GETTERS */ function _getSignSignature( bytes32 _contractId, address _signer ) private view returns (bytes memory _signature) { bytes32 _key = keccak256(abi.encodePacked(_contractId, _signer)); _signature = store.get(signsSignatureStorage, _key); } function _isContractSigned(bytes32 _contractId, address _signer) private view returns (bool) { return store.get(signsStorage, keccak256(abi.encodePacked(_contractId, _signer))); } function _emitContractCreated(bytes32 _contractId, DataParser.ContractData memory _contractInfo, uint _value) private { (uint _employerPercent, uint _assigneePercent,) = getEscrow(_contractId).getFeeInfo( _contractId, _contractInfo.employer, _contractInfo.assignee ); emit ContractStarted(_contractId, _value,_employerPercent,_assigneePercent); } }