diff --git a/.papi/descriptors/package.json b/.papi/descriptors/package.json index 79e4920..3462628 100644 --- a/.papi/descriptors/package.json +++ b/.papi/descriptors/package.json @@ -1,5 +1,5 @@ { - "version": "0.1.0-autogenerated.403250971526209557", + "version": "0.1.0-autogenerated.11438231719699111413", "name": "@polkadot-api/descriptors", "files": [ "dist" diff --git a/.papi/metadata/polkadot_people.scale b/.papi/metadata/polkadot_people.scale index db7c44d..a039769 100644 Binary files a/.papi/metadata/polkadot_people.scale and b/.papi/metadata/polkadot_people.scale differ diff --git a/.papi/polkadot-api.json b/.papi/polkadot-api.json index 63e2885..a570476 100644 --- a/.papi/polkadot-api.json +++ b/.papi/polkadot-api.json @@ -6,7 +6,7 @@ "chain": "polkadot_people", "metadata": ".papi/metadata/polkadot_people.scale", "genesis": "0x67fa177a097bfa18f77ea95ab56e9bcdfeb0e5b8a40e46298bb93e16b6fc5008", - "codeHash": "0x19d152fef983bf5be332cd07dc6f5c7291d842a4e2d5501eca02a86c10df211d" + "codeHash": "0x3f0652ba1159157eff567b8e0e5987c8fc67a614f0909ce0f65f0d8a2b532864" } } } diff --git a/src/pages/Extrinsics/Analyzer/ExtrinsicAnalyzer.tsx b/src/pages/Extrinsics/Analyzer/ExtrinsicAnalyzer.tsx new file mode 100644 index 0000000..029c571 --- /dev/null +++ b/src/pages/Extrinsics/Analyzer/ExtrinsicAnalyzer.tsx @@ -0,0 +1,41 @@ +import { TextInputField } from "@/components/TextInputField" +import { useHashParamState } from "@/hashParams" +import { Subscribe } from "@react-rxjs/core" +import { FC } from "react" +import { BlockPicker } from "../../Storage/BlockPicker" +import { ExtrinsicDecoder, extrinsicDecoder$ } from "./ExtrinsicDecoder" + +export const ExtrinsicAnalyzer: FC = () => { + const [extrinsicHex, setExtrinsicHex] = useHashParamState( + "extrinsic", + () => "", + ) + + return ( +
+

Analyze Extrinsic

+
+
+ +
+
+ +
+
+ + {extrinsicHex ? : null} + +
+ ) +} diff --git a/src/pages/Extrinsics/Analyzer/ExtrinsicDecoder.tsx b/src/pages/Extrinsics/Analyzer/ExtrinsicDecoder.tsx new file mode 100644 index 0000000..1156135 --- /dev/null +++ b/src/pages/Extrinsics/Analyzer/ExtrinsicDecoder.tsx @@ -0,0 +1,104 @@ +import { CopyBinary } from "@/codec-components/ViewCodec/CopyBinary" +import { JsonDisplay } from "@/components/JsonDisplay" +import { DecodedExtrinsic, getExtrinsicDecoder } from "@polkadot-api/tx-utils" +import { state, useStateObservable } from "@react-rxjs/core" +import { HexString, TxCallData } from "polkadot-api" +import { toHex } from "polkadot-api/utils" +import { FC, useMemo } from "react" +import { map, merge } from "rxjs" +import { Sender, SignedExtensions } from "../../Explorer/Detail/Extrinsic" +import { AnalyzeMortality, analyzeMortality$ } from "./Mortality" +import { AnalyzePriority, analyzePriority$ } from "./Priority" +import { selectedBlock$ } from "./selectedBlock" + +export const extDecoder$ = state( + selectedBlock$.pipe( + map((block) => getExtrinsicDecoder(block.ctx.metadataRaw)), + ), +) + +export const ExtrinsicDecoder: FC<{ + extrinsic: HexString +}> = ({ extrinsic }) => { + const extrinsicDecoder = useStateObservable(extDecoder$) + const decodeResult = useMemo(() => { + try { + return { + type: "success" as const, + value: extrinsicDecoder(extrinsic), + } + } catch (ex) { + console.error(ex) + return { + type: "error" as const, + value: ex as Error, + } + } + }, [extrinsicDecoder, extrinsic]) + + if (decodeResult.type === "error") { + return
Can't decode: {decodeResult.value.message}
+ } + + const decoded = decodeResult.value + + return ( +
+

+ {decoded.type} Transaction v{decoded.version} +

+ {decoded.type === "signed" ? ( + + ) : decoded.type === "general" ? ( +
TODO
+ ) : ( + + )} + +
+ ) +} + +const SignedInfo: FC<{ + extrinsic: HexString + decoded: DecodedExtrinsic & { type: "signed" } +}> = ({ extrinsic, decoded }) => { + const mortality = decoded.extra.CheckMortality + const txPayment = + decoded.extra.ChargeAssetTxPayment ?? decoded.extra.ChargeTxPayment + + return ( +
+ + + {mortality ? : null} + +
+ ) +} + +const CallData: FC<{ call: TxCallData; callData: Uint8Array }> = ({ + call, + callData, +}) => ( +
+
+
+ {call.type}.{call.value.type} +
+
+
+ {toHex(callData)} +
+ +
+
+ +
+) + +export const extrinsicDecoder$ = merge( + extDecoder$, + analyzeMortality$, + analyzePriority$, +) diff --git a/src/pages/Extrinsics/Analyzer/Mortality.tsx b/src/pages/Extrinsics/Analyzer/Mortality.tsx new file mode 100644 index 0000000..25f0c4b --- /dev/null +++ b/src/pages/Extrinsics/Analyzer/Mortality.tsx @@ -0,0 +1,66 @@ +import { client$ } from "@/state/chains/chain.state" +import { polkadot_people } from "@polkadot-api/descriptors" +import { state, useStateObservable } from "@react-rxjs/core" +import { FC } from "react" +import { combineLatest, map, switchMap } from "rxjs" +import { selectedBlockHex$ } from "./selectedBlock" + +const selectedBlockNumber$ = state( + combineLatest([selectedBlockHex$, client$]).pipe( + switchMap(([selectedBlock, client]) => + selectedBlock + ? (client + .getTypedApi(polkadot_people) + .query.System.Number.getValue({ + at: selectedBlock, + }) + .catch(() => null) as Promise) + : client.finalizedBlock$.pipe(map((block) => block.number)), + ), + ), +) + +export const AnalyzeMortality: FC<{ + mortality: { type: string; value: number } +}> = ({ mortality }) => { + const selectedBlockNumber = useStateObservable(selectedBlockNumber$) + + if (mortality.type === "Immortal") return null + const parseResult = /^Mortal(\d+)$/.exec(mortality.type) + if (parseResult === null) return null + const first = BigInt(parseResult[1]) + const second = BigInt(mortality.value) + // from polkadot-sdk primitives runtime generic era fn decode + const encoded = first + (second << 8n) + const period = Number(2n << (encoded % (1n << 4n))) + const factor = period >> 12 || 1 + const phase = Number(encoded >> 4n) * factor + + // From fn birth + const birthBlock = selectedBlockNumber + ? Math.floor((Math.max(selectedBlockNumber, phase) - phase) / period) * + period + + phase + : null + + const referencedBlocks = birthBlock + ? new Array(5) + .fill(0) + .map((_, i) => (birthBlock - period * i).toLocaleString()) + : null + + return ( +
+ Mortality: period={period} phase={phase}{" "} + {referencedBlocks ? ( + <> + currentBlock={selectedBlockNumber?.toLocaleString()} nextPeriod= + {(birthBlock! + period).toLocaleString()} oldPeriods= + {referencedBlocks.join(" ")} + + ) : null} +
+ ) +} + +export const analyzeMortality$ = selectedBlockNumber$ diff --git a/src/pages/Extrinsics/Analyzer/Priority.tsx b/src/pages/Extrinsics/Analyzer/Priority.tsx new file mode 100644 index 0000000..19f7b6c --- /dev/null +++ b/src/pages/Extrinsics/Analyzer/Priority.tsx @@ -0,0 +1,116 @@ +import { client$ } from "@/state/chains/chain.state" +import { polkadot_people } from "@polkadot-api/descriptors" +import { state, useStateObservable } from "@react-rxjs/core" +import { Binary, HexString } from "polkadot-api" +import { FC, useEffect, useState } from "react" +import { combineLatest, firstValueFrom, switchMap } from "rxjs" +import { selectedBlockHex$ } from "./selectedBlock" + +const maxBlockSize$ = state( + combineLatest([selectedBlockHex$, client$]).pipe( + switchMap(([selectedBlock, client]) => + Promise.all([ + client.getTypedApi(polkadot_people).constants.System.BlockLength({ + at: selectedBlock, + }), + client.getTypedApi(polkadot_people).constants.System.BlockWeights({ + at: selectedBlock, + }), + ]).then( + ([length, weights]) => ({ + length: length.max, + maxWeight: weights.max_block, + }), + () => null, + ), + ), + ), +) + +export const AnalyzePriority: FC<{ + txPayment?: { tip?: bigint } + extrinsic: HexString +}> = ({ txPayment, extrinsic }) => { + const blockHex = useStateObservable(selectedBlockHex$) + const maxBlockSize = useStateObservable(maxBlockSize$) + const [queryInfo, setQueryInfo] = useState<{ + weight: Weight + class: { type: "Normal" | "Operational" | "Mandatory" } + partial_fee: bigint + } | null>(null) + const length = (extrinsic.length - 2) / 8 + const tip = txPayment?.tip ?? 0n + + useEffect(() => { + let cancelled = false + ;(async () => { + setQueryInfo(null) + const client = await firstValueFrom(client$) + const result = await client + .getUnsafeApi() + .apis.TransactionPaymentApi.query_info( + Binary.fromOpaque(extrinsic), + length, + { + at: blockHex, + }, + ) + if (!cancelled) { + setQueryInfo(result as any) + } + })() + return () => { + cancelled = true + } + }, [extrinsic, blockHex, length]) + + if (!queryInfo) return null + + const priority = (() => { + if (!maxBlockSize) return null + const maxLength = + maxBlockSize.length[ + queryInfo.class.type.toLocaleLowerCase() as keyof typeof maxBlockSize.length + ] + + const maxTxPerBlockWeight = divWeight( + maxBlockSize.maxWeight, + queryInfo.weight, + ) + const maxTxPerBlockLength = BigInt(Math.floor(maxLength / length)) + const maxTxPerBlock = + maxTxPerBlockWeight < maxTxPerBlockLength + ? maxTxPerBlockWeight + : maxTxPerBlockLength + const priority = (tip + 1n) * maxTxPerBlock + + return { priority, maxTxPerBlockLength, maxTxPerBlockWeight } + })() + + return ( +
+ Priority: tip={tip.toLocaleString()} fee= + {queryInfo.partial_fee.toLocaleString()} class={queryInfo.class.type}{" "} + weight= + {queryInfo.weight.proof_size.toLocaleString() + + "/" + + queryInfo.weight.ref_time.toLocaleString()}{" "} + {priority ? ( + <> + txPerBlockLength={priority.maxTxPerBlockLength.toLocaleString()}{" "} + txPerBlockWeight={priority.maxTxPerBlockWeight.toLocaleString()}{" "} + priority={priority.priority.toLocaleString()} + + ) : null} +
+ ) +} + +export const analyzePriority$ = maxBlockSize$ + +type Weight = { ref_time: bigint; proof_size: bigint } +const divWeight = (a: Weight, b: Weight) => { + const ref_time = b.ref_time === 0n ? 1n : a.ref_time / b.ref_time + const proof_size = b.proof_size === 0n ? 1n : a.proof_size / b.proof_size + return ref_time < proof_size ? ref_time : proof_size +} diff --git a/src/pages/Extrinsics/Analyzer/selectedBlock.ts b/src/pages/Extrinsics/Analyzer/selectedBlock.ts new file mode 100644 index 0000000..57ffc46 --- /dev/null +++ b/src/pages/Extrinsics/Analyzer/selectedBlock.ts @@ -0,0 +1,6 @@ +import { state } from "@react-rxjs/core" +import { map } from "rxjs" +import { selectedBlock$ } from "../../Storage/BlockPicker" + +export { selectedBlock$ } +export const selectedBlockHex$ = state(selectedBlock$.pipe(map((v) => v.hash))) diff --git a/src/pages/Extrinsics/ExtrinsicAnalyzer.tsx b/src/pages/Extrinsics/ExtrinsicAnalyzer.tsx deleted file mode 100644 index 533596a..0000000 --- a/src/pages/Extrinsics/ExtrinsicAnalyzer.tsx +++ /dev/null @@ -1,309 +0,0 @@ -import { CopyBinary } from "@/codec-components/ViewCodec/CopyBinary" -import { JsonDisplay } from "@/components/JsonDisplay" -import { TextInputField } from "@/components/TextInputField" -import { useHashParamState } from "@/hashParams" -import { client$ } from "@/state/chains/chain.state" -import { polkadot_people } from "@polkadot-api/descriptors" -import { DecodedExtrinsic, getExtrinsicDecoder } from "@polkadot-api/tx-utils" -import { state, Subscribe, useStateObservable } from "@react-rxjs/core" -import { Binary, HexString, TxCallData } from "polkadot-api" -import { toHex } from "polkadot-api/utils" -import { FC, useEffect, useMemo, useState } from "react" -import { combineLatest, firstValueFrom, map, merge, switchMap } from "rxjs" -import { Sender, SignedExtensions } from "../Explorer/Detail/Extrinsic" -import { BlockPicker, selectedBlock$ } from "../Storage/BlockPicker" - -const extDecoder$ = state( - selectedBlock$.pipe( - map((block) => getExtrinsicDecoder(block.ctx.metadataRaw)), - ), -) -const selectedBlockHex$ = state(selectedBlock$.pipe(map((v) => v.hash))) - -export const ExtrinsicAnalyzer: FC = () => { - const [extrinsicHex, setExtrinsicHex] = useHashParamState( - "extrinsic", - () => "", - ) - - return ( -
-

Analyze Extrinsic

-
-
- -
-
- -
-
- - {extrinsicHex ? : null} - -
- ) -} - -const DecodeExtrinsic: FC<{ - extrinsic: HexString -}> = ({ extrinsic }) => { - const extrinsicDecoder = useStateObservable(extDecoder$) - const decodeResult = useMemo(() => { - try { - return { - type: "success" as const, - value: extrinsicDecoder(extrinsic), - } - } catch (ex) { - console.error(ex) - return { - type: "error" as const, - value: ex as Error, - } - } - }, [extrinsicDecoder, extrinsic]) - - if (decodeResult.type === "error") { - return
Can't decode: {decodeResult.value.message}
- } - - const decoded = decodeResult.value - - return ( -
-

- {decoded.type} Transaction v{decoded.version} -

- {decoded.type === "signed" ? ( - - ) : decoded.type === "general" ? ( -
TODO
- ) : ( - - )} - -
- ) -} - -const SignedInfo: FC<{ - extrinsic: HexString - decoded: DecodedExtrinsic & { type: "signed" } -}> = ({ extrinsic, decoded }) => { - const mortality = decoded.extra.CheckMortality - const txPayment = - decoded.extra.ChargeAssetTxPayment ?? decoded.extra.ChargeTxPayment - - return ( -
- - - {mortality ? : null} - -
- ) -} - -const CallData: FC<{ call: TxCallData; callData: Uint8Array }> = ({ - call, - callData, -}) => ( -
-
-
- {call.type}.{call.value.type} -
-
-
- {toHex(callData)} -
- -
-
- -
-) - -const selectedBlockNumber$ = state( - combineLatest([selectedBlockHex$, client$]).pipe( - switchMap(([selectedBlock, client]) => - selectedBlock - ? (client - .getTypedApi(polkadot_people) - .query.System.Number.getValue({ - at: selectedBlock, - }) - .catch(() => null) as Promise) - : client.finalizedBlock$.pipe(map((block) => block.number)), - ), - ), -) - -const AnalyzeMortality: FC<{ mortality: { type: string; value: number } }> = ({ - mortality, -}) => { - const selectedBlockNumber = useStateObservable(selectedBlockNumber$) - - if (mortality.type === "Immortal") return null - const parseResult = /^Mortal(\d+)$/.exec(mortality.type) - if (parseResult === null) return null - const first = BigInt(parseResult[1]) - const second = BigInt(mortality.value) - // from polkadot-sdk primitives runtime generic era fn decode - const encoded = first + (second << 8n) - const period = Number(2n << (encoded % (1n << 4n))) - const factor = period >> 12 || 1 - const phase = Number(encoded >> 4n) * factor - - // From fn birth - const birthBlock = selectedBlockNumber - ? Math.floor((Math.max(selectedBlockNumber, phase) - phase) / period) * - period + - phase - : null - - const referencedBlocks = birthBlock - ? new Array(5) - .fill(0) - .map((_, i) => (birthBlock - period * i).toLocaleString()) - : null - - return ( -
- Mortality: period={period} phase={phase}{" "} - {referencedBlocks ? ( - <> - currentBlock={selectedBlockNumber?.toLocaleString()} nextPeriod= - {(birthBlock! + period).toLocaleString()} oldPeriods= - {referencedBlocks.join(" ")} - - ) : null} -
- ) -} - -const maxBlockSize$ = state( - combineLatest([selectedBlockHex$, client$]).pipe( - switchMap(([selectedBlock, client]) => - Promise.all([ - client.getTypedApi(polkadot_people).constants.System.BlockLength({ - at: selectedBlock, - }), - client.getTypedApi(polkadot_people).constants.System.BlockWeights({ - at: selectedBlock, - }), - ]).then( - ([length, weights]) => ({ - length, - maxWeight: weights.max_block, - }), - () => null, - ), - ), - ), -) - -const AnalyzePriority: FC<{ - txPayment?: { tip?: bigint } - extrinsic: HexString -}> = ({ txPayment, extrinsic }) => { - const blockHex = useStateObservable(selectedBlockHex$) - const maxBlockSize = useStateObservable(maxBlockSize$) - const [queryInfo, setQueryInfo] = useState<{ - weight: Weight - class: { type: "Normal" | "Operational" | "Mandatory" } - partial_fee: bigint - } | null>(null) - const length = (extrinsic.length - 2) / 8 - const tip = txPayment?.tip ?? 0n - - useEffect(() => { - let cancelled = false - ;(async () => { - setQueryInfo(null) - const client = await firstValueFrom(client$) - console.log(extrinsic.length) - const result = await client - .getUnsafeApi() - .apis.TransactionPaymentApi.query_info( - Binary.fromOpaque(extrinsic), - length, - { - at: blockHex, - }, - ) - if (!cancelled) { - setQueryInfo(result as any) - } - })() - return () => { - cancelled = true - } - }, [extrinsic, blockHex, length]) - - if (!queryInfo) return null - - const priority = (() => { - if (!maxBlockSize) return null - const maxLength = - maxBlockSize.length.max[ - queryInfo.class.type.toLocaleLowerCase() as keyof typeof maxBlockSize.length.max - ] - - const maxTxPerBlockWeight = divWeight( - maxBlockSize.maxWeight, - queryInfo.weight, - ) - const maxTxPerBlockLength = BigInt(Math.floor(maxLength / length)) - const maxTxPerBlock = - maxTxPerBlockWeight < maxTxPerBlockLength - ? maxTxPerBlockWeight - : maxTxPerBlockLength - const priority = (tip + 1n) * maxTxPerBlock - - return { priority, maxTxPerBlockLength, maxTxPerBlockWeight } - })() - - return ( -
- Priority: tip={tip.toLocaleString()} fee= - {queryInfo.partial_fee.toLocaleString()} class={queryInfo.class.type}{" "} - weight= - {queryInfo.weight.proof_size.toLocaleString() + - "/" + - queryInfo.weight.ref_time.toLocaleString()}{" "} - {priority ? ( - <> - txPerBlockLength={priority.maxTxPerBlockLength.toLocaleString()}{" "} - txPerBlockWeight={priority.maxTxPerBlockWeight.toLocaleString()}{" "} - priority={priority.priority.toLocaleString()} - - ) : null} -
- ) -} - -const extAnalyzer$ = merge( - extDecoder$, - selectedBlockHex$, - selectedBlockNumber$, - maxBlockSize$, -) - -type Weight = { ref_time: bigint; proof_size: bigint } -const divWeight = (a: Weight, b: Weight) => { - const ref_time = b.ref_time === 0n ? 1n : a.ref_time / b.ref_time - const proof_size = b.proof_size === 0n ? 1n : a.proof_size / b.proof_size - return ref_time < proof_size ? ref_time : proof_size -} diff --git a/src/pages/Extrinsics/Extrinsics.tsx b/src/pages/Extrinsics/Extrinsics.tsx index 49cd252..14bd139 100644 --- a/src/pages/Extrinsics/Extrinsics.tsx +++ b/src/pages/Extrinsics/Extrinsics.tsx @@ -19,7 +19,7 @@ import { map } from "rxjs" import { twMerge } from "tailwind-merge" import { CustomSignedExt, customSignedExtensions$ } from "./CustomSignedExt" import { EditMode } from "./EditMode" -import { ExtrinsicAnalyzer } from "./ExtrinsicAnalyzer" +import { ExtrinsicAnalyzer } from "./Analyzer/ExtrinsicAnalyzer" import { JsonMode } from "./JsonMode" import { ExtrinsicModal } from "./SubmitTx/SubmitTx"