From eadaa7abb5a63538b09fb64315a59e5c7a18638b Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Thu, 1 Oct 2020 05:57:04 -0700 Subject: [PATCH 1/5] add order history --- src/ocean/Assets.ts | 45 +++++++++++++++++++++++- test/integration/ComputeFlow.test.ts | 5 ++- test/integration/Marketplaceflow.test.ts | 4 +++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/ocean/Assets.ts b/src/ocean/Assets.ts index 69f0f3e9..65494cf2 100644 --- a/src/ocean/Assets.ts +++ b/src/ocean/Assets.ts @@ -11,7 +11,7 @@ import { import { EditableMetadata } from '../ddo/interfaces/EditableMetadata' import Account from './Account' import DID from './DID' -import { SubscribablePromise } from '../utils' +import { SubscribablePromise, didNoZeroX, didPrefixed } from '../utils' import { Instantiable, InstantiableConfig } from '../Instantiable.abstract' import { WebServiceConnector } from './utils/WebServiceConnector' import BigNumber from 'bignumber.js' @@ -33,6 +33,15 @@ export enum OrderProgressStep { TransferDataToken } +export interface OrderHistory { + dtAddress: string + timestamp: number + transactionHash: string + did?: string + serviceId?: number + serviceType?: string +} + /** * Assets submodule of Ocean Protocol. */ @@ -581,4 +590,38 @@ export class Assets extends Instantiable { return serviceEndpoint } + + /** + * get Order History + * @param {Account} account + * @return {Promise} transactionHash of the payment + */ + public async getOrderHistory(account: Account): Promise { + const results: OrderHistory[] = [] + const address = this.web3.utils.toChecksumAddress(account.getId()) + const events = await this.web3.eth.getPastLogs({ + topics: [ + ['0x24c95b9bea47f62df4b9eea32c98c597eccfc5cac47f8477647be875ad925eee', address] + ], + fromBlock: 0 + }) + for (let i = 0; i < events.length; i++) { + const blockDetails = await this.web3.eth.getBlock(events[i].blockNumber) + const order: OrderHistory = { + dtAddress: events[i].address, + timestamp: parseInt(String(blockDetails.timestamp)), + transactionHash: events[i].transactionHash + } + const params = this.web3.eth.abi.decodeParameters( + ['uint256', 'uint256', 'uint256', 'uint256'], + events[i].data + ) + order.serviceId = parseInt(params[1]) + order.did = didPrefixed(didNoZeroX(order.dtAddress)) + const service = await this.getServiceByIndex(order.did, order.serviceId) + order.serviceType = service.type + results.push(order) + } + return results + } } diff --git a/test/integration/ComputeFlow.test.ts b/test/integration/ComputeFlow.test.ts index 58e890bf..f47661bb 100644 --- a/test/integration/ComputeFlow.test.ts +++ b/test/integration/ComputeFlow.test.ts @@ -460,7 +460,10 @@ describe('Compute flow', () => { newComputePrivacy.trustedAlgorithms ) }) - + it('Bob gets his order History', async () => { + const history = await ocean.assets.getOrderHistory(bob) + assert(history.length > 0) + }) // it('Bob restarts compute job', async () => {}) // it('Bob gets outputs', async () => {}) }) diff --git a/test/integration/Marketplaceflow.test.ts b/test/integration/Marketplaceflow.test.ts index 8a621b58..6b3e538e 100644 --- a/test/integration/Marketplaceflow.test.ts +++ b/test/integration/Marketplaceflow.test.ts @@ -252,4 +252,8 @@ describe('Marketplace flow', () => { ddo = await ocean.assets.create(asset, alice, [service1]) assert.equal(ddo, null) }) + it('Alice gets hers order History', async () => { + const history = await ocean.assets.getOrderHistory(alice) + assert(history.length > 0) + }) }) From 2c3c8bb04e67855efeabe36676017be8edf083e2 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Thu, 1 Oct 2020 05:59:39 -0700 Subject: [PATCH 2/5] add amount in history --- src/ocean/Assets.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/ocean/Assets.ts b/src/ocean/Assets.ts index 65494cf2..407d6bb9 100644 --- a/src/ocean/Assets.ts +++ b/src/ocean/Assets.ts @@ -35,6 +35,7 @@ export enum OrderProgressStep { export interface OrderHistory { dtAddress: string + amount: string timestamp: number transactionHash: string did?: string @@ -610,13 +611,15 @@ export class Assets extends Instantiable { const order: OrderHistory = { dtAddress: events[i].address, timestamp: parseInt(String(blockDetails.timestamp)), - transactionHash: events[i].transactionHash + transactionHash: events[i].transactionHash, + amount: null } const params = this.web3.eth.abi.decodeParameters( ['uint256', 'uint256', 'uint256', 'uint256'], events[i].data ) order.serviceId = parseInt(params[1]) + order.amount = this.web3.utils.fromWei(params[0]) order.did = didPrefixed(didNoZeroX(order.dtAddress)) const service = await this.getServiceByIndex(order.did, order.serviceId) order.serviceType = service.type From 4d00c882df3915eb172b3df7bcb70d3dfd7a5f5f Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Thu, 1 Oct 2020 06:12:56 -0700 Subject: [PATCH 3/5] add optional filter --- src/ocean/Assets.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/ocean/Assets.ts b/src/ocean/Assets.ts index 407d6bb9..e424f431 100644 --- a/src/ocean/Assets.ts +++ b/src/ocean/Assets.ts @@ -595,9 +595,13 @@ export class Assets extends Instantiable { /** * get Order History * @param {Account} account + * @param {string} serviceType Optional, filter by * @return {Promise} transactionHash of the payment */ - public async getOrderHistory(account: Account): Promise { + public async getOrderHistory( + account: Account, + serviceType?: string + ): Promise { const results: OrderHistory[] = [] const address = this.web3.utils.toChecksumAddress(account.getId()) const events = await this.web3.eth.getPastLogs({ @@ -623,7 +627,8 @@ export class Assets extends Instantiable { order.did = didPrefixed(didNoZeroX(order.dtAddress)) const service = await this.getServiceByIndex(order.did, order.serviceId) order.serviceType = service.type - results.push(order) + if (!serviceType || (serviceType && serviceType === service.type)) + results.push(order) } return results } From 386bb892ac3802f1f761de5aeaf2c1c54467852f Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Thu, 1 Oct 2020 06:23:56 -0700 Subject: [PATCH 4/5] add optional fromBlock --- src/ocean/Assets.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ocean/Assets.ts b/src/ocean/Assets.ts index e424f431..d2dfa53a 100644 --- a/src/ocean/Assets.ts +++ b/src/ocean/Assets.ts @@ -596,11 +596,13 @@ export class Assets extends Instantiable { * get Order History * @param {Account} account * @param {string} serviceType Optional, filter by + * @param {number} fromBlock Optional, start at block * @return {Promise} transactionHash of the payment */ public async getOrderHistory( account: Account, - serviceType?: string + serviceType?: string, + fromBlock?: number ): Promise { const results: OrderHistory[] = [] const address = this.web3.utils.toChecksumAddress(account.getId()) @@ -608,7 +610,7 @@ export class Assets extends Instantiable { topics: [ ['0x24c95b9bea47f62df4b9eea32c98c597eccfc5cac47f8477647be875ad925eee', address] ], - fromBlock: 0 + fromBlock: fromBlock || 0 }) for (let i = 0; i < events.length; i++) { const blockDetails = await this.web3.eth.getBlock(events[i].blockNumber) From 2a715c94427247d92f06deedde5ecc2f717dd644 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Thu, 1 Oct 2020 06:49:43 -0700 Subject: [PATCH 5/5] rename from OrderHistory to Order --- src/ocean/Assets.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ocean/Assets.ts b/src/ocean/Assets.ts index d2dfa53a..a0df988e 100644 --- a/src/ocean/Assets.ts +++ b/src/ocean/Assets.ts @@ -33,7 +33,7 @@ export enum OrderProgressStep { TransferDataToken } -export interface OrderHistory { +export interface Order { dtAddress: string amount: string timestamp: number @@ -603,8 +603,8 @@ export class Assets extends Instantiable { account: Account, serviceType?: string, fromBlock?: number - ): Promise { - const results: OrderHistory[] = [] + ): Promise { + const results: Order[] = [] const address = this.web3.utils.toChecksumAddress(account.getId()) const events = await this.web3.eth.getPastLogs({ topics: [ @@ -614,7 +614,7 @@ export class Assets extends Instantiable { }) for (let i = 0; i < events.length; i++) { const blockDetails = await this.web3.eth.getBlock(events[i].blockNumber) - const order: OrderHistory = { + const order: Order = { dtAddress: events[i].address, timestamp: parseInt(String(blockDetails.timestamp)), transactionHash: events[i].transactionHash,