[Docs] Go through and update existing ApiRx examples (#616)

* [Docs] Go through and update existing ApiRx examples

* [Docs] update existing ApiRx examples -> formatting

* [Docs] Fix name collision in package.json
This commit is contained in:
Stefanie Doll
2019-01-22 21:32:25 +01:00
committed by Jaco Greeff
parent 2d6001197b
commit bc697d9449
3 changed files with 46 additions and 31 deletions
+44 -29
View File
@@ -1,5 +1,3 @@
@polkadot/api/rx
================
@@ -55,14 +53,15 @@ new ApiRx(provider)
api.query.timestamp.now()
])
)
.subscribe(([blockPeriod, timestamp]) => {
const elapsed = last
? `, ${timestamp.toNumber() - last}s since last`
: '';
)
.subscribe(([blockPeriod, timestamp]) => {
const elapsed = last
? `, ${timestamp.toNumber() - last}s since last`
: '';
last = timestamp.toNumber();
console.log(`timestamp ${timestamp}${elapsed} (${blockPeriod}s target)`);
}));
last = timestamp.toNumber();
console.log(`timestamp ${timestamp}${elapsed} (${blockPeriod}s target)`);
});
```
@@ -70,8 +69,13 @@ new ApiRx(provider)
Submitting a transaction -
```javascript
import { first, switchMap } from 'rxjs/operators';
import ApiRx from '@polkadot/api/rx';
// import the test keyring (already has dev keys for Alice, Bob, Charlie, Eve & Ferdie)
import testingPairs from '@polkadot/keyring/testingPairs';
const keyring = testingPairs();
// get api via Promise
const api = await ApiRx.create().toPromise();
@@ -79,20 +83,23 @@ const api = await ApiRx.create().toPromise();
api.query.system
.accountNonce(keyring.alice.address())
.pipe(
// pipe nonce into transfer
switchMap((nonce) =>
api.tx.balances
// create transfer
.transfer(keyring.bob.address(), 12345)
// sign the transcation
.sign(keyring.alice, nonce)
// send the transaction
.send()
)
first(),
// pipe nonce into transfer
switchMap((nonce) =>
api.tx.balances
// create transfer
.transfer(keyring.bob.address(), 12345)
// sign the transcation
.sign(keyring.alice, nonce)
// send the transaction
.send()
)
)
// subscribe to overall result
.subscribe((hash) => {
console.log(`submitted with hash ${hash}`);
.subscribe(({ status, type }) => {
if (type === 'Finalised') {
console.log('Completed at block hash', status.asFinalised.toHex());
}
});
```
@@ -124,13 +131,17 @@ api.query.system
*__example__*:
```javascript
import { switchMap } from 'rxjs/operators';
import Api from '@polkadot/api/rx';
new Api().isReady.subscribe((api) => {
api.rpc.subscribeNewHead().subscribe((header) => {
new Api().isReady
.pipe(
switchMap((api) =>
api.rpc.chain.subscribeNewHead()
))
.subscribe((header) => {
console.log(`new block #${header.blockNumber.toNumber()}`);
});
});
```
**Parameters:**
@@ -322,8 +333,8 @@ gettx(): [SubmittableExtrinsics](../interfaces/_types_.submittableextrinsics.md)
api.tx.balances
.transfer(<recipientId>, <balance>)
.sign(<keyPair>, <accountNonce>, <blockHash (optional)>)
.send((status) => {
console.log('tx status', status);
.send(({status}) => {
console.log('tx status', status.asFinalised.toHex());
});
```
@@ -414,13 +425,17 @@ ___
*__example__*:
```javascript
const { switchMap } = require('rxjs/operators');
import Api from '@polkadot/api/rx';
Api.create().subscribe((api) => {
api.query.timestamp.now.subscribe((timestamp) => {
Api.create()
.pipe(
switchMap((api) =>
api.query.timestamp.now()
))
.subscribe((timestamp) => {
console.log(`lastest block timestamp ${timestamp}`);
});
});
```
**Parameters:**
+1 -1
View File
@@ -1,7 +1,7 @@
// Import the API
const { ApiRx } = require('@polkadot/api');
// Import dependencies from RxJs
const { combineLatest, of, tap } = require('rxjs');
const { combineLatest, of } = require('rxjs');
const { first, switchMap } = require('rxjs/operators');
// Our address for Alice on the dev chain
@@ -1,5 +1,5 @@
{
"name": "10_upgrade_chain",
"name": "rx_10_upgrade_chain",
"version": "0.2.0",
"description": "Example showing how to upgrade via upgradeKey",
"main": "index.js",