Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: updated near-* to 0.24.0 #151

Merged
merged 4 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ thiserror = "1.0.37"
serde_json = "1.0.85"
lazy_static = "1.4.0"

near-crypto = ">0.22,<0.24"
near-primitives = ">0.22,<0.24"
near-chain-configs = ">0.22,<0.24"
near-jsonrpc-primitives = ">0.22,<0.24"
near-crypto = ">0.22,<0.25"
near-primitives = ">0.22,<0.25"
near-chain-configs = ">0.22,<0.25"
near-jsonrpc-primitives = ">0.22,<0.25"
akorchyn marked this conversation as resolved.
Show resolved Hide resolved

[dev-dependencies]
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
Expand Down
7 changes: 4 additions & 3 deletions examples/contract_change_method.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use near_crypto::Signer;
use near_jsonrpc_client::{methods, JsonRpcClient};
use near_jsonrpc_primitives::types::query::QueryResponseKind;
use near_jsonrpc_primitives::types::transactions::{RpcTransactionError, TransactionInfo};
use near_primitives::transaction::{Action, FunctionCallAction, Transaction};
use near_primitives::transaction::{Action, FunctionCallAction, Transaction, TransactionV0};
use near_primitives::types::BlockReference;
use near_primitives::views::TxExecutionStatus;

Expand Down Expand Up @@ -39,7 +40,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let other_account = utils::input("Enter the account to be rated: ")?;
let rating = utils::input("Enter a rating: ")?.parse::<f32>()?;

