diff --git a/src/v0.8/core/finance/escrow/EscrowDataTradingFee.sol b/src/v0.8/core/finance/escrow/EscrowDataTradingFee.sol index bfb61b1c..40b975f6 100644 --- a/src/v0.8/core/finance/escrow/EscrowDataTradingFee.sol +++ b/src/v0.8/core/finance/escrow/EscrowDataTradingFee.sol @@ -195,50 +195,69 @@ contract EscrowDataTradingFee is EscrowBase { address _payer, address _token ) public view override onlyRole(roles, RolesType.DATASWAP_CONTRACT) returns (uint256 amount) { + (, , uint256 current, uint256 total ) = roles.finance().getAccountEscrow( + _datasetId, + _matchingId, + _payer, + _token, + FinanceType.Type.EscrowDataTradingFee + ); + if (_payer == roles.matchingsBids().getMatchingWinner(_matchingId)) { amount = roles.matchingsBids().getMatchingBidAmount( _matchingId, _payer ); } else if (_payer == roles.datasets().getDatasetMetadataSubmitter(_datasetId)) { - // Source account balance - (, , uint256 balance, ) = roles.finance().getAccountEscrow( - _datasetId, - 0, - _payer, - _token, - FinanceType.Type.EscrowDataTradingFee - ); - - (, , uint64 matchingSize, , , ) = roles - .matchingsTarget() - .getMatchingTarget(_matchingId); - - (uint256 usedSize, , , , , ) = roles - .storages() - .getDatasetStorageOverview(_datasetId); - - uint256 unusedSize = roles.datasetsProof().getDatasetSize( - _datasetId, - DatasetType.DataType.Source - ) * - roles.datasetsRequirement().getDatasetReplicasCount(_datasetId) - - usedSize; - - amount = (balance / unusedSize) * matchingSize; + if (total <= 0) { + amount = _getSubsidyAmount(_datasetId, _matchingId, _payer, _token); + } else { + amount = total; + } } else { require(false, "payer account does not exist"); } - (, , uint256 current, ) = roles.finance().getAccountEscrow( + amount = current >= amount ? 0 : amount - current; + } + + /// @notice Get matching subsidy amount. + /// @param _datasetId The ID of the dataset. + /// @param _matchingId The ID of the matching process. + /// @param _payer An array containing the addresses of the dataset and matching process payers. + /// @param _token The type of token for escrow handling (e.g., FIL, ERC-20). + /// @return subsidy The subsidy amount. + function _getSubsidyAmount( + uint64 _datasetId, + uint64 _matchingId, + address _payer, + address _token + ) internal view returns (uint256 subsidy) { + // Source account balance + (, , uint256 balance, ) = roles.finance().getAccountEscrow( _datasetId, - _matchingId, + 0, _payer, _token, FinanceType.Type.EscrowDataTradingFee ); - amount = current >= amount ? 0 : amount - current; + (, , uint64 matchingSize, , , ) = roles + .matchingsTarget() + .getMatchingTarget(_matchingId); + + (uint256 usedSize, , , , , ) = roles + .storages() + .getDatasetStorageOverview(_datasetId); + + uint256 unusedSize = roles.datasetsProof().getDatasetSize( + _datasetId, + DatasetType.DataType.Source + ) * + roles.datasetsRequirement().getDatasetReplicasCount(_datasetId) - + usedSize; + + subsidy = (balance / unusedSize) * matchingSize; } /// @dev Internal function to get payers associated with a dataset and matching process. diff --git a/src/v0.8/module/matching/MatchingsBids.sol b/src/v0.8/module/matching/MatchingsBids.sol index f3e36daf..339b1c44 100644 --- a/src/v0.8/module/matching/MatchingsBids.sol +++ b/src/v0.8/module/matching/MatchingsBids.sol @@ -359,13 +359,18 @@ contract MatchingsBids is winner ); - // Transfer parent account escrow to sub-account escrow - roles.finance().__claimMoveEscrow( - _datasetId, - _matchingId, - FinanceType.FIL, - FinanceType.Type.EscrowDataTradingFee - ); + (,,,DatasetType.DataType dataType,,) = roles.matchingsTarget().getMatchingTarget(_matchingId); + + if (dataType == DatasetType.DataType.Source) { + // Transfer parent account escrow to sub-account escrow + roles.finance().__claimMoveEscrow( + _datasetId, + _matchingId, + FinanceType.FIL, + FinanceType.Type.EscrowDataTradingFee + ); + } + } else { uint64 _size = _afterMatchingFailed(_matchingId); roles.matchings().__reportMatchingNoWinner(_matchingId, _size);