Skip to content

Commit ee28edd

Browse files
committed
feat(arbitrable): get ruling
1 parent ba9368b commit ee28edd

File tree

5 files changed

+155
-14
lines changed

5 files changed

+155
-14
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"babel-preset-stage-2": "^6.24.1",
5252
"coveralls": "^3.0.0",
5353
"cross-env": "^5.1.4",
54-
"esdoc": "^1.0.4",
54+
"esdoc": "^1.1.0",
5555
"esdoc-ecmascript-proposal-plugin": "^1.0.0",
5656
"esdoc-standard-plugin": "^1.0.0",
5757
"ganache-cli": "^6.1.8",

src/standards/Arbitrable.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,47 @@ class Arbitrable extends StandardContract {
147147
fileValid
148148
}
149149
}
150+
151+
/**
152+
* Fetch the ruling for a dispute.
153+
* @param {string} contractAddress - The address of the arbitrable contract.
154+
* @param {string} arbitratorAddress - The address of the arbitrator contract.
155+
* @param {number} disputeID - The index of the dispute.
156+
* @param {object} options - Optional parameters. Includes fromBlock and toBlock.
157+
* @returns {number} The number denoting the ruling.
158+
*/
159+
getRuling = async (
160+
contractAddress,
161+
arbitratorAddress,
162+
disputeID,
163+
options = {}
164+
) => {
165+
const contractInstance = this._loadContractInstance(contractAddress)
166+
167+
const rulingLog = await EventListener.getEventLogs(
168+
contractInstance,
169+
'Ruling',
170+
options.fromBlock || 0,
171+
options.toBlock || 'latest',
172+
{
173+
_arbitrator: arbitratorAddress,
174+
_disputeID: disputeID,
175+
...options.filters
176+
}
177+
)
178+
179+
if (rulingLog.length === 0) return null
180+
else if (rulingLog.length > 1)
181+
throw new Error(
182+
errorConstants.CONTRACT_ERROR(
183+
`There is more than one ruling for dispute ${disputeID} in arbitrator ${arbitratorAddress}`
184+
)
185+
)
186+
187+
const args = await rulingLog[0].returnValues
188+
189+
return args._ruling
190+
}
150191
}
151192

152193
export default Arbitrable

tests/contracts/TestArbitrable.sol

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
11
pragma solidity ^0.4.15;
22

