lamarge 0.6.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
//! The Polygon wallet conversion: native USDC converted into pUSD by the payer's own
//! transactions (an `approve` when the allowance is short, then the convert call) before a
//! wallet-funded transfer from Polymarket takes. Each transaction is journalled before it is
//! asked and once it has a hash, so it is never asked twice.

use alloy_primitives::{Address, B256, U256, address};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use super::records;
use crate::{
    Error, Result, SendError, SendStatus, Session, Transaction, TransactionSender,
    deployments::{self, POLYMARKET},
    error::VerificationError,
    model::{ConversionState, EscrowChain, TransferIntent, WalletConversion},
    protocol::checks::{
        details::{self, changed, differs, stale, unsupported},
        funding::check_wallet_funding,
    },
    transport::{Call, ops},
    types::{Amount, VenueId},
};

/// The converter the escrow's conversion of Polygon native USDC into pUSD is pinned to.
pub const POLYGON_FUNDING_CONVERTER: Address = address!("2d31ff03f5da1c385a64d764389962c160e789f8");
/// Polygon's native USDC, which a conversion spends: Polymarket's wallet token.
pub const POLYGON_USDC: Address = match POLYMARKET.wallet_token {
    Some(token) => token,
    None => Address::ZERO,
};
/// The least native USDC a conversion converts: one cent.
pub(super) const LEAST_CONVERTED: u64 = 10_000;
/// How far ahead a conversion's deadline may be: the server's 30 minutes and 15 seconds of skew.
pub(super) const DEADLINE_WINDOW_SECONDS: i64 = 1_815;
/// `approve(address,uint256)`.
pub(super) const APPROVE: [u8; 4] = [0x09, 0x5e, 0xa7, 0xb3];
/// The escrow's `convert(uint256 amountIn, uint256 minOut, uint256 nonce, uint256 deadline)`.
pub(super) const CONVERT: [u8; 4] = [0x32, 0x0a, 0x91, 0xb1];

/// The least pUSD the escrow lets a conversion of `amount_in` native USDC return:
/// `⌈amount_in × 9 995 / 10 000⌉`; `None` on overflow.
pub fn conversion_minimum(amount_in: Amount) -> Option<Amount> {
    let scaled = amount_in.get().checked_mul(U256::from(deployments::WALLET_SWAP_MIN_BPS))?;
    Some(Amount::new(scaled.div_ceil(U256::from(10_000u16))))
}

/// The Polygon conversion `chain` advertises is the pinned one: [`POLYGON_FUNDING_CONVERTER`],
/// [`POLYGON_USDC`] of six decimals, and the escrow's floor.
///
/// # Errors
///
/// `Verification(Unsupported)` when `chain` advertises no conversion, `Verification(Differs)`
/// when it advertises another.
pub fn check_conversion_capability(chain: &EscrowChain) -> Result<()> {
    let capability = chain
        .wallet_funding
        .as_ref()
        .and_then(|funding| funding.conversion.as_ref())
        .ok_or(unsupported(details::WALLET_FUNDING))?;
    let pinned = capability.converter == POLYGON_FUNDING_CONVERTER
        && capability.token == POLYGON_USDC
        && capability.decimals == POLYMARKET.decimals
        && capability.min_out_bps == u32::from(deployments::WALLET_SWAP_MIN_BPS);
    if pinned { Ok(()) } else { Err(differs(details::WALLET_FUNDING).into()) }
}

/// A conversion funding a wallet-funded transfer from Polymarket: `amount_in` native USDC into
/// at least `min_out` pUSD, the transfer's `amount_in`. `min_out` must be at least 99.95 % of
/// `amount_in`, rounded up, and `amount_in` at least 0.01 USDC.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct ConversionTerms {
    amount_in: Amount,
    min_out: Amount,
}

impl ConversionTerms {
    /// Converts `amount_in` native USDC into at least `min_out` pUSD.
    pub const fn new(amount_in: Amount, min_out: Amount) -> Self {
        Self { amount_in, min_out }
    }

    /// The most native USDC whose conversion floor is `min_out` pUSD: `⌊min_out × 10 000 /
    /// 9 995⌋`. Use it to fund a transfer of `min_out` and quote what the owner spends.
    pub fn covering(min_out: Amount) -> Option<Self> {
        let bps = U256::from(deployments::WALLET_SWAP_MIN_BPS);
        let amount_in = min_out.get().checked_mul(U256::from(10_000u16))? / bps;
        Some(Self { amount_in: Amount::new(amount_in), min_out })
    }

    /// Native USDC converted, in base units.
    pub const fn amount_in(&self) -> Amount {
        self.amount_in
    }

    /// The least pUSD received, in base units.
    pub const fn min_out(&self) -> Amount {
        self.min_out
    }

