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
//! Contains the endpoint for all payment operations.
use super::{Body, Cursor, Direction, IntoRequest, Limit, Order, Records};
use error::Result;
use http::{Request, Uri};
use resources::{Amount, AssetIdentifier, Operation, PaymentPath};
use std::str::FromStr;
use uri::{self, TryFromUri, UriWrap};

pub use super::account::Payments as ForAccount;
pub use super::ledger::Payments as ForLedger;
pub use super::transaction::Payments as ForTransaction;

/// This endpoint represents all payment operations that are part of validated transactions.
/// The endpoint will return all payments and accepts query params for a cursor, order, and limit.
///
/// <https://www.stellar.org/developers/horizon/reference/endpoints/payments-all.html>
///
/// ## Example
/// ```
/// use stellar_client::sync::Client;
/// use stellar_client::endpoint::payment;
///
/// let client      = Client::horizon_test().unwrap();
/// let endpoint    = payment::All::default();
/// let records     = client.request(endpoint).unwrap();
/// #
/// # assert!(records.records().len() > 0);
/// ```
#[derive(Debug, Default, Clone)]
pub struct All {
    cursor: Option<String>,
    order: Option<Direction>,
    limit: Option<u32>,
}

impl_cursor!(All);
impl_limit!(All);
impl_order!(All);

impl All {
    fn has_query(&self) -> bool {
        self.order.is_some() || self.cursor.is_some() || self.limit.is_some()
    }
}

impl IntoRequest for All {
    type Response = Records<Operation>;

    fn into_request(self, host: &str) -> Result<Request<Body>> {
        let mut uri = format!("{}/payments", host);

        if self.has_query() {
            uri.push_str("?");

            if let Some(order) = self.order {
                uri.push_str(&format!("order={}&", order.to_string()));
            }

            if let Some(cursor) = self.cursor {
                uri.push_str(&format!("cursor={}&", cursor));
            }

            if let Some(limit) = self.limit {
                uri.push_str(&format!("limit={}", limit));
            }
        }

        let uri = Uri::from_str(&uri)?;
        let request = Request::get(uri).body(Body::None)?;
        Ok(request)
    }
}

impl TryFromUri for All {
    fn try_from_wrap(wrap: &UriWrap) -> ::std::result::Result<All, uri::Error> {
        let params = wrap.params();
        Ok(All {
            cursor: params.get_parse("cursor").ok(),
            order: params.get_parse("order").ok(),
            limit: params.get_parse("limit").ok(),
        })
    }
}

#[cfg(test)]
mod all_payments_tests {
    use super::*;

    #[test]
    fn it_leaves_off_the_params_if_not_specified() {
        let ep = All::default();
        let req = ep.into_request("https://www.google.com").unwrap();
        assert_eq!(req.uri().path(), "/payments");
        assert_eq!(req.uri().query(), None);
    }

    #[test]
    fn it_puts_the_query_params_on_the_uri() {
        let ep = All::default()
            .with_cursor("CURSOR")
            .with_limit(123)
            .with_order(Direction::Desc);
        let req = ep.into_request("https://www.google.com").unwrap();
        assert_eq!(req.uri().path(), "/payments");
        assert_eq!(
            req.uri().query(),
            Some("order=desc&cursor=CURSOR&limit=123")
        );
    }

    #[test]
    fn it_parses_query_params_from_uri() {
        let uri: Uri = "/payments?order=desc&cursor=CURSOR&limit=123"
            .parse()
            .unwrap();
        let all = All::try_from(&uri).unwrap();
        assert_eq!(all.order, Some(Direction::Desc));
        assert_eq!(all.cursor, Some("CURSOR".to_string()));
        assert_eq!(all.limit, Some(123));
    }
}

/// This endpoint represents a search for a series of assets through which to route a payment,
/// from source asset (debited from payer) to destination asset (credited to payee).
/// The endpoint will return any payment paths using assets available to a source account to the
/// desired destination asset.
///
/// <https://www.stellar.org/developers/horizon/reference/endpoints/path-finding.html>
///
/// ## Example
/// ```
/// use stellar_client::sync::Client;
/// use stellar_client::endpoint::{payment, Limit};
/// use stellar_client::resources::{Amount, AssetIdentifier, OperationKind};
///
/// let client = Client::horizon_test().unwrap();
///
/// // Cast a wide net for `create_account` operations to ensure two valid accounts.
/// let operations  = client.request(payment::All::default().with_limit(20)).unwrap();
/// let account_ids = &operations
///     .records()
///     .iter()
///     .filter_map(|op| {
///         match op.kind() {
///           &OperationKind::CreateAccount(ref acct) => Some(acct.account()),
///           _ => None
///         }
///     })
///     .take(2)
///     .collect::<Vec<&str>>();
/// # assert_eq!(account_ids.len(), 2);
///
/// let endpoint = payment::FindPath::new(
///     &account_ids[0], // source_account
///     &account_ids[1], // destination_account
///     AssetIdentifier::Native,
///     Amount::new(1)
/// );
///
/// // Now we issue a request for a path to payment of 1 stroop
/// let records = client.request(endpoint).unwrap();
///
/// assert!(records.records().len() > 0);
/// ```
#[derive(Debug, Clone)]
pub struct FindPath {
    source_account: String,
    destination_account: String,
    destination_asset: AssetIdentifier,
    destination_amount: Amount,
}

