|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +/** |
| 4 | + * @authors: [@ferittuncer, @unknownunknown1] |
| 5 | + * @reviewers: [] |
| 6 | + * @auditors: [] |
| 7 | + * @bounties: [] |
| 8 | + */ |
| 9 | + |
| 10 | +import "../IArbitrable.sol"; |
| 11 | +import "../../evidence/IMetaEvidence.sol"; |
| 12 | + |
| 13 | +pragma solidity ^0.8; |
| 14 | + |
| 15 | +/** |
| 16 | + * @title DisputeResolver |
| 17 | + * DisputeResolver contract adapted for V2 https://github.com/kleros/arbitrable-proxy-contracts/blob/master/contracts/ArbitrableProxy.sol. |
| 18 | + */ |
| 19 | +contract DisputeResolver is IArbitrable, IMetaEvidence { |
| 20 | + struct DisputeStruct { |
| 21 | + bytes arbitratorExtraData; // Extra data for the dispute. |
| 22 | + bool isRuled; // True if the dispute has been ruled. |
| 23 | + uint256 ruling; // Ruling given to the dispute. |
| 24 | + uint256 numberOfRulingOptions; // The number of choices the arbitrator can give. |
| 25 | + } |
| 26 | + |
| 27 | + IArbitrator public immutable arbitrator; // Arbitrator is set in constructor and never changed. |
| 28 | + |
| 29 | + DisputeStruct[] public disputes; // Local disputes. |
| 30 | + mapping(uint256 => uint256) public externalIDtoLocalID; // Maps external (arbitrator side) dispute IDs to local dispute IDs. |
| 31 | + |
| 32 | + /** @dev Constructor |
| 33 | + * @param _arbitrator Target global arbitrator for any disputes. |
| 34 | + */ |
| 35 | + constructor(IArbitrator _arbitrator) { |
| 36 | + arbitrator = _arbitrator; |
| 37 | + } |
| 38 | + |
| 39 | + /** @dev TRUSTED. Calls createDispute function of the specified arbitrator to create a dispute. |
| 40 | + Note that we don’t need to check that msg.value is enough to pay arbitration fees as it’s the responsibility of the arbitrator contract. |
| 41 | + * @param _arbitratorExtraData Extra data for the arbitrator of the dispute. |
| 42 | + * @param _metaevidenceURI Link to metaevidence of the dispute. |
| 43 | + * @param _numberOfRulingOptions Number of ruling options. |
| 44 | + * @return disputeID Dispute id (on arbitrator side) of the created dispute. |
| 45 | + */ |
| 46 | + function createDispute( |
| 47 | + bytes calldata _arbitratorExtraData, |
| 48 | + string calldata _metaevidenceURI, |
| 49 | + uint256 _numberOfRulingOptions |
| 50 | + ) external payable returns (uint256 disputeID) { |
| 51 | + require(_numberOfRulingOptions > 1, "Should be at least 2 ruling options."); |
| 52 | + |
| 53 | + disputeID = arbitrator.createDispute{value: msg.value}(_numberOfRulingOptions, _arbitratorExtraData); |
| 54 | + uint256 localDisputeID = disputes.length; |
| 55 | + disputes.push( |
| 56 | + DisputeStruct({ |
| 57 | + arbitratorExtraData: _arbitratorExtraData, |
| 58 | + isRuled: false, |
| 59 | + ruling: 0, |
| 60 | + numberOfRulingOptions: _numberOfRulingOptions |
| 61 | + }) |
| 62 | + ); |
| 63 | + |
| 64 | + externalIDtoLocalID[disputeID] = localDisputeID; |
| 65 | + |
| 66 | + emit MetaEvidence(localDisputeID, _metaevidenceURI); |
| 67 | + emit Dispute(arbitrator, disputeID, localDisputeID, localDisputeID); |
| 68 | + } |
| 69 | + |
| 70 | + /** @dev To be called by the arbitrator of the dispute, to declare the winning ruling. |
| 71 | + * @param _externalDisputeID ID of the dispute in arbitrator contract. |
| 72 | + * @param _ruling The ruling choice of the arbitration. |
| 73 | + */ |
| 74 | + function rule(uint256 _externalDisputeID, uint256 _ruling) external override { |
| 75 | + uint256 localDisputeID = externalIDtoLocalID[_externalDisputeID]; |
| 76 | + DisputeStruct storage dispute = disputes[localDisputeID]; |
| 77 | + require(msg.sender == address(arbitrator), "Only the arbitrator can execute this."); |
| 78 | + require(_ruling <= dispute.numberOfRulingOptions, "Invalid ruling."); |
| 79 | + require(!dispute.isRuled, "This dispute has been ruled already."); |
| 80 | + |
| 81 | + dispute.isRuled = true; |
| 82 | + dispute.ruling = _ruling; |
| 83 | + |
| 84 | + emit Ruling(IArbitrator(msg.sender), _externalDisputeID, dispute.ruling); |
| 85 | + } |
| 86 | +} |
0 commit comments