lamarge 0.9.0

Client for the LaMarge API: sign-in, takes, transfers, withdrawals and market making, each request checked before it is signed.
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
use serde_json::json;

use super::*;
use crate::{
    error::VerificationKind,
    protocol::polymarket::InternalTransfer,
    testing::{MAKER, NOW, OTHER, OWNER, at, edited, fixtures, sample},
};

fn units(amount: u128) -> Amount {
    Amount::from(amount)
}

fn withdraw(
    raw: &Value,
    venue: &VenueId,
    destination: Address,
    amount: u128,
) -> Result<Verified, VerificationError> {
    check_margin_withdrawal(raw, venue, OWNER, destination, units(amount), fixtures::now(), None)
}

/// `intents.test.ts` "withdrawal pins exact schema and prevents external destinations or fee
/// changes", for Polymarket and Hyperliquid.
#[test]
fn a_withdrawal_pins_its_destination_amount_and_fee() {
    let pm = |raw: &Value, amount: u128| withdraw(raw, &VenueId::Polymarket, OWNER, amount);
    let verified = pm(&sample("polymarket_withdraw"), 10_000_000).unwrap();
    assert_eq!(verified.not_after(), Some(NOW + 60));
    let message = |name: &str, value: Value| {
        edited(sample("polymarket_withdraw"), |raw| raw["message"][name] = value.clone())
    };
    let cases = [
        (message("to", json!(OTHER)), details::TO),
        (message("fee", json!("1")), details::FEE),
        (message("account", json!(OTHER)), details::ACCOUNT),
        (message("token", json!(OTHER)), details::TOKEN),
        (message("amount", json!("10000001")), details::AMOUNT),
        (message("ts", json!(NOW - 61)), details::TS),
    ];
    for (index, (raw, detail)) in cases.iter().enumerate() {
        assert_eq!(pm(raw, 10_000_000).unwrap_err().detail, *detail, "case {index}");
    }
    assert_eq!(pm(&sample("polymarket_withdraw"), 0).unwrap_err().detail, details::AMOUNT);
    let domain = edited(sample("polymarket_withdraw"), |raw| {
        raw["domain"]["verifyingContract"] = json!(OTHER);
    });
    assert_eq!(pm(&domain, 10_000_000).unwrap_err().detail, details::DOMAIN);
    // A venue wallet destination is the caller's, never the API's.
    assert_eq!(
        withdraw(&sample("polymarket_withdraw"), &VenueId::Polymarket, MAKER, 10_000_000)
            .unwrap_err()
            .detail,
        details::TO
    );

    let hl = |raw: &Value| withdraw(raw, &VenueId::Hyperliquid, OWNER, 12_500_000);
    assert!(hl(&sample("send_asset_withdrawal")).is_ok());
    let message = |name: &str, value: Value| {
        edited(sample("send_asset_withdrawal"), |raw| raw["message"][name] = value.clone())
    };
    let cases = [
        (message("destination", json!(format!("{OWNER:#x}"))), details::DESTINATION),
        (message("sourceDex", json!("xyz")), details::SOURCE_DEX),
        (message("destinationDex", json!("")), details::DESTINATION_DEX),
        (message("hyperliquidChain", json!("Testnet")), details::HYPERLIQUID_CHAIN),
        (message("token", json!("USDC:0x00")), details::TOKEN),
        (message("fromSubAccount", json!(format!("{OTHER:#x}"))), details::FROM_SUB_ACCOUNT),
        (message("amount", json!("12.6")), details::AMOUNT),
        (message("amount", json!("12.5000001")), details::AMOUNT),
        (message("nonce", json!((NOW - 61) * 1000)), details::NONCE),
    ];
    for (index, (raw, detail)) in cases.iter().enumerate() {
        assert_eq!(hl(raw).unwrap_err().detail, *detail, "case {index}");
    }
    // Spot pays too.
    assert!(hl(&message("sourceDex", json!("spot"))).is_ok());
    // A Hyperliquid withdrawal goes to the signer only.
    assert_eq!(
        withdraw(&sample("send_asset_withdrawal"), &VenueId::Hyperliquid, MAKER, 12_500_000)
            .unwrap_err()
            .detail,
        details::DESTINATION
    );
}

