11//! The `NodeInterface` struct is defined which allows for interacting with an Ergo Node via Rust.
22
3- use std :: convert :: TryInto ;
3+ use crate :: { BlockHeight , NanoErg , P2PKAddressString , P2SAddressString } ;
44use ergo_lib:: chain:: ergo_state_context:: { ErgoStateContext , Headers } ;
55use ergo_lib:: ergo_chain_types:: { Header , PreHeader } ;
6- use crate :: { BlockHeight , NanoErg , P2PKAddressString , P2SAddressString } ;
76use ergo_lib:: ergotree_ir:: chain:: ergo_box:: ErgoBox ;
87use ergo_lib:: ergotree_ir:: chain:: token:: TokenId ;
98use reqwest:: Url ;
109use serde_json:: from_str;
10+ use std:: convert:: TryInto ;
1111use thiserror:: Error ;
1212
1313pub type Result < T > = std:: result:: Result < T , NodeError > ;
@@ -88,13 +88,17 @@ impl NodeInterface {
8888 }
8989
9090 /// Acquires unspent boxes from the blockchain by specific address
91- pub fn unspent_boxes_by_address ( & self , address : & P2PKAddressString , offset : u64 , limit : u64 ) -> Result < Vec < ErgoBox > > {
91+ pub fn unspent_boxes_by_address (
92+ & self ,
93+ address : & P2PKAddressString ,
94+ offset : u64 ,
95+ limit : u64 ,
96+ ) -> Result < Vec < ErgoBox > > {
9297 let endpoint = format ! (
9398 "/blockchain/box/unspent/byAddress?offset={}&limit={}" ,
94- offset,
95- limit
99+ offset, limit
96100 ) ;
97- let res = self . send_post_req ( & endpoint, address. clone ( ) ) ;
101+ let res = self . send_post_req ( endpoint. as_str ( ) , address. clone ( ) ) ;
98102 let res_json = self . parse_response_to_json ( res) ?;
99103
100104 let mut box_list = vec ! [ ] ;
@@ -114,15 +118,18 @@ impl NodeInterface {
114118 }
115119
116120 /// Acquires unspent boxes from the blockchain by specific token_id
117- pub fn unspent_boxes_by_token_id ( & self , token_id : & TokenId , offset : u64 , limit : u64 ) -> Result < Vec < ErgoBox > > {
118- let id: String = token_id. clone ( ) . into ( ) ;
121+ pub fn unspent_boxes_by_token_id (
122+ & self ,
123+ token_id : & TokenId ,
124+ offset : u64 ,
125+ limit : u64 ,
126+ ) -> Result < Vec < ErgoBox > > {
127+ let id: String = ( * token_id) . into ( ) ;
119128 let endpoint = format ! (
120129 "/blockchain/box/unspent/byTokenId/{}?offset={}&limit={}" ,
121- id,
122- offset,
123- limit
130+ id, offset, limit
124131 ) ;
125- let res = self . send_get_req ( & endpoint) ;
132+ let res = self . send_get_req ( endpoint. as_str ( ) ) ;
126133 let res_json = self . parse_response_to_json ( res) ?;
127134
128135 let mut box_list = vec ! [ ] ;
@@ -144,7 +151,7 @@ impl NodeInterface {
144151 /// Get the current nanoErgs balance held in the `address`
145152 pub fn nano_ergs_balance ( & self , address : & P2PKAddressString ) -> Result < NanoErg > {
146153 let endpoint = "/blockchain/balance" ;
147- let res = self . send_post_req ( & endpoint, address. clone ( ) ) ;
154+ let res = self . send_post_req ( endpoint, address. clone ( ) ) ;
148155 let res_json = self . parse_response_to_json ( res) ?;
149156
150157 let balance = res_json[ "confirmed" ] [ "nanoErgs" ] . clone ( ) ;
@@ -273,19 +280,16 @@ impl NodeInterface {
273280 vec_headers. reverse ( ) ;
274281 let ten_headers: [ Header ; 10 ] = vec_headers. try_into ( ) . unwrap ( ) ;
275282 let headers = Headers :: from ( ten_headers) ;
276- let pre_header = PreHeader :: from ( headers. get ( 0 ) . unwrap ( ) . clone ( ) ) ;
283+ let pre_header = PreHeader :: from ( headers. first ( ) . unwrap ( ) . clone ( ) ) ;
277284 let state_context = ErgoStateContext :: new ( pre_header, headers) ;
278285
279286 Ok ( state_context)
280287 }
281288
282289 /// Get the last `number` of block headers from the blockchain
283290 pub fn get_last_block_headers ( & self , number : u32 ) -> Result < Vec < Header > > {
284- let endpoint = format ! (
285- "/blocks/lastHeaders/{}" ,
286- number
287- ) ;
288- let res = self . send_get_req ( & endpoint) ;
291+ let endpoint = format ! ( "/blocks/lastHeaders/{}" , number) ;
292+ let res = self . send_get_req ( endpoint. as_str ( ) ) ;
289293 let res_json = self . parse_response_to_json ( res) ?;
290294
291295 let mut headers: Vec < Header > = vec ! [ ] ;
@@ -315,8 +319,12 @@ impl NodeInterface {
315319 } ) ;
316320 }
317321
318- let full_height = res_json[ "fullHeight" ] . as_u64 ( ) . ok_or ( NodeError :: FailedParsingNodeResponse ( res_json. to_string ( ) ) ) ?;
319- let indexed_height = res_json[ "indexedHeight" ] . as_u64 ( ) . ok_or ( NodeError :: FailedParsingNodeResponse ( res_json. to_string ( ) ) ) ?;
322+ let full_height = res_json[ "fullHeight" ]
323+ . as_u64 ( )
324+ . ok_or ( NodeError :: FailedParsingNodeResponse ( res_json. to_string ( ) ) ) ?;
325+ let indexed_height = res_json[ "indexedHeight" ]
326+ . as_u64 ( )
327+ . ok_or ( NodeError :: FailedParsingNodeResponse ( res_json. to_string ( ) ) ) ?;
320328
321329 let is_sync = full_height. abs_diff ( indexed_height) < 10 ;
322330 Ok ( IndexerStatus {
0 commit comments