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

chore: clean up weights + estimates #960

Merged
merged 8 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions examples/tx/weights_&_estimates.ts
harrysolovay marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @title Weights & Estimates
peetzweg marked this conversation as resolved.
Show resolved Hide resolved
* @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)
peetzweg marked this conversation as resolved.
Show resolved Hide resolved
$.assert(
$.object(
$.field("weight", $weight),
$.field("estimate", $.u128),
),
collection,
)
24 changes: 20 additions & 4 deletions fluent/ExtrinsicRune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class ExtrinsicRune<out C extends Chain, out U> extends PatternRune<Chain
.into(SignedExtrinsicRune, this.chain)
}

feeEstimate() {
weightRaw() {
const extrinsic = this.chain.$extrinsic.encoded(Rune.object({
protocolVersion: ExtrinsicRune.PROTOCOL_VERSION,
call: this,
Expand All @@ -59,13 +59,29 @@ export class ExtrinsicRune<out C extends Chain, out U> extends PatternRune<Chain
.fn(concat)
.call(extrinsic, extrinsic.access("length").map((n) => $.u32.encode(n)))
.map(hex.encodePrefixed)
const data = this.chain.connection
// The TransactionPaymentApi_query_info endpoint returns a heavily-padded hex str.
// If we don't strip the excess before passing to other calls, we get a runtime panic.
return this.chain.connection
.call("state_call", "TransactionPaymentApi_query_info", arg)
harrysolovay marked this conversation as resolved.
Show resolved Hide resolved
.map(hex.decode)
.map((v) => v.slice(0, 6))
harrysolovay marked this conversation as resolved.
Show resolved Hide resolved
.map(hex.encode)
}

weight() {
return this.chain.metadata
.access("paths", "sp_weights::weight_v2::Weight")
.map(($c) => $.field("weight", $c))
.into(CodecRune)
.decoded(data)
.decoded(this.weightRaw().map(hex.decode))
}

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(encoded)
}
}
2 changes: 1 addition & 1 deletion patterns/multisig/MultisigRune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class MultisigRune<out C extends Chain, out U> extends PatternRune<Multis
threshold,
call,
otherSignatories,
maxWeight: call.feeEstimate().access("weight"),
maxWeight: call.weight(),
maybeTimepoint,
}),
})
Expand Down