Skip to content

Commit

Permalink
Remove unwrap macro #839 (#842)
Browse files Browse the repository at this point in the history
* Remove unwrap! macro dependency.

* Fix after merge.
  • Loading branch information
artemii235 committed Mar 3, 2021
1 parent 4f378cd commit 7079405
Show file tree
Hide file tree
Showing 40 changed files with 2,367 additions and 2,402 deletions.
9 changes: 0 additions & 9 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ term = "=0.5.1"
tokio = { version = "0.2.22", features = ["io-util", "rt-threaded", "stream", "tcp"] }
trie-db = "0.22.1"
trie-root = "0.16.0"
unwrap = "1.2"
uuid = { version = "0.7", features = ["serde", "v4"] }
wasm-timer = "0.2.4"

Expand Down
1 change: 0 additions & 1 deletion mm2src/coins/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ sha2 = "0.8"
sha3 = "0.8"
tokio = { version = "0.2", optional = true }
tokio-rustls = { version = "0.14.1", optional = true }
unwrap = "1.2"
# One of web3 dependencies is the old `tokio-uds 0.1.7` which fails cross-compiling to ARM.
# We don't need the default web3 features at all since we added our own web3 transport using shared HYPER instance.
web3 = { git = "https://github.com/artemii235/rust-web3", default-features = false }
Expand Down
54 changes: 25 additions & 29 deletions mm2src/coins/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ const GAS_PRICE_APPROXIMATION_PERCENT_ON_TRADE_PREIMAGE: u64 = 7;
const APPROVE_GAS_LIMIT: u64 = 50_000;

lazy_static! {
pub static ref SWAP_CONTRACT: Contract = unwrap!(Contract::load(SWAP_CONTRACT_ABI.as_bytes()));
pub static ref ERC20_CONTRACT: Contract = unwrap!(Contract::load(ERC20_ABI.as_bytes()));
pub static ref SWAP_CONTRACT: Contract = Contract::load(SWAP_CONTRACT_ABI.as_bytes()).unwrap();
pub static ref ERC20_CONTRACT: Contract = Contract::load(ERC20_ABI.as_bytes()).unwrap();
}

#[derive(Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -239,10 +239,10 @@ impl EthCoinImpl {

/// Store ETH traces to local DB
fn store_eth_traces(&self, ctx: &MmArc, traces: &SavedTraces) {
let content = unwrap!(json::to_vec(traces));
let content = json::to_vec(traces).unwrap();
let tmp_file = format!("{}.tmp", self.eth_traces_path(&ctx).display());
unwrap!(std::fs::write(&tmp_file, content));
unwrap!(std::fs::rename(tmp_file, self.eth_traces_path(&ctx)));
std::fs::write(&tmp_file, content).unwrap();
std::fs::rename(tmp_file, self.eth_traces_path(&ctx)).unwrap();
}

