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

use crate::{api_get_available_assets_for_pool, api_get_available_lending_assets, api_get_loans};
use crate::{LendingAsset, Loan, Swapkit};

impl Swapkit {
	/// # Errors
	/// Endpoint not returning a response.
	pub async fn get_available_assets_for_pool(&mut self, asset: &str) -> Result<()> {
		// Wait for rate limit timer
		self.sleep_until_ok_to_call().await;

		self.set_last_call(Utc::now());
		api_get_available_assets_for_pool(self.get_config().get_base_url(), self.get_headers(), asset).await
	}

	/// Retrieve a list of all the lending assets the API supports.
	///
	/// # Returns JSON Equivalent
	/// ```json
	/// [
	///     {
	///         "asset": "BTC.BTC",
	///         "assetDepthAssetAmount": "1103.58594672",
	///         "runeDepthAssetAmount": "10841279.17868466",
	///         "loanCr": "200",
	///         "loanCollateral": "1931.73698979",
	///         "lendingAvailable": true,
	///         "filledPercentage": "95.31",
	///         "derivedDepthPercentage": "9497",
	///         "ltvPercentage": "50.00"
	///     }
	/// ]
	/// ```
	///
	/// # Example
	///
	/// ```rust
	/// use swapkit_rs::Swapkit;
	/// use dotenv;
	/// use swapkit_rs::Configuration;
	///
	/// # tokio_test::block_on(async {
	/// let swapkit_config = Configuration::new(None, dotenv::var("SWAPKIT_REFERER").unwrap().as_str(), dotenv::var("SWAPKIT_X_API_KEY").unwrap().as_str());
	/// let mut swapkit = Swapkit::new(swapkit_config);
	///
	/// let lending_assets = swapkit.get_available_lending_assets().await.unwrap();
	///
	/// assert_ne!(lending_assets.len(), 0);
	/// # });
	/// ```
	///
	/// # Errors
	/// todo
	pub async fn get_available_lending_assets(&mut self) -> Result<Vec<LendingAsset>> {
		// Wait for rate limit timer
		self.sleep_until_ok_to_call().await;

		self.set_last_call(Utc::now());
		api_get_available_lending_assets(self.get_config().get_base_url(), self.get_headers()).await
	}

	/// Retrieve a loan for a given address and asset.
	///
	/// # Returns JSON Equivalent
	///
	/// ```json
	/// {
	///     "asset": "BTC.BTC",
	///     "debtCurrent": "1764.0462",
	///     "debtIssued": "1764.0462",
	///     "debtRepaid": "0",
	///     "collateralCurrent": "0.06489696",
	///     "collateralDeposited": "0.06489696",
	///     "collateralWithdrawn": "0",
	///     "lastOpenHeight": 14876835,
	///     "lastRepayHeight": 0,
	///     "ltvPercentage": "39.53",
	///     "owner": "bc1qzafz3f0h90u7n9j862uupaf5hpeydmhvpnzwzz"    
	/// }
	/// ```
	///
	/// # Example
	///
	/// ```rust
	/// use swapkit_rs::Swapkit;
	/// use dotenv;
	/// use swapkit_rs::Configuration;
	///
	/// # tokio_test::block_on(async {
	/// let swapkit_config = Configuration::new(None, dotenv::var("SWAPKIT_REFERER").unwrap().as_str(), dotenv::var("SWAPKIT_X_API_KEY").unwrap().as_str());
	/// let mut swapkit = Swapkit::new(swapkit_config);
	///
	/// let loan = swapkit.get_loans("bc1qzafz3f0h90u7n9j862uupaf5hpeydmhvpnzwzz", "BTC.BTC").await.unwrap();
	///
	/// assert_eq!(loan.get_asset(), "BTC.BTC".to_string());
	/// # });
	/// ```
	///
	/// # Errors
	/// todo
	pub async fn get_loans(&mut self, address: &str, asset: &str) -> Result<Loan> {
		// Wait for rate limit timer
		self.sleep_until_ok_to_call().await;

		self.set_last_call(Utc::now());
		api_get_loans(self.get_config().get_base_url(), self.get_headers(), address, asset).await
	}
}