Skip to content

Commit

Permalink
Merge pull request #834 from KomodoPlatform/electrum-enable-mature-co…
Browse files Browse the repository at this point in the history
…nfirmations

Add mature_confirmations field to the electrum and enable RPC responses
  • Loading branch information
artemii235 authored Feb 26, 2021
2 parents bf975ca + c42d027 commit dbd3a1b
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 21 deletions.
2 changes: 2 additions & 0 deletions mm2src/coins/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2584,6 +2584,8 @@ impl MmCoin for EthCoin {
fn swap_contract_address(&self) -> Option<BytesJson> {
Some(BytesJson::from(self.swap_contract_address.0.as_ref()))
}

fn mature_confirmations(&self) -> Option<u32> { None }
}

pub trait TryToAddress {
Expand Down
3 changes: 3 additions & 0 deletions mm2src/coins/lp_coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,9 @@ pub trait MmCoin: SwapOps + MarketCoinOps + fmt::Debug + Send + Sync + 'static {

/// Get swap contract address if the coin uses it in Atomic Swaps.
fn swap_contract_address(&self) -> Option<BytesJson>;

/// The minimum number of confirmations at which a transaction is considered mature.
fn mature_confirmations(&self) -> Option<u32>;
}

#[derive(Clone, Debug)]
Expand Down
2 changes: 2 additions & 0 deletions mm2src/coins/qrc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,8 @@ impl MmCoin for Qrc20Coin {
fn swap_contract_address(&self) -> Option<BytesJson> {
Some(BytesJson::from(self.swap_contract_address.0.as_ref()))
}

fn mature_confirmations(&self) -> Option<u32> { Some(self.utxo.conf.mature_confirmations) }
}

