use crate::error::Result;
use alloy_primitives::{hex::encode_prefixed, Address, U256};
use alloy_sol_types::{eip712_domain, sol, SolStruct};
sol! {
struct ClobAuth {
address address;
string timestamp;
uint256 nonce;
string message;
}
}
sol! {
struct Order {
uint256 salt;
address maker;
address signer;
address taker;
uint256 tokenId;
uint256 makerAmount;
uint256 takerAmount;
uint256 expiration;
uint256 nonce;
uint256 feeRateBps;
uint8 side;
uint8 signatureType;
}
}
pub fn sign_clob_auth_message<T>(
signer: &T,
timestamp: String,
nonce: U256,
chain_id: u64,
) -> Result<String>
where
T: alloy_signer::Signer + alloy_signer::SignerSync,
{
let message = "This message attests that I control the given wallet".to_owned();
let auth_struct = ClobAuth {
address: signer.address(),
timestamp,
nonce,
message,
};
let domain = eip712_domain!(
name: "ClobAuthDomain",
version: "1",
chain_id: chain_id,
);
let hash = auth_struct.eip712_signing_hash(&domain);
let signature = signer
.sign_hash_sync(&hash)
.map_err(|e| crate::error::Error::Signing(format!("Failed to sign auth message: {}", e)))?;
Ok(encode_prefixed(signature.as_bytes()))
}
pub fn sign_order_message<T>(
signer: &T,
order: Order,
chain_id: u64,
verifying_contract: Address,
) -> Result<String>
where
T: alloy_signer::Signer + alloy_signer::SignerSync,
{
let domain = eip712_domain!(
name: "Polymarket CTF Exchange",
version: "1",
chain_id: chain_id,
verifying_contract: verifying_contract,
);
let hash = order.eip712_signing_hash(&domain);
let signature = signer
.sign_hash_sync(&hash)
.map_err(|e| crate::error::Error::Signing(format!("Failed to sign order: {}", e)))?;
Ok(encode_prefixed(signature.as_bytes()))
}