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
//! Transfer models: intents, routes and funding plans.
use alloy_primitives::Address;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use uuid::Uuid;
use super::{Destination, Dex, Funding, TokenAmount, Withdrawal};
use crate::{
error::{ErrorCode, Reason},
types::{
Amount, Bps, ClientId, MaxParts, MoveId, SetupStepKind, TakeId, TransferId, VenueId,
open_enum,
},
};
/// The takes that would pay now: one, or two through another venue.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Route {
/// Where the payment comes from.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub funding: Option<Funding>,
/// Where the route delivers.
pub destination: Destination,
/// For a wallet destination, the least its recipient receives of the wallet token.
#[serde(deserialize_with = "super::required")]
pub minimum_received: Option<TokenAmount>,
/// The paying venue.
pub from: VenueId,
/// The receiving venue.
pub to: VenueId,
/// What the first take pays, in `from` base units.
pub amount_in: Amount,
/// Whether the route spends less than asked beyond rounding.
pub capped: bool,
/// What the last take delivers, in `to` base units.
pub amount_out: Amount,
/// The takes in order.
pub legs: Vec<RouteLeg>,
}
/// One take of a [`Route`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct RouteLeg {
/// The paying venue.
pub from: VenueId,
/// The receiving venue.
pub to: VenueId,
/// What it pays, in `from` base units.
pub amount_in: Amount,
/// What it delivers, in `to` base units.
pub amount_out: Amount,
/// The price levels it goes through.
pub parts: u8,
/// Its fee, included in its amounts: in `from` base units, or in `to` base units when
/// paying from Lighter or Aster.
pub taker_fee: Amount,
/// The pair's fee now.
pub taker_fee_bps: Bps,
}
open_enum! {
/// Where a funding plan's withdrawal goes.
pub enum WithdrawalDestination {
/// To the signer.
Signer = "signer",
/// To the payer's deposit address, swept into the escrow's available balance.
Escrow = "escrow",
}
}
open_enum! {
/// Whether the escrow may pull the payer's wallet token.
pub enum PaymentSetup {
/// Ready.
Done = "done",
/// A permit is submitted, not yet effective.
Pending = "pending",
/// A permit is needed first (the plan lists it).
Required = "required",
}
}
open_enum! {
/// A wallet request a funding plan expects.
pub enum FundingRequestKind {
/// A token permit for the escrow.
Permit = "permit",
/// A withdrawal out of venue margin.
MarginMove = "margin_move",
/// GMX only: a transaction the signer sends itself.
TransferOut = "transfer_out",
/// The take's payment.
PayTake = "pay_take",
}
}
open_enum! {
/// A grouped route's lifecycle.
pub enum GroupedRouteState {
/// Its takes hold no maker inventory; commit before `commit_by`.
Prepared = "prepared",
/// Committed; the operator runs the route.
Committed = "committed",
/// Never committed; its second take released.
Aborted = "aborted",
/// Committed, and nothing of it can move any more.
Closed = "closed",
}
}
open_enum! {
/// A Polygon wallet conversion's progress.
pub enum ConversionState {
/// Prepared, not sent.
Prepared = "prepared",
/// Sent, not confirmed.
Pending = "pending",
/// Converted.
Ready = "ready",
/// Past its deadline.
Expired = "expired",
}
}
/// A transfer: one take from `from` to `to`, with its funding.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct TransferIntent {
/// The move it belongs to.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub move_id: Option<MoveId>,
/// The Polygon wallet conversion funding it, if any.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub conversion: Option<WalletConversion>,
/// Where the payment comes from.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub funding: Option<Funding>,
/// Where it delivers.
pub destination: Destination,
/// The transfer.
pub id: TransferId,
/// The caller's idempotency key.
pub client_transfer_id: ClientId,
/// The key its take uses: `transfer:<id>`.
pub client_take_id: ClientId,
/// The paying venue.
pub from: VenueId,
/// The receiving venue.
pub to: VenueId,
/// The most paid, in `from` base units.
pub amount_in: Amount,
/// The least received, in `to` base units.
pub min_out: Amount,
/// The most maker fills.
pub max_parts: MaxParts,
/// The highest taker fee accepted; `None` when it consented to no fee.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_fee_bps: Option<Bps>,
/// The Hyperliquid balance its withdrawal leaves, when not the perps.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub from_dex: Option<Dex>,
/// The linked account on `from`, or with wallet funding the paying wallet.
pub payer: Address,
/// The receiving account, or with a wallet destination any address.
pub recipient: Address,
/// `from`'s chain.
pub from_chain_id: u64,
/// `to`'s chain.
pub to_chain_id: u64,
/// The prepared, unsigned withdrawal.
#[serde(deserialize_with = "super::required")]
pub withdrawal_preparation: Option<PreparedWithdrawal>,
/// The submitted withdrawal.
#[serde(deserialize_with = "super::required")]
pub withdrawal: Option<Withdrawal>,
/// The expected wallet requests, when computed.
#[serde(deserialize_with = "super::required")]
pub funding_plan: Option<FundingPlan>,
/// Why `funding_plan` could not be computed when it was asked for.
#[serde(deserialize_with = "super::required")]
pub funding_plan_error: Option<FundingPlanError>,
/// Its take, once made.
#[serde(deserialize_with = "super::required")]
pub take_id: Option<TakeId>,
/// Whether every fill of its take is final; `None` without a take.
#[serde(deserialize_with = "super::required")]
pub take_final: Option<bool>,
/// A route through `to`: where it ends.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub then: Option<TransferThen>,
/// The transfer this one continues.
#[serde(deserialize_with = "super::required")]
pub previous_transfer_id: Option<TransferId>,
/// With `then`, what a continuation may still pay, in `to` base units.
#[serde(deserialize_with = "super::required")]
pub continuable: Option<Amount>,
/// When it was dismissed.
#[serde(deserialize_with = "super::required")]
pub cancelled_at: Option<DateTime<Utc>>,
/// When it was created.
pub created_at: DateTime<Utc>,
/// The grouped route it belongs to.
#[serde(deserialize_with = "super::required")]
pub grouped: Option<GroupedRoute>,
}
/// A transfer's prepared withdrawal, not yet signed.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct PreparedWithdrawal {
/// In `from` base units.
pub amount: Amount,
/// The typed data to sign, as the server prepared it; the SDK checks it before signing.
pub typed_data: Value,
/// When it was prepared.
pub created_at: DateTime<Utc>,
}
/// The wallet requests a transfer expects from current balances and allowances; recheck each
/// preparation before signing.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct FundingPlan {
/// When the balances were read.
pub observed_at: DateTime<Utc>,
/// The paying venue.
pub venue: VenueId,
/// The paying address.
pub owner: Address,
/// The payment, in `venue` base units.
pub amount: Amount,
/// What the escrow pulls from the wallet.
pub deposit: Amount,
/// What leaves venue margin first.
pub withdrawal_amount: Amount,
/// Where that withdrawal goes, if one is needed.
#[serde(deserialize_with = "super::required")]
pub withdrawal_to: Option<WithdrawalDestination>,
/// Whether the escrow may pull the wallet token.
pub payment_setup: PaymentSetup,
/// Whether the take is signed at once with the withdrawal, or after its funds arrive.
pub signed_at_once: bool,
/// The expected requests, in order.
pub requests: Vec<FundingRequest>,
}
/// One request of a [`FundingPlan`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct FundingRequest {
/// What it is.
pub kind: FundingRequestKind,
/// Who signs or sends it.
pub signer: Address,
/// The chain it is for.
pub chain_id: u64,
}
/// Why a funding plan could not be computed, as the refusal would carry it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct FundingPlanError {
/// The error code.
pub code: ErrorCode,
/// Human-readable; do not parse.
pub message: String,
/// The stable sub-case of `code`.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reason: Option<Reason>,
/// With `SETUP_REQUIRED`: the venue.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub venue: Option<VenueId>,
/// With `SETUP_REQUIRED`: the step.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub step: Option<SetupStepKind>,
/// The HTTP status the refusal would have.
pub status: u16,
/// Seconds after which the plan may be computed.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub retry_after: Option<u64>,
}
/// The second transfer of a route.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct TransferThen {
/// Where the route ends.
pub to: VenueId,
/// The receiving account, or with a wallet destination any address.
pub recipient: Address,
/// Where it delivers; the server's default is [`Destination::Venue`].
#[serde(default, skip_serializing_if = "Option::is_none")]
pub destination: Option<Destination>,
/// What the second transfer pays, in the intermediate venue's base units.
pub amount_in: Amount,
/// The least accepted on `to` for that `amount_in`.
pub min_out: Amount,
/// The highest taker fee the second take accepts.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_fee_bps: Option<Bps>,
/// `to`'s chain, in an intent.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub chain_id: Option<u64>,
}
/// A route signed at once.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct GroupedRoute {
/// Its lifecycle.
pub state: GroupedRouteState,
/// The route's second transfer.
pub second_transfer_id: TransferId,
/// The first transfer's take.
pub first_take_id: TakeId,
/// The second transfer's take, once made.
#[serde(deserialize_with = "super::required")]
pub second_take_id: Option<TakeId>,
/// Every signature must be committed before it.
pub commit_by: DateTime<Utc>,
}
/// A Polygon wallet conversion of native USDC into pUSD, funding a transfer.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct WalletConversion {
/// Native USDC converted, in base units.
pub amount_in: Amount,
/// The least pUSD received, in base units.
pub min_out: Amount,
/// The conversion.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub id: Option<Uuid>,
/// The chain.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub chain_id: Option<u64>,
/// The escrow funded.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub escrow: Option<Address>,
/// The paying wallet.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub payer: Option<Address>,
/// The converter contract.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub converter: Option<Address>,
/// The token converted.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub token: Option<Address>,
/// The conversion's nonce.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub nonce: Option<Amount>,
/// The conversion's deadline, Unix seconds.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub deadline: Option<Amount>,
/// The block the conversion was prepared at.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub prepared_block: Option<u64>,
/// Its progress.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub state: Option<ConversionState>,
/// Transaction hashes sent for it.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub attempts: Option<Vec<String>>,
/// The confirmed transaction.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub tx_hash: Option<String>,
/// What it received.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub received: Option<String>,
/// The confirming block.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub block_number: Option<u64>,
}