hypercall-client 0.5.0

Client library for interacting with Hypercall trading platform
Documentation
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
//! Managed vaults: read vaults, their settlement history and a holder's
//! position, create a vault, and queue deposits and withdrawals. Every write
//! is EIP-712 signed in the Hypercall domain by the owner or holder it names.
//!
//! Deposits and withdrawals queue and fill at the next keeper settlement, at
//! the vault's NAV. Poll [`HypercallClient::get_vault_holder`] for the fill.

#![expect(
    clippy::result_large_err,
    reason = "these helpers preserve the established unboxed public ClientError contract"
)]

use alloy::sol;
use reqwest::Method;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};

use crate::api::HypercallClient;
use crate::error::{ClientError, Result};
use crate::wallet::{AccountAddress, HypercallWallet};

const API_ROUTE_VAULTS: &str = "/v1/vaults";

fn vault_route(vault_id: u64, suffix: &str) -> String {
    format!("{API_ROUTE_VAULTS}/{vault_id}{suffix}")
}

sol! {
    /// `POST /v1/vaults`, signed by the owner.
    #[derive(Debug, PartialEq, Eq)]
    struct CreateVault {
        address owner;
        uint64 salt;
        string initialSharePrice;
        string minSeedUsdc;
        string minDepositUsdc;
        string minWithdrawUsdc;
        uint64 cooldownMs;
        uint64 settleIntervalMs;
        uint64 withdrawalDeadlineMs;
        uint32 forcedUnwindSlippageBps;
        uint64 nonce;
    }

    /// `POST /v1/vaults/:id/deposit`, signed by the holder.
    #[derive(Debug, PartialEq, Eq)]
    struct VaultDeposit {
        address holder;
        uint64 vault;
        string amount;
        uint64 nonce;
    }

    /// `POST /v1/vaults/:id/withdraw`, signed by the holder.
    #[derive(Debug, PartialEq, Eq)]
    struct VaultWithdraw {
        address holder;
        uint64 vault;
        string shares;
        uint64 nonce;
    }

    /// `POST /v1/vaults/:id/cancel`, signed by the holder.
    #[derive(Debug, PartialEq, Eq)]
    struct VaultCancel {
        address holder;
        uint64 vault;
        uint64 nonce;
    }

    /// `POST /v1/vaults/:id/close`, signed by the owner.
    #[derive(Debug, PartialEq, Eq)]
    struct VaultClose {
        address owner;
        uint64 vault;
        uint64 nonce;
    }
}

/// The exact decimal string a request sends and signs: positive, normalized
/// (`60.50` signs as `60.5`).
fn signed_decimal(field: &str, value: Decimal) -> Result<String> {
    if value <= Decimal::ZERO {
        return Err(ClientError::InvalidInput(format!(
            "{field} must be positive"
        )));
    }
    Ok(value.normalize().to_string())
}

/// A vault's owner-signed terms.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct VaultConfigParams {
    pub initial_share_price: Decimal,
    pub min_seed_usdc: Decimal,
    pub min_deposit_usdc: Decimal,
    pub min_withdraw_usdc: Decimal,
    pub cooldown_ms: u64,
    pub settle_interval_ms: u64,
    pub withdrawal_deadline_ms: u64,
    pub forced_unwind_slippage_bps: u32,
}

#[derive(Debug, Serialize)]
struct VaultConfigBody {
    initial_share_price: String,
    min_seed_usdc: String,
    min_deposit_usdc: String,
    min_withdraw_usdc: String,
    cooldown_ms: u64,
    settle_interval_ms: u64,
    withdrawal_deadline_ms: u64,
    forced_unwind_slippage_bps: u32,
}

impl VaultConfigParams {
    fn body(&self) -> Result<VaultConfigBody> {
        Ok(VaultConfigBody {
            initial_share_price: signed_decimal("initial_share_price", self.initial_share_price)?,
            min_seed_usdc: signed_decimal("min_seed_usdc", self.min_seed_usdc)?,
            min_deposit_usdc: signed_decimal("min_deposit_usdc", self.min_deposit_usdc)?,
            min_withdraw_usdc: signed_decimal("min_withdraw_usdc", self.min_withdraw_usdc)?,
            cooldown_ms: self.cooldown_ms,
            settle_interval_ms: self.settle_interval_ms,
            withdrawal_deadline_ms: self.withdrawal_deadline_ms,
            forced_unwind_slippage_bps: self.forced_unwind_slippage_bps,
        })
    }
}

