Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

Commit

Permalink
feat: add block author example
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanleecode committed Jan 21, 2023
1 parent c3e4828 commit 428c0b3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions effects/rpc_known_methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export namespace chain {
export const getBlockHash = rpcCall<[height?: known.ListOrValue<known.NumberOrHex>], U.HexHash>(
"chain_getBlockHash",
)
export const getBlockHeader = rpcCall<[hash?: U.HexHash], known.Header>("chain_getHeader")
}
export namespace system {
export const accountNextIndex = rpcCall<[accountId: known.AccountId], number>(
Expand Down
42 changes: 42 additions & 0 deletions examples/block_author.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as C from "http://localhost:5646/@local/mod.ts"
import {
Session,
System,
} from "http://localhost:5646/@local/proxy/wss:rpc.polkadot.io/pallets/mod.ts"
import { $preDigest } from "http://localhost:5646/@local/proxy/wss:rpc.polkadot.io/types/sp_consensus_babe/digests.ts"
import {
$digestItem,
DigestItem,
} from "http://localhost:5646/@local/proxy/wss:rpc.polkadot.io/types/sp_runtime/generic/digest.ts"
import * as U from "http://localhost:5646/@local/util/mod.ts"

const blockHeader = C.chain.getBlockHeader(C.polkadot)()
const header = U.throwIfError(await blockHeader.run())
const digestedLogs = header.digest.logs.map(C.hex.decode).map($digestItem.decode.bind($digestItem))

function preRuntimeGuard(digestItem: DigestItem): digestItem is DigestItem.PreRuntime {
return digestItem.type === "PreRuntime"
}

const digest = digestedLogs.find(preRuntimeGuard)
if (!digest) {
throw new Error("Missing PreRuntime log")
}

const utf8decoder = new TextDecoder()
const consensusEngineId = utf8decoder.decode(digest.value[0])

if (consensusEngineId !== "BABE") {
throw new Error(`Unsupported consensus engine id: ${consensusEngineId}. Only BABE is supported.`)
}

const preDigest = $preDigest.decode(digest.value[1])
const validators = U.throwIfError(await Session.Validators.entry().read().run())
const pubKey = validators.value[preDigest.value.authorityIndex]!

const ss58EncodedPubKey = U.ss58.encode(
System.SS58Prefix,
pubKey,
)

console.log(ss58EncodedPubKey)
2 changes: 2 additions & 0 deletions examples/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

export * from "./balance.ts"
export * from "./batch.ts"
export * from "./block_author.ts"
export * from "./derived.ts"
export * from "./fee_estimate.ts"
export * from "./first_ten_keys.ts"
Expand All @@ -19,6 +20,7 @@ export * from "./read_era_rewards.ts"
export * from "./read_events.ts"
export * from "./rpc_call.ts"
export * from "./rpc_subscription.ts"
export * from "./smart_contract.ts"
export * from "./ticker.ts"
export * from "./transfer.ts"
export * from "./watch_blocks.ts"
Expand Down

0 comments on commit 428c0b3

Please sign in to comment.