calculate encoded key

This commit is contained in:
Victor Oliva
2024-10-24 13:56:26 +02:00
parent 1aac787954
commit 4efd7dc998
3 changed files with 98 additions and 7 deletions
+10 -1
View File
@@ -45,12 +45,19 @@ export const Storage = withSubscribe(() => {
}
if (storageEntry.tag === "plain") {
return setSelectedEntry({ value: storageEntry.value, key: [] })
return setSelectedEntry({
value: storageEntry.value,
key: [],
pallet: pallet!,
entry: entry!,
})
}
if (storageEntry.value.hashers.length === 1) {
return setSelectedEntry({
value: storageEntry.value.value,
key: [storageEntry.value.key],
pallet: pallet!,
entry: entry!,
})
}
@@ -67,6 +74,8 @@ export const Storage = withSubscribe(() => {
setSelectedEntry({
key,
value: storageEntry.value.value,
pallet: pallet!,
entry: entry!,
})
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedPallet, entry])
+86 -6
View File
@@ -1,15 +1,25 @@
import SliderToggle from "@/components/Toggle"
import { useStateObservable, withDefault } from "@react-rxjs/core"
import { createSignal } from "@react-rxjs/utils"
import { state, useStateObservable, withDefault } from "@react-rxjs/core"
import { combineKeys, createSignal } from "@react-rxjs/utils"
import { FC } from "react"
import { filter, map, scan, startWith, switchMap } from "rxjs"
import { combineLatest, filter, map, scan, startWith, switchMap } from "rxjs"
import { selectedEntry$ } from "./storage.state"
import { NOTIN } from "@codec-components"
import { ActionButton } from "@/components/ActionButton"
import { lookup$ } from "@/chain.state"
import { getDynamicBuilder } from "@polkadot-api/metadata-builders"
import { CopyText } from "@/components/Copy"
import { twMerge } from "tailwind-merge"
export const StorageQuery: FC = () => {
const isReady = useStateObservable(isReady$)
return (
<div>
<>
<StorageKeysInput />
</div>
<KeyDisplay />
<ActionButton disabled={!isReady}>Query</ActionButton>
</>
)
}
@@ -39,6 +49,36 @@ const keysEnabled$ = keys$.pipeState(
withDefault(0),
)
const [keyValueChange$, setKeyValue] = createSignal<{
idx: number
value: unknown | NOTIN
}>()
const keyValues$ = keys$.pipeState(
switchMap((keys) => {
const values: unknown[] = keys.map(() => NOTIN)
return keyValueChange$.pipe(
scan((acc, change) => {
const newValue = [...acc]
newValue[change.idx] = change.value
return newValue
}, values),
startWith(values),
)
}),
withDefault([] as unknown[]),
)
const isReady$ = state(
combineLatest([keyValues$, keysEnabled$]).pipe(
map(
([keyValues, keysEnabled]) =>
keyValues.length >= keysEnabled &&
keyValues.slice(0, keysEnabled).every((v) => v !== NOTIN),
),
),
false,
)
const StorageKeysInput: FC = () => {
const keys = useStateObservable(keys$)
const keysEnabled = useStateObservable(keysEnabled$)
@@ -63,8 +103,48 @@ const StorageKeyInput: FC<{ type: number; disabled: boolean }> = ({
disabled,
}) => {
return (
<div>
<div className="border-l p-2">
Key type={type} enabled={String(!disabled)}
</div>
)
}
const encodedKey$ = state(
combineLatest([lookup$, selectedEntry$, keyValues$, keysEnabled$]).pipe(
map(([lookup, selectedEntry, keyValues, keysEnabled]) => {
const args = keyValues.slice(0, keysEnabled)
if (
keyValues.length < keysEnabled ||
!args.every((v) => v !== NOTIN) ||
!selectedEntry
) {
return null
}
const codec = getDynamicBuilder(lookup).buildStorage(
selectedEntry.pallet,
selectedEntry.entry,
)
return codec.enc(...args)
}),
),
null,
)
const KeyDisplay: FC = () => {
const key = useStateObservable(encodedKey$)
return (
<div className="flex w-full overflow-hidden bg-polkadot-800 p-2 gap-2 rounded items-center">
<div className="flex-shrink-0 text-polkadot-200">Encoded key:</div>
<div
className={twMerge(
"flex-1 overflow-hidden whitespace-nowrap text-ellipsis text-sm tabular-nums",
key === null ? "text-slate-400" : null,
)}
>
{key ?? "Fill in all the storage keys to calculate the encoded key"}
</div>
<CopyText text={key ?? ""} disabled={key === null} />
</div>
)
}
+2
View File
@@ -2,6 +2,8 @@ import { state } from "@react-rxjs/core"
import { createSignal } from "@react-rxjs/utils"
export type StorageMetadataEntry = {
pallet: string
entry: string
key: number[]
value: number
}