    /// At least one cent, and a floor no lower than the escrow's.
    pub(super) fn valid(&self) -> bool {
        self.amount_in.get() >= U256::from(LEAST_CONVERTED)
            && conversion_minimum(self.amount_in).is_some_and(|floor| self.min_out >= floor)
    }
}

/// What a conversion will ask the sender for, read before anything is asked.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ConversionPreview {
    /// The chain the transactions are sent on (Polygon, 137).
    pub chain_id: u64,
    /// The address that sends them and pays their gas.
    pub payer: Address,
    /// Its native USDC balance, in base units.
    pub balance: Amount,
    /// Whether an `approve` is sent before the convert call: the escrow's allowance is short.
    pub approval: bool,
    /// How many transactions: 1, or 2 with the `approve`.
    pub transactions: u8,
    /// Whether the balance covers the conversion's `amount_in`.
    pub covered: bool,
}

impl Session {
    /// What converting `terms` from the session's wallet would ask the sender for, without
    /// asking: the chain, whether an `approve` comes first, and whether the balance covers it.
    ///
    /// # Errors
    ///
    /// [`Error::InvalidRequest`] for terms the escrow refuses; `Verification(..)` when the
    /// conversion is unavailable or its pinned deployment differs.
    pub async fn conversion_preview(&self, terms: &ConversionTerms) -> Result<ConversionPreview> {
        if !terms.valid() {
            return Err(Error::InvalidRequest("conversion below the escrow's floor".into()));
        }
        let escrow = check_capability(self).await?;
        let funds = self.account().wallet_funding(&VenueId::Polymarket, None).await?;
        let balance = convertible(self, &funds, escrow)?;
        let approval = balance.allowance < terms.amount_in;
        Ok(ConversionPreview {
            chain_id: POLYMARKET.id,
            payer: self.address(),
            balance: balance.balance,
            approval,
            transactions: if approval { 2 } else { 1 },
            covered: balance.balance >= terms.amount_in,
        })
    }
}

/// The transfer's conversion is the request's: both or neither, its terms, from Polymarket's
/// wallet, its floor kept.
pub(super) fn check_conversion(
    intent: &TransferIntent,
    requested: Option<&ConversionTerms>,
) -> std::result::Result<(), VerificationError> {
    match (&intent.conversion, requested) {
        (None, None) => Ok(()),
        (Some(conversion), Some(requested)) => check_terms(intent, conversion, requested),
        _ => Err(changed(details::FUNDING)),
    }
}

fn check_terms(
    intent: &TransferIntent,
    conversion: &WalletConversion,
    requested: &ConversionTerms,
) -> std::result::Result<(), VerificationError> {
    let terms = ConversionTerms::new(conversion.amount_in, conversion.min_out);
    let ok = intent.from == VenueId::Polymarket
        && terms == *requested
        && intent.amount_in == conversion.min_out
        && terms.valid();
    if ok { Ok(()) } else { Err(changed(details::FUNDING)) }
}

/// Where a conversion stands after one pass.
pub(super) enum Progress {
    /// Converted: the take may pay.
    Ready,
    /// Sent or being confirmed: read again later.
    Waiting,
    /// The sender sent nothing (declined, timed out): running again asks again.
    NotSent,
}

/// A conversion's terms, journalled under `conversion:{id}:terms` and
/// `conversion-intent:{transfer id}`: a conversion answered with others stops.
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct Terms {
    id: Uuid,
    escrow: Address,
    payer: Address,
    amount_in: Amount,
    min_out: Amount,
    nonce: Amount,
    deadline: Amount,
}

/// A transaction asked of the sender: saved before asking, with its hash once returned;
/// `refused` once the sender said it sent nothing.
#[derive(Debug, Default, Serialize, Deserialize)]
struct Asked {
    hash: Option<B256>,
    #[serde(default)]
    refused: bool,
}

#[derive(Serialize)]
struct Report {
    #[serde(skip_serializing_if = "Option::is_none")]
    tx_hash: Option<B256>,
}

