fynd-client 0.50.0

Rust client for the Fynd DEX router
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
use std::{future::Future, pin::Pin};

use alloy::{
    consensus::{TxEip1559, TypedTransaction},
    dyn_abi::TypedData,
    primitives::{Address, Signature, B256},
};
use num_bigint::BigUint;

use crate::{error::FyndError, Quote};

// ============================================================================
// PAYLOADS
// ============================================================================

/// A ready-to-sign EIP-1559 transaction produced by the Fynd execution path.
///
/// Obtain one via [`FyndClient::swap_payload`](crate::FyndClient::swap_payload) when
/// the quote's backend is [`BackendKind::Fynd`](crate::BackendKind::Fynd).
#[derive(Debug)]
pub struct FyndPayload {
    quote: Quote,
    tx: TypedTransaction,
}

impl FyndPayload {
    pub(crate) fn new(quote: Quote, tx: TypedTransaction) -> Self {
        Self { quote, tx }
    }

    /// The order quote this payload was built from.
    pub fn quote(&self) -> &Quote {
        &self.quote
    }

    /// The unsigned EIP-1559 transaction. Sign its
    /// [`signature_hash()`](alloy::consensus::SignableTransaction::signature_hash) and pass the
    /// result to [`SignedSwap::assemble`].
    pub fn tx(&self) -> &TypedTransaction {
        &self.tx
    }

    /// Consume the payload and return the inner parts for use in `execute()`.
    pub(crate) fn into_parts(self) -> (Quote, TypedTransaction) {
        (self.quote, self.tx)
    }
}

/// Turbine payload stub. Fields are `()` placeholders until the Turbine signing story lands.
#[derive(Debug)]
pub struct TurbinePayload {
    #[allow(dead_code)]
    // Placeholder: will be populated in the Turbine signing story
    _order_quote: (),
}

/// A payload that needs to be signed before a swap can be executed.
///
/// Use [`signing_hash`](Self::signing_hash) to obtain the bytes to sign, then pass the resulting
/// [`alloy::primitives::Signature`] to [`SignedSwap::assemble`].
///
/// Only the [`Fynd`](Self::Fynd) variant is currently executable; calling methods on the
/// [`Turbine`](Self::Turbine) variant will panic with `unimplemented!`.
#[derive(Debug)]
pub enum SwapPayload {
    /// Fynd execution path — an EIP-1559 transaction targeting the RouterV3 contract.
    Fynd(Box<FyndPayload>),
    /// Turbine execution path — not yet implemented.
    Turbine(TurbinePayload),
}

impl SwapPayload {
    /// Returns the 32-byte hash that must be signed.
    ///
    /// For the Fynd path this is the EIP-1559 transaction's `signature_hash()`.
    ///
    /// # Panics
    ///
    /// Panics if called on the `Turbine` variant.
    pub fn signing_hash(&self) -> B256 {
        match self {
            Self::Fynd(p) => {
                use alloy::consensus::SignableTransaction;
                p.tx.signature_hash()
            }
            Self::Turbine(_) => unimplemented!("Turbine signing not yet implemented"),
        }
    }

    /// Returns EIP-712 typed data for wallets that support `eth_signTypedData_v4`.
    ///
    /// Always returns `None` for EIP-1559 transactions (Fynd path); those use
    /// [`signing_hash`](Self::signing_hash) instead.
    pub fn typed_data(&self) -> Option<&TypedData> {
        // EIP-1559 transactions use a signing hash, not EIP-712 typed data.
        match self {
            Self::Fynd(_) | Self::Turbine(_) => None,
        }
    }

    /// The order quote embedded in this payload.
    ///
    /// # Panics
    ///
    /// Panics if called on the `Turbine` variant.
    pub fn quote(&self) -> &Quote {
        match self {
            Self::Fynd(p) => &p.quote,
            Self::Turbine(_) => unimplemented!("Turbine signing not yet implemented"),
        }
    }

    /// Consume the payload and return its inner parts for use in `execute_swap()`.
    pub(crate) fn into_fynd_parts(
        self,
    ) -> Result<(Quote, TypedTransaction), crate::error::FyndError> {
        match self {
            Self::Fynd(p) => Ok(p.into_parts()),
            Self::Turbine(_) => Err(crate::error::FyndError::Protocol(
                "Turbine execution not yet implemented".into(),
            )),
        }
    }
}

