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
//! Direct operations its owner sends itself, recorded and followed by LaMarge, which relays
//! nothing there: a deposit from a wallet into its own chain's escrow venue, or a GMX withdrawal.
//! [`Session::direct_transfer`](crate::Session::direct_transfer) runs one;
//! [`Session::move_funds_with`](crate::Session::move_funds_with) runs a GMX withdrawal for a move
//! from GMX.
use alloy_primitives::{Address, B256};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use super::Directs;
use crate::{
Error, Result, deployments,
flows::check_direct_amounts,
model::{BridgeFunding, DirectKind, DirectOperation},
transport::{Call, ops},
types::{Amount, ClientId, MoveId, VenueId},
};
/// `POST /v1/direct`: an operation's terms, the same `client_operation_id` and terms finding it
/// again.
///
/// Persist it before the first call.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DirectRequest {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(crate) move_id: Option<MoveId>,
pub(crate) client_operation_id: ClientId,
pub(crate) venue: VenueId,
pub(crate) kind: DirectKind,
pub(crate) owner: Address,
pub(crate) recipient: Address,
pub(crate) token: Address,
pub(crate) amount: Amount,
pub(crate) min_out: Amount,
}
impl DirectRequest {
/// `amount` of `token` from `owner`'s wallet into `recipient`'s account on escrow venue
/// `venue` (Hyperliquid, Polymarket, GMX), crediting at least `min_out`: the venue's own
/// token whole (`min_out == amount`), or on Polymarket Polygon's native USDC, at least one
/// cent, converted by the escrow into at least its
/// [`conversion_minimum`](crate::flows::conversion_minimum) of pUSD.
///
/// The venue must advertise direct deposits (`EscrowChain::direct_deposit`), checked when the
/// deposit runs.
///
/// # Errors
///
/// [`Error::InvalidRequest`] for a venue settled inside itself or unknown, or amounts
/// [`check_direct_amounts`] refuses.
pub fn deposit(
id: ClientId,
venue: VenueId,
owner: Address,
recipient: Address,
token: Address,
amount: Amount,
min_out: Amount,
) -> Result<Self> {
let request = Self {
move_id: None,
client_operation_id: id,
venue,
kind: DirectKind::Deposit,
owner,
recipient,
token,
amount,
min_out,
};
request.validate()?;
Ok(request)
}
/// The checks [`Self::deposit`] makes, also for a request read back from storage: a deposit
/// into an escrow venue, or GMX's withdrawal to its owner, with amounts
/// [`check_direct_amounts`] takes.
pub(crate) fn validate(&self) -> Result<()> {
let escrow_venue = deployments::chain(&self.venue).is_some()
&& !deployments::is_venue_settled(&self.venue);
let deposit = match self.kind {
DirectKind::Deposit if escrow_venue => true,
DirectKind::Withdrawal
if self.venue == VenueId::Gmx && self.recipient == self.owner =>
{
false
}
_ => {
let message = "a direct deposit into an escrow venue, or a GMX withdrawal";
return Err(Error::InvalidRequest(message.into()));
}
};
check_direct_amounts(&self.venue, self.token, self.amount, self.min_out, deposit).map_err(
|error| Error::InvalidRequest(format!("direct operation terms: {error}").into()),
)
}
/// `amount` of GMX's settlement token (Arbitrum USDC), out of `owner`'s GMX Account to
/// `owner`'s own address on Arbitrum.
pub fn gmx_withdrawal(id: ClientId, owner: Address, token: Address, amount: Amount) -> Self {
Self {
move_id: None,
client_operation_id: id,
venue: VenueId::Gmx,
kind: DirectKind::Withdrawal,
owner,
recipient: owner,
token,
amount,
min_out: amount,
}
}
/// Attaches the withdrawal to move `id`, whose bridge it feeds.
#[must_use]
pub fn move_id(mut self, id: MoveId) -> Self {
self.move_id = Some(id);
self
}
}
#[derive(Serialize)]
struct Report {
#[serde(skip_serializing_if = "Option::is_none")]
tx_hash: Option<B256>,
}
impl Directs<'_> {
/// Records the operation (idempotent by `client_operation_id` and terms), unchecked: check it
/// before sending anything.
pub async fn create(&self, request: &DirectRequest) -> Result<DirectOperation> {
self.inner.send(Call::new(&ops::CREATE_DIRECT).json(request)?, self.auth).await
}
/// An operation.
pub async fn get(&self, id: Uuid) -> Result<DirectOperation> {
self.inner.send(Call::new(&ops::GET_DIRECT).segment(id), self.auth).await
}
/// The owner's funds and allowance toward the operation's escrow, read fresh on its chain.
pub async fn funding(&self, id: Uuid) -> Result<BridgeFunding> {
self.inner.send(Call::new(&ops::GET_DIRECT_FUNDING).segment(id), self.auth).await
}
/// Fences the operation before its owner is asked to send it; sent once, never retried. After
/// a lost answer, never ask again.
pub async fn fence(&self, id: Uuid) -> Result<DirectOperation> {
let call = Call::new(&ops::FENCE_DIRECT).segment(id).json(&Report { tx_hash: None })?;
self.inner.send(call, self.auth).await
}
/// Reports a broadcast transaction of the operation.
pub async fn report_transaction(&self, id: Uuid, tx_hash: B256) -> Result<DirectOperation> {
let body = Report { tx_hash: Some(tx_hash) };
let call = Call::new(&ops::REPORT_DIRECT).segment(id).json(&body)?;
self.inner.send(call, self.auth).await
}
}