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
//! Module with structs for handling gas reports.
use crate::prelude::*;
use crate::{Address, CallDef};
use casper_types::U512;
use core::fmt::{Debug, Display, Formatter, Result};

/// A Vector of deploy reports makes a full gas report.
pub type GasReport = Vec<DeployReport>;

/// Represents a deploy report, which includes the gas used and the deploy details.
#[cfg_attr(
    not(target_arch = "wasm32"),
    derive(serde::Serialize, serde::Deserialize)
)]
#[derive(Clone, Debug)]
pub enum DeployReport {
    /// Represents a Wasm deploy.
    WasmDeploy {
        /// The gas used for the deploy.
        gas: U512,
        /// The file name of the deployed WASM.
        file_name: String
    },
    /// Represents a contract call.
    ContractCall {
        /// The gas used for the call.
        gas: U512,
        /// The address of the contract called.
        contract_address: Address,
        /// The call definition.
        call_def: CallDef
    }
}

impl Display for DeployReport {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        match self {
            DeployReport::WasmDeploy { gas: _, file_name } => {
                write!(f, "Wasm deploy: {}", file_name)
            }
            DeployReport::ContractCall {
                gas: _,
                contract_address: _,
                call_def
            } => {
                write!(f, "Contract call: {}", call_def.entry_point(),)
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Address::Account;
    use casper_types::account::AccountHash;
    use casper_types::RuntimeArgs;

    #[test]
    fn test_deploy_report_display() {
        let wasm_deploy = DeployReport::WasmDeploy {
            gas: U512::from(1000),
            file_name: String::from("test.wasm")
        };
        assert_eq!(format!("{}", wasm_deploy), "Wasm deploy: test.wasm");

        let contract_call = DeployReport::ContractCall {
            gas: U512::from(1000),
            contract_address: Account(AccountHash([0; 32])),
            call_def: CallDef::new("test", false, RuntimeArgs::new())
        };
        assert_eq!(format!("{}", contract_call), "Contract call: test");
    }
}