cargo-contract 5.0.0

Setup and deployment tool for developing Wasm based smart contracts via ink!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// Copyright (C) Use Ink (UK) Ltd.
// This file is part of cargo-contract.
//
// cargo-contract is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// cargo-contract is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with cargo-contract.  If not, see <http://www.gnu.org/licenses/>.

use super::{
    config::SignerConfig,
    display_contract_exec_result,
    display_contract_exec_result_debug,
    display_dry_run_result_warning,
    parse_balance,
    print_dry_running_status,
    print_gas_required_success,
    prompt_confirm_tx,
    CLIExtrinsicOpts,
    MAX_KEY_COL_WIDTH,
};
use crate::{
    anyhow,
    call_with_config,
    cmd::prompt_confirm_unverifiable_upload,
    ErrorVariant,
    InstantiateExec,
    Weight,
};
use anyhow::Result;
use contract_build::{
    name_value_println,
    util::{
        decode_hex,
        DEFAULT_KEY_COL_WIDTH,
    },
    Verbosity,
};
use contract_extrinsics::{
    Code,
    DisplayEvents,
    ExtrinsicOptsBuilder,
    InstantiateCommandBuilder,
    InstantiateDryRunResult,
    InstantiateExecResult,
    TokenMetadata,
};
use ink_env::Environment;
use serde::Serialize;
use sp_core::Bytes;
use std::{
    fmt::{
        Debug,
        Display,
    },
    str::FromStr,
};
use subxt::{
    config::{
        DefaultExtrinsicParams,
        ExtrinsicParams,
    },
    ext::{
        codec::Decode,
        scale_decode::IntoVisitor,
        scale_encode::EncodeAsType,
    },
    Config,
};

#[derive(Debug, clap::Args)]
pub struct InstantiateCommand {
    /// The name of the contract constructor to call
    #[clap(name = "constructor", long, default_value = "new")]
    constructor: String,
    /// The constructor arguments, encoded as strings
    #[clap(long, num_args = 0..)]
    args: Vec<String>,
    #[clap(flatten)]
    extrinsic_cli_opts: CLIExtrinsicOpts,
    /// Transfers an initial balance to the instantiated contract
    #[clap(name = "value", long, default_value = "0")]
    value: String,
    /// Maximum amount of gas to be used for this command.
    /// If not specified will perform a dry-run to estimate the gas consumed for the
    /// instantiation.
    #[clap(name = "gas", long)]
    gas_limit: Option<u64>,
    /// Maximum proof size for this instantiation.
    /// If not specified will perform a dry-run to estimate the proof size required.
    #[clap(long)]
    proof_size: Option<u64>,
    /// A salt used in the address derivation of the new contract. Use to create multiple
    /// instances of the same contract code from the same account.
    #[clap(long, value_parser = parse_hex_bytes)]
    salt: Option<Bytes>,
    /// Export the instantiate output in JSON format.
    #[clap(long, conflicts_with = "verbose")]
    output_json: bool,
}

/// Parse hex encoded bytes.
fn parse_hex_bytes(input: &str) -> Result<Bytes> {
    let bytes = decode_hex(input)?;
    Ok(bytes.into())
}

impl InstantiateCommand {
    /// Returns whether to export the call output in JSON format.
    pub fn output_json(&self) -> bool {
        self.output_json
    }

    pub async fn handle(&self) -> Result<(), ErrorVariant> {
        call_with_config!(
            self,
            run,
            self.extrinsic_cli_opts.chain_cli_opts.chain().config()
        )
    }

