diff --git a/Cargo.toml b/Cargo.toml index 00d4fef..a20194c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" [dev-dependencies] tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] } diff --git a/examples/contract_change_method.rs b/examples/contract_change_method.rs index a77e308..8823cd4 100644 --- a/examples/contract_change_method.rs +++ b/examples/contract_change_method.rs @@ -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; @@ -39,7 +40,7 @@ async fn main() -> Result<(), Box> { let other_account = utils::input("Enter the account to be rated: ")?; let rating = utils::input("Enter a rating: ")?.parse::()?; - let transaction = Transaction { + let transaction = TransactionV0 { signer_id: signer.account_id.clone(), public_key: signer.public_key.clone(), nonce: current_nonce + 1, @@ -59,7 +60,7 @@ async fn main() -> Result<(), Box> { }; 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(); diff --git a/examples/contract_change_method_commit.rs b/examples/contract_change_method_commit.rs index db074eb..78e0b56 100644 --- a/examples/contract_change_method_commit.rs +++ b/examples/contract_change_method_commit.rs @@ -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; @@ -36,7 +37,7 @@ async fn main() -> Result<(), Box> { let other_account = utils::input("Enter the account to be rated: ")?; let rating = utils::input("Enter a rating: ")?.parse::()?; - let transaction = Transaction { + let transaction = TransactionV0 { signer_id: signer.account_id.clone(), public_key: signer.public_key.clone(), nonce: current_nonce + 1, @@ -56,7 +57,7 @@ async fn main() -> Result<(), Box> { }; 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?; diff --git a/examples/create_account.rs b/examples/create_account.rs index dce9ae0..055a9c6 100644 --- a/examples/create_account.rs +++ b/examples/create_account.rs @@ -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}; @@ -124,7 +126,7 @@ async fn main() -> Result<(), Box> { 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!"); }; @@ -132,27 +134,30 @@ async fn main() -> Result<(), Box> { 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()? @@ -161,24 +166,27 @@ async fn main() -> Result<(), Box> { } 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!("============================================================="); @@ -189,7 +197,7 @@ async fn main() -> Result<(), Box> { 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(); @@ -227,8 +235,7 @@ async fn main() -> Result<(), Box> { 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); diff --git a/examples/query_tx.rs b/examples/query_tx.rs index 28169a8..b97a27a 100644 --- a/examples/query_tx.rs +++ b/examples/query_tx.rs @@ -1,4 +1,7 @@ +use std::str::FromStr; + use near_jsonrpc_client::methods; +use near_primitives::{hash::CryptoHash, types::AccountId}; mod utils; @@ -57,6 +60,22 @@ pub fn specify_block_reference() -> std::io::Result( + prompt: &str, + max_retries: usize, +) -> Result> { + 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> { env_logger::init(); @@ -64,35 +83,9 @@ async fn main() -> Result<(), Box> { 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))?; diff --git a/examples/send_tx.rs b/examples/send_tx.rs index 141f395..d6dd3f5 100644 --- a/examples/send_tx.rs +++ b/examples/send_tx.rs @@ -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; @@ -39,7 +39,7 @@ async fn main() -> Result<(), Box> { let other_account = utils::input("Enter the account to be rated: ")?; let rating = utils::input("Enter a rating: ")?.parse::()?; - let transaction = Transaction { + let transaction = Transaction::V0(TransactionV0 { signer_id: signer.account_id.clone(), public_key: signer.public_key.clone(), nonce: current_nonce + 1, @@ -56,11 +56,11 @@ async fn main() -> Result<(), Box> { 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(), }; diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..f2343a7 --- /dev/null +++ b/rust-toolchain.toml @@ -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"] diff --git a/src/methods/broadcast_tx_async.rs b/src/methods/broadcast_tx_async.rs index 92d4724..e19a226 100644 --- a/src/methods/broadcast_tx_async.rs +++ b/src/methods/broadcast_tx_async.rs @@ -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; @@ -28,7 +28,7 @@ //! let other_account = "rpc_docs.testnet".parse::()?; //! let rating = "4.5".parse::()?; //! -//! let transaction = Transaction { +//! let transaction = Transaction::V0(TransactionV0 { //! signer_id: signer.account_id.clone(), //! public_key: signer.public_key.clone(), //! nonce: 10223934 + 1, @@ -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(()) //! # } diff --git a/src/methods/broadcast_tx_commit.rs b/src/methods/broadcast_tx_commit.rs index c3bc79e..15166c6 100644 --- a/src/methods/broadcast_tx_commit.rs +++ b/src/methods/broadcast_tx_commit.rs @@ -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] @@ -29,7 +29,7 @@ //! let other_account = "rpc_docs.testnet".parse::()?; //! let rating = "4.5".parse::()?; //! -//! let transaction = Transaction { +//! let transaction = Transaction::V0(TransactionV0 { //! signer_id: signer.account_id.clone(), //! public_key: signer.public_key.clone(), //! nonce: 904565 + 1, @@ -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(()) //! # } diff --git a/src/methods/send_tx.rs b/src/methods/send_tx.rs index 8ce1ec7..decd370 100644 --- a/src/methods/send_tx.rs +++ b/src/methods/send_tx.rs @@ -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] @@ -30,7 +30,7 @@ //! let other_account = "rpc_docs.testnet".parse::()?; //! let rating = "4.5".parse::()?; //! -//! let transaction = Transaction { +//! let transaction = Transaction::V0(TransactionV0 { //! signer_id: signer.account_id.clone(), //! public_key: signer.public_key.clone(), //! nonce: 904565 + 1, @@ -47,10 +47,10 @@ //! gas: 100_000_000_000_000, // 100 TeraGas //! deposit: 0, //! }))], -//! }; +//! }); //! //! let request = methods::send_tx::RpcSendTransactionRequest { -//! signed_transaction: transaction.sign(&signer), +//! signed_transaction: transaction.sign(&near_crypto::Signer::InMemory(signer)), //! wait_until: TxExecutionStatus::IncludedFinal, //! }; //! # Ok(())