let transaction = Transaction {
let transaction = TransactionV0 {
signer_id: signer.account_id.clone(),
public_key: signer.public_key.clone(),
nonce: current_nonce + 1,
Expand All @@ -59,7 +60,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
};

let request = methods::broadcast_tx_async::RpcBroadcastTxAsyncRequest {
signed_transaction: transaction.sign(&signer),
signed_transaction: Transaction::V0(transaction).sign(&Signer::InMemory(signer.clone())),
};

let sent_at = time::Instant::now();
Expand Down
7 changes: 4 additions & 3 deletions examples/contract_change_method_commit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use near_crypto::Signer;
use near_jsonrpc_client::{methods, JsonRpcClient};
use near_jsonrpc_primitives::types::query::QueryResponseKind;
use near_primitives::transaction::{Action, FunctionCallAction, Transaction};
use near_primitives::transaction::{Action, FunctionCallAction, Transaction, TransactionV0};
use near_primitives::types::BlockReference;

use serde_json::json;
Expand Down Expand Up @@ -36,7 +37,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let other_account = utils::input("Enter the account to be rated: ")?;
let rating = utils::input("Enter a rating: ")?.parse::<f32>()?;

let transaction = Transaction {
let transaction = TransactionV0 {
signer_id: signer.account_id.clone(),
public_key: signer.public_key.clone(),
nonce: current_nonce + 1,
Expand All @@ -56,7 +57,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
};

let request = methods::broadcast_tx_commit::RpcBroadcastTxCommitRequest {
signed_transaction: transaction.sign(&signer),
signed_transaction: Transaction::V0(transaction).sign(&Signer::InMemory(signer)),
};

let response = client.call(request).await?;
Expand Down
95 changes: 51 additions & 44 deletions examples/create_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
//!
//! This script is interactive.

use near_crypto::Signer;
use near_jsonrpc_client::methods::broadcast_tx_commit::RpcTransactionError;
use near_jsonrpc_client::{methods, JsonRpcClient};
use near_jsonrpc_primitives::types::query::QueryResponseKind;
use near_jsonrpc_primitives::types::transactions::TransactionInfo;
use near_primitives::hash::CryptoHash;
use near_primitives::transaction::{
Action, AddKeyAction, CreateAccountAction, FunctionCallAction, Transaction, TransferAction,
Action, AddKeyAction, CreateAccountAction, FunctionCallAction, Transaction, TransactionV0,
TransferAction,
};
use near_primitives::types::{AccountId, BlockReference};
use near_primitives::views::{FinalExecutionStatus, TxExecutionStatus};
Expand Down Expand Up @@ -124,35 +126,38 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
utils::input("How much do you want to fund this account with (in Ⓝ units)? ")?
.parse()?;
if deposit >= 0.0 {
break ((deposit * 1_000_000.0) as u128) * 1_000_000_000_000_000_000 as u128;
break ((deposit * 1_000_000.0) as u128) * 1_000_000_000_000_000_000_u128;
}
println!("(i) Enter a non-zero deposit value!");
};

let is_sub_account = new_account_id.is_sub_account_of(&signer.account_id);
let new_key_pair = near_crypto::SecretKey::from_random(near_crypto::KeyType::ED25519);

let transaction = if is_sub_account {
Transaction {
signer_id: signer.account_id.clone(),
public_key: signer.public_key.clone(),
nonce: current_nonce + 1,
receiver_id: new_account_id.clone(),
block_hash: latest_hash,
actions: vec![
Action::CreateAccount(CreateAccountAction {}),
Action::AddKey(Box::new(AddKeyAction {
access_key: near_primitives::account::AccessKey {
nonce: 0,
permission: near_primitives::account::AccessKeyPermission::FullAccess,
},
public_key: new_key_pair.public_key(),
})),
Action::Transfer(TransferAction {
deposit: initial_deposit,
}),
],
}
let (transaction, expected_output) = if is_sub_account {
(
TransactionV0 {
signer_id: signer.account_id.clone(),
public_key: signer.public_key.clone(),
nonce: current_nonce + 1,
receiver_id: new_account_id.clone(),
block_hash: latest_hash,
actions: vec![
Action::CreateAccount(CreateAccountAction {}),
Action::AddKey(Box::new(AddKeyAction {
access_key: near_primitives::account::AccessKey {
nonce: 0,
permission: near_primitives::account::AccessKeyPermission::FullAccess,
},
public_key: new_key_pair.public_key(),
})),
Action::Transfer(TransferAction {
deposit: initial_deposit,
}),
],
},
vec![],
)
} else {
let contract_id = if client.server_addr().ends_with("testnet.near.org") {
"testnet".parse()?
Expand All @@ -161,24 +166,27 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
} else {
Err("can only create non-sub accounts for mainnet / testnet\nconsider creating a sub-account instead")?
};
Transaction {
signer_id: signer.account_id.clone(),
public_key: signer.public_key.clone(),
nonce: current_nonce + 1,
receiver_id: contract_id,
block_hash: latest_hash,
actions: vec![Action::FunctionCall(Box::new(FunctionCallAction {
method_name: "create_account".to_string(),
args: json!({
"new_account_id": new_account_id,
"new_public_key": new_key_pair.public_key(),
})
.to_string()
.into_bytes(),
gas: 300_000_000_000_000,
deposit: initial_deposit,
}))],
}
(
TransactionV0 {
signer_id: signer.account_id.clone(),
public_key: signer.public_key.clone(),
nonce: current_nonce + 1,
receiver_id: contract_id,
block_hash: latest_hash,
actions: vec![Action::FunctionCall(Box::new(FunctionCallAction {
method_name: "create_account".to_string(),
args: json!({
"new_account_id": new_account_id,
"new_public_key": new_key_pair.public_key(),
})
.to_string()
.into_bytes(),
gas: 300_000_000_000_000,
deposit: initial_deposit,
}))],
},
b"true".to_vec(),
)
};

println!("=============================================================");
Expand All @@ -189,7 +197,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("-------------------------------------------------------------");

let request = methods::broadcast_tx_async::RpcBroadcastTxAsyncRequest {
signed_transaction: transaction.sign(&signer),
signed_transaction: Transaction::V0(transaction).sign(&Signer::InMemory(signer.clone())),
};

let sent_at = time::Instant::now();
Expand Down Expand Up @@ -227,8 +235,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
break;
}
FinalExecutionStatus::SuccessValue(ref s) => {
// outcome.status != SuccessValue(`false`)
if s == b"false" {
if s == &expected_output {
println!("(i) Account successfully created after {}s", delta);
} else {
println!("{:#?}", outcome);
Expand Down
51 changes: 22 additions & 29 deletions examples/query_tx.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::str::FromStr;

use near_jsonrpc_client::methods;
use near_primitives::{hash::CryptoHash, types::AccountId};

mod utils;

Expand Down Expand Up @@ -57,42 +60,32 @@ pub fn specify_block_reference() -> std::io::Result<near_primitives::types::Bloc
Ok(block_reference)
}

fn get_valid_input<T: FromStr>(
prompt: &str,
max_retries: usize,
) -> Result<T, Box<dyn std::error::Error>> {
for _ in 0..max_retries {
let input = utils::input(prompt)?;
if let Ok(value) = input.parse() {
return Ok(value);
} else {
println!("(i) Invalid input!");
}
}

Err(format!("(i) Maximum number of retries ({}) reached", max_retries).into())
}

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

let client = utils::select_network()?;

// tolerate only 3 retries for a non-failing transaction hash
'root: for _ in 1..=3 {
let tx_hash = 'tx_hash: loop {
// tolerate only 3 retries for a valid transaction hash
for _ in 1..=3 {
if let Ok(tx_hash) =
utils::input("What transaction hash should we query? ")?.parse()
{
break 'tx_hash tx_hash;
}
println!("(i) Invalid transaction hash!");
}

break 'root;
};

let account_id = 'account_id: loop {
// tolerate only 3 retries for a valid Account ID
for _ in 1..=3 {
if let Ok(account_id) =
utils::input("What account signed this transaction? ")?.parse()
{
break 'account_id account_id;
}
println!("(i) Invalid Account ID!");
}

break 'root;
};

for _ in 1..=3 {
let tx_hash: CryptoHash = get_valid_input("What transaction hash should we query", 3)?;
let account_id: AccountId = get_valid_input("What account signed this transaction", 3)?;
let wait_until_str = utils::input("Enter the desired guaranteed execution status (can be one of: NONE, INCLUDED, INCLUDED_FINAL, EXECUTED, FINAL): ")?;
let wait_until = serde_json::from_value(serde_json::json!(wait_until_str))?;

Expand Down
8 changes: 4 additions & 4 deletions examples/send_tx.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use near_jsonrpc_client::{methods, JsonRpcClient};
use near_jsonrpc_primitives::types::query::QueryResponseKind;
use near_jsonrpc_primitives::types::transactions::{RpcTransactionError, TransactionInfo};
use near_primitives::transaction::{Action, FunctionCallAction, Transaction};
use near_primitives::transaction::{Action, FunctionCallAction, Transaction, TransactionV0};
use near_primitives::types::BlockReference;
use near_primitives::views::TxExecutionStatus;
use tokio::time;
Expand Down Expand Up @@ -39,7 +39,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let other_account = utils::input("Enter the account to be rated: ")?;
let rating = utils::input("Enter a rating: ")?.parse::<f32>()?;

let transaction = Transaction {
let transaction = Transaction::V0(TransactionV0 {
signer_id: signer.account_id.clone(),
public_key: signer.public_key.clone(),
nonce: current_nonce + 1,
Expand All @@ -56,11 +56,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
gas: 100_000_000_000_000, // 100 TeraGas
deposit: 0,
}))],
};
});
let tx_hash = transaction.get_hash_and_size().0;