fn erc20_events_path(&self, ctx: &MmArc) -> PathBuf {
Expand All @@ -253,10 +253,10 @@ impl EthCoinImpl {

/// Store ERC20 events to local DB
fn store_erc20_events(&self, ctx: &MmArc, events: &SavedErc20Events) {
let content = unwrap!(json::to_vec(events));
let content = json::to_vec(events).unwrap();
let tmp_file = format!("{}.tmp", self.erc20_events_path(&ctx).display());
unwrap!(std::fs::write(&tmp_file, content));
unwrap!(std::fs::rename(tmp_file, self.erc20_events_path(&ctx)));
std::fs::write(&tmp_file, content).unwrap();
std::fs::rename(tmp_file, self.erc20_events_path(&ctx)).unwrap();
}

/// Load saved ERC20 events from local DB
Expand Down Expand Up @@ -933,7 +933,7 @@ impl MarketCoinOps for EthCoin {
let selfi = self.clone();
let fut = async move {
loop {
if unwrap!(status.ms2deadline()) < 0 {
if status.ms2deadline().unwrap() < 0 {
status.append(" Timed out.");
return ERR!(
"Waited too long until {} for transaction {:?} confirmation ",
Expand Down Expand Up @@ -1699,7 +1699,7 @@ impl EthCoin {
break;
};
{
let coins_ctx = unwrap!(CoinsContext::from_ctx(&ctx));
let coins_ctx = CoinsContext::from_ctx(&ctx).unwrap();
let coins = block_on(coins_ctx.coins.lock());
if !coins.contains_key(&self.ticker) {
ctx.log.log("", &[&"tx_history", &self.ticker], "Loop stopped");
Expand Down Expand Up @@ -1728,7 +1728,7 @@ impl EthCoin {
latest_block: current_block,
},
};
*unwrap!(self.history_sync_state.lock()) = HistorySyncState::InProgress(json!({
*self.history_sync_state.lock().unwrap() = HistorySyncState::InProgress(json!({
"blocks_left": u64::from(saved_events.earliest_block),
}));

Expand Down Expand Up @@ -1952,11 +1952,9 @@ impl EthCoin {
},
};
let fee_details = match receipt {
Some(r) => Some(unwrap!(EthTxFeeDetails::new(
r.gas_used.unwrap_or_else(|| 0.into()),
web3_tx.gas_price,
"ETH"
))),
Some(r) => Some(
EthTxFeeDetails::new(r.gas_used.unwrap_or_else(|| 0.into()), web3_tx.gas_price, "ETH").unwrap(),
),
None => None,
};
let block_number = event.block_number.unwrap();
Expand Down Expand Up @@ -2012,7 +2010,7 @@ impl EthCoin {
b.block_height.cmp(&a.block_height)
}
});
self.save_history_to_file(&unwrap!(json::to_vec(&existing_history)), &ctx);
self.save_history_to_file(&json::to_vec(&existing_history).unwrap(), &ctx);
}
if saved_events.earliest_block == 0.into() {
if success_iteration == 0 {
Expand All @@ -2024,7 +2022,7 @@ impl EthCoin {
}

success_iteration += 1;
*unwrap!(self.history_sync_state.lock()) = HistorySyncState::Finished;
*self.history_sync_state.lock().unwrap() = HistorySyncState::Finished;
thread::sleep(Duration::from_secs(15));
} else {
thread::sleep(Duration::from_secs(2));
Expand All @@ -2048,7 +2046,7 @@ impl EthCoin {
break;
};
{
let coins_ctx = unwrap!(CoinsContext::from_ctx(&ctx));
let coins_ctx = CoinsContext::from_ctx(&ctx).unwrap();
let coins = block_on(coins_ctx.coins.lock());
if !coins.contains_key(&self.ticker) {
ctx.log.log("", &[&"tx_history", &self.ticker], "Loop stopped");
Expand Down Expand Up @@ -2077,7 +2075,7 @@ impl EthCoin {
latest_block: current_block,
},
};
*unwrap!(self.history_sync_state.lock()) = HistorySyncState::InProgress(json!({
*self.history_sync_state.lock().unwrap() = HistorySyncState::InProgress(json!({
"blocks_left": u64::from(saved_traces.earliest_block),
}));
let mut existing_history = self.load_history_from_file(ctx);
Expand Down Expand Up @@ -2278,11 +2276,9 @@ impl EthCoin {
},
};
let fee_details: Option<EthTxFeeDetails> = match receipt {
Some(r) => Some(unwrap!(EthTxFeeDetails::new(
r.gas_used.unwrap_or_else(|| 0.into()),
web3_tx.gas_price,
"ETH"
))),
Some(r) => Some(
EthTxFeeDetails::new(r.gas_used.unwrap_or_else(|| 0.into()), web3_tx.gas_price, "ETH").unwrap(),
),
None => None,
};

Expand Down Expand Up @@ -2314,7 +2310,7 @@ impl EthCoin {
.block(BlockId::Number(BlockNumber::Number(trace.block_number)))
.wait()
{
Ok(b) => unwrap!(b),
Ok(b) => b.unwrap(),
Err(e) => {
ctx.log.log(
"",
Expand Down Expand Up @@ -2351,7 +2347,7 @@ impl EthCoin {
b.block_height.cmp(&a.block_height)
}
});
self.save_history_to_file(&unwrap!(json::to_vec(&existing_history)), &ctx);
self.save_history_to_file(&json::to_vec(&existing_history).unwrap(), &ctx);
}
if saved_traces.earliest_block == 0.into() {
if success_iteration == 0 {
Expand All @@ -2363,7 +2359,7 @@ impl EthCoin {
}

success_iteration += 1;
*unwrap!(self.history_sync_state.lock()) = HistorySyncState::Finished;
*self.history_sync_state.lock().unwrap() = HistorySyncState::Finished;
thread::sleep(Duration::from_secs(15));
} else {
thread::sleep(Duration::from_secs(2));
Expand Down Expand Up @@ -2436,7 +2432,7 @@ impl MmCoin for EthCoin {
}
}

fn history_sync_status(&self) -> HistorySyncState { unwrap!(self.history_sync_state.lock()).clone() }
fn history_sync_status(&self) -> HistorySyncState { self.history_sync_state.lock().unwrap().clone() }

fn get_trade_fee(&self) -> Box<dyn Future<Item = TradeFee, Error = String> + Send> {
Box::new(self.get_gas_price().and_then(|gas_price| {
Expand Down
32 changes: 15 additions & 17 deletions mm2src/coins/eth/eth_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,13 +461,12 @@ fn test_search_for_swap_tx_spend_was_spent() {
243, 128, 2, 235, 208, 193, 192, 74, 208, 242, 26, 221, 83, 54, 74, 160, 111, 29, 92, 8, 75, 61, 97, 103, 199,
100, 189, 72, 74, 221, 144, 66, 170, 68, 121, 29, 105, 19, 194, 35, 245, 196, 131, 236, 29, 105, 101, 30,
];
let spend_tx = FoundSwapTxSpend::Spent(unwrap!(signed_eth_tx_from_bytes(&spend_tx)).into());
let spend_tx = FoundSwapTxSpend::Spent(signed_eth_tx_from_bytes(&spend_tx).unwrap().into());

let found_tx = unwrap!(unwrap!(coin.search_for_swap_tx_spend(
&payment_tx,
swap_contract_address,
6051857,
)));
let found_tx = coin
.search_for_swap_tx_spend(&payment_tx, swap_contract_address, 6051857)
.unwrap()
.unwrap();
assert_eq!(spend_tx, found_tx);
}

Expand Down Expand Up @@ -530,13 +529,12 @@ fn test_search_for_swap_tx_spend_was_refunded() {
19, 27, 208, 119, 219, 60, 231, 2, 118, 91, 169, 99, 78, 209, 135, 160, 51, 115, 90, 189, 124, 172, 205, 134,
203, 159, 238, 40, 39, 99, 88, 48, 160, 189, 37, 60, 20, 117, 65, 238, 36, 98, 226, 48, 22, 235, 86, 183,
];
let refund_tx = FoundSwapTxSpend::Refunded(unwrap!(signed_eth_tx_from_bytes(&refund_tx)).into());
let refund_tx = FoundSwapTxSpend::Refunded(signed_eth_tx_from_bytes(&refund_tx).unwrap().into());

let found_tx = unwrap!(unwrap!(coin.search_for_swap_tx_spend(
&payment_tx,
swap_contract_address,
5886908,
)));
let found_tx = coin
.search_for_swap_tx_spend(&payment_tx, swap_contract_address, 5886908)
.unwrap()
.unwrap();
assert_eq!(refund_tx, found_tx);
}

Expand All @@ -562,7 +560,7 @@ fn test_withdraw_impl_manual_fee() {
};
coin.my_balance().wait().unwrap();

let tx_details = unwrap!(block_on(withdraw_impl(ctx, coin.clone(), withdraw_req)));
let tx_details = block_on(withdraw_impl(ctx, coin.clone(), withdraw_req)).unwrap();
let expected = Some(
EthTxFeeDetails {
coin: "ETH".into(),
Expand Down Expand Up @@ -593,12 +591,12 @@ fn test_nonce_lock() {
}
let results = block_on(join_all(futures));
for result in results {
unwrap!(result);
result.unwrap();
}
// Waiting for NONCE_LOCK… might not appear at all if waiting takes less than 0.5 seconds
// but all transactions are sent successfully still
// unwrap!(wait_for_log(&ctx.log, 1.1, &|line| line.contains("Waiting for NONCE_LOCK…")));
unwrap!(wait_for_log(&ctx.log, 1.1, &|line| line.contains("get_addr_nonce…")));
// wait_for_log(&ctx.log, 1.1, &|line| line.contains("Waiting for NONCE_LOCK…")));
wait_for_log(&ctx.log, 1.1, &|line| line.contains("get_addr_nonce…")).unwrap();
}

#[cfg(feature = "w-bindgen")]
Expand All @@ -614,7 +612,7 @@ mod wasm_bindgen_tests {
use super::CoinsContext;
use common::mm_ctx::MmCtxBuilder;
let ctx = MmCtxBuilder::default().into_mm_arc();
let coins_context = unwrap!(CoinsContext::from_ctx(&ctx));
let coins_context = CoinsContext::from_ctx(&ctx).unwrap();
assert_eq!(1, 1);
}

Expand Down
9 changes: 4 additions & 5 deletions mm2src/coins/lp_coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#[macro_use] extern crate lazy_static;
#[macro_use] extern crate serde_derive;
#[macro_use] extern crate serde_json;
#[macro_use] extern crate unwrap;

use async_trait::async_trait;
use bigdecimal::BigDecimal;
Expand Down Expand Up @@ -551,7 +550,7 @@ pub trait MmCoin: SwapOps + MarketCoinOps + fmt::Debug + Send + Sync + 'static {
&[&"tx_history", &self.ticker().to_string()],
&ERRL!("Error {} on history deserialization, resetting the cache.", e),
);
unwrap!(std::fs::remove_file(&self.tx_history_path(&ctx)));
std::fs::remove_file(&self.tx_history_path(&ctx)).unwrap();
vec![]
},
}
Expand Down Expand Up @@ -1089,7 +1088,7 @@ pub fn my_tx_history(ctx: MmArc, req: Json) -> HyRes {
let history: Vec<Json> = history
.map(|item| {
let tx_block = item.block_height;
let mut json = unwrap!(json::to_value(item));
let mut json = json::to_value(item).unwrap();
json["confirmations"] = if tx_block == 0 {
Json::from(0)
} else if block_number >= tx_block {
Expand Down Expand Up @@ -1246,7 +1245,7 @@ pub async fn check_balance_update_loop(ctx: MmArc, ticker: String) {
Err(_) => continue,
};
if Some(&balance) != current_balance.as_ref() {
let coins_ctx = unwrap!(CoinsContext::from_ctx(&ctx));
let coins_ctx = CoinsContext::from_ctx(&ctx).unwrap();
coins_ctx.balance_updated(&coin, &balance).await;
current_balance = Some(balance);
}
Expand All @@ -1261,7 +1260,7 @@ pub async fn register_balance_update_handler(
ctx: MmArc,
handler: Box<dyn BalanceTradeFeeUpdatedHandler + Send + Sync>,
) {
let coins_ctx = unwrap!(CoinsContext::from_ctx(&ctx));
let coins_ctx = CoinsContext::from_ctx(&ctx).unwrap();
coins_ctx.balance_update_handlers.lock().await.push(handler);
}

Expand Down
4 changes: 2 additions & 2 deletions mm2src/coins/qrc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,10 @@ impl MutContractCallType {

fn as_function(&self) -> &'static Function {
match self {
MutContractCallType::Transfer => unwrap!(eth::ERC20_CONTRACT.function(self.as_function_name())),
MutContractCallType::Transfer => eth::ERC20_CONTRACT.function(self.as_function_name()).unwrap(),
MutContractCallType::Erc20Payment
| MutContractCallType::ReceiverSpend
| MutContractCallType::SenderRefund => unwrap!(eth::SWAP_CONTRACT.function(self.as_function_name())),
| MutContractCallType::SenderRefund => eth::SWAP_CONTRACT.function(self.as_function_name()).unwrap(),
}
}

Expand Down
Loading

0 comments on commit 7079405

Please sign in to comment.