1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00

Add ownerAssets method to assets module.

This commit is contained in:
Pedro Gutiérrez 2019-04-16 13:27:37 +02:00 committed by Pedro Gutiérrez
parent 652d4e7575
commit 47ced8af1b
4 changed files with 46 additions and 8 deletions

View File

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

View File

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

View File

@ -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<number> {
return +await this.call("getBlockNumberUpdated", [zeroX(did)])
}
public async getAttributesByOwner(owner: string): Promise<string[]> {
return (await this.getPastEvents('DIDAttributeRegistered', {_owner: zeroX(owner)}))
.map(({returnValues}) => returnValues._did)
.map(didPrefixed)
}
}

View File

@ -277,6 +277,15 @@ export class OceanAssets extends Instantiable {
return creator
}
/**
* Returns the assets of a owner.
* @param {string} owner Owner address.
* @return {Promise<string[]>} 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.