let request = methods::send_tx::RpcSendTransactionRequest {
signed_transaction: transaction.sign(&signer),
signed_transaction: transaction.sign(&near_crypto::Signer::InMemory(signer.clone())),
wait_until: wait_until.clone(),
};

Expand Down
6 changes: 6 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[toolchain]
# This specifies the version of Rust we use to build.
# Individual crates in the workspace may support a lower version, as indicated by `rust-version` field in each crate's `Cargo.toml`.
# The version specified below, should be at least as high as the maximum `rust-version` within the workspace.
channel = "1.80.0"
components = ["rustfmt", "clippy", "rust-analyzer"]
8 changes: 4 additions & 4 deletions src/methods/broadcast_tx_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//! ```no_run
//! use near_jsonrpc_client::{methods, JsonRpcClient};
//! use near_primitives::types::{AccountId};
//! use near_primitives::transaction::{Action, FunctionCallAction, Transaction};
//! use near_primitives::transaction::{Action, FunctionCallAction, Transaction, TransactionV0};
//! use near_crypto::SecretKey;
//! use core::str::FromStr;
//! use serde_json::json;
Expand All @@ -28,7 +28,7 @@
//! let other_account = "rpc_docs.testnet".parse::<AccountId>()?;
//! let rating = "4.5".parse::<f32>()?;
//!
//! let transaction = Transaction {
//! let transaction = Transaction::V0(TransactionV0 {
//! signer_id: signer.account_id.clone(),
//! public_key: signer.public_key.clone(),
//! nonce: 10223934 + 1,
Expand All @@ -45,10 +45,10 @@
//! gas: 100_000_000_000_000, // 100 TeraGas
//! deposit: 0,
//! }))],
//! };
//! });
//!
//! let request = methods::broadcast_tx_async::RpcBroadcastTxAsyncRequest {
//! signed_transaction: transaction.sign(&signer)
//! signed_transaction: transaction.sign(&near_crypto::Signer::InMemory(signer))
//! };
//! # Ok(())
//! # }
Expand Down
8 changes: 4 additions & 4 deletions src/methods/broadcast_tx_commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//! use near_jsonrpc_client::{methods, JsonRpcClient};
//! use near_jsonrpc_primitives::types::{query::QueryResponseKind, transactions::TransactionInfo};
//! use near_primitives::types::{AccountId, BlockReference};
//! use near_primitives::transaction::{Action, FunctionCallAction, Transaction};
//! use near_primitives::transaction::{Action, FunctionCallAction, Transaction, TransactionV0};
//! use serde_json::json;
//!
//! # #[tokio::main]
Expand All @@ -29,7 +29,7 @@
//! let other_account = "rpc_docs.testnet".parse::<AccountId>()?;
//! let rating = "4.5".parse::<f32>()?;
//!
//! let transaction = Transaction {
//! let transaction = Transaction::V0(TransactionV0 {
//! signer_id: signer.account_id.clone(),
//! public_key: signer.public_key.clone(),
//! nonce: 904565 + 1,
Expand All @@ -46,10 +46,10 @@
//! gas: 100_000_000_000_000, // 100 TeraGas
//! deposit: 0,
//! }))],
//! };
//! });
//!
//! let request = methods::broadcast_tx_commit::RpcBroadcastTxCommitRequest {
//! signed_transaction: transaction.sign(&signer)
//! signed_transaction: transaction.sign(&near_crypto::Signer::InMemory(signer))
//! };
//! # Ok(())
//! # }
Expand Down
Loading
Loading