kraken_async_rs/crypto/
nonce_request.rs

1//! Struct for  wrapping arbitrary data with a nonce on serialization
2#[allow(unused)]
3use crate::clients::core_kraken_client::CoreKrakenClient;
4use serde::Serialize;
5
6/// A generic wrapper around any serializable request data that contains the nonce for a request.
7///
8/// Using `#[serde(flatten)]`, this allows any serializable request to have a nonce field added to
9/// its output.
10///
11/// Users are able to, but unlikely to need to use this at all as nonces are generated in the HTTP
12/// methods internal to [`CoreKrakenClient`].
13#[derive(Serialize)]
14pub struct NonceRequest<'r, R>
15where
16    R: Serialize,
17{
18    nonce: u64,
19    #[serde(flatten)]
20    request: &'r R,
21}
22
23impl<'r, R> NonceRequest<'r, R>
24where
25    R: Serialize,
26{
27    pub fn new(nonce: u64, request: &'r R) -> Self {
28        NonceRequest { nonce, request }
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35    use serde::Serialize;
36
37    #[test]
38    fn test_nonce_request() {
39        // an arbitrary T: Serialize request should be flattened into an object containing the nonce
40        //  when serialized
41
42        #[derive(Serialize)]
43        struct Request {
44            id: u64,
45            name: String,
46        }
47
48        let nonce_request = NonceRequest {
49            nonce: 999,
50            request: &Request {
51                name: "SomeRequest".to_string(),
52                id: 123,
53            },
54        };
55
56        assert_eq!(
57            "{\"nonce\":999,\"id\":123,\"name\":\"SomeRequest\"}",
58            serde_json::to_string(&nonce_request).unwrap()
59        );
60    }
61}