Skip to content

Commit

Permalink
feat(arbitrable): get ruling
Browse files Browse the repository at this point in the history
  • Loading branch information
satello committed Sep 28, 2018
1 parent ba9368b commit ee28edd
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"babel-preset-stage-2": "^6.24.1",
"coveralls": "^3.0.0",
"cross-env": "^5.1.4",
"esdoc": "^1.0.4",
"esdoc": "^1.1.0",
"esdoc-ecmascript-proposal-plugin": "^1.0.0",
"esdoc-standard-plugin": "^1.0.0",
"ganache-cli": "^6.1.8",
Expand Down
41 changes: 41 additions & 0 deletions src/standards/Arbitrable.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,47 @@ class Arbitrable extends StandardContract {
fileValid
}
}

/**
* Fetch the ruling for a dispute.
* @param {string} contractAddress - The address of the arbitrable contract.
* @param {string} arbitratorAddress - The address of the arbitrator contract.
* @param {number} disputeID - The index of the dispute.
* @param {object} options - Optional parameters. Includes fromBlock and toBlock.
* @returns {number} The number denoting the ruling.
*/
getRuling = async (
contractAddress,
arbitratorAddress,
disputeID,
options = {}
) => {
const contractInstance = this._loadContractInstance(contractAddress)

const rulingLog = await EventListener.getEventLogs(
contractInstance,
'Ruling',
options.fromBlock || 0,
options.toBlock || 'latest',
{
_arbitrator: arbitratorAddress,
_disputeID: disputeID,
...options.filters
}
)

if (rulingLog.length === 0) return null
else if (rulingLog.length > 1)
throw new Error(
errorConstants.CONTRACT_ERROR(
`There is more than one ruling for dispute ${disputeID} in arbitrator ${arbitratorAddress}`
)
)

const args = await rulingLog[0].returnValues

return args._ruling
}
}

export default Arbitrable
20 changes: 8 additions & 12 deletions tests/contracts/TestArbitrable.sol
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
pragma solidity ^0.4.15;

contract TestArbitrable {

/* EVENTS */
event MetaEvidence(uint indexed _metaEvidenceID, string _evidence);

/** @dev To be emmited when a dispute is created to link the correct meta-evidence to the disputeID
* @param _arbitrator The arbitrator of the contract.
* @param _disputeID ID of the dispute in the Arbitrator contract.
* @param _metaEvidenceID Unique identifier of meta-evidence.
*/
event Dispute(address indexed _arbitrator, uint indexed _disputeID, uint _metaEvidenceID);

/** @dev To be raised when evidence are submitted. Should point to the ressource (evidences are not to be stored on chain due to gas considerations).
* @param _arbitrator The arbitrator of the contract.
* @param _disputeID ID of the dispute in the Arbitrator contract.
* @param _party The address of the party submiting the evidence. Note that 0x0 refers to evidence not submitted by any party.
* @param _evidence A URI to the evidence JSON file whose name should be its keccak256 hash followed by .json.
*/
event Evidence(address indexed _arbitrator, uint indexed _disputeID, address _party, string _evidence);

event Ruling(address indexed _arbitrator, uint indexed _disputeID, uint _ruling);

/* EVENT EMITTERS */
function emitMetaEvidence(uint _metaEvidenceID, string _evidence) public {
emit MetaEvidence(_metaEvidenceID, _evidence);
}

function emitEvidence(address _arbitrator, uint _disputeID, address _party, string _evidence) public {
emit Evidence(_arbitrator, _disputeID, _party, _evidence);
}

function emitRuling(address _arbitrator, uint _disputeID, uint _ruling) public {
emit Ruling(_arbitrator, _disputeID, _ruling);
}
}
104 changes: 104 additions & 0 deletions tests/standards/arbitrable/Ruling.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import Web3 from 'web3'

import { _deplyTestArbitrableContract } from '../../utils.js'
import Arbitrable from '../../../src/standards/Arbitrable'

const provider = new Web3.providers.HttpProvider('http://localhost:8545')

/**
* Deploy a basic Arbitrable contract
* @param {string[]} arguments - The argument array for the Arbitrable Contract
* @example
* ["0x211f01e59b425253c0a0e9a7bf612605b42ce82c", "0x0"] // [arbitratorAddress, extraData]
* @returns {object} web3 contract object
*/

describe('Ruling', () => {
let web3
let arbitrableInstance
let accounts

beforeAll(async () => {
web3 = new Web3(provider)
accounts = await web3.eth.getAccounts()
arbitrableInstance = new Arbitrable(provider)
})

it('get ruling on dispute', async () => {
// deploy arbitrable contract to test with
const arbitrableContract = await _deplyTestArbitrableContract(provider)
expect(arbitrableContract.options.address).toBeTruthy()

const arbitratorAddress = '0x0000000000000000000000000000000000000000'
const disputeID = 0
const ruling = 1
// emit evidence with evidence = fakeURI
let receipt = await arbitrableContract.methods
.emitRuling(arbitratorAddress, disputeID, ruling)
.send({
from: accounts[0],
gas: 500000
})
expect(receipt.transactionHash).toBeTruthy()

const _ruling = await arbitrableInstance.getRuling(
arbitrableContract.options.address,
arbitratorAddress,
disputeID
)
expect(_ruling).toEqual(`${ruling}`)
})
it('get ruling on dispute -- no ruling yet', async () => {
// deploy arbitrable contract to test with
const arbitrableContract = await _deplyTestArbitrableContract(provider)
expect(arbitrableContract.options.address).toBeTruthy()

const arbitratorAddress = '0x0000000000000000000000000000000000000000'
const disputeID = 0

const _ruling = await arbitrableInstance.getRuling(
arbitrableContract.options.address,
arbitratorAddress,
disputeID
)
expect(_ruling).toEqual(null)
})
it('get ruling on dispute -- multiple rulings same dispute', async () => {
// deploy arbitrable contract to test with
const arbitrableContract = await _deplyTestArbitrableContract(provider)
expect(arbitrableContract.options.address).toBeTruthy()

const arbitratorAddress = '0x0000000000000000000000000000000000000000'
const disputeID = 0
const ruling = 1
// emit evidence with evidence = fakeURI
let receipt = await arbitrableContract.methods
.emitRuling(arbitratorAddress, disputeID, ruling)
.send({
from: accounts[0],
gas: 500000
})
expect(receipt.transactionHash).toBeTruthy()
// emit evidence with evidence = fakeURI
receipt = await arbitrableContract.methods
.emitRuling(arbitratorAddress, disputeID, ruling + 1)
.send({
from: accounts[0],
gas: 500000
})
expect(receipt.transactionHash).toBeTruthy()

let errored = false
try {
const _ruling = await arbitrableInstance.getRuling(
arbitrableContract.options.address,
arbitratorAddress,
disputeID
)
} catch (err) {
expect(err).toBeTruthy()
errored = true
}
expect(errored).toBeTruthy()
})
})
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3399,7 +3399,7 @@ esdoc-unexported-identifier-plugin@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/esdoc-unexported-identifier-plugin/-/esdoc-unexported-identifier-plugin-1.0.0.tgz#1f9874c6a7c2bebf9ad397c3ceb75c9c69dabab1"

esdoc@^1.0.4:
esdoc@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/esdoc/-/esdoc-1.1.0.tgz#07d40ebf791764cd537929c29111e20a857624f3"
dependencies:
Expand Down

0 comments on commit ee28edd

Please sign in to comment.