xKlerosLiquidV2

Git Source

Inherits: Initializable, ITokenController, IArbitrator

This contract is an adaption of Mainnet's KlerosLiquid (https://github.com/kleros/kleros/blob/69cfbfb2128c29f1625b3a99a3183540772fda08/contracts/kleros/KlerosLiquid.sol) for xDai chain. Notice that variables referring to ETH values in this contract, will hold the native token values of the chain on which xKlerosLiquid is deployed. When this contract gets deployed on xDai chain, ETH variables will hold xDai values.

State Variables

MAX_STAKE_PATHS

uint256 public constant MAX_STAKE_PATHS = 4;

DEFAULT_NB_OF_JURORS

uint256 public constant DEFAULT_NB_OF_JURORS = 3;

NON_PAYABLE_AMOUNT

uint256 public constant NON_PAYABLE_AMOUNT = (2 ** 256 - 2) / 2;

ALPHA_DIVISOR

uint256 public constant ALPHA_DIVISOR = 1e4;

governor

address public governor;

pinakion

WrappedPinakion public pinakion;

RNGenerator

IRandomAuRa public RNGenerator;

phase

Phase public phase;

lastPhaseChange

uint256 public lastPhaseChange;

disputesWithoutJurors

uint256 public disputesWithoutJurors;

RNBlock

uint256 public RNBlock;

RN

uint256 public RN;

minStakingTime

uint256 public minStakingTime;

maxDrawingTime

uint256 public maxDrawingTime;

lockInsolventTransfers

bool public lockInsolventTransfers;

courts

Court[] public courts;

sortitionSumTrees

SortitionSumTreeFactory.SortitionSumTrees internal sortitionSumTrees;

delayedSetStakes

mapping(uint256 => DelayedSetStake) public delayedSetStakes;

nextDelayedSetStake

uint256 public nextDelayedSetStake;

lastDelayedSetStake

uint256 public lastDelayedSetStake;

disputes

mapping(uint256 => Dispute) public disputes;

totalDisputes

uint256 public totalDisputes;

jurors

mapping(address => Juror) public jurors;

foreignGateway

IForeignGateway public foreignGateway;

weth

IERC20 public weth;

disputesRuling

mapping(uint256 => uint256) public disputesRuling;

Functions

onlyDuringPhase

Requires a specific phase.

modifier onlyDuringPhase(Phase _phase);

Parameters

NameTypeDescription
_phasePhaseThe required phase.

onlyDuringPeriod

Requires a specific period in a dispute.

modifier onlyDuringPeriod(uint256 _disputeID, Period _period);

Parameters

NameTypeDescription
_disputeIDuint256The ID of the dispute.
_periodPeriodThe required period.

onlyByGovernor

Requires that the sender is the governor. Note that the governor is expected to not be malicious.

modifier onlyByGovernor();

initialize

Constructs the KlerosLiquid contract.

function initialize(
    address _governor,
    WrappedPinakion _pinakion,
    IRandomAuRa _RNGenerator,
    uint256 _minStakingTime,
    uint256 _maxDrawingTime,
    bool _hiddenVotes,
    uint256[4] memory _courtParameters,
    uint256[4] memory _timesPerPeriod,
    uint256 _sortitionSumTreeK,
    IForeignGateway _foreignGateway,
    IERC20 _weth
) public initializer;

Parameters

NameTypeDescription
_governoraddressThe governor's address.
_pinakionWrappedPinakionThe address of the token contract.
_RNGeneratorIRandomAuRaThe address of the random number generator contract.
_minStakingTimeuint256The minimum time that the staking phase should last.
_maxDrawingTimeuint256The maximum time that the drawing phase should last.
_hiddenVotesboolThe hiddenVotes property value of the general court.
_courtParametersuint256[4]MinStake, alpha, feeForJuror and jurorsForCourtJump respectively.
_timesPerPerioduint256[4]The timesPerPeriod property value of the general court.
_sortitionSumTreeKuint256The number of children per node of the general court's sortition sum tree.
_foreignGatewayIForeignGatewayForeign gateway on xDai.
_wethIERC20Weth contract.

