use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
pub type ContractName = String;
#[derive(Debug, Serialize, Deserialize)]
pub struct ContractMap {
map: HashMap<ContractName, DeployInfo>,
}
#[derive(Debug, Serialize, Deserialize)]
struct DeployInfo {
code_id: u64,
address: Option<String>,
}
impl ContractMap {
pub fn new(code_map: &HashMap<String, u64>) -> Self {
let mut map = HashMap::new();
for (name, code_id) in code_map {
map.insert(
name.clone(),
DeployInfo {
code_id: *code_id,
address: None,
},
);
}
Self { map }
}
pub fn register_contract(&mut self, name: String, code_id: u64) {
self.map.insert(
name,
DeployInfo {
code_id,
address: None,
},
);
}
pub fn code_id(&self, name: &str) -> Result<u64> {
let info = self.map.get(name).context("contract not stored")?;
Ok(info.code_id)
}
pub fn address(&self, name: &str) -> Result<String> {
self.map
.get(name)
.context("contract not stored")?
.address
.clone()
.context("contract not deployed")
}
pub fn add_address(&mut self, name: &str, address: String) -> Result<()> {
self.map
.get_mut(name)
.context("contract not stored")?
.address = Some(address);
Ok(())
}
pub fn clear_addresses(&mut self) {
for (_n, d) in self.map.iter_mut() {
d.address = None
}
}
}