2018-09-03 10:34:14 +02:00
[![banner ](https://raw.githubusercontent.com/oceanprotocol/art/master/github/repo-banner%402x.png )](https://oceanprotocol.com)
2019-06-20 00:20:09 +02:00
< h1 align = "center" > Squid-js< / h1 >
2018-09-03 10:34:14 +02:00
2018-09-07 14:38:42 +02:00
> 🦑 JavaScript client library for Ocean Protocol
2018-09-03 10:34:14 +02:00
> [oceanprotocol.com](https://oceanprotocol.com)
2018-09-10 12:14:40 +02:00
[![npm ](https://img.shields.io/npm/v/@oceanprotocol/squid.svg )](https://www.npmjs.com/package/@oceanprotocol/squid)
2018-09-06 10:01:57 +02:00
[![Travis (.com) ](https://img.shields.io/travis/com/oceanprotocol/squid-js.svg )](https://travis-ci.com/oceanprotocol/squid-js)
[![GitHub contributors ](https://img.shields.io/github/contributors/oceanprotocol/squid-js.svg )](https://github.com/oceanprotocol/squid-js/graphs/contributors)
2020-01-22 15:56:03 +01:00
[![Maintainability ](https://api.codeclimate.com/v1/badges/ff173cf1c7adc6b21ae5/maintainability )](https://codeclimate.com/github/oceanprotocol/squid-js/maintainability)
[![Test Coverage ](https://api.codeclimate.com/v1/badges/ff173cf1c7adc6b21ae5/test_coverage )](https://codeclimate.com/github/oceanprotocol/squid-js/test_coverage)
2019-06-20 00:20:09 +02:00
[![js oceanprotocol ](https://img.shields.io/badge/js-oceanprotocol-7b1173.svg )](https://github.com/oceanprotocol/eslint-config-oceanprotocol)
2020-05-19 16:23:53 +02:00
[![Dependabot enabled ](https://badgen.net/dependabot/thepracticaldev/dev.to?icon=dependabot )](https://dependabot.com/)
2018-09-03 10:34:14 +02:00
---
2019-06-24 21:11:24 +02:00
**🐲🦑 THERE COULD BE DRAGONS AND SQUIDS. If you run into problems, please open up [a new issue ](https://github.com/oceanprotocol/squid-js/issues ). 🦑🐲**
2018-09-03 10:34:14 +02:00
---
2019-07-31 13:48:57 +02:00
- [Get started ](#get-started )
- [Examples ](#examples )
- [Documentation ](#documentation )
2019-11-15 11:09:08 +01:00
- [Migration Guide ](#migration-guide )
2019-07-31 13:48:57 +02:00
- [Development ](#development )
- [Testing ](#testing )
- [Unit Tests ](#unit-tests )
- [Integration Tests ](#integration-tests )
- [Code Style ](#code-style )
- [Production build ](#production-build )
- [Releases ](#releases )
- [License ](#license )
2018-09-03 10:34:14 +02:00
---
2018-09-06 16:58:39 +02:00
2018-09-10 12:14:40 +02:00
## Get started
Start by adding the package to your dependencies:
```bash
2019-06-24 21:11:24 +02:00
npm i @oceanprotocol/squid
2018-09-10 12:14:40 +02:00
```
2018-09-20 15:05:24 +02:00
The package exposes `Ocean` and `Logger` which you can import in your code like this:
2018-09-10 12:14:40 +02:00
```js
// ES6
2018-09-20 15:05:24 +02:00
import { Ocean, Logger } from '@oceanprotocol/squid'
2018-09-10 12:14:40 +02:00
// ES2015
2018-09-20 15:05:24 +02:00
const { Ocean, Logger } = require('@oceanprotocol/squid')
2018-09-10 12:14:40 +02:00
```
2019-06-24 21:11:24 +02:00
You can then connect to running [Keeper ](https://github.com/oceanprotocol/keeper-contracts ), [Aquarius ](https://github.com/oceanprotocol/aquarius ), [Brizo ](https://github.com/oceanprotocol/brizo ) instances, e.g.:
2018-09-10 12:14:40 +02:00
```js
2018-11-20 15:50:02 +01:00
const ocean: Ocean = await Ocean.getInstance({
// the node of the blockchain to connect to, could also be infura
2019-06-20 00:20:09 +02:00
nodeUri: 'http://localhost:8545',
2018-11-20 15:50:02 +01:00
// the uri of aquarius
2019-06-20 00:20:09 +02:00
aquariusUri: 'http://localhost:5000',
2018-11-20 15:50:02 +01:00
// the uri of brizo
2019-06-20 00:20:09 +02:00
brizoUri: 'http://localhost:8030',
2019-04-18 12:21:45 +02:00
// address that brizo uses
2019-06-20 00:20:09 +02:00
brizoAddress: '0x00bd138abd70e2f00903268f3db08f2d25677c9e'
2018-11-20 15:50:02 +01:00
// the uri to the parity node you want to use for encryption and decryption
2019-06-20 00:20:09 +02:00
parityUri: 'http://localhost:9545',
2018-11-20 15:50:02 +01:00
// the uri of the secret store that holds the keys
2019-06-20 00:20:09 +02:00
secretStoreUri: 'http://localhost:12001'
2018-10-30 12:09:51 +01:00
})
2018-09-10 12:14:40 +02:00
```
2019-06-24 21:11:24 +02:00
For an overview of endpoint configurations making up various Ocean networks, please refer to [`.env.local.example` ](https://github.com/oceanprotocol/commons/blob/master/client/.env.local.example ) from commons.
2020-01-20 11:38:20 +01:00
Optionally, you can initialize an Aquarius connection without relying on the rest of Ocean to be loaded. This is useful for outputting asset metadata stored in Aquarius without the need to configure Web3 and all other Ocean Protocol network connections.
```js
import { Ocean, Aquarius, Logger } from 'squid'
const aquarius = new Aquarius('http://localhost:5000', Logger)
const asset = aquarius.retrieveDDO('did:op:e6fda48e8d814d5d9655645aac3c046cc87528dbc1a9449799e579d7b83d1360')
const ocean = await Ocean.getInstance({ ... })
// Aquarius will still be available under ocean.aquarius, just later
const asset = ocean.aquarius.retrieveDDO('did:op:e6fda48e8d814d5d9655645aac3c046cc87528dbc1a9449799e579d7b83d1360')
```
2018-11-20 15:50:02 +01:00
### Examples
2019-05-15 12:57:52 +02:00
You can see how `squid-js` is used on:
2019-11-15 11:09:08 +01:00
- [Docs: React Tutorial ](https://docs.oceanprotocol.com/tutorials/react-setup/ )
2019-06-20 01:03:40 +02:00
- [Integration test ](/src/integration/ocean/ )
- [Tuna ](https://github.com/oceanprotocol/tuna/tree/develop/node )
2018-11-20 15:50:02 +01:00
2019-01-30 17:37:23 +01:00
## Documentation
2019-01-30 18:14:14 +01:00
**[Docs: squid-js API Reference →](https://docs.oceanprotocol.com/references/squid-js/)**
2019-01-30 17:46:14 +01:00
Alternatively, you can generate the raw TypeDoc documentation locally by running:
```bash
# will output to ./doc folder
npm run doc
```
2019-01-30 17:37:23 +01:00
2019-11-15 11:09:08 +01:00
### Migration Guide
Instructions on how to migrate between breaking versions:
- [Migration Guide ](MIGRATION.md )
2018-09-10 12:14:40 +02:00
## Development
2018-09-06 16:58:39 +02:00
2018-09-06 17:37:11 +02:00
To start development you need to:
2018-09-06 16:58:39 +02:00
2018-09-10 12:14:40 +02:00
```bash
2018-09-03 10:51:02 +02:00
npm i
2018-09-06 16:58:39 +02:00
npm start
```
2019-06-07 13:07:30 +02:00
## Testing
2018-09-06 17:37:11 +02:00
2019-06-07 13:07:30 +02:00
### Unit Tests
2019-10-31 14:41:07 +01:00
For unit tests, running [`ganache-cli` ](https://github.com/trufflesuite/ganache-cli ) is required before starting the tests. It's best to start it on a different port so it doesn't clash with anything running in Barge:
2019-06-07 13:07:30 +02:00
```bash
npm i -g ganache-cli
2019-10-31 14:41:07 +01:00
ganache-cli --port 18545
export ETH_PORT=18545
2019-06-07 13:07:30 +02:00
```
To start unit tests, run:
2018-09-06 16:58:39 +02:00
```bash
2019-06-07 13:07:30 +02:00
npm test
2018-09-03 10:51:02 +02:00
```
2019-06-07 13:07:30 +02:00
or to watch for changes:
2018-09-06 17:44:17 +02:00
```bash
2018-11-20 15:50:02 +01:00
npm run test:watch
2018-09-06 17:44:17 +02:00
```
2019-06-07 13:07:30 +02:00
to create code coverage information:
2018-09-06 17:44:17 +02:00
```bash
2018-11-20 15:50:02 +01:00
npm run test:cover
2018-09-06 17:44:17 +02:00
```
2019-06-07 13:07:30 +02:00
### Integration Tests
2020-01-30 22:08:18 +01:00
A locally running Ocean network is required. To do so before running the tests, use [Barge ](https://github.com/oceanprotocol/barge ):
2018-09-06 17:44:17 +02:00
2019-06-07 13:07:30 +02:00
```bash
git clone https://github.com/oceanprotocol/barge
cd barge
2019-10-08 13:10:59 +02:00
./start_ocean.sh --no-commons
2019-06-07 13:07:30 +02:00
```
2019-01-07 17:31:13 +01:00
2019-06-07 13:07:30 +02:00
In another terminal window, run this script and export the seed phrase:
```bash
# copies the contract artifacts once the local Ocean network is up and running
2019-07-31 13:48:57 +02:00
./scripts/keeper.sh
2019-06-07 13:07:30 +02:00
# export Spree accounts seed phrase
export SEED_WORDS="taxi music thumb unique chat sand crew more leg another off lamp"
```
Once everything is up, run the integration tests:
```bash
2020-01-20 11:44:13 +01:00
# integration tests work with the spree network and the SEED_WORDS in previous step are required.
# Make sure to reset `ETH_PORT` to 8545 (or whatever port is used in `spree1)
2020-01-30 22:08:18 +01:00
npm run test:integration
2019-06-07 13:07:30 +02:00
```
to generate code coverage information during test, run:
```bash
2020-01-30 22:08:18 +01:00
npm run test:integration:cover
2019-06-07 13:07:30 +02:00
```
2019-06-20 01:35:36 +02:00
## Code Style
Project follows [eslint-config-oceanprotocol ](https://github.com/oceanprotocol/eslint-config-oceanprotocol ). For linting and auto-formatting you can use:
```bash
# lint all ts with eslint
npm run lint
# auto format all ts with prettier, taking all configs into account
npm run format
```
2019-06-07 13:07:30 +02:00
## Production build
2018-09-06 17:44:17 +02:00
2019-06-20 01:03:40 +02:00
To create a production build:
2018-09-06 17:44:17 +02:00
```bash
2018-11-20 15:50:02 +01:00
npm run build
2018-09-06 17:44:17 +02:00
```
2019-06-07 13:07:30 +02:00
## Releases
2018-11-20 15:50:02 +01:00
2019-06-20 01:03:40 +02:00
From a clean `master` branch you can run any release task doing the following:
2018-11-20 15:50:02 +01:00
2019-06-20 01:03:40 +02:00
- bumps the project version in `package.json` , `package-lock.json`
- auto-generates and updates the CHANGELOG.md file from commit messages
- creates a Git tag
- commits and pushes everything
- creates a GitHub release with commit messages as description
- Git tag push will trigger Travis to do a npm release
You can execute the script using arguments to bump the version accordingly:
- To bump a patch version: `npm run release`
- To bump a minor version: `npm run release minor`
- To bump a major version: `npm run release major`
2018-09-06 17:44:17 +02:00
2019-06-20 01:03:40 +02:00
For the GitHub releases steps a GitHub personal access token, exported as `GITHUB_TOKEN` is required. [Setup ](https://github.com/release-it/release-it#github-releases )
2018-11-20 15:50:02 +01:00
2018-09-03 10:34:14 +02:00
## License
2019-06-07 13:07:30 +02:00
```text
Copyright 2019 Ocean Protocol Foundation Ltd.
2018-09-03 10:34:14 +02:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
2019-06-20 00:20:09 +02:00
limitations under the License.
2019-07-31 13:48:57 +02:00
```