Skip to main content

hypercall/client/wallet_client/
signing.rs

1use super::WalletClient;
2use alloy::signers::{local::PrivateKeySigner, Signer};
3use hypercall_types::WalletAddress;
4use std::str::FromStr;
5
6impl WalletClient {
7    /// Sign a PlaceOrder action with EIP-712
8    pub(super) async fn sign_place_order(
9        &self,
10        symbol: &str,
11        side: &str,
12        size: &str,
13        price: &str,
14        tif: &str,
15        client_id: &str,
16        nonce: u64,
17    ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
18        use hypercall_auth::PlaceOrder;
19
20        let wallet_addr = self.wallet_address.inner();
21
22        tracing::debug!(
23            "Signing PlaceOrder: wallet={}, symbol={}, side={}, size={}, price={}, tif={}, client_id={}, nonce={}",
24            self.wallet_address, symbol, side, size, price, tif, client_id, nonce
25        );
26
27        let message = PlaceOrder {
28            wallet: wallet_addr,
29            symbol: symbol.to_string(),
30            side: side.to_string(),
31            size: size.to_string(),
32            price: price.to_string(),
33            tif: tif.to_string(),
34            clientId: client_id.to_string(),
35            nonce,
36        };
37
38        let signer = PrivateKeySigner::from_str(&self.private_key)?;
39        tracing::debug!("Signer address: {:?}", signer.address());
40
41        let domain = self.hypercall_domain();
42        let signature = signer.sign_typed_data(&message, &domain).await?;
43
44        tracing::debug!("Generated signature: {}", signature);
45
46        Ok(format!("{}", signature))
47    }
48
49    /// Sign a CancelOrder action with EIP-712
50    pub(super) async fn sign_cancel_order(
51        &self,
52        order_id: &str,
53        nonce: u64,
54    ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
55        use hypercall_auth::CancelOrder;
56
57        let wallet_addr = self.wallet_address.inner();
58        let message = CancelOrder {
59            wallet: wallet_addr,
60            orderId: order_id.to_string(),
61            nonce,
62        };
63
64        let signer = PrivateKeySigner::from_str(&self.private_key)?;
65        let domain = self.hypercall_domain();
66        let signature = signer.sign_typed_data(&message, &domain).await?;
67
68        Ok(format!("{}", signature))
69    }
70
71    /// Sign an option withdrawal action with EIP-712.
72    pub async fn sign_withdraw_option(
73        &self,
74        account: WalletAddress,
75        symbol: &str,
76        amount: &str,
77        nonce: u64,
78    ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
79        use hypercall_auth::eip712::WithdrawOption;
80
81        let message = WithdrawOption {
82            wallet: self.wallet_address.inner(),
83            account: account.inner(),
84            symbol: symbol.to_string(),
85            amount: amount.to_string(),
86            nonce,
87        };
88
89        let signer = PrivateKeySigner::from_str(&self.private_key)?;
90        let domain = self.hypercall_domain();
91        let signature = signer.sign_typed_data(&message, &domain).await?;
92
93        Ok(format!("{}", signature))
94    }
95
96    pub async fn sign_withdraw_usdc(
97        &self,
98        account: WalletAddress,
99        destination: WalletAddress,
100        amount: &str,
101        nonce: u64,
102    ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
103        use hypercall_auth::eip712::WithdrawUsdc;
104
105        let message = WithdrawUsdc {
106            wallet: self.wallet_address.inner(),
107            account: account.inner(),
108            destination: destination.inner(),
109            amount: amount.to_string(),
110            nonce,
111        };
112
113        let signer = PrivateKeySigner::from_str(&self.private_key)?;
114        let domain = self.hypercall_domain();
115        let signature = signer.sign_typed_data(&message, &domain).await?;
116
117        Ok(format!("{}", signature))
118    }
119
120    /// Sign a SetMarginMode action with EIP-712
121    pub async fn sign_set_margin_mode(
122        &self,
123        margin_mode: &str,
124        nonce: u64,
125    ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
126        use hypercall_auth::SetMarginMode;
127
128        let wallet_addr = self.wallet_address.inner();
129        let message = SetMarginMode {
130            wallet: wallet_addr,
131            marginMode: margin_mode.to_string(),
132            nonce,
133        };
134
135        let signer = PrivateKeySigner::from_str(&self.private_key)?;
136        let domain = self.hypercall_domain();
137        let signature = signer.sign_typed_data(&message, &domain).await?;
138
139        Ok(format!("{}", signature))
140    }
141}