breez_sdk_spark/sdk/bridging/models.rs
1use serde::{Deserialize, Serialize};
2
3use crate::{
4 CrossChainConfig, CrossChainProvider, CrossChainReceiveInfo, CrossChainRouteLimits, FeePolicy,
5 ProxyConfig,
6};
7
8/// Settings for a [`BridgingClient`](super::BridgingClient) built with
9/// [`new_bridging_client`](super::new_bridging_client).
10#[derive(Debug, Clone)]
11#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
12pub struct BridgingConfig {
13 /// Breez API key.
14 pub api_key: String,
15 /// Routes the client's connections through a SOCKS5 proxy.
16 #[cfg_attr(feature = "uniffi", uniffi(default = None))]
17 pub proxy: Option<ProxyConfig>,
18 /// Default slippage and overpay. Unset uses the built-in defaults.
19 #[cfg_attr(feature = "uniffi", uniffi(default = None))]
20 pub cross_chain_config: Option<CrossChainConfig>,
21}
22
23#[derive(Debug, Clone)]
24#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
25pub struct GetBridgeRoutesRequest {
26 /// Who to pay: a Spark address, a Lightning address (a Cash App
27 /// `$cashtag` included), a Bitcoin address or BIP21 URI, an
28 /// EVM/Solana/Tron address or URI, or an XRP or Zcash transparent address.
29 pub recipient: String,
30}
31
32#[derive(Debug, Clone, Serialize)]
33#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
34pub struct GetBridgeRoutesResponse {
35 /// The routes that can pay the recipient. Empty when none can.
36 pub routes: Vec<BridgeRoutePair>,
37}
38
39/// A way to pay a recipient: what the payer pays, and what the recipient
40/// receives.
41#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
42#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
43pub struct BridgeRoutePair {
44 pub provider: CrossChainProvider,
45 /// What the payer pays.
46 pub source: BridgeRoutePairSide,
47 /// What the recipient receives.
48 pub destination: BridgeRoutePairSide,
49 /// Bounds on what the payer pays, in `source` base units. On a route
50 /// paying Bitcoin on-chain, the minimum is what must reach the recipient.
51 /// Unset when the provider publishes none.
52 pub limits: Option<CrossChainRouteLimits>,
53 /// The side whose base units [`CreateBridgeRequest::amount`] is in, per
54 /// its `decimals`.
55 pub amount_side: BridgeAmountSide,
56}
57
58/// The side of a [`BridgeRoutePair`] whose base units an amount is in.
59#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
60#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
61pub enum BridgeAmountSide {
62 /// What the payer pays.
63 Source,
64 /// What the recipient receives.
65 Destination,
66}
67
68/// One side of a [`BridgeRoutePair`].
69#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
70#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
71pub struct BridgeRoutePairSide {
72 /// `"spark"`, `"lightning"`, `"bitcoin"`, or another chain, e.g. `"base"`
73 /// or `"tron"`.
74 pub chain: String,
75 /// The provider's id for the chain: the EVM `chainId` as a decimal string,
76 /// or an id such as `"xrp:mainnet"` elsewhere. Unset when it gives none.
77 pub chain_id: Option<String>,
78 /// Asset symbol, e.g. `"USDC"`, `"USDT"`, `"BTC"`, `"USDB"` or `"ETH"`.
79 pub asset: String,
80 /// Token contract or mint address, or the token identifier on Spark.
81 /// Unset for Bitcoin and for a chain's native coin.
82 pub contract_address: Option<String>,
83 pub decimals: u8,
84}
85
86#[derive(Debug, Clone)]
87#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
88pub struct CreateBridgeRequest {
89 /// Who to pay, as given to [`BridgingClient::get_routes`](super::BridgingClient::get_routes).
90 pub recipient: String,
91 /// A route from [`BridgingClient::get_routes`](super::BridgingClient::get_routes)
92 /// for this recipient.
93 pub route: BridgeRoutePair,
94 /// Amount in the base units of the route's `amount_side`, per its
95 /// `decimals`: the recipient's stablecoin when they are paid in one,
96 /// otherwise what the payer pays (sats over Lightning, or their
97 /// stablecoin). A stablecoin amount at parity is the USD value:
98 /// `1_000_000` is 1 USDC at 6 decimals.
99 pub amount: u128,
100 /// The payer's address on the source chain, where the deposit is refunded
101 /// if the recipient can't be paid. Required to pay a Lightning address or
102 /// when the payer sends from an exchange, and advised for BSC and Tron:
103 /// unset, the provider refunds the sending address only when that is
104 /// safe. When the payer pays over Lightning, it is a Lightning
105 /// address, a Cash App `$cashtag` or an amountless BOLT11 invoice that
106 /// doesn't expire before the bridge, refunded if the payment fails before
107 /// the swap. Anything else is rejected.
108 #[cfg_attr(feature = "uniffi", uniffi(default = None))]
109 pub refund_address: Option<String>,
110 /// Whether fees are added on top of `amount` (`FeesExcluded`, the default)
111 /// or deducted from it (`FeesIncluded`).
112 #[cfg_attr(feature = "uniffi", uniffi(default = None))]
113 pub fee_policy: Option<FeePolicy>,
114 /// Maximum slippage tolerance in basis points. Unset uses the configured
115 /// default.
116 #[cfg_attr(feature = "uniffi", uniffi(default = None))]
117 pub max_slippage_bps: Option<u32>,
118}
119
120#[derive(Debug, Clone, Serialize)]
121#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
122pub struct CreateBridgeResponse {
123 /// Identifies the bridge for [`BridgingClient::get_bridge_status`](super::BridgingClient::get_bridge_status).
124 /// Store it to look up the status later.
125 pub bridge_id: String,
126 /// Pass it to
127 /// [`BridgingClient::get_bridge_status`](super::BridgingClient::get_bridge_status).
128 pub status_token: Option<String>,
129 /// What the payer pays: an EIP-681 URI on EVM chains, a BOLT11 invoice or a
130 /// Cash App link over Lightning, otherwise the bare deposit address.
131 pub payment_request: String,
132 /// The deposit and the expected delivery.
133 pub info: CrossChainReceiveInfo,
134}
135
136#[derive(Debug, Clone)]
137#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
138pub struct GetBridgeStatusRequest {
139 pub bridge_id: String,
140 /// The bridge's status token, if any.
141 #[cfg_attr(feature = "uniffi", uniffi(default = None))]
142 pub status_token: Option<String>,
143}
144
145/// Where a bridge is in its lifecycle.
146#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
147#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
148pub enum BridgeStatus {
149 /// The bridge was created, and the provider hasn't registered a payment
150 /// yet. A payment over Lightning can take several minutes to register. A
151 /// bridge from Cash App becomes `Expired` once it can no longer be paid.
152 /// Other unpaid bridges stay `Created` after their `expires_at`, and a
153 /// late deposit on another chain may still be accepted at a new price.
154 Created,
155 /// The provider is processing the payment, from confirming the deposit to
156 /// paying the recipient. `provider_status` gives the stage.
157 Pending,
158 /// The recipient can't be paid, and the payment is being refunded.
159 Refunding,
160 /// The recipient was paid.
161 Completed,
162 /// The payment was refunded to the payer's refund address, or without one
163 /// to the address it came from.
164 Refunded,
165 /// The bridge failed. `error_code` gives the reason.
166 Failed,
167 /// The bridge can no longer be paid, and nobody paid it.
168 Expired,
169}
170
171#[derive(Debug, Clone, Serialize)]
172#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
173pub struct GetBridgeStatusResponse {
174 pub status: BridgeStatus,
175 /// The provider's own status, e.g. `"bridging"` or `"awaiting_approval"`,
176 /// for display and support. Unset before the payment arrives.
177 pub provider_status: Option<String>,
178 /// The provider's order id, once the payment arrived.
179 pub order_id: Option<String>,
180 /// The amount the payer paid, in the source asset's base units.
181 pub amount_in: Option<u128>,
182 /// The amount the recipient received, in the destination asset's base
183 /// units. Set once completed.
184 pub amount_out: Option<u128>,
185 /// The payer's transaction on the source chain.
186 pub deposit_tx_hash: Option<String>,
187 /// The transaction that paid the recipient, or the Spark transfer when the
188 /// recipient is on Spark. Set once completed.
189 pub delivery_tx_hash: Option<String>,
190 /// The transaction that refunded the payer.
191 pub refund_tx_hash: Option<String>,
192 /// The provider's error code when the bridge failed.
193 pub error_code: Option<String>,
194}