Skip to content

Commit

Permalink
removed create_account function from examples and removed Transaction…
Browse files Browse the repository at this point in the history
…Builder from near-accounts as well.
  • Loading branch information
jaswinder6991 committed Jun 24, 2024
1 parent 925bb26 commit 29dbcf3
Show file tree
Hide file tree
Showing 12 changed files with 256 additions and 83 deletions.
44 changes: 35 additions & 9 deletions near-accounts/examples/access_keys.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
mod example_config;
use near_accounts::Account;
use near_crypto::InMemorySigner;
use near_crypto::{InMemorySigner, SecretKey};
use near_primitives::types::Balance;
use near_providers::JsonRpcProvider;
use std::sync::Arc;
mod utils;
use near_primitives::types::AccountId;

async fn add_full_access() -> Result<(), Box<dyn std::error::Error>> {
let new_secret_key = near_crypto::SecretKey::from_random(near_crypto::KeyType::ED25519);
// Get test account and rpc details.
let config = example_config::get_test_config();

//Create a signer
let signer_account_id: AccountId = config.near_account.account_id.parse().unwrap();
let signer_secret_key: SecretKey = config.near_account.secret_key.parse().unwrap();
let signer = Arc::new(InMemorySigner::from_secret_key(
signer_account_id.clone(),
signer_secret_key,
));

let account = example_config::create_account();
//Creat a Provider
let provider = Arc::new(JsonRpcProvider::new(config.rpc_testnet_endpoint.as_str()));

//Create an Account object
let account = Account::new(signer_account_id, signer, provider);

//Generate a secret Key for new access key
let new_secret_key = near_crypto::SecretKey::from_random(near_crypto::KeyType::ED25519);

//Call add_key function on an Account
let result = account
.add_key(new_secret_key.public_key(), None, None, None)
.await;
Expand All @@ -27,17 +44,26 @@ async fn add_full_access() -> Result<(), Box<dyn std::error::Error>> {
}

async fn add_function_call_key() -> Result<(), Box<dyn std::error::Error>> {
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()?;
let signer = InMemorySigner::from_secret_key(signer_account_id.clone(), signer_secret_key);
// Get test account and rpc details.
let config = example_config::get_test_config();

let new_secret_key = near_crypto::SecretKey::from_random(near_crypto::KeyType::ED25519);
//Create a signer
let signer_account_id: AccountId = config.near_account.account_id.parse().unwrap();
let signer_secret_key: SecretKey = config.near_account.secret_key.parse().unwrap();
let signer = Arc::new(InMemorySigner::from_secret_key(
signer_account_id.clone(),
signer_secret_key,
));

let provider = Arc::new(JsonRpcProvider::new("https://rpc.testnet.near.org"));
let signer = Arc::new(signer);
//Creat a Provider
let provider = Arc::new(JsonRpcProvider::new(config.rpc_testnet_endpoint.as_str()));

//Create an Account object
let account = Account::new(signer_account_id, signer, provider);

// Create a secret key for the new function call access key
let new_secret_key = near_crypto::SecretKey::from_random(near_crypto::KeyType::ED25519);

let allowance: Balance = 1_000_000_000_000_000_000_000_000; // Example amount in yoctoNEAR
let contract_id = "contract.near-api-rs.testnet".to_string();
//Create an array of methods
Expand Down
40 changes: 23 additions & 17 deletions near-accounts/examples/async_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,49 @@ use near_providers::JsonRpcProvider;
use near_providers::Provider;
use std::sync::Arc;
mod utils;
use near_accounts::Account;
use near_crypto::{InMemorySigner, SecretKey};
use near_primitives::types::AccountId;
use serde_json::json;
use tokio::time;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
let contract_id: AccountId = "contract.near-api-rs.testnet".parse::<AccountId>()?;
// Get test account and rpc details.
let config = example_config::get_test_config();

let gas: Gas = 100_000_000_000_000; // Example amount in yoctoNEAR
//Create a signer
let signer_account_id: AccountId = config.near_account.account_id.parse().unwrap();
let signer_secret_key: SecretKey = config.near_account.secret_key.parse().unwrap();
let signer = Arc::new(InMemorySigner::from_secret_key(
signer_account_id.clone(),
signer_secret_key,
));

let provider = Arc::new(JsonRpcProvider::new("https://rpc.testnet.near.org"));
//Creat a Provider
let provider = Arc::new(JsonRpcProvider::new(config.rpc_testnet_endpoint.as_str()));

let account = example_config::create_account();
let method_name = "set_status".to_string();
//Create an Account object
let account = Account::new(signer_account_id, signer, provider.clone());

//Create argumements for function_call
//Contract id, method_name, method args, gas and deposit.
let contract_id: AccountId = "contract.near-api-rs.testnet".parse::<AccountId>()?;
let method_name = "set_status".to_string();
let args_json = json!({"message": "working1"});
let gas: Gas = 100_000_000_000_000; // Example amount in yoctoNEAR

//Create a Transaction Sender Object;
let transaction_sender = account
.function_call(&contract_id, method_name, args_json, gas, 0)
.await?;

//Get the transaction hash to query the chain later.
let tx_hash = transaction_sender.clone().get_transaction_hash().unwrap();

let t1 = time::Instant::now();
//Send the transaction
//Different Wait_until values: None, Included, ExecutedOptimistic, IncludedFinal, Executed, Final
let result = transaction_sender.transact_advanced("NONE").await;
let t2 = time::Instant::now();
match result {
Ok(res) => match &res.final_execution_outcome {
//Final Execution outcome for finality NONE would always be empty.
Expand All @@ -58,22 +73,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
sender_account_id: account.account_id,
};
let wait_until = TxExecutionStatus::ExecutedOptimistic;

time::sleep(time::Duration::from_secs(5)).await;

let t3 = time::Instant::now();
let tx_status = provider.tx_status(transaction_info, wait_until).await;
let t4 = time::Instant::now();

match tx_status {
Ok(response) => {
//println!("response gotten after: {}s", delta);
println!("response: {:#?}", response);
}
Err(err) => println!("Error: {:#?}", err),
}

println!("Time taken for async request: {:?}", t2 - t1);
println!("Time taken for status request: {:?}", t4 - t3);
Ok(())
}
25 changes: 23 additions & 2 deletions near-accounts/examples/create_account.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,40 @@
mod example_config;
use near_accounts::Account;
use near_crypto::{InMemorySigner, SecretKey};
use near_primitives::{types::Gas, views::FinalExecutionOutcomeViewEnum};
mod utils;
use near_primitives::types::{AccountId, Balance};
use near_providers::JsonRpcProvider;
use serde_json::json;
use std::sync::Arc;

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

// Get test account and rpc details.
let config = example_config::get_test_config();

//Create a signer
let signer_account_id: AccountId = config.near_account.account_id.parse().unwrap();
let signer_secret_key: SecretKey = config.near_account.secret_key.parse().unwrap();
let signer = Arc::new(InMemorySigner::from_secret_key(
signer_account_id.clone(),
signer_secret_key,
));

//Creat a Provider
let provider = Arc::new(JsonRpcProvider::new(config.rpc_testnet_endpoint.as_str()));

//Create an Account object
let account = Account::new(signer_account_id, signer, provider.clone());

//Ask the user for the new Account id.
let new_account_id: AccountId = utils::input("Enter new account name: ")?.parse()?;
// 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 account = example_config::create_account();

let contract_id: AccountId = "testnet".parse::<AccountId>()?;
let method_name = "create_account".to_string();

Expand Down
29 changes: 25 additions & 4 deletions near-accounts/examples/create_subaccount.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
//use near_providers::Provider;
mod example_config;
use near_primitives::types::Balance;
use near_accounts::Account;
use near_crypto::{InMemorySigner, SecretKey};
mod utils;
use near_primitives::types::AccountId;
use near_primitives::types::{AccountId, Balance};
use near_providers::JsonRpcProvider;
use std::sync::Arc;

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

// Get test account and rpc details.
let config = example_config::get_test_config();

//Create a signer
let signer_account_id: AccountId = config.near_account.account_id.parse().unwrap();
let signer_secret_key: SecretKey = config.near_account.secret_key.parse().unwrap();
let signer = Arc::new(InMemorySigner::from_secret_key(
signer_account_id.clone(),
signer_secret_key,
));

//Creat a Provider
let provider = Arc::new(JsonRpcProvider::new(config.rpc_testnet_endpoint.as_str()));

//Create an Account object
let account = Account::new(signer_account_id, signer, provider.clone());

//Ask user for the new account id, it should be of the form something.near-api-rs.testnet
//or whatever signer account you are using.
let new_account_id: AccountId =
utils::input("Enter the account name of new account ")?.parse()?;

Expand All @@ -15,7 +37,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

let new_key_pair = near_crypto::SecretKey::from_random(near_crypto::KeyType::ED25519);

let account = example_config::create_account();
// Call create_account
let result = account
.create_account(&new_account_id, new_key_pair.public_key(), amount)
Expand Down
23 changes: 22 additions & 1 deletion near-accounts/examples/delete_account.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
mod example_config;
use near_accounts::Account;
use near_crypto::{InMemorySigner, SecretKey};
use near_providers::JsonRpcProvider;
use std::sync::Arc;
mod utils;
use near_primitives::types::AccountId;

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

// Get test account and rpc details.
let config = example_config::get_test_config();

//Create a signer
let signer_account_id: AccountId = config.near_account.account_id.parse().unwrap();
let signer_secret_key: SecretKey = config.near_account.secret_key.parse().unwrap();
let signer = Arc::new(InMemorySigner::from_secret_key(
signer_account_id.clone(),
signer_secret_key,
));

//Creat a Provider
let provider = Arc::new(JsonRpcProvider::new(config.rpc_testnet_endpoint.as_str()));

//Create an Account object
let account = Account::new(signer_account_id, signer, provider.clone());

let beneficiary_account_id: AccountId =
utils::input("Enter the account name where you want to transfer current account balance before deleting it")?.parse()?;
let account = example_config::create_account();

let response = account.delete_account(beneficiary_account_id.clone()).await;

Expand Down
23 changes: 21 additions & 2 deletions near-accounts/examples/delete_key.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
mod example_config;
use near_crypto::PublicKey;
use near_accounts::Account;
use near_crypto::{InMemorySigner, PublicKey, SecretKey};
use near_providers::JsonRpcProvider;
use std::sync::Arc;
mod utils;
use near_primitives::types::AccountId;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
let account = example_config::create_account();
// Get test account and rpc details.
let config = example_config::get_test_config();

//Create a signer
let signer_account_id: AccountId = config.near_account.account_id.parse().unwrap();
let signer_secret_key: SecretKey = config.near_account.secret_key.parse().unwrap();
let signer = Arc::new(InMemorySigner::from_secret_key(
signer_account_id.clone(),
signer_secret_key,
));

//Creat a Provider
let provider = Arc::new(JsonRpcProvider::new(config.rpc_testnet_endpoint.as_str()));

//Create an Account object
let account = Account::new(signer_account_id, signer, provider.clone());

let public_key: PublicKey =
"ed25519:EohEtHT8Dt8jURC3DcJ661hWCx6ExPRtDV82FpT4jfNB".parse::<PublicKey>()?;
Expand Down
25 changes: 22 additions & 3 deletions near-accounts/examples/deploy_contract.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
use near_accounts::Account;
mod example_config;
use near_accounts::Account;
use near_crypto::{InMemorySigner, SecretKey};
use near_providers::JsonRpcProvider;
use std::sync::Arc;
mod utils;
use near_primitives::types::AccountId;

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

let account: Account = example_config::create_account();
// Get test account and rpc details.
let config = example_config::get_test_config();

let wasm_code = example_config::read_wasm_file()?;
//Create a signer
let signer_account_id: AccountId = config.near_account.account_id.parse().unwrap();
let signer_secret_key: SecretKey = config.near_account.secret_key.parse().unwrap();
let signer = Arc::new(InMemorySigner::from_secret_key(
signer_account_id.clone(),
signer_secret_key,
));

//Creat a Provider
let provider = Arc::new(JsonRpcProvider::new(config.rpc_testnet_endpoint.as_str()));

//Create an Account object
let account = Account::new(signer_account_id, signer, provider.clone());

//wasm code to deploy
let wasm_code = example_config::read_wasm_file()?;
let response = account.deploy_contract(&wasm_code).await;

match response {
Expand Down
19 changes: 12 additions & 7 deletions near-accounts/examples/function_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,34 @@ use serde_json::json;
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();

//Read test account details from config file
let config = example_config::get_test_config();
let signer_account_id: AccountId = config.near_account.account_id.parse().unwrap();
let signer_secret_key: SecretKey = config.near_account.secret_key.parse().unwrap();

let contract_id: AccountId = config.contract_account.account_id.parse().unwrap();
let signer = InMemorySigner::from_secret_key(signer_account_id.clone(), signer_secret_key);

let gas: Gas = 100_000_000_000_000; // Example amount in yoctoNEAR
//Create a signer
let signer = Arc::new(InMemorySigner::from_secret_key(
signer_account_id.clone(),
signer_secret_key,
));

//Create a provider
let provider = Arc::new(JsonRpcProvider::new(config.rpc_testnet_endpoint.as_str()));
let signer = Arc::new(signer);

//Create an Account
let account = Account::new(signer_account_id, signer, provider);
let method_name = "set_status".to_string();

let contract_id: AccountId = config.contract_account.account_id.parse().unwrap();
let method_name = "set_status".to_string();
let args_json = json!({"message": "working1"});
let gas: Gas = 100_000_000_000_000; // Example amount in yoctoNEAR

let result = account
.function_call(&contract_id, method_name, args_json, gas, 0)
.await?
.transact()
.await;
println!("response: {:#?}", result);

println!("response: {:#?}", result);
Ok(())
}
1 change: 0 additions & 1 deletion near-accounts/examples/multi-thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
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,
Expand Down
Loading

0 comments on commit 29dbcf3

Please sign in to comment.