1use std::error::Error;
2use serde::{Deserialize, Serialize};
3use serde_json::{Value};
4use crate::transaction_builder::TransactionBuilder;
5use crate::wallet::Wallet;
6
7#[derive(Default, Serialize, Deserialize)]
8pub struct IconService {
9 pub(crate) icon_service_url: String,
10}
11
12impl IconService {
13 pub fn new(icon_service_url: Option<String>) -> Self {
14 Self {
15 icon_service_url: icon_service_url
16 .unwrap_or_else(|| "https://api.icon.community/api/v3".to_string())
17 }
18 }
19
20 pub async fn get_last_block(&self) -> Result<Value, Box<dyn Error>> {
21 let transaction = TransactionBuilder::new(self)
22 .method("icx_getLastBlock")
23 .build();
24
25 let response: Value = transaction.send().await.map_err(|e| Box::new(e) as Box<dyn Error>)?;
26
27 Ok(response)
28 }
29
30 pub async fn get_block_by_height(&self, block_height: &str) -> Result<Value, Box<dyn Error>> {
31 let transaction = TransactionBuilder::new(self)
32 .method("icx_getBlockByHeight")
33 .block_height(block_height)
34 .build();
35
36 let response: Value = transaction.send().await.map_err(|e| Box::new(e) as Box<dyn Error>)?;
37
38 Ok(response)
39 }
40
41 pub async fn get_block_by_hash(&self, block_hash: &str) -> Result<Value, Box<dyn Error>> {
42 let transaction = TransactionBuilder::new(self)
43 .method("icx_getBlockByHash")
44 .block_hash(block_hash)
45 .build();
46
47 let response: Value = transaction.send().await.map_err(|e| Box::new(e) as Box<dyn Error>)?;
48
49 Ok(response)
50 }
51
52 pub async fn get_balance(&self, address: &str) -> Result<Value, Box<dyn Error>> {
53 let transaction = TransactionBuilder::new(self)
54 .method("icx_getBalance")
55 .address(address)
56 .build();
57
58 let response: Value = transaction.send().await.map_err(|e| Box::new(e) as Box<dyn Error>)?;
59
60 Ok(response)
61 }
62
63 pub async fn get_transaction_result(&self, tx_hash: &str) -> Result<Value, Box<dyn Error>> {
64 let transaction = TransactionBuilder::new(self)
65 .method("icx_getTransactionResult")
66 .tx_hash(tx_hash)
67 .build();
68
69 let response: Value = transaction.send().await.map_err(|e| Box::new(e) as Box<dyn Error>)?;
70
71 Ok(response)
72 }
73
74 pub async fn get_transaction_by_hash(&self, tx_hash: &str) -> Result<Value, Box<dyn Error>> {
75 let transaction = TransactionBuilder::new(self)
76 .method("icx_getTransactionByHash")
77 .tx_hash(tx_hash)
78 .build();
79
80 let response: Value = transaction.send().await.map_err(|e| Box::new(e) as Box<dyn Error>)?;
81
82 Ok(response)
83 }
84
85 pub async fn call(&self, score: &str, params: Value) -> Result<Value, Box<dyn Error>> {
86 let transaction = TransactionBuilder::new(self)
87 .method("icx_call")
88 .to(score)
89 .call(params)
90 .build();
91
92 let response: Value = transaction.send().await.map_err(|e| Box::new(e) as Box<dyn Error>)?;
93
94 Ok(response)
95 }
96
97 pub async fn send_transaction(&self, wallet: Wallet, to: &str, value: &str, version: &str, nid: &str, nonce: &str, step_limit: &str) -> Result<Value, Box<dyn Error>> {
98 let transaction = TransactionBuilder::new(self)
99 .method("icx_sendTransaction")
100 .from(wallet.get_public_address().as_str())
101 .to(to)
102 .value(value)
103 .version(version)
104 .nid(nid)
105 .timestamp()
106 .nonce(nonce)
107 .step_limit(step_limit)
108 .sign(wallet.get_private_key().as_str())
109 .build();
110
111 let response: Value = transaction.send().await.map_err(|e| Box::new(e) as Box<dyn Error>)?;
112
113 Ok(response)
114 }
115
116 pub async fn send_transaction_with_message(&self, wallet: Wallet, to: &str, value: &str, version: &str, nid: &str, nonce: &str, step_limit: &str, message: &str) -> Result<Value, Box<dyn Error>> {
117 let transaction = TransactionBuilder::new(self)
118 .method("icx_sendTransaction")
119 .from(wallet.get_public_address().as_str())
120 .to(to)
121 .value(value)
122 .version(version)
123 .nid(nid)
124 .timestamp()
125 .nonce(nonce)
126 .step_limit(step_limit)
127 .message(message)
128 .sign(wallet.get_private_key().as_str())
129 .build();
130
131 let response: Value = transaction.send().await.map_err(|e| Box::new(e) as Box<dyn Error>)?;
132
133 Ok(response)
134 }
135}