Skip to content

Commit

Permalink
[cleanup] leverage collect() to eliminate error cases in transaction_…
Browse files Browse the repository at this point in the history
…builder

Rust's `collect` is quite powerful--it will turn a `Vec<Result<T>>` into a `Result<Vec<T>>` for you, which usually leads to much shorter code.

Leverage this in a few places in the tx builder code.
  • Loading branch information
sblackshear committed Nov 6, 2022
1 parent 37bad27 commit 2240d42
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 45 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/sui-transaction-builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ anyhow = "1.0.64"
async-trait = "0.1.57"
futures = "0.3.23"
bcs = "0.1.4"
tracing = "0.1.36"

sui-json-rpc-types= { path = "../sui-json-rpc-types" }
sui-types = { path = "../sui-types" }
Expand Down
53 changes: 10 additions & 43 deletions crates/sui-transaction-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use futures::future::join_all;
use anyhow::anyhow;
use move_core_types::identifier::Identifier;
use move_core_types::language_storage::TypeTag;
use tracing::debug;

use sui_adapter::adapter::resolve_and_type_check;
use sui_json::{resolve_move_function_args, SuiJsonCallArg, SuiJsonValue};
Expand Down Expand Up @@ -137,21 +136,10 @@ impl TransactionBuilder {
.iter()
.map(|id| self.get_object_ref(*id))
.collect();
let coins: Vec<anyhow::Result<ObjectRef>> = join_all(handles).await.into_iter().collect();
let coins_len = coins.len();
let coin_refs: Vec<ObjectRef> = coins
let coin_refs = join_all(handles)
.await
.into_iter()
.map(|c| {
c.map_err(|e| {
debug!("Failed reading Pay input coin with err {:?}", e);
e
})
})
.filter_map(|c| c.ok())
.collect();
if coins_len != coin_refs.len() {
return Err(anyhow!("Failed reading Pay input coin objects!"));
}
.collect::<anyhow::Result<Vec<ObjectRef>>>()?;
let gas = self
.select_gas(signer, gas, gas_budget, input_coins)
.await?;
Expand All @@ -174,21 +162,10 @@ impl TransactionBuilder {
.into_iter()
.map(|id| self.get_object_ref(id))
.collect();
let coins: Vec<anyhow::Result<ObjectRef>> = join_all(handles).await.into_iter().collect();
let coins_len = coins.len();
let coin_refs: Vec<ObjectRef> = coins
let coin_refs = join_all(handles)
.await
.into_iter()
.map(|c| {
c.map_err(|e| {
debug!("Failed reading PaySui input coin with err {:?}", e);
e
})
})
.filter_map(|c| c.ok())
.collect();
if coins_len != coin_refs.len() {
return Err(anyhow!("Failed reading PaySui input coin objects!"));
}
.collect::<anyhow::Result<Vec<ObjectRef>>>()?;
// [0] is safe because input_coins is non-empty and coins are of same length as input_coins.
let gas_object_ref = coin_refs[0];
Ok(TransactionData::new_pay_sui(
Expand All @@ -214,21 +191,11 @@ impl TransactionBuilder {
.into_iter()
.map(|id| self.get_object_ref(id))
.collect();
let coins: Vec<anyhow::Result<ObjectRef>> = join_all(handles).await.into_iter().collect();
let coins_len = coins.len();
let coin_refs: Vec<ObjectRef> = coins

let coin_refs = join_all(handles)
.await
.into_iter()
.map(|c| {
c.map_err(|e| {
debug!("Failed reading PayAllSui input coin with err {:?}", e);
e
})
})
.filter_map(|c| c.ok())
.collect();
if coins_len != coin_refs.len() {
return Err(anyhow!("Failed reading PayAllSui input coin objects!"));
}
.collect::<anyhow::Result<Vec<ObjectRef>>>()?;
// [0] is safe because input_coins is non-empty and coins are of same length as input_coins.
let gas_object_ref = coin_refs[0];
Ok(TransactionData::new_pay_all_sui(
Expand Down

0 comments on commit 2240d42

Please sign in to comment.