// ============================================================================
// SIGNED ORDER
// ============================================================================

/// A [`SwapPayload`] paired with its cryptographic signature.
///
/// Construct via [`SignedSwap::assemble`] after signing the
/// [`signing_hash`](SwapPayload::signing_hash). Pass to
/// [`FyndClient::execute_swap`](crate::FyndClient::execute_swap) to broadcast and settle.
pub struct SignedSwap {
    payload: SwapPayload,
    signature: Signature,
}

impl SignedSwap {
    /// Pair a payload with the signature produced by signing its
    /// [`signing_hash`](SwapPayload::signing_hash).
    pub fn assemble(payload: SwapPayload, signature: Signature) -> Self {
        Self { payload, signature }
    }

    /// The underlying swap payload.
    pub fn payload(&self) -> &SwapPayload {
        &self.payload
    }

    /// The signature over the payload's signing hash.
    pub fn signature(&self) -> &Signature {
        &self.signature
    }

    pub(crate) fn into_parts(self) -> (SwapPayload, Signature) {
        (self.payload, self.signature)
    }
}

// ============================================================================
// SETTLED ORDER
// ============================================================================

/// The result of a successfully mined or simulated swap transaction.
///
/// Returned by awaiting an [`ExecutionReceipt`]. For dry-run executions
/// ([`ExecutionOptions::dry_run`](crate::ExecutionOptions)), `tx_hash` and `tx_receipt` are `None`.
#[derive(Debug, Clone)]
pub struct SettledOrder {
    tx_hash: Option<B256>,
    settled_amount: Option<BigUint>,
    gas_cost: BigUint,
}

impl SettledOrder {
    pub(crate) fn new(
        tx_hash: Option<B256>,
        settled_amount: Option<BigUint>,
        gas_cost: BigUint,
    ) -> Self {
        Self { tx_hash, settled_amount, gas_cost }
    }

    /// The transaction hash of the mined swap. `None` for dry-run simulations.
    pub fn tx_hash(&self) -> Option<B256> {
        self.tx_hash
    }

    /// The total amount of `token_out` actually received by the receiver, summed across all
    /// matching ERC-20 and ERC-6909 Transfer logs. Returns `None` when no matching logs are found
    /// (e.g. the swap reverted or used an unsupported token standard).
    pub fn settled_amount(&self) -> Option<&BigUint> {
        self.settled_amount.as_ref()
    }

    /// The actual gas cost of the transaction in wei (`gas_used * effective_gas_price`).
    pub fn gas_cost(&self) -> &BigUint {
        &self.gas_cost
    }
}

// ============================================================================
// EXECUTION RECEIPT
// ============================================================================

/// A future that resolves once the swap transaction is mined and settled.
///
/// Returned by [`FyndClient::execute_swap`](crate::FyndClient::execute_swap). The inner future
/// polls the RPC node every 2 seconds and **has no built-in timeout** — it will poll indefinitely
/// if the transaction is never mined. Callers should wrap it with [`tokio::time::timeout`]:
///
/// ```rust,no_run
/// # use fynd_client::ExecutionReceipt;
/// # use std::time::Duration;
/// # async fn example(receipt: ExecutionReceipt) {
/// let result = tokio::time::timeout(Duration::from_secs(120), receipt).await;
/// # }
/// ```
pub enum ExecutionReceipt {
    /// A pending on-chain transaction.
    Transaction(Pin<Box<dyn Future<Output = Result<SettledOrder, FyndError>> + Send + 'static>>),
}

impl Future for ExecutionReceipt {
    type Output = Result<SettledOrder, FyndError>;

    fn poll(
        self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Self::Output> {
        match self.get_mut() {
            Self::Transaction(fut) => fut.as_mut().poll(cx),
        }
    }
}

// ============================================================================
// APPROVAL PAYLOAD
// ============================================================================

/// An unsigned EIP-1559 `approve(spender, amount)` transaction.
///
/// Obtain via [`FyndClient::approval`](crate::FyndClient::approval). Sign its
/// [`signing_hash`](Self::signing_hash) and pass the result to [`SignedApproval::assemble`].
pub struct ApprovalPayload {
    pub(crate) tx: TxEip1559,
    /// ERC-20 token contract address (20 raw bytes).
    pub(crate) token: bytes::Bytes,
    /// Spender address being approved (20 raw bytes).
    pub(crate) spender: bytes::Bytes,
    /// Amount being approved (token units).
    pub(crate) amount: BigUint,
}

impl ApprovalPayload {
    /// The 32-byte hash to sign.
    pub fn signing_hash(&self) -> B256 {
        use alloy::consensus::SignableTransaction;
        self.tx.signature_hash()
    }

