Skip to content

Commit

Permalink
Allow to set estimate_fee_mode in config #651
Browse files Browse the repository at this point in the history
  • Loading branch information
artemii235 committed Jun 1, 2020
1 parent 4f54c3c commit 8496d71
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
7 changes: 5 additions & 2 deletions mm2src/coins/utxo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ use std::time::Duration;

pub use chain::Transaction as UtxoTx;

use self::rpc_clients::{electrum_script_hash, ElectrumClient, ElectrumClientImpl, EstimateFeeMethod, NativeClient, UtxoRpcClientEnum, UnspentInfo};
use self::rpc_clients::{electrum_script_hash, ElectrumClient, ElectrumClientImpl,
EstimateFeeMethod, EstimateFeeMode, NativeClient, UtxoRpcClientEnum, UnspentInfo};
use super::{CoinsContext, CoinTransportMetrics, FoundSwapTxSpend, HistorySyncState, MarketCoinOps, MmCoin, RpcClientType, RpcTransportEventHandlerShared,
SwapOps, TradeFee, TradeInfo, Transaction, TransactionEnum, TransactionFut, TransactionDetails, WithdrawFee, WithdrawRequest};
use crate::utxo::rpc_clients::{NativeClientImpl, UtxoRpcClientOps, ElectrumRpcRequest};
Expand Down Expand Up @@ -226,14 +227,15 @@ pub struct UtxoCoinImpl { // pImpl idiom.
/// relay fee amount instead of calculated
/// https://github.com/KomodoPlatform/atomicDEX-API/issues/617
force_min_relay_fee: bool,
estimate_fee_mode: Option<EstimateFeeMode>,
}

impl UtxoCoinImpl {
async fn get_tx_fee(&self) -> Result<ActualTxFee, JsonRpcError> {
match &self.tx_fee {
TxFee::Fixed(fee) => Ok(ActualTxFee::Fixed(*fee)),
TxFee::Dynamic(method) => {
let fee = self.rpc_client.estimate_fee_sat(self.decimals, method).compat().await?;
let fee = self.rpc_client.estimate_fee_sat(self.decimals, method, &self.estimate_fee_mode).compat().await?;
Ok(ActualTxFee::Dynamic(fee))
},
}
Expand Down Expand Up @@ -2125,6 +2127,7 @@ pub async fn utxo_coin_from_conf_and_request(
history_sync_state: Mutex::new(initial_history_state),
required_confirmations: required_confirmations.into(),
force_min_relay_fee: conf["force_min_relay_fee"].as_bool().unwrap_or (false),
estimate_fee_mode: json::from_value(conf["estimate_fee_mode"].clone()).unwrap_or(None),
};
Ok(UtxoCoin(Arc::new(coin)))
}
Expand Down
27 changes: 17 additions & 10 deletions mm2src/coins/utxo/rpc_clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ pub trait UtxoRpcClientOps: fmt::Debug + Send + Sync + 'static {
fn display_balance(&self, address: Address, decimals: u8) -> RpcRes<BigDecimal>;

/// returns fee estimation per KByte in satoshis
fn estimate_fee_sat(&self, decimals: u8, fee_method: &EstimateFeeMethod) -> RpcRes<u64>;
fn estimate_fee_sat(&self, decimals: u8, fee_method: &EstimateFeeMethod, mode: &Option<EstimateFeeMode>) -> RpcRes<u64>;

fn get_relay_fee(&self) -> RpcRes<BigDecimal>;

Expand Down Expand Up @@ -433,7 +433,7 @@ impl UtxoRpcClientOps for NativeClient {
))
}

