koios_sdk/api/
script.rs

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
use crate::{
    error::Result,
    models::{
        requests::ScriptHashesRequest,
        script::{ScriptInfo, ScriptList, ScriptRedeemer},
        UtxoInfo,
    },
    types::{Extended, ScriptHash},
    Client,
};

impl Client {
    /// Get list of script information for given script hashes
    ///
    /// # Arguments
    ///
    /// * `script_hashes` - List of script hashes to query
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use koios_sdk::Client;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = Client::new()?;
    ///     let script_hashes = vec![
    ///         "67f33146617a5e61936081db3b2117cbf59bd2123748f58ac9678656".to_string()
    ///     ];
    ///     let info = client.get_script_info(&script_hashes).await?;
    ///     println!("Script info: {:?}", info);
    ///     Ok(())
    /// }
    /// ```
    pub async fn get_script_info(&self, script_hashes: &[String]) -> Result<Vec<ScriptInfo>> {
        let request = ScriptHashesRequest::new(script_hashes.to_vec());
        self.post("/script_info", &request).await
    }

    /// Get list of all existing native script hashes along with their creation transaction hashes
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use koios_sdk::Client;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = Client::new()?;
    ///     let scripts = client.get_native_script_list().await?;
    ///     println!("Native scripts: {:?}", scripts);
    ///     Ok(())
    /// }
    /// ```
    pub async fn get_native_script_list(&self) -> Result<Vec<ScriptList>> {
        self.get("/native_script_list").await
    }

    /// Get list of all existing Plutus script hashes along with their creation transaction hashes
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use koios_sdk::Client;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = Client::new()?;
    ///     let scripts = client.get_plutus_script_list().await?;
    ///     println!("Plutus scripts: {:?}", scripts);
    ///     Ok(())
    /// }
    /// ```
    pub async fn get_plutus_script_list(&self) -> Result<Vec<ScriptList>> {
        self.get("/plutus_script_list").await
    }

    /// Get list of all redeemers for a given script hash
    ///
    /// # Arguments
    ///
    /// * `script_hash` - Script hash to query
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use koios_sdk::Client;
    /// use koios_sdk::types::ScriptHash;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = Client::new()?;
    ///     let script_hash = ScriptHash::new(
    ///         "67f33146617a5e61936081db3b2117cbf59bd2123748f58ac9678656"
    ///     );
    ///     let redeemers = client.get_script_redeemers(&script_hash).await?;
    ///     println!("Script redeemers: {:?}", redeemers);
    ///     Ok(())
    /// }
    /// ```
    pub async fn get_script_redeemers(
        &self,
        script_hash: &ScriptHash,
    ) -> Result<Vec<ScriptRedeemer>> {
        self.get(&format!(
            "/script_redeemers?_script_hash={}",
            script_hash.value()
        ))
        .await
    }

    /// Get list of all UTXOs for a given script hash
    ///
    /// # Arguments
    ///
    /// * `script_hash` - Script hash to query
    /// * `extended` - Optional flag to include extended information
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use koios_sdk::Client;
    /// use koios_sdk::types::{ScriptHash, Extended};
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = Client::new()?;
    ///     let script_hash = ScriptHash::new(
    ///         "67f33146617a5e61936081db3b2117cbf59bd2123748f58ac9678656"
    ///     );
    ///     let utxos = client.get_script_utxos(&script_hash, Some(Extended::new(true))).await?;
    ///     println!("Script UTXOs: {:?}", utxos);
    ///     Ok(())
    /// }
    /// ```
    pub async fn get_script_utxos(
        &self,
        script_hash: &ScriptHash,
        extended: Option<Extended>,
    ) -> Result<Vec<UtxoInfo>> {
        let mut endpoint = format!("/script_utxos?_script_hash={}", script_hash.value());

        if let Some(ext) = extended {
            endpoint.push_str(&format!("&_extended={}", ext.value()));
        }

        self.get(&endpoint).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    use wiremock::matchers::{method, path, query_param};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    #[tokio::test]
    async fn test_get_plutus_script_list() {
        let mock_server = MockServer::start().await;
        let client = Client::builder()
            .base_url(mock_server.uri())
            .build()
            .unwrap();

        let mock_response = json!([{
            "script_hash": "67f33146617a5e61936081db3b2117cbf59bd2123748f58ac9678656",
            "creation_tx_hash": "6ed09ba58a56c6e946668038ba4d3cef8eb97a20cbf76c5970e1402e8a8d6541",
            "type": "plutusv1",
            "size": 42
        }]);

        Mock::given(method("GET"))
            .and(path("/plutus_script_list"))
            .respond_with(ResponseTemplate::new(200).set_body_json(&mock_response))
            .mount(&mock_server)
            .await;

        let response = client.get_plutus_script_list().await.unwrap();
        assert_eq!(response.len(), 1);
        assert_eq!(
            response[0].script_hash,
            "67f33146617a5e61936081db3b2117cbf59bd2123748f58ac9678656"
        );
    }

    #[tokio::test]
    async fn test_get_script_redeemers() {
        let mock_server = MockServer::start().await;
        let client = Client::builder()
            .base_url(mock_server.uri())
            .build()
            .unwrap();

        let script_hash = "67f33146617a5e61936081db3b2117cbf59bd2123748f58ac9678656";
        let mock_response = json!([{
            "script_hash": script_hash,
            "redeemers": [{
                "tx_hash": "6ed09ba58a56c6e946668038ba4d3cef8eb97a20cbf76c5970e1402e8a8d6541",
                "tx_index": 0,
                "unit_mem": 1000,
                "unit_steps": 500,
                "fee": "500000",
                "purpose": "spend",
                "datum_hash": "ab01cd23",
                "datum_value": null
            }]
        }]);

        Mock::given(method("GET"))
            .and(path("/script_redeemers"))
            .and(query_param("_script_hash", script_hash))
            .respond_with(ResponseTemplate::new(200).set_body_json(&mock_response))
            .mount(&mock_server)
            .await;

        let response = client
            .get_script_redeemers(&ScriptHash::new(script_hash))
            .await
            .unwrap();
        assert_eq!(response.len(), 1);
        assert_eq!(response[0].script_hash, script_hash);
    }

    // Add more tests for other endpoints...
}