Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial branch with api-types replaced in types #186

Merged
merged 10 commits into from
Oct 22, 2020
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ module.exports = {
],
"import/no-extraneous-dependencies": "off",
"import/order": "error",
"no-console": "error",
"no-duplicate-imports": "error",
// "no-magic-numbers": "error",
"no-return-await": "error",
Expand Down
12 changes: 0 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,6 @@ The CENNZnet JavaScript API library for browsers and Node.js.

## Quick Start

You must use **yarn** with @cennznet/api and set the following **resolutions** in your package.json, otherwise your install may break due to breaking changes in downstream package versions.
```
"resolutions": {
"@polkadot/types": "1.2.1",
"@polkadot/metadata": "1.2.1",
"@polkadot/api": "1.2.1",
"@polkadot/api-derive": "1.2.1",
"@polkadot/rpc-core": "1.2.1",
"@polkadot/rpc-provider": "1.2.1",
"@polkadot/jsonrpc": "1.2.1",
}
```
See the [getting started guide](docs/GET_STARTED.md), [example snippets](docs/examples), or the [wiki](https://github.com/cennznet/cennznet/wiki/Javascript-API-Reference) to get started.

# Components
Expand Down
2 changes: 1 addition & 1 deletion jest/initApiRx.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {WsProvider} from '@polkadot/api';
import config from '../config';
import {ApiRx} from '../packages/api/src/ApiRx';
import { TypeRegistry } from '@cennznet/types';
import { TypeRegistry } from '@polkadot/types';
KarishmaBothara marked this conversation as resolved.
Show resolved Hide resolved

const initApiRx = async () => {
const providerUrl = config.wsProvider[`${process.env.TEST_TYPE}`] || 'ws://localhost:9944';
Expand Down
39 changes: 16 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,41 @@
"lint": "eslint . --ext .ts"
},
"devDependencies": {
"@babel/cli": "^7.0.0",
"@babel/core": "^7.7.4",
"@babel/cli": "^7.12.1",
"@babel/core": "^7.11.6",
"@babel/helper-skip-transparent-expression-wrappers": "^7.11.0",
"@babel/helper-validator-identifier": "^7.10.4",
"@babel/plugin-proposal-numeric-separator": "^7.10.4",
"@babel/preset-env": "^7.7.6",
"@babel/runtime-corejs3": "^7.7.6",
"@polkadot/dev": "0.31.1",
"@polkadot/ts": "^0.1.86",
"@types/jest": "^23.3.10",
"@typescript-eslint/eslint-plugin": "^3.10.0",
"@typescript-eslint/parser": "^3.10.0",
"babel-jest": "^24.9.0",
"@types/jest": "^26.0.14",
"@typescript-eslint/eslint-plugin": "^4.4.1",
"@typescript-eslint/parser": "^4.4.1",
"babel-jest": "^26.5.2",
"babel-plugin-conditional-compile": "^0.0.5",
"core-js": "^3.4.8",
"cz-lerna-changelog": "^2.0.2",
"eslint": "^7.7.0",
"eslint-plugin-import": "^2.22.0",
"eslint": "^7.11.0",
"eslint-plugin-import": "^2.22.1",
"husky": "^1.1.1",
"jest": "24.1.0",
"jest-environment-node": "^24.0.0",
"jest": "^26.5.3",
"jest-environment-node": "^26.5.2",
"lerna": "^3.13.1",
"prettier": "^1.13.5",
"pretty-quick": "^1.11.1",
"ts-jest": "^24.0.0",
"ts-jest": "^26.4.1",
"ts-node": "^8.0.3",
"tsconfig-paths": "^3.8.0",
"tslint": "^5.14.0",
"tslint-microsoft-contrib": "^5.2.1",
"typedoc": "^0.14.2",
"typedoc": "^0.19.2",
"typedoc-plugin-monorepo": "https://github.com/ianhe8x/typedoc-plugin-monorepo",
"typescript": "^4.0.2"
"typescript": "^4.0.3",
"cz-lerna-changelog": "^2.0.3",
"semantic-release": "^15.14.0"
},
"resolutions": {
"babel-core": "^7.0.0-bridge.0",
"typescript": "^3.7.2",
"braces": "^2.3.2",
"fstream": "^1.0.12",
"handlebars": "^4.5.3",
"js-yaml": "^3.13.1",
"marked": "^0.6.2",
"tar": "^4.4.8"
"typescript": "^4.0.3"
},
"config": {
"commitizen": {
Expand Down
27 changes: 2 additions & 25 deletions packages/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,12 @@

The CENNZNet JavaScript SDK for browsers, RN and Node.js.

## Quick Start

You must use **yarn** with @cennznet/api and set the following **resolutions** in your package.json, otherwise your install may break due to breaking changes in downstream package versions.

## Install

```
$> npm i --save @cennznet/api
```

At the moment a breaking change in the recent versions of polkadot has prevented us from updating with the recent versions.
That's why in package.json there should be a resolution set so we can make sure the right version of polkadot packages are fetched. Therefore you need to have the following lines in your package.json:
```json
"dependencies": {
"@cennznet/api": "^1.2.1"
},
"resolutions": {
"@polkadot/types": "1.2.1",
"@polkadot/metadata": "1.2.1",
"@polkadot/api": "1.2.1",
"@polkadot/api-derive": "1.2.1",
"@polkadot/rpc-core": "1.2.1",
"@polkadot/rpc-provider": "1.2.1",
"@polkadot/jsonrpc": "1.2.1",
}
```
Please use yarn as package manager to install the above dependecies.
## Usage

The cennznet's main network is residing at `wss://cennznet.unfrastructure.io/public/ws`.
Expand Down Expand Up @@ -64,16 +43,14 @@ We suggest passing provider as a string url, sdk will chose Provider Class based

All `api.tx.<section>.<method>(...)` return CennznetExtrinsic, which have

To use doughnut or feeExchange, it is required to pass them as part of SignerOption
To set fee exchange options on a transaction, it should be included as an additional argument when signing e.g

```
const tx = api.tx.genericAsset.transfer(16000, 'some address', 1000000);

tx.signAndSend('sender address', {doughnut, feeExchangeOpt}, callbackFn);
tx.signAndSend('sender address', {feeExchangeOpt}, callbackFn);
```

[see more](https://github.com/cennznet/cennznet/wiki/Javascript-API-Reference#sending-extrinsics-with-doughnut)

## Dynamic

After connecting to CENNZNet, api will dynamically create queries and transaction methods.
Expand Down
14 changes: 5 additions & 9 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,14 @@
},
"dependencies": {
"@cennznet/types": "^1.2.1",
"@polkadot/api": "^1.1.1",
"@polkadot/metadata": "1.1.1",
"@polkadot/rpc-core": "^1.1.1",
"@polkadot/rpc-provider": "^1.1.1",
"@polkadot/types": "^1.1.1",
"@polkadot/api": "^2.3.1",
"@polkadot/metadata": "^2.3.1",
"@polkadot/rpc-core": "^2.3.1",
"@polkadot/rpc-provider": "^2.3.1",
"@polkadot/types": "^2.3.1",
"eventemitter3": "^4.0.0"
},
"resolution": {
"@polkadot/keyring": "2.3.1"
},
"devDependencies": {
"@cennznet/cennznut-wasm": "^0.0.1-dev.0",
"@types/jest": "^23.3.5",
"jest": "24.1.0",
"rimraf": "^2.6.2",
Expand Down
16 changes: 7 additions & 9 deletions packages/api/src/Api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 Centrality Investments Limited
// Copyright 2019-2020 Centrality Investments Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -12,19 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import Types from '@cennznet/types/injects';
import Types from '@cennznet/types/interfaces/injects';
import { ApiPromise } from '@polkadot/api';
import { ApiOptions as ApiOptionsBase } from '@polkadot/api/types';
import { ApiOptions as ApiOptionsBase, SubmittableExtrinsics } from '@polkadot/api/types';

import derives from './derives';
import rpc from './rpc';
import staticMetadata from './staticMetadata';
import { ApiOptions, Derives, SubmittableExtrinsics } from './types';
import { ApiOptions, Derives } from './types';
import { mergeDeriveOptions } from './util/derives';
import { getProvider } from './util/getProvider';
import { getTimeout } from './util/getTimeout';

export const DEFAULT_TIMEOUT = 10000;
import rpc from './rpc';

export class Api extends ApiPromise {
static async create(options: ApiOptions = {}): Promise<Api> {
Expand All @@ -40,7 +38,7 @@ export class Api extends ApiPromise {

api.isReady.then(res => {
// Remove error listener if API initialization success.
(api as any)._eventemitter.removeListener('error', rejectError);
(api as ApiPromise).off('error', rejectError);
resolve((res as unknown) as Api);
}, reject);

Expand Down Expand Up @@ -73,7 +71,7 @@ export class Api extends ApiPromise {
}
}

async function withTimeout(promise: Promise<Api>, timeoutMs: number = DEFAULT_TIMEOUT): Promise<Api> {
async function withTimeout(promise: Promise<Api>, timeoutMs: number): Promise<Api> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the new default timeout / behaviour here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexsednz ?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same as before. DEFAULT_TIMEOUT is moved to the same file where getTimeout is implemented. So instead of having DEFAULT_TIMEOUT in Api and then importing it in several places including ApiRx (which is considered a sibling to Api, meaning at the same level in the hierarchy) Now we call getTimeout and getTimeout will return the user-specified timeout or otherwise the default value.

if (timeoutMs === 0) {
return promise;
}
Expand Down
50 changes: 25 additions & 25 deletions packages/api/src/ApiRx.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 Centrality Investments Limited
// Copyright 2019-2020 Centrality Investments Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -12,41 +12,41 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import { mergeDeriveOptions } from '@cennznet/api/util/derives';
import Types from '@cennznet/types/injects';
import Types from '@cennznet/types/interfaces/injects';
import { ApiRx as ApiRxBase } from '@polkadot/api';
import { ApiOptions as ApiOptionsBase } from '@polkadot/api/types';
import { fromEvent, Observable, race, throwError } from 'rxjs';
import { switchMap, timeout } from 'rxjs/operators';
import { ApiOptions as ApiOptionsBase, SubmittableExtrinsics } from '@polkadot/api/types';
import { Observable } from 'rxjs';
import { timeout } from 'rxjs/operators';
import { mergeDeriveOptions } from './util/derives';

import rpc from '@cennznet/api/rpc';
import { DEFAULT_TIMEOUT } from './Api';
import derives from './derives';
import staticMetadata from './staticMetadata';
import { ApiOptions, Derives, SubmittableExtrinsics } from './types';
import { ApiOptions, Derives } from './types';
import { getProvider } from './util/getProvider';
import { getTimeout } from './util/getTimeout';
import rpc from './rpc';

export class ApiRx extends ApiRxBase {
static create(options: ApiOptions = {}): Observable<ApiRx> {
const apiRx = new ApiRx(options);

const timeoutMs = getTimeout(options);
const observable: Observable<ApiRx> = new Observable(x => {
apiRx.on('error', (): void => {
x.error(new Error('Connection fail'));
});
apiRx.on('disconnected', (): void => {
x.error(new Error('Disconnected'));
});
apiRx.on('connected', (): void => {
console.info('API has been connected to the endpoint');
});
apiRx.once('ready', (): void => {
x.next(apiRx);
x.complete();
});
});

const rejectError = fromEvent((apiRx as any)._eventemitter, 'error').pipe(
switchMap(err => {
// Disconnect provider if API initialization fails
apiRx.disconnect();

return throwError(new Error('Connection fail'));
})
);
const api$ = (apiRx.isReady as unknown) as Observable<ApiRx>;
// api$.subscribe(api => api.decorateCennznetExtrinsics());

return timeoutMs === 0
? race(api$, rejectError)
: race(api$.pipe(timeout(timeoutMs || DEFAULT_TIMEOUT)), rejectError);
return observable.pipe(timeout(getTimeout(options)));
}

get tx(): SubmittableExtrinsics<'rxjs'> {
Expand All @@ -64,7 +64,7 @@ export class ApiRx extends ApiRxBase {
}
options.metadata = Object.assign(staticMetadata, options.metadata);
options.types = { ...options.types, ...Types };
options.derives = mergeDeriveOptions(derives as any, options.derives);
options.derives = mergeDeriveOptions(derives, options.derives);
options.rpc = { ...(rpc as any), ...options.rpc };

super(options as ApiOptionsBase);
Expand Down
Loading