/// One pass of the transfer's conversion: prepares it (idempotent), checks it, and sends what it
/// still needs.
pub(super) async fn convert(
    session: &Session,
    intent: &TransferIntent,
    requested: &ConversionTerms,
    sender: &dyn TransactionSender,
) -> Result<Progress> {
    let call = Call::new(&ops::PREPARE_TRANSFER_CONVERSION).segment(intent.id);
    let conversion: WalletConversion = session.transport().send(call, session.auth()).await?;
    check_terms(intent, &conversion, requested)?;
    let terms = pinned(session, &conversion)?;
    let journal = session.journal();
    let key = |record: &str| session.record_key(&format!("conversion:{}:{record}", terms.id));
    let _turn = journal.lock(&key("turn")).await;
    for record in [session.record_key(&format!("conversion-intent:{}", intent.id)), key("terms")] {
        if let Some(saved) = records::load::<Terms>(journal, &record).await?
            && saved != terms
        {
            return Err(differs(details::FUNDING).into());
        }
        records::save(journal, &record, &terms, false).await?;
    }
    let state = conversion.state.clone().unwrap_or(ConversionState::Prepared);
    match state {
        ConversionState::Ready => {
            let received =
                conversion.received.as_deref().and_then(|text| text.parse::<U256>().ok());
            return if received.is_some_and(|received| received >= terms.min_out.get())
                && conversion.tx_hash.is_some()
            {
                Ok(Progress::Ready)
            } else {
                Err(differs(details::FUNDING).into())
            };
        }
        ConversionState::Expired => return Err(stale(details::DEADLINE).into()),
        _ => {}
    }

    let ids = (
        format!("lamarge.conversion.{}.approve", terms.id),
        format!("lamarge.conversion.{}.convert", terms.id),
    );
    let sent_key = key("sent");
    let mut sent = records::load::<Asked>(journal, &sent_key).await?;
    if let Some(asked) = &mut sent
        && asked.hash.is_none()
        && !asked.refused
    {
        // Asked with no answer recorded: only the sender knows whether it went.
        match status(sender, &ids.1).await {
            SendStatus::Sent(hash) => asked.hash = Some(hash),
            SendStatus::NotSent => asked.refused = true,
            _ => return Ok(Progress::Waiting),
        }
        records::save(journal, &sent_key, asked, true).await?;
    }
    if let Some(hash) = sent.as_ref().and_then(|asked| asked.hash) {
        let attempts = conversion.attempts.as_deref().unwrap_or_default();
        if !attempts.iter().any(|attempt| attempt.parse::<B256>().is_ok_and(|seen| seen == hash)) {
            report(session, intent, Some(hash)).await?;
        }
        return Ok(Progress::Waiting);
    }
    let retry = sent.as_ref().is_some_and(|asked| asked.refused);
    if !retry && state == ConversionState::Pending {
        // Fenced by another client: it sends, this run follows.
        return Ok(Progress::Waiting);
    }
    let now = session.transport().now().timestamp();
    let deadline = i64::try_from(terms.deadline.get()).unwrap_or(i64::MAX);
    if !(state == ConversionState::Prepared || state == ConversionState::Pending)
        || deadline <= now
        || deadline > now + DEADLINE_WINDOW_SECONDS
    {
        return Err(stale(details::DEADLINE).into());
    }

    // The capability, the escrow and the balance, read fresh before anything is asked.
    if check_capability(session).await? != terms.escrow {
        return Err(differs(details::ESCROW).into());
    }
    let funds = session.account().wallet_funding(&VenueId::Polymarket, None).await?;
    let balance = convertible(session, &funds, terms.escrow)?;
    if balance.balance < terms.amount_in {
        return Err(changed(details::WALLET_FUNDS).into());
    }
    if balance.allowance < terms.amount_in {
        return approve(session, sender, &terms, &key("approval"), &ids.0).await;
    }
    let fence = state == ConversionState::Prepared;
    send_convert(session, intent, sender, &terms, &sent_key, ids.1, fence).await
}

/// The convert call, journalled before it is asked; fenced first while the conversion is only
/// prepared.
async fn send_convert(
    session: &Session,
    intent: &TransferIntent,
    sender: &dyn TransactionSender,
    terms: &Terms,
    sent_key: &str,
    id: String,
    fence: bool,
) -> Result<Progress> {
    let journal = session.journal();
    records::save(journal, sent_key, &Asked::default(), false).await?;
    if fence {
        // The fence comes before the prompt; after a lost answer, no prompt follows.
        let call = Call::new(&ops::FENCE_TRANSFER_CONVERSION)
            .segment(intent.id)
            .json(&Report { tx_hash: None })?;
        session.transport().send::<WalletConversion>(call, session.auth()).await?;
    }
    let mut data = CONVERT.to_vec();
    for word in [terms.amount_in, terms.min_out, terms.nonce, terms.deadline] {
        data.extend_from_slice(&word.get().to_be_bytes::<32>());
    }
    match sender.send(transaction(id, terms.escrow, data)).await {
        Ok(hash) => {
            records::save(journal, sent_key, &Asked { hash: Some(hash), refused: false }, true)
                .await?;
            report(session, intent, Some(hash)).await?;
            Ok(Progress::Waiting)
        }
        Err(SendError::NotSent(error)) => {
            tracing::info!(%error, "conversion not sent");
            records::save(journal, sent_key, &Asked { hash: None, refused: true }, true).await?;
            Ok(Progress::NotSent)
        }
        Err(error) => {
            tracing::warn!(%error, "conversion outcome unknown");
            Ok(Progress::Waiting)
        }
    }
}

