diff --git a/examples/tx/weights_and_estimates.eg.ts b/examples/tx/weights_and_estimates.eg.ts new file mode 100644 index 000000000..71d23311f --- /dev/null +++ b/examples/tx/weights_and_estimates.eg.ts @@ -0,0 +1,33 @@ +/** + * @title Weights & Estimates + * @stability nearing + * @description Get the timepoint and estimate the fee of a tx. + */ + +import { $weight, westend } from "@capi/westend" +import { $, alice, Rune } from "capi" + +/// Create the call data. +const call = westend.Balances + .transfer({ + value: 12345n, + dest: alice.address, + }) + +/// Gather the weight and estimate. +const collection = await Rune + .object({ + weight: call.weight(), + estimate: call.estimate(), + }) + .run() + +/// Ensure the data is of the expected shape. +console.log(collection) +$.assert( + $.object( + $.field("weight", $weight), + $.field("estimate", $.u128), + ), + collection, +) diff --git a/fluent/ExtrinsicRune.ts b/fluent/ExtrinsicRune.ts index 085c43ff1..6bfec64eb 100644 --- a/fluent/ExtrinsicRune.ts +++ b/fluent/ExtrinsicRune.ts @@ -50,7 +50,10 @@ export class ExtrinsicRune extends PatternRune extends PatternRune $.u32.encode(n))) .map(hex.encodePrefixed) - const data = this.chain.connection + const info = this.chain.connection .call("state_call", "TransactionPaymentApi_query_info", arg) .map(hex.decode) - return this.chain.metadata - .access("paths", "sp_weights::weight_v2::Weight") - .map(($c) => $.field("weight", $c)) + return this.$dispatchInfo.decoded(info) + } + + $weight = this.chain.metadata.access("paths", "sp_weights::weight_v2::Weight").into(CodecRune) + weight() { + return this.dispatchInfo().access("weight") + } + weightRaw() { + return this.$weight.encoded(this.weight()).map(hex.encode) + } + + estimate() { + const encoded = this.chain.connection + .call("state_call", "TransactionPaymentApi_query_weight_to_fee", this.weightRaw()) + .map(hex.decode) + return Rune + .constant($.u128) .into(CodecRune) - .decoded(data) + .decoded(encoded) } } diff --git a/patterns/multisig/MultisigRune.ts b/patterns/multisig/MultisigRune.ts index db8f34da5..60239dbfe 100644 --- a/patterns/multisig/MultisigRune.ts +++ b/patterns/multisig/MultisigRune.ts @@ -59,7 +59,7 @@ export class MultisigRune extends PatternRune