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

Merge pull request #119 from oceanprotocol/feature/assets-download

Feature/assets download
This commit is contained in:
Ahmed Ali 2020-07-02 17:23:38 +02:00 committed by GitHub
commit 8cfffd5976
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 162 additions and 92 deletions

View File

@ -38,7 +38,8 @@
"no-empty": ["error", { "allowEmptyCatch": true }], "no-empty": ["error", { "allowEmptyCatch": true }],
"prefer-destructuring": ["warn"], "prefer-destructuring": ["warn"],
"no-dupe-class-members": ["warn"], "no-dupe-class-members": ["warn"],
"no-useless-constructor": ["warn"] "no-useless-constructor": ["warn"],
"dot-notation": 0
}, },
"env": { "env": {
"es6": true, "es6": true,

View File

@ -19,6 +19,7 @@ before_script:
- git clone https://github.com/oceanprotocol/barge - git clone https://github.com/oceanprotocol/barge
- cd barge - cd barge
- git checkout v3 - git checkout v3
- export PROVIDER_VERSION=alex
- bash -x start_ocean.sh --no-dashboard 2>&1 > start_ocean.log & - bash -x start_ocean.sh --no-dashboard 2>&1 > start_ocean.log &
- cd .. - cd ..
- sleep 300 - sleep 300

View File

@ -19,7 +19,7 @@ export interface ServiceAccessAttributes extends ServiceCommonAttributes {
creator: string creator: string
name: string name: string
datePublished: string datePublished: string
dtCost: number cost: number
timeout: number timeout: number
} }
} }

View File

