|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.24; |
| 3 | + |
| 4 | +import {KlerosCore_TestBase} from "./KlerosCore_TestBase.sol"; |
| 5 | +import {KlerosCoreBase} from "../../src/arbitration/KlerosCoreBase.sol"; |
| 6 | +import {IArbitratorV2} from "../../src/arbitration/KlerosCoreBase.sol"; |
| 7 | +import {DisputeKitClassicBase} from "../../src/arbitration/dispute-kits/DisputeKitClassicBase.sol"; |
| 8 | +import {IArbitrableV2} from "../../src/arbitration/arbitrables/ArbitrableExample.sol"; |
| 9 | +import "../../src/libraries/Constants.sol"; |
| 10 | + |
| 11 | +/// @title KlerosCore_DisputesTest |
| 12 | +/// @dev Tests for KlerosCore dispute creation and management |
| 13 | +contract KlerosCore_DisputesTest is KlerosCore_TestBase { |
| 14 | + function test_createDispute_eth() public { |
| 15 | + // Create a new court and DK to test non-standard extra data |
| 16 | + uint256 newFee = 0.01 ether; |
| 17 | + uint96 newCourtID = 2; |
| 18 | + uint256 newNbJurors = 4; |
| 19 | + uint256 newDkID = 2; |
| 20 | + uint256[] memory supportedDK = new uint256[](1); |
| 21 | + supportedDK[0] = DISPUTE_KIT_CLASSIC; |
| 22 | + bytes memory newExtraData = abi.encodePacked(uint256(newCourtID), newNbJurors, newDkID); |
| 23 | + |
| 24 | + vm.prank(owner); |
| 25 | + core.addNewDisputeKit(disputeKit); // Just add the same dk to avoid dealing with initialization |
| 26 | + vm.prank(owner); |
| 27 | + core.createCourt( |
| 28 | + GENERAL_COURT, |
| 29 | + true, // Hidden votes |
| 30 | + 2000, // min stake |
| 31 | + 20000, // alpha |
| 32 | + newFee, // fee for juror |
| 33 | + 50, // jurors for jump |
| 34 | + [uint256(10), uint256(20), uint256(30), uint256(40)], // Times per period |
| 35 | + abi.encode(uint256(4)), // Sortition extra data |
| 36 | + supportedDK |
| 37 | + ); |
| 38 | + |
| 39 | + arbitrable.changeArbitratorExtraData(newExtraData); |
| 40 | + |
| 41 | + vm.expectRevert(KlerosCoreBase.ArbitrationFeesNotEnough.selector); |
| 42 | + vm.prank(disputer); |
| 43 | + arbitrable.createDispute{value: newFee * newNbJurors - 1}("Action"); |
| 44 | + |
| 45 | + vm.expectRevert(KlerosCoreBase.DisputeKitNotSupportedByCourt.selector); |
| 46 | + vm.prank(disputer); |
| 47 | + arbitrable.createDispute{value: 0.04 ether}("Action"); |
| 48 | + |
| 49 | + vm.prank(owner); |
| 50 | + supportedDK = new uint256[](1); |
| 51 | + supportedDK[0] = newDkID; |
| 52 | + core.enableDisputeKits(newCourtID, supportedDK, true); |
| 53 | + |
| 54 | + uint256 disputeID = 0; |
| 55 | + uint256 nbChoices = 2; |
| 56 | + vm.prank(disputer); |
| 57 | + vm.expectEmit(true, true, true, true); |
| 58 | + emit DisputeKitClassicBase.DisputeCreation(disputeID, nbChoices, newExtraData); |
| 59 | + vm.expectEmit(true, true, true, true); |
| 60 | + emit IArbitratorV2.DisputeCreation(disputeID, arbitrable); |
| 61 | + arbitrable.createDispute{value: 0.04 ether}("Action"); |
| 62 | + |
| 63 | + assertEq(sortitionModule.disputesWithoutJurors(), 1, "Wrong disputesWithoutJurors count"); |
| 64 | + ( |
| 65 | + uint96 courtID, |
| 66 | + IArbitrableV2 arbitrated, |
| 67 | + KlerosCoreBase.Period period, |
| 68 | + bool ruled, |
| 69 | + uint256 lastPeriodChange |
| 70 | + ) = core.disputes(disputeID); |
| 71 | + |
| 72 | + assertEq(courtID, newCourtID, "Wrong court ID"); |
| 73 | + assertEq(address(arbitrated), address(arbitrable), "Wrong arbitrable"); |
| 74 | + assertEq(uint256(period), uint256(KlerosCoreBase.Period.evidence), "Wrong period"); |
| 75 | + assertEq(ruled, false, "Should not be ruled"); |
| 76 | + assertEq(lastPeriodChange, block.timestamp, "Wrong lastPeriodChange"); |
| 77 | + |
| 78 | + KlerosCoreBase.Round memory round = core.getRoundInfo(disputeID, 0); |
| 79 | + assertEq(round.disputeKitID, newDkID, "Wrong DK ID"); |
| 80 | + assertEq(round.pnkAtStakePerJuror, 4000, "Wrong pnkAtStakePerJuror"); // minStake * alpha / divisor = 2000 * 20000/10000 |
| 81 | + assertEq(round.totalFeesForJurors, 0.04 ether, "Wrong totalFeesForJurors"); |
| 82 | + assertEq(round.nbVotes, 4, "Wrong nbVotes"); |
| 83 | + assertEq(round.repartitions, 0, "repartitions should be 0"); |
| 84 | + assertEq(round.pnkPenalties, 0, "pnkPenalties should be 0"); |
| 85 | + assertEq(round.sumFeeRewardPaid, 0, "sumFeeRewardPaid should be 0"); |
| 86 | + assertEq(round.sumPnkRewardPaid, 0, "sumPnkRewardPaid should be 0"); |
| 87 | + assertEq(address(round.feeToken), address(0), "feeToken should be 0"); |
| 88 | + assertEq(round.drawIterations, 0, "drawIterations should be 0"); |
| 89 | + |
| 90 | + (uint256 numberOfChoices, bool jumped, bytes memory extraData) = disputeKit.disputes(disputeID); |
| 91 | + |
| 92 | + assertEq(numberOfChoices, 2, "Wrong numberOfChoices"); |
| 93 | + assertEq(jumped, false, "jumped should be false"); |
| 94 | + assertEq(extraData, newExtraData, "Wrong extra data"); |
| 95 | + assertEq(disputeKit.coreDisputeIDToLocal(0), disputeID, "Wrong local disputeID"); |
| 96 | + assertEq(disputeKit.coreDisputeIDToActive(0), true, "Wrong disputes length"); |
| 97 | + |
| 98 | + ( |
| 99 | + uint256 winningChoice, |
| 100 | + bool tied, |
| 101 | + uint256 totalVoted, |
| 102 | + uint256 totalCommited, |
| 103 | + uint256 nbVoters, |
| 104 | + uint256 choiceCount |
| 105 | + ) = disputeKit.getRoundInfo(0, 0, 0); |
| 106 | + assertEq(winningChoice, 0, "winningChoice should be 0"); |
| 107 | + assertEq(tied, true, "tied should be true"); |
| 108 | + assertEq(totalVoted, 0, "totalVoted should be 0"); |
| 109 | + assertEq(totalCommited, 0, "totalCommited should be 0"); |
| 110 | + assertEq(nbVoters, 0, "nbVoters should be 0"); |
| 111 | + assertEq(choiceCount, 0, "choiceCount should be 0"); |
| 112 | + } |
| 113 | + |
| 114 | + function test_createDispute_tokens() public { |
| 115 | + feeToken.transfer(disputer, 1 ether); |
| 116 | + vm.prank(disputer); |
| 117 | + feeToken.approve(address(arbitrable), 1 ether); |
| 118 | + |
| 119 | + vm.expectRevert(KlerosCoreBase.TokenNotAccepted.selector); |
| 120 | + vm.prank(disputer); |
| 121 | + arbitrable.createDispute("Action", 0.18 ether); |
| 122 | + |
| 123 | + vm.prank(owner); |
| 124 | + core.changeAcceptedFeeTokens(feeToken, true); |
| 125 | + vm.prank(owner); |
| 126 | + core.changeCurrencyRates(feeToken, 500, 3); |
| 127 | + |
| 128 | + vm.expectRevert(KlerosCoreBase.ArbitrationFeesNotEnough.selector); |
| 129 | + vm.prank(disputer); |
| 130 | + arbitrable.createDispute("Action", 0.18 ether - 1); |
| 131 | + |
| 132 | + vm.expectRevert(KlerosCoreBase.TransferFailed.selector); |
| 133 | + vm.prank(address(arbitrable)); // Bypass createDispute in arbitrable to avoid transfer checks there and make the arbitrable call KC directly |
| 134 | + core.createDispute(2, arbitratorExtraData, feeToken, 0.18 ether); |
| 135 | + |
| 136 | + assertEq(core.arbitrationCost(arbitratorExtraData, feeToken), 0.18 ether, "Wrong token cost"); |
| 137 | + vm.prank(disputer); |
| 138 | + arbitrable.createDispute("Action", 0.18 ether); |
| 139 | + |
| 140 | + KlerosCoreBase.Round memory round = core.getRoundInfo(0, 0); |
| 141 | + assertEq(round.totalFeesForJurors, 0.18 ether, "Wrong totalFeesForJurors"); |
| 142 | + assertEq(round.nbVotes, 3, "Wrong nbVotes"); |
| 143 | + assertEq(address(round.feeToken), address(feeToken), "Wrong feeToken"); |
| 144 | + |
| 145 | + assertEq(feeToken.balanceOf(address(core)), 0.18 ether, "Wrong token balance of the core"); |
| 146 | + assertEq(feeToken.balanceOf(disputer), 0.82 ether, "Wrong token balance of the disputer"); |
| 147 | + } |
| 148 | +} |
0 commit comments