    /// The unsigned EIP-1559 transaction.
    pub fn tx(&self) -> &TxEip1559 {
        &self.tx
    }

    /// ERC-20 token address (20 raw bytes).
    pub fn token(&self) -> &bytes::Bytes {
        &self.token
    }

    /// Spender address (20 raw bytes).
    pub fn spender(&self) -> &bytes::Bytes {
        &self.spender
    }

    /// Amount to approve (token units).
    pub fn amount(&self) -> &BigUint {
        &self.amount
    }
}

/// An [`ApprovalPayload`] paired with its cryptographic signature.
///
/// Construct via [`SignedApproval::assemble`] after signing the
/// [`signing_hash`](ApprovalPayload::signing_hash). Pass to
/// [`FyndClient::execute_approval`](crate::FyndClient::execute_approval).
pub struct SignedApproval {
    payload: ApprovalPayload,
    signature: Signature,
}

impl SignedApproval {
    /// Pair a payload with the signature produced by signing its
    /// [`signing_hash`](ApprovalPayload::signing_hash).
    pub fn assemble(payload: ApprovalPayload, signature: Signature) -> Self {
        Self { payload, signature }
    }

    /// The underlying approval payload.
    pub fn payload(&self) -> &ApprovalPayload {
        &self.payload
    }

    /// The signature over the payload's signing hash.
    pub fn signature(&self) -> &Signature {
        &self.signature
    }

    pub(crate) fn into_parts(self) -> (ApprovalPayload, Signature) {
        (self.payload, self.signature)
    }
}

// ============================================================================
// MINED TX / TX RECEIPT
// ============================================================================

/// The result of a successfully mined transaction (non-swap).
#[derive(Debug, Clone)]
pub struct MinedTx {
    tx_hash: B256,
    gas_cost: BigUint,
}

impl MinedTx {
    pub(crate) fn new(tx_hash: B256, gas_cost: BigUint) -> Self {
        Self { tx_hash, gas_cost }
    }

    /// Transaction hash.
    pub fn tx_hash(&self) -> B256 {
        self.tx_hash
    }

    /// Actual gas cost in wei (`gas_used * effective_gas_price`).
    pub fn gas_cost(&self) -> &BigUint {
        &self.gas_cost
    }
}

/// A future that resolves once a submitted transaction is mined.
///
/// Returned by [`FyndClient::execute_approval`](crate::FyndClient::execute_approval). Polls the RPC
/// node every 2 seconds with no built-in timeout — wrap with [`tokio::time::timeout`] as needed.
pub enum TxReceipt {
    /// A pending on-chain transaction.
    Pending(Pin<Box<dyn Future<Output = Result<MinedTx, FyndError>> + Send + 'static>>),
}

impl Future for TxReceipt {
    type Output = Result<MinedTx, FyndError>;

