Step-by-step guide to building a Kleros V2 arbitrable smart contract, from IArbitratorV2 integration to dispute creation, evidence, and ruling.
This guide is specific to Kleros V2: it uses the IArbitrableV2 and IArbitratorV2 interfaces and KlerosCore on Arbitrum. To build against Kleros V1 (ERC-792 and ERC-1497), see V1 Arbitrable Apps.
Interface Reference
Before building, understand the two interfaces your contract interacts with.
IArbitratorV2 (KlerosCore)
interface IArbitratorV2 { // ---- Dispute Creation ---- /// @dev Create dispute, pay fee in native currency (ETH on Arbitrum). function createDispute( uint256 _numberOfChoices, bytes calldata _extraData ) external payable returns (uint256 disputeID); /// @dev Create dispute, pay fee in an ERC-20 token. function createDispute( uint256 _numberOfChoices, bytes calldata _extraData, IERC20 _feeToken, uint256 _feeAmount ) external returns (uint256 disputeID); // ---- Cost Queries ---- /// @dev Returns arbitration cost in ETH for the given extraData. function arbitrationCost(bytes calldata _extraData) external view returns (uint256 cost); /// @dev Returns arbitration cost in the specified ERC-20 token. function arbitrationCost(bytes calldata _extraData, IERC20 _feeToken) external view returns (uint256 cost); // ---- Ruling ---- /// @dev Returns the current ruling and its status. /// @param _disputeID The dispute to query. /// @return ruling Current winning option (0 = Refuse to Arbitrate / no majority yet). /// @return tied True if two or more options are tied for the most votes. /// A tied dispute will default to ruling 0 unless resolved by appeal. /// @return overridden True if the ruling was changed by the parent court on appeal. function currentRuling(uint256 _disputeID) external view returns (uint256 ruling, bool tied, bool overridden); // ---- Events ---- event DisputeCreation(uint256 indexed _disputeID, IArbitrableV2 indexed _arbitrable); event Ruling(IArbitrableV2 indexed _arbitrable, uint256 indexed _disputeID, uint256 _ruling); event AcceptedFeeToken(IERC20 indexed _token, bool indexed _accepted); event NewCurrencyRate(IERC20 indexed _feeToken, uint64 _rateInEth, uint8 _rateDecimals);}
IArbitrableV2 (your contract)
interface IArbitrableV2 { /// @dev Emitted when your contract creates a dispute. Required by the Kleros Court UI /// to link the dispute to a display template. /// @param _arbitrator Address of KlerosCore. /// @param _arbitratorDisputeID The dispute ID assigned by KlerosCore. /// @param _externalDisputeID Your contract's internal dispute/transaction ID. /// Used in data mappings as {{externalDisputeID}}. /// @param _templateId ID from DisputeTemplateRegistry.setDisputeTemplate(). /// Pass 0 if using templateUri instead. /// @param _templateUri IPFS URI of the dispute template JSON. /// Pass "" if using templateId instead. event DisputeRequest( IArbitratorV2 indexed _arbitrator, uint256 indexed _arbitratorDisputeID, uint256 _externalDisputeID, uint256 _templateId, string _templateUri ); /// @dev Called by the arbitrator when a ruling is final. /// @param _disputeID The arbitrator-assigned dispute ID. /// @param _ruling The winning option. 0 = "Refuse to Arbitrate" - always handle this. function rule(uint256 _disputeID, uint256 _ruling) external; event Ruling(IArbitratorV2 indexed _arbitrator, uint256 indexed _disputeID, uint256 _ruling);}
extraData Encoding
extraData configures the court and juror count:
// V2 uses uint96 for courtID (not uint256 as in V1 - this is a breaking change)bytes memory extraData = abi.encodePacked( uint96(1), // courtID - 1 = General Court uint256(3) // minJurors - always use odd numbers to avoid ties);
Bytes
Type
Field
Notes
0–11
uint96
courtID
Must be uint96, not uint256. Using uint256 encodes a wrong value.
12–43
uint256
minJurors
Use 3, 5, or 7. Even numbers risk tied votes (see below).
Tied votes: When votes are evenly split, currentRuling() returns tied = true and ruling = 0. The dispute remains in appeal until resolved. If never appealed after the period ends, ruling 0 (Refuse to Arbitrate) is executed. Always handle ruling 0 explicitly in your contract.
ERC-20 Fee Payment
On Arbitrum One, KlerosCore accepts ETH and accepted ERC-20 tokens (e.g. WETH):
ERC-20 fees only work when calling KlerosCore directly on Arbitrum. The ForeignGateway (for cross-chain disputes from Ethereum or Gnosis) only accepts native ETH.
To check accepted tokens, listen for the AcceptedFeeToken(token, accepted) event on KlerosCore.
Building an Escrow Contract
We’ll build a complete escrow that uses Kleros arbitration. This covers all the patterns you’ll need.
Contract Structure
// SPDX-License-Identifier: MITpragma solidity ^0.8.18;import "@kleros/kleros-v2-contracts/arbitration/interfaces/IArbitrableV2.sol";import "@kleros/kleros-v2-contracts/arbitration/interfaces/IArbitratorV2.sol";import "@kleros/kleros-v2-contracts/evidence/interfaces/IEvidence.sol";contract Escrow is IArbitrableV2, IEvidence { IArbitratorV2 public immutable arbitrator; bytes public arbitratorExtraData; string public templateUri; enum Status { Created, Disputed, Resolved } enum Ruling { RefusedToRule, PaySeller, RefundBuyer } struct Transaction { address buyer; address seller; uint256 amount; Status status; uint256 disputeID; } mapping(uint256 => Transaction) public transactions; mapping(uint256 => uint256) public disputeToTx; // arbitrator disputeID → txID uint256 public txCount; constructor( IArbitratorV2 _arbitrator, bytes memory _extraData, string memory _templateUri ) { arbitrator = _arbitrator; arbitratorExtraData = _extraData; templateUri = _templateUri; }}
{ "name": "Delivery Receipt", "description": "Screenshot showing package was delivered on Jan 15", "fileURI": "/ipfs/QmWQV5ZFFhEJiW8Lm7ay2zLxC2XS4wx1b2W7FfdrLMyQQc"}
Evidence is immutable once submitted. The fileURI should point to IPFS/Arweave for permanence.
Handling Appeals
Appeals are managed by the Dispute Kit, not your contract. However, you can track appeal status: