mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
updated aquarius class methods
This commit is contained in:
parent
21bebf4747
commit
456699f34d
@ -1,4 +1,4 @@
|
||||
import { LoggerInstance } from '../utils'
|
||||
import { LoggerInstance, crossFetchGeneric } from '../utils'
|
||||
import { Asset, DDO, Metadata, ValidateMetadata } from '../@types/'
|
||||
import { json } from 'stream/consumers'
|
||||
|
||||
@ -17,12 +17,15 @@ export class Aquarius {
|
||||
* @param {string} fetchMethod fetch client instance
|
||||
* @return {Promise<DDO>} DDO
|
||||
*/
|
||||
public async resolve(did: string, fetchMethod: any): Promise<DDO> {
|
||||
public async resolve(did: string, fetchMethod?: any): Promise<DDO> {
|
||||
const preferedFetch = fetchMethod || crossFetchGeneric
|
||||
const path = this.aquariusURL + '/api/aquarius/assets/ddo/' + did
|
||||
try {
|
||||
const response = await fetchMethod('GET', path)
|
||||
const response = await preferedFetch('GET', path, null, {
|
||||
'Content-Type': 'application/json'
|
||||
})
|
||||
if (response.ok) {
|
||||
const raw = await response.json()
|
||||
const raw = response.data ? response.data : await response.json()
|
||||
return raw as DDO
|
||||
} else {
|
||||
throw new Error('HTTP request failed with status ' + response.status)
|
||||
@ -44,19 +47,27 @@ export class Aquarius {
|
||||
|
||||
/**
|
||||
* Blocks until Aqua will cache the did (or the update for that did) or timeouts
|
||||
* @param {string} fetchMethod fetch client instance
|
||||
|
||||
* @param {string} did DID of the asset.
|
||||
* @param {string} txid used when the did exists and we expect an update with that txid.
|
||||
* @param {string} fetchMethod fetch client instance
|
||||
* @return {Promise<DDO>} DDO of the asset.
|
||||
*/
|
||||
public async waitForAqua(fetchMethod: any, did: string, txid?: string): Promise<Asset> {
|
||||
public async waitForAqua(
|
||||
did: string,
|
||||
txid?: string,
|
||||
fetchMethod?: any
|
||||
): Promise<Asset> {
|
||||
let tries = 0
|
||||
do {
|
||||
try {
|
||||
const preferedFetch = fetchMethod || crossFetchGeneric
|
||||
const path = this.aquariusURL + '/api/aquarius/assets/ddo/' + did
|
||||
const response = await fetchMethod('GET', path)
|
||||
const response = await preferedFetch('GET', path, null, {
|
||||
'Content-Type': 'application/json'
|
||||
})
|
||||
if (response.ok) {
|
||||
const ddo = await response.json()
|
||||
const ddo = response.data ? response.data : await response.json()
|
||||
if (txid) {
|
||||
// check tx
|
||||
if (ddo.event && ddo.event.txid === txid) return ddo as Asset
|
||||
@ -73,21 +84,22 @@ export class Aquarius {
|
||||
|
||||
/**
|
||||
* Validate DDO content
|
||||
* @param {string} fetchMethod fetch client instance
|
||||
* @param {DDO} ddo DID Descriptor Object content.
|
||||
* @param {string} fetchMethod fetch client instance
|
||||
* @return {Promise<ValidateMetadata>}.
|
||||
*/
|
||||
public async validate(fetchMethod: any, ddo: DDO): Promise<ValidateMetadata> {
|
||||
public async validate(ddo: DDO, fetchMethod: any): Promise<ValidateMetadata> {
|
||||
const preferedFetch = fetchMethod || crossFetchGeneric
|
||||
const status: ValidateMetadata = {
|
||||
valid: false
|
||||
}
|
||||
let jsonResponse
|
||||
try {
|
||||
const path = this.aquariusURL + '/api/aquarius/assets/ddo/validate'
|
||||
const response = await fetchMethod('POST', path, JSON.stringify(ddo), {
|
||||
const response = await preferedFetch('POST', path, JSON.stringify(ddo), {
|
||||
'Content-Type': 'application/octet-stream'
|
||||
})
|
||||
jsonResponse = await response.json()
|
||||
jsonResponse = response.data ? response.data : await response.json()
|
||||
if (response.status === 200) {
|
||||
status.valid = true
|
||||
status.hash = jsonResponse.hash
|
||||
|
@ -241,9 +241,13 @@ describe('Simple compute tests', async () => {
|
||||
'0x' + metadataHash
|
||||
)
|
||||
// let's wait
|
||||
const resolvedDDOAsset = await aquarius.waitForAqua(crossFetchGeneric, ddo.id)
|
||||
const resolvedDDOAsset = await aquarius.waitForAqua(ddo.id, null, crossFetchGeneric)
|
||||
assert(resolvedDDOAsset, 'Cannot fetch DDO from Aquarius')
|
||||
const resolvedDDOAlgo = await aquarius.waitForAqua(crossFetchGeneric, algoDdo.id)
|
||||
const resolvedDDOAlgo = await aquarius.waitForAqua(
|
||||
algoDdo.id,
|
||||
null,
|
||||
crossFetchGeneric
|
||||
)
|
||||
assert(resolvedDDOAlgo, 'Cannot fetch DDO from Aquarius')
|
||||
// mint 1 ERC20 and send it to the consumer
|
||||
await datatoken.mint(datatokenAddressAsset, publisherAccount, '1', consumerAccount)
|
||||
|
@ -147,8 +147,8 @@ describe('Publish tests', async () => {
|
||||
'did:op:' + SHA256(web3.utils.toChecksumAddress(nftAddress) + chain.toString(10))
|
||||
|
||||
const AssetValidation: ValidateMetadata = await aquarius.validate(
|
||||
crossFetchGeneric,
|
||||
poolDdo
|
||||
poolDdo,
|
||||
crossFetchGeneric
|
||||
)
|
||||
assert(AssetValidation.valid === true, 'Published asset is not valid')
|
||||
|
||||
@ -173,7 +173,7 @@ describe('Publish tests', async () => {
|
||||
[AssetValidation.proof]
|
||||
)
|
||||
|
||||
const resolvedDDO = await aquarius.waitForAqua(crossFetchGeneric, poolDdo.id)
|
||||
const resolvedDDO = await aquarius.waitForAqua(poolDdo.id, null, crossFetchGeneric)
|
||||
assert(resolvedDDO, 'Cannot fetch DDO from Aquarius')
|
||||
})
|
||||
|
||||
@ -236,8 +236,8 @@ describe('Publish tests', async () => {
|
||||
'did:op:' + SHA256(web3.utils.toChecksumAddress(nftAddress) + chain.toString(10))
|
||||
|
||||
const isAssetValid: ValidateMetadata = await aquarius.validate(
|
||||
crossFetchGeneric,
|
||||
fixedPriceDdo
|
||||
fixedPriceDdo,
|
||||
crossFetchGeneric
|
||||
)
|
||||
assert(isAssetValid.valid === true, 'Published asset is not valid')
|
||||
|
||||
@ -260,7 +260,11 @@ describe('Publish tests', async () => {
|
||||
'0x' + metadataHash,
|
||||
[]
|
||||
)
|
||||
const resolvedDDO = await aquarius.waitForAqua(crossFetchGeneric, fixedPriceDdo.id)
|
||||
const resolvedDDO = await aquarius.waitForAqua(
|
||||
fixedPriceDdo.id,
|
||||
null,
|
||||
crossFetchGeneric
|
||||
)
|
||||
assert(resolvedDDO, 'Cannot fetch DDO from Aquarius')
|
||||
})
|
||||
|
||||
@ -317,8 +321,8 @@ describe('Publish tests', async () => {
|
||||
'did:op:' + SHA256(web3.utils.toChecksumAddress(nftAddress) + chain.toString(10))
|
||||
|
||||
const isAssetValid: ValidateMetadata = await aquarius.validate(
|
||||
crossFetchGeneric,
|
||||
dispenserDdo
|
||||
dispenserDdo,
|
||||
crossFetchGeneric
|
||||
)
|
||||
assert(isAssetValid.valid === true, 'Published asset is not valid')
|
||||
|
||||
@ -340,7 +344,11 @@ describe('Publish tests', async () => {
|
||||
encryptedResponse,
|
||||
'0x' + metadataHash
|
||||
)
|
||||
const resolvedDDO = await aquarius.waitForAqua(crossFetchGeneric, dispenserDdo.id)
|
||||
const resolvedDDO = await aquarius.waitForAqua(
|
||||
dispenserDdo.id,
|
||||
null,
|
||||
crossFetchGeneric
|
||||
)
|
||||
assert(resolvedDDO, 'Cannot fetch DDO from Aquarius')
|
||||
})
|
||||
})
|
||||
|
@ -122,7 +122,7 @@ describe('Simple Publish & consume test', async () => {
|
||||
encryptedResponse,
|
||||
'0x' + metadataHash
|
||||
)
|
||||
const resolvedDDO = await aquarius.waitForAqua(crossFetchGeneric, ddo.id)
|
||||
const resolvedDDO = await aquarius.waitForAqua(ddo.id, null, crossFetchGeneric)
|
||||
assert(resolvedDDO, 'Cannot fetch DDO from Aquarius')
|
||||
// mint 1 ERC20 and send it to the consumer
|
||||
await datatoken.mint(datatokenAddress, publisherAccount, '1', consumerAccount)
|
||||
|
Loading…
x
Reference in New Issue
Block a user