    fn poll(
        self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Self::Output> {
        match self.get_mut() {
            Self::Pending(fut) => fut.as_mut().poll(cx),
        }
    }
}

// ============================================================================
// TRANSFER LOG DECODING
// ============================================================================

/// Compute the total amount of `token_out` received by `receiver` from a transaction receipt.
///
/// Scans ERC-20 and ERC-6909 Transfer logs matching the given token address and receiver.
pub(crate) fn compute_settled_amount(
    receipt: &alloy::rpc::types::TransactionReceipt,
    token_out_addr: &Address,
    receiver_addr: &Address,
) -> Option<BigUint> {
    use alloy::primitives::keccak256;

    // ERC-20: Transfer(address indexed from, address indexed to, uint256 value)
    let erc20_topic = keccak256(b"Transfer(address,address,uint256)");
    // ERC-6909: Transfer(address caller, address indexed from, address indexed to,
    //                    uint256 indexed id, uint256 amount)
    let erc6909_topic = keccak256(b"Transfer(address,address,address,uint256,uint256)");

    let mut total = BigUint::ZERO;
    let mut found = false;

    for log in receipt.logs() {
        if log.address() != *token_out_addr {
            continue;
        }
        let topics = log.topics();
        if topics.is_empty() {
            continue;
        }

        if topics[0] == erc20_topic && topics.len() >= 3 {
            // topics[2] is the `to` address (padded to 32 bytes); address is in last 20 bytes.
            let to = Address::from_slice(&topics[2].as_slice()[12..]);
            if to == *receiver_addr {
                let data = &log.data().data;
                if data.len() >= 32 {
                    let amount = BigUint::from_bytes_be(&data[0..32]);
                    total += amount;
                    found = true;
                }
            }
        } else if topics[0] == erc6909_topic && topics.len() >= 3 {
            // topics[2] is the `to` address.
            let to = Address::from_slice(&topics[2].as_slice()[12..]);
            if to == *receiver_addr {
                // data encodes (address caller, uint256 amount) = 64 bytes; amount at bytes 32..64.
                let data = &log.data().data;
                if data.len() >= 64 {
                    let amount = BigUint::from_bytes_be(&data[32..64]);
                    total += amount;
                    found = true;
                }
            }
        }
    }

    if found {
        Some(total)
    } else {
        None
    }
}

#[cfg(test)]
mod tests {
    use alloy::{
        primitives::{keccak256, Address, Bytes as AlloyBytes, LogData, B256},
        rpc::types::{Log, TransactionReceipt},
    };

    use super::*;

    // -----------------------------------------------------------------------
    // Helpers
    // -----------------------------------------------------------------------

    fn make_receipt(logs: Vec<Log>) -> TransactionReceipt {
        use alloy::{
            consensus::{Receipt, ReceiptEnvelope, ReceiptWithBloom},
            primitives::{Bloom, TxHash},
        };

        TransactionReceipt {
            inner: ReceiptEnvelope::Eip1559(ReceiptWithBloom {
                receipt: Receipt {
                    status: alloy::consensus::Eip658Value::Eip658(true),
                    cumulative_gas_used: 21_000,
                    logs,
                },
                logs_bloom: Bloom::default(),
            }),
            transaction_hash: TxHash::default(),
            transaction_index: None,
            block_hash: None,
            block_number: None,
            gas_used: 21_000,
            effective_gas_price: 1,
            blob_gas_used: None,
            blob_gas_price: None,
            from: Address::ZERO,
            to: None,
            contract_address: None,
        }
    }

    fn erc20_topic() -> B256 {
        keccak256(b"Transfer(address,address,uint256)")
    }

    fn erc6909_topic() -> B256 {
        keccak256(b"Transfer(address,address,address,uint256,uint256)")
    }

    /// Pad an address into a 32-byte B256 topic (right-align in 32 bytes).
    fn addr_topic(addr: Address) -> B256 {
        let mut topic = [0u8; 32];
        topic[12..].copy_from_slice(addr.as_slice());
        B256::from(topic)
    }

    /// Encode a u64 amount as 32 big-endian bytes.
    fn encode_u256(amount: u64) -> Vec<u8> {
        let mut buf = [0u8; 32];
        let bytes = amount.to_be_bytes();
        buf[24..].copy_from_slice(&bytes);
        buf.to_vec()
    }

    fn make_log(address: Address, topics: Vec<B256>, data: Vec<u8>) -> Log {
        Log {
            inner: alloy::primitives::Log {
                address,
                data: LogData::new_unchecked(topics, AlloyBytes::from(data)),
            },
            block_hash: None,
            block_number: None,
            block_timestamp: None,
            transaction_hash: None,
            transaction_index: None,
            log_index: None,
            removed: false,
        }
    }

    // -----------------------------------------------------------------------
    // ERC-20 tests
    // -----------------------------------------------------------------------

    #[test]
    fn erc20_transfer_log_matched() {
        let token = Address::with_last_byte(0x01);
        let from = Address::with_last_byte(0x02);
        let receiver = Address::with_last_byte(0x03);

        let log = make_log(
            token,
            vec![erc20_topic(), addr_topic(from), addr_topic(receiver)],
            encode_u256(500),
        );
        let receipt = make_receipt(vec![log]);

        let result = compute_settled_amount(&receipt, &token, &receiver);
        assert_eq!(result, Some(BigUint::from(500u64)));
    }

