Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
cmichi committed Feb 2, 2025
1 parent dfbbbe8 commit c98bd47
Show file tree
Hide file tree
Showing 8 changed files with 323 additions and 68 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/analyze/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ pub fn determine_language(_code: &[u8]) -> Result<Language> {
#[cfg(test)]
mod tests {
/*
// todo
#[test]
fn fails_with_unsupported_language() {
let contract = r#"
Expand Down
57 changes: 43 additions & 14 deletions crates/cargo-contract/src/cmd/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ use subxt::{
ext::{
scale_decode::IntoVisitor,
scale_encode::EncodeAsType,
sp_runtime::traits::Zero,
},
Config,
};
Expand Down Expand Up @@ -124,8 +125,14 @@ impl CallCommand {
where
<C as Config>::AccountId: IntoVisitor + FromStr + EncodeAsType,
<<C as Config>::AccountId as FromStr>::Err: Display,
C::Balance:
From<u128> + Display + Default + FromStr + Serialize + Debug + EncodeAsType,
C::Balance: From<u128>
+ Display
+ Default
+ FromStr
+ Serialize
+ Debug
+ EncodeAsType
+ Zero,
<C::ExtrinsicParams as ExtrinsicParams<C>>::Params:
From<<DefaultExtrinsicParams<C> as ExtrinsicParams<C>>::Params>,
{
Expand Down Expand Up @@ -214,12 +221,13 @@ impl CallCommand {
}
}
} else {
let gas_limit = pre_submit_dry_run_gas_estimate_call(
&call_exec,
self.output_json(),
self.extrinsic_cli_opts.skip_dry_run,
)
.await?;
let (gas_limit, storage_deposit_limit) =
pre_submit_dry_run_gas_estimate_call(
&call_exec,
self.output_json(),
self.extrinsic_cli_opts.skip_dry_run,
)
.await?;
if !self.extrinsic_cli_opts.skip_confirm {
prompt_confirm_tx(|| {
name_value_println!(
Expand All @@ -239,7 +247,9 @@ impl CallCommand {
);
})?;
}
let events = call_exec.call(Some(gas_limit)).await?;
let events = call_exec
.call(Some(gas_limit), Some(storage_deposit_limit))
.await?;
let display_events =
DisplayEvents::from_events::<C, C>(&events, None, &metadata)?;

Expand All @@ -262,23 +272,32 @@ async fn pre_submit_dry_run_gas_estimate_call<C: Config + Environment, Signer>(
call_exec: &CallExec<C, C, Signer>,
output_json: bool,
skip_dry_run: bool,
) -> Result<Weight>
) -> Result<(Weight, C::Balance)>
where
Signer: subxt::tx::Signer<C> + Clone,
<C as Config>::AccountId: IntoVisitor + EncodeAsType,
C::Balance: Debug + EncodeAsType,
C::Balance: Debug + EncodeAsType + Zero,
<C::ExtrinsicParams as ExtrinsicParams<C>>::Params:
From<<DefaultExtrinsicParams<C> as ExtrinsicParams<C>>::Params>,
{
if skip_dry_run {
return match (call_exec.gas_limit(), call_exec.proof_size()) {
let weight = match (call_exec.gas_limit(), call_exec.proof_size()) {
(Some(ref_time), Some(proof_size)) => Ok(Weight::from_parts(ref_time, proof_size)),
_ => {
Err(anyhow!(
"Weight args `--gas` and `--proof-size` required if `--skip-dry-run` specified"
))
}
};
}?;
let storage_deposit_limit = match call_exec.opts().storage_deposit_limit() {
Some(limit) => Ok(limit),
_ => {
Err(anyhow!(
"Storage deposit limit arg `--storage-deposit-limit` required if `--skip-dry-run` specified"
))
}
}?;
return Ok((weight, storage_deposit_limit));
}
if !output_json {
print_dry_running_status(call_exec.message());
Expand All @@ -296,7 +315,17 @@ where
let proof_size = call_exec
.proof_size()
.unwrap_or_else(|| call_result.gas_required.proof_size());
Ok(Weight::from_parts(ref_time, proof_size))
let storage_deposit_limit =
call_exec.opts().storage_deposit_limit().unwrap_or_else(|| {
match call_result.storage_deposit {
StorageDeposit::Refund(_) => C::Balance::zero(),
StorageDeposit::Charge(charge) => charge,
}
});
Ok((
Weight::from_parts(ref_time, proof_size),
storage_deposit_limit,
))
}
Err(ref err) => {
let object =
Expand Down
2 changes: 1 addition & 1 deletion crates/cargo-contract/src/cmd/instantiate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ where
Some(limit) => Ok(limit),
_ => {
Err(anyhow!(
"Weight args `--gas` and `--proof-size` required if `--skip-dry-run` specified"
"Storage deposit limit arg `--storage-deposit-limit` required if `--skip-dry-run` specified"
))
}
}?;
Expand Down
1 change: 1 addition & 0 deletions crates/extrinsics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ predicates = "3.1.3"
tempfile = "3.10.1"
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
subxt-signer = { version = "0.38.0", features = ["subxt", "sr25519"] }
stdio-override = "0.2.0"

[features]
integration-tests = []
Expand Down
40 changes: 31 additions & 9 deletions crates/extrinsics/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use ink_env::Environment;
use scale::Encode;
use sp_weights::Weight;

use crate::pallet_revive_primitives::StorageDeposit;
use subxt::{
backend::{
legacy::LegacyRpcMethods,
Expand All @@ -48,6 +49,7 @@ use subxt::{
ext::{
scale_decode::IntoVisitor,
scale_encode::EncodeAsType,
sp_runtime::traits::Zero,
},
tx,
utils::H160,
Expand Down Expand Up @@ -173,7 +175,7 @@ where
<C::ExtrinsicParams as ExtrinsicParams<C>>::Params:
From<<DefaultExtrinsicParams<C> as ExtrinsicParams<C>>::Params>,
C::AccountId: EncodeAsType + IntoVisitor,
E::Balance: EncodeAsType,
E::Balance: EncodeAsType + Zero,
Signer: tx::Signer<C> + Clone,
{
/// Simulates a contract call without modifying the blockchain.
Expand Down Expand Up @@ -209,6 +211,7 @@ where
pub async fn call(
&self,
gas_limit: Option<Weight>,
storage_deposit_limit: Option<E::Balance>,
) -> Result<ExtrinsicEvents<C>, ErrorVariant> {
if !self
.transcoder()
Expand All @@ -228,18 +231,23 @@ where
}

// use user specified values where provided, otherwise estimate
// todo write in a way that estimate_gas() is only executed when really needed
let estimate = self.estimate_gas().await?;
let gas_limit = match gas_limit {
Some(gas_limit) => gas_limit,
None => self.estimate_gas().await?,
None => estimate.0,
};
let storage_deposit_limit = match storage_deposit_limit {
Some(deposit_limit) => deposit_limit,
None => estimate.1,
};
tracing::debug!("calling contract {:?}", self.contract);
let storage_deposit_limit = self.opts.storage_deposit_limit();

let call = Call::new(
self.contract,
self.value,
gas_limit,
storage_deposit_limit.expect("no storage deposit limit available"),
storage_deposit_limit,
self.call_data.clone(),
)
.build();
Expand All @@ -258,10 +266,14 @@ where
///
/// Returns the estimated gas weight of type [`Weight`] for contract calls, or an
/// error.
pub async fn estimate_gas(&self) -> Result<Weight> {
match (self.gas_limit, self.proof_size) {
(Some(ref_time), Some(proof_size)) => {
Ok(Weight::from_parts(ref_time, proof_size))
pub async fn estimate_gas(&self) -> Result<(Weight, E::Balance)> {
match (
self.gas_limit,
self.proof_size,
self.opts.storage_deposit_limit(),
) {
(Some(ref_time), Some(proof_size), Some(deposit_limit)) => {
Ok((Weight::from_parts(ref_time, proof_size), deposit_limit))
}
_ => {
let call_result = self.call_dry_run().await?;
Expand All @@ -275,7 +287,17 @@ where
let proof_size = self
.proof_size
.unwrap_or_else(|| call_result.gas_required.proof_size());
Ok(Weight::from_parts(ref_time, proof_size))
let storage_deposit_limit =
self.opts.storage_deposit_limit().unwrap_or_else(|| {
match call_result.storage_deposit {
StorageDeposit::Refund(_) => E::Balance::zero(),
StorageDeposit::Charge(charge) => charge,
}
});
Ok((
Weight::from_parts(ref_time, proof_size),
storage_deposit_limit,
))
}
Err(ref err) => {
let object = ErrorVariant::from_dispatch_error(
Expand Down
Loading

0 comments on commit c98bd47

Please sign in to comment.