    async fn run<C: Config + Environment + SignerConfig<C>>(
        &self,
    ) -> Result<(), ErrorVariant>
    where
        <C as SignerConfig<C>>::Signer: subxt::tx::Signer<C> + Clone + FromStr,
        <C as Config>::AccountId: IntoVisitor + FromStr + EncodeAsType + Decode + Display,
        <<C as Config>::AccountId as FromStr>::Err: Display,
        C::Balance:
            From<u128> + Display + Default + FromStr + Serialize + Debug + EncodeAsType,
        <C::ExtrinsicParams as ExtrinsicParams<C>>::Params:
            From<<DefaultExtrinsicParams<C> as ExtrinsicParams<C>>::Params>,
        <C as Config>::Hash: From<[u8; 32]> + IntoVisitor + EncodeAsType,
    {
        let signer = C::Signer::from_str(&self.extrinsic_cli_opts.suri)
            .map_err(|_| anyhow::anyhow!("Failed to parse suri option"))?;
        let chain = self.extrinsic_cli_opts.chain_cli_opts.chain();
        let token_metadata = TokenMetadata::query::<C>(&chain.url()).await?;

        let storage_deposit_limit = self
            .extrinsic_cli_opts
            .storage_deposit_limit
            .clone()
            .map(|b| parse_balance(&b, &token_metadata))
            .transpose()
            .map_err(|e| {
                anyhow::anyhow!("Failed to parse storage_deposit_limit option: {}", e)
            })?;
        let value = parse_balance(&self.value, &token_metadata)
            .map_err(|e| anyhow::anyhow!("Failed to parse value option: {}", e))?;
        let extrinsic_opts = ExtrinsicOptsBuilder::new(signer)
            .file(self.extrinsic_cli_opts.file.clone())
            .manifest_path(self.extrinsic_cli_opts.manifest_path.clone())
            .url(chain.url())
            .storage_deposit_limit(storage_deposit_limit)
            .done();

        let instantiate_exec: InstantiateExec<C, C, _> =
            InstantiateCommandBuilder::new(extrinsic_opts)
                .constructor(self.constructor.clone())
                .args(self.args.clone())
                .value(value)
                .gas_limit(self.gas_limit)
                .proof_size(self.proof_size)
                .salt(self.salt.clone())
                .done()
                .await?;

        if !self.extrinsic_cli_opts.execute {
            let result = instantiate_exec.instantiate_dry_run().await?;
            match instantiate_exec.decode_instantiate_dry_run(&result).await {
                Ok(dry_run_result) => {
                    if self.output_json() {
                        println!("{}", dry_run_result.to_json()?);
                    } else {
                        print_instantiate_dry_run_result(&dry_run_result);
                        display_contract_exec_result_debug::<_, DEFAULT_KEY_COL_WIDTH, _>(
                            &result,
                        )?;
                        display_dry_run_result_warning("instantiate");
                    }
                    Ok(())
                }
                Err(object) => {
                    if self.output_json() {
                        return Err(object)
                    } else {
                        name_value_println!("Result", object, MAX_KEY_COL_WIDTH);
                        display_contract_exec_result::<_, MAX_KEY_COL_WIDTH, _>(&result)?;
                    }
                    Err(object)
                }
            }
        } else {
            if let Some(chain) = chain.production() {
                if !instantiate_exec
                    .opts()
                    .contract_artifacts()?
                    .is_verifiable()
                {
                    prompt_confirm_unverifiable_upload(&chain.to_string())?
                }
            }
            tracing::debug!("instantiate data {:?}", instantiate_exec.args().data());
            let gas_limit = pre_submit_dry_run_gas_estimate_instantiate(
                &instantiate_exec,
                self.output_json(),
                self.extrinsic_cli_opts.skip_dry_run,
            )
            .await?;
            if !self.extrinsic_cli_opts.skip_confirm {
                prompt_confirm_tx(|| {
                    print_default_instantiate_preview(&instantiate_exec, gas_limit);
                    if let Code::Existing(code_hash) =
                        instantiate_exec.args().code().clone()
                    {
                        name_value_println!(
                            "Code hash",
                            format!("{code_hash:?}"),
                            DEFAULT_KEY_COL_WIDTH
                        );
                    }
                })?;
            }
            let instantiate_result =
                instantiate_exec.instantiate(Some(gas_limit)).await?;
            display_result(
                &instantiate_exec,
                instantiate_result,
                &token_metadata,
                self.output_json(),
                self.extrinsic_cli_opts.verbosity().unwrap(),
            )
            .await?;
            Ok(())
        }
    }
}

/// A helper function to estimate the gas required for a contract instantiation.
async fn pre_submit_dry_run_gas_estimate_instantiate<
    C: Config + Environment + SignerConfig<C>,
>(
    instantiate_exec: &InstantiateExec<C, C, C::Signer>,
    output_json: bool,
    skip_dry_run: bool,
) -> Result<Weight>
where
    C::Signer: subxt::tx::Signer<C> + Clone,
    <C as Config>::AccountId: IntoVisitor + Display + Decode,
    <C as Config>::Hash: IntoVisitor + EncodeAsType,
    C::Balance: Serialize + Debug + EncodeAsType,
    <C::ExtrinsicParams as ExtrinsicParams<C>>::Params:
        From<<DefaultExtrinsicParams<C> as ExtrinsicParams<C>>::Params>,
{
    if skip_dry_run {
        return match (instantiate_exec.args().gas_limit(), instantiate_exec.args().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"
                    ))
                }
            };
    }
    if !output_json {
        print_dry_running_status(instantiate_exec.args().constructor());
    }
    let instantiate_result = instantiate_exec.instantiate_dry_run().await?;
    match instantiate_result.result {
        Ok(_) => {
            if !output_json {
                print_gas_required_success(instantiate_result.gas_required);
            }
            // use user specified values where provided, otherwise use the estimates
            let ref_time = instantiate_exec
                .args()
                .gas_limit()
                .unwrap_or_else(|| instantiate_result.gas_required.ref_time());
            let proof_size = instantiate_exec
                .args()
                .proof_size()
                .unwrap_or_else(|| instantiate_result.gas_required.proof_size());
            Ok(Weight::from_parts(ref_time, proof_size))
        }
        Err(ref err) => {
            let object = ErrorVariant::from_dispatch_error(
                err,
                &instantiate_exec.client().metadata(),
            )?;
            if output_json {
                Err(anyhow!("{}", serde_json::to_string_pretty(&object)?))
            } else {
                name_value_println!("Result", object, MAX_KEY_COL_WIDTH);
                display_contract_exec_result::<_, MAX_KEY_COL_WIDTH, _>(
                    &instantiate_result,
                )?;

                Err(anyhow!("Pre-submission dry-run failed. Use --skip-dry-run to skip this step."))
            }
        }
    }
}

