Skip to content
Permalink
Browse files

feat: pull open disputes from cache

  • Loading branch information...
satello committed Aug 29, 2018
1 parent 8b0acea commit 39505f58cdb9f4a0f5da48a809f8573af59010ac
@@ -4,3 +4,6 @@ export const STATE = {
EXECUTABLE: 2,
RESOLVED: 3
}

export const DISPUTE_CACHE_URI =
'https://plkzpnwz7e.execute-api.us-east-2.amazonaws.com/session'
@@ -2,7 +2,8 @@ import _ from 'lodash'

import * as arbitratorConstants from '../../constants/arbitrator'
import AbstractContract from '../AbstractContract'

import httpRequest from '../../utils/httpRequest'
import { DISPUTE_CACHE_URI } from '../../constants/dispute'
/**
* Arbitrator Abstract Contarct API. This wraps an arbitrator contract. It provides
* interaction with both the off chain store as well as the arbitrator instance. All
@@ -12,10 +13,13 @@ import AbstractContract from '../AbstractContract'
class Arbitrator extends AbstractContract {
/**
* Get disputes for user with extra data from arbitrated transaction and store
* @param {string} account address of user
* @param {string} account Address of user
* @param {bool} allowOffChainCache Should open disputes be pulled from off chain cache.
* The cache is maintained by Kleros for performance. To pull open disputes from the blockchain
* set false
* @returns {object[]} dispute data objects for user
*/
getDisputesForUser = async account => {
getDisputesForUser = async (account, allowOffChainCache = true) => {
// contract data
const [period, currentSession] = await Promise.all([
this._contractImplementation.getPeriod(),
@@ -29,9 +33,30 @@ class Arbitrator extends AbstractContract {

let profile = await this._StoreProvider.newUserProfile(account)
if (currentSession !== profile.session) {
// pull open disputes from off chain cache
let cachedDisputes = null
if (allowOffChainCache) {
const cachedDisputesResponse = await httpRequest(
'GET',
`${DISPUTE_CACHE_URI}/${currentSession}`
)

if (
cachedDisputesResponse.body &&
cachedDisputesResponse.body.open_disputes
) {
cachedDisputes = await Promise.all(
cachedDisputesResponse.body.open_disputes.map(disputeIdString =>
this._contractImplementation.getDispute(Number(disputeIdString))
)
)
}
}

// get disputes for juror
const myDisputes = await this._contractImplementation.getDisputesForJuror(
account
account,
cachedDisputes
)

// update user profile for each dispute
@@ -75,7 +75,7 @@ class MiniMePinakion extends ContractImplementation {

try {
return this.contractInstance.changeController(klerosAddress, {
from: account,
from: account
})
} catch (err) {
console.error(err)
@@ -91,15 +91,19 @@ class MiniMePinakion extends ContractImplementation {
* @param {string} account - The users account.
* @returns {bool} If the transfer succeeded or not
*/
approveAndCall = async (arbitratorAddress, amount, account = this._Web3Wrapper.getAccount(0)) => {
approveAndCall = async (
arbitratorAddress,
amount,
account = this._Web3Wrapper.getAccount(0)
) => {
await this.loadContract()

return this.contractInstance.approveAndCall(
arbitratorAddress,
this._Web3Wrapper.toWei(amount, 'ether'),
'0x0',
{
from: account,
from: account
}
)
}
@@ -109,7 +113,7 @@ class MiniMePinakion extends ContractImplementation {
* @param {string} account - The users account.
* @returns {number} the amount of tokens.
*/
getTokenBalance = async (account) => {
getTokenBalance = async account => {
await this.loadContract()

return this.contractInstance.balanceOf(account)
@@ -50,7 +50,7 @@ class PinakionPOC extends ContractImplementation {

try {
return this.contractInstance.setKleros(klerosAddress, {
from: account,
from: account
})
} catch (err) {
console.error(err)
@@ -72,7 +72,7 @@ class PinakionPOC extends ContractImplementation {

try {
return this.contractInstance.transferOwnership(klerosAddress, {
from: account,
from: account
})
} catch (err) {
console.error(err)
@@ -40,15 +40,13 @@ class Arbitrable extends ContractImplementation {

const metaEvidenceUri = metaEvidenceLog[0].args._evidence
// FIXME caching issue need a query param to fetch from AWS
const metaEvidenceResponse = await httpRequest(
'GET',
metaEvidenceUri
)
const metaEvidenceResponse = await httpRequest('GET', metaEvidenceUri)

if (metaEvidenceResponse.status >= 400)
throw new Error(`Unable to fetch meta-evidence at ${metaEvidenceUri}`)

this.metaEvidenceCache[this.contractAddress] = metaEvidenceResponse.body || metaEvidenceResponse
this.metaEvidenceCache[this.contractAddress] =
metaEvidenceResponse.body || metaEvidenceResponse
return metaEvidenceResponse.body || metaEvidenceResponse
}

@@ -94,9 +92,7 @@ class Arbitrable extends ContractImplementation {
getContractData = async () => {
await this.loadContract()

const [metaEvidence] = await Promise.all([
this.getMetaEvidence()
])
const [metaEvidence] = await Promise.all([this.getMetaEvidence()])

return {
metaEvidence
@@ -63,15 +63,24 @@ class Kleros extends ContractImplementation {
* @param {string} account - The address of the user.
* @returns {object} - Balance information including total PNK balance and activated tokens.
*/
transferPNKToArbitrator = async (amount, account = this._Web3Wrapper.getAccount(0)) => {
transferPNKToArbitrator = async (
amount,
account = this._Web3Wrapper.getAccount(0)
) => {
await this.loadContract()

const pinakionContractAddress = await this.contractInstance.pinakion()
const pnkInstance = new MiniMePinakion(this.getWeb3Provider(), pinakionContractAddress)
const pnkInstance = new MiniMePinakion(
this.getWeb3Provider(),
pinakionContractAddress
)

const deposited = await pnkInstance.approveAndCall(this.contractAddress, amount, account)
if (!deposited)
throw new Error('Unable to deposit PNK')
const deposited = await pnkInstance.approveAndCall(
this.contractAddress,
amount,
account
)
if (!deposited) throw new Error('Unable to deposit PNK')

return this.getPNKBalance(account)
}
@@ -87,7 +96,7 @@ class Kleros extends ContractImplementation {
await this.contractInstance.withdraw(
this._Web3Wrapper.toWei(amount, 'ether'),
{
from: account,
from: account
}
)

@@ -103,10 +112,13 @@ class Kleros extends ContractImplementation {
await this.loadContract()

const pinakionContractAddress = await this.contractInstance.pinakion()
const pnkInstance = new MiniMePinakion(this.getWeb3Provider(), pinakionContractAddress)
const pnkInstance = new MiniMePinakion(
this.getWeb3Provider(),
pinakionContractAddress
)

const contractBalance = this._Web3Wrapper.fromWei(
(await pnkInstance.getTokenBalance(account)),
await pnkInstance.getTokenBalance(account),
'ether'
)

@@ -127,7 +139,7 @@ class Kleros extends ContractImplementation {
let activatedTokens = 0
if (juror[2].toNumber() === currentSession.toNumber())
activatedTokens = this._Web3Wrapper.fromWei(
(juror[4].minus(juror[3])).toNumber(),
juror[4].minus(juror[3]).toNumber(),
'ether'
)

@@ -158,7 +170,7 @@ class Kleros extends ContractImplementation {
await this.contractInstance.activateTokens(
this._Web3Wrapper.toWei(amount, 'ether'),
{
from: account,
from: account
}
)
} catch (err) {
@@ -221,7 +233,7 @@ class Kleros extends ContractImplementation {

try {
await this.contractInstance.passPeriod.original({
from: account,
from: account
})
return this.getData()
} catch (err) {
@@ -248,7 +260,7 @@ class Kleros extends ContractImplementation {

try {
return this.contractInstance.voteRuling(disputeId, ruling, votes, {
from: account,
from: account
})
} catch (err) {
console.error(err)
@@ -273,7 +285,7 @@ class Kleros extends ContractImplementation {
try {
return this.contractInstance.appeal(disputeId, extraData, {
from: account,
value: await this.contractInstance.appealCost(disputeId, extraData),
value: await this.contractInstance.appealCost(disputeId, extraData)
})
} catch (err) {
console.error(err)
@@ -295,7 +307,7 @@ class Kleros extends ContractImplementation {

try {
return this.contractInstance.oneShotTokenRepartition(disputeId, {
from: account,
from: account
})
} catch (err) {
console.error(err)
@@ -317,7 +329,7 @@ class Kleros extends ContractImplementation {

try {
return this.contractInstance.executeRuling(disputeId, {
from: account,
from: account
})
} catch (err) {
console.error(err)
@@ -374,7 +386,9 @@ class Kleros extends ContractImplementation {
voteCounters.push(voteCounts)
}

voteCounters = await Promise.all(voteCounters.map(voteCounts => Promise.all(voteCounts)))
voteCounters = await Promise.all(
voteCounters.map(voteCounts => Promise.all(voteCounts))
)
}

return {
@@ -528,13 +542,14 @@ class Kleros extends ContractImplementation {
/**
* Get disputes from Kleros contract.
* @param {string} account - Address of user.
* @param {string} openDisputes - <optional> Pass open disputes from cached source
* @returns {object[]} - Array of disputes.
*/
getDisputesForJuror = async account => {
getDisputesForJuror = async (account, openDisputes) => {
await this.loadContract()

// contract data
const openDisputes = await this.getOpenDisputesForSession()
if (!openDisputes) openDisputes = await this.getOpenDisputesForSession()

const disputes = await Promise.all(
openDisputes.map(async disputeData => {
@@ -621,7 +636,7 @@ class Kleros extends ContractImplementation {

const numberOfJurors = await this.getAmountOfJurorsForDispute(disputeId)
let jurorDraw = null
for (let i=0; i < numberOfJurors; i++) {
for (let i = 0; i < numberOfJurors; i++) {
const jurorAddress = await this.contractInstance.getVoteAccount(
disputeId,
appeal,
@@ -465,14 +465,20 @@ class Notifications {
const amount = event.args._amount.toNumber()

if (account === address) {
const message =
amount === 0
? `Dispute Resolved: No PNK won`
: `Dispute Resolved: You have ${
amount < 0 ? 'lost' : 'won'
} ${this._Web3Wrapper.fromWei(amount, 'ether')} PNK.`
const arbitratorAddress = this._ArbitratorInstance.getContractAddress()
const notification = await this._newNotification(
account,
event.transactionHash,
event.blockNumber,
event.logIndex,
notificationConstants.TYPE.TOKEN_SHIFT,
`You have ${amount < 0 ? 'lost' : 'won'} ${this._Web3Wrapper.fromWei(amount, 'ether')} PNK.`,
message,
{
disputeId,
arbitratorAddress,
@@ -505,7 +511,10 @@ class Notifications {
event.blockNumber,
event.logIndex,
notificationConstants.TYPE.ARBITRATION_REWARD,
`You have been awarded an ${this._Web3Wrapper.fromWei(amount, 'ether')} ETH arbitration fee`,
`You have been awarded an ${this._Web3Wrapper.fromWei(
amount,
'ether'
)} ETH arbitration fee`,
{
disputeId,
arbitratorAddress,
@@ -35,7 +35,6 @@ class StoreProviderWrapper {
)
}


/**
* If we know we are waiting on some other write before we want to read we can add a read request to the end of the queue.
* @param {string} uri uri to hit

0 comments on commit 39505f5

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