fn estimate_fee_sat(&self, decimals: u8, fee_method: &EstimateFeeMethod) -> RpcRes<u64> {
fn estimate_fee_sat(&self, decimals: u8, fee_method: &EstimateFeeMethod, mode: &Option<EstimateFeeMode>) -> RpcRes<u64> {
match fee_method {
EstimateFeeMethod::Standard => Box::new(self.estimate_fee().map(move |fee|
if fee > 0.00001 {
Expand All @@ -442,7 +442,7 @@ impl UtxoRpcClientOps for NativeClient {
1000
}
)),
EstimateFeeMethod::SmartFee => Box::new(self.estimate_smart_fee().map(move |res|
EstimateFeeMethod::SmartFee => Box::new(self.estimate_smart_fee(mode).map(move |res|
if res.fee_rate > 0.00001 {
(res.fee_rate * 10.0_f64.powf(decimals as f64)) as u64
} else {
Expand Down Expand Up @@ -538,9 +538,9 @@ impl NativeClientImpl {

/// https://bitcoincore.org/en/doc/0.18.0/rpc/util/estimatesmartfee/
/// Always estimate fee for transaction to be confirmed in next block
pub fn estimate_smart_fee(&self) -> RpcRes<EstimateSmartFeeRes> {
pub fn estimate_smart_fee(&self, mode: &Option<EstimateFeeMode>) -> RpcRes<EstimateSmartFeeRes> {
let n_blocks = 1;
rpc_func!(self, "estimatesmartfee", n_blocks)
rpc_func!(self, "estimatesmartfee", n_blocks, mode)
}

/// https://bitcoin.org/en/developer-reference#listtransactions
Expand All @@ -557,7 +557,7 @@ impl NativeClientImpl {

pub fn detect_fee_method(&self) -> impl Future<Item=EstimateFeeMethod, Error=String> + Send {
let estimate_fee_fut = self.estimate_fee();
self.estimate_smart_fee().then(move |res| -> Box<dyn Future<Item=EstimateFeeMethod, Error=String> + Send> {
self.estimate_smart_fee(&None).then(move |res| -> Box<dyn Future<Item=EstimateFeeMethod, Error=String> + Send> {
match res {
Ok(smart_fee) => if smart_fee.fee_rate > 0. {
Box::new(futures01::future::ok(EstimateFeeMethod::SmartFee))
Expand Down Expand Up @@ -646,6 +646,13 @@ pub enum ElectrumBlockHeader {
V14(ElectrumBlockHeaderV14),
}

#[derive(Debug, Deserialize, Serialize)]
pub enum EstimateFeeMode {
ECONOMICAL,
CONSERVATIVE,
UNSET,
}

impl ElectrumBlockHeader {
fn block_height(&self) -> u64 {
match self {
Expand Down Expand Up @@ -1026,9 +1033,9 @@ impl ElectrumClient {

/// https://electrumx.readthedocs.io/en/latest/protocol-methods.html#blockchain-estimatefee
/// Always estimate fee for transaction to be confirmed in next block
fn estimate_fee(&self) -> RpcRes<f64> {
fn estimate_fee(&self, mode: &Option<EstimateFeeMode>) -> RpcRes<f64> {
let n_blocks = 1;
rpc_func!(self, "blockchain.estimatefee", n_blocks)
rpc_func!(self, "blockchain.estimatefee", n_blocks, mode)
}
}

Expand Down Expand Up @@ -1121,8 +1128,8 @@ impl UtxoRpcClientOps for ElectrumClient {
}))
}

fn estimate_fee_sat(&self, decimals: u8, _fee_method: &EstimateFeeMethod) -> RpcRes<u64> {
Box::new(self.estimate_fee().map(move |fee|
fn estimate_fee_sat(&self, decimals: u8, _fee_method: &EstimateFeeMethod, mode: &Option<EstimateFeeMode>) -> RpcRes<u64> {
Box::new(self.estimate_fee(mode).map(move |fee|
if fee > 0.00001 {
(fee * 10.0_f64.powf(decimals as f64)) as u64
} else {
Expand Down
1 change: 1 addition & 0 deletions mm2src/coins/utxo/utxo_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ fn utxo_coin_for_test(rpc_client: UtxoRpcClientEnum, force_seed: Option<&str>) -
history_sync_state: Mutex::new(HistorySyncState::NotEnabled),
required_confirmations: 1.into(),
force_min_relay_fee: false,
estimate_fee_mode: None,
};
coin
}
Expand Down

0 comments on commit 8496d71

Please sign in to comment.