executeGovernorProposal

Lets the governor call anything on behalf of the contract.

function executeGovernorProposal(address _destination, uint256 _amount, bytes memory _data) external onlyByGovernor;

Parameters

NameTypeDescription
_destinationaddressThe destination of the call.
_amountuint256The value sent with the call.
_databytesThe data sent with the call.

changeGovernor

Changes the governor storage variable.

function changeGovernor(address _governor) external onlyByGovernor;

Parameters

NameTypeDescription
_governoraddressThe new value for the governor storage variable.

changePinakion

Changes the pinakion storage variable.

function changePinakion(WrappedPinakion _pinakion) external onlyByGovernor;

Parameters

NameTypeDescription
_pinakionWrappedPinakionThe new value for the pinakion storage variable.

changeRNGenerator

Changes the RNGenerator storage variable.

function changeRNGenerator(IRandomAuRa _RNGenerator) external onlyByGovernor;

Parameters

NameTypeDescription
_RNGeneratorIRandomAuRaThe new value for the RNGenerator storage variable.

changeMinStakingTime

Changes the minStakingTime storage variable.

function changeMinStakingTime(uint256 _minStakingTime) external onlyByGovernor;

Parameters

NameTypeDescription
_minStakingTimeuint256The new value for the minStakingTime storage variable.

changeMaxDrawingTime

Changes the maxDrawingTime storage variable.

function changeMaxDrawingTime(uint256 _maxDrawingTime) external onlyByGovernor;

Parameters

NameTypeDescription
_maxDrawingTimeuint256The new value for the maxDrawingTime storage variable.

changeForeignGateway

Changes the foreignGateway storage variable.

function changeForeignGateway(IForeignGateway _foreignGateway) external onlyByGovernor;

Parameters

NameTypeDescription
_foreignGatewayIForeignGatewayThe new value for the foreignGateway storage variable.

changeWethAddress

Changes the weth storage variable.

function changeWethAddress(IERC20 _weth) external onlyByGovernor;

Parameters

NameTypeDescription
_wethIERC20The new value for the weth storage variable.

createSubcourt

Creates a subcourt under a specified parent court.

function createSubcourt(
    uint96 _parent,
    bool _hiddenVotes,
    uint256 _minStake,
    uint256 _alpha,
    uint256 _feeForJuror,
    uint256 _jurorsForCourtJump,
    uint256[4] memory _timesPerPeriod,
    uint256 _sortitionSumTreeK
) external onlyByGovernor;

Parameters

NameTypeDescription
_parentuint96The parent property value of the subcourt.
_hiddenVotesboolThe hiddenVotes property value of the subcourt.
_minStakeuint256The minStake property value of the subcourt.
_alphauint256The alpha property value of the subcourt.
_feeForJuroruint256The feeForJuror property value of the subcourt.
_jurorsForCourtJumpuint256The jurorsForCourtJump property value of the subcourt.
_timesPerPerioduint256[4]The timesPerPeriod property value of the subcourt.
_sortitionSumTreeKuint256The number of children per node of the subcourt's sortition sum tree.

changeSubcourtMinStake

Changes the minStake property value of a specified subcourt. Don't set to a value lower than its parent's minStake property value.

function changeSubcourtMinStake(uint96 _subcourtID, uint256 _minStake) external onlyByGovernor;

Parameters

NameTypeDescription
_subcourtIDuint96The ID of the subcourt.
_minStakeuint256The new value for the minStake property value.

changeSubcourtAlpha

Changes the alpha property value of a specified subcourt.

function changeSubcourtAlpha(uint96 _subcourtID, uint256 _alpha) external onlyByGovernor;

Parameters

NameTypeDescription
_subcourtIDuint96The ID of the subcourt.
_alphauint256The new value for the alpha property value.

changeSubcourtJurorFee

