use alloy_primitives::{Address, address, keccak256};
use super::types::CowShedHookParams;
pub const COW_SHED_1_0_0_VERSION: &str = "1.0.0";
pub const COW_SHED_1_0_1_VERSION: &str = "1.0.1";
pub const COW_SHED_LATEST_VERSION: &str = COW_SHED_1_0_1_VERSION;
pub const COW_SHED_FACTORY_V1_0_0: Address = address!("00E989b87700514118Fa55326CD1cCE82faebEF6");
pub const COW_SHED_FACTORY_V1_0_1: Address = address!("312f92fe5f1710408B20D52A374fa29e099cFA86");
pub const COW_SHED_IMPLEMENTATION_V1_0_0: Address =
address!("2CFFA8cf11B90C9F437567b86352169dF4009F73");
pub const COW_SHED_IMPLEMENTATION_V1_0_1: Address =
address!("a2704cf562ad418bf0453f4b662ebf6a2489ed88");
pub const COW_SHED_PROXY_CREATION_GAS: u64 = 360_000;
pub const COW_SHED_FACTORY_MAINNET: Address = address!("d8d3789083bb4b92b56dbda5b2ac8c5e4d3e7d30");
pub const COW_SHED_FACTORY_GNOSIS: Address = address!("d8d3789083bb4b92b56dbda5b2ac8c5e4d3e7d30");
#[derive(Debug, Clone)]
pub struct CowShedSdk {
chain_id: u64,
}
impl CowShedSdk {
#[must_use]
pub const fn new(chain_id: u64) -> Self {
Self { chain_id }
}
#[must_use]
pub const fn factory_address(&self) -> Option<Address> {
match self.chain_id {
1 => Some(COW_SHED_FACTORY_MAINNET),
100 => Some(COW_SHED_FACTORY_GNOSIS),
_ => None,
}
}
#[must_use]
pub fn encode_execute_hooks_calldata(params: &CowShedHookParams) -> Vec<u8> {
let sig = b"executeHooks((address,bytes,uint256,bool)[],bytes32,uint256,address,bytes)";
let h = keccak256(sig);
let sel = [h[0], h[1], h[2], h[3]];
let mut buf = Vec::with_capacity(68);
buf.extend_from_slice(&sel);
buf.extend_from_slice(params.nonce.as_slice());
buf.extend_from_slice(¶ms.deadline.to_be_bytes::<32>());
buf
}
pub fn build_hook(
&self,
_user: Address,
proxy: Address,
params: &CowShedHookParams,
) -> Result<crate::app_data::CowHook, crate::CowError> {
let calldata = Self::encode_execute_hooks_calldata(params);
let gas_limit = 100_000_u64 + 50_000_u64 * params.call_count() as u64;
Ok(crate::app_data::CowHook {
target: format!("{proxy:#x}"),
call_data: alloy_primitives::hex::encode(&calldata),
gas_limit: gas_limit.to_string(),
dapp_id: None,
})
}
}