/// `withdraw.test.ts` "an Aster withdrawal is refused before any request": the flows' check
/// signs no Aster `Action`.
#[test]
fn aster_and_gmx_withdrawals_are_unsupported() {
    for venue in [VenueId::Aster, VenueId::Gmx, VenueId::Lighter] {
        let error = withdraw(&sample("send_asset_withdrawal"), &venue, OWNER, 1).unwrap_err();
        assert_eq!(error.kind, VerificationKind::Unsupported, "{venue}");
        assert_eq!(
            margin_withdrawal_cost(&sample("send_asset_withdrawal"), &venue).unwrap_err().kind,
            VerificationKind::Unsupported
        );
    }
}

fn send(
    raw: &Value,
    venue: &VenueId,
    recipient: Address,
    amount: u128,
) -> Result<Verified, VerificationError> {
    check_send(raw, venue, OWNER, recipient, units(amount), fixtures::now(), None)
}

/// The vector's internal transfer with its `op` arguments replaced and its hash rebuilt.
fn operation(account: &str, amount: &str, to: &str) -> Value {
    edited(sample("polymarket_internal_transfer"), |raw| {
        raw["op"]["args"]["account"] = json!(account);
        raw["op"]["args"]["amount"] = json!(amount);
        raw["op"]["args"]["to"] = json!(to);
        let transfer = InternalTransfer::read(&raw["op"]).unwrap();
        raw["message"]["data"] = json!(transfer.hash().unwrap());
    })
}

/// `send.test.ts` "a Polymarket send is checked by rebuilding the hash its wallet is shown".
#[test]
fn a_polymarket_send_is_checked_by_rebuilding_its_hash() {
    let pm = |raw: &Value, amount: u128| send(raw, &VenueId::Polymarket, MAKER, amount);
    let verified = pm(&sample("polymarket_internal_transfer"), 250_500_000).unwrap();
    let raw = sample("polymarket_internal_transfer");
    let served = eip712::parse_operation(&raw, &polymarket::operation_domain()).unwrap().0;
    assert_eq!(verified.digest(), served.digest);
    let owner = "0x84026D3BefF123AAb7DFF957574D1362d6F880A5";
    let maker = "0x669ED91296e0098eb827D85eAE6F2A60c5f14f8d";
    let other = "0x3333333333333333333333333333333333333333";
    assert!(pm(&operation(&owner.to_lowercase(), "250.5", maker), 250_500_000).is_ok());
    let cases = [
        (operation(owner, "250.5", other), 250_500_000, details::TO),
        (operation(owner, "250.5", maker), 250_000_000, details::AMOUNT),
        (operation(maker, "250.5", maker), 250_500_000, details::ACCOUNT),
        (operation(owner, "250.5000001", maker), 250_500_000, details::AMOUNT),
    ];
    for (index, (raw, amount, detail)) in cases.iter().enumerate() {
        assert_eq!(pm(raw, *amount).unwrap_err().detail, *detail, "case {index}");
    }
    // The arguments shown differ from the ones the signed hash covers.
    let swapped = edited(sample("polymarket_internal_transfer"), |raw| {
        raw["op"]["args"]["to"] = json!(other);
    });
    assert_eq!(
        send(&swapped, &VenueId::Polymarket, other.parse().unwrap(), 250_500_000)
            .unwrap_err()
            .detail,
        details::DATA
    );
    let forged = edited(sample("polymarket_internal_transfer"), |raw| {
        raw["message"]["data"] = operation(owner, "250.5", other)["message"]["data"].clone();
    });
    assert_eq!(pm(&forged, 250_500_000).unwrap_err().detail, details::DATA);
    let withdraw_op = edited(sample("polymarket_internal_transfer"), |raw| {
        raw["op"]["type"] = json!("withdraw");
    });
    assert_eq!(pm(&withdraw_op, 250_500_000).unwrap_err().detail, details::OP);
    let token = edited(sample("polymarket_internal_transfer"), |raw| {
        raw["op"]["args"]["token"] = json!(other);
        raw["message"]["data"] = json!(InternalTransfer::read(&raw["op"]).unwrap().hash().unwrap());
    });
    assert_eq!(pm(&token, 250_500_000).unwrap_err().detail, details::TOKEN);
    let contract = edited(sample("polymarket_internal_transfer"), |raw| {
        raw["domain"]["verifyingContract"] = json!("0xdca4af75705dbb50f62437045aff9921947917d2");
    });
    assert_eq!(pm(&contract, 250_500_000).unwrap_err().detail, details::DOMAIN);
    let stale = edited(sample("polymarket_internal_transfer"), |raw| {
        raw["message"]["ts"] = json!((NOW - 120) * 1000);
    });
    assert_eq!(pm(&stale, 250_500_000).unwrap_err().detail, details::TS);
    assert_eq!(
        send(&sample("polymarket_internal_transfer"), &VenueId::Polymarket, OWNER, 250_500_000)
            .unwrap_err()
            .detail,
        details::RECIPIENT
    );
}

