Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(hd-wallet): use CoinBalanceMap for UTXO and QTUM #2259

Merged
merged 7 commits into from
Nov 4, 2024
4 changes: 2 additions & 2 deletions mm2src/coins/rpc_command/account_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ pub async fn account_balance(
req: HDAccountBalanceRequest,
) -> MmResult<HDAccountBalanceResponseEnum, HDAccountBalanceRpcError> {
match lp_coinfind_or_err(&ctx, &req.coin).await? {
MmCoinEnum::UtxoCoin(utxo) => Ok(HDAccountBalanceResponseEnum::Single(
mariocynicys marked this conversation as resolved.
Show resolved Hide resolved
MmCoinEnum::UtxoCoin(utxo) => Ok(HDAccountBalanceResponseEnum::Map(
utxo.account_balance_rpc(req.params).await?,
)),
MmCoinEnum::QtumCoin(qtum) => Ok(HDAccountBalanceResponseEnum::Single(
MmCoinEnum::QtumCoin(qtum) => Ok(HDAccountBalanceResponseEnum::Map(
qtum.account_balance_rpc(req.params).await?,
)),
MmCoinEnum::EthCoin(eth) => Ok(HDAccountBalanceResponseEnum::Map(
Expand Down
8 changes: 4 additions & 4 deletions mm2src/coins/rpc_command/get_new_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ impl RpcTask for InitGetNewAddressTask {
}

match self.coin {
MmCoinEnum::UtxoCoin(ref utxo) => Ok(GetNewAddressResponseEnum::Single(
MmCoinEnum::UtxoCoin(ref utxo) => Ok(GetNewAddressResponseEnum::Map(
get_new_address_helper(
&self.ctx,
utxo,
Expand All @@ -330,7 +330,7 @@ impl RpcTask for InitGetNewAddressTask {
)
.await?,
)),
MmCoinEnum::QtumCoin(ref qtum) => Ok(GetNewAddressResponseEnum::Single(
MmCoinEnum::QtumCoin(ref qtum) => Ok(GetNewAddressResponseEnum::Map(
get_new_address_helper(
&self.ctx,
qtum,
Expand Down Expand Up @@ -362,10 +362,10 @@ pub async fn get_new_address(
) -> MmResult<GetNewAddressResponseEnum, GetNewAddressRpcError> {
let coin = lp_coinfind_or_err(&ctx, &req.coin).await?;
match coin {
MmCoinEnum::UtxoCoin(utxo) => Ok(GetNewAddressResponseEnum::Single(
MmCoinEnum::UtxoCoin(utxo) => Ok(GetNewAddressResponseEnum::Map(
utxo.get_new_address_rpc_without_conf(req.params).await?,
)),
MmCoinEnum::QtumCoin(qtum) => Ok(GetNewAddressResponseEnum::Single(
MmCoinEnum::QtumCoin(qtum) => Ok(GetNewAddressResponseEnum::Map(
qtum.get_new_address_rpc_without_conf(req.params).await?,
)),
MmCoinEnum::EthCoin(eth) => Ok(GetNewAddressResponseEnum::Map(
Expand Down
4 changes: 2 additions & 2 deletions mm2src/coins/rpc_command/init_account_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ impl RpcTask for InitAccountBalanceTask {
_task_handle: InitAccountBalanceTaskHandleShared,
) -> Result<Self::Item, MmError<Self::Error>> {
match self.coin {
MmCoinEnum::UtxoCoin(ref utxo) => Ok(HDAccountBalanceEnum::Single(
MmCoinEnum::UtxoCoin(ref utxo) => Ok(HDAccountBalanceEnum::Map(
utxo.init_account_balance_rpc(self.req.params.clone()).await?,
)),
MmCoinEnum::QtumCoin(ref qtum) => Ok(HDAccountBalanceEnum::Single(
MmCoinEnum::QtumCoin(ref qtum) => Ok(HDAccountBalanceEnum::Map(
qtum.init_account_balance_rpc(self.req.params.clone()).await?,
)),
MmCoinEnum::EthCoin(ref eth) => Ok(HDAccountBalanceEnum::Map(
Expand Down
4 changes: 2 additions & 2 deletions mm2src/coins/rpc_command/init_create_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ impl RpcTask for InitCreateAccountTask {
}

match self.coin {
MmCoinEnum::UtxoCoin(ref utxo) => Ok(HDAccountBalanceEnum::Single(
MmCoinEnum::UtxoCoin(ref utxo) => Ok(HDAccountBalanceEnum::Map(
create_new_account_helper(
&self.ctx,
utxo,
Expand All @@ -298,7 +298,7 @@ impl RpcTask for InitCreateAccountTask {
)
.await?,
)),
MmCoinEnum::QtumCoin(ref qtum) => Ok(HDAccountBalanceEnum::Single(
MmCoinEnum::QtumCoin(ref qtum) => Ok(HDAccountBalanceEnum::Map(
create_new_account_helper(
&self.ctx,
qtum,
Expand Down
4 changes: 2 additions & 2 deletions mm2src/coins/rpc_command/init_scan_for_new_addresses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ impl RpcTask for InitScanAddressesTask {

async fn run(&mut self, _task_handle: ScanAddressesTaskHandleShared) -> Result<Self::Item, MmError<Self::Error>> {
match self.coin {
MmCoinEnum::UtxoCoin(ref utxo) => Ok(ScanAddressesResponseEnum::Single(
MmCoinEnum::UtxoCoin(ref utxo) => Ok(ScanAddressesResponseEnum::Map(
utxo.init_scan_for_new_addresses_rpc(self.req.params.clone()).await?,
)),
MmCoinEnum::QtumCoin(ref qtum) => Ok(ScanAddressesResponseEnum::Single(
MmCoinEnum::QtumCoin(ref qtum) => Ok(ScanAddressesResponseEnum::Map(
qtum.init_scan_for_new_addresses_rpc(self.req.params.clone()).await?,
)),
MmCoinEnum::EthCoin(ref eth) => Ok(ScanAddressesResponseEnum::Map(
Expand Down
37 changes: 22 additions & 15 deletions mm2src/coins/utxo/bch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ use crate::utxo::utxo_common::{big_decimal_from_sat_unsigned, utxo_prepare_addre
use crate::utxo::utxo_hd_wallet::{UtxoHDAccount, UtxoHDAddress};
use crate::utxo::utxo_tx_history_v2::{UtxoMyAddressesHistoryError, UtxoTxDetailsError, UtxoTxDetailsParams,
UtxoTxHistoryOps};
use crate::{coin_balance, BlockHeightAndTime, CanRefundHtlc, CheckIfMyPaymentSentArgs, CoinBalance, CoinProtocol,
CoinWithDerivationMethod, CoinWithPrivKeyPolicy, ConfirmPaymentInput, DexFee, GetWithdrawSenderAddress,
IguanaBalanceOps, IguanaPrivKey, MakerSwapTakerCoin, MmCoinEnum, NegotiateSwapContractAddrErr,
PaymentInstructionArgs, PaymentInstructions, PaymentInstructionsErr, PrivKeyBuildPolicy,
RawTransactionFut, RawTransactionRequest, RawTransactionResult, RefundError, RefundPaymentArgs,
RefundResult, SearchForSwapTxSpendInput, SendMakerPaymentSpendPreimageInput, SendPaymentArgs,
SignRawTransactionRequest, SignatureResult, SpendPaymentArgs, SwapOps, TakerSwapMakerCoin,
TradePreimageValue, TransactionFut, TransactionResult, TransactionType, TxFeeDetails, TxMarshalingErr,
UnexpectedDerivationMethod, ValidateAddressResult, ValidateFeeArgs, ValidateInstructionsErr,
ValidateOtherPubKeyErr, ValidatePaymentError, ValidatePaymentFut, ValidatePaymentInput,
ValidateWatcherSpendInput, VerificationResult, WaitForHTLCTxSpendArgs, WatcherOps, WatcherReward,
WatcherRewardError, WatcherSearchForSwapTxSpendInput, WatcherValidatePaymentInput,
use crate::{coin_balance, BlockHeightAndTime, CanRefundHtlc, CheckIfMyPaymentSentArgs, CoinBalance, CoinBalanceMap,
CoinProtocol, CoinWithDerivationMethod, CoinWithPrivKeyPolicy, ConfirmPaymentInput, DexFee,
GetWithdrawSenderAddress, IguanaBalanceOps, IguanaPrivKey, MakerSwapTakerCoin, MmCoinEnum,
NegotiateSwapContractAddrErr, PaymentInstructionArgs, PaymentInstructions, PaymentInstructionsErr,
PrivKeyBuildPolicy, RawTransactionFut, RawTransactionRequest, RawTransactionResult, RefundError,
RefundPaymentArgs, RefundResult, SearchForSwapTxSpendInput, SendMakerPaymentSpendPreimageInput,
SendPaymentArgs, SignRawTransactionRequest, SignatureResult, SpendPaymentArgs, SwapOps,
TakerSwapMakerCoin, TradePreimageValue, TransactionFut, TransactionResult, TransactionType, TxFeeDetails,
TxMarshalingErr, UnexpectedDerivationMethod, ValidateAddressResult, ValidateFeeArgs,
ValidateInstructionsErr, ValidateOtherPubKeyErr, ValidatePaymentError, ValidatePaymentFut,
ValidatePaymentInput, ValidateWatcherSpendInput, VerificationResult, WaitForHTLCTxSpendArgs, WatcherOps,
WatcherReward, WatcherRewardError, WatcherSearchForSwapTxSpendInput, WatcherValidatePaymentInput,
WatcherValidateTakerFeeInput, WithdrawFut};
use common::executor::{AbortableSystem, AbortedError};
use common::log::warn;
Expand Down Expand Up @@ -1450,7 +1450,7 @@ impl HDCoinWithdrawOps for BchCoin {}
#[async_trait]
impl HDWalletBalanceOps for BchCoin {
type HDAddressScanner = UtxoAddressScanner;
type BalanceObject = CoinBalance;
type BalanceObject = CoinBalanceMap;

async fn produce_hd_address_scanner(&self) -> BalanceResult<Self::HDAddressScanner> {
utxo_common::produce_hd_address_scanner(self).await
Expand Down Expand Up @@ -1487,14 +1487,21 @@ impl HDWalletBalanceOps for BchCoin {
}

async fn known_address_balance(&self, address: &Address) -> BalanceResult<Self::BalanceObject> {
utxo_common::address_balance(self, address).await
let balance = utxo_common::address_balance(self, address).await?;
Ok(HashMap::from([(self.ticker().to_string(), balance)]))
}

async fn known_addresses_balances(
&self,
addresses: Vec<Address>,
) -> BalanceResult<Vec<(Address, Self::BalanceObject)>> {
utxo_common::addresses_balances(self, addresses).await
let ticker = self.ticker().to_string();
let balances = utxo_common::addresses_balances(self, addresses).await?;

balances
.into_iter()
.map(|(address, balance)| Ok((address, HashMap::from([(ticker.clone(), balance)]))))
.collect()
}

async fn prepare_addresses_for_balance_stream_if_enabled(
Expand Down
32 changes: 21 additions & 11 deletions mm2src/coins/utxo/qtum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::utxo::utxo_builder::{MergeUtxoArcOps, UtxoCoinBuildError, UtxoCoinBui
use crate::utxo::utxo_hd_wallet::{UtxoHDAccount, UtxoHDAddress};
use crate::utxo::utxo_tx_history_v2::{UtxoMyAddressesHistoryError, UtxoTxDetailsError, UtxoTxDetailsParams,
UtxoTxHistoryOps};
use crate::{eth, CanRefundHtlc, CheckIfMyPaymentSentArgs, CoinBalance, CoinWithDerivationMethod,
use crate::{eth, CanRefundHtlc, CheckIfMyPaymentSentArgs, CoinBalance, CoinBalanceMap, CoinWithDerivationMethod,
CoinWithPrivKeyPolicy, ConfirmPaymentInput, DelegationError, DelegationFut, DexFee,
GetWithdrawSenderAddress, IguanaBalanceOps, IguanaPrivKey, MakerSwapTakerCoin, MmCoinEnum,
NegotiateSwapContractAddrErr, PaymentInstructionArgs, PaymentInstructions, PaymentInstructionsErr,
Expand Down Expand Up @@ -1062,9 +1062,12 @@ impl CoinWithDerivationMethod for QtumCoin {

#[async_trait]
impl IguanaBalanceOps for QtumCoin {
type BalanceObject = CoinBalance;
type BalanceObject = CoinBalanceMap;

async fn iguana_balances(&self) -> BalanceResult<Self::BalanceObject> { self.my_balance().compat().await }
async fn iguana_balances(&self) -> BalanceResult<Self::BalanceObject> {
let balance = self.my_balance().compat().await?;
Ok(HashMap::from([(self.ticker().to_string(), balance)]))
}
}

#[async_trait]
Expand Down Expand Up @@ -1103,7 +1106,7 @@ impl HDCoinWithdrawOps for QtumCoin {}
#[async_trait]
impl HDWalletBalanceOps for QtumCoin {
type HDAddressScanner = UtxoAddressScanner;
type BalanceObject = CoinBalance;
type BalanceObject = CoinBalanceMap;

async fn produce_hd_address_scanner(&self) -> BalanceResult<Self::HDAddressScanner> {
utxo_common::produce_hd_address_scanner(self).await
Expand Down Expand Up @@ -1140,14 +1143,21 @@ impl HDWalletBalanceOps for QtumCoin {
}

async fn known_address_balance(&self, address: &Address) -> BalanceResult<Self::BalanceObject> {
utxo_common::address_balance(self, address).await
let balance = utxo_common::address_balance(self, address).await?;
Ok(HashMap::from([(self.ticker().to_string(), balance)]))
}

async fn known_addresses_balances(
&self,
addresses: Vec<Address>,
) -> BalanceResult<Vec<(Address, Self::BalanceObject)>> {
utxo_common::addresses_balances(self, addresses).await
let ticker = self.ticker().to_string();
let balances = utxo_common::addresses_balances(self, addresses).await?;

balances
.into_iter()
.map(|(address, balance)| Ok((address, HashMap::from([(ticker.clone(), balance)]))))
.collect()
}

async fn prepare_addresses_for_balance_stream_if_enabled(
Expand All @@ -1160,7 +1170,7 @@ impl HDWalletBalanceOps for QtumCoin {

#[async_trait]
impl GetNewAddressRpcOps for QtumCoin {
type BalanceObject = CoinBalance;
type BalanceObject = CoinBalanceMap;

async fn get_new_address_rpc_without_conf(
&self,
Expand All @@ -1183,7 +1193,7 @@ impl GetNewAddressRpcOps for QtumCoin {

#[async_trait]
impl AccountBalanceRpcOps for QtumCoin {
type BalanceObject = CoinBalance;
type BalanceObject = CoinBalanceMap;

async fn account_balance_rpc(
&self,
Expand All @@ -1195,7 +1205,7 @@ impl AccountBalanceRpcOps for QtumCoin {

#[async_trait]
impl InitAccountBalanceRpcOps for QtumCoin {
type BalanceObject = CoinBalance;
type BalanceObject = CoinBalanceMap;

async fn init_account_balance_rpc(
&self,
Expand All @@ -1207,7 +1217,7 @@ impl InitAccountBalanceRpcOps for QtumCoin {

#[async_trait]
impl InitScanAddressesRpcOps for QtumCoin {
type BalanceObject = CoinBalance;
type BalanceObject = CoinBalanceMap;

async fn init_scan_for_new_addresses_rpc(
&self,
Expand All @@ -1219,7 +1229,7 @@ impl InitScanAddressesRpcOps for QtumCoin {

#[async_trait]
impl InitCreateAccountRpcOps for QtumCoin {
type BalanceObject = CoinBalance;
type BalanceObject = CoinBalanceMap;

async fn init_create_account_rpc<XPubExtractor>(
&self,
Expand Down
8 changes: 6 additions & 2 deletions mm2src/coins/utxo/rpc_clients/electrum_rpc/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,11 +386,15 @@ impl ElectrumConnection {
};

let Some(dns_name) = uri.host().map(String::from) else {
return Err(ElectrumConnectionErr::Irrecoverable("Couldn't retrieve host from address".to_string()));
return Err(ElectrumConnectionErr::Irrecoverable(
"Couldn't retrieve host from address".to_string(),
));
};

let Ok(dns) = server_name_from_domain(dns_name.as_str()) else {
return Err(ElectrumConnectionErr::Irrecoverable("Address isn't a valid domain name".to_string()));
return Err(ElectrumConnectionErr::Irrecoverable(
"Address isn't a valid domain name".to_string(),
));
};

let tls_connector = if connection.settings.disable_cert_verification {
Expand Down
Loading
Loading