    #[test]
    fn erc20_transfer_log_wrong_token() {
        let token = Address::with_last_byte(0x01);
        let other_token = Address::with_last_byte(0x99);
        let receiver = Address::with_last_byte(0x03);

        let log = make_log(
            other_token, // different token address
            vec![erc20_topic(), addr_topic(Address::ZERO), addr_topic(receiver)],
            encode_u256(500),
        );
        let receipt = make_receipt(vec![log]);

        let result = compute_settled_amount(&receipt, &token, &receiver);
        assert!(result.is_none());
    }

    #[test]
    fn erc20_transfer_log_wrong_receiver() {
        let token = Address::with_last_byte(0x01);
        let receiver = Address::with_last_byte(0x03);
        let other_receiver = Address::with_last_byte(0x04);

        let log = make_log(
            token,
            vec![erc20_topic(), addr_topic(Address::ZERO), addr_topic(other_receiver)],
            encode_u256(500),
        );
        let receipt = make_receipt(vec![log]);

        let result = compute_settled_amount(&receipt, &token, &receiver);
        assert!(result.is_none());
    }

    // -----------------------------------------------------------------------
    // ERC-6909 tests
    // -----------------------------------------------------------------------

    #[test]
    fn erc6909_transfer_log_matched() {
        let token = Address::with_last_byte(0x10);
        let from = Address::with_last_byte(0x11);
        let receiver = Address::with_last_byte(0x12);

        // ERC-6909: data = (address caller [32 bytes], uint256 amount [32 bytes])
        let mut data = [0u8; 64];
        // caller address in first 32 bytes (right-aligned)
        data[12..32].copy_from_slice(Address::with_last_byte(0xca).as_slice());
        // amount in last 32 bytes
        let amount_bytes = encode_u256(750);
        data[32..].copy_from_slice(&amount_bytes);

        let log = make_log(
            token,
            vec![erc6909_topic(), addr_topic(from), addr_topic(receiver)],
            data.to_vec(),
        );
        let receipt = make_receipt(vec![log]);

        let result = compute_settled_amount(&receipt, &token, &receiver);
        assert_eq!(result, Some(BigUint::from(750u64)));
    }

    // -----------------------------------------------------------------------
    // Edge cases
    // -----------------------------------------------------------------------

    #[test]
    fn no_matching_logs_returns_none() {
        let token = Address::with_last_byte(0x01);
        let receiver = Address::with_last_byte(0x03);

        // Log with unrelated topic
        let unrelated_topic = keccak256(b"Approval(address,address,uint256)");
        let log = make_log(
            token,
            vec![unrelated_topic, addr_topic(Address::ZERO), addr_topic(receiver)],
            encode_u256(100),
        );
        let receipt = make_receipt(vec![log]);

        let result = compute_settled_amount(&receipt, &token, &receiver);
        assert!(result.is_none());
    }

    #[test]
    fn empty_logs_returns_none() {
        let token = Address::with_last_byte(0x01);
        let receiver = Address::with_last_byte(0x03);
        let receipt = make_receipt(vec![]);
        assert!(compute_settled_amount(&receipt, &token, &receiver).is_none());
    }

    #[test]
    fn multiple_matching_logs_amounts_summed() {
        let token = Address::with_last_byte(0x01);
        let from = Address::with_last_byte(0x02);
        let receiver = Address::with_last_byte(0x03);

        let log1 = make_log(
            token,
            vec![erc20_topic(), addr_topic(from), addr_topic(receiver)],
            encode_u256(100),
        );
        let log2 = make_log(
            token,
            vec![erc20_topic(), addr_topic(from), addr_topic(receiver)],
            encode_u256(200),
        );
        // A log to a different receiver that should NOT be counted.
        let log3 = make_log(
            token,
            vec![erc20_topic(), addr_topic(from), addr_topic(Address::with_last_byte(0xff))],
            encode_u256(999),
        );
        let receipt = make_receipt(vec![log1, log2, log3]);

        let result = compute_settled_amount(&receipt, &token, &receiver);
        assert_eq!(result, Some(BigUint::from(300u64)));
    }
}