casper_client/types/
contract.rs

1use std::fmt::{self, Display, Formatter};
2
3use itertools::Itertools;
4use serde::{Deserialize, Serialize};
5
6use casper_types::{ContractPackageHash, ContractWasmHash, EntryPoint, ProtocolVersion};
7
8use super::NamedKey;
9
10/// Details of a smart contract.
11#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
12#[serde(deny_unknown_fields)]
13pub struct Contract {
14    contract_package_hash: ContractPackageHash,
15    contract_wasm_hash: ContractWasmHash,
16    named_keys: Vec<NamedKey>,
17    entry_points: Vec<EntryPoint>,
18    protocol_version: ProtocolVersion,
19}
20
21impl Contract {
22    /// Returns the contract package hash of the contract.
23    pub fn contract_package_hash(&self) -> &ContractPackageHash {
24        &self.contract_package_hash
25    }
26
27    /// Returns the contract Wasm hash of the contract.
28    pub fn contract_wasm_hash(&self) -> &ContractWasmHash {
29        &self.contract_wasm_hash
30    }
31
32    /// Returns the named keys of the contract.
33    pub fn named_keys(&self) -> impl Iterator<Item = &NamedKey> {
34        self.named_keys.iter()
35    }
36
37    /// Returns the entry-points of the contract.
38    pub fn entry_points(&self) -> impl Iterator<Item = &EntryPoint> {
39        self.entry_points.iter()
40    }
41
42    /// Returns the protocol version of the contract.
43    pub fn protocol_version(&self) -> &ProtocolVersion {
44        &self.protocol_version
45    }
46}
47
48impl Display for Contract {
49    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
50        write!(
51            formatter,
52            "contract {{ package-hash {}, wasm-hash {}, named keys {{{}}}, entry-points \
53            {{{}}}, protocol-version: {} }}",
54            self.contract_package_hash,
55            self.contract_wasm_hash,
56            self.named_keys.iter().format(", "),
57            self.entry_points
58                .iter()
59                .format_with(", ", |entry_point, fmt_fn| {
60                    fmt_fn(&format_args!(
61                        "{{ {}, parameters {{{}}}, ret {:?}, access {:?}, type {:?} }}",
62                        entry_point.name(),
63                        entry_point
64                            .args()
65                            .iter()
66                            .format_with(", ", |param, fmt_fn| {
67                                fmt_fn(&format_args!(
68                                    "{{ {}, {:?} }}",
69                                    param.name(),
70                                    param.cl_type()
71                                ))
72                            }),
73                        entry_point.ret(),
74                        entry_point.access(),
75                        entry_point.entry_point_type()
76                    ))
77                }),
78            self.protocol_version
79        )
80    }
81}