Add documentation and examples (#145)
* Add documentation * Justification u8a decoding and json encoding (#146) * Decode Justifications * Json encoding for justifications * Bump deps * Allow extrinsic length encoding to operate on u8a[] (#148) * Fix config and move generated doc to a subfolder * Cleanup extrinsic encoding (#150) * Cleanup extrinsic encoding * Removed now-defunct type * Add uncheckedLength encoding * Add examples * Ignore package-lock since we use yarn * Cleanup examples * Add gitbook config and some base files for the doc * Do not make full doc mandatory for now * Add contributing guide, polish doc and samples * Update the deps of all samples and add yarn scripts Fix and regenerate the doc * Fix Contributing guide
This commit is contained in:
Executable
+42
@@ -0,0 +1,42 @@
|
||||
const Api = require('@polkadot/api').default
|
||||
const WsProvider = require('@polkadot/api-provider/ws').default
|
||||
const extrinsics = require('@polkadot/extrinsics').default
|
||||
const encodeExtrinsic = require('@polkadot/extrinsics/codec/encode/uncheckedLength').default
|
||||
const encodeLength = require('@polkadot/extrinsics/codec/encode/length').default
|
||||
const Keyring = require('@polkadot/util-keyring').default
|
||||
const storage = require('@polkadot/storage').default
|
||||
const BN = require('bn.js')
|
||||
|
||||
const Encoder = new TextEncoder()
|
||||
const keyring = new Keyring()
|
||||
const provider = new WsProvider('ws://127.0.0.1:9944')
|
||||
const api = new Api(provider)
|
||||
|
||||
async function getAccountIndex (address) {
|
||||
return api.state.getStorage([storage.system.public.accountIndexOf, address])
|
||||
}
|
||||
|
||||
async function transfer (keyRingFrom, addressTo, amount) {
|
||||
const nextAccountIndex = await getAccountIndex(keyRingFrom.address())
|
||||
|
||||
// encode the call for signing
|
||||
const encoded = encodeExtrinsic(
|
||||
keyRingFrom,
|
||||
nextAccountIndex,
|
||||
extrinsics.staking.public.transfer,
|
||||
[addressTo, amount]
|
||||
)
|
||||
|
||||
await api.author.submitExtrinsic(encoded)
|
||||
.catch(e => console.log)
|
||||
}
|
||||
|
||||
const Alice = keyring.addFromSeed(Encoder.encode('Alice '))
|
||||
const addressBob = '5Gw3s7q4QLkSWwknsiPtjujPv3XM4Trxi5d4PgKMMk3gfGTE'
|
||||
const amount = new BN(999) // on dev chain, the fee is 1, that makes it a round 1000
|
||||
|
||||
console.log(`Crafting and sending an extrinsic for Alice to send Bob ${amount} DOTs`)
|
||||
transfer(Alice, addressBob, amount)
|
||||
.then(() => console.log('Done'))
|
||||
.catch((e) => console.log(e))
|
||||
.finally(_ => process.exit(0))
|
||||
Reference in New Issue
Block a user