contract_change_method_commit/
contract_change_method_commit.rs

1use near_api_lib::primitives::transaction::{Action, FunctionCallAction, Transaction};
2use near_api_lib::primitives::types::BlockReference;
3use near_api_lib::providers::jsonrpc_client::{methods, JsonRpcClient};
4use near_api_lib::providers::types::query::QueryResponseKind;
5
6use near_api_lib::JsonRpcProvider;
7
8// items from traits can only be used if the trait is in scope
9// can we change it somehow with better crate design?
10use near_api_lib::providers::Provider;
11
12use serde_json::json;
13
14mod utils;
15
16#[tokio::main]
17async fn main() -> Result<(), Box<dyn std::error::Error>> {
18    env_logger::init();
19
20    let client = JsonRpcClient::connect("https://rpc.testnet.near.org");
21
22    let provider = JsonRpcProvider::new("https://rpc.testnet.near.org");
23
24    let signer_account_id = utils::input("Enter the signer Account ID: ")?.parse()?;
25    let signer_secret_key = utils::input("Enter the signer's private key: ")?.parse()?;
26
27    let signer = near_crypto::InMemorySigner::from_secret_key(signer_account_id, signer_secret_key);
28
29    let access_key_query_response = client
30        .call(methods::query::RpcQueryRequest {
31            block_reference: BlockReference::latest(),
32            request: near_primitives::views::QueryRequest::ViewAccessKey {
33                account_id: signer.account_id.clone(),
34                public_key: signer.public_key.clone(),
35            },
36        })
37        .await?;
38
39    let current_nonce = match access_key_query_response.kind {
40        QueryResponseKind::AccessKey(access_key) => access_key.nonce,
41        _ => Err("failed to extract current nonce")?,
42    };
43
44    let other_account = utils::input("Enter the account to be rated: ")?;
45    let rating = utils::input("Enter a rating: ")?.parse::<f32>()?;
46
47    let transaction = Transaction {
48        signer_id: signer.account_id.clone(),
49        public_key: signer.public_key.clone(),
50        nonce: current_nonce + 1,
51        receiver_id: "nosedive.testnet".parse()?,
52        block_hash: access_key_query_response.block_hash,
53        actions: vec![Action::FunctionCall(Box::new(FunctionCallAction {
54            method_name: "rate".to_string(),
55            args: json!({
56                "account_id": other_account,
57                "rating": rating,
58            })
59            .to_string()
60            .into_bytes(),
61            gas: 100_000_000_000_000, // 100 TeraGas
62            deposit: 0,
63        }))],
64    };
65
66    let response = provider.send_transaction(transaction.sign(&signer)).await?;
67
68    println!("response: {:#?}", response);
69
70    Ok(())
71}