/// `send.test.ts` "a Hyperliquid send names its recipient, mainnet and the two dexes" and
/// "trade.xyz pays a Hyperliquid send only when asked" (see also
/// `trade_xyz_pays_only_when_asked`).
#[test]
fn a_hyperliquid_send_names_its_recipient_mainnet_and_the_two_dexes() {
    let hl =
        |raw: &Value, recipient: Address| send(raw, &VenueId::Hyperliquid, recipient, 5_250_000);
    assert!(hl(&sample("send_asset_send"), MAKER).is_ok());
    let message = |name: &str, value: Value| {
        edited(sample("send_asset_send"), |raw| raw["message"][name] = value.clone())
    };
    let cases = [
        (message("destination", json!(format!("0x{}", "33".repeat(20)))), details::DESTINATION),
        (
            message("destination", json!("0x2000000000000000000000000000000000000000")),
            details::DESTINATION,
        ),
        // The recipient exactly as written: lowercase.
        (
            message("destination", json!("0x669ED91296e0098eb827D85eAE6F2A60c5f14f8d")),
            details::DESTINATION,
        ),
        (message("hyperliquidChain", json!("Testnet")), details::HYPERLIQUID_CHAIN),
        (message("destinationDex", json!("xyz")), details::DESTINATION_DEX),
        (message("sourceDex", json!("xyz")), details::SOURCE_DEX),
        (message("amount", json!("5.3")), details::AMOUNT),
        (message("nonce", json!((NOW - 120) * 1000)), details::NONCE),
    ];
    for (index, (raw, detail)) in cases.iter().enumerate() {
        assert_eq!(hl(raw, MAKER).unwrap_err().detail, *detail, "case {index}");
    }
    assert!(hl(&message("destinationDex", json!("spot")), MAKER).is_ok());
    // A system address is never a recipient, even when asked.
    let index = "0x2000000000000000000000000000000000010000";
    assert_eq!(
        hl(&message("destination", json!(index)), index.parse().unwrap()).unwrap_err().detail,
        details::DESTINATION
    );
    let own = message("destination", json!(format!("{OWNER:#x}")));
    assert_eq!(hl(&own, OWNER).unwrap_err().detail, details::RECIPIENT);
}

fn aster_send(id: &str, to: Address, amount: &str) -> Value {
    let fields = [
        ("asset", "USDT".to_owned()),
        ("amount", amount.to_owned()),
        ("toAddress", format!("{to:#x}")),
        ("clientTranId", id.to_owned()),
        ("nonce", (NOW * 1_000_000).to_string()),
        ("user", format!("{OWNER:#x}")),
    ];
    fixtures::aster_request(&fields, 1666)
}

