Skip to content

Commit

Permalink
add consts for near-hosted rpc endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
miraclx committed Aug 31, 2021
1 parent bec8144 commit 35f5e8f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,29 @@ Generic, low-level interfaces for interacting with the NEAR Protocol via JSON_RP
// Here, we manually construct a method and execute that on a client
// This is useful if you have multiple clients to call methods on

use near_primitives::types::AccountId;
use near_api_providers::{jsonrpc::JsonRpcMethod, NearClient};
use near_api_providers::{NEAR_MAINNET_RPC_ENDPOINT_URL, NEAR_TESTNET_RPC_ENDPOINT_URL};
use near_jsonrpc_primitives::views::FinalExecutionOutcomeView;
use near_primitives::types::AccountId;

let client_builder = NearClient::new(); // instantiate once, reuse

let jsonrpc_client_1 = client_builder.connect("http://localhost:3030").as_jsonrpc();
let jsonrpc_client_2 = client_builder.connect("http://rpc.testnet.near.org").as_jsonrpc();
let mainnet_jsonrpc_client = client_builder.connect(NEAR_MAINNET_RPC_ENDPOINT_URL).as_jsonrpc();
let testnet_jsonrpc_client = client_builder.connect(NEAR_TESTNET_RPC_ENDPOINT_URL).as_jsonrpc();

let method = RpcMethod::Tx { // this method can be reused
let method = RpcMethod::Tx {
// this method can be reused
id: "miraclx.near".parse::<AccountId>()?,
hash: "9FtHUFBQsZ2MG77K3x3MJ9wjX3UT8zE1TczCrhZEcG8U".parse::<CryptoHash>()?,
};

let tx_status_1: FinalExecutionOutcomeView = method.call_on(&jsonrpc_client_1).await?;
let tx_status_2: FinalExecutionOutcomeView = method.call_on(&jsonrpc_client_2).await?;
let tx_status_on_mainnet: FinalExecutionOutcomeView =
method.call_on(&mainnet_jsonrpc_client).await?;
let tx_status_on_testnet: FinalExecutionOutcomeView =
method.call_on(&testnet_jsonrpc_client).await?;

println!("{:?}", tx_status_1);
println!("{:?}", tx_status_2);
println!("{:?}", tx_status_on_mainnet);
println!("{:?}", tx_status_on_testnet);
```

## Testing
Expand Down
8 changes: 6 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
//!
//! ```
//! # #![allow(deprecated)]
//! # use near_api_providers::NearClient;
//! use near_api_providers::{NearClient, NEAR_TESTNET_RPC_ENDPOINT_URL};
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let near_client = NearClient::new().connect("https://rpc.testnet.near.org");
//! let near_client = NearClient::new().connect(NEAR_TESTNET_RPC_ENDPOINT_URL);
//!
//! let jsonrpc_client = near_client.as_jsonrpc();
//! let http_client = near_client.as_http() ;
Expand All @@ -29,6 +30,9 @@
pub mod http;
pub mod jsonrpc;

pub const NEAR_MAINNET_RPC_ENDPOINT_URL: &str = "http://rpc.mainnet.near.org";
pub const NEAR_TESTNET_RPC_ENDPOINT_URL: &str = "http://rpc.testnet.near.org";

/// A generic RPC/HTTP NEAR Client builder.
///
/// Use this to create dedicated clients for each server.
Expand Down

0 comments on commit 35f5e8f

Please sign in to comment.