derive-rs 0.1.14

Rust SDK for Derive.xyz
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use std::sync::Arc;

use alloy::{
    primitives::{Address, TxHash, U256},
    providers::{Provider, ProviderBuilder},
    signers::local::PrivateKeySigner,
    sol,
};
use bigdecimal::BigDecimal;
use bon::Builder;
use dashmap::DashMap;
use reqwest::Url;
use serde::Deserialize;
use strum::EnumString;
use tracing::debug;

use crate::{
    actions::utils::decimal_to_u256_with_prec,
    models::SpotAssetEntry,
    types::{ClientError, Environment},
};
const ONCHAIN_ACTION_MANAGER: &str = "0x1b4f369b585D40a27F66775844FC265151f278A4";

#[derive(Clone, Debug, Deserialize, EnumString)]
#[strum(ascii_case_insensitive)]
pub enum SupportDepositAssets {
    USDH,
    XAUT,
    SFP,
    DAI,
    SDAI,
    CBADA,
    KHYPE,
    RSETH,
    ETH,
    HYPE,
    OLAS,
    WSTETH,
    WEETH,
    FXUSDC,
    RSWETH,
    SUSDE,
    OP,
    EBTC,
    USDC,
    DRV,
    USDT,
    WBTC,
    JITOSOL,
    LBTC,
    USDE,
    SOL,
    CBBTC,
    AAVE,
}

#[derive(Clone, Debug, Deserialize)]
pub enum DirectDepositType {
    Deposit,
    DepositToNewSubaccount,
}

#[derive(Clone, Debug, Deserialize)]
pub enum DepositTypes {
    Direct(DirectDepositType),
}

sol! {
     #[sol(rpc)]
    contract OnchainActionManager {
        function deposit(
            address asset,
            uint256 amount,
            uint64 subaccountId,
            address fallbackRecipient
        ) returns (uint256 actionId);

        function depositToNewSubaccount(
            address asset,
            uint256 amount,
            uint32 managerId,
            address owner
        ) returns (uint256 actionId);

        function submit(
            uint256 actionType,
            bytes data
        ) payable returns (uint256 actionId);
    }
}

sol! {
    #[sol(rpc)]
    contract Erc20 {
        function approve(address spender, uint256 amount) returns (bool);
        function allowance(address owner, address spender) view returns (uint256);

        function balanceOf(address account) view returns (uint256);
    }
}

#[derive(Clone, Debug, Deserialize, Builder)]
pub struct DepositArgs {
    #[builder(into)]
    asset: SupportDepositAssets,
    amount: BigDecimal,
    #[builder(into)]
    recipient: Address,
    subaccount_id: Option<u64>,
    manager_id: Option<u32>,
    rpc_provider: Option<String>,
    // we use a default to specific subaccount
    #[builder(default=DepositTypes::Direct(DirectDepositType::Deposit))]
    deposit_type: DepositTypes,
}

struct AssetInfo {
    token_address: Address,
    underlying_erc20: Address,
    decimals: u8,
}

pub struct DepositManager {
    deposit_args: DepositArgs,
    private_key: PrivateKeySigner,
    wallet_address: Address,
    erc20_cache: Arc<DashMap<String, SpotAssetEntry>>,
    rpc_url: Url,
    manager_address: Address,
}

impl DepositManager {
    pub fn new(
        deposit_args: &DepositArgs,
        private_key: &PrivateKeySigner,
        wallet_address: &str,
        env: &Environment,
        erc20_cache: Arc<DashMap<String, SpotAssetEntry>>,
    ) -> Result<Self, ClientError> {
        let rpc_url = deposit_args
            .rpc_provider
            .clone()
            .unwrap_or_else(|| env.get_default_rpc())
            .parse::<Url>()
            .map_err(Self::string_error)?;

        let wallet_address = wallet_address
            .parse::<Address>()
            .map_err(Self::string_error)?;

        let manager_address = ONCHAIN_ACTION_MANAGER
            .parse::<Address>()
            .map_err(Self::string_error)?;

        Ok(Self {
            deposit_args: deposit_args.clone(),
            private_key: private_key.clone(),
            wallet_address,
            erc20_cache,
            rpc_url,
            manager_address,
        })
    }

    pub async fn deposit(&self) -> Result<Vec<TxHash>, ClientError> {
        let asset = &self.deposit_args.asset;
        let amount = &self.deposit_args.amount;

        if !self.check_balance(asset, amount).await? {
            return Err(ClientError::InsufficientBalance(format!(
                "Insufficient balance for {:?}: required {}",
                asset, amount
            )));
        }

        let mut tx_hashes = Vec::with_capacity(2);

        if !self.check_allowance(asset, amount).await? {
            let approval_hash = self.approve(asset, amount).await?;
            tx_hashes.push(approval_hash);
        }

        match &self.deposit_args.deposit_type {
            DepositTypes::Direct(deposit_type) => match deposit_type {
                DirectDepositType::Deposit => {
                    if self.deposit_args.subaccount_id.is_some() {
                        let tx_hash = self.send_deposit().await?;
                        tx_hashes.push(tx_hash);
                    } else {
                        return Err(ClientError::SubaccountError(
                            "Subaccount ID must be passed for direct deposit to subaccount"
                                .to_string(),
                        ));
                    }
                }

                DirectDepositType::DepositToNewSubaccount => {
                    if self.deposit_args.subaccount_id.is_some() {
                        return Err(ClientError::SubaccountError(
                            "Subaccount ID should not be passed required for DepositToNewSubaccount".into(),
                        ));
                    }

                    if let Some(manager_id) = self.deposit_args.manager_id {
                        let tx_hash = self.send_deposit_to_new_subaccount(manager_id).await?;
                        tx_hashes.push(tx_hash);
                    } else {
                        return Err(ClientError::SubaccountError(
                            "Manager ID must be passed for DepositToNewSubaccount".into(),
                        ));
                    }
                }
            },
        }

        Ok(tx_hashes)
    }

    pub async fn check_allowance(
        &self,
        asset: &SupportDepositAssets,
        amount: &BigDecimal,
    ) -> Result<bool, ClientError> {
        let asset_info = self.asset_info(asset)?;
        let required = self.amount_to_units(amount, asset_info.decimals)?;

        let provider = ProviderBuilder::new().connect_http(self.rpc_url.clone());
        let erc20 = Erc20::new(asset_info.underlying_erc20, provider);

        let allowance = erc20
            .allowance(self.wallet_address, self.manager_address)
            .call()
            .await
            .map_err(Self::string_error)?;

        Ok(allowance >= required)
    }

    pub async fn check_balance(
        &self,
        asset: &SupportDepositAssets,
        amount: &BigDecimal,
    ) -> Result<bool, ClientError> {
        let asset_info = self.asset_info(asset)?;
        let required = self.amount_to_units(amount, asset_info.decimals)?;

        let provider = ProviderBuilder::new().connect_http(self.rpc_url.clone());
        let erc20 = Erc20::new(asset_info.underlying_erc20, provider);

        let balance = erc20
            .balanceOf(self.wallet_address)
            .call()
            .await
            .map_err(Self::string_error)?;

        Ok(balance >= required)
    }

    pub async fn approve(
        &self,
        asset: &SupportDepositAssets,
        amount: &BigDecimal,
    ) -> Result<TxHash, ClientError> {
        let asset_info = self.asset_info(asset)?;
        let amount = self.amount_to_units(amount, asset_info.decimals)?;

        let provider = ProviderBuilder::new()
            .wallet(self.private_key.clone())
            .connect_http(self.rpc_url.clone());

        let fees = provider
            .estimate_eip1559_fees()
            .await
            .map_err(Self::string_error)?;
        let erc20 = Erc20::new(asset_info.underlying_erc20, provider);

        let pending_tx = erc20
            .approve(self.manager_address, amount)
            .max_priority_fee_per_gas(fees.max_priority_fee_per_gas * 2)
            .max_fee_per_gas(fees.max_fee_per_gas * 2)
            .send()
            .await
            .map_err(Self::string_error)?;

        let tx_hash = *pending_tx.tx_hash();
        debug!("Approval transaction sent: {:?}", tx_hash);
        pending_tx.get_receipt().await.map_err(Self::string_error)?;
        Ok(tx_hash)
    }

