From 3a082def35504a9fe6e88e471fe4fe67106c74b0 Mon Sep 17 00:00:00 2001 From: Harry Solovay Date: Wed, 3 May 2023 11:13:58 -0400 Subject: [PATCH 1/8] clean up estimates --- examples/tx/estimate.ts | 33 +++++++++++++++++++++++++++++++ fluent/ExtrinsicRune.ts | 19 ++++++++++++++---- patterns/multisig/MultisigRune.ts | 2 +- 3 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 examples/tx/estimate.ts diff --git a/examples/tx/estimate.ts b/examples/tx/estimate.ts new file mode 100644 index 000000000..dceb0d262 --- /dev/null +++ b/examples/tx/estimate.ts @@ -0,0 +1,33 @@ +/** + * @title Estimate + * @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..cb9eb0424 100644 --- a/fluent/ExtrinsicRune.ts +++ b/fluent/ExtrinsicRune.ts @@ -50,7 +50,7 @@ export class ExtrinsicRune extends PatternRune extends PatternRune $.u32.encode(n))) .map(hex.encodePrefixed) - const data = this.chain.connection + return this.chain.connection .call("state_call", "TransactionPaymentApi_query_info", arg) .map(hex.decode) + .map((v) => v.slice(0, 6)) + .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() { + return this.chain.connection + .call("state_call", "TransactionPaymentApi_query_weight_to_fee", this.weightRaw()) + .map(hex.decode) + .map((value) => $.u128.decode(value)) } } diff --git a/patterns/multisig/MultisigRune.ts b/patterns/multisig/MultisigRune.ts index db8f34da5..f4ae27805 100644 --- a/patterns/multisig/MultisigRune.ts +++ b/patterns/multisig/MultisigRune.ts @@ -59,7 +59,7 @@ export class MultisigRune extends PatternRune Date: Wed, 3 May 2023 11:16:55 -0400 Subject: [PATCH 2/8] clean up estimates --- examples/tx/{estimate.ts => weights_&_estimates.ts} | 2 +- patterns/multisig/MultisigRune.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename examples/tx/{estimate.ts => weights_&_estimates.ts} (95%) diff --git a/examples/tx/estimate.ts b/examples/tx/weights_&_estimates.ts similarity index 95% rename from examples/tx/estimate.ts rename to examples/tx/weights_&_estimates.ts index dceb0d262..983c8ed0c 100644 --- a/examples/tx/estimate.ts +++ b/examples/tx/weights_&_estimates.ts @@ -1,5 +1,5 @@ /** - * @title Estimate + * @title Weights & Estimates * @stability nearing * @description Get the timepoint and estimate the fee of a tx. */ diff --git a/patterns/multisig/MultisigRune.ts b/patterns/multisig/MultisigRune.ts index f4ae27805..60239dbfe 100644 --- a/patterns/multisig/MultisigRune.ts +++ b/patterns/multisig/MultisigRune.ts @@ -59,7 +59,7 @@ export class MultisigRune extends PatternRune Date: Wed, 3 May 2023 11:20:33 -0400 Subject: [PATCH 3/8] clean up estimates --- fluent/ExtrinsicRune.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/fluent/ExtrinsicRune.ts b/fluent/ExtrinsicRune.ts index cb9eb0424..3f1e16937 100644 --- a/fluent/ExtrinsicRune.ts +++ b/fluent/ExtrinsicRune.ts @@ -74,9 +74,12 @@ export class ExtrinsicRune extends PatternRune $.u128.decode(value)) + return Rune + .constant($.u128) + .into(CodecRune) + .decoded(encoded) } } From f1a3ce83234b2e4e3ab03239e5b91544a9cd2185 Mon Sep 17 00:00:00 2001 From: Harry Solovay Date: Fri, 5 May 2023 10:44:07 -0400 Subject: [PATCH 4/8] add reasoning behind slice --- fluent/ExtrinsicRune.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fluent/ExtrinsicRune.ts b/fluent/ExtrinsicRune.ts index 3f1e16937..f056165ec 100644 --- a/fluent/ExtrinsicRune.ts +++ b/fluent/ExtrinsicRune.ts @@ -59,6 +59,8 @@ export class ExtrinsicRune extends PatternRune $.u32.encode(n))) .map(hex.encodePrefixed) + // 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) .map(hex.decode) From bb2122543e386c752c96904a1b4da8f8e6382e2b Mon Sep 17 00:00:00 2001 From: Harry Solovay Date: Fri, 5 May 2023 12:06:41 -0400 Subject: [PATCH 5/8] Update examples/tx/weights_&_estimates.ts --- examples/tx/weights_&_estimates.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/tx/weights_&_estimates.ts b/examples/tx/weights_&_estimates.ts index 983c8ed0c..71d23311f 100644 --- a/examples/tx/weights_&_estimates.ts +++ b/examples/tx/weights_&_estimates.ts @@ -22,7 +22,7 @@ const collection = await Rune }) .run() -/// Ensure the data is of the expected shape +/// Ensure the data is of the expected shape. console.log(collection) $.assert( $.object( From 827cfbebef39d9e33330b35277741e067a5aa709 Mon Sep 17 00:00:00 2001 From: Harry Solovay Date: Mon, 8 May 2023 08:36:22 -0400 Subject: [PATCH 6/8] t6 feedback --- ..._estimates.ts => weights_and_estimates.ts} | 0 fluent/ExtrinsicRune.ts | 21 ++++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) rename examples/tx/{weights_&_estimates.ts => weights_and_estimates.ts} (100%) diff --git a/examples/tx/weights_&_estimates.ts b/examples/tx/weights_and_estimates.ts similarity index 100% rename from examples/tx/weights_&_estimates.ts rename to examples/tx/weights_and_estimates.ts diff --git a/fluent/ExtrinsicRune.ts b/fluent/ExtrinsicRune.ts index f056165ec..85919f5a9 100644 --- a/fluent/ExtrinsicRune.ts +++ b/fluent/ExtrinsicRune.ts @@ -50,7 +50,7 @@ export class ExtrinsicRune extends PatternRune extends PatternRune $.u32.encode(n))) .map(hex.encodePrefixed) - // 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 + const info = this.chain.connection .call("state_call", "TransactionPaymentApi_query_info", arg) .map(hex.decode) - .map((v) => v.slice(0, 6)) - .map(hex.encode) + return this.chain.metadata + .access("types", "DispatchInfo") + .into(CodecRune) + .decoded(info) } + $weight = this.chain.metadata.access("types", "Weight").into(CodecRune) weight() { - return this.chain.metadata - .access("paths", "sp_weights::weight_v2::Weight") - .into(CodecRune) - .decoded(this.weightRaw().map(hex.decode)) + return this.dispatchInfo().access("weight") + } + weightRaw() { + return this.$weight.encoded(this.weight()).map(hex.encode) } estimate() { From 8af88cd04d3f574788e7b8d136e56890251ab795 Mon Sep 17 00:00:00 2001 From: Harry Solovay Date: Mon, 8 May 2023 08:36:47 -0400 Subject: [PATCH 7/8] rename to eg --- .../tx/{weights_and_estimates.ts => weights_and_estimates.eg.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename examples/tx/{weights_and_estimates.ts => weights_and_estimates.eg.ts} (100%) diff --git a/examples/tx/weights_and_estimates.ts b/examples/tx/weights_and_estimates.eg.ts similarity index 100% rename from examples/tx/weights_and_estimates.ts rename to examples/tx/weights_and_estimates.eg.ts From 648820a81bc38069bc09acaa641968b0f2b860da Mon Sep 17 00:00:00 2001 From: Harry Solovay Date: Mon, 8 May 2023 08:52:29 -0400 Subject: [PATCH 8/8] t6 feedback --- fluent/ExtrinsicRune.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/fluent/ExtrinsicRune.ts b/fluent/ExtrinsicRune.ts index 85919f5a9..6bfec64eb 100644 --- a/fluent/ExtrinsicRune.ts +++ b/fluent/ExtrinsicRune.ts @@ -50,6 +50,9 @@ export class ExtrinsicRune extends PatternRune extends PatternRune