@oceanprotocol/lib 10.0.0-next.1 → 10.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +213 -149
- package/dist/lib.modern.mjs +2 -2
- package/dist/lib.module.mjs +2 -2
- package/dist/types/@types/Provider.d.ts +12 -0
- package/dist/types/services/providers/BaseProvider.d.ts +14 -0
- package/dist/types/services/providers/HttpProvider.d.ts +26 -0
- package/dist/types/services/providers/P2pProvider.d.ts +37 -0
- package/dist/types/utils/ProviderErrors.d.ts +41 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
<h1 align="center">ocean.js</h1>
|
|
4
4
|
|
|
5
|
-
> JavaScript library to privately & securely publish, exchange, and consume data.
|
|
5
|
+
> JavaScript/TypeScript client library to privately & securely publish, exchange, and consume data on Ocean Protocol.
|
|
6
6
|
|
|
7
7
|
[](https://www.npmjs.com/package/@oceanprotocol/lib)
|
|
8
8
|
[](https://github.com/oceanprotocol/ocean.js/actions)
|
|
@@ -11,243 +11,307 @@
|
|
|
11
11
|
[](https://github.com/prettier/prettier)
|
|
12
12
|
[](https://github.com/oceanprotocol/eslint-config-oceanprotocol)
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
`@oceanprotocol/lib` v10 | ESM-only | Node.js >= 22 | ethers v6
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
- **Consume** data: download files or run compute-to-data jobs on datasets while preserving data privacy.
|
|
18
|
-
- **Sell** datatokens via fixed-rate exchanges or dispense them for free.
|
|
19
|
-
- **Manage pricing**: create fixed-rate exchanges, dispensers, or use dynamic pricing mechanisms for your assets.
|
|
20
|
-
- **Compute-to-Data**: run algorithms on the ocean network.
|
|
16
|
+
## What it is
|
|
21
17
|
|
|
22
|
-
ocean.js is
|
|
18
|
+
ocean.js (`@oceanprotocol/lib`) is the client library for [Ocean Nodes](https://github.com/oceanprotocol/ocean-node) and the Ocean smart contracts. It gives you:
|
|
23
19
|
|
|
24
|
-
|
|
20
|
+
- **An Ocean Node client** that speaks both **HTTP** and **P2P (libp2p)**. The same API works over either transport.
|
|
21
|
+
- **Typed wrappers for the Ocean contracts**: data NFTs, datatokens, fixed-rate exchange, dispenser, router, escrow, access lists, fee collector.
|
|
22
|
+
- **Publish and consume flows**: publish downloadable files or compute-to-data (C2D) services, order access, download, and run free or paid compute jobs.
|
|
23
|
+
- **Pricing**: fixed-rate exchanges and dispensers (free access).
|
|
24
|
+
- **v9 highlights**: P2P transport, JWT/auth tokens, escrow for paid compute, Oasis Sapphire (confidential EVM) support, access lists, and node persistent storage.
|
|
25
|
+
- **v10 highlights**: Escrow v2 (authorization expiry/renew, prefunded sponsorship reads, ERC-165 discovery), read-only subsidy-provider wrappers for dashboards (`SubsidyView` / `OPFSubsidyProvider` / `OneTimeSubsidyProvider`), consumer-selectable subsidy providers on compute/service start, node maintenance (drain) mode, and updated deps (`@oceanprotocol/contracts@^3.2.0`, `@oceanprotocol/ddo-js@^1.0.0`).
|
|
25
26
|
|
|
26
|
-
- [
|
|
27
|
-
- [🏗 Installation & Usage](#-installation--usage)
|
|
28
|
-
- [📖 Documentation](#-documentation)
|
|
29
|
-
- [🦑 Development](#-development)
|
|
30
|
-
- [✨ Code Style](#-code-style)
|
|
31
|
-
- [👩🔬 Testing](#-testing)
|
|
32
|
-
- [Unit Tests](#unit-tests)
|
|
33
|
-
- [Integration Tests](#integration-tests)
|
|
34
|
-
- [🛳 Production](#-production)
|
|
35
|
-
- [⬆️ Releases](#️-releases)
|
|
36
|
-
- [Production](#production)
|
|
37
|
-
- [Pre-Releases](#pre-releases)
|
|
38
|
-
- [🏛 License](#-license)
|
|
27
|
+
The package is **ESM-only** and requires **Node.js >= 22**. It is built with [tsup](https://tsup.egoist.dev); `tsc` is only used to emit type declarations.
|
|
39
28
|
|
|
40
|
-
|
|
29
|
+
ocean.js is part of the [Ocean Protocol](https://oceanprotocol.com) toolset. Questions: [Discord](https://discord.gg/TnXjkR5). Bugs: [open an issue](https://github.com/oceanprotocol/ocean.js/issues/new?assignees=&labels=bug&template=bug_report.md&title=).
|
|
41
30
|
|
|
42
|
-
-
|
|
43
|
-
-
|
|
44
|
-
-
|
|
31
|
+
- [Install](#install)
|
|
32
|
+
- [Quickstart](#quickstart)
|
|
33
|
+
- [Core concepts](#core-concepts)
|
|
34
|
+
- [Common recipes](#common-recipes)
|
|
35
|
+
- [Configuration and networks](#configuration-and-networks)
|
|
36
|
+
- [Architecture](#architecture)
|
|
37
|
+
- [Error handling and gotchas](#error-handling-and-gotchas)
|
|
38
|
+
- [Docs map](#docs-map)
|
|
39
|
+
- [Contributing](#contributing)
|
|
40
|
+
- [License](#license)
|
|
45
41
|
|
|
46
|
-
##
|
|
42
|
+
## Install
|
|
47
43
|
|
|
48
44
|
```bash
|
|
49
|
-
npm
|
|
45
|
+
npm i @oceanprotocol/lib ethers
|
|
50
46
|
```
|
|
51
47
|
|
|
52
|
-
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
- If you have any difficulties or if you have further questions about how to use ocean.js please reach out to us on [Discord](https://discord.gg/TnXjkR5).
|
|
56
|
-
- If you notice any bugs or issues with ocean.js please [open an issue on github](https://github.com/oceanprotocol/ocean.js/issues/new?assignees=&labels=bug&template=bug_report.md&title=).
|
|
57
|
-
- Developers using this library should ensure their tsconfig.json includes the necessary options:
|
|
58
|
-
```
|
|
48
|
+
`ethers` v6 is used for signers and providers. `@oceanprotocol/ddo-js` ships as a runtime dependency of this library; if you import it directly (e.g. `DDOManager`) to build or validate DDOs, add it to your own `package.json` as well (`npm i @oceanprotocol/ddo-js`) so strict package managers such as pnpm resolve it. Developers using this library from TypeScript should ensure their `tsconfig.json` includes the necessary options:
|
|
49
|
+
|
|
50
|
+
```json
|
|
59
51
|
{
|
|
60
|
-
"
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
52
|
+
"compilerOptions": {
|
|
53
|
+
"moduleResolution": "bundler",
|
|
54
|
+
"module": "esnext",
|
|
55
|
+
"esModuleInterop": true,
|
|
56
|
+
"allowSyntheticDefaultImports": true
|
|
57
|
+
}
|
|
64
58
|
}
|
|
65
59
|
```
|
|
66
|
-
|
|
67
|
-
|
|
60
|
+
|
|
61
|
+
`nodenext` works too, but it requires both `"module": "nodenext"` and `"moduleResolution": "nodenext"`. Avoid the older `"node"` (node10) mode — it cannot resolve this package's `exports` map.
|
|
68
62
|
|
|
69
63
|
### ESM only
|
|
70
64
|
|
|
71
|
-
`@oceanprotocol/lib` ships as ES modules only
|
|
72
|
-
its runtime dependencies (the libp2p stack, `multiformats`, `uint8arrays`) are themselves
|
|
73
|
-
ESM-only. Import it:
|
|
65
|
+
`@oceanprotocol/lib` ships as ES modules only. There is no CommonJS build, because most of its runtime dependencies (the libp2p stack, `multiformats`, `uint8arrays`) are themselves ESM-only. Import it:
|
|
74
66
|
|
|
75
67
|
```js
|
|
76
68
|
import { ProviderInstance, ConfigHelper } from '@oceanprotocol/lib'
|
|
77
69
|
```
|
|
78
70
|
|
|
79
|
-
From CommonJS code, use Node's built-in `require(esm)` support, available from **Node
|
|
80
|
-
22.12**:
|
|
71
|
+
From CommonJS code, use Node's built-in `require(esm)` support, available from **Node 22.12**:
|
|
81
72
|
|
|
82
73
|
```js
|
|
83
74
|
const { ConfigHelper } = require('@oceanprotocol/lib')
|
|
84
75
|
```
|
|
85
76
|
|
|
86
|
-
On Node 22.0
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
77
|
+
On Node 22.0 to 22.11 this throws `ERR_REQUIRE_ESM`; use a dynamic `await import(...)` instead, or move the calling module to ESM.
|
|
78
|
+
|
|
79
|
+
## Quickstart
|
|
80
|
+
|
|
81
|
+
Resolve an asset, order access, and download its file. This assumes the asset is already published, has a fixed-rate or dispenser price, and that the wallet holds the needed gas and base token. For the full publish-then-consume walkthrough, see [CodeExamples.md](CodeExamples.md).
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
import { JsonRpcProvider, Wallet } from 'ethers'
|
|
85
|
+
import {
|
|
86
|
+
ConfigHelper,
|
|
87
|
+
Datatoken,
|
|
88
|
+
ProviderInstance,
|
|
89
|
+
orderAsset,
|
|
90
|
+
downloadFile
|
|
91
|
+
} from '@oceanprotocol/lib'
|
|
92
|
+
|
|
93
|
+
const chainId = 11155111 // or a network name such as 'sepolia'
|
|
94
|
+
// sepolia/mainnet/polygon need an Infura project id for config.nodeUri; or set config.nodeUri to your own RPC URL
|
|
95
|
+
const config = new ConfigHelper().getConfig(chainId, process.env.INFURA_PROJECT_ID)
|
|
96
|
+
// config.oceanNodeUri defaults to http://127.0.0.1:8001; getConfig applies NODE_ENDPOINT / NODE_URL (URL or peer ID) automatically
|
|
97
|
+
|
|
98
|
+
const privateKey = process.env.PRIVATE_KEY
|
|
99
|
+
if (!privateKey) throw new Error('PRIVATE_KEY environment variable is not set')
|
|
100
|
+
const signer = new Wallet(privateKey, new JsonRpcProvider(config.nodeUri))
|
|
101
|
+
|
|
102
|
+
// 1. Resolve the DDO from the node
|
|
103
|
+
const did = 'did:op:...'
|
|
104
|
+
const ddo = await ProviderInstance.resolveDdo(config.oceanNodeUri, did)
|
|
105
|
+
|
|
106
|
+
// Select the access service and the datatoken that grants access to it
|
|
107
|
+
const serviceIndex = ddo.services.findIndex((s) => s.type === 'access')
|
|
108
|
+
if (serviceIndex === -1) throw new Error(`Asset ${did} has no access service`)
|
|
109
|
+
const service = ddo.services[serviceIndex]
|
|
110
|
+
const datatokenIndex = ddo.datatokens.findIndex(
|
|
111
|
+
(dt) => dt.address.toLowerCase() === service.datatokenAddress.toLowerCase()
|
|
112
|
+
)
|
|
113
|
+
if (datatokenIndex === -1) throw new Error('No datatoken matches the access service')
|
|
114
|
+
|
|
115
|
+
// 2. Order access (always buys 1 datatoken, then starts the order)
|
|
116
|
+
const datatoken = new Datatoken(signer, chainId, config)
|
|
117
|
+
const tx = await orderAsset(
|
|
118
|
+
ddo,
|
|
119
|
+
signer,
|
|
120
|
+
config,
|
|
121
|
+
datatoken,
|
|
122
|
+
undefined, // providerUrl (defaults to config.oceanNodeUri)
|
|
123
|
+
undefined, // consumerAddress (defaults to the signer)
|
|
124
|
+
undefined, // consumeMarketOrderFee
|
|
125
|
+
undefined, // providerFees (fetched from the node)
|
|
126
|
+
'0', // consumeMarketFixedSwapFee
|
|
127
|
+
datatokenIndex,
|
|
128
|
+
serviceIndex
|
|
129
|
+
)
|
|
130
|
+
// orderAsset returns null if the order fails (bad funds, approvals or provider fees)
|
|
131
|
+
if (!tx) throw new Error('orderAsset failed')
|
|
132
|
+
const receipt = await tx.wait()
|
|
133
|
+
|
|
134
|
+
// 3. Ask the node for the download, then fetch the file
|
|
135
|
+
const download = await ProviderInstance.getDownloadUrl(
|
|
136
|
+
did,
|
|
137
|
+
service.id, // getDownloadUrl takes the service id, not its index
|
|
138
|
+
0, // file index
|
|
139
|
+
receipt.hash, // transferTxId: the order transaction
|
|
140
|
+
config.oceanNodeUri,
|
|
141
|
+
signer
|
|
142
|
+
)
|
|
143
|
+
const file = await downloadFile(download) // works for HTTP (URL) and P2P (buffered response)
|
|
144
|
+
```
|
|
92
145
|
|
|
93
|
-
-
|
|
94
|
-
- Creating Data NFTs and Datatokens
|
|
95
|
-
- Setting up fixed-rate exchanges and dispensers
|
|
96
|
-
- Buying and consuming data assets
|
|
97
|
-
- Using ERC725 key-value store
|
|
146
|
+
`orderAsset` detects whether the service is priced by a fixed-rate exchange or a dispenser, handles the approvals, fetches provider fees from the node, and starts the order.
|
|
98
147
|
|
|
99
|
-
|
|
100
|
-
- Publishing datasets and algorithms for compute
|
|
101
|
-
- Starting free compute jobs
|
|
102
|
-
- Running paid compute jobs with custom resources
|
|
103
|
-
- Checking compute status and retrieving results
|
|
148
|
+
## Core concepts
|
|
104
149
|
|
|
105
150
|
- **[Cheatsheet](Cheatsheet.md)**: Quick reference for common operations
|
|
106
151
|
- Publishing datasets
|
|
107
152
|
- Consuming data
|
|
108
153
|
- Free and paid compute workflows
|
|
109
154
|
- Getting compute job status
|
|
155
|
+
- Node maintenance (drain) mode: `setMaintenance`, `NodeStatus.maintenance`, and detecting rejections with `isMaintenanceError` / `OceanNodeProviderError` / `MAINTENANCE_MODE_CODE`
|
|
156
|
+
- **Data NFT** (ERC721): represents ownership of an asset. Its metadata points at the DDO.
|
|
157
|
+
- **Datatoken** (ERC20): the access token. One datatoken (1.0) buys access to one service. Created from a data NFT.
|
|
158
|
+
- **DDO**: the DID document describing the asset (metadata, services, files, credentials). Schema and validation live in [`@oceanprotocol/ddo-js`](https://github.com/oceanprotocol/ddo.js).
|
|
159
|
+
- **Node**: an [Ocean Node](https://github.com/oceanprotocol/ocean-node) that plays three roles for you: **Provider** (encrypts file URLs, signs fees, serves downloads, runs C2D), **Aquarius** (DDO metadata store and search) and **Indexer** (follows the chain and indexes published assets). In the library these are `ProviderInstance` and `Aquarius`, both pointed at the same node.
|
|
110
160
|
|
|
161
|
+
### HTTP vs P2P transport
|
|
111
162
|
|
|
112
|
-
|
|
163
|
+
The same functions work over both transports. The library inspects the node identifier you pass and routes the call with `isP2pUri()`.
|
|
113
164
|
|
|
114
|
-
|
|
165
|
+
| | HTTP | P2P |
|
|
166
|
+
| --- | --- | --- |
|
|
167
|
+
| Node identifier | URL, e.g. `http://127.0.0.1:8001` | libp2p peer ID or multiaddr, e.g. `16Uiu2HAm...` |
|
|
168
|
+
| Setup | none | optional `ProviderInstance.setupP2P(config)`; call `stopP2P()` / `dispose()` on shutdown in long-lived processes |
|
|
169
|
+
| `getDownloadUrl` returns | a URL string | a buffered `DownloadResponse` |
|
|
170
|
+
| `downloadFile()` | fetches the URL | passes the response through |
|
|
115
171
|
|
|
116
|
-
|
|
172
|
+
Always pass the result of `getDownloadUrl` to `downloadFile()` and your code works on both.
|
|
117
173
|
|
|
118
|
-
|
|
119
|
-
npm install
|
|
120
|
-
npm start
|
|
121
|
-
```
|
|
174
|
+
## Common recipes
|
|
122
175
|
|
|
123
|
-
|
|
176
|
+
Each recipe is a pointer plus the key calls. Complete, tested versions are in the generated guides.
|
|
124
177
|
|
|
125
|
-
|
|
178
|
+
**Publish** ([CodeExamples.md](CodeExamples.md)). Use `createAsset(...)` from `@oceanprotocol/lib`. It creates the data NFT and datatoken, encrypts the files through the node, builds and registers the DDO, and returns the DID. Then wait for indexing:
|
|
126
179
|
|
|
127
|
-
```
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
# auto format all js & css with prettier, taking all configs into account
|
|
132
|
-
npm run format
|
|
180
|
+
```ts
|
|
181
|
+
const did = await createAsset(name, symbol, signer, files, 1, ddo, true, config.oceanNodeUri, config.oceanTokenAddress, aquarius)
|
|
182
|
+
const asset = await aquarius.waitForIndexer(did)
|
|
133
183
|
```
|
|
134
184
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
Test suite for unit & integration tests is setup with [Mocha](https://mochajs.org) as test runner, and [nyc](https://github.com/istanbuljs/nyc) for coverage reporting. A combined coverage report is sent to CodeClimate via the `coverage` GitHub Actions job.
|
|
185
|
+
**Consume** ([CodeExamples.md](CodeExamples.md)). `ProviderInstance.initialize(...)` to get provider fees, `datatoken.startOrder(...)` (or `orderAsset`), then `ProviderInstance.getDownloadUrl(...)` and `downloadFile(...)` as in the quickstart.
|
|
138
186
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
```bash
|
|
142
|
-
git clone https://github.com/oceanprotocol/barge
|
|
143
|
-
cd barge
|
|
187
|
+
**Compute-to-data, free and paid** ([ComputeExamples.md](ComputeExamples.md)).
|
|
144
188
|
|
|
145
|
-
|
|
189
|
+
```ts
|
|
190
|
+
const envs = await ProviderInstance.getComputeEnvironments(config.oceanNodeUri)
|
|
191
|
+
// free tier
|
|
192
|
+
await ProviderInstance.freeComputeStart(config.oceanNodeUri, signer, envId, datasets, algorithm)
|
|
193
|
+
// paid: initializeCompute -> order datasets/algorithm -> computeStart
|
|
194
|
+
await ProviderInstance.initializeCompute(/* assets, algorithm, env, token, validUntil, node, consumer, resources, chainId */)
|
|
195
|
+
await ProviderInstance.computeStart(/* node, signer, env, datasets, algorithm, maxJobDuration, token, resources, chainId */)
|
|
196
|
+
await ProviderInstance.computeStatus(/* ... */)
|
|
146
197
|
```
|
|
147
198
|
|
|
148
|
-
|
|
199
|
+
**Pricing**. Two models are supported: **fixed-rate** (`FixedRateExchange`, or `datatoken.createFixedRate`) and **dispenser** for free access (`Dispenser`, or `datatoken.createDispenser`). There are no other pricing mechanisms in this library.
|
|
149
200
|
|
|
150
|
-
|
|
201
|
+
**Escrow** (paid compute). Funds are deposited into the escrow contract and authorized for the node to lock and charge per job:
|
|
151
202
|
|
|
152
|
-
```
|
|
153
|
-
|
|
203
|
+
```ts
|
|
204
|
+
import { EscrowContract, Datatoken } from '@oceanprotocol/lib'
|
|
205
|
+
const escrow = new EscrowContract(config.escrow, signer, chainId, config)
|
|
206
|
+
// Approve the escrow contract to pull the deposit first (approve scales by the token's decimals)
|
|
207
|
+
await new Datatoken(signer, chainId, config).approve(tokenAddress, config.escrow, '10')
|
|
208
|
+
await escrow.deposit(tokenAddress, '10')
|
|
209
|
+
// also: authorize(...), withdraw(...), getUserFunds(payer, token), getUserTokens(payer)
|
|
154
210
|
```
|
|
155
211
|
|
|
156
|
-
|
|
212
|
+
## Configuration and networks
|
|
157
213
|
|
|
158
|
-
```
|
|
159
|
-
|
|
214
|
+
```ts
|
|
215
|
+
const config = new ConfigHelper().getConfig(11155111) // chainId (number) or name (string)
|
|
160
216
|
```
|
|
161
217
|
|
|
162
|
-
|
|
218
|
+
`getConfig(chainId | networkName, infuraProjectId?)` returns a `Config` with RPC `nodeUri`, `oceanNodeUri` (the Ocean Node, default `http://127.0.0.1:8001`) and the contract addresses (bundled from `@oceanprotocol/contracts`). Some networks (`mainnet`, `polygon`, `sepolia`) ship a keyless Infura `nodeUri` that will not connect on its own: pass your Infura project id as the second argument or set `config.nodeUri` to your own RPC URL. It returns `null` and logs an error for an unknown network.
|
|
163
219
|
|
|
164
|
-
|
|
165
|
-
npm test
|
|
166
|
-
```
|
|
220
|
+
Accepted network names: `mainnet` (1), `polygon` (137), `base` (8453), `optimism` (10), `sepolia` (11155111), `optimism_sepolia` (11155420), `bsc` (56), `energyweb` (246), `moonriver` (1285), `oasis_sapphire` (23294), `oasis_sapphire_testnet` (23295), `pontus-x-devnet` (32456), and `development` (8996, a local [barge](https://github.com/oceanprotocol/barge) stack). Legacy entries that remain in the table but are unmaintained: `mumbai` (80001), `gaiaxtestnet` (2021000) and `gen-x-testnet` (100). `src/config/ConfigHelper.ts` is the authoritative list.
|
|
167
221
|
|
|
168
|
-
|
|
222
|
+
**Environment variables** (Node.js only):
|
|
169
223
|
|
|
170
|
-
|
|
224
|
+
| Variable | Effect |
|
|
225
|
+
| --- | --- |
|
|
226
|
+
| `NODE_ENDPOINT` (or `NODE_URL`) | overrides `oceanNodeUri`. Set it to an `http(s)` URL for HTTP or a peer ID for P2P. |
|
|
227
|
+
| `ADDRESS_FILE` | path to a contracts `address.json` (e.g. written by barge) that overrides the bundled addresses. |
|
|
171
228
|
|
|
172
|
-
|
|
173
|
-
npm run test:unit
|
|
174
|
-
# same thing, but with coverage reporting
|
|
175
|
-
npm run test:unit:cover
|
|
176
|
-
```
|
|
229
|
+
**Auth modes.** Node calls that need authentication accept any of:
|
|
177
230
|
|
|
178
|
-
|
|
231
|
+
- a **signer** (an ethers `Signer`): the library fetches a nonce and signs each request;
|
|
232
|
+
- a **JWT auth token** (`string`): create one with `ProviderInstance.generateAuthToken(signer, nodeUri)` and reuse it, avoiding a signature per call; revoke with `invalidateAuthToken`;
|
|
233
|
+
- a **precomputed signature** object (`{ consumerAddress, nonce, signature }`): for agents or wallets that sign out of process. `generateSignedAuthToken` mints a token from such a signature (P2P only; on HTTP use `generateAuthToken(signer, nodeUri)`).
|
|
179
234
|
|
|
180
|
-
|
|
235
|
+
**Sapphire / confidential EVM.** On `oasis_sapphire` and `oasis_sapphire_testnet` the config sets `sdk: 'oasis'`. Contract calls are wrapped with the Sapphire ethers shim and datatokens use the confidential **Template 4** (`Datatoken4`). Use `useOasisSDK(network)` to check programmatically.
|
|
181
236
|
|
|
182
|
-
|
|
183
|
-
npm run test:integration
|
|
184
|
-
# same thing, but with coverage reporting
|
|
185
|
-
npm run test:integration:cover
|
|
186
|
-
```
|
|
237
|
+
## Architecture
|
|
187
238
|
|
|
188
|
-
|
|
239
|
+
There is no single `Ocean` object. The library is a set of small, composable pieces exported from one barrel (`src/index.ts`: types, config, contracts, services, utils):
|
|
189
240
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
export PRIVATE_KEY='0x<YOUR_PRIVATE_KEY>'
|
|
194
|
-
export PRIVATE_KEY_CONSUMER='0x<YOUR_CONSUMER_PRIVATE_KEY>'
|
|
195
|
-
```
|
|
241
|
+
- **Contract wrappers** (`src/contracts`): `NftFactory`, `Nft`, `Datatoken` (+ `Datatoken4`), `FixedRateExchange`, `Dispenser`, `Router`, `EscrowContract`, `AccessListContract`, `AccesslistFactory`, `EnterpriseFeeCollectorContract`.
|
|
242
|
+
- **Node services** (`src/services`): `ProviderInstance` (a ready-made `BaseProvider`, routing HTTP/P2P) and `Aquarius`.
|
|
243
|
+
- **Flows** (`src/utils`): `createAsset`, `orderAsset`, and helpers for approvals, DDO handling and signatures.
|
|
196
244
|
|
|
197
|
-
|
|
245
|
+
Every state-changing contract method comes in two forms:
|
|
198
246
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
```
|
|
247
|
+
- `foo(..., estimateGas?)` signs, sends and waits for the transaction, returning the ethers `TransactionResponse` (not the receipt — call `await tx.wait()` for that), or `null` if the send fails; pass `estimateGas = true` to get the gas estimate instead of sending;
|
|
248
|
+
- `fooTx(...)` returns the **unsigned** `TransactionRequest`, for custom signing, batching, or smart-account/agent flows.
|
|
202
249
|
|
|
250
|
+
## Error handling and gotchas
|
|
203
251
|
|
|
204
|
-
|
|
252
|
+
- **Node unreachable.** HTTP calls throw on connection failure; P2P calls throw a `P2pError` with a `type` you can branch on (see `classifyP2pError`, `isRetryableP2pError`). Check `oceanNodeUri`, and that the node is reachable from your network. Most node methods accept an `AbortSignal`.
|
|
253
|
+
- **Wrong chain.** `orderAsset` throws if the DDO's `chainId` differs from the signer's network. Make sure `ConfigHelper.getConfig(chainId)` and the signer's RPC point at the same chain as the asset.
|
|
254
|
+
- **Transaction reverted / insufficient balance.** Reverts surface as ethers errors. Check native-token balance for gas and base-token / datatoken balance and approvals (`approve`, `approveWei`) before ordering. Use the `estimateGas` flag to fail early.
|
|
255
|
+
- **Stale nonces.** If you send several transactions from one signer in parallel, serialize them or manage nonces yourself; prefer `await tx.wait()` between dependent steps.
|
|
256
|
+
- **Indexer lag.** A freshly published asset is not resolvable until the node indexes it. Use `aquarius.waitForIndexer(did)` (it polls, `interval` defaults to 30 s) before resolving or ordering.
|
|
257
|
+
- **Provider errors.** Node-side validation failures for compute and downloads are mapped to readable messages (see `src/utils/ProviderErrors.ts`).
|
|
258
|
+
- **Browser bundles.** `fs` is stubbed out; `ADDRESS_FILE` and the `NODE_ENDPOINT` env override are Node-only.
|
|
205
259
|
|
|
206
|
-
##
|
|
260
|
+
## Docs map
|
|
207
261
|
|
|
208
|
-
|
|
262
|
+
| Resource | What it is |
|
|
263
|
+
| --- | --- |
|
|
264
|
+
| [CodeExamples.md](CodeExamples.md) | **Generated** from `test/integration/CodeExamples.test.ts` (`npm run create:guide`). Publish and consume walkthrough. Do not edit by hand. |
|
|
265
|
+
| [ComputeExamples.md](ComputeExamples.md) | **Generated** from `test/integration/ComputeExamples.test.ts` (`npm run create:guidec2d`). Free and paid C2D walkthrough. Do not edit by hand. |
|
|
266
|
+
| [Cheatsheet.md](Cheatsheet.md) | **Hand-written** quick reference. |
|
|
267
|
+
| [`docs/`](docs/README.md) | **Generated** TypeDoc API reference (`npm run docs`). May lag `src/`; source and `dist/types` are authoritative. |
|
|
268
|
+
| [CHANGELOG.md](CHANGELOG.md) | Auto-generated release notes. |
|
|
269
|
+
| [Ocean docs](https://docs.oceanprotocol.com/) | Protocol guides and tutorials. |
|
|
270
|
+
| [Ocean Protocol website](https://oceanprotocol.com) | General information. |
|
|
209
271
|
|
|
210
|
-
|
|
211
|
-
npm run build
|
|
212
|
-
```
|
|
272
|
+
**Upgrading from v8.** v9 is a major release: the library is ESM-only (no CJS/UMD bundle), requires Node >= 22 and ethers v6, talks to a single Ocean Node via `oceanNodeUri` (the separate `providerUri`/`metadataCacheUri`/subgraph configuration is gone) and adds P2P, auth tokens and escrow. See the [v9.0.0 entry in the CHANGELOG](CHANGELOG.md) and update imports and config accordingly.
|
|
213
273
|
|
|
214
|
-
|
|
274
|
+
**Upgrading from v9 to v10.** v10 is largely additive — the publish/consume/compute APIs are unchanged. It bumps to `@oceanprotocol/contracts@^3.2.0` (Escrow v2) and adds the subsidy-provider read wrappers, consumer-selectable subsidy providers on compute/service start, and node maintenance (drain) mode. See the v10 highlights above and the [CHANGELOG](CHANGELOG.md).
|
|
215
275
|
|
|
216
|
-
|
|
276
|
+
**Compatibility.** Contract ABIs and addresses are bundled into the build from `@oceanprotocol/contracts` (`^3.2.0`, a devDependency; consumers do not install it); DDO handling uses the runtime dependency `@oceanprotocol/ddo-js` (`^1.0.0`). [ocean-cli](https://github.com/oceanprotocol/ocean-cli) consumes this library (`^10.0.0`).
|
|
217
277
|
|
|
218
|
-
|
|
278
|
+
## Contributing
|
|
219
279
|
|
|
220
|
-
|
|
280
|
+
See [CLAUDE.md](CLAUDE.md) for repository conventions and structure.
|
|
221
281
|
|
|
222
282
|
```bash
|
|
223
|
-
|
|
283
|
+
nvm use # Node version from .nvmrc (22.x)
|
|
284
|
+
npm ci
|
|
285
|
+
npm run build:metadata # required before type-check and tests (generates src/metadata.json)
|
|
286
|
+
npm run lint # eslint + tsc --noEmit
|
|
287
|
+
npm run test:unit # contract-wrapper + unit tests; needs a local Barge chain + ADDRESS_FILE (see "Testing against barge")
|
|
288
|
+
npm run build # tsup bundles + tsc declarations into dist/
|
|
289
|
+
npm start # tsc watch mode
|
|
224
290
|
```
|
|
225
291
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
- bumps the project version in `package.json`, `package-lock.json`
|
|
229
|
-
- auto-generates and updates the CHANGELOG.md file from commit messages
|
|
230
|
-
- creates a Git tag
|
|
231
|
-
- commits and pushes everything
|
|
232
|
-
- creates a GitHub release with commit messages as description
|
|
233
|
-
- Git tag push will trigger a GitHub Action workflow to do a npm release
|
|
292
|
+
Formatting: `npm run format` (prettier).
|
|
234
293
|
|
|
235
|
-
|
|
294
|
+
### Testing against barge
|
|
236
295
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
For pre-releases, this is required for the first one like `v0.18.0-next.0`:
|
|
296
|
+
Both suites run against a local [Barge](https://github.com/oceanprotocol/barge) stack (Docker on Linux/macOS). `test/unit` deploys and calls the contracts on the local chain (chainId 8996) and reads the deployed addresses from `ADDRESS_FILE`; `test/integration` additionally needs the Ocean Node and indexer. Start Barge and export `ADDRESS_FILE`:
|
|
240
297
|
|
|
241
298
|
```bash
|
|
242
|
-
|
|
299
|
+
git clone https://github.com/oceanprotocol/barge && cd barge && ./start_ocean.sh
|
|
300
|
+
export ADDRESS_FILE="${HOME}/.ocean/ocean-contracts/artifacts/address.json"
|
|
301
|
+
npm run test:integration # defaults to NODE_ENDPOINT=http://127.0.0.1:8001 (HTTP)
|
|
302
|
+
NODE_ENDPOINT=<node peer id> npm run test:integration # same suite over P2P
|
|
243
303
|
```
|
|
244
304
|
|
|
245
|
-
|
|
305
|
+
CI runs the integration suite as an `http` x `p2p` matrix by switching `NODE_ENDPOINT`. Sapphire tests (`npm run test:sapphire`) run against the live Sapphire testnet and need `PRIVATE_KEY` and `PRIVATE_KEY_CONSUMER`. On macOS, barge container IPs may be unreachable; use `http://127.0.0.1:8001` for the node.
|
|
306
|
+
|
|
307
|
+
### Releases
|
|
308
|
+
|
|
309
|
+
Releases are manually triggered with `npm run release` (release-it) from a clean `main`: it bumps the version, regenerates the CHANGELOG and docs, tags, creates a GitHub release, and the tag push triggers the npm publish workflow. A `GITHUB_TOKEN` is required ([setup](https://github.com/release-it/release-it#github-releases)). Pre-releases: `./node_modules/.bin/release-it major|minor|patch --preRelease=next`. Follow semver: breaking changes need a major bump and a migration note.
|
|
246
310
|
|
|
247
|
-
##
|
|
311
|
+
## License
|
|
248
312
|
|
|
249
313
|
```
|
|
250
|
-
Copyright (
|
|
314
|
+
Copyright (C) 2025 Ocean Protocol Foundation
|
|
251
315
|
|
|
252
316
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
253
317
|
you may not use this file except in compliance with the License.
|