forked from Txio-labs/txio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolana.rs
More file actions
108 lines (93 loc) · 3.04 KB
/
Copy pathsolana.rs
File metadata and controls
108 lines (93 loc) · 3.04 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
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;
pub struct SolanaAdapter {
client: Client,
rpc_url: String,
}
impl SolanaAdapter {
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://api.mainnet-beta.solana.com".to_string(),
Network::Testnet => "https://api.testnet.solana.com".to_string(),
Network::Devnet => "https://api.devnet.solana.com".to_string(),
Network::Localnet => "http://127.0.0.1:8899".to_string(),
});
Self {
client: Client::new(),
rpc_url: url,
}
}
}
#[async_trait]
impl ChainAdapter for SolanaAdapter {
fn name(&self) -> &'static str {
"Solana"
}
fn default_rpc(&self) -> &'static str {
"https://api.mainnet-beta.solana.com"
}
async fn call_rpc(&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?;
Ok(body.get("result").cloned().unwrap_or(Value::Null))
}
async fn get_balance(&self, address: &str) -> Result<Value> {
let params = json!([address]);
self.call_rpc("getBalance", params).await
}
async fn get_transaction(&self, hash: &str) -> Result<Value> {
self.call_rpc("getTransaction", json!([
hash,
{ "encoding": "jsonParsed", "maxSupportedTransactionVersion": 0 }
])).await
}
async fn get_block(&self, block: Option<u64>) -> Result<Value> {
let slot = match block {
Some(n) => n,
None => {
let val = self.call_rpc("getSlot", json!([])).await?;
val.as_u64().unwrap_or(0)
}
};
self.call_rpc("getBlock", json!([
slot,
{
"encoding": "jsonParsed",
"maxSupportedTransactionVersion": 0,
"transactionDetails": "full",
"rewards": false
}
])).await
}
async fn get_gas_price(&self) -> Result<Value> {
self.call_rpc("getRecentPrioritizationFees", json!([[]])).await
}
async fn get_account(&self, address: &str) -> Result<Value> {
self.call_rpc("getAccountInfo", json!([
address,
{ "encoding": "jsonParsed" }
])).await
}
async fn get_history(&self, address: &str, limit: u32) -> Result<Value> {
self.call_rpc("getSignaturesForAddress", json!([
address,
{ "limit": limit }
])).await
}
}