Skip to main content

contract_extrinsics/
extrinsic_opts.rs

1// Copyright (C) Use Ink (UK) Ltd.
2// This file is part of cargo-contract.
3//
4// cargo-contract is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// cargo-contract is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with cargo-contract.  If not, see <http://www.gnu.org/licenses/>.
16
17use anyhow::Result;
18use contract_build::Verbosity;
19use derivative::Derivative;
20use ink_env::Environment;
21use subxt::{
22    Config,
23    tx,
24};
25use url::Url;
26
27use crate::{
28    ContractArtifacts,
29    url_to_string,
30};
31use std::{
32    marker::PhantomData,
33    option::Option,
34    path::PathBuf,
35};
36
37/// Arguments required for creating and sending an extrinsic to a Substrate node.
38#[derive(Derivative)]
39#[derivative(Clone(bound = "E::Balance: Clone"))]
40pub struct ExtrinsicOpts<C: Config, E: Environment, Signer: Clone> {
41    file: Option<PathBuf>,
42    manifest_path: Option<PathBuf>,
43    url: url::Url,
44    signer: Signer,
45    storage_deposit_limit: Option<E::Balance>,
46    verbosity: Verbosity,
47    _marker: PhantomData<C>,
48}
49
50pub struct ExtrinsicOptsBuilder<C: Config, E: Environment, Signer: Clone> {
51    opts: ExtrinsicOpts<C, E, Signer>,
52}
53
54impl<C: Config, E: Environment, Signer> ExtrinsicOptsBuilder<C, E, Signer>
55where
56    Signer: tx::Signer<C> + Clone,
57{
58    /// Returns a clean builder for [`ExtrinsicOpts`].
59    pub fn new(signer: Signer) -> ExtrinsicOptsBuilder<C, E, Signer> {
60        ExtrinsicOptsBuilder {
61            opts: ExtrinsicOpts {
62                file: None,
63                manifest_path: None,
64                url: url::Url::parse("ws://localhost:9944").unwrap(),
65                signer,
66                storage_deposit_limit: None,
67                verbosity: Verbosity::Default,
68                _marker: PhantomData,
69            },
70        }
71    }
72
73    /// Sets the path to the contract build artifact file.
74    pub fn file<T: Into<PathBuf>>(self, file: Option<T>) -> Self {
75        let mut this = self;
76        this.opts.file = file.map(|f| f.into());
77        this
78    }
79
80    /// Sets the path to the Cargo.toml of the contract.
81    pub fn manifest_path<T: Into<PathBuf>>(self, manifest_path: Option<T>) -> Self {
82        let mut this = self;
83        this.opts.manifest_path = manifest_path.map(|f| f.into());
84        this
85    }
86
87    /// Sets the websockets url of a Substrate node.
88    pub fn url<T: Into<Url>>(self, url: T) -> Self {
89        let mut this = self;
90        this.opts.url = url.into();
91        this
92    }
93
94    /// Sets the maximum amount of balance that can be charged from the caller to pay for
95    /// storage.
96    pub fn storage_deposit_limit(
97        self,
98        storage_deposit_limit: Option<E::Balance>,
99    ) -> Self {
100        let mut this = self;
101        this.opts.storage_deposit_limit = storage_deposit_limit;
102        this
103    }
104
105    /// Set the verbosity level.
106    pub fn verbosity(self, verbosity: Verbosity) -> Self {
107        let mut this = self;
108        this.opts.verbosity = verbosity;
109        this
110    }
111
112    pub fn done(self) -> ExtrinsicOpts<C, E, Signer> {
113        self.opts
114    }
115}
116
117impl<C: Config, E: Environment, Signer> ExtrinsicOpts<C, E, Signer>
118where
119    Signer: tx::Signer<C> + Clone,
120{
121    /// Load contract artifacts.
122    pub fn contract_artifacts(&self) -> Result<ContractArtifacts> {
123        ContractArtifacts::from_manifest_or_file(
124            self.manifest_path.as_ref(),
125            self.file.as_ref(),
126        )
127    }
128
129    /// Sets a new storage deposit limit.
130    pub fn set_storage_deposit_limit(&mut self, limit: Option<E::Balance>) {
131        self.storage_deposit_limit = limit;
132    }
133
134    /// Return the file path of the contract artifact.
135    pub fn file(&self) -> Option<&PathBuf> {
136        self.file.as_ref()
137    }
138
139    /// Return the path to the `Cargo.toml` of the contract.
140    pub fn manifest_path(&self) -> Option<&PathBuf> {
141        self.manifest_path.as_ref()
142    }
143
144    /// Return the URL of the Substrate node.
145    pub fn url(&self) -> String {
146        url_to_string(&self.url)
147    }
148
149    /// Return the signer.
150    pub fn signer(&self) -> &Signer {
151        &self.signer
152    }
153
154    /// Return the storage deposit limit.
155    pub fn storage_deposit_limit(&self) -> Option<E::Balance> {
156        self.storage_deposit_limit
157    }
158
159    /// Verbosity for message reporting.
160    pub fn verbosity(&self) -> &Verbosity {
161        &self.verbosity
162    }
163}