Changes the feeForJuror property value of a specified subcourt.

function changeSubcourtJurorFee(uint96 _subcourtID, uint256 _feeForJuror) external onlyByGovernor;

Parameters

NameTypeDescription
_subcourtIDuint96The ID of the subcourt.
_feeForJuroruint256The new value for the feeForJuror property value.

changeSubcourtJurorsForJump

Changes the jurorsForCourtJump property value of a specified subcourt.

function changeSubcourtJurorsForJump(uint96 _subcourtID, uint256 _jurorsForCourtJump) external onlyByGovernor;

Parameters

NameTypeDescription
_subcourtIDuint96The ID of the subcourt.
_jurorsForCourtJumpuint256The new value for the jurorsForCourtJump property value.

changeSubcourtTimesPerPeriod

Changes the timesPerPeriod property value of a specified subcourt.

function changeSubcourtTimesPerPeriod(uint96 _subcourtID, uint256[4] memory _timesPerPeriod) external onlyByGovernor;

Parameters

NameTypeDescription
_subcourtIDuint96The ID of the subcourt.
_timesPerPerioduint256[4]The new value for the timesPerPeriod property value.

setStake

Sets the caller's stake in a subcourt.

function setStake(uint96 _subcourtID, uint128 _stake) external;

Parameters

NameTypeDescription
_subcourtIDuint96The ID of the subcourt.
_stakeuint128The new stake.

executeDelayedSetStakes

Executes the next delayed set stakes. O(n) where n is the number of iterations to run.

function executeDelayedSetStakes(uint256 _iterations) external onlyDuringPhase(Phase.staking);

Parameters

NameTypeDescription
_iterationsuint256The number of delayed set stakes to execute.

rule

Receive the ruling from foreign gateway which technically is an arbitrator of this contract.

function rule(uint256 _disputeID, uint256 _ruling) external;

Parameters

NameTypeDescription
_disputeIDuint256ID of the dispute.
_rulinguint256Ruling given by V2 court and relayed by foreign gateway.

createDispute

Public

Creates a dispute. Must be called by the arbitrable contract.

function createDispute(uint256 _numberOfChoices, bytes memory _extraData)
    public
    payable
    override
    returns (uint256 disputeID);

Parameters

NameTypeDescription
_numberOfChoicesuint256Number of choices to choose from in the dispute to be created.
_extraDatabytesAdditional info about the dispute to be created. We use it to pass the ID of the subcourt to create the dispute in (first 32 bytes) and the minimum number of jurors required (next 32 bytes).

Returns

NameTypeDescription
disputeIDuint256The ID of the created dispute.

proxyPayment

DEPRECATED. Called when _owner sends ETH to the Wrapped Token contract.

function proxyPayment(address _owner) public payable override returns (bool allowed);

Parameters

NameTypeDescription
_owneraddressThe address that sent the ETH to create tokens.

Returns

NameTypeDescription
allowedboolWhether the operation should be allowed or not.

onTransfer

Notifies the controller about a token transfer allowing the controller to react if desired.

function onTransfer(address _from, address _to, uint256 _amount) public override returns (bool allowed);

Parameters

NameTypeDescription
_fromaddressThe origin of the transfer.
_toaddressThe destination of the transfer.
_amountuint256The amount of the transfer.

Returns

NameTypeDescription
allowedboolWhether the operation should be allowed or not.

onApprove

Notifies the controller about an approval allowing the controller to react if desired.

function onApprove(address _owner, address _spender, uint256 _amount) public override returns (bool allowed);

Parameters

NameTypeDescription
_owneraddressThe address that calls approve().
_spenderaddressThe spender in the approve() call.
_amountuint256The amount in the approve() call.

Returns

NameTypeDescription
allowedboolWhether the operation should be allowed or not.

_setStake

Sets the specified juror's stake in a subcourt. O(n + p * log_k(j)) where n is the number of subcourts the juror has staked in, p is the depth of the subcourt tree, k is the minimum number of children per node of one of these subcourts' sortition sum tree, and j is the maximum number of jurors that ever staked in one of these subcourts simultaneously.

