-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
123 additions
and
127 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use calimero_sdk::env; | ||
use serde::de::DeserializeOwned; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
pub(crate) struct Client { | ||
url: String, | ||
} | ||
|
||
impl Client { | ||
pub fn new(url: String) -> Self { | ||
Self { url } | ||
} | ||
|
||
pub fn call<T: DeserializeOwned, E: DeserializeOwned>( | ||
&self, | ||
method: &str, | ||
params: serde_json::Value, | ||
) -> Result<Response<T, E>, String> { | ||
let headers = [("Content-Type", "application/json")]; | ||
|
||
let body = serde_json::to_vec(&serde_json::json!({ | ||
"jsonrpc": "2.0", | ||
"id": "1", | ||
"method": method, | ||
"params": params, | ||
})) | ||
.map_err(|err| format!("Cannot serialize request: {:?}", err))?; | ||
|
||
let response = unsafe { env::ext::fetch(&self.url, "POST", &headers, &body) }?; | ||
serde_json::from_slice(&response).unwrap() | ||
} | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
#[serde(remote = "Result")] | ||
pub enum ResultAlt<T, E> { | ||
#[serde(rename = "result")] | ||
Ok(T), | ||
#[serde(rename = "error")] | ||
Err(E), | ||
} | ||
|
||
impl<T, E> From<ResultAlt<T, E>> for Result<T, E> { | ||
fn from(result: ResultAlt<T, E>) -> Self { | ||
match result { | ||
ResultAlt::Ok(value) => Ok(value), | ||
ResultAlt::Err(err) => Err(err), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Deserialize)] | ||
pub struct Response<T: DeserializeOwned, E: DeserializeOwned> { | ||
pub jsonrpc: Option<String>, | ||
pub id: String, | ||
|
||
#[serde(with = "ResultAlt")] | ||
pub data: Result<T, RpcError<E>>, | ||
} | ||
|
||
#[derive(Debug, Clone, Deserialize)] | ||
pub struct RpcError<E> { | ||
pub code: i32, | ||
pub message: String, | ||
pub data: Option<E>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use jsonrpc::Response; | ||
use serde::Deserialize; | ||
|
||
mod jsonrpc; | ||
|
||
pub struct NearSdk { | ||
client: jsonrpc::Client, | ||
} | ||
|
||
#[derive(Debug, Clone, Deserialize)] | ||
pub struct BalanceResponse { | ||
pub amount: String, | ||
} | ||
|
||
impl NearSdk { | ||
pub fn testnet() -> Self { | ||
Self { | ||
client: jsonrpc::Client::new("https://rpc.testnet.near.org/".to_string()), | ||
} | ||
} | ||
|
||
pub fn mainnet() -> Self { | ||
Self { | ||
client: jsonrpc::Client::new("https://rpc.mainnet.near.org/".to_string()), | ||
} | ||
} | ||
|
||
pub fn get_balance(&self, account_id: &str) -> Result<BalanceResponse, String> { | ||
let response: Response<BalanceResponse, String> = self.client.call( | ||
"query", | ||
serde_json::json!({ | ||
"request_type": "view_account", | ||
"finality": "final", | ||
"account_id": account_id, | ||
}), | ||
)?; | ||
|
||
match response.data { | ||
Ok(r) => Ok(r), | ||
Err(e) => Err(e.message), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters