From eadaa7abb5a63538b09fb64315a59e5c7a18638b Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Thu, 1 Oct 2020 05:57:04 -0700 Subject: [PATCH] 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) + }) })