/// The escrow's allowance, asked once: an `approve` sent or of unknown outcome is waited for
/// until the allowance shows it.
async fn approve(
    session: &Session,
    sender: &dyn TransactionSender,
    terms: &Terms,
    key: &str,
    id: &str,
) -> Result<Progress> {
    let journal = session.journal();
    if let Some(asked) = records::load::<Asked>(journal, key).await? {
        if asked.hash.is_some() {
            return Ok(Progress::Waiting);
        }
        match status(sender, id).await {
            SendStatus::Sent(hash) => {
                records::save(journal, key, &Asked { hash: Some(hash), refused: false }, true)
                    .await?;
                return Ok(Progress::Waiting);
            }
            SendStatus::NotSent => {}
            _ => return Ok(Progress::Waiting),
        }
    }
    records::save(journal, key, &Asked::default(), false).await?;
    let mut data = APPROVE.to_vec();
    data.extend_from_slice(&[0; 12]);
    data.extend_from_slice(terms.escrow.as_slice());
    data.extend_from_slice(&terms.amount_in.get().to_be_bytes::<32>());
    match sender.send(transaction(id.to_owned(), POLYGON_USDC, data)).await {
        Ok(hash) => {
            records::save(journal, key, &Asked { hash: Some(hash), refused: false }, true).await?;
            Ok(Progress::Waiting)
        }
        Err(SendError::NotSent(error)) => {
            tracing::info!(%error, "approval not sent");
            records::delete(journal, key, true).await?;
            Ok(Progress::NotSent)
        }
        Err(error) => {
            tracing::warn!(%error, "approval outcome unknown");
            Ok(Progress::Waiting)
        }
    }
}

fn transaction(id: String, to: Address, data: Vec<u8>) -> Transaction {
    Transaction { id, chain_id: POLYMARKET.id, to, data, value: U256::ZERO }
}

/// What the sender knows of `id`; a failed lookup knows nothing.
async fn status(sender: &dyn TransactionSender, id: &str) -> SendStatus {
    sender.status(id).await.unwrap_or(SendStatus::Unknown)
}

async fn report(session: &Session, intent: &TransferIntent, hash: Option<B256>) -> Result<()> {
    let call = Call::new(&ops::REPORT_TRANSFER_CONVERSION)
        .segment(intent.id)
        .json(&Report { tx_hash: hash })?;
    session.transport().send::<WalletConversion>(call, session.auth()).await.map(drop)
}

/// The prepared conversion on the pinned deployment: Polygon, the environment's escrow, the
/// session's address, native USDC and the pinned converter.
fn pinned(session: &Session, conversion: &WalletConversion) -> Result<Terms> {
    let escrow = deployments::escrow(POLYMARKET.id, session.escrow_environment());
    let ok = conversion.chain_id == Some(POLYMARKET.id)
        && conversion.escrow.is_some()
        && conversion.escrow == escrow
        && conversion.payer == Some(session.address())
        && conversion.token == Some(POLYGON_USDC)
        && conversion.converter == Some(POLYGON_FUNDING_CONVERTER);
    let (Some(id), Some(escrow), Some(nonce), Some(deadline), true) =
        (conversion.id, conversion.escrow, conversion.nonce, conversion.deadline, ok)
    else {
        return Err(differs(details::FUNDING).into());
    };
    Ok(Terms {
        id,
        escrow,
        payer: session.address(),
        amount_in: conversion.amount_in,
        min_out: conversion.min_out,
        nonce,
        deadline,
    })
}

/// Polymarket's advertised conversion is the pinned one; its escrow.
async fn check_capability(session: &Session) -> Result<Address> {
    let venues = session.client().market().venues().await?;
    let chain = venues
        .iter()
        .find(|venue| venue.id == VenueId::Polymarket && venue.enabled)
        .and_then(|venue| venue.chain.as_ref())
        .ok_or(unsupported(details::VENUE))?;
    if deployments::escrow(POLYMARKET.id, session.escrow_environment()) != Some(chain.escrow) {
        return Err(differs(details::ESCROW).into());
    }
    check_conversion_capability(chain)?;
    Ok(chain.escrow)
}

/// The native USDC balance and allowance of a fresh, checked wallet read on `escrow`.
fn convertible(
    session: &Session,
    funds: &crate::model::WalletFunding,
    escrow: Address,
) -> Result<crate::model::ConversionBalance> {
    let (venue, payer, environment) =
        (&VenueId::Polymarket, session.address(), session.escrow_environment());
    check_wallet_funding(funds, venue, payer, environment, session.transport().now())?;
    let balance = funds.conversion.as_ref().ok_or(unsupported(details::WALLET_FUNDING))?;
    if funds.escrow != escrow || balance.token != POLYGON_USDC {
        return Err(differs(details::WALLET_FUNDS).into());
    }
    Ok(balance.clone())
}

#[cfg(test)]
mod tests;