Skip to content
Permalink
Browse files

feat(disputes): all functions working with new structure

  • Loading branch information...
satello committed Mar 2, 2018
1 parent 2382e39 commit 39e9b2c541dd0d7ea289efcdfe557b4c4161b26c
@@ -90,7 +90,7 @@ class Disputes extends AbstractWrapper {
const dispute = userProfile.disputes[disputeIndex]
await this._StoreProvider.updateDisputeProfile(
address,
dispute.votes,
dispute.appealDraws,
dispute.arbitratorAddress,
dispute.disputeId,
(dispute.netPNK ? dispute.netPNK : 0) + amountShift
@@ -137,27 +137,31 @@ class Disputes extends AbstractWrapper {
while (1) {
// iterate over all disputes (FIXME inefficient)
try {
dispute = await this._Arbitrator.getDispute(
contractAddress,
disputeId
)
try {
dispute = await this._Arbitrator.getDispute(
contractAddress,
disputeId
)
// eslint-disable-next-line no-unused-vars
} catch (err) {
// FIXME standardize
throw new Error('DisputeOutOfRange')
}

if (dispute.arbitratedContract === ethConstants.NULL_ADDRESS) break
// session + number of appeals
const disputeSession =
dispute.firstSession + dispute.numberOfAppeals
// if dispute not in current session skip
if (disputeSession !== currentSession) {
disputeId++
dispute = await this._Arbitrator.getDispute(
contractAddress,
disputeId
)
continue
}

const ruling = await this._Arbitrator.currentRulingForDispute(
contractAddress,
disputeId
disputeId,
dispute.numberOfAppeals
)

if (
@@ -168,7 +172,7 @@ class Disputes extends AbstractWrapper {
dispute.arbitratorAddress === contractAddress
) >= 0
) {
await this._StoreProvider.newNotification(
const notification = await this._StoreProvider.newNotification(
address,
txHash,
disputeId, // use disputeId instead of logIndex since it doens't have its own event
@@ -192,28 +196,15 @@ class Disputes extends AbstractWrapper {
block.timestamp * 1000
)

if (notificationCallback) {
const userProfile = await this._StoreProvider.getUserProfile(
address
)
const notification = _.filter(
userProfile.notifications,
notification =>
notification.txHash === txHash &&
notification.logIndex === disputeId
)

if (notification) {
notificationCallback(notification[0])
}
if (notificationCallback && notification) {
notificationCallback(notification[0])
}
}
// check next dispute
disputeId += 1
// eslint-disable-next-line no-unused-vars
} catch (err) {
// getDispute(n) throws an error if index out of range
break
if (err === 'DisputeOutOfRange') break
throw err
}
}
}
@@ -433,7 +424,6 @@ class Disputes extends AbstractWrapper {
draws.push(draw)
}
}

return draws
}

@@ -442,7 +432,7 @@ class Disputes extends AbstractWrapper {
* @param {string} arbitratorAddress - Address of KlerosPOC contract.
* @param {number} disputeId - Index of the dispute.
* @param {number} ruling - Int representing the jurors decision.
* @param {number[]} votes - Int[] of drawn votes for dispute.
* @param {number[]} draws - Int[] of drawn votes for dispute.
* @param {string} account - Address of user.
* @returns {string} - Transaction hash | Error.
*/
@@ -482,11 +472,12 @@ class Disputes extends AbstractWrapper {
const arbitratorData = await this._Arbitrator.getData(arbitratorAddress)

// Last period change + current period duration = deadline
return (
const result =
1000 *
(arbitratorData.lastPeriodChange +
(await this._Arbitrator.getTimeForPeriod(arbitratorAddress, period)))
)

return result
}

/**
@@ -511,36 +502,45 @@ class Disputes extends AbstractWrapper {
account
)

if (createdAt)
disputeData.appealCreatedAt[disputeData.numberOfAppeals] = createdAt
if (ruledAt)
disputeData.appealRuledAt[disputeData.numberOfAppeals] = ruledAt

// update dispute
const dispute = await this._StoreProvider.updateDispute(
disputeData.disputeId,
disputeData.arbitratorAddress,
disputeData.hash,
disputeData.arbitrableContractAddress,
disputeData.partyA,
disputeData.partyB,
disputeData.title,
disputeData.deadline,
disputeData.status,
disputeData.fee,
disputeData.information,
disputeData.justification,
disputeData.resolutionOptions,
createdAt || disputeData.createdAt,
ruledAt || disputeData.ruledAt
disputeData.appealCreatedAt,
disputeData.appealRuledAt
)

const storedDisputeData = await this.getDisputeData(
arbitratorAddress,
disputeId,
account
)
const sessionDraws = await this.getDrawsForJuror(
const storedDisputeData = await this._StoreProvider.getDisputeData(
arbitratorAddress,
disputeId,
account
)
storedDisputeData.appealDraws[disputeData.lastSession] = sessionDraws

const currentSession = await this._Arbitrator.getSession(arbitratorAddress)
if (disputeData.lastSession === currentSession) {
const sessionDraws = await this.getDrawsForJuror(
arbitratorAddress,
disputeId,
account
)

if (!storedDisputeData.appealDraws) storedDisputeData.appealDraws = []
storedDisputeData.appealDraws[disputeData.numberOfAppeals] = sessionDraws
}

// update profile for account
await this._StoreProvider.updateDisputeProfile(
account,
@@ -681,10 +681,10 @@ class Disputes extends AbstractWrapper {
disputeId,
account
)
appealDraws = userData.appealDraws
netPNK = userData.netPNK
appealCreatedAt = userData.appealCreatedAt
appealRuledAt = userData.appealRuledAt
appealDraws = userData.appealDraws || []
netPNK = userData.netPNK || 0
appealCreatedAt = userData.appealCreatedAt || []
appealRuledAt = userData.appealRuledAt || []
// eslint-disable-next-line no-unused-vars
} catch (err) {
// fetching dispute will fail if it hasn't been added to the store yet. this is ok we can just not return store data
@@ -701,7 +701,7 @@ class Disputes extends AbstractWrapper {
// NOTE arrays indexed by appeal number
const appealRulings = []
const appealJuror = []
for (let appeal = 0; appeal < lastSession - firstSession; appeal++) {
for (let appeal = 0; appeal <= lastSession - firstSession; appeal++) {
// get ruling for appeal. Note appeal 0 is first session
const ruling = await this._Arbitrator.currentRulingForDispute(
arbitratorAddress,
@@ -712,20 +712,24 @@ class Disputes extends AbstractWrapper {
appealRulings[appeal] = {
ruling,
voteCounter: dispute.voteCounters[appeal],
ruledAt: appealsRuledAt[appeal],
deadline: this.getDeadlineForDispute(arbitratorAddress, appeal)
ruledAt: appealRuledAt[appeal],
deadline: await this.getDeadlineForDispute(arbitratorAddress, appeal)
}

const baseFee = dispute.arbitrationFeePerJuror
const draws = appealDraws[appeal]
const draws = appealDraws[appeal] || []
let canRule = false
if (appeal === lastSession && draws.length) {
canRule = this._Arbitrator.canRuleDispute(account, disputeId, draws)
if (appeal === lastSession && draws.length > 0) {
canRule = await this._Arbitrator.canRuleDispute(
account,
disputeId,
draws
)
}

appealJuror[appeal] = {
fee: baseFee * draws.length,
draws: appealDraws[appeal],
draws,
canRule
}
}
@@ -755,7 +759,8 @@ class Disputes extends AbstractWrapper {
email: constractStoreData ? constractStoreData.email : undefined,
evidence,
netPNK,
appealCreatedAt
appealCreatedAt,
appealRuledAt
}
}
}
@@ -92,12 +92,15 @@ class Notifications extends AbstractWrapper {
)
}
} else if (currentPeriod === arbitratorConstants.PERIOD.VOTE) {
userProfile.disputes.forEach(dispute => {
if (
dispute.isJuror &&
dispute.votes.length > 0 &&
!dispute.hasRuled
) {
for (let dispute of userProfile.disputes) {
const draws = dispute.appealDraws[dispute.appealDraws.length - 1]
const canVote = await this._Arbitrator.canRuleDispute(
dispute.arbitratorAddress,
dispute.disputeId,
draws,
account
)
if (canVote) {
notifications.push(
this._createNotification(
notificationConstants.TYPE.CAN_VOTE,
@@ -109,7 +112,7 @@ class Notifications extends AbstractWrapper {
)
)
}
})
}
}
} else {
/* Counterparty notifications:
@@ -1,5 +1,6 @@
export const LOCALHOST_ETH_PROVIDER = 'http://localhost:8545'
export const LOCALHOST_STORE_PROVIDER = 'https://kleros.in'
// export const LOCALHOST_STORE_PROVIDER = 'https://kleros.in'
export const LOCALHOST_STORE_PROVIDER = 'http://localhost:3001'

export const NULL_ADDRESS = '0x'

@@ -438,9 +438,9 @@ class KlerosWrapper extends ContractWrapper {
contractAddress,
jurorAddress = this._Web3Wrapper.getAccount(0)
) => {
this.contractInstance = await this.load(contractAddress)
const contractInstance = await this.load(contractAddress)

const isDrawn = await this.contractInstance.isDrawn(
const isDrawn = await contractInstance.isDrawn(
disputeId,
jurorAddress,
draw
@@ -450,23 +450,28 @@ class KlerosWrapper extends ContractWrapper {
}

/**
* Can juror currently rule in dispute
*
* Can juror currently rule in dispute.
* @param {string} arbitratorAddress - address of arbitrator contract.
* @param {number} disputeId - index of dispute.
* @param {int[]} draws - voting positions for dispute.
* @param {string} account - address of user.
* @returns {bool} - Boolean indicating if juror can rule or not.
*/
canRuleDispute = async (arbitratorAddress, disputeId, draws, account) => {
this.contractInstance = await this.load(contractAddress)
const contractInstance = await this.load(arbitratorAddress)

return this.contractInstance.validDraws(account, disputeId, draws)
return contractInstance.validDraws(account, disputeId, draws)
}

/**
* Get number of jurors for a dispute.
* @param {string} contractAddress - Address of KlerosPOC contract.
* @param {number} disputeId - Index of dispute.
* @param {number} appeal - Index of appeal.
* @returns {number} - Int indicating the ruling of the dispute.
*/
currentRulingForDispute = async (contractAddress, disputeId, appeal) => {
this.contractInstance = await this.load(contractAddress)
const contractInstance = await this.load(contractAddress)

const ruling = await contractInstance.getWinningChoice(disputeId, appeal)

@@ -479,7 +484,7 @@ class KlerosWrapper extends ContractWrapper {
* @returns {number} - Int indicating the period.
*/
getPeriod = async contractAddress => {
this.contractInstance = await this.load(contractAddress)
const contractInstance = await this.load(contractAddress)

const currentPeriod = await contractInstance.period()

@@ -492,7 +497,7 @@ class KlerosWrapper extends ContractWrapper {
* @returns {number} - Int indicating the session.
*/
getSession = async contractAddress => {
this.contractInstance = await this.load(contractAddress)
const contractInstance = await this.load(contractAddress)

const currentSession = await contractInstance.session()

Oops, something went wrong.

0 comments on commit 39e9b2c

Please sign in to comment.
You can’t perform that action at this time.