docs: add examples from polkadot

This commit is contained in:
ian.he
2019-04-12 17:40:12 +12:00
parent f103863fdd
commit c14a071b74
59 changed files with 462 additions and 74 deletions
@@ -0,0 +1,5 @@
# Simple Connect
The following example shows how to instantiate a Polkadot API object and use it to connect to a node using ApiRx.
<<< @/docs/examples/rx/01_simple_connect/index.js
@@ -0,0 +1,26 @@
// Required imports
const { zip } = require('rxjs');
const { ApiRx } = require('@polkadot/api');
const { WsProvider } = require('@polkadot/rpc-provider');
function main () {
// Initialise the provider to connect to the local node
const provider = new WsProvider('ws://127.0.0.1:9944');
// Create the API and wait until ready
const api = await ApiRx.create(provider).toPromise();
// We're using RxJs 'zip()' combination operator to get the emitted values
// of multiple observables as an array
zip(
api.rpc.system.chain(),
api.rpc.system.name(),
api.rpc.system.version()
)
// Then we subscribe to the result
.subscribe(([chain, nodeName, nodeVersion]) => {
console.log(`You are connected to chain ${chain} using ${nodeName} v${nodeVersion}`);
});
}
main().catch(console.error).finally(() => process.exit());
@@ -11,8 +11,7 @@
},
"dependencies": {
"@polkadot/api": "^0.43.6",
"@polkadot/rpc-provider": "^0.43.6"
"@polkadot/api": "^0.53.1"
},
"devDependencies": {
"rimraf": "^2.6.2"