@ -26,10 +26,7 @@ export enum CreateProgressStep {
} }
export enum OrderProgressStep { export enum OrderProgressStep {
CreatingAgreement, TransferDataToken
AgreementInitialized,
LockingPayment,
LockedPayment
} }
/** /**
@ -357,7 +354,10 @@ export class Assets extends Instantiable {
} as SearchQuery) } as SearchQuery)
} }
public async getService(did: string, serviceType: string): Promise<ServiceCommon> { public async getServiceByType(
did: string,
serviceType: string
): Promise<ServiceCommon> {
const services: ServiceCommon[] = (await this.resolve(did)).service const services: ServiceCommon[] = (await this.resolve(did)).service
let service let service
services.forEach((serv) => { services.forEach((serv) => {
@ -382,7 +382,7 @@ export class Assets extends Instantiable {
main: { main: {
creator: creator.getId(), creator: creator.getId(),
datePublished, datePublished,
dtCost, cost: dtCost,
timeout: timeout, timeout: timeout,
name: 'dataAssetAccessServiceAgreement' name: 'dataAssetAccessServiceAgreement'
} }
@ -390,7 +390,61 @@ export class Assets extends Instantiable {
} }
} }
public async order(
did: string,
serviceType: string,
consumerAddress: string
): Promise<string> {
const service = await this.getServiceByType(did, serviceType)
return await this.ocean.provider.initialize(
did,
service.index,
serviceType,
consumerAddress
)
}
// marketplace flow
public async download( public async download(
did: string,
txId: string,
tokenAddress: string,
consumerAccount: Account,
destination: string
): Promise<string | true> {
const ddo = await this.resolve(did)
const { attributes } = ddo.findServiceByType('metadata')
const service = ddo.findServiceByType('access')
const { files } = attributes.main
const { serviceEndpoint } = service
if (!serviceEndpoint) {
throw new Error(
'Consume asset failed, service definition is missing the `serviceEndpoint`.'
)
}
this.logger.log('Consuming files')
destination = destination
? `${destination}/datafile.${ddo.shortId()}.${service.index}/`
: undefined
await this.ocean.provider.download(
did,
txId,
tokenAddress,
service.type,
service.index.toString(),
destination,
consumerAccount,
files
)
return true
}
// simple flow
public async simpleDownload(
dtAddress: string, dtAddress: string,
serviceEndpoint: string, serviceEndpoint: string,
txId: string, txId: string,
@ -400,9 +454,8 @@ export class Assets extends Instantiable {
consumeUrl += `?consumerAddress=${account}` consumeUrl += `?consumerAddress=${account}`
consumeUrl += `&tokenAddress=${dtAddress}` consumeUrl += `&tokenAddress=${dtAddress}`
consumeUrl += `&transferTxId=${txId}` consumeUrl += `&transferTxId=${txId}`
const serviceConnector = new WebServiceConnector(this.logger) const serviceConnector = new WebServiceConnector(this.logger)
console.log(consumeUrl)
try { try {
await serviceConnector.downloadFile(consumeUrl) await serviceConnector.downloadFile(consumeUrl)
} catch (e) { } catch (e) {

View File

@ -69,7 +69,7 @@ export class Provider extends Instantiable {
serviceIndex: number, serviceIndex: number,
serviceType: string, serviceType: string,
consumerAddress: string consumerAddress: string
): Promise<any> { ): Promise<string> {
let DDO let DDO
try { try {
DDO = await this.ocean.assets.resolve(did) DDO = await this.ocean.assets.resolve(did)
@ -77,21 +77,17 @@ export class Provider extends Instantiable {
this.logger.error(e) this.logger.error(e)
throw new Error('Failed to resolve DID') throw new Error('Failed to resolve DID')
} }
const { dtAddress } = DDO
const args = { let initializeUrl = this.getInitializeEndpoint()
documentId: did, initializeUrl += `?documentId=${did}`
serviceId: serviceIndex, initializeUrl += `&serviceId=${serviceIndex}`
serviceType: serviceType, initializeUrl += `&serviceType=${serviceType}`
tokenAddress: dtAddress, initializeUrl += `&dataToken=${DDO.dataToken}`
consumerAddress: consumerAddress initializeUrl += `&consumerAddress=${consumerAddress}`
}
try { try {
return await this.ocean.utils.fetch.post( const response = await this.ocean.utils.fetch.get(initializeUrl)
this.getInitializeEndpoint(), return await response.text()
decodeURI(JSON.stringify(args))
)
} catch (e) { } catch (e) {
this.logger.error(e) this.logger.error(e)
throw new Error('HTTP request failed') throw new Error('HTTP request failed')
@ -114,11 +110,11 @@ export class Provider extends Instantiable {
.filter((_, i) => index === -1 || i === index) .filter((_, i) => index === -1 || i === index)
.map(async ({ index: i }) => { .map(async ({ index: i }) => {
let consumeUrl = this.getDownloadEndpoint() let consumeUrl = this.getDownloadEndpoint()
consumeUrl += `?index=${i}` consumeUrl += `?fileIndex=${i}`
consumeUrl += `&documentId=${did}` consumeUrl += `&documentId=${did}`
consumeUrl += `&serviceId=${serviceIndex}` consumeUrl += `&serviceId=${serviceIndex}`
consumeUrl += `&serviceType=${serviceType}` consumeUrl += `&serviceType=${serviceType}`
consumeUrl += `tokenAddress=${tokenAddress}` consumeUrl += `&dataToken=${tokenAddress}`
consumeUrl += `&transferTxId=${txId}` consumeUrl += `&transferTxId=${txId}`
consumeUrl += `&consumerAddress=${account.getId()}` consumeUrl += `&consumerAddress=${account.getId()}`
consumeUrl += `&signature=${signature}` consumeUrl += `&signature=${signature}`

View File

@ -4,8 +4,6 @@ import { Ocean } from '../../src/ocean/Ocean'
import config from './config' import config from './config'
import { assert } from 'console' import { assert } from 'console'
// import Accounts from "../../src/ocean/Account"
const Web3 = require('web3') const Web3 = require('web3')
const web3 = new Web3('http://127.0.0.1:8545') const web3 = new Web3('http://127.0.0.1:8545')
const factory = require('@oceanprotocol/contracts/artifacts/development/Factory.json') const factory = require('@oceanprotocol/contracts/artifacts/development/Factory.json')
@ -24,11 +22,12 @@ describe('Marketplace flow', () => {
let service1 let service1
let price let price
let ocean let ocean
let accessService
let data
let blob
const marketplaceAllowance = 20 const marketplaceAllowance = 20
const tokenAmount = 100 const tokenAmount = 100
const transferAmount = 2
const blob = 'http://localhost:8030/api/v1/provider/services'
describe('#test', () => { describe('#test', () => {
it('Initialize Ocean contracts v3', async () => { it('Initialize Ocean contracts v3', async () => {
@ -41,12 +40,12 @@ describe('Marketplace flow', () => {
) )
ocean = await Ocean.getInstance(config) ocean = await Ocean.getInstance(config)
owner = (await ocean.accounts.list())[0] owner = (await ocean.accounts.list())[0]
alice = (await ocean.accounts.list())[1] alice = (await ocean.accounts.list())[1]
bob = (await ocean.accounts.list())[2] bob = (await ocean.accounts.list())[2]
marketplace = (await ocean.accounts.list())[3] marketplace = (await ocean.accounts.list())[3]
data = { t: 1, url: ocean.config.metadataStoreUri }
blob = JSON.stringify(data)
await contracts.deployContracts(owner.getId()) await contracts.deployContracts(owner.getId())
}) })
@ -57,8 +56,8 @@ describe('Marketplace flow', () => {
datatokensTemplate.abi, datatokensTemplate.abi,
web3 web3
) )
tokenAddress = await datatoken.create(blob, alice.getId()) tokenAddress = await datatoken.create(blob, alice.getId())
console.log(blob)
}) })
it('Generates metadata', async () => { it('Generates metadata', async () => {
@ -139,28 +138,48 @@ describe('Marketplace flow', () => {
}) })
}) })
it('Marketplace should resolve asset using DID', async () => { it('Marketplace should resolve asset using DID', async () => {
assert(ddo, await ocean.assets.resolve(ddo.id)) await ocean.assets.resolve(ddo.id).then((newDDO) => {
assert(newDDO.id === ddo.id)
})
}) })
it('Marketplace posts asset for sale', async () => { it('Marketplace posts asset for sale', async () => {
const accessService = await ocean.assets.getService(ddo.id, 'access') accessService = await ocean.assets.getServiceByType(ddo.id, 'access')
const price = 20 price = 20
assert(accessService.attributes.main.dtCost * price === 200) assert(accessService.attributes.main.cost * price === 200)
}) })
it('Bob gets datatokens', async () => { it('Bob gets datatokens', async () => {
const dTamount = 20
await datatoken await datatoken
.transfer(tokenAddress, bob.getId(), transferAmount, alice.getId()) .transfer(tokenAddress, bob.getId(), dTamount, alice.getId())
.then(async () => { .then(async () => {
const balance = await datatoken.balance(tokenAddress, bob.getId()) const balance = await datatoken.balance(tokenAddress, bob.getId())
assert(balance.toString() === transferAmount.toString()) assert(balance.toString() === dTamount.toString())
}) })
}) })
// it('Bob consumes asset 1', async () => { it('Bob consumes asset 1', async () => {
// // const config = new Config() await ocean.assets
// const ocean = await Ocean.getInstance(config) .order(ddo.id, accessService.type, bob.getId())
// await ocean.assets.download(asset.did, service1.index, bob, '~/my-datasets') .then(async (res: string) => {
// }) res = JSON.parse(res)
return await datatoken.transfer(
res['dataToken'],
res['to'],
res['numTokens'],
res['from']
)
})
.then(async (tx) => {
await ocean.assets.download(
ddo.id,
tx.transactionHash,
tokenAddress,
bob,
'~/my-datasets'
)
})
})
}) })
}) })

View File

@ -9,52 +9,52 @@ const factory = require('@oceanprotocol/contracts/artifacts/development/Factory.
const datatokensTemplate = require('@oceanprotocol/contracts/artifacts/development/DataTokenTemplate.json') const datatokensTemplate = require('@oceanprotocol/contracts/artifacts/development/DataTokenTemplate.json')
describe('Simple flow', () => { describe('Simple flow', () => {
// let owner let owner
// let bob let bob
// let alice let alice
// let contracts let contracts
// let datatoken let datatoken
// let tokenAddress let tokenAddress
// let transactionId let transactionId
// const tokenAmount = 100 const tokenAmount = 100
// const transferAmount = 1 const transferAmount = 1
// const blob = 'http://localhost:8030/api/v1/provider/services' const blob = 'http://localhost:8030/api/v1/services/consume'
// describe('#test', () => { describe('#test', () => {
// it('Initialize Ocean contracts v3', async () => { it('Initialize Ocean contracts v3', async () => {
// contracts = new TestContractHandler( contracts = new TestContractHandler(
// factory.abi, factory.abi,
// datatokensTemplate.abi, datatokensTemplate.abi,
// datatokensTemplate.bytecode, datatokensTemplate.bytecode,
// factory.bytecode, factory.bytecode,
// web3 web3
// ) )
// await contracts.getAccounts() await contracts.getAccounts()
// owner = contracts.accounts[0] owner = contracts.accounts[0]
// alice = contracts.accounts[1] alice = contracts.accounts[1]
// bob = contracts.accounts[2] bob = contracts.accounts[2]
// await contracts.deployContracts(owner) await contracts.deployContracts(owner)
// }) })
// it('Alice publishes a dataset', async () => { it('Alice publishes a dataset', async () => {
// // Alice creates a Datatoken // Alice creates a Datatoken
// datatoken = new DataTokens( datatoken = new DataTokens(
// contracts.factoryAddress, contracts.factoryAddress,
// factory.abi, factory.abi,
// datatokensTemplate.abi, datatokensTemplate.abi,
// web3 web3
// ) )
// tokenAddress = await datatoken.create(blob, alice) tokenAddress = await datatoken.create(blob, alice)
// }) })
// it('Alice mints 100 tokens', async () => { it('Alice mints 100 tokens', async () => {
// await datatoken.mint(tokenAddress, alice, tokenAmount) await datatoken.mint(tokenAddress, alice, tokenAmount)
// }) })
// it('Alice transfers 1 token to Bob', async () => { it('Alice transfers 1 token to Bob', async () => {
// const ts = await datatoken.transfer(tokenAddress, bob, transferAmount, alice) const ts = await datatoken.transfer(tokenAddress, bob, transferAmount, alice)
// transactionId = ts.transactionHash transactionId = ts.transactionHash
// }) })
// it('Bob consumes dataset', async () => { // it('Bob consumes dataset', async () => {
// const config = new Config() // const config = new Config()
// const ocean = await Ocean.getInstance(config) // const ocean = await Ocean.getInstance(config)
// await ocean.assets.download(tokenAddress, blob, transactionId, bob) // await ocean.assets.simpleDownload(tokenAddress, blob, transactionId, bob)
// })
// }) // })
}) })
})

View File

@ -7,7 +7,7 @@ const Web3 = require('web3')
const web3 = new Web3('http://127.0.0.1:8545') const web3 = new Web3('http://127.0.0.1:8545')
export default { export default {
metadataStoreUri: 'http://localhost:5000', metadataStoreUri: 'http://aquarius:5000',
providerUri: 'http://localhost:8030', providerUri: 'http://localhost:8030',
nodeUri: `http://localhost:${process.env.ETH_PORT || 8545}`, nodeUri: `http://localhost:${process.env.ETH_PORT || 8545}`,
verbose: LogLevel.Error, verbose: LogLevel.Error,