pub struct JsonRpcProvider { /* private fields */ }
Expand description
Represents a provider that uses JSON RPC to interact with the NEAR blockchain.
Implementations§
Source§impl JsonRpcProvider
impl JsonRpcProvider
Sourcepub fn new(rpc_endpoint: &str) -> JsonRpcProvider
pub fn new(rpc_endpoint: &str) -> JsonRpcProvider
Constructs a new JsonRpcProvider
with the specified RPC endpoint.
Examples found in repository?
7 8 9 10 11 12 13 14 15 16 17 18 19
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
let account_id: AccountId = "near-api-rs.testnet".parse::<AccountId>()?;
let provider = Arc::new(JsonRpcProvider::new("https://rpc.testnet.near.org"));
let result = get_access_key(provider, account_id).await;
println!("response: {:#?}", result);
Ok(())
}
More examples
7 8 9 10 11 12 13 14 15 16 17 18
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
let provider = Arc::new(JsonRpcProvider::new("https://rpc.testnet.near.org"));
let contract_id: AccountId = "contract.near-api-rs.testnet".parse::<AccountId>()?;
let result = view_state(provider, contract_id, None).await;
println!("response: {:#?}", result);
Ok(())
}
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
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
let signer_account_id: AccountId = utils::input("Enter the signer Account ID: ")?.parse()?;
let signer_secret_key = utils::input("Enter the signer's private key: ")?.parse()?;
//To-do, implement account exist check.
let new_account_id: AccountId = utils::input("Enter new account name: ")?.parse()?;
let signer = InMemorySigner::from_secret_key(signer_account_id.clone(), signer_secret_key);
// Amount to transfer to the new account
let gas: Gas = 100_000_000_000_000; // Example amount in yoctoNEAR
let amount: Balance = 10_000_000_000_000_000_000_000; // Example amount in yoctoNEAR
let new_secret_key = near_crypto::SecretKey::from_random(near_crypto::KeyType::ED25519);
let provider = Arc::new(JsonRpcProvider::new("https://rpc.testnet.near.org"));
let signer = Arc::new(signer);
let account = Account::new(signer_account_id, signer, provider);
let contract_id: AccountId = "testnet".parse::<AccountId>()?;
let method_name = "create_account".to_string();
let args_json = json!({
"new_account_id": new_account_id,
"new_public_key": new_secret_key.public_key()
});
let result = account
.function_call(contract_id, method_name, args_json, gas, amount)
.await;
println!("response: {:#?}", result);
println!("New Account ID: {}", new_account_id);
println!("Secret Key: {}", new_secret_key);
Ok(())
}
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
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
let client = JsonRpcClient::connect("https://rpc.testnet.near.org");
let provider = JsonRpcProvider::new("https://rpc.testnet.near.org");
let signer_account_id = utils::input("Enter the signer Account ID: ")?.parse()?;
let signer_secret_key = utils::input("Enter the signer's private key: ")?.parse()?;
let signer = near_crypto::InMemorySigner::from_secret_key(signer_account_id, signer_secret_key);
let access_key_query_response = client
.call(methods::query::RpcQueryRequest {
block_reference: BlockReference::latest(),
request: near_primitives::views::QueryRequest::ViewAccessKey {
account_id: signer.account_id.clone(),
public_key: signer.public_key.clone(),
},
})
.await?;
let current_nonce = match access_key_query_response.kind {
QueryResponseKind::AccessKey(access_key) => access_key.nonce,
_ => Err("failed to extract current nonce")?,
};
let other_account = utils::input("Enter the account to be rated: ")?;
let rating = utils::input("Enter a rating: ")?.parse::<f32>()?;
let transaction = Transaction {
signer_id: signer.account_id.clone(),
public_key: signer.public_key.clone(),
nonce: current_nonce + 1,
receiver_id: "nosedive.testnet".parse()?,
block_hash: access_key_query_response.block_hash,
actions: vec![Action::FunctionCall(Box::new(FunctionCallAction {
method_name: "rate".to_string(),
args: json!({
"account_id": other_account,
"rating": rating,
})
.to_string()
.into_bytes(),
gas: 100_000_000_000_000, // 100 TeraGas
deposit: 0,
}))],
};
let response = provider.send_transaction(transaction.sign(&signer)).await?;
println!("response: {:#?}", response);
Ok(())
}
Trait Implementations§
Source§impl Provider for JsonRpcProvider
impl Provider for JsonRpcProvider
Source§fn status<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<StatusResponse, JsonRpcError<RpcStatusError>>> + Send + 'async_trait>>where
'life0: 'async_trait,
JsonRpcProvider: 'async_trait,
fn status<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<StatusResponse, JsonRpcError<RpcStatusError>>> + Send + 'async_trait>>where
'life0: 'async_trait,
JsonRpcProvider: 'async_trait,
Retrieves the current status of the NEAR blockchain.
Source§fn query<'life0, 'async_trait>(
&'life0 self,
request: QueryRequest,
) -> Pin<Box<dyn Future<Output = Result<RpcQueryResponse, JsonRpcError<RpcQueryError>>> + Send + 'async_trait>>where
'life0: 'async_trait,
JsonRpcProvider: 'async_trait,
fn query<'life0, 'async_trait>(
&'life0 self,
request: QueryRequest,
) -> Pin<Box<dyn Future<Output = Result<RpcQueryResponse, JsonRpcError<RpcQueryError>>> + Send + 'async_trait>>where
'life0: 'async_trait,
JsonRpcProvider: 'async_trait,
Executes a query on the NEAR blockchain using a given QueryRequest
.
Source§fn send_transaction<'life0, 'async_trait>(
&'life0 self,
signed_transaction: SignedTransaction,
) -> Pin<Box<dyn Future<Output = Result<FinalExecutionOutcomeView, JsonRpcError<RpcTransactionError>>> + Send + 'async_trait>>where
'life0: 'async_trait,
JsonRpcProvider: 'async_trait,
fn send_transaction<'life0, 'async_trait>(
&'life0 self,
signed_transaction: SignedTransaction,
) -> Pin<Box<dyn Future<Output = Result<FinalExecutionOutcomeView, JsonRpcError<RpcTransactionError>>> + Send + 'async_trait>>where
'life0: 'async_trait,
JsonRpcProvider: 'async_trait,
Sends a signed transaction to the NEAR blockchain, waiting for its final execution outcome.
Source§fn send_transaction_async<'life0, 'async_trait>(
&'life0 self,
signed_transaction: SignedTransaction,
) -> Pin<Box<dyn Future<Output = Result<CryptoHash, JsonRpcError<RpcBroadcastTxAsyncError>>> + Send + 'async_trait>>where
'life0: 'async_trait,
JsonRpcProvider: 'async_trait,
fn send_transaction_async<'life0, 'async_trait>(
&'life0 self,
signed_transaction: SignedTransaction,
) -> Pin<Box<dyn Future<Output = Result<CryptoHash, JsonRpcError<RpcBroadcastTxAsyncError>>> + Send + 'async_trait>>where
'life0: 'async_trait,
JsonRpcProvider: 'async_trait,
Sends a signed transaction to the NEAR blockchain asynchronously, without waiting for its final execution outcome.
Source§fn tx_status<'life0, 'async_trait>(
&'life0 self,
transaction_info: TransactionInfo,
) -> Pin<Box<dyn Future<Output = Result<FinalExecutionOutcomeView, JsonRpcError<RpcTransactionError>>> + Send + 'async_trait>>where
'life0: 'async_trait,
JsonRpcProvider: 'async_trait,
fn tx_status<'life0, 'async_trait>(
&'life0 self,
transaction_info: TransactionInfo,
) -> Pin<Box<dyn Future<Output = Result<FinalExecutionOutcomeView, JsonRpcError<RpcTransactionError>>> + Send + 'async_trait>>where
'life0: 'async_trait,
JsonRpcProvider: 'async_trait,
Retrieves the status of a transaction on the NEAR blockchain, identified by TransactionInfo
.
Source§fn chunk<'life0, 'async_trait>(
&'life0 self,
chunk_reference: ChunkReference,
) -> Pin<Box<dyn Future<Output = Result<ChunkView, JsonRpcError<RpcChunkError>>> + Send + 'async_trait>>where
'life0: 'async_trait,
JsonRpcProvider: 'async_trait,
fn chunk<'life0, 'async_trait>(
&'life0 self,
chunk_reference: ChunkReference,
) -> Pin<Box<dyn Future<Output = Result<ChunkView, JsonRpcError<RpcChunkError>>> + Send + 'async_trait>>where
'life0: 'async_trait,
JsonRpcProvider: 'async_trait,
Fetches details of a specific chunk from the NEAR blockchain, identified by ChunkReference
.
Source§fn block<'life0, 'async_trait>(
&'life0 self,
block_reference: BlockReference,
) -> Pin<Box<dyn Future<Output = Result<BlockView, JsonRpcError<RpcBlockError>>> + Send + 'async_trait>>where
'life0: 'async_trait,
JsonRpcProvider: 'async_trait,
fn block<'life0, 'async_trait>(
&'life0 self,
block_reference: BlockReference,
) -> Pin<Box<dyn Future<Output = Result<BlockView, JsonRpcError<RpcBlockError>>> + Send + 'async_trait>>where
'life0: 'async_trait,
JsonRpcProvider: 'async_trait,
Retrieves a block from the NEAR blockchain, specified by its BlockReference
.
Source§fn experimental_protocol_config<'life0, 'async_trait>(
&'life0 self,
block_reference: BlockReference,
) -> Pin<Box<dyn Future<Output = Result<ProtocolConfigView, JsonRpcError<RpcProtocolConfigError>>> + Send + 'async_trait>>where
'life0: 'async_trait,
JsonRpcProvider: 'async_trait,
fn experimental_protocol_config<'life0, 'async_trait>(
&'life0 self,
block_reference: BlockReference,
) -> Pin<Box<dyn Future<Output = Result<ProtocolConfigView, JsonRpcError<RpcProtocolConfigError>>> + Send + 'async_trait>>where
'life0: 'async_trait,
JsonRpcProvider: 'async_trait,
Fetches the experimental protocol configuration for a specific block, identified by BlockReference
.
Source§fn validators<'life0, 'async_trait>(
&'life0 self,
epoch_reference: EpochReference,
) -> Pin<Box<dyn Future<Output = Result<EpochValidatorInfo, JsonRpcError<RpcValidatorError>>> + Send + 'async_trait>>where
'life0: 'async_trait,
JsonRpcProvider: 'async_trait,
fn validators<'life0, 'async_trait>(
&'life0 self,
epoch_reference: EpochReference,
) -> Pin<Box<dyn Future<Output = Result<EpochValidatorInfo, JsonRpcError<RpcValidatorError>>> + Send + 'async_trait>>where
'life0: 'async_trait,
JsonRpcProvider: 'async_trait,
Retrieves information about validators for a given epoch, specified by EpochReference
.