Skip to content

Commit

Permalink
Merge pull request #325 from oceanprotocol/feature/assetHistory
Browse files Browse the repository at this point in the history
add order history
  • Loading branch information
alexcos20 authored Oct 1, 2020
2 parents 50e2f8d + 2a715c9 commit 7c1dd36
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
55 changes: 54 additions & 1 deletion src/ocean/Assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -33,6 +33,16 @@ export enum OrderProgressStep {
TransferDataToken
}

export interface Order {
dtAddress: string
amount: string
timestamp: number
transactionHash: string
did?: string
serviceId?: number
serviceType?: string
}

/**
* Assets submodule of Ocean Protocol.
*/
Expand Down Expand Up @@ -581,4 +591,47 @@ export class Assets extends Instantiable {

return serviceEndpoint
}

/**
* get Order History
* @param {Account} account
* @param {string} serviceType Optional, filter by
* @param {number} fromBlock Optional, start at block
* @return {Promise<OrderHistory[]>} transactionHash of the payment
*/
public async getOrderHistory(
account: Account,
serviceType?: string,
fromBlock?: number
): Promise<Order[]> {
const results: Order[] = []
const address = this.web3.utils.toChecksumAddress(account.getId())
const events = await this.web3.eth.getPastLogs({
topics: [
['0x24c95b9bea47f62df4b9eea32c98c597eccfc5cac47f8477647be875ad925eee', address]
],
fromBlock: fromBlock || 0
})
for (let i = 0; i < events.length; i++) {
const blockDetails = await this.web3.eth.getBlock(events[i].blockNumber)
const order: Order = {
dtAddress: events[i].address,
timestamp: parseInt(String(blockDetails.timestamp)),
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
if (!serviceType || (serviceType && serviceType === service.type))
results.push(order)
}
return results
}
}
5 changes: 4 additions & 1 deletion test/integration/ComputeFlow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {})
})
4 changes: 4 additions & 0 deletions test/integration/Marketplaceflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})

0 comments on commit 7c1dd36

Please sign in to comment.