function _setStake(address _account, uint96 _subcourtID, uint128 _stake) internal returns (bool succeeded);

Parameters

NameTypeDescription
_accountaddressThe address of the juror.
_subcourtIDuint96The ID of the subcourt.
_stakeuint128The new stake.

Returns

NameTypeDescription
succeededboolTrue if the call succeeded, false otherwise.

extraDataToSubcourtIDAndMinJurors

Gets a subcourt ID and the minimum number of jurors required from a specified extra data bytes array.

function extraDataToSubcourtIDAndMinJurors(bytes memory _extraData)
    internal
    view
    returns (uint96 subcourtID, uint256 minJurors);

Parameters

NameTypeDescription
_extraDatabytesThe extra data bytes array. The first 32 bytes are the subcourt ID and the next 32 bytes are the minimum number of jurors.

Returns

NameTypeDescription
subcourtIDuint96The subcourt ID.
minJurorsuint256The minimum number of jurors required.

accountAndSubcourtIDToStakePathID

Packs an account and a subcourt ID into a stake path ID.

function accountAndSubcourtIDToStakePathID(address _account, uint96 _subcourtID)
    internal
    pure
    returns (bytes32 stakePathID);

Parameters

NameTypeDescription
_accountaddressThe account to pack.
_subcourtIDuint96The subcourt ID to pack.

Returns

NameTypeDescription
stakePathIDbytes32The stake path ID.

arbitrationCost

Gets the cost of arbitration in a specified subcourt.

function arbitrationCost(bytes memory _extraData) public view override returns (uint256 cost);

Parameters

NameTypeDescription
_extraDatabytesAdditional info about the dispute. We use it to pass the ID of the subcourt to create the dispute in (first 32 bytes) and the minimum number of jurors required (next 32 bytes).

Returns

NameTypeDescription
costuint256The cost.

currentRuling

Gets the current ruling of a specified dispute.

function currentRuling(uint256 _disputeID) public view returns (uint256 ruling);

Parameters

NameTypeDescription
_disputeIDuint256The ID of the dispute.

Returns

NameTypeDescription
rulinguint256The current ruling.

getSubcourt

Gets a specified subcourt's non primitive properties.

function getSubcourt(uint96 _subcourtID)
    external
    view
    returns (uint256[] memory children, uint256[4] memory timesPerPeriod);

Parameters

NameTypeDescription
_subcourtIDuint96The ID of the subcourt.

Returns

NameTypeDescription
childrenuint256[]The subcourt's child court list.
timesPerPerioduint256[4]The subcourt's time per period.

getVote

Gets a specified vote for a specified appeal in a specified dispute.

function getVote(uint256 _disputeID, uint256 _appeal, uint256 _voteID)
    external
    view
    returns (address account, bytes32 commit, uint256 choice, bool voted);

Parameters

NameTypeDescription
_disputeIDuint256The ID of the dispute.
_appealuint256The appeal.
_voteIDuint256The ID of the vote.

Returns

NameTypeDescription
accountaddressThe account for vote.
commitbytes32The commit for vote.
choiceuint256The choice for vote.
votedboolTrue if the account voted, False otherwise.

getVoteCounter

Gets the vote counter for a specified appeal in a specified dispute. Note: This function is only to be used by the interface and it won't work if the number of choices is too high.

function getVoteCounter(uint256 _disputeID, uint256 _appeal)
    external
    view
    returns (uint256 winningChoice, uint256[] memory counts, bool tied);

Parameters

NameTypeDescription
_disputeIDuint256The ID of the dispute.
_appealuint256The appeal.

Returns

NameTypeDescription
winningChoiceuint256The winning choice.
countsuint256[]The count.
tiedboolWhether the vote tied. O(n) where n is the number of choices of the dispute.

getDispute

Gets a specified dispute's non primitive properties.

