forked from StellarSend/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayment.rs
More file actions
105 lines (96 loc) · 3.14 KB
/
Copy pathpayment.rs
File metadata and controls
105 lines (96 loc) · 3.14 KB
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
use serde::{Deserialize, Serialize};
/// An asset on the Stellar network — either native XLM or a code:issuer pair.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Asset {
/// e.g. "XLM", "USDC", "BTC"
pub code: String,
/// None for XLM (native), Some(issuer_address) otherwise
pub issuer: Option<String>,
}
impl Asset {
pub fn native() -> Self {
Self {
code: "XLM".to_string(),
issuer: None,
}
}
pub fn is_native(&self) -> bool {
self.code.to_uppercase() == "XLM" && self.issuer.is_none()
}
/// Format as "native" or "CODE:ISSUER" for Horizon query params.
pub fn to_horizon_param(&self) -> String {
if self.is_native() {
"native".to_string()
} else {
format!(
"{}:{}",
self.code,
self.issuer.as_deref().unwrap_or_default()
)
}
}
}
/// One hop in a payment path.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PathHop {
pub asset_code: String,
pub asset_issuer: Option<String>,
pub asset_type: String,
}
/// Request body for POST /api/payments/quote.
#[derive(Debug, Deserialize)]
pub struct QuoteRequest {
pub from_asset: Asset,
pub to_asset: Asset,
/// Amount to send (in the from_asset denomination).
pub amount: String,
pub destination: String,
}
/// Response body for GET /api/payments/quote.
#[derive(Debug, Serialize)]
pub struct QuoteResponse {
/// Amount the destination will receive.
pub estimated_receive: String,
/// Implied exchange rate (to_amount / from_amount).
pub rate: String,
/// Estimated Stellar network fee in XLM.
pub fee_xlm: String,
/// Asset hops between source and destination.
pub path: Vec<PathHop>,
/// The raw send amount echoed back.
pub send_amount: String,
pub from_asset: Asset,
pub to_asset: Asset,
}
/// Request body for POST /api/payments/send.
///
/// The client builds and signs the Stellar transaction client-side, then
/// submits the signed XDR envelope here for relay to Horizon.
#[derive(Debug, Deserialize)]
pub struct SendPaymentRequest {
/// The Stellar public key of the sending account.
pub source_account: String,
/// Base64-encoded signed TransactionEnvelope XDR.
pub signed_xdr: String,
/// Internal transaction record id (returned by the quote or a prior POST).
pub transaction_id: Option<uuid::Uuid>,
// Metadata echoed into the DB record when transaction_id is None.
pub from_asset: Option<super::payment::Asset>,
pub to_asset: Option<super::payment::Asset>,
pub send_amount: Option<String>,
pub destination_account: Option<String>,
}
/// Response body for POST /api/payments/send.
#[derive(Debug, Serialize)]
pub struct PaymentResult {
/// Stellar transaction hash.
pub tx_hash: String,
/// Whether the Horizon submission was successful.
pub success: bool,
/// Ledger number in which the transaction was included.
pub ledger: Option<u64>,
/// Fee charged in stroops.
pub fee_charged: Option<String>,
/// Internal transaction id.
pub transaction_id: uuid::Uuid,
}