Skip to content
Permalink
Browse files

feat(all): cleanup for the demo

  • Loading branch information...
satello committed May 9, 2018
1 parent f2e838d commit 0dc14e1a81b3a0807f79c1ed6cf09b5b4cfc9496
@@ -5,6 +5,8 @@ export const MISSING_PARAMETERS = name => `Missing required parameter: ${name}`
export const PROFILE_NOT_FOUND = user => `No profile found for user: ${user}.`
export const NOTIFICATION_NOT_FOUND = txHash =>
`No notification with txHash ${txHash} exists.`
export const REQUEST_FAILED = error =>
`Request returned an error response: ${error}`

// Contracts
export const UNABLE_TO_DEPLOY_CONTRACT =
@@ -47,7 +47,7 @@ class ArbitrableContract extends AbstractContract {
...args
)

await this._StoreProvider.updateContract(
const newContract = await this._StoreProvider.updateContract(
account,
contractInstance.address,
{
@@ -62,8 +62,7 @@ class ArbitrableContract extends AbstractContract {
}
)

// return contract data
return this.getData(contractInstance.address, account)
return newContract
}

/**
@@ -162,6 +162,7 @@ class KlerosPOC extends ContractImplementation {
/**
* Call contract to move on to the next period.
* @param {string} account - address of user.
* @returns {Promise} - resulting object.
*/
passPeriod = async (account = this._Web3Wrapper.getAccount(0)) => {
await this.loadContract()
@@ -171,6 +172,7 @@ class KlerosPOC extends ContractImplementation {
from: account,
gas: ethConstants.TRANSACTION.GAS
})
return this.getData()
} catch (err) {
console.error(err)
throw new Error(errorConstants.UNABLE_TO_PASS_PERIOD)
@@ -158,7 +158,8 @@ class Disputes {
* @param {string} account - The users eth account.
*/
_storeDisputeRuledAtTimestamp = async (event, account) => {
const newPeriod = event.args._period.toNumber()
// we fetch the current period in case we are consuming old events from previous sessions
const newPeriod = await this._ArbitratorInstance.getPeriod()
// send appeal possible notifications
if (newPeriod === arbitratorConstants.PERIOD.APPEAL) {
const disputes = await this._StoreProviderInstance.getDisputesForUser(
@@ -202,11 +203,12 @@ class Disputes {

/**
* Event listener handler that sets the deadline for an appeal
* @param {object} event - The event log.
* @param {object} _ - The event log. Unused in function.
* @param {string} account - The users eth account.
*/
_storeAppealDeadline = async (event, account) => {
const newPeriod = event.args._period.toNumber()
_storeAppealDeadline = async (_, account) => {
// we fetch the current period in case we are consuming old events from previous sessions
const newPeriod = await this._ArbitratorInstance.getPeriod()
// send appeal possible notifications
if (newPeriod === arbitratorConstants.PERIOD.VOTE) {
const disputes = await this._StoreProviderInstance.getDisputesForUser(
@@ -246,6 +248,17 @@ class Disputes {
// **************************** //
// * Public * //
// **************************** //
/**
* Fetch the shared dispute data from the store.
* @param {string} disputeId - The index of the dispute.
* @returns {Promise} The dispute data in the store.
*/
getDisputeFromStore = disputeId => {
const arbitratorAddress = this._ArbitratorInstance.getContractAddress()

return this._StoreProviderInstance.getDispute(arbitratorAddress, disputeId)
}

/**
* Get data for a dispute. This method provides data from the store as well as both
* arbitrator and arbitrable contracts. Used to get all relevant data on a dispute.
@@ -236,7 +236,14 @@ class Notifications {
this._requireStoreProvider()

const profile = await this._StoreProviderInstance.getUserProfile(account)
return _.filter(profile.notifications, notification => !notification.read)
const currentArbitrator = this._ArbitratorInstance.getContractAddress()
// return notifications that are for current arbitrator and are unread
return _.filter(
profile.notifications,
notification =>
notification.data.arbitratorAddress === currentArbitrator &&
!notification.read
)
}

/**
@@ -246,15 +253,15 @@ class Notifications {
* @param {number} logIndex index of the log. used to differentiate logs if multiple logs per tx
* @returns {promise} promise that can be waited on for syncronousity
*/
markStoredNotificationAsRead = (account, txHash, logIndex) => {
markStoredNotificationAsRead = async (account, txHash, logIndex) => {
this._requireStoreProvider()

return this._StoreProviderInstance.markNotificationAsRead(
const result = await this._StoreProviderInstance.markNotificationAsRead(
account,
txHash,
logIndex,
true
)
return result
}

/**
@@ -286,7 +293,7 @@ class Notifications {
if (newPeriod === arbitratorConstants.PERIOD.APPEAL) {
const disputes = await this._getDisputes(account) // get users disputes
const openDisputes = await this._ArbitratorInstance.getOpenDisputesForSession() // get all disputes for session
const contractAddress = this._ArbitratorInstance.getContractAddress()
const arbitratorAddress = this._ArbitratorInstance.getContractAddress()

await Promise.all(
openDisputes.map(async disputeId => {
@@ -295,7 +302,7 @@ class Notifications {
disputes,
dispute =>
dispute.disputeId === disputeId &&
dispute.arbitratorAddress === contractAddress
dispute.arbitratorAddress === arbitratorAddress
) >= 0
) {
const dispute = await this._ArbitratorInstance.getDispute(disputeId)
@@ -311,8 +318,8 @@ class Notifications {
notificationConstants.TYPE.APPEAL_POSSIBLE,
'A ruling has been made. Appeal is possible',
{
disputeId,
contractAddress,
disputeId: disputeId,
arbitratorAddress,
ruling
}
)
@@ -619,7 +626,7 @@ class Notifications {

// If we have store provider fetch contracts and disputes from the store.
if (this._StoreProviderInstance) {
await this._StoreProviderInstance.getDisputesForUser(account)
disputes = await this._StoreProviderInstance.getDisputesForUser(account)
} else if (isJuror) {
// We have no way to get contracts. Get disputes from current session
// TODO make a function to get open disputes for parites
@@ -245,7 +245,7 @@ class StoreProviderWrapper {
* @param {object} params - Params we want to update.
* @returns {Promise} - The resulting contract data.
*/
updateContract = (userAddress, contractAddress, params) => {
updateContract = async (userAddress, contractAddress, params) => {
const getBodyFn = async () => {
let currentContractData = await this.getContractByAddress(
userAddress,
@@ -259,11 +259,20 @@ class StoreProviderWrapper {
return JSON.stringify({ ...currentContractData, ...params })
}

return this.queueWriteRequest(
const httpResponse = await this.queueWriteRequest(
getBodyFn,
'POST',
`${this._storeUri}/${userAddress}/contracts/${contractAddress}`
)

if (httpResponse.status !== 200) {
throw new Error(errorConstants.REQUEST_FAILED(httpResponse.error))
}

return _.filter(
httpResponse.body[0].contracts,
contract => contract.address === contractAddress
)[0]
}

/**
@@ -322,14 +331,14 @@ class StoreProviderWrapper {
const getBodyFn = async () => {
const userProfile = await this.getUserProfile(userAddress)

const disputeIndex = _.filter(
userProfile.disputes,
dispute =>
dispute.arbitratorAddress === arbitratorAddress &&
dispute.disputeId === disputeId
)
const currentDisputeProfile =
_.filter(
userProfile.disputes,
dispute =>
dispute.arbitratorAddress === arbitratorAddress &&
dispute.disputeId === disputeId
)[0] || {}

const currentDisputeProfile = userProfile.disputes[disputeIndex] || {}
delete currentDisputeProfile._id
// set these so if it is a new dispute they are included
params.disputeId = disputeId
@@ -446,11 +455,13 @@ class StoreProviderWrapper {
return JSON.stringify(userProfile)
}

return this.queueWriteRequest(
const result = await this.queueWriteRequest(
getBodyFn,
'POST',
`${this._storeUri}/${userAddress}`
)

return result.body.notifications
}
}

0 comments on commit 0dc14e1

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