Skip to content

Commit 9e01383

Browse files
authored
feat(cargo-contract): display fee estimation on map account dry-run (#2045)
* feat(cargo-contract): display fee estimation on map account dry-run * chore(docs): update CHANGELOG and code comments
1 parent 9071dc3 commit 9e01383

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Added
1010
- Support setting ABI in `Cargo.toml` and propagate ABI into build environment via `cfg` flag - [#2033](https://github.com/use-ink/cargo-contract/pull/2033)
1111
- Add `cargo contract test` subcommand - [#2034](https://github.com/use-ink/cargo-contract/pull/2034)
12+
- Show cost of mapping an address in `cargo-contract` prompt - [#1990](https://github.com/use-ink/cargo-contract/issues/1990)
1213

1314
### Fixed
1415
- Fixed erroneous "[lib] name" warnings - [#2035](https://github.com/use-ink/cargo-contract/pull/2035)

crates/cargo-contract/src/cmd/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,13 @@ where
300300
let map_exec: MapAccountExec<C, C, _> =
301301
MapAccountCommandBuilder::new(extrinsic_opts).done().await?;
302302
let result = map_exec.map_account_dry_run().await;
303-
if result.is_ok() {
303+
if let Ok(partial_fee_estimation) = result {
304304
let reply = prompt_confirm_mapping(|| {
305-
// todo print additional information about the costs of mapping an account
305+
name_value_println!(
306+
"Estimated fee",
307+
format!("{:?}", partial_fee_estimation),
308+
DEFAULT_KEY_COL_WIDTH
309+
);
306310
});
307311
if reply.is_ok() {
308312
let res = map_exec.map_account().await?;

crates/extrinsics/src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,8 @@ where
221221
Err(RpcError::SubscriptionDropped.into())
222222
}
223223

224-
/// Wait for the transaction to be included successfully into a block.
224+
/// Wait for the transaction to be included successfully into a block. Returns the
225+
/// estimated fee to execute the transaction.
225226
///
226227
/// # Errors
227228
///
@@ -238,7 +239,7 @@ async fn dry_run_extrinsic<C, Call, Signer>(
238239
rpc: &LegacyRpcMethods<C>,
239240
call: &Call,
240241
signer: &Signer,
241-
) -> core::result::Result<DryRunResultBytes, subxt::Error>
242+
) -> core::result::Result<(DryRunResultBytes, u128), subxt::Error>
242243
where
243244
C: Config,
244245
Call: tx::Payload,
@@ -256,7 +257,9 @@ where
256257
.tx()
257258
.create_partial_offline(call, params.into())?
258259
.sign(signer);
259-
Ok(rpc.dry_run(extrinsic.encoded(), None).await?)
260+
let result = rpc.dry_run(extrinsic.encoded(), None).await?;
261+
let partial_fee_estimate = extrinsic.partial_fee_estimate().await?;
262+
Ok((result, partial_fee_estimate))
260263
}
261264

262265
/// Return the account nonce at the *best* block for an account ID.

crates/extrinsics/src/map_account.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,20 +143,20 @@ where
143143
/// instantiation on the blockchain.
144144
///
145145
/// Returns the dry run simulation result, or an error in case of failure.
146-
pub async fn map_account_dry_run(&self) -> Result<()> {
146+
pub async fn map_account_dry_run(&self) -> Result<u128> {
147147
let call = MapAccount::new().build();
148-
let bytes =
148+
let (bytes, partial_fee_estimation) =
149149
dry_run_extrinsic(&self.client, &self.rpc, &call, self.opts.signer()).await?;
150150
let res = bytes.into_dry_run_result();
151151
match res {
152-
Ok(DryRunResult::Success) => Ok(()),
152+
Ok(DryRunResult::Success) => Ok(partial_fee_estimation),
153153
Ok(DryRunResult::DispatchError(err)) => {
154154
Err(anyhow::format_err!("dispatch error: {:?}", err))
155155
}
156156
Ok(DryRunResult::TransactionValidityError) => {
157157
// todo seems like an external bug: https://github.com/paritytech/polkadot-sdk/issues/7305
158158
// Err(anyhow::format_err!("validity err"))
159-
Ok(())
159+
Ok(partial_fee_estimation)
160160
}
161161
Err(err) => {
162162
match err {

0 commit comments

Comments
 (0)