/// `send.test.ts` "an Aster send carries a send's own id, never a fill's or a margin move's".
#[test]
fn an_aster_send_carries_a_sends_own_id() {
    let id = format!("s{}", "ab".repeat(16));
    let five = 5_000_000_000_000_000_000;
    let aster = |raw: &Value, amount: u128| send(raw, &VenueId::Aster, MAKER, amount);
    assert!(aster(&aster_send(&id, MAKER, "5"), five).is_ok());
    let cases = [
        (aster_send(&"ab".repeat(16), MAKER, "5"), details::CLIENT_TRAN_ID),
        (aster_send(&format!("m{}", "ab".repeat(16)), MAKER, "5"), details::CLIENT_TRAN_ID),
        (aster_send(&format!("s{}", "AB".repeat(16)), MAKER, "5"), details::CLIENT_TRAN_ID),
        (aster_send(&id, OTHER, "5"), details::TO_ADDRESS),
        (aster_send(&id, MAKER, "6"), details::AMOUNT),
    ];
    for (index, (raw, detail)) in cases.iter().enumerate() {
        assert_eq!(aster(raw, five).unwrap_err().detail, *detail, "case {index}");
    }
    // Rounded up to whole millionths, as Aster moves it.
    assert!(aster(&aster_send(&id, MAKER, "5.000001"), five + 1).is_ok());
    let user = edited(sample("aster_transfer"), |raw| {
        let msg = aster_send(&id, MAKER, "5")["message"]["msg"]
            .as_str()
            .unwrap()
            .replace(&format!("{OWNER:#x}"), &format!("{OTHER:#x}"));
        raw["message"]["msg"] = json!(msg);
    });
    assert_eq!(aster(&user, five).unwrap_err().detail, details::USER);
    let stale = edited(sample("aster_transfer"), |raw| {
        let msg = aster_send(&id, MAKER, "5")["message"]["msg"]
            .as_str()
            .unwrap()
            .replace(&(NOW * 1_000_000).to_string(), &((NOW - 61) * 1_000_000).to_string());
        raw["message"]["msg"] = json!(msg);
    });
    assert_eq!(aster(&stale, five).unwrap_err().detail, details::NONCE);
    let garbled =
        edited(sample("aster_transfer"), |raw| raw["message"]["msg"] = json!("asset=USDT"));
    assert_eq!(aster(&garbled, five).unwrap_err().detail, details::ASTER_TRANSFER);
    let approval = aster_send(&id, MAKER, "5");
    let mut bsc = approval.clone();
    bsc["domain"]["chainId"] = json!(56);
    assert_eq!(aster(&bsc, five).unwrap_err().detail, details::DOMAIN);
}

/// `send.test.ts` "signing times read each venue's own unit".
#[test]
fn signing_times_read_each_venues_own_unit() {
    let aster = aster_send(&format!("s{}", "ab".repeat(16)), MAKER, "5");
    for raw in [
        sample("polymarket_internal_transfer"),
        sample("send_asset_send"),
        sample("polymarket_withdraw"),
        aster,
    ] {
        assert_eq!(signed_at(&raw).unwrap(), at(0), "{}", raw["primaryType"]);
    }
    assert_eq!(signed_at(&sample("fund_offer")).unwrap_err().detail, details::TYPED_DATA);
    // A stored signature is checked again at its own signing time.
    let stored = sample("send_asset_withdrawal");
    let later = check_margin_withdrawal(
        &stored,
        &VenueId::Hyperliquid,
        OWNER,
        OWNER,
        units(12_500_000),
        at(3600),
        None,
    );
    assert_eq!(later.unwrap_err().kind, VerificationKind::Stale);
    assert!(
        check_margin_withdrawal(
            &stored,
            &VenueId::Hyperliquid,
            OWNER,
            OWNER,
            units(12_500_000),
            signed_at(&stored).unwrap(),
            None,
        )
        .is_ok()
    );
}

#[test]
fn a_withdrawals_cost_is_its_signed_amount() {
    assert_eq!(
        margin_withdrawal_cost(&sample("send_asset_withdrawal"), &VenueId::Hyperliquid).unwrap(),
        units(12_500_000)
    );
    assert_eq!(
        margin_withdrawal_cost(&sample("polymarket_withdraw"), &VenueId::Polymarket).unwrap(),
        units(10_000_000)
    );
    let garbled = edited(sample("send_asset_withdrawal"), |raw| {
        raw["message"]["amount"] = json!("12.5000001");
    });
    assert_eq!(
        margin_withdrawal_cost(&garbled, &VenueId::Hyperliquid).unwrap_err().detail,
        details::AMOUNT
    );
    assert_eq!(
        margin_withdrawal_cost(&sample("polymarket_withdraw"), &VenueId::Hyperliquid)
            .unwrap_err()
            .detail,
        details::PRIMARY_TYPE
    );
}

