Skip to content

Commit

Permalink
Merge pull request #86 from oceanprotocol/issue-85-refactor
Browse files Browse the repository at this point in the history
refactor fn call waitForAqua to waitForIndexer
  • Loading branch information
paulo-ocean authored Jan 9, 2025
2 parents ce5ea7f + 9d2ac40 commit dd45fb6
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 23 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ export PROVIDER_URL='XXXX'
export ADDRESS_FILE='path-to-address-file'
```

- Optional set INDEXING_MAX_RETRIES to the max number of retries when waiting for an asset to be indexed. Default is 100 retries max.

```
export INDEXING_MAX_RETRIES='100'
```

- Optional set INDEXING_RETRY_INTERVAL to the interval (in miliseconds) for each retry when waiting for an asset to be indexed. Default is 3 seconds.

```
export INDEXING_RETRY_INTERVAL='3000'
```

### Build the TypeScript code

```
Expand Down
15 changes: 7 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
"dependencies": {
"@oceanprotocol/contracts": "^2.0.4",
"@oceanprotocol/lib": "^3.4.1",
"@oceanprotocol/lib": "^3.4.6",
"cross-fetch": "^3.1.5",
"crypto-js": "^4.1.1",
"decimal.js": "^10.4.1",
Expand Down
25 changes: 15 additions & 10 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
downloadFile,
isOrderable,
getMetadataURI,
getIndexingWaitSettings,
IndexerWaitParams,
} from "./helpers";
import {
Aquarius,
Expand All @@ -34,11 +36,14 @@ export class Commands {
public aquarius: Aquarius;
public providerUrl: string;
public macOsProviderUrl: string;
// optional settings for indexing wait time
private indexingParams: IndexerWaitParams;

constructor(signer: Signer, network: string | number, config?: Config) {
this.signer = signer;
this.config = config || new ConfigHelper().getConfig(network);
this.providerUrl = process.env.NODE_URL || process.env.PROVIDER_URL || this.config.providerUri;
this.indexingParams = getIndexingWaitSettings();
if (
!process.env.PROVIDER_URL && !process.env.NODE_URL &&
this.config.chainId === 8996 &&
Expand Down Expand Up @@ -142,7 +147,7 @@ export class Commands {
}

public async editAsset(args: string[]) {
const asset = await this.aquarius.waitForAqua(args[1]);
const asset = await this.aquarius.waitForIndexer(args[1],null,null, this.indexingParams.retryInterval, this.indexingParams.maxRetries);
if (!asset) {
console.error(
"Error fetching DDO " + args[1] + ". Does this asset exists?"
Expand Down Expand Up @@ -178,7 +183,7 @@ export class Commands {

public async getDDO(args: string[]) {
console.log("Resolving Asset with DID: " + args[1]);
const resolvedDDO = await this.aquarius.waitForAqua(args[1]);
const resolvedDDO = await this.aquarius.waitForIndexer(args[1],null,null, this.indexingParams.retryInterval, this.indexingParams.maxRetries);
if (!resolvedDDO) {
console.error(
"Error fetching Asset with DID: " +
Expand All @@ -189,7 +194,7 @@ export class Commands {
}

public async download(args: string[]) {
const dataDdo = await this.aquarius.waitForAqua(args[1]);
const dataDdo = await this.aquarius.waitForIndexer(args[1],null,null, this.indexingParams.retryInterval, this.indexingParams.maxRetries);
if (!dataDdo) {
console.error(
"Error fetching DDO " + args[1] + ". Does this asset exists?"
Expand Down Expand Up @@ -258,7 +263,7 @@ export class Commands {
const ddos = [];

for (const dataset in inputDatasets) {
const dataDdo = await this.aquarius.waitForAqua(inputDatasets[dataset]);
const dataDdo = await this.aquarius.waitForIndexer(inputDatasets[dataset],null,null, this.indexingParams.retryInterval, this.indexingParams.maxRetries);
if (!dataDdo) {
console.error(
"Error fetching DDO " + dataset[1] + ". Does this asset exists?"
Expand All @@ -277,7 +282,7 @@ export class Commands {
? this.macOsProviderUrl
: ddos[0].services[0].serviceEndpoint;

const algoDdo = await this.aquarius.waitForAqua(args[2]);
const algoDdo = await this.aquarius.waitForIndexer(args[2],null,null, this.indexingParams.retryInterval, this.indexingParams.maxRetries);
if (!algoDdo) {
console.error(
"Error fetching DDO " + args[1] + ". Does this asset exists?"
Expand Down Expand Up @@ -436,7 +441,7 @@ export class Commands {
}

public async computeStop(args: string[]) {
const dataDdo = await this.aquarius.waitForAqua(args[1]);
const dataDdo = await this.aquarius.waitForIndexer(args[1],null,null, this.indexingParams.retryInterval, this.indexingParams.maxRetries);
if (!dataDdo) {
console.error(
"Error fetching DDO " + args[1] + ". Does this asset exists?"
Expand Down Expand Up @@ -468,7 +473,7 @@ export class Commands {
}

public async allowAlgo(args: string[]) {
const asset = await this.aquarius.waitForAqua(args[1]);
const asset = await this.aquarius.waitForIndexer(args[1],null,null, this.indexingParams.retryInterval, this.indexingParams.maxRetries);
if (!asset) {
console.error(
"Error fetching DDO " + args[1] + ". Does this asset exists?"
Expand All @@ -491,7 +496,7 @@ export class Commands {
);
return;
}
const algoAsset = await this.aquarius.waitForAqua(args[2]);
const algoAsset = await this.aquarius.waitForIndexer(args[2],null,null, this.indexingParams.retryInterval, this.indexingParams.maxRetries);
if (!algoAsset) {
console.error(
"Error fetching DDO " + args[2] + ". Does this asset exists?"
Expand Down Expand Up @@ -538,7 +543,7 @@ export class Commands {
}

public async disallowAlgo(args: string[]) {
const asset = await this.aquarius.waitForAqua(args[1]);
const asset = await this.aquarius.waitForIndexer(args[1],null,null, this.indexingParams.retryInterval, this.indexingParams.maxRetries);
if (!asset) {
console.error(
"Error fetching DDO " + args[1] + ". Does this asset exists?"
Expand Down Expand Up @@ -603,7 +608,7 @@ export class Commands {
// args[3] - agreementId
const hasAgreementId = args.length === 4;

const dataDdo = await this.aquarius.waitForAqua(args[1]);
const dataDdo = await this.aquarius.waitForIndexer(args[1],null,null, this.indexingParams.retryInterval, this.indexingParams.maxRetries);
if (!dataDdo) {
console.error(
"Error fetching DDO " + args[1] + ". Does this asset exists?"
Expand Down
34 changes: 34 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,4 +433,38 @@ export function isPrivateIP(ip): boolean {
}
return ip
}
// for waiting for an asset to index
export interface IndexerWaitParams {
maxRetries: number,
retryInterval: number
}

// defines how much time we wait for an asset to index + the interval for retries
export function getIndexingWaitSettings(): IndexerWaitParams {
const indexingParams: IndexerWaitParams = {
maxRetries: 100, // 100 retries
retryInterval: 3000 // retries every 3 seconds
}
try {

if(!isNaN(Number(process.env.INDEXING_RETRY_INTERVAL))) {

indexingParams.retryInterval = Number(process.env.INDEXING_RETRY_INTERVAL)
if(indexingParams.retryInterval < 0) {
indexingParams.retryInterval = 3000
}

}
if(!isNaN(Number(process.env.INDEXING_MAX_RETRIES))) {

indexingParams.maxRetries = Number(process.env.INDEXING_MAX_RETRIES)
if(indexingParams.maxRetries < 0) {
indexingParams.maxRetries = 100
}
}
}catch(err) {
console.error('Error getting indexing wait arguments:' , err)
}

return indexingParams
}
11 changes: 7 additions & 4 deletions src/publishAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Signer } from 'ethers';
import {
Config,
Aquarius,
DDO
Asset
} from '@oceanprotocol/lib';
import { createAsset, updateAssetMetadata } from './helpers';

Expand All @@ -28,7 +28,7 @@ export async function publishAsset(params: PublishAssetParams, signer: Signer, c
const aquarius = new Aquarius(config.metadataCacheUri);

// Prepare initial metadata for the asset
const metadata: DDO = {
const metadata: Asset = {
'@context': ['https://w3id.org/did/v1'],
id: '', // Will be updated after creating asset
version: '4.1.0',
Expand All @@ -48,7 +48,7 @@ export async function publishAsset(params: PublishAssetParams, signer: Signer, c
allocated: 0,
orders: 0,
price: {
value: params.isCharged ? params.price : "0"
value: params.isCharged ? Number(params.price) : 0
}
},
services: [
Expand All @@ -70,7 +70,10 @@ export async function publishAsset(params: PublishAssetParams, signer: Signer, c
tokenURI: "",
owner: "",
created: ""
}
},
datatokens: [],
event: undefined,
purgatory: undefined
};

// Asset URL setup based on storage type
Expand Down

0 comments on commit dd45fb6

Please sign in to comment.