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

use crate::Midgard;
use crate::{api_get_action_list, ActionList, GetActionList};

impl Midgard {
	/// List actions along with their related transactions. An action is generated by one or more inbound transactions with the intended action set in the transaction memo. The action may result in one or more outbound transactions. Results are paginated by sets of 50. Filters may be applied to query actions.
	///
	/// # Example
	///
	/// ```rust
	/// use midgard_rs::GetActionList;
	/// use midgard_rs::Midgard;
	///
	/// # tokio_test::block_on(async {
	/// let mut midgard = Midgard::new();
	///
	/// let params = GetActionList::new(vec!["BTC.BTC".to_string()], 10);
	/// let actions = midgard.get_actions(params).await.unwrap();
	///
	/// assert!(!actions.get_actions().get_actions().is_empty());
	/// # });
	/// ```
	///
	/// To get paginated responses, you can pass the `next_page_token` from the previous response to the next request.
	/// ```rust
	/// use midgard_rs::GetActionList;
	/// use midgard_rs::Midgard;
	///
	/// # tokio_test::block_on(async {
	/// let mut midgard = Midgard::new();
	///
	/// let mut params = GetActionList::new(vec!["BTC.BTC".to_string()], 10);
	/// let actions = midgard.get_actions(params.clone()).await.unwrap();
	///
	/// assert!(!actions.get_actions().get_actions().is_empty());
	///
	///
	/// let next_page_token = actions.get_meta().get_next_page_token();
	/// if let Some(next_page_token) = next_page_token {
	///         params.set_next_page_token(next_page_token);
	///         let next_actions = midgard.get_actions(params).await.unwrap();
	///         assert!(!next_actions.get_actions().get_actions().is_empty());
	/// }
	/// # });
	/// ```
        /// 
        /// # Errors
        /// todo
	pub async fn get_actions(&mut self, params: GetActionList) -> Result<ActionList> {
		// Wait for rate limit timer
		self.sleep_until_ok_to_call().await;

		self.set_last_call(Utc::now());
		api_get_action_list(self.get_config().get_base_url(), params).await
	}
}

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

	use super::*;
	use crate::api_get_pool_list;

	#[tokio::test]
	async fn test_get_actions() {
		let mut midgard = Midgard::new();

		let params = GetActionList::new(vec!["BTC.BTC".to_string()], 10);
		let actions = midgard.get_actions(params).await.unwrap();

		println!("actions: {}", json!(actions));

		assert!(!actions.get_actions().get_actions().is_empty());
	}

	#[tokio::test]
	async fn test_get_actions_pagination() {
		let mut midgard = Midgard::new();

		let pool_list = midgard.get_pool_list(None, None).await.unwrap();
		let random_usize = thread_rng().gen_range(0..pool_list.get_assets().len());
		let pool = pool_list.get_assets()[random_usize].clone();
		println!("pool: {}", &pool);

		let mut params = GetActionList::new(vec![pool.clone()], 5);
		let actions = midgard.get_actions(params.clone()).await.unwrap();
		assert!(!actions.get_actions().get_actions().is_empty());

		println!("actions: {}", json!(actions));

		let next_page_token = actions.get_meta().get_next_page_token();
		if let Some(next_page_token) = next_page_token {
			params.set_next_page_token(next_page_token);
			let next_actions = midgard.get_actions(params).await.unwrap();

			println!("next actions: {}", json!(next_actions));

			assert!(!next_actions.get_actions().get_actions().is_empty());
		}
	}
}