-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathcontract_change_method_commit.rs
More file actions
68 lines (54 loc) · 2.36 KB
/
contract_change_method_commit.rs
File metadata and controls
68 lines (54 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use near_jsonrpc_client::{methods, JsonRpcClient};
use near_jsonrpc_primitives::types::query::QueryResponseKind;
use near_primitives::gas::Gas;
use near_primitives::transaction::{Action, FunctionCallAction, Transaction, TransactionV0};
use near_primitives::types::{Balance, BlockReference};
use serde_json::json;
mod utils;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
let client = JsonRpcClient::connect("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.get_account_id(),
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 = TransactionV0 {
signer_id: signer.get_account_id(),
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: Gas::from_teragas(100),
deposit: Balance::ZERO,
}))],
};
let request = methods::broadcast_tx_commit::RpcBroadcastTxCommitRequest {
signed_transaction: Transaction::V0(transaction).sign(&signer),
};
let response = client.call(request).await?;
println!("response: {:#?}", response);
Ok(())
}