Skip to main content

contract_extrinsics/
extrinsic_calls.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 crate::ContractBinary;
18use subxt::{
19    ext::scale_encode::EncodeAsType,
20    utils::{
21        H160,
22        H256,
23    },
24};
25
26/// Copied from `sp_weight` to additionally implement `scale_encode::EncodeAsType`.
27#[derive(Debug, EncodeAsType)]
28#[encode_as_type(crate_path = "subxt::ext::scale_encode")]
29pub(crate) struct Weight {
30    #[codec(compact)]
31    /// The weight of computational time used based on some reference hardware.
32    ref_time: u64,
33    #[codec(compact)]
34    /// The weight of storage space used by proof of validity.
35    proof_size: u64,
36}
37
38impl From<sp_weights::Weight> for Weight {
39    fn from(weight: sp_weights::Weight) -> Self {
40        Self {
41            ref_time: weight.ref_time(),
42            proof_size: weight.proof_size(),
43        }
44    }
45}
46
47impl core::fmt::Display for Weight {
48    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
49        write!(
50            f,
51            "Weight(ref_time: {}, proof_size: {})",
52            self.ref_time, self.proof_size
53        )
54    }
55}
56
57/// A raw call to `pallet-contracts`'s `remove_code`.
58#[derive(EncodeAsType)]
59#[encode_as_type(crate_path = "subxt::ext::scale_encode")]
60pub(crate) struct RemoveCode<Hash> {
61    code_hash: Hash,
62}
63
64impl<Hash> RemoveCode<Hash> {
65    pub fn new(code_hash: Hash) -> Self {
66        Self { code_hash }
67    }
68
69    pub fn build(self) -> subxt::tx::DefaultPayload<Self> {
70        subxt::tx::DefaultPayload::new("Revive", "remove_code", self)
71    }
72}
73
74/// A raw call to `pallet-contracts`'s `upload_code`.
75#[derive(Debug, EncodeAsType)]
76#[encode_as_type(crate_path = "subxt::ext::scale_encode")]
77pub struct UploadCode<Balance> {
78    code: Vec<u8>,
79    storage_deposit_limit: Balance,
80}
81
82impl<Balance> UploadCode<Balance> {
83    pub fn new(code: ContractBinary, storage_deposit_limit: Balance) -> Self {
84        Self {
85            code: code.0,
86            storage_deposit_limit,
87        }
88    }
89
90    pub fn build(self) -> subxt::tx::DefaultPayload<Self> {
91        subxt::tx::DefaultPayload::new("Revive", "upload_code", self)
92    }
93}
94
95/// A raw call to `pallet-contracts`'s `instantiate_with_code`.
96#[derive(Debug, EncodeAsType)]
97#[encode_as_type(crate_path = "subxt::ext::scale_encode")]
98pub struct InstantiateWithCode<Balance> {
99    #[codec(compact)]
100    value: Balance,
101    gas_limit: Weight,
102    #[codec(compact)]
103    storage_deposit_limit: Balance,
104    code: Vec<u8>,
105    data: Vec<u8>,
106    salt: Option<Vec<u8>>,
107}
108
109impl<Balance> InstantiateWithCode<Balance> {
110    pub fn new(
111        value: Balance,
112        gas_limit: sp_weights::Weight,
113        storage_deposit_limit: Balance,
114        code: Vec<u8>,
115        data: Vec<u8>,
116        salt: Option<Vec<u8>>,
117    ) -> Self {
118        Self {
119            value,
120            gas_limit: gas_limit.into(),
121            storage_deposit_limit,
122            code,
123            data,
124            salt,
125        }
126    }
127
128    pub fn build(self) -> subxt::tx::DefaultPayload<Self> {
129        subxt::tx::DefaultPayload::new("Revive", "instantiate_with_code", self)
130    }
131}
132
133/// A raw call to `pallet-contracts`'s `instantiate_with_code_hash`.
134#[derive(Debug, EncodeAsType)]
135#[encode_as_type(crate_path = "subxt::ext::scale_encode")]
136pub struct Instantiate<Balance> {
137    #[codec(compact)]
138    value: Balance,
139    gas_limit: Weight,
140    #[codec(compact)]
141    storage_deposit_limit: Balance,
142    code_hash: H256,
143    data: Vec<u8>,
144    salt: Option<[u8; 32]>,
145}
146
147impl<Balance> Instantiate<Balance> {
148    pub fn new(
149        value: Balance,
150        gas_limit: sp_weights::Weight,
151        storage_deposit_limit: Balance,
152        code_hash: H256,
153        data: Vec<u8>,
154        salt: Option<[u8; 32]>,
155    ) -> Self {
156        Self {
157            value,
158            gas_limit: gas_limit.into(),
159            storage_deposit_limit,
160            code_hash,
161            data,
162            salt,
163        }
164    }
165
166    pub fn build(self) -> subxt::tx::DefaultPayload<Self> {
167        subxt::tx::DefaultPayload::new("Revive", "instantiate", self)
168    }
169}
170
171/// A raw call to `pallet-contracts`'s `call`.
172#[derive(EncodeAsType)]
173#[encode_as_type(crate_path = "subxt::ext::scale_encode")]
174pub struct Call<Balance> {
175    dest: H160,
176    #[codec(compact)]
177    value: Balance,
178    gas_limit: Weight,
179    storage_deposit_limit: Balance,
180    data: Vec<u8>,
181}
182
183impl<Balance> Call<Balance> {
184    pub fn new(
185        dest: H160,
186        value: Balance,
187        gas_limit: sp_weights::Weight,
188        storage_deposit_limit: Balance,
189        data: Vec<u8>,
190    ) -> Self {
191        Self {
192            dest,
193            value,
194            gas_limit: gas_limit.into(),
195            storage_deposit_limit,
196            data,
197        }
198    }
199
200    pub fn build(self) -> subxt::tx::DefaultPayload<Self> {
201        subxt::tx::DefaultPayload::new("Revive", "call", self)
202    }
203}
204
205/// A raw call to `pallet-contracts`'s `instantiate_with_code_hash`.
206#[derive(Debug, EncodeAsType)]
207#[encode_as_type(crate_path = "subxt::ext::scale_encode")]
208pub(crate) struct MapAccount {}
209
210impl MapAccount {
211    pub fn new() -> Self {
212        Self {}
213    }
214
215    pub fn build(self) -> subxt::tx::DefaultPayload<Self> {
216        subxt::tx::DefaultPayload::new("Revive", "map_account", self)
217    }
218}