use alloy::{
primitives::{Address, FixedBytes, U256},
providers::Provider,
};
use crate::{
contracts::ValidationRegistry,
error::Result,
types::{ValidationStatus, ValidationSummary},
};
#[derive(Debug)]
pub struct Validation<P> {
address: Address,
provider: P,
}
impl<P: Provider> Validation<P> {
pub(crate) const fn new(provider: P, address: Address) -> Self {
Self { address, provider }
}
pub async fn submit_request(
&self,
validator_address: Address,
agent_id: U256,
request_uri: &str,
request_hash: FixedBytes<32>,
) -> Result<()> {
let contract = ValidationRegistry::new(self.address, &self.provider);
contract
.validationRequest(
validator_address,
agent_id,
request_uri.to_owned(),
request_hash,
)
.send()
.await?
.get_receipt()
.await?;
Ok(())
}
pub async fn submit_response(
&self,
request_hash: FixedBytes<32>,
response: u8,
response_uri: &str,
response_hash: FixedBytes<32>,
tag: &str,
) -> Result<()> {
let contract = ValidationRegistry::new(self.address, &self.provider);
contract
.validationResponse(
request_hash,
response,
response_uri.to_owned(),
response_hash,
tag.to_owned(),
)
.send()
.await?
.get_receipt()
.await?;
Ok(())
}
pub async fn get_validation_status(
&self,
request_hash: FixedBytes<32>,
) -> Result<ValidationStatus> {
let contract = ValidationRegistry::new(self.address, &self.provider);
let r = contract.getValidationStatus(request_hash).call().await?;
Ok(ValidationStatus {
validator_address: r.validatorAddress,
agent_id: r.agentId,
response: r.response,
response_hash: r.responseHash,
tag: r.tag,
last_update: r.lastUpdate,
})
}
pub async fn get_summary(
&self,
agent_id: U256,
validator_addresses: &[Address],
tag: &str,
) -> Result<ValidationSummary> {
let contract = ValidationRegistry::new(self.address, &self.provider);
let r = contract
.getSummary(agent_id, validator_addresses.to_vec(), tag.to_owned())
.call()
.await?;
Ok(ValidationSummary {
count: r.count,
avg_response: r.avgResponse,
})
}
pub async fn get_agent_validations(&self, agent_id: U256) -> Result<Vec<FixedBytes<32>>> {
let contract = ValidationRegistry::new(self.address, &self.provider);
Ok(contract.getAgentValidations(agent_id).call().await?)
}
pub async fn get_validator_requests(
&self,
validator_address: Address,
) -> Result<Vec<FixedBytes<32>>> {
let contract = ValidationRegistry::new(self.address, &self.provider);
Ok(contract
.getValidatorRequests(validator_address)
.call()
.await?)
}
pub async fn get_identity_registry(&self) -> Result<Address> {
let contract = ValidationRegistry::new(self.address, &self.provider);
Ok(contract.getIdentityRegistry().call().await?)
}
pub async fn get_version(&self) -> Result<String> {
let contract = ValidationRegistry::new(self.address, &self.provider);
Ok(contract.getVersion().call().await?)
}
}