impl FindPath {
    /// Creates a new payment::FindPath endpoint struct. Hand this to the client
    /// in order to request series of assets through which to route a desired
    /// payment.
    ///
    /// ```
    /// use stellar_client::endpoint::payment;
    /// use stellar_client::resources::{Amount, AssetIdentifier};
    ///
    /// let paths = payment::FindPath::new(
    ///     "source_account",
    ///     "destination_account",
    ///     AssetIdentifier::new(
    ///         "credit_alphanum4",
    ///         Some("code".to_string()),
    ///         Some("issuer".to_string())
    ///     ).unwrap(),
    ///     Amount::new(8675309)
    /// );
    /// ```
    pub fn new(
        source_account: &str,
        destination_account: &str,
        destination_asset: AssetIdentifier,
        destination_amount: Amount,
    ) -> Self {
        Self {
            source_account: source_account.to_string(),
            destination_account: destination_account.to_string(),
            destination_asset,
            destination_amount,
        }
    }
}

impl IntoRequest for FindPath {
    type Response = Records<PaymentPath>;

    fn into_request(self, host: &str) -> Result<Request<Body>> {
        let mut uri = format!(
            "{}/paths?source_account={}&destination_account={}&\
             destination_amount={}&destination_asset_type={}",
            host,
            self.source_account,
            self.destination_account,
            self.destination_amount,
            self.destination_asset.asset_type()
        );
        if !self.destination_asset.is_native() {
            uri.push_str(&format!(
                "&destination_asset_code={}",
                self.destination_asset.asset_code().unwrap()
            ));
            uri.push_str(&format!(
                "&destination_asset_issuer={}",
                self.destination_asset.issuer().to_string()
            ));
        }

        let uri = Uri::from_str(&uri)?;
        let request = Request::get(uri).body(Body::None)?;
        Ok(request)
    }
}

impl TryFromUri for FindPath {
    fn try_from_wrap(wrap: &UriWrap) -> ::std::result::Result<FindPath, uri::Error> {
        let params = wrap.params();
        Ok(FindPath {
            source_account: params.get_parse("from")?,
            destination_account: params.get_parse("to")?,
            destination_asset: params.get_parse("asset")?,
            destination_amount: params.get_parse("amount")?,
        })
    }
}

#[cfg(test)]
mod find_path_tests {
    use super::*;

    #[test]
    fn it_can_make_a_paths_uri_for_native_assets() {
        let paths = FindPath::new(
            "account_a",
            "account_b",
            AssetIdentifier::new("native", None, None).unwrap(),
            Amount::new(1000),
        );
        let request = paths
            .into_request("https://horizon-testnet.stellar.org")
            .unwrap();
        assert_eq!(request.uri().host().unwrap(), "horizon-testnet.stellar.org");
        assert_eq!(request.uri().path(), "/paths");
        assert_eq!(
            request.uri().query(),
            Some(
                "source_account=account_a&destination_account=account_b&\
                 destination_amount=0.0001000&destination_asset_type=native"
            )
        );
    }

    #[test]
    fn it_can_make_a_paths_uri_for_non_native_assets() {
        let paths = FindPath::new(
            "account_a",
            "account_b",
            AssetIdentifier::new(
                "credit_alphanum4",
                Some("codx".to_string()),
                Some("me".to_string()),
            ).unwrap(),
            Amount::new(1000),
        );
        let request = paths
            .into_request("https://horizon-testnet.stellar.org")
            .unwrap();
        assert_eq!(request.uri().host().unwrap(), "horizon-testnet.stellar.org");
        assert_eq!(request.uri().path(), "/paths");
        assert_eq!(
            request.uri().query(),
            Some(
                "source_account=account_a&destination_account=account_b&\
                 destination_amount=0.0001000&destination_asset_type=credit_alphanum4&\
                 destination_asset_code=codx&destination_asset_issuer=me"
            )
        );
    }
}