use crate::prelude::*;
use crate::{Address, CallDef};
use casper_types::U512;
use core::fmt::{Debug, Display, Formatter, Result};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
pub struct GasReport(Vec<DeployReport>);
impl GasReport {
pub fn push(&mut self, report: DeployReport) {
self.0.push(report)
}
pub fn new() -> Self {
GasReport::default()
}
pub fn iter(&self) -> Iter<'_, DeployReport> {
self.0.iter()
}
}
impl Display for GasReport {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
for report in &self.0 {
writeln!(f, "{}", report)?;
}
Ok(())
}
}
impl IntoIterator for GasReport {
type Item = DeployReport;
type IntoIter = IntoIter<DeployReport>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum DeployReport {
WasmDeploy {
gas: U512,
file_name: String
},
ContractCall {
gas: U512,
contract_address: Address,
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: {} - {} CSPR",
file_name,
cspr_pretty_string(gas)
)
}
DeployReport::ContractCall { gas, call_def, .. } => {
write!(
f,
"Contract call: {} - {} CSPR",
call_def.entry_point(),
cspr_pretty_string(gas)
)
}
}
}
}
fn cspr_pretty_string(cspr: &U512) -> String {
let cspr_top = cspr / U512::from(1_000_000_000);
let cspr_bottom = cspr % U512::from(1_000_000_000); let cspr_bottom_str = format!("{:09}", cspr_bottom);
let mut cspr_bottom_str = cspr_bottom_str.trim_end_matches('0');
if cspr_bottom_str.is_empty() {
cspr_bottom_str = "0";
}
format!("{}.{}", cspr_top, cspr_bottom_str)
}
#[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 - 0.000001 CSPR"
);
let contract_call = DeployReport::ContractCall {
gas: U512::from(1_000_000_000),
contract_address: Account(AccountHash([0; 32])),
call_def: CallDef::new("test", false, RuntimeArgs::new())
};
assert_eq!(
format!("{}", contract_call),
"Contract call: test - 1.0 CSPR"
);
}
}