This repository has been archived by the owner on Jun 30, 2022. It is now read-only.
/
Arbitrator.js
91 lines (80 loc) · 2.93 KB
/
Arbitrator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import _ from 'lodash'
import * as arbitratorConstants from '../../constants/arbitrator'
import AbstractContract from '../AbstractContract'
/**
* 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
* arbitrator methods from the supplied contract implementation can be called from this
* object.
*/
class Arbitrator extends AbstractContract {
/**
* Get disputes for user with extra data from arbitrated transaction and store
* @param {string} account address of user
* @returns {object[]} dispute data objects for user
*/
getDisputesForUser = async account => {
// contract data
const [period, currentSession] = await Promise.all([
this._contractImplementation.getPeriod(),
this._contractImplementation.getSession()
])
// new jurors have not been chosen yet. don't update
if (period !== arbitratorConstants.PERIOD.VOTE) {
return this.getDisputesForUserFromStore(account)
}
let profile = await this._StoreProvider.newUserProfile(account)
if (currentSession !== profile.session) {
// get disputes for juror
const myDisputes = await this._contractImplementation.getDisputesForJuror(
account
)
// update user profile for each dispute
await Promise.all(
myDisputes.map(async dispute => {
const disputeCreationLog = await this._contractImplementation.getDisputeCreationEvent(
dispute.disputeId
)
if (!disputeCreationLog)
throw new Error('Could not fetch dispute creation event log')
// update profile for account
await this._StoreProvider.updateDisputeProfile(
account,
dispute.arbitratorAddress,
dispute.disputeId,
{
appealDraws: dispute.appealDraws,
blockNumber: disputeCreationLog.blockNumber
}
)
})
)
// FIXME do we want to store session?
// this._StoreProvider.updateUserProfile(account, {
// session: currentSession
// })
}
return this.getDisputesForUserFromStore(account)
}
getDisputesForUserFromStore = async account => {
const aribtratorAddress = this._contractImplementation.getContractAddress()
return Promise.all(
(await this._StoreProvider.getDisputes(account)).filter(dispute =>
dispute.arbitratorAddress === aribtratorAddress
).map(dispute =>
this._contractImplementation.getDispute(dispute.disputeId)
)
)
}
/**
* Buy PNK.
* @param {number} amount - Number of pinakion to buy.
* @param {string} account - Address of user.
* @returns {object[]} - Balance of user.
*/
buyPNK = async (amount, account) => {
await this._contractImplementation.buyPNK(amount, account)
return this._contractImplementation.getPNKBalance(account)
}
}
export default Arbitrator