const USDT: u128 = 1_000_000_000_000_000_000;

/// `aster.test.ts`'s Aster withdrawal of 10 USDT after a 0.11 USDT fee, by [`OWNER`] at [`NOW`],
/// its message's `patch` fields replaced.
fn action(patch: &Value) -> Value {
    let mut message = json!({
        "type": "Withdraw", "destination": OWNER, "destination Chain": "BSC", "token": "USDT",
        "amount": "10.11", "fee": "0.11", "nonce": (i128::from(NOW) * 1_000_000).to_string(),
        "aster chain": "Mainnet",
    });
    for (name, value) in patch.as_object().unwrap() {
        message[name] = value.clone();
    }
    json!({
        "types": {
            "EIP712Domain": [
                {"name": "name", "type": "string"}, {"name": "version", "type": "string"},
                {"name": "chainId", "type": "uint256"},
                {"name": "verifyingContract", "type": "address"},
            ],
            "Action": [
                {"name": "type", "type": "string"}, {"name": "destination", "type": "address"},
                {"name": "destination Chain", "type": "string"}, {"name": "token", "type": "string"},
                {"name": "amount", "type": "string"}, {"name": "fee", "type": "string"},
                {"name": "nonce", "type": "uint256"}, {"name": "aster chain", "type": "string"},
            ],
        },
        "primaryType": "Action",
        "domain": {
            "name": "Aster", "version": "1", "chainId": 56,
            "verifyingContract": "0x0000000000000000000000000000000000000000",
        },
        "message": message,
    })
}

fn aster_withdraw(
    raw: &Value,
    destination: Address,
    amount: u128,
) -> Result<Verified, VerificationError> {
    check_withdrawal(raw, &VenueId::Aster, OWNER, destination, units(amount), fixtures::now(), None)
}

/// `aster.test.ts` "an Aster withdrawal is pinned to the account, BSC, USDT, a bounded fee and a
/// fresh nonce": the public check accepts it as the TypeScript SDK does.
#[test]
fn an_aster_withdrawal_is_pinned_to_the_account_bsc_usdt_a_bounded_fee_and_a_fresh_nonce() {
    let ten = 10 * USDT;
    let verified = aster_withdraw(&action(&json!({})), OWNER, ten).unwrap();
    assert_eq!(verified.not_after(), Some(NOW + 60));
    assert_eq!(withdrawal_cost(&action(&json!({})), &VenueId::Aster).unwrap(), units(ten));
    assert_eq!(signed_at(&action(&json!({}))).unwrap(), at(0));
    let stale_nonce = ((i128::from(NOW) - 61) * 1_000_000).to_string();
    let refused = [
        (json!({"amount": "10.1"}), ten, details::AMOUNT),
        (json!({"fee": "2.000001", "amount": "12.000001"}), ten, details::FEE),
        (json!({"destination": OTHER}), ten, details::DESTINATION),
        (json!({"destination Chain": "Arbitrum"}), ten, details::DESTINATION_CHAIN),
        (json!({"token": "USDC"}), ten, details::TOKEN),
        (json!({"type": "Transfer"}), ten, details::TYPE),
        (json!({"aster chain": "Testnet"}), ten, details::ASTER_CHAIN),
        (json!({"nonce": stale_nonce}), ten, details::NONCE),
        (json!({}), 10_110_000_000_000_000_000, details::AMOUNT),
        (json!({}), 10_000_000, details::AMOUNT),
    ];
    for (index, (patch, amount, detail)) in refused.iter().enumerate() {
        let error = aster_withdraw(&action(patch), OWNER, *amount).unwrap_err();
        assert_eq!(error.detail, *detail, "case {index}");
    }
    let elsewhere = edited(action(&json!({})), |raw| raw["domain"]["chainId"] = json!(42161));
    assert!(aster_withdraw(&elsewhere, OWNER, ten).is_err());
    assert_eq!(
        aster_withdraw(&action(&json!({})), OTHER, ten).unwrap_err().detail,
        details::DESTINATION
    );
    // The flows' own check still signs no Aster withdrawal.
    let error = withdraw(&action(&json!({})), &VenueId::Aster, OWNER, ten).unwrap_err();
    assert_eq!(error.kind, VerificationKind::Unsupported);
}