33
contract TestArbitrable {
4-
4+
/* EVENTS */
55
event MetaEvidence(uint indexed _metaEvidenceID, string _evidence);
66

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

14-
/** @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).
15-
* @param _arbitrator The arbitrator of the contract.
16-
* @param _disputeID ID of the dispute in the Arbitrator contract.
17-
* @param _party The address of the party submiting the evidence. Note that 0x0 refers to evidence not submitted by any party.
18-
* @param _evidence A URI to the evidence JSON file whose name should be its keccak256 hash followed by .json.
19-
*/
209
event Evidence(address indexed _arbitrator, uint indexed _disputeID, address _party, string _evidence);
2110

11+
event Ruling(address indexed _arbitrator, uint indexed _disputeID, uint _ruling);
12+
13+
/* EVENT EMITTERS */
2214
function emitMetaEvidence(uint _metaEvidenceID, string _evidence) public {
2315
emit MetaEvidence(_metaEvidenceID, _evidence);
2416
}
2517

2618
function emitEvidence(address _arbitrator, uint _disputeID, address _party, string _evidence) public {
2719
emit Evidence(_arbitrator, _disputeID, _party, _evidence);
2820
}
21+
22+
function emitRuling(address _arbitrator, uint _disputeID, uint _ruling) public {
23+
emit Ruling(_arbitrator, _disputeID, _ruling);
24+
}
2925
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import Web3 from 'web3'
2+
3+
import { _deplyTestArbitrableContract } from '../../utils.js'
4+
import Arbitrable from '../../../src/standards/Arbitrable'
5+
6+
const provider = new Web3.providers.HttpProvider('http://localhost:8545')
7+
8+
/**
9+
* Deploy a basic Arbitrable contract
10+
* @param {string[]} arguments - The argument array for the Arbitrable Contract
11+
* @example
12+
* ["0x211f01e59b425253c0a0e9a7bf612605b42ce82c", "0x0"] // [arbitratorAddress, extraData]
13+
* @returns {object} web3 contract object
14+
*/
15+
16+
describe('Ruling', () => {
17+
let web3
18+
let arbitrableInstance
19+
let accounts
20+
21+
beforeAll(async () => {
22+
web3 = new Web3(provider)
23+
accounts = await web3.eth.getAccounts()
24+
arbitrableInstance = new Arbitrable(provider)
25+
})
26+
27+
it('get ruling on dispute', async () => {
28+
// deploy arbitrable contract to test with
29+
const arbitrableContract = await _deplyTestArbitrableContract(provider)
30+
expect(arbitrableContract.options.address).toBeTruthy()
31+
32+
const arbitratorAddress = '0x0000000000000000000000000000000000000000'
33+
const disputeID = 0
34+
const ruling = 1
35+
// emit evidence with evidence = fakeURI
36+
let receipt = await arbitrableContract.methods
37+
.emitRuling(arbitratorAddress, disputeID, ruling)
38+
.send({
39+
from: accounts[0],
40+
gas: 500000
41+
})
42+
expect(receipt.transactionHash).toBeTruthy()
43+
44+
const _ruling = await arbitrableInstance.getRuling(
45+
arbitrableContract.options.address,
46+
arbitratorAddress,
47+
disputeID
48+
)
49+
expect(_ruling).toEqual(`${ruling}`)
50+
})
51+
it('get ruling on dispute -- no ruling yet', async () => {
52+
// deploy arbitrable contract to test with
53+
const arbitrableContract = await _deplyTestArbitrableContract(provider)
54+
expect(arbitrableContract.options.address).toBeTruthy()
55+
56+
const arbitratorAddress = '0x0000000000000000000000000000000000000000'
57+
const disputeID = 0
58+
59+
const _ruling = await arbitrableInstance.getRuling(
60+
arbitrableContract.options.address,
61+
arbitratorAddress,
62+
disputeID
63+
)
64+
expect(_ruling).toEqual(null)
65+
})
66+
it('get ruling on dispute -- multiple rulings same dispute', async () => {
67+
// deploy arbitrable contract to test with
68+
const arbitrableContract = await _deplyTestArbitrableContract(provider)
69+
expect(arbitrableContract.options.address).toBeTruthy()
70+
71+
const arbitratorAddress = '0x0000000000000000000000000000000000000000'
72+
const disputeID = 0
73+
const ruling = 1
74+
// emit evidence with evidence = fakeURI
75+
let receipt = await arbitrableContract.methods
76+
.emitRuling(arbitratorAddress, disputeID, ruling)
77+
.send({
78+
from: accounts[0],
79+
gas: 500000
80+
})
81+
expect(receipt.transactionHash).toBeTruthy()
82+
// emit evidence with evidence = fakeURI
83+
receipt = await arbitrableContract.methods
84+
.emitRuling(arbitratorAddress, disputeID, ruling + 1)
85+
.send({
86+
from: accounts[0],
87+
gas: 500000
88+
})
89+
expect(receipt.transactionHash).toBeTruthy()
90+
91+
let errored = false
92+
try {
93+
const _ruling = await arbitrableInstance.getRuling(
94+
arbitrableContract.options.address,
95+
arbitratorAddress,
96+
disputeID
97+
)
98+
} catch (err) {
99+
expect(err).toBeTruthy()
100+
errored = true
101+
}
102+
expect(errored).toBeTruthy()
103+
})
104+
})

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3399,7 +3399,7 @@ esdoc-unexported-identifier-plugin@^1.0.0:
33993399
version "1.0.0"
34003400
resolved "https://registry.yarnpkg.com/esdoc-unexported-identifier-plugin/-/esdoc-unexported-identifier-plugin-1.0.0.tgz#1f9874c6a7c2bebf9ad397c3ceb75c9c69dabab1"
34013401

3402-
esdoc@^1.0.4:
3402+
esdoc@^1.1.0:
34033403
version "1.1.0"
34043404
resolved "https://registry.yarnpkg.com/esdoc/-/esdoc-1.1.0.tgz#07d40ebf791764cd537929c29111e20a857624f3"
34053405
dependencies:

0 commit comments

Comments
 (0)