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
//! Market data models: venues, books, markets, volume and trade proofs.
use alloy_primitives::{Address, B256, Bytes};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use super::{Destination, Funding, Transaction, WalletFundingCapability, WalletReceipt};
use crate::types::{Amount, Bps, FillId, OrderId, Price, VenueId, open_enum};
/// The executable quotes for paying on `from` and receiving on `to` (`GET /v1/books`).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Book {
/// The venue the taker pays on.
pub from: VenueId,
/// The venue the taker receives on.
pub to: VenueId,
/// The most one fill takes from one level, in `to` base units.
pub max_fill: Amount,
/// The pair's taker fee now; levels show the makers' prices without it.
pub taker_fee_bps: Bps,
/// The quotes, best price first.
pub levels: Vec<Level>,
}
/// One quote of an order: the taker pays `receive` on `from` per `supply` on `to`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Level {
/// The order.
pub order_id: OrderId,
/// The quote's position in the order's signed set.
pub quote_index: u32,
/// Price denominator and this quote's budget in the sequence, in `to` base units.
pub supply: Amount,
/// Price numerator, in `from` base units.
pub receive: Amount,
/// What the level can fill now, in `to` base units.
pub available: Amount,
/// Executable minimum, in `to` base units.
pub min_fill: Amount,
/// Signed on-chain settlement deadline, after `valid_until`.
pub expires_at: DateTime<Utc>,
/// When the quote was signed.
pub quoted_at: DateTime<Utc>,
/// The quote set's sequence.
pub sequence: u64,
/// Last admission time.
pub valid_until: DateTime<Utc>,
}
open_enum! {
/// How a venue settles the maker's side of a fill.
pub enum Settlement {
/// Locked in the escrow on the venue's chain.
Escrow = "escrow",
/// Delivered by the maker inside the venue (Lighter, Aster).
Venue = "venue",
}
}
open_enum! {
/// How a margin move signs its amount.
pub enum AmountFormat {
/// Integer base units.
BaseUnits = "base_units",
/// Decimal tokens, such as `"5.25"`.
Decimal = "decimal",
}
}
open_enum! {
/// Where a venue withdrawal may go.
pub enum MarginDestination {
/// The venue wallet.
Wallet = "wallet",
/// The signer.
Signer = "signer",
/// The escrow.
Escrow = "escrow",
}
}
/// A venue of this deployment (`GET /v1/venues`).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Venue {
/// The venue.
pub id: VenueId,
/// Its display name.
pub name: String,
/// Enabled and configured in this deployment.
pub enabled: bool,
/// How it settles; `None` for a disabled venue.
#[serde(deserialize_with = "super::required")]
pub settlement: Option<Settlement>,
/// Its escrow or signing chain; `None` for a disabled venue.
#[serde(deserialize_with = "super::required")]
pub chain: Option<EscrowChain>,
/// Its margin transfer format; `None` when disabled or without margin transfers.
#[serde(deserialize_with = "super::required")]
pub margin_move: Option<MarginMoveSpec>,
/// The smallest amount the venue credits, in its base units; a smaller escrow release stays
/// in escrow.
#[serde(deserialize_with = "super::required")]
pub release_minimum: Option<Amount>,
}
/// A venue's escrow chain, or for Lighter and Aster its signing terms.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct EscrowChain {
/// Whether the escrow accepts direct deposits; absent means no.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub direct_deposit: Option<bool>,
/// Whether direct withdrawals from the venue account to its owner's wallet exist; absent
/// means no.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub direct_withdrawal: Option<bool>,
/// Wallet funding in the escrow token; `None` (or absent) when unsupported.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub wallet_funding: Option<WalletFundingCapability>,
/// What a wallet delivery pays on this chain; `None` where the escrow cannot deliver to a
/// wallet.
#[serde(deserialize_with = "super::required")]
pub wallet: Option<WalletToken>,
/// The chain id (999 HyperEVM, 137 Polygon, 42161 Arbitrum; 304 Lighter, 56 Aster).
pub id: u64,
/// The escrow (Lighter: its zkLighter contract; Aster: its vault).
pub escrow: Address,
/// The venue token.
pub token: Address,
/// The token's symbol.
pub symbol: String,
/// The token's decimals.
pub decimals: u8,
/// The token's EIP-2612 domain name and version; `None` on Lighter and Aster.
#[serde(deserialize_with = "super::required")]
pub permit: Option<PermitDomain>,
}
/// The token a wallet delivery pays.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct WalletToken {
/// The token.
pub token: Address,
/// Its symbol.
pub symbol: String,
/// Its decimals.
pub decimals: u8,
/// The least each part of a wallet take delivers, in this token's base units.
pub minimum: Amount,
}
/// An EIP-2612 domain's name and version; the chain id and token complete it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct PermitDomain {
/// The domain name.
pub name: String,
/// The domain version.
pub version: String,
}
/// A venue's margin transfer format: the typed data every withdrawal from its margin signs.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct MarginMoveSpec {
/// The signed `primaryType`.
pub primary_type: String,
/// The EIP-712 types, as served.
pub types: Value,
/// The EIP-712 domain, as served.
pub domain: Value,
/// Fields every transfer signs with these values.
pub fixed: serde_json::Map<String, Value>,
/// How the amount is signed.
pub amount: AmountFormat,
/// Where a withdrawal from this venue may go.
pub destinations: Vec<MarginDestination>,
}
/// Prices, liquidity and volume per venue (`GET /v1/markets`).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Markets {
/// When the figures were computed.
pub as_of: DateTime<Utc>,
/// One entry per enabled venue.
pub venues: Vec<MarketVenue>,
}
/// One venue's market figures.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct MarketVenue {
/// The venue.
pub venue: VenueId,
/// Its token's symbol.
pub symbol: String,
/// Its token's decimals.
pub decimals: u8,
/// The best price makers pay for the venue's token.
#[serde(deserialize_with = "super::required")]
pub best_buy: Option<Price>,
/// The best price makers sell the venue's token at.
#[serde(deserialize_with = "super::required")]
pub best_sell: Option<Price>,
/// What the books show of the pools supplying the venue, in its base units.
pub liquidity: Amount,
/// Settled public trades delivered on the venue in the last 24 hours, in its base units.
pub volume_24h: Amount,
}
/// A pair's trading volume over the last 24 hours (`GET /v1/volume`).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Volume {
/// One venue of the pair.
pub a: VenueId,
/// The other venue of the pair.
pub b: VenueId,
/// The volume, in `b` base units.
pub volume_24h: Amount,
/// The number of trades.
pub trades_24h: u64,
}
/// A settled trade with everything signed for it (`GET /v1/trades/{id}/proof`).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct TradeProof {
/// Where the payment came from; absent on older trades.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub funding: Option<Funding>,
/// Where the trade delivered.
pub destination: Destination,
/// A wallet delivery's receipt; `None` for a venue delivery.
#[serde(deserialize_with = "super::required")]
pub received: Option<WalletReceipt>,
/// The proof format: 2 for a trade with a taker fee.
pub version: u8,
/// The trade (its fill).
pub id: FillId,
/// The paying venue.
pub from: VenueId,
/// The receiving venue.
pub to: VenueId,
/// The payment, in `from` base units.
pub amount_in: Amount,
/// The delivery, in `to` base units.
pub amount_out: Amount,
/// Version 2: the fee the `swap_id` carries, in `taker_fee_venue` base units.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub taker_fee: Option<Amount>,
/// Version 2: the venue whose token `taker_fee` is in.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub taker_fee_venue: Option<VenueId>,
/// The escrow swap id.
pub swap_id: B256,
/// The maker's order.
pub order_id: OrderId,
/// The priced quote set's sequence.
pub sequence: u64,
/// The priced quote's position in the maker's signed set.
pub quote_index: u32,
/// The order's owner.
pub maker_owner: Address,
/// Who paid.
pub payer: Address,
/// Where the payment went.
pub maker_recipient: Address,
/// Where the delivery went.
pub taker_recipient: Address,
/// When the trade settled.
pub settled_at: DateTime<Utc>,
/// The maker's signed quote set.
pub maker: SignedAgreement,
/// The owner's `FundOffer`; `None` for in-venue inventory.
#[serde(deserialize_with = "super::required")]
pub maker_authorization: Option<SignedAgreement>,
/// The delivery to Lighter or Aster; `None` otherwise.
#[serde(deserialize_with = "super::required")]
pub delivery: Option<VenueDelivery>,
/// The payer's `PayTake`; `None` for a take paid from Lighter or Aster.
#[serde(deserialize_with = "super::required")]
pub taker: Option<SignedAgreement>,
/// The taker's executed transfer, for a take paid from Lighter or Aster; `None` otherwise.
#[serde(deserialize_with = "super::required")]
pub payment: Option<VenuePayment>,
/// The escrow transactions of the trade.
pub transactions: Vec<Transaction>,
}
/// EIP-712 typed data with its signature.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct SignedAgreement {
/// Who signed.
pub signer: Address,
/// What was signed, as served.
pub typed_data: Value,
/// The signature (an EOA signature, or on older proofs an ERC-1271 one).
pub signature: Bytes,
/// The EIP-712 digest of `typed_data`.
pub digest: B256,
}
/// A fill's delivery to Lighter or Aster.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct VenueDelivery {
/// The receiving venue.
pub venue: VenueId,
/// The Lighter transaction hash, or the Aster `transferId`.
pub tx_hash: String,
/// The taker's account on the venue: a Lighter index in decimal, an Aster address.
#[serde(deserialize_with = "super::required")]
pub account: Option<String>,
/// When it executed.
#[serde(deserialize_with = "super::required")]
pub executed_at: Option<DateTime<Utc>>,
}
/// A take's payment from Lighter or Aster.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct VenuePayment {
/// The paying venue.
pub venue: VenueId,
/// Lighter's transaction hash (without `0x`), or Aster's `transferId`.
pub tx_hash: String,
/// When it executed.
#[serde(deserialize_with = "super::required")]
pub executed_at: Option<DateTime<Utc>>,
/// The transfer as sent, as served.
pub tx_info: serde_json::Map<String, Value>,
}