casper_rust_wasm_sdk/sdk/rpcs/
get_auction_info.rs1#[cfg(all(feature = "js", target_arch = "wasm32"))]
2use crate::types::identifier::block_identifier::BlockIdentifier;
3use crate::{
4 types::{
5 identifier::block_identifier::BlockIdentifierInput, sdk_error::SdkError,
6 verbosity::Verbosity,
7 },
8 SDK,
9};
10use casper_client::{
11 cli::get_auction_info as get_auction_info_cli, get_auction_info as get_auction_info_lib,
12 rpcs::results::GetAuctionInfoResult as _GetAuctionInfoResult, JsonRpcId, SuccessResponse,
13};
14#[cfg(all(feature = "js", target_arch = "wasm32"))]
15use gloo_utils::format::JsValueSerdeExt;
16use rand::RngExt;
17#[cfg(all(feature = "js", target_arch = "wasm32"))]
18use serde::{Deserialize, Serialize};
19#[cfg(all(feature = "js", target_arch = "wasm32"))]
20use wasm_bindgen::prelude::*;
21
22#[cfg(all(feature = "js", target_arch = "wasm32"))]
24#[derive(Debug, Deserialize, Clone, Serialize)]
25#[cfg_attr(feature = "js", wasm_bindgen)]
26pub struct GetAuctionInfoResult(_GetAuctionInfoResult);
27
28#[cfg(all(feature = "js", target_arch = "wasm32"))]
29impl From<GetAuctionInfoResult> for _GetAuctionInfoResult {
30 fn from(result: GetAuctionInfoResult) -> Self {
31 result.0
32 }
33}
34
35#[cfg(all(feature = "js", target_arch = "wasm32"))]
36impl From<_GetAuctionInfoResult> for GetAuctionInfoResult {
37 fn from(result: _GetAuctionInfoResult) -> Self {
38 GetAuctionInfoResult(result)
39 }
40}
41
42#[cfg(all(feature = "js", target_arch = "wasm32"))]
43#[cfg_attr(feature = "js", wasm_bindgen)]
44impl GetAuctionInfoResult {
45 #[cfg_attr(feature = "js", wasm_bindgen(getter))]
47 #[cfg(feature = "js")]
48 pub fn api_version(&self) -> JsValue {
49 JsValue::from_serde(&self.0.api_version).unwrap()
50 }
51
52 #[cfg_attr(feature = "js", wasm_bindgen(getter))]
54 #[cfg(feature = "js")]
55 pub fn auction_state(&self) -> JsValue {
56 JsValue::from_serde(&self.0.auction_state).unwrap()
57 }
58
59 #[cfg_attr(feature = "js", wasm_bindgen(js_name = "toJson"))]
61 #[cfg(feature = "js")]
62 pub fn to_json(&self) -> JsValue {
63 JsValue::from_serde(&self.0).unwrap_or(JsValue::null())
64 }
65}
66
67#[derive(Debug, Deserialize, Clone, Default, Serialize)]
69#[cfg(all(feature = "js", target_arch = "wasm32"))]
70#[cfg_attr(
71 feature = "js",
72 wasm_bindgen(js_name = "getAuctionInfoOptions", getter_with_clone)
73)]
74pub struct GetAuctionInfoOptions {
75 pub maybe_block_id_as_string: Option<String>,
76 pub maybe_block_identifier: Option<BlockIdentifier>,
77 pub rpc_address: Option<String>,
78 pub verbosity: Option<Verbosity>,
79}
80
81#[cfg(all(feature = "js", target_arch = "wasm32"))]
82#[cfg_attr(feature = "js", wasm_bindgen)]
83impl SDK {
84 #[cfg(feature = "js")]
95 pub fn get_auction_info_options(
96 &self,
97 options: JsValue,
98 ) -> Result<GetAuctionInfoOptions, JsError> {
99 options
100 .into_serde::<GetAuctionInfoOptions>()
101 .map_err(|err| JsError::new(&format!("Error deserializing options: {err:?}")))
102 }
103
104 #[cfg_attr(feature = "js", wasm_bindgen(js_name = "get_auction_info"))]
118 #[cfg(feature = "js")]
119 pub async fn get_auction_info_js_alias(
120 &self,
121 options: Option<GetAuctionInfoOptions>,
122 ) -> Result<GetAuctionInfoResult, JsError> {
123 let GetAuctionInfoOptions {
124 maybe_block_id_as_string,
125 maybe_block_identifier,
126 verbosity,
127 rpc_address,
128 } = options.unwrap_or_default();
129
130 let maybe_block_identifier = if let Some(maybe_block_identifier) = maybe_block_identifier {
131 Some(BlockIdentifierInput::BlockIdentifier(
132 maybe_block_identifier,
133 ))
134 } else {
135 maybe_block_id_as_string.map(BlockIdentifierInput::String)
136 };
137
138 let result = self
139 .get_auction_info(maybe_block_identifier, verbosity, rpc_address)
140 .await;
141 match result {
142 Ok(data) => Ok(data.result.into()),
143 Err(err) => {
144 let err = &format!("Error occurred with {err:?}");
145 Err(JsError::new(err))
146 }
147 }
148 }
149
150 #[cfg_attr(
152 feature = "js",
153 wasm_bindgen(js_name = "state_get_auction_info_js_alias")
154 )]
155 #[cfg(feature = "js")]
156 pub async fn state_get_auction_info(
157 &self,
158 options: Option<GetAuctionInfoOptions>,
159 ) -> Result<GetAuctionInfoResult, JsError> {
160 self.get_auction_info_js_alias(options).await
161 }
162}
163
164impl SDK {
165 pub async fn get_auction_info(
181 &self,
182 maybe_block_identifier: Option<BlockIdentifierInput>,
183 verbosity: Option<Verbosity>,
184 rpc_address: Option<String>,
185 ) -> Result<SuccessResponse<_GetAuctionInfoResult>, SdkError> {
186 let random_id = rand::rng().random::<u64>().to_string();
188 if let Some(BlockIdentifierInput::String(maybe_block_id)) = maybe_block_identifier {
189 get_auction_info_cli(
190 &random_id,
191 &self.get_rpc_address(rpc_address),
192 self.get_verbosity(verbosity).into(),
193 &maybe_block_id,
194 )
195 .await
196 .map_err(SdkError::from)
197 } else {
198 let maybe_block_identifier =
199 if let Some(BlockIdentifierInput::BlockIdentifier(maybe_block_identifier)) =
200 maybe_block_identifier
201 {
202 Some(maybe_block_identifier)
203 } else {
204 None
205 };
206 get_auction_info_lib(
207 JsonRpcId::from(random_id),
208 &self.get_rpc_address(rpc_address),
209 self.get_verbosity(verbosity).into(),
210 maybe_block_identifier.map(Into::into),
211 )
212 .await
213 .map_err(SdkError::from)
214 }
215 }
216}
217
218#[cfg(test)]
219mod tests {
220 use super::*;
221 use crate::types::{
222 hash::block_hash::BlockHash, identifier::block_identifier::BlockIdentifier,
223 };
224 use sdk_tests::tests::helpers::get_network_constants;
225
226 #[tokio::test]
227 async fn test_get_auction_info_with_none_values() {
228 let sdk = SDK::new(None, None, None);
230 let error_message = "failed to parse node address as valid URL";
231
232 let result = sdk.get_auction_info(None, None, None).await;
234
235 assert!(result.is_err());
237 let err_string = result.err().unwrap().to_string();
238 assert!(err_string.contains(error_message));
239 }
240
241 #[ignore = "currently failing on node"]
242 #[tokio::test]
243 async fn test_get_auction_info_with_block_id_string() {
244 let sdk = SDK::new(None, None, None);
246 let verbosity = Some(Verbosity::High);
247 let (rpc_address, _, _, _, _) = get_network_constants();
248 let result = sdk
249 .get_block(None, verbosity, Some(rpc_address.clone()))
250 .await;
251 let block_hash = BlockHash::from(
252 *result
253 .unwrap()
254 .result
255 .block_with_signatures
256 .unwrap()
257 .block
258 .hash(),
259 )
260 .to_string();
261 let block_identifier = BlockIdentifierInput::String(block_hash.to_string());
262
263 let result = sdk
265 .get_auction_info(Some(block_identifier), verbosity, Some(rpc_address))
266 .await;
267
268 assert!(result.is_ok());
270 }
271
272 #[ignore = "currently failing on node"]
273 #[tokio::test]
274 async fn test_get_auction_info_with_block_identifier() {
275 let sdk = SDK::new(None, None, None);
277 let block_identifier =
278 BlockIdentifierInput::BlockIdentifier(BlockIdentifier::from_height(1));
279 let verbosity = Some(Verbosity::High);
280 let (rpc_address, _, _, _, _) = get_network_constants();
281
282 let result = sdk
284 .get_auction_info(Some(block_identifier), verbosity, Some(rpc_address))
285 .await;
286
287 assert!(result.is_ok());
289 }
290
291 #[tokio::test]
292 async fn test_get_auction_info_with_error() {
293 let sdk = SDK::new(Some("http://localhost".to_string()), None, None);
294
295 let error_message = "error sending request";
296
297 let result = sdk.get_auction_info(None, None, None).await;
299
300 assert!(result.is_err());
302 let err_string = result.err().unwrap().to_string();
303 assert!(err_string.contains(error_message));
304 }
305}