Add api/promise (#251)
* Add api/promise * Observable -> Promise * super(extrinsic) * Add ApiPromise to docs
This commit is contained in:
@@ -2,4 +2,5 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
export { default as ApiPromise } from './promise';
|
||||
export { default as ApiRx } from './rx';
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
// Copyright 2017-2018 @polkadot/api authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { KeyringPair } from '@polkadot/util-keyring/types';
|
||||
import { AnyNumber, AnyU8a } from '@polkadot/types/types';
|
||||
import { ApiPromiseInterface } from './types';
|
||||
|
||||
import { Extrinsic, Hash } from '@polkadot/types/index';
|
||||
|
||||
export default class SubmittableExtrinsic extends Extrinsic {
|
||||
private _api: ApiPromiseInterface;
|
||||
|
||||
constructor (api: ApiPromiseInterface, extrinsic: Extrinsic) {
|
||||
super(extrinsic);
|
||||
|
||||
this._api = api;
|
||||
}
|
||||
|
||||
// TODO We probably want a subscriptable sendAndWatch here as well
|
||||
|
||||
send (): Promise<Hash> {
|
||||
return this._api.rpc.author.submitExtrinsic(this);
|
||||
}
|
||||
|
||||
sign (signerPair: KeyringPair, nonce: AnyNumber, blockHash?: AnyU8a): SubmittableExtrinsic {
|
||||
super.sign(signerPair, nonce, blockHash || this._api.genesisHash);
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
// Copyright 2017-2018 @polkadot/api authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { ApiPromiseInterface, QueryableStorageFunction, QueryableModuleStorage, QueryableStorage, SubmittableExtrinsics, SubmittableModuleExtrinsics, SubmittableExtrinsicFunction } from './types';
|
||||
|
||||
import WsProvider from '@polkadot/rpc-provider/ws';
|
||||
import Rpc from '@polkadot/rpc-core/index';
|
||||
import { Extrinsics, ExtrinsicFunction } from '@polkadot/extrinsics/types';
|
||||
import { Storage } from '@polkadot/storage/types';
|
||||
import { Base } from '@polkadot/types/codec';
|
||||
|
||||
import ApiBase from '../Base';
|
||||
import SubmittableExtrinsic from './SubmittableExtrinsic';
|
||||
import { StorageFunction } from '@polkadot/types/StorageKey';
|
||||
|
||||
export default class ApiPromise extends ApiBase<Rpc, QueryableStorage, SubmittableExtrinsics> implements ApiPromiseInterface {
|
||||
private _isReady: Promise<ApiPromise>;
|
||||
|
||||
/**
|
||||
* @description Creates an ApiPromise instance using the supplied provider. Returns an Promise containing the actual Api instance.
|
||||
* @param wsProvider Optional WebSocket provider that is passed to the class contructor
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import Api from '@polkadot/api/promise';
|
||||
*
|
||||
* Api.create().then(async (api) => {
|
||||
* const timestamp = await api.st.timestamp.now();
|
||||
*
|
||||
* console.log(`lastest block timestamp ${timestamp}`);
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
static create (wsProvider?: WsProvider): Promise<ApiPromise> {
|
||||
return new ApiPromise(wsProvider).isReady;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param wsProvider An optional WebSocket provider from rpc-provider/ws. If not specified, it will default to connecting to the localhost with the default port
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import Api from '@polkadot/api/promise';
|
||||
*
|
||||
* new Api().isReady.then((api) => {
|
||||
* api.rpc.newHead((error, header) => {
|
||||
* console.log(`new block #${header.blockNumber.toNumber()}`);
|
||||
* });
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
constructor (wsProvider?: WsProvider) {
|
||||
super(wsProvider);
|
||||
|
||||
this._isReady = new Promise((resolveReady) =>
|
||||
super.on('ready', () =>
|
||||
resolveReady(this)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Promise that returns the first time we are connected and loaded
|
||||
*/
|
||||
get isReady (): Promise<ApiPromise> {
|
||||
return this._isReady;
|
||||
}
|
||||
|
||||
protected decorateRpc (rpc: Rpc): Rpc {
|
||||
return rpc;
|
||||
}
|
||||
|
||||
protected decorateExtrinsics (extrinsics: Extrinsics): SubmittableExtrinsics {
|
||||
return Object.keys(extrinsics).reduce((result, sectionName) => {
|
||||
const section = extrinsics[sectionName];
|
||||
|
||||
result[sectionName] = Object.keys(section).reduce((result, methodName) => {
|
||||
result[methodName] = this.decorateExtrinsicEntry(section[methodName]);
|
||||
|
||||
return result;
|
||||
}, {} as SubmittableModuleExtrinsics);
|
||||
|
||||
return result;
|
||||
}, {} as SubmittableExtrinsics);
|
||||
}
|
||||
|
||||
private decorateExtrinsicEntry (method: ExtrinsicFunction): SubmittableExtrinsicFunction {
|
||||
const decorated: any = (...args: Array<any>): SubmittableExtrinsic =>
|
||||
new SubmittableExtrinsic(this, method(...args));
|
||||
|
||||
return this.decorateFunctionMeta(method, decorated) as SubmittableExtrinsicFunction;
|
||||
}
|
||||
|
||||
protected decorateStorage (storage: Storage): QueryableStorage {
|
||||
return Object.keys(storage).reduce((result, sectionName) => {
|
||||
const section = storage[sectionName];
|
||||
|
||||
result[sectionName] = Object.keys(section).reduce((result, methodName) => {
|
||||
result[methodName] = this.decorateStorageEntry(section[methodName]);
|
||||
|
||||
return result;
|
||||
}, {} as QueryableModuleStorage);
|
||||
|
||||
return result;
|
||||
}, {} as QueryableStorage);
|
||||
}
|
||||
|
||||
private decorateStorageEntry (method: StorageFunction): QueryableStorageFunction {
|
||||
const decorated: any = (arg: any): Promise<Base | null | undefined> =>
|
||||
this.rpc.state.getStorage([method, arg]);
|
||||
|
||||
return this.decorateFunctionMeta(method, decorated) as QueryableStorageFunction;
|
||||
}
|
||||
}
|
||||
Vendored
+43
@@ -0,0 +1,43 @@
|
||||
// Copyright 2017-2018 @polkadot/api authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { ApiBaseInterface } from '../types';
|
||||
|
||||
import { Observable } from 'rxjs';
|
||||
import { ExtrinsicFunction } from '@polkadot/extrinsics/types';
|
||||
import Rpc from '@polkadot/rpc-core/index';
|
||||
import { Base } from '@polkadot/types/codec';
|
||||
import { Hash, RuntimeVersion } from '@polkadot/types/index';
|
||||
import RuntimeMetadata, { FunctionMetadata } from '@polkadot/types/Metadata';
|
||||
import { StorageFunction } from '@polkadot/types/StorageKey';
|
||||
|
||||
import SubmittableExtrinsic from './SubmittableExtrinsic';
|
||||
|
||||
export interface QueryableStorageFunction extends StorageFunction {
|
||||
(arg?: any): Promise<Base | null | undefined>;
|
||||
}
|
||||
|
||||
export interface QueryableModuleStorage {
|
||||
[index: string]: QueryableStorageFunction;
|
||||
}
|
||||
|
||||
export interface QueryableStorage {
|
||||
[index: string]: QueryableModuleStorage;
|
||||
}
|
||||
|
||||
export interface SubmittableExtrinsicFunction extends ExtrinsicFunction {
|
||||
(...args: any[]): SubmittableExtrinsic;
|
||||
}
|
||||
|
||||
export interface SubmittableModuleExtrinsics {
|
||||
[index: string]: SubmittableExtrinsicFunction;
|
||||
}
|
||||
|
||||
export interface SubmittableExtrinsics {
|
||||
[index: string]: SubmittableModuleExtrinsics;
|
||||
}
|
||||
|
||||
export interface ApiPromiseInterface extends ApiBaseInterface<Rpc, QueryableStorage, SubmittableExtrinsics> {
|
||||
readonly isReady: Promise<ApiPromiseInterface>;
|
||||
}
|
||||
@@ -13,10 +13,7 @@ export default class SubmittableExtrinsic extends Extrinsic {
|
||||
private _api: ApiRxInterface;
|
||||
|
||||
constructor (api: ApiRxInterface, extrinsic: Extrinsic) {
|
||||
super({
|
||||
method: extrinsic.method,
|
||||
signature: extrinsic.signature
|
||||
});
|
||||
super(extrinsic);
|
||||
|
||||
this._api = api;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
// Copyright 2017-2018 @polkadot/api authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import testingPairs from '@polkadot/util-keyring/testingPairs';
|
||||
|
||||
import Api from '../../src/promise';
|
||||
|
||||
const keyring = testingPairs();
|
||||
|
||||
describe.skip('e2e queries', () => {
|
||||
let api;
|
||||
|
||||
beforeEach(async () => {
|
||||
api = await Api.create();
|
||||
|
||||
console.error(api);
|
||||
});
|
||||
|
||||
it('makes the runtime, rpc, state & extrinsics available', () => {
|
||||
expect(api.genesisHash).toBeDefined();
|
||||
expect(api.runtimeMetadata).toBeDefined();
|
||||
expect(api.runtimeVersion).toBeDefined();
|
||||
expect(api.rpc).toBeDefined();
|
||||
expect(api.st).toBeDefined();
|
||||
expect(api.tx).toBeDefined();
|
||||
});
|
||||
|
||||
it('queries state for a balance', async () => {
|
||||
const balance = await api.st.balances.freeBalance(keyring.alice.address());
|
||||
|
||||
expect(
|
||||
balance.isZero()
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it('subscribes to queries', (done) => {
|
||||
api.rpc.chain.newHead((error, header) => {
|
||||
expect(error).toBe(null);
|
||||
expect(header.blockNumber.isZero()).toBe(false);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,29 @@
|
||||
// Copyright 2017-2018 @polkadot/api authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import testingPairs from '@polkadot/util-keyring/testingPairs';
|
||||
|
||||
import Api from '../../src/promise';
|
||||
|
||||
const keyring = testingPairs();
|
||||
|
||||
describe.skip('e2e transfer', () => {
|
||||
let api;
|
||||
|
||||
beforeEach(async () => {
|
||||
api = await Api.create();
|
||||
});
|
||||
|
||||
it('makes a transfer', async () => {
|
||||
const nonce = await api.st.system.accountNonce(keyring.alice.address());
|
||||
const hash = await api.tx.balances
|
||||
.transfer(keyring.bob.address(), 12345)
|
||||
.sign(keyring.alice, nonce)
|
||||
.send();
|
||||
|
||||
expect(
|
||||
hash.toString()
|
||||
).not.toEqual('0x');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user