Skip to content

Commit

Permalink
wip: working example for multithreading
Browse files Browse the repository at this point in the history
  • Loading branch information
jaswinder6991 committed May 17, 2024
1 parent 422bd6b commit eab8ffd
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
62 changes: 62 additions & 0 deletions near-accounts/examples/multi-thread.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use near_accounts::Account;
use near_crypto::{InMemorySigner,SecretKey};
use near_primitives::{types::Gas, views::FinalExecutionOutcomeViewEnum};
use near_providers::JsonRpcProvider;
use std::sync::Arc;
mod utils;
use near_primitives::types::{AccountId, Balance};
use serde_json::json;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();

// Initialization and user inputs as before

let signer_account_id: AccountId = "near-api-rs.testnet".parse::<AccountId>()?;
let signer_secret_key = "ed25519:29nYmQCZMsQeYtztXZzm57ayQt2uBHXdn2SAjK4ccMGSQaNUFNJ7Aoteno81eKTex9cGBbk1FuDuqJRsdzx34xDY".parse::<SecretKey>()?;

let provider = Arc::new(JsonRpcProvider::new("https://rpc.testnet.near.org"));
//let provider = JsonRpcProvider::new("https://rpc.testnet.near.org");
let signer = Arc::new(InMemorySigner::from_secret_key(signer_account_id.clone(), signer_secret_key));
let account = Account::new(signer_account_id, signer.clone(), provider.clone());

let contract_id: AccountId = "contract.near-api-rs.testnet".parse::<AccountId>()?;

// This spawns a new asynchronous task to handle account creation
let handle = tokio::spawn(async move {

let method_name = "set_status".to_string();
let args_json = json!({"message": "working1"});

// 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 result = account
.function_call(&contract_id, method_name, args_json, gas, amount)
.await;
//.await.expect("Reason")
//.transact()
//.await;

// match result {
// Ok(res) => match &res.final_execution_outcome {
// Some(FinalExecutionOutcomeViewEnum::FinalExecutionOutcome(outcome)) => {
// println!("Final Execution outcome: {:?}", outcome);
// },
// Some(FinalExecutionOutcomeViewEnum::FinalExecutionOutcomeWithReceipt(outcome_receipt)) => {
// println!("Final Execution outcome with receipt: {:?}", outcome_receipt);
// },
// None => println!("No Final execution outcome."),
// },
// Err(err) => println!("Error: {:#?}", err),
// }
println!("Result - {:#?}",result);
});

// You can do more work here or wait for the handle if needed
handle.await?;

Ok(())
}
10 changes: 6 additions & 4 deletions near-accounts/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,10 @@ impl TransactionSender {
/// Represents a NEAR account, encapsulating account ID, signer, and provider for blockchain interaction.
pub struct Account {
pub account_id: AccountId,
pub signer: Arc<dyn Signer>, // Use your Signer abstraction
pub provider: Arc<dyn Provider>, // Use your Provider abstraction
//pub signer: Arc<dyn Signer>, // Use your Signer abstraction
pub signer: Arc<dyn Signer + Send + Sync>, // Use your Signer abstraction
//pub provider: Arc<dyn Provider>, // Use your Provider abstraction
provider: Arc<dyn Provider + Send + Sync>,
}

/// Represents the balance details of a NEAR account.
Expand All @@ -139,8 +141,8 @@ impl Account {
/// A new `Account` instance.
pub fn new(
account_id: AccountId,
signer: Arc<dyn Signer>,
provider: Arc<dyn Provider>,
signer: Arc<dyn Signer + Send + Sync>,
provider: Arc<dyn Provider + Send + Sync>,
) -> Self {
Self {
account_id,
Expand Down

0 comments on commit eab8ffd

Please sign in to comment.