1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

Merge pull request #325 from oceanprotocol/feature/assetHistory

add order history
This commit is contained in:
Alex Coseru 2020-10-01 17:00:13 +03:00 committed by GitHub
commit 7c1dd365ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 2 deletions

View File

@ -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,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.
*/
@ -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
}
}

View File

@ -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 () => {})
})

View File

@ -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)
})
})