Skip to content

Commit

Permalink
Status Result<Poll>
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaoticTempest committed Oct 31, 2022
1 parent 21b9c15 commit 5a3ed24
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 11 additions & 17 deletions workspaces/src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<ExecutionFinalResult> {
/// 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<Poll<ExecutionFinalResult>> {
let result = self
.client
.tx_async_status(
Expand All @@ -471,23 +473,22 @@ 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)),
},
}
}

/// Wait until the completion of the transaction by polling [`TransactionStatus::status`].
pub(crate) async fn wait(self) -> Result<ExecutionFinalResult> {
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;
Expand All @@ -505,13 +506,6 @@ impl<'a> TransactionStatus<'a> {
}
}

#[derive(Debug)]
pub enum TransactionPoll<T> {
Ready(T),
Pending,
Error(Error),
}

impl<'a> IntoFuture for TransactionStatus<'a> {
type Output = Result<ExecutionFinalResult>;
type IntoFuture = BoxFuture<'a, Self::Output>;
Expand Down

0 comments on commit 5a3ed24

Please sign in to comment.