From 5d7ac4b1a7933e94837f92c5172e38af72160eaf Mon Sep 17 00:00:00 2001 From: satello Date: Wed, 9 May 2018 15:58:53 -0700 Subject: [PATCH] feat(all): cleanup for the demo --- src/constants/error.js | 2 ++ src/contracts/abstractions/Arbitrable.js | 5 ++- .../implementations/arbitrator/KlerosPOC.js | 2 ++ src/resources/Disputes.js | 21 ++++++++++--- src/resources/Notifications.js | 25 +++++++++------ src/utils/StoreProviderWrapper.js | 31 +++++++++++++------ 6 files changed, 60 insertions(+), 26 deletions(-) diff --git a/src/constants/error.js b/src/constants/error.js index 45610ff..5ced8f7 100644 --- a/src/constants/error.js +++ b/src/constants/error.js @@ -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 = diff --git a/src/contracts/abstractions/Arbitrable.js b/src/contracts/abstractions/Arbitrable.js index 747bb5a..acc46a8 100644 --- a/src/contracts/abstractions/Arbitrable.js +++ b/src/contracts/abstractions/Arbitrable.js @@ -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 } /** diff --git a/src/contracts/implementations/arbitrator/KlerosPOC.js b/src/contracts/implementations/arbitrator/KlerosPOC.js index d5fad89..27c2a4e 100644 --- a/src/contracts/implementations/arbitrator/KlerosPOC.js +++ b/src/contracts/implementations/arbitrator/KlerosPOC.js @@ -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) diff --git a/src/resources/Disputes.js b/src/resources/Disputes.js index 3239b11..1af7b55 100644 --- a/src/resources/Disputes.js +++ b/src/resources/Disputes.js @@ -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. diff --git a/src/resources/Notifications.js b/src/resources/Notifications.js index 1547e97..038d74c 100644 --- a/src/resources/Notifications.js +++ b/src/resources/Notifications.js @@ -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 diff --git a/src/utils/StoreProviderWrapper.js b/src/utils/StoreProviderWrapper.js index f3579da..81a0e59 100644 --- a/src/utils/StoreProviderWrapper.js +++ b/src/utils/StoreProviderWrapper.js @@ -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 } }