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

Commit

Permalink
feat: storageRune.size method + sizeTree pattern (#657)
Browse files Browse the repository at this point in the history
  • Loading branch information
harrysolovay authored Feb 27, 2023
1 parent 9cb3352 commit c846f1e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions _tasks/dnt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ await Promise.all([
}, {
name: "./patterns/identity",
path: "./patterns/identity.ts",
}, {
name: "./patterns/size_tree",
path: "./patterns/sizeTree.ts",
}, {
name: "./server",
path: "./server/mod.ts",
Expand Down
6 changes: 6 additions & 0 deletions examples/size_tree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { sizeTree } from "capi/patterns/sizeTree.ts"
import { chain } from "polkadot_dev/mod.ts"

const result = await sizeTree(chain).run()

console.log(result)
11 changes: 11 additions & 0 deletions fluent/StorageRune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ export class StorageRune<in out K extends unknown[], out V, out U> extends Rune<
this.$value = this.pallet.metadata.codec(this.into(ValueRune).access("value"))
}

size<X>(...[partialKey, blockHash]: RunicArgs<X, [partialKey?: unknown[], blockHash?: HexHash]>) {
return this.pallet.metadata.chain.connection
.call(
"state_getStorageSize",
this.$key.encoded(Rune.resolve(partialKey).map((x) => x ?? [])).map(hex.encode),
blockHash,
)
.unhandle(null)
.rehandle(null, () => Rune.constant(undefined))
}

entryPageRaw<X>(
...[count, partialKey, start, blockHash]: RunicArgs<X, [
count: number,
Expand Down
23 changes: 23 additions & 0 deletions patterns/sizeTree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Chain, ChainRune, HexHash, MetaRune, Rune, RunicArgs, ValueRune } from "../mod.ts"

export function sizeTree<U, C extends Chain, X>(
chain: ChainRune<U, C>,
...[blockHash]: RunicArgs<X, [blockHash?: HexHash]>
) {
const metadata = chain.metadata(blockHash)
return metadata
.into(ValueRune)
.map(({ pallets }) =>
Rune.rec(Object.fromEntries(pallets.map((pallet) => [
pallet.name,
Rune.rec(Object.fromEntries(
pallet.storage?.entries.map((entry) => [
entry.name,
metadata.pallet(pallet.name).storage(entry.name).size([], blockHash),
]) || [],
)),
])))
)
.into(MetaRune)
.flat()
}

0 comments on commit c846f1e

Please sign in to comment.