#[derive(Debug, Serialize)]
struct CreateVaultRequest {
    owner: AccountAddress,
    salt: u64,
    config: VaultConfigBody,
    nonce: u64,
    signature: String,
}

#[derive(Debug, Serialize)]
struct VaultHolderRequest {
    holder: AccountAddress,
    #[serde(skip_serializing_if = "Option::is_none")]
    amount: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    shares: Option<String>,
    nonce: u64,
    signature: String,
}

/// `POST /v1/vaults` response: the derived account and the owner's
/// `VaultManager` calldata.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct CreateVaultResponse {
    pub success: bool,
    pub request_id: String,
    pub vault_id: Option<u64>,
    pub account: AccountAddress,
    pub vault_manager: AccountAddress,
    /// Send with the HYPE creation deposit as `msg.value`.
    pub create_vault_calldata: String,
    /// Send once HyperCore has processed the creation deposit.
    pub activate_vault_calldata: String,
}

/// A queued deposit, withdrawal or cancel.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct VaultRequestResponse {
    pub success: bool,
    pub request_id: String,
    pub vault_id: u64,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct VaultSettlement {
    pub seq: u64,
    pub at_ms: u64,
    pub nav: Decimal,
    pub share_price: Decimal,
    pub total_shares: Decimal,
    pub paged: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct VaultMetadata {
    pub name: String,
    pub description: String,
    pub website_url: Option<String>,
    pub logo_url: Option<String>,
    pub updated_at_ms: u64,
}

/// One vault, from the engine snapshot.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct Vault {
    pub vault_id: u64,
    pub account: AccountAddress,
    pub owner: AccountAddress,
    /// `deploying`, `active`, `deposits_paused`, `halted`, `frozen` or
    /// `closed`.
    pub status: String,
    pub config: VaultConfigParams,
    pub total_shares: Decimal,
    pub holder_count: usize,
    pub queued_deposits: usize,
    pub queued_withdrawals: usize,
    pub last_settlement: Option<VaultSettlement>,
    pub metadata: Option<VaultMetadata>,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct VaultQueuedRequest {
    pub request_id: String,
    /// `deposit` or `withdrawal`.
    pub kind: String,
    /// USDC for a deposit, shares for a withdrawal.
    pub amount: Decimal,
    pub at_ms: u64,
}

/// One holder's shares and queued requests.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct VaultHolder {
    pub vault_id: u64,
    pub wallet: AccountAddress,
    pub free_shares: Decimal,
    pub locked_shares: Decimal,
    pub last_deposit_fill_ms: Option<u64>,
    pub queued: Vec<VaultQueuedRequest>,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct VaultSettlementsPage {
    pub vault_id: u64,
    pub settlements: Vec<VaultSettlement>,
    pub next_before_seq: Option<u64>,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct VaultFill {
    pub seq: u64,
    pub at_ms: u64,
    pub request_id: String,
    /// `redemption`, `mint`, `release` or `close_redemption`.
    pub kind: String,
    pub usdc: Decimal,
    pub shares: Decimal,
    pub share_price: Decimal,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct VaultHistoryPage {
    pub vault_id: u64,
    pub wallet: AccountAddress,
    pub fills: Vec<VaultFill>,
    pub next_before_seq: Option<u64>,
}

#[derive(Debug, Serialize)]
struct VaultCloseRequest {
    owner: AccountAddress,
    nonce: u64,
    signature: String,
}

/// One status transition (pause, resume, freeze, close).
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct VaultStatusChange {
    pub at_ms: u64,
    pub from: String,
    pub to: String,
    /// `admin`, `keeper` or `owner`.
    pub signer: String,
    pub signer_wallet: Option<AccountAddress>,
    pub cursor: u64,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct VaultStatusChangesPage {
    pub vault_id: u64,
    pub changes: Vec<VaultStatusChange>,
    /// Pass as `before` for the next page.
    pub next_before: Option<u64>,
}

/// A page of history: older than `before_seq`, at most `limit` settlements.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct VaultPage {
    pub before_seq: Option<u64>,
    pub limit: Option<u32>,
}

impl VaultPage {
    fn query(&self) -> Vec<(&'static str, String)> {
        let mut query = Vec::new();
        if let Some(before_seq) = self.before_seq {
            query.push(("before_seq", before_seq.to_string()));
        }
        if let Some(limit) = self.limit {
            query.push(("limit", limit.to_string()));
        }
        query
    }
}

impl HypercallWallet {
    /// Sign a `CreateVault` for this wallet as owner.
    pub async fn sign_create_vault(
        &self,
        salt: u64,
        config: &VaultConfigParams,
        nonce: u64,
    ) -> Result<String> {
        let body = config.body()?;
        let message = CreateVault {
            owner: self.address.as_wallet_address().inner(),
            salt,
            initialSharePrice: body.initial_share_price,
            minSeedUsdc: body.min_seed_usdc,
            minDepositUsdc: body.min_deposit_usdc,
            minWithdrawUsdc: body.min_withdraw_usdc,
            cooldownMs: body.cooldown_ms,
            settleIntervalMs: body.settle_interval_ms,
            withdrawalDeadlineMs: body.withdrawal_deadline_ms,
            forcedUnwindSlippageBps: body.forced_unwind_slippage_bps,
            nonce,
        };
        self.sign_hypercall_action(&message, "CreateVault").await
    }
}

impl HypercallClient {
    /// Every vault, in id order.
    pub async fn list_vaults(&self) -> Result<Vec<Vault>> {
        Self::send_reqwest_json(self.request(Method::GET, API_ROUTE_VAULTS), "list vaults").await
    }

    /// One vault.
    pub async fn get_vault(&self, vault_id: u64) -> Result<Vault> {
        Self::send_reqwest_json(
            self.request(Method::GET, &vault_route(vault_id, "")),
            "get vault",
        )
        .await
    }

    /// One holder's shares and queued requests.
    pub async fn get_vault_holder(
        &self,
        vault_id: u64,
        wallet: AccountAddress,
    ) -> Result<VaultHolder> {
        Self::send_reqwest_json(
            self.request(Method::GET, &vault_route(vault_id, "/me"))
                .query(&[("wallet", wallet.to_string())]),
            "get vault holder",
        )
        .await
    }

    /// A vault's settlements (NAV and share price per page), newest first.
    pub async fn get_vault_settlements(
        &self,
        vault_id: u64,
        page: VaultPage,
    ) -> Result<VaultSettlementsPage> {
        Self::send_reqwest_json(
            self.request(Method::GET, &vault_route(vault_id, "/settlements"))
                .query(&page.query()),
            "get vault settlements",
        )
        .await
    }

    /// One holder's settlement fills in a vault, newest first.
    pub async fn get_vault_history(
        &self,
        vault_id: u64,
        wallet: AccountAddress,
        page: VaultPage,
    ) -> Result<VaultHistoryPage> {
        let mut query = page.query();
        query.push(("wallet", wallet.to_string()));
        Self::send_reqwest_json(
            self.request(Method::GET, &vault_route(vault_id, "/history"))
                .query(&query),
            "get vault history",
        )
        .await
    }

    /// Create a vault owned by `owner`. Send the returned calldata from the
    /// owner to `VaultManager` to deploy and then activate it.
    pub async fn create_vault(
        &self,
        owner: &HypercallWallet,
        salt: u64,
        config: &VaultConfigParams,
    ) -> Result<CreateVaultResponse> {
        let nonce = owner.next_nonce();
        let signature = owner.sign_create_vault(salt, config, nonce).await?;
        let request = CreateVaultRequest {
            owner: owner.address,
            salt,
            config: config.body()?,
            nonce,
            signature,
        };
        Self::send_json_body(
            self.request(Method::POST, API_ROUTE_VAULTS),
            &request,
            "create vault",
        )
        .await
    }

    /// Queue a deposit of `amount` USDC of the holder's free engine cash.
    pub async fn deposit_to_vault(
        &self,
        holder: &HypercallWallet,
        vault_id: u64,
        amount: Decimal,
    ) -> Result<VaultRequestResponse> {
        let amount = signed_decimal("amount", amount)?;
        let nonce = holder.next_nonce();
        let message = VaultDeposit {
            holder: holder.address.as_wallet_address().inner(),
            vault: vault_id,
            amount: amount.clone(),
            nonce,
        };
        let signature = holder
            .sign_hypercall_action(&message, "VaultDeposit")
            .await?;
        self.post_holder_request(
            vault_id,
            "/deposit",
            VaultHolderRequest {
                holder: holder.address,
                amount: Some(amount),
                shares: None,
                nonce,
                signature,
            },
        )
        .await
    }

    /// Queue a withdrawal of `shares`.
    pub async fn withdraw_from_vault(
        &self,
        holder: &HypercallWallet,
        vault_id: u64,
        shares: Decimal,
    ) -> Result<VaultRequestResponse> {
        let shares = signed_decimal("shares", shares)?;
        let nonce = holder.next_nonce();
        let message = VaultWithdraw {
            holder: holder.address.as_wallet_address().inner(),
            vault: vault_id,
            shares: shares.clone(),
            nonce,
        };
        let signature = holder
            .sign_hypercall_action(&message, "VaultWithdraw")
            .await?;
        self.post_holder_request(
            vault_id,
            "/withdraw",
            VaultHolderRequest {
                holder: holder.address,
                amount: None,
                shares: Some(shares),
                nonce,
                signature,
            },
        )
        .await
    }

    /// A vault's status transitions, newest first. `page.before_seq` is a
    /// previous page's `next_before`; this route names it `before`.
    pub async fn get_vault_status_changes(
        &self,
        vault_id: u64,
        page: VaultPage,
    ) -> Result<VaultStatusChangesPage> {
        let mut query = Vec::new();
        if let Some(before) = page.before_seq {
            query.push(("before", before.to_string()));
        }
        if let Some(limit) = page.limit {
            query.push(("limit", limit.to_string()));
        }
        Self::send_reqwest_json(
            self.request(Method::GET, &vault_route(vault_id, "/status-changes"))
                .query(&query),
            "get vault status changes",
        )
        .await
    }

    /// Request that a vault close, as its owner. A deploying vault closes at
    /// once; a live one freezes, is unwound, and redeems every holder. Repeat
    /// the request to advance a frozen vault's close pages.
    pub async fn close_vault(
        &self,
        owner: &HypercallWallet,
        vault_id: u64,
    ) -> Result<VaultRequestResponse> {
        let nonce = owner.next_nonce();
        let message = VaultClose {
            owner: owner.address.as_wallet_address().inner(),
            vault: vault_id,
            nonce,
        };
        let signature = owner.sign_hypercall_action(&message, "VaultClose").await?;
        Self::send_json_body(
            self.request(Method::POST, &vault_route(vault_id, "/close")),
            &VaultCloseRequest {
                owner: owner.address,
                nonce,
                signature,
            },
            "close vault",
        )
        .await
    }

    /// Release every queued deposit and withdrawal the holder has in a vault.
    pub async fn cancel_vault_requests(
        &self,
        holder: &HypercallWallet,
        vault_id: u64,
    ) -> Result<VaultRequestResponse> {
        let nonce = holder.next_nonce();
        let message = VaultCancel {
            holder: holder.address.as_wallet_address().inner(),
            vault: vault_id,
            nonce,
        };
        let signature = holder
            .sign_hypercall_action(&message, "VaultCancel")
            .await?;
        self.post_holder_request(
            vault_id,
            "/cancel",
            VaultHolderRequest {
                holder: holder.address,
                amount: None,
                shares: None,
                nonce,
                signature,
            },
        )
        .await
    }

    async fn post_holder_request(
        &self,
        vault_id: u64,
        suffix: &str,
        request: VaultHolderRequest,
    ) -> Result<VaultRequestResponse> {
        Self::send_json_body(
            self.request(Method::POST, &vault_route(vault_id, suffix)),
            &request,
            "vault request",
        )
        .await
    }
}

#[cfg(test)]
#[path = "vaults_test.rs"]
mod tests;