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
use anyhow::Result;
use chrono::Utc;

use crate::Midgard;
use crate::{api_get_thorname_details, api_get_thorname_owner, api_get_thorname_reverse_lookup, ThornameDetails, ThornameOwner, ThornameReverseLookup};

impl Midgard {
        /// Returns an array of chains and their addresses associated with the given `THORName`.
        /// # Example
        /// ```rust
        /// use midgard_rs::Midgard;
        /// 
        /// # tokio_test::block_on(async {
        /// let mut midgard = Midgard::new();
        /// let name = "thorchain";
        /// 
        /// let thorname_details = midgard.get_thorname_details(&name).await.unwrap();
        /// assert!(!thorname_details.get_owner().is_empty());
        /// # });
        /// ```
        /// 
        /// # Errors
        /// todo
        pub async fn get_thorname_details(&mut self, name: &str) -> Result<ThornameDetails> {
                // Wait for rate limit timer
                self.sleep_until_ok_to_call().await;

                self.set_last_call(Utc::now());
                api_get_thorname_details(self.get_config().get_base_url(), name).await
        }

        /// Returns an array of `THORNames` owned by the address. The address is not necessarily an associated address for those thornames.
        /// # Example
        /// ```rust
        /// use midgard_rs::Midgard;
        /// 
        /// # tokio_test::block_on(async {
        /// let mut midgard = Midgard::new();
        /// let address = "thor18w0hsdru75ug0x4uvamgjn6ghlu43mr4dcypq9";
        /// let thorname_owner = midgard.get_thorname_owner(&address).await.unwrap();
        /// assert!(!thorname_owner.get_thorname_owner().is_empty());
        /// # });
        /// ```
        /// 
        /// # Errors
        /// todo
        pub async fn get_thorname_owner(&mut self, address: &str) -> Result<ThornameOwner> {
                // Wait for rate limit timer
                self.sleep_until_ok_to_call().await;

                self.set_last_call(Utc::now());
                api_get_thorname_owner(self.get_config().get_base_url(), address).await
        }

        /// Returns an array of `THORNames` associated with the given address
        /// # Example
        /// ```rust
        /// use midgard_rs::Midgard;
        /// 
        /// # tokio_test::block_on(async {
        /// let mut midgard = Midgard::new();
        /// let address = "thor18w0hsdru75ug0x4uvamgjn6ghlu43mr4dcypq9";
        /// let thorname_owner = midgard.get_thorname_owner(&address).await.unwrap();
        /// assert!(!thorname_owner.get_thorname_owner().is_empty());
        /// # });
        /// ```
        /// # Errors
        /// todo
        pub async fn get_thorname_reverse_lookup(&mut self, address: &str) -> Result<ThornameReverseLookup> {
                // Wait for rate limit timer
                self.sleep_until_ok_to_call().await;

                self.set_last_call(Utc::now());
                api_get_thorname_reverse_lookup(self.get_config().get_base_url(), address).await
        }
}

#[cfg(test)]
mod tests {
        use rand::prelude::*;
        use serde_json::json;

        use super::*;

        #[tokio::test]
        async fn test_get_thorname_details() {
                let mut midgard = Midgard::new();
                let name = "thorchain";

                let thorname_details = midgard.get_thorname_details(&name).await.unwrap();
                println!("{}", json!(thorname_details));
                assert!(!thorname_details.get_owner().is_empty());
        }

        #[tokio::test]
        async fn test_get_thorname_owner() {
                let mut midgard = Midgard::new();
                let address = "thor18w0hsdru75ug0x4uvamgjn6ghlu43mr4dcypq9";

                let thorname_owner = midgard.get_thorname_owner(&address).await.unwrap();
                println!("{}", json!(thorname_owner));
                assert!(!thorname_owner.get_thorname_owner().is_empty());
        }

        #[tokio::test]
        async fn test_get_thorname_reverse_lookup() {
                let mut midgard = Midgard::new();
                let address = "thor18w0hsdru75ug0x4uvamgjn6ghlu43mr4dcypq9";

                let thorname_reverse_lookup = midgard.get_thorname_reverse_lookup(&address).await.unwrap();
                println!("{}", json!(thorname_reverse_lookup));
                assert!(!thorname_reverse_lookup.get_thorname_reverse_lookup().is_empty());
        }
}