/// The nonce window is exact to the microsecond, on both sides, at a fractional clock; the fee is
/// 2 USDT at most.
#[test]
fn an_aster_withdrawal_nonce_is_within_sixty_seconds_exactly_and_its_fee_two_usdt() {
    let ten = 10 * USDT;
    let half = chrono::DateTime::from_timestamp(NOW, 500_000_000).unwrap();
    let now = i128::from(NOW) * 1_000_000;
    for (clock, center) in [(fixtures::now(), now), (half, now + 500_000)] {
        for (offset, accepted) in
            [(60_000_000, true), (60_000_001, false), (-60_000_000, true), (-60_000_001, false)]
        {
            let nonce = (center + offset).to_string();
            let raw = action(&json!({"nonce": nonce}));
            let checked =
                check_withdrawal(&raw, &VenueId::Aster, OWNER, OWNER, units(ten), clock, None);
            if accepted {
                let limit = i64::try_from((center + offset) / 1_000_000).unwrap() + 60;
                assert_eq!(checked.unwrap().not_after(), Some(limit), "{clock} {offset}");
            } else {
                assert_eq!(checked.unwrap_err().detail, details::NONCE, "{clock} {offset}");
            }
        }
    }
    let overflow = action(&json!({"nonce": U256::MAX.to_string()}));
    assert_eq!(aster_withdraw(&overflow, OWNER, ten).unwrap_err().detail, details::NONCE);
    let most = action(&json!({"fee": "2", "amount": "12"}));
    assert!(aster_withdraw(&most, OWNER, ten).is_ok());
    let more = action(&json!({"fee": "2.000001", "amount": "12.000001"}));
    assert_eq!(aster_withdraw(&more, OWNER, ten).unwrap_err().detail, details::FEE);
}

/// `send.test.ts` "trade.xyz pays a Hyperliquid send only when asked" and `withdraw.test.ts` "a
/// standard Hyperliquid account withdraws from trade.xyz as asked".
#[test]
fn trade_xyz_pays_only_when_asked() {
    let xyz = Some(&Dex::Xyz);
    let at = fixtures::now();
    let edit = |name: &str, field: &str, value: Value| {
        edited(sample(name), |raw| raw["message"][field] = value.clone())
    };
    let from_xyz = edit("send_asset_send", "sourceDex", json!("xyz"));
    let hl = VenueId::Hyperliquid;
    let amount = units(5_250_000);
    assert!(check_send(&from_xyz, &hl, OWNER, MAKER, amount, at, xyz).is_ok());
    // The perps answered for trade.xyz, or trade.xyz unasked, sign nothing.
    let perps = sample("send_asset_send");
    let refused = check_send(&perps, &hl, OWNER, MAKER, amount, at, xyz).unwrap_err();
    assert_eq!(refused.detail, details::SOURCE_DEX);
    let unasked = check_send(&from_xyz, &hl, OWNER, MAKER, amount, at, None).unwrap_err();
    assert_eq!(unasked.detail, details::SOURCE_DEX);
    let out = units(12_500_000);
    let withdrawal = edit("send_asset_withdrawal", "sourceDex", json!("xyz"));
    assert!(check_withdrawal(&withdrawal, &hl, OWNER, OWNER, out, at, xyz).is_ok());
    let perps = sample("send_asset_withdrawal");
    let refused = check_withdrawal(&perps, &hl, OWNER, OWNER, out, at, xyz).unwrap_err();
    assert_eq!(refused.detail, details::SOURCE_DEX);
    // Only Hyperliquid has a trade.xyz balance.
    let pm = check_withdrawal(&perps, &VenueId::Polymarket, OWNER, OWNER, out, at, xyz);
    assert_eq!(pm.unwrap_err(), differs(details::VENUE));
    let aster = check_withdrawal(&perps, &VenueId::Aster, OWNER, OWNER, out, at, xyz);
    assert_eq!(aster.unwrap_err(), differs(details::VENUE));
}