/// Displays the results of contract instantiation, including contract address,
/// events, and optional code hash.
pub async fn display_result<C: Config + Environment + SignerConfig<C>>(
    instantiate_exec: &InstantiateExec<C, C, C::Signer>,
    instantiate_exec_result: InstantiateExecResult<C>,
    token_metadata: &TokenMetadata,
    output_json: bool,
    verbosity: Verbosity,
) -> Result<(), ErrorVariant>
where
    <C as Config>::AccountId: IntoVisitor + EncodeAsType + Display + Decode,
    <C as Config>::Hash: IntoVisitor + EncodeAsType,
    C::Balance: Serialize + From<u128> + Display + EncodeAsType,
    <C::ExtrinsicParams as ExtrinsicParams<C>>::Params:
        From<<DefaultExtrinsicParams<C> as ExtrinsicParams<C>>::Params>,
{
    let events = DisplayEvents::from_events::<C, C>(
        &instantiate_exec_result.events,
        Some(instantiate_exec.transcoder()),
        &instantiate_exec.client().metadata(),
    )?;
    let contract_address = instantiate_exec_result.contract_address.to_string();
    if output_json {
        let display_instantiate_result = InstantiateResult {
            code_hash: instantiate_exec_result
                .code_hash
                .map(|ch| format!("{ch:?}")),
            contract: Some(contract_address),
            events,
        };
        println!("{}", display_instantiate_result.to_json()?)
    } else {
        println!("{}", events.display_events::<C>(verbosity, token_metadata)?);
        if let Some(code_hash) = instantiate_exec_result.code_hash {
            name_value_println!("Code hash", format!("{code_hash:?}"));
        }
        name_value_println!("Contract", contract_address);
    };
    Ok(())
}

pub fn print_default_instantiate_preview<C: Config + Environment + SignerConfig<C>>(
    instantiate_exec: &InstantiateExec<C, C, C::Signer>,
    gas_limit: Weight,
) where
    C::Signer: subxt::tx::Signer<C> + Clone,
    <C as Config>::AccountId: IntoVisitor + EncodeAsType + Display + Decode,
    <C as Config>::Hash: IntoVisitor + EncodeAsType,
    C::Balance: Serialize + EncodeAsType,
    <C::ExtrinsicParams as ExtrinsicParams<C>>::Params:
        From<<DefaultExtrinsicParams<C> as ExtrinsicParams<C>>::Params>,
{
    name_value_println!(
        "Constructor",
        instantiate_exec.args().constructor(),
        DEFAULT_KEY_COL_WIDTH
    );
    name_value_println!(
        "Args",
        instantiate_exec.args().raw_args().join(" "),
        DEFAULT_KEY_COL_WIDTH
    );
    name_value_println!("Gas limit", gas_limit.to_string(), DEFAULT_KEY_COL_WIDTH);
}

/// Result of a successful contract instantiation for displaying.
#[derive(serde::Serialize)]
pub struct InstantiateResult {
    /// Instantiated contract hash
    #[serde(skip_serializing_if = "Option::is_none")]
    pub contract: Option<String>,
    /// Instantiated code hash
    #[serde(skip_serializing_if = "Option::is_none")]
    pub code_hash: Option<String>,
    /// The events emitted from the instantiate extrinsic invocation.
    pub events: DisplayEvents,
}

impl InstantiateResult {
    pub fn to_json(&self) -> Result<String> {
        Ok(serde_json::to_string_pretty(self)?)
    }
}

pub fn print_instantiate_dry_run_result<Balance: Serialize>(
    result: &InstantiateDryRunResult<Balance>,
) {
    name_value_println!(
        "Result",
        format!("{}", result.result),
        DEFAULT_KEY_COL_WIDTH
    );
    name_value_println!(
        "Reverted",
        format!("{:?}", result.reverted),
        DEFAULT_KEY_COL_WIDTH
    );
    name_value_println!("Contract", result.contract, DEFAULT_KEY_COL_WIDTH);
    name_value_println!(
        "Gas consumed",
        result.gas_consumed.to_string(),
        DEFAULT_KEY_COL_WIDTH
    );
}