diff --git a/src/ocean/Assets.ts b/src/ocean/Assets.ts index 79ff3387..c037f550 100644 --- a/src/ocean/Assets.ts +++ b/src/ocean/Assets.ts @@ -1,7 +1,12 @@ import { SearchQuery } from '../metadatastore/MetadataStore' import { DDO } from '../ddo/DDO' import { Metadata } from '../ddo/interfaces/Metadata' -import { Service, ServiceAccess, ServiceComputePrivacy } from '../ddo/interfaces/Service' +import { + Service, + ServiceAccess, + ServiceComputePrivacy, + ServiceCommon +} from '../ddo/interfaces/Service' import { EditableMetadata } from '../ddo/interfaces/EditableMetadata' import Account from './Account' import DID from './DID' @@ -352,8 +357,19 @@ export class Assets extends Instantiable { } as SearchQuery) } + public async getService(did: string, serviceType: string): Promise { + const services: ServiceCommon[] = (await this.resolve(did)).service + let service + services.forEach((serv) => { + if (serv.type.toString() === serviceType) { + service = serv + } + }) + return service + } + public async createAccessServiceAttributes( - consumerAccount: Account, + creator: Account, dtCost: number, datePublished: string, timeout: number = 0 @@ -364,7 +380,7 @@ export class Assets extends Instantiable { serviceEndpoint: this.ocean.provider.getConsumeEndpoint(), attributes: { main: { - creator: consumerAccount.getId(), + creator: creator.getId(), datePublished, dtCost, timeout: timeout, diff --git a/test/integration/Marketplaceflow.test.ts b/test/integration/Marketplaceflow.test.ts index 94909b61..ac04867a 100644 --- a/test/integration/Marketplaceflow.test.ts +++ b/test/integration/Marketplaceflow.test.ts @@ -17,16 +17,12 @@ describe('Marketplace flow', () => { let ddo let alice let asset - let accounts let marketplace - let marketOcean let contracts let datatoken let tokenAddress - let transactionId let service1 - let service2 - + let price let ocean const marketplaceAllowance = 20 @@ -89,7 +85,16 @@ describe('Marketplace flow', () => { }) it('Alice publishes a dataset', async () => { - ddo = await ocean.assets.create(asset, alice, [], tokenAddress) + price = 10 // in datatoken + const publishedDate = new Date(Date.now()).toISOString().split('.')[0] + 'Z' + const timeout = 0 + service1 = await ocean.assets.createAccessServiceAttributes( + alice, + price, + publishedDate, + timeout + ) + ddo = await ocean.assets.create(asset, alice, [service1], tokenAddress) assert(ddo.dataToken === tokenAddress) }) @@ -133,24 +138,23 @@ describe('Marketplace flow', () => { ) }) }) + it('Marketplace should resolve asset using DID', async () => { + assert(ddo, await ocean.assets.resolve(ddo.id)) + }) - // it('Marketplace posts asset for sale', async () => { - // const config = new Config() - // marketOcean = await Ocean.getInstance(config) - - // service1 = marketOcean.assets.getService('download') - // service2 = marketOcean.assets.getService('access') - - // }) + it('Marketplace posts asset for sale', async () => { + const accessService = await ocean.assets.getService(ddo.id, 'access') + const price = 20 + assert(accessService.attributes.main.dtCost * price === 200) + }) it('Bob gets datatokens', async () => { - const ts = await datatoken.transfer( - tokenAddress, - bob.getId(), - transferAmount, - alice.getId() - ) - transactionId = ts.transactionHash + await datatoken + .transfer(tokenAddress, bob.getId(), transferAmount, alice.getId()) + .then(async () => { + const balance = await datatoken.balance(tokenAddress, bob.getId()) + assert(balance.toString() === transferAmount.toString()) + }) }) // it('Bob consumes asset 1', async () => { @@ -158,9 +162,5 @@ describe('Marketplace flow', () => { // const ocean = await Ocean.getInstance(config) // await ocean.assets.download(asset.did, service1.index, bob, '~/my-datasets') // }) - - // it('Bob consumes asset 2', async () => { - // // TODO - // }) }) })