use crate::account_config::constants::CORE_CODE_ADDRESS;
use anyhow::{bail, Result};
use move_core_types::{
identifier::Identifier,
language_storage::{ModuleId, StructTag, TypeTag},
};
use once_cell::sync::Lazy;
pub const DIEM_MODULE_NAME: &str = "Diem";
static COIN_MODULE_NAME: Lazy<Identifier> =
Lazy::new(|| Identifier::new("Diem").unwrap());
pub static COIN_MODULE: Lazy<ModuleId> =
Lazy::new(|| ModuleId::new(CORE_CODE_ADDRESS, COIN_MODULE_NAME.clone()));
pub fn type_tag_for_currency_code(currency_code: Identifier) -> TypeTag {
TypeTag::Struct(StructTag {
address: CORE_CODE_ADDRESS,
module: currency_code.clone(),
name: currency_code,
type_params: vec![],
})
}
pub fn allowed_currency_code_string(
possible_currency_code_string: &str,
) -> bool {
possible_currency_code_string
.chars()
.all(|chr| matches!(chr, 'A'..='Z' | '0'..='9'))
&& Identifier::is_valid(possible_currency_code_string)
}
pub fn from_currency_code_string(
currency_code_string: &str,
) -> Result<Identifier> {
if !allowed_currency_code_string(currency_code_string) {
bail!("Invalid currency code '{}'", currency_code_string)
}
Identifier::new(currency_code_string)
}