renegade_sdk/renegade_wallet_client/actions/approvals.rs
1//! Helper methods for building ERC20 and Permit2 approval transactions
2
3use alloy::{
4 primitives::{Address, U160, U256, aliases::U48},
5 sol_types::SolCall,
6};
7use alloy_rpc_types_eth::{TransactionInput, TransactionRequest};
8
9use crate::{
10 client::RenegadeClient,
11 renegade_wallet_client::utils::{IAllowanceTransfer, approveCall},
12};
13
14impl RenegadeClient {
15 /// Build a transaction to approve the Permit2 contract as a spender for
16 /// the given ERC20 token.
17 ///
18 /// # Arguments
19 /// * `token` - The ERC20 token address to approve
20 /// * `amount` - The amount to approve for spending
21 ///
22 /// # Returns
23 /// A `TransactionRequest` that can be executed by the user with their
24 /// provider
25 pub fn build_erc20_approval_tx(&self, token: Address, amount: U256) -> TransactionRequest {
26 let calldata = approveCall { spender: self.get_permit2_address(), amount }.abi_encode();
27 TransactionRequest::default().to(token).input(TransactionInput::new(calldata.into()))
28 }
29
30 /// Build a transaction to approve the darkpool as a spender through
31 /// Permit2's AllowanceTransfer interface.
32 ///
33 /// # Arguments
34 /// * `token` - The ERC20 token address to approve
35 /// * `amount` - The amount to approve for spending (uint160)
36 /// * `expiration` - The Unix timestamp when this approval expires (uint48)
37 ///
38 /// # Returns
39 /// A `TransactionRequest` that can be executed by the user with their
40 /// provider
41 pub fn build_permit2_allowance_tx(
42 &self,
43 token: Address,
44 amount: U160,
45 expiration: U48,
46 ) -> TransactionRequest {
47 let calldata = IAllowanceTransfer::approveCall {
48 token,
49 spender: self.get_darkpool_address(),
50 amount,
51 expiration,
52 }
53 .abi_encode();
54 TransactionRequest::default()
55 .to(self.get_permit2_address())
56 .input(TransactionInput::new(calldata.into()))
57 }
58}