Skip to content

Commit

Permalink
rewrite contract_change_method example
Browse files Browse the repository at this point in the history
  • Loading branch information
telezhnaya committed Jan 16, 2024
1 parent 86ee0d9 commit 58a2c0b
Showing 1 changed file with 8 additions and 35 deletions.
43 changes: 8 additions & 35 deletions examples/contract_change_method.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use near_jsonrpc_client::{methods, JsonRpcClient};
use near_jsonrpc_primitives::types::query::QueryResponseKind;
use near_jsonrpc_primitives::types::transactions::TransactionInfo;
use near_primitives::transaction::{Action, FunctionCallAction, Transaction};
use near_primitives::types::BlockReference;
use near_primitives::views::TxExecutionStatus;

use serde_json::json;
use tokio::time;
Expand Down Expand Up @@ -57,44 +57,17 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}))],
};

let request = methods::broadcast_tx_async::RpcBroadcastTxAsyncRequest {
let request = methods::send_tx::RpcSendTransactionRequest {
signed_transaction: transaction.sign(&signer),
wait_until: TxExecutionStatus::Executed,
};

let sent_at = time::Instant::now();
let tx_hash = client.call(request).await?;

loop {
let response = client
.call(methods::tx::RpcTransactionStatusRequest {
transaction_info: TransactionInfo::TransactionId {
tx_hash,
sender_account_id: signer.account_id.clone(),
},
})
.await;
let received_at = time::Instant::now();
let delta = (received_at - sent_at).as_secs();

if delta > 60 {
Err("time limit exceeded for the transaction to be recognized")?;
}

match response {
Err(err) => match err.handler_error() {
Some(methods::tx::RpcTransactionError::UnknownTransaction { .. }) => {
time::sleep(time::Duration::from_secs(2)).await;
continue;
}
_ => Err(err)?,
},
Ok(response) => {
println!("response gotten after: {}s", delta);
println!("response: {:#?}", response);
break;
}
}
}
let response = client.call(request).await?;
let received_at = time::Instant::now();
let delta = (received_at - sent_at).as_secs();
println!("response gotten after: {}s", delta);
println!("response: {:#?}", response);

Ok(())
}

0 comments on commit 58a2c0b

Please sign in to comment.