From 5a3ed24f7d02c2f73d0d5ece4d037d6227566670 Mon Sep 17 00:00:00 2001 From: Phuong Nguyen Date: Mon, 31 Oct 2022 15:03:49 -0700 Subject: [PATCH] Status Result --- CHANGELOG.md | 1 + workspaces/src/operations.rs | 28 +++++++++++----------------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e7a4d0c8..f723e20c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Added - [`view_*` asynchronous builders have been added which provides being able to query from a specific block hash or block height](https://github.com/near/workspaces-rs/pull/218) +- [`{CallTransaction, Transaction}::transact_async` for performing transactions without directly having to wait for it complete it on chain](https://github.com/near/workspaces-rs/pull/222) ### Changed diff --git a/workspaces/src/operations.rs b/workspaces/src/operations.rs index a329aa6c..57c3eda8 100644 --- a/workspaces/src/operations.rs +++ b/workspaces/src/operations.rs @@ -23,6 +23,7 @@ use near_primitives::transaction::{ use near_primitives::views::FinalExecutionOutcomeView; use std::convert::TryInto; use std::future::IntoFuture; +use std::task::Poll; const MAX_GAS: Gas = 300_000_000_000_000; @@ -458,9 +459,10 @@ impl<'a> TransactionStatus<'a> { } } - /// Checks the status of the transaction. If an [`TransactionPoll::Pending`] is returned, then the - /// transaction has not completed yet. - pub async fn status(&self) -> TransactionPoll { + /// Checks the status of the transaction. If an `Err` is returned, then the transaction + /// is in an unexpected state. The error should have further context. Otherwise, if an + /// `Ok` value with [`Poll::Pending`] is returned, then the transaction has not finished. + pub async fn status(&self) -> Result> { let result = self .client .tx_async_status( @@ -471,12 +473,12 @@ impl<'a> TransactionStatus<'a> { .map(ExecutionFinalResult::from_view); match result { - Ok(result) => TransactionPoll::Ready(result), + Ok(result) => Ok(Poll::Ready(result)), Err(err) => match err { JsonRpcError::ServerError(JsonRpcServerError::HandlerError( RpcTransactionError::UnknownTransaction { .. }, - )) => TransactionPoll::Pending, - other => TransactionPoll::Error(RpcErrorCode::BroadcastTxFailure.custom(other)), + )) => Ok(Poll::Pending), + other => Err(RpcErrorCode::BroadcastTxFailure.custom(other)), }, } } @@ -484,10 +486,9 @@ impl<'a> TransactionStatus<'a> { /// Wait until the completion of the transaction by polling [`TransactionStatus::status`]. pub(crate) async fn wait(self) -> Result { loop { - match self.status().await { - TransactionPoll::Ready(val) => break Ok(val), - TransactionPoll::Error(err) => break Err(err), - TransactionPoll::Pending => (), + match self.status().await? { + Poll::Ready(val) => break Ok(val), + Poll::Pending => (), } tokio::time::sleep(std::time::Duration::from_millis(300)).await; @@ -505,13 +506,6 @@ impl<'a> TransactionStatus<'a> { } } -#[derive(Debug)] -pub enum TransactionPoll { - Ready(T), - Pending, - Error(Error), -} - impl<'a> IntoFuture for TransactionStatus<'a> { type Output = Result; type IntoFuture = BoxFuture<'a, Self::Output>;