use alloy::json_abi::JsonAbi;
use alloy::primitives::Bytes;
use serde::Deserialize;
use crate::error::EvmError;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ParalockContract {
Htlc,
AdaptorEscrow,
MockUsd,
}
impl ParalockContract {
pub const ALL: [Self; 3] = [Self::Htlc, Self::AdaptorEscrow, Self::MockUsd];
pub fn name(self) -> &'static str {
match self {
Self::Htlc => "ParalockHTLC",
Self::AdaptorEscrow => "ParalockAdaptorEscrow",
Self::MockUsd => "MockUSD",
}
}
pub fn artifact_json(self) -> &'static str {
match self {
Self::Htlc => include_str!("../artifacts/ParalockHTLC.json"),
Self::AdaptorEscrow => include_str!("../artifacts/ParalockAdaptorEscrow.json"),
Self::MockUsd => include_str!("../artifacts/MockUSD.json"),
}
}
pub fn artifact(self) -> Result<ContractArtifact, EvmError> {
let artifact: ContractArtifact = serde_json::from_str(self.artifact_json())
.map_err(|e| EvmError::Config(format!("embedded {} artifact is invalid: {e}", self.name())))?;
if artifact.contract_name != self.name() || artifact.bytecode.is_empty() {
return Err(EvmError::Config(format!("embedded {} artifact is inconsistent", self.name())));
}
Ok(artifact)
}
}
#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ContractArtifact {
pub contract_name: String,
pub source_name: String,
pub compiler: CompilerSettings,
pub abi: JsonAbi,
pub bytecode: Bytes,
pub deployed_bytecode: Bytes,
}
#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct CompilerSettings {
pub version: String,
pub evm_version: String,
pub optimizer: OptimizerSettings,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize)]
#[non_exhaustive]
pub struct OptimizerSettings {
pub enabled: bool,
pub runs: u64,
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
#[test]
fn embedded_artifacts_parse() {
for contract in ParalockContract::ALL {
let artifact = contract.artifact().unwrap();
assert_eq!(artifact.contract_name, contract.name());
assert!(!artifact.deployed_bytecode.is_empty());
assert!(artifact.compiler.optimizer.enabled);
}
}
}