function getDispute(uint256 _disputeID)
    external
    view
    returns (
        uint256[] memory votesLengths,
        uint256[] memory tokensAtStakePerJuror,
        uint256[] memory totalFeesForJurors,
        uint256[] memory votesInEachRound,
        uint256[] memory repartitionsInEachRound,
        uint256[] memory penaltiesInEachRound
    );

Parameters

NameTypeDescription
_disputeIDuint256The ID of the dispute.

Returns

NameTypeDescription
votesLengthsuint256[]The dispute's vote length.
tokensAtStakePerJuroruint256[]The dispute's required tokens at stake per Juror.
totalFeesForJurorsuint256[]The dispute's total fees for Jurors.
votesInEachRounduint256[]The dispute's counter of votes made in each round.
repartitionsInEachRounduint256[]The dispute's counter of vote reward repartitions made in each round.
penaltiesInEachRounduint256[]The dispute's amount of tokens collected from penalties in each round. O(a) where a is the number of appeals of the dispute.

getJuror

Gets a specified juror's non primitive properties.

function getJuror(address _account) external view returns (uint96[] memory subcourtIDs);

Parameters

NameTypeDescription
_accountaddressThe address of the juror.

Returns

NameTypeDescription
subcourtIDsuint96[]The juror's IDs of subcourts where the juror has stake path.

stakeOf

Gets the stake of a specified juror in a specified subcourt.

function stakeOf(address _account, uint96 _subcourtID) external view returns (uint256 stake);

Parameters

NameTypeDescription
_accountaddressThe address of the juror.
_subcourtIDuint96The ID of the subcourt.

Returns

NameTypeDescription
stakeuint256The stake.

Events

NewPhase

Emitted when we pass to a new phase.

event NewPhase(Phase _phase);

NewPeriod

Emitted when a dispute passes to a new period.

event NewPeriod(uint256 indexed _disputeID, Period _period);

StakeSet

Emitted when a juror's stake is set.

event StakeSet(address indexed _address, uint256 _subcourtID, uint128 _stake, uint256 _newTotalStake);

Draw

Emitted when a juror is drawn.

event Draw(address indexed _address, uint256 indexed _disputeID, uint256 _appeal, uint256 _voteID);

TokenAndETHShift

Emitted when a juror wins or loses tokens and ETH from a dispute.

event TokenAndETHShift(address indexed _address, uint256 indexed _disputeID, int256 _tokenAmount, int256 _ETHAmount);

Structs

Court

struct Court {
    uint96 parent;
    uint256[] children;
    bool hiddenVotes;
    uint256 minStake;
    uint256 alpha;
    uint256 feeForJuror;
    uint256 jurorsForCourtJump;
    uint256[4] timesPerPeriod;
}

DelayedSetStake

struct DelayedSetStake {
    address account;
    uint96 subcourtID;
    uint128 stake;
}

Vote

struct Vote {
    address account;
    bytes32 commit;
    uint256 choice;
    bool voted;
}

VoteCounter

struct VoteCounter {
    uint256 winningChoice;
    mapping(uint256 => uint256) counts;
    bool tied;
}

Dispute

struct Dispute {
    uint96 subcourtID;
    IArbitrable arbitrated;
    uint256 numberOfChoices;
    Period period;
    uint256 lastPeriodChange;
    Vote[][] votes;
    VoteCounter[] voteCounters;
    uint256[] tokensAtStakePerJuror;
    uint256[] totalFeesForJurors;
    uint256 drawsInRound;
    uint256 commitsInRound;
    uint256[] votesInEachRound;
    uint256[] repartitionsInEachRound;
    uint256[] penaltiesInEachRound;
    bool ruled;
}

Juror

struct Juror {
    uint96[] subcourtIDs;
    uint256 stakedTokens;
    uint256 lockedTokens;
}

Enums

Phase

enum Phase {
    staking,
    generating,
    drawing
}

Period

enum Period {
    evidence,
    commit,
    vote,
    appeal,
    execution
}