forked from Txio-labs/txio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsui.rs
More file actions
128 lines (111 loc) · 4.14 KB
/
Copy pathsui.rs
File metadata and controls
128 lines (111 loc) · 4.14 KB
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
use crate::chains::traits::ChainAdapter;
use crate::cli::parser::Network;
use async_trait::async_trait;
use serde_json::{json, Value};
use anyhow::{Result, anyhow};
use reqwest::Client;
use regex::Regex;
pub struct SuiAdapter {
client: Client,
rpc_url: String,
}
impl SuiAdapter {
pub fn new() -> Self {
Self::with_rpc(None, Network::Mainnet)
}
pub fn with_rpc(rpc_url: Option<String>, network: Network) -> Self {
let url = rpc_url.unwrap_or_else(|| match network {
Network::Mainnet => "https://fullnode.mainnet.sui.io".to_string(),
Network::Testnet => "https://fullnode.testnet.sui.io".to_string(),
Network::Devnet => "https://fullnode.devnet.sui.io".to_string(),
Network::Localnet => "http://127.0.0.1:9000".to_string(),
});
Self {
client: Client::new(),
rpc_url: url,
}
}
async fn call_rpc_internal(&self, method: &str, params: Value) -> Result<Value> {
let payload = json!({
"jsonrpc": "2.0",
"id": 1,
"method": method,
"params": params
});
let response = self.client.post(&self.rpc_url)
.json(&payload)
.send()
.await?;
let body: Value = response.json().await?;
if let Some(error) = body.get("error") {
let msg = error.get("message").and_then(|m| m.as_str()).unwrap_or("Unknown RPC Error");
let data = error.get("data").and_then(|d| d.as_str()).unwrap_or("");
let code = error.get("code").and_then(|c| c.as_i64()).unwrap_or(0);
let err_str = if data.is_empty() {
format!("{} (Code: {})", msg, code)
} else {
format!("{} - {} (Code: {})", msg, data, code)
};
return Err(anyhow!("{}", err_str));
}
Ok(body.get("result").cloned().unwrap_or(Value::Null))
}
async fn resolve_names_in_value(&self, value: &mut Value) -> Result<()> {
let suins_regex = Regex::new(r"([a-zA-Z0-9-]+\.sui)")?;
match value {
Value::String(s) => {
if suins_regex.is_match(s) {
let mut new_string = s.to_string();
let matches: Vec<String> = suins_regex.find_iter(s)
.map(|m| m.as_str().to_string())
.collect();
for name in matches {
if let Ok(Some(addr)) = self.resolve_name(&name).await {
new_string = new_string.replace(&name, &addr);
}
}
*s = new_string;
}
}
Value::Array(arr) => {
for v in arr.iter_mut() {
let _ = Box::pin(self.resolve_names_in_value(v)).await;
}
}
Value::Object(map) => {
for v in map.values_mut() {
let _ = Box::pin(self.resolve_names_in_value(v)).await;
}
}
_ => {}
}
Ok(())
}
}
#[async_trait]
impl ChainAdapter for SuiAdapter {
fn name(&self) -> &'static str {
"Sui"
}
fn default_rpc(&self) -> &'static str {
"https://fullnode.mainnet.sui.io"
}
async fn call_rpc(&self, method: &str, mut params: Value) -> Result<Value> {
// Resolve names in the parameters before making the call
self.resolve_names_in_value(&mut params).await?;
self.call_rpc_internal(method, params).await
}
async fn resolve_name(&self, name: &str) -> Result<Option<String>> {
if !name.ends_with(".sui") {
return Ok(None);
}
let params = json!([name]);
// Call internal RPC to prevent infinite recursion
let result = self.call_rpc_internal("suix_resolveNameServiceAddress", params).await?;
Ok(result.as_str().map(|s| s.to_string()))
}
async fn get_balance(&self, address: &str) -> Result<Value> {
let params = json!([address]);
self.call_rpc("suix_getAllBalances", params).await
}
}