From 47ced8af1b6b741b55fab0eed5f52952aaa99de2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Guti=C3=A9rrez?= Date: Tue, 16 Apr 2019 13:27:37 +0200 Subject: [PATCH] Add ownerAssets method to assets module. --- integration/ocean/AssetOwners.test.ts | 27 +++++++++++++++++++++------ src/keeper/contracts/ContractBase.ts | 10 +++++++++- src/keeper/contracts/DIDRegistry.ts | 8 +++++++- src/ocean/OceanAssets.ts | 9 +++++++++ 4 files changed, 46 insertions(+), 8 deletions(-) diff --git a/integration/ocean/AssetOwners.test.ts b/integration/ocean/AssetOwners.test.ts index 6aeadaf..ee4ee07 100644 --- a/integration/ocean/AssetOwners.test.ts +++ b/integration/ocean/AssetOwners.test.ts @@ -6,10 +6,11 @@ import { getMetadata } from "../utils" import { Ocean, Account } from "../../src" // @oceanprotocol/squid -describe("Asset Owners", () => { +describe.only("Asset Owners", () => { let ocean: Ocean - let publisher: Account + let account1: Account + let account2: Account const metadata = getMetadata() @@ -17,15 +18,29 @@ describe("Asset Owners", () => { ocean = await Ocean.getInstance(config) // Accounts - publisher = (await ocean.accounts.list())[0] - publisher.setPassword(process.env.ACCOUNT_PASSWORD) + const accounts = await ocean.accounts.list() + account1 = accounts[0] + account2 = accounts[1] }) it("should be set correctly the owner of a asset", async () => { - const ddo = await ocean.assets.create(metadata as any, publisher) + const ddo = await ocean.assets.create(metadata as any, account1) const owner = await ocean.assets.owner(ddo.id) - assert.equal(owner, publisher.getId()) + assert.equal(owner, account1.getId()) + }) + + it("should get the assets owned by a user", async () => { + const {length: initialLength} = await ocean.assets.ownerAssets(account2.getId()) + + await ocean.assets.create(metadata as any, account1) + await ocean.assets.create(metadata as any, account1) + + await ocean.assets.create(metadata as any, account2) + + const {length: finalLength} = await ocean.assets.ownerAssets(account2.getId()) + + assert.equal(finalLength - initialLength, 1) }) }) diff --git a/src/keeper/contracts/ContractBase.ts b/src/keeper/contracts/ContractBase.ts index 597f2ad..71a8210 100644 --- a/src/keeper/contracts/ContractBase.ts +++ b/src/keeper/contracts/ContractBase.ts @@ -16,13 +16,21 @@ export default abstract class ContractBase extends Instantiable { this.contractName = contractName } - public async getEventData(eventName: any, options: any) { + public async getEventData(eventName: string, options: any) { if (!this.contract.events[eventName]) { throw new Error(`Event "${eventName}" not found on contract "${this.contractName}"`) } return this.contract.getPastEvents(eventName, options) } + public getPastEvents(eventName: string, filter: {[key: string]: any}) { + return this.getEventData(eventName, { + filter, + fromBlock: 0, + toBlock: "latest", + }) + } + public getAddress(): string { return this.contract.options.address } diff --git a/src/keeper/contracts/DIDRegistry.ts b/src/keeper/contracts/DIDRegistry.ts index 0a7d603..8ff0185 100644 --- a/src/keeper/contracts/DIDRegistry.ts +++ b/src/keeper/contracts/DIDRegistry.ts @@ -1,6 +1,6 @@ import Web3Provider from "../Web3Provider" import ContractBase from "./ContractBase" -import { zeroX } from "../../utils" +import { zeroX, didPrefixed } from "../../utils" import { InstantiableConfig } from "../../Instantiable.abstract" export default class DIDRegistry extends ContractBase { @@ -26,4 +26,10 @@ export default class DIDRegistry extends ContractBase { public async getBlockNumberUpdated(did: string): Promise { return +await this.call("getBlockNumberUpdated", [zeroX(did)]) } + + public async getAttributesByOwner(owner: string): Promise { + return (await this.getPastEvents('DIDAttributeRegistered', {_owner: zeroX(owner)})) + .map(({returnValues}) => returnValues._did) + .map(didPrefixed) + } } diff --git a/src/ocean/OceanAssets.ts b/src/ocean/OceanAssets.ts index 1325b42..35374b7 100644 --- a/src/ocean/OceanAssets.ts +++ b/src/ocean/OceanAssets.ts @@ -277,6 +277,15 @@ export class OceanAssets extends Instantiable { return creator } + /** + * Returns the assets of a owner. + * @param {string} owner Owner address. + * @return {Promise} List of DIDs. + */ + public async ownerAssets(owner: string) { + return this.ocean.keeper.didRegistry.getAttributesByOwner(owner) + } + /** * Search over the assets using a query. * @param {SearchQuery} query Query to filter the assets.