    async fn send_deposit(&self) -> Result<TxHash, ClientError> {
        let asset_info = self.asset_info(&self.deposit_args.asset)?;
        let amount = self.amount_to_units(&self.deposit_args.amount, asset_info.decimals)?;

        let subaccount_id = self.deposit_args.subaccount_id.ok_or_else(|| {
            ClientError::SubaccountError("Subaccount ID is required for this deposit type".into())
        })?;

        let provider = ProviderBuilder::new()
            .wallet(self.private_key.clone())
            .connect_http(self.rpc_url.clone());

        let fees = provider
            .estimate_eip1559_fees()
            .await
            .map_err(Self::string_error)?;
        let manager = OnchainActionManager::new(self.manager_address, provider);

        let pending_tx = manager
            .deposit(
                asset_info.token_address,
                amount,
                subaccount_id,
                self.deposit_args.recipient,
            )
            .max_priority_fee_per_gas(fees.max_priority_fee_per_gas * 2)
            .max_fee_per_gas(fees.max_fee_per_gas * 2)
            .send()
            .await
            .map_err(Self::string_error)?;

        let tx_hash = *pending_tx.tx_hash();
        debug!("Deposit transaction sent: {:?}", tx_hash);
        pending_tx.get_receipt().await.map_err(Self::string_error)?;
        Ok(tx_hash)
    }

    async fn send_deposit_to_new_subaccount(&self, manager_id: u32) -> Result<TxHash, ClientError> {
        let asset_info = self.asset_info(&self.deposit_args.asset)?;
        let amount = self.amount_to_units(&self.deposit_args.amount, asset_info.decimals)?;

        let provider = ProviderBuilder::new()
            .wallet(self.private_key.clone())
            .connect_http(self.rpc_url.clone());

        let fees = provider
            .estimate_eip1559_fees()
            .await
            .map_err(Self::string_error)?;
        let manager = OnchainActionManager::new(self.manager_address, provider);

        let pending_tx = manager
            .depositToNewSubaccount(
                asset_info.token_address,
                amount,
                manager_id,
                self.deposit_args.recipient,
            )
            .max_priority_fee_per_gas(fees.max_priority_fee_per_gas * 2)
            .max_fee_per_gas(fees.max_fee_per_gas * 2)
            .send()
            .await
            .map_err(Self::string_error)?;

        let tx_hash = *pending_tx.tx_hash();
        debug!("Deposit to new subaccount transaction sent: {:?}", tx_hash);
        pending_tx.get_receipt().await.map_err(Self::string_error)?;
        Ok(tx_hash)
    }

    fn asset_info(&self, asset: &SupportDepositAssets) -> Result<AssetInfo, ClientError> {
        let key = format!("{asset:?}");

        let entry = self.erc20_cache.get(&key).ok_or_else(|| {
            Self::message_error(format!(
                "Asset {:?} is not present in the ERC20 cache",
                asset
            ))
        })?;

        let token_address = entry
            .address
            .parse::<Address>()
            .map_err(Self::string_error)?;

        let underlying_erc20 = entry
            .erc20
            .underlying_erc20
            .as_deref()
            .ok_or_else(|| {
                Self::message_error(format!("Asset {:?} has no underlying ERC20 address", asset))
            })?
            .parse::<Address>()
            .map_err(Self::string_error)?;

        Ok(AssetInfo {
            token_address,
            underlying_erc20,
            decimals: entry.erc20.decimals,
        })
    }

    fn amount_to_units(&self, amount: &BigDecimal, decimals: u8) -> Result<U256, ClientError> {
        decimal_to_u256_with_prec(amount, decimals as u32).map_err(|_| {
            Self::message_error(format!(
                "Failed to convert amount {} to U256 with {} decimals",
                amount, decimals
            ))
        })
    }

    fn string_error<E>(error: E) -> ClientError
    where
        E: std::error::Error + Send + Sync + 'static,
    {
        ClientError::StringError(Box::new(error))
    }

    fn message_error(message: String) -> ClientError {
        ClientError::StringError(Box::new(std::io::Error::other(message)))
    }
}