pub fn qrc20_swap_id(time_lock: u32, secret_hash: &[u8]) -> Vec<u8> {
Expand Down
2 changes: 2 additions & 0 deletions mm2src/coins/test_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,6 @@ impl MmCoin for TestCoin {
fn my_unspendable_balance(&self) -> Box<dyn Future<Item = BigDecimal, Error = String> + Send> { unimplemented!() }

fn swap_contract_address(&self) -> Option<BytesJson> { unimplemented!() }

fn mature_confirmations(&self) -> Option<u32> { unimplemented!() }
}
2 changes: 1 addition & 1 deletion mm2src/coins/utxo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ pub struct UtxoCoinConf {
/// Block count for median time past calculation
pub mtp_block_count: NonZeroU64,
pub estimate_fee_mode: Option<EstimateFeeMode>,
/// Minimum number of confirmations at which a transaction is considered mature
/// The minimum number of confirmations at which a transaction is considered mature
pub mature_confirmations: u32,
/// The number of blocks used for estimate_fee/estimate_smart_fee RPC calls
pub estimate_fee_blocks: u32,
Expand Down
2 changes: 2 additions & 0 deletions mm2src/coins/utxo/qtum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,8 @@ impl MmCoin for QtumCoin {
}

fn swap_contract_address(&self) -> Option<BytesJson> { utxo_common::swap_contract_address() }

fn mature_confirmations(&self) -> Option<u32> { Some(self.utxo_arc.conf.mature_confirmations) }
}

/// Parse contract address (H160) from string.
Expand Down
2 changes: 2 additions & 0 deletions mm2src/coins/utxo/utxo_standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,4 +457,6 @@ impl MmCoin for UtxoStandardCoin {
}

fn swap_contract_address(&self) -> Option<BytesJson> { utxo_common::swap_contract_address() }

fn mature_confirmations(&self) -> Option<u32> { Some(self.utxo_arc.conf.mature_confirmations) }
}
9 changes: 5 additions & 4 deletions mm2src/mm2_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2260,12 +2260,10 @@ fn test_show_priv_key() {
#[test]
#[cfg(feature = "native")]
// https://github.com/KomodoPlatform/atomicDEX-API/issues/586
fn electrum_and_enable_required_confirmations_and_nota() {
fn test_electrum_and_enable_response() {
let coins = json! ([
{"coin":"RICK","asset":"RICK","rpcport":8923,"txversion":4,"overwintered":1,"protocol":{"type":"UTXO"}},
{"coin":"MORTY","asset":"MORTY","rpcport":11608,"txversion":4,"overwintered":1,"protocol":{"type":"UTXO"}},
{"coin":"RICK","asset":"RICK","rpcport":8923,"txversion":4,"overwintered":1,"protocol":{"type":"UTXO"},"mature_confirmations":101},
{"coin":"ETH","name":"ethereum","protocol":{"type":"ETH"}},
{"coin":"JST","name":"jst","protocol":{"type":"ERC20","protocol_data":{"platform":"ETH","contract_address":"0x2b294F029Fde858b2c62184e8390591755521d8E"}}}
]);

let mut mm = unwrap!(MarketMakerIt::start(
Expand Down Expand Up @@ -2312,6 +2310,7 @@ fn electrum_and_enable_required_confirmations_and_nota() {
let rick_response: Json = unwrap!(json::from_str(&electrum_rick.1));
assert_eq!(rick_response["required_confirmations"], Json::from(10));
assert_eq!(rick_response["requires_notarization"], Json::from(true));
assert_eq!(rick_response["mature_confirmations"], Json::from(101));

// should change requires notarization at runtime
let requires_nota_rick = unwrap!(block_on(mm.rpc(json! ({
Expand Down Expand Up @@ -2355,6 +2354,8 @@ fn electrum_and_enable_required_confirmations_and_nota() {
assert_eq!(eth_response["required_confirmations"], Json::from(10));
// requires_notarization doesn't take any effect on ETH/ERC20 coins
assert_eq!(eth_response["requires_notarization"], Json::from(false));
// check if there is no `mature_confirmations` field
assert_eq!(eth_response.get("mature_confirmations"), None);
}

fn check_too_low_volume_order_creation_fails(mm: &MarketMakerIt, base: &str, rel: &str) {
Expand Down
47 changes: 31 additions & 16 deletions mm2src/rpc/lp_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#![cfg_attr(not(feature = "native"), allow(dead_code))]
#![cfg_attr(not(feature = "native"), allow(unused_imports))]

use bigdecimal::BigDecimal;
use coins::{disable_coin as disable_coin_impl, lp_coinfind, lp_coininit, MmCoinEnum};
use common::executor::{spawn, Timer};
use common::mm_ctx::MmArc;
Expand Down Expand Up @@ -78,19 +79,32 @@ pub async fn disable_coin(ctx: MmArc, req: Json) -> Result<Response<Vec<u8>>, St
.map_err(|e| ERRL!("{}", e))
}

#[derive(Serialize)]
struct CoinInitResponse<'a> {
result: &'a str,
address: String,
balance: BigDecimal,
coin: &'a str,
required_confirmations: u64,
requires_notarization: bool,
#[serde(skip_serializing_if = "Option::is_none")]
mature_confirmations: Option<u32>,
}

/// Enable a coin in the Electrum mode.
pub async fn electrum(ctx: MmArc, req: Json) -> Result<Response<Vec<u8>>, String> {
let ticker = try_s!(req["coin"].as_str().ok_or("No 'coin' field")).to_owned();
let coin: MmCoinEnum = try_s!(lp_coininit(&ctx, &ticker, &req).await);
let balance = try_s!(coin.my_balance().compat().await);
let res = json! ({
"result": "success",
"address": try_s!(coin.my_address()),
"balance": balance,
"coin": coin.ticker(),
"required_confirmations": coin.required_confirmations(),
"requires_notarization": coin.requires_notarization(),
});
let res = CoinInitResponse {
result: "success",
address: try_s!(coin.my_address()),
balance,
coin: coin.ticker(),
required_confirmations: coin.required_confirmations(),
requires_notarization: coin.requires_notarization(),
mature_confirmations: coin.mature_confirmations(),
};
let res = try_s!(json::to_vec(&res));
Ok(try_s!(Response::builder().body(res)))
}
Expand All @@ -100,14 +114,15 @@ pub async fn enable(ctx: MmArc, req: Json) -> Result<Response<Vec<u8>>, String>
let ticker = try_s!(req["coin"].as_str().ok_or("No 'coin' field")).to_owned();
let coin: MmCoinEnum = try_s!(lp_coininit(&ctx, &ticker, &req).await);
let balance = try_s!(coin.my_balance().compat().await);
let res = json! ({
"result": "success",
"address": try_s!(coin.my_address()),
"balance": balance,
"coin": coin.ticker(),
"required_confirmations": coin.required_confirmations(),
"requires_notarization": coin.requires_notarization(),
});
let res = CoinInitResponse {
result: "success",
address: try_s!(coin.my_address()),
balance,
coin: coin.ticker(),
required_confirmations: coin.required_confirmations(),
requires_notarization: coin.requires_notarization(),
mature_confirmations: coin.mature_confirmations(),
};
let res = try_s!(json::to_vec(&res));
Ok(try_s!(Response::builder().body(res)))
}
Expand Down

0 comments on commit dbd3a1b

Please sign in to comment.