use serde::{Deserialize, Serialize};
const PRICING_FILE: &str = ".lh_pricing.json";
#[derive(Debug, Clone, Serialize, Deserialize)]
struct PricingFile {
#[serde(default)]
per_turn_wei: String,
}
pub(crate) async fn load() -> Option<u128> {
let fs = super::shared_opfs();
let bytes = fs.read(PRICING_FILE).await.ok()?;
if bytes.is_empty() {
return None;
}
let parsed: PricingFile = serde_json::from_slice(&bytes).ok()?;
if parsed.per_turn_wei.is_empty() {
return Some(0);
}
parsed.per_turn_wei.parse::<u128>().ok()
}
pub(crate) async fn save(per_turn_wei: u128) -> Result<(), String> {
let fs = super::shared_opfs();
let body = PricingFile {
per_turn_wei: per_turn_wei.to_string(),
};
let bytes = serde_json::to_vec(&body).map_err(|e| format!("encode pricing: {e}"))?;
fs.write_atomic(PRICING_FILE, &bytes)
.await
.map_err(|e| format!("save pricing: {e}"))?;
Ok(())
}