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

feat(UTXO): balance event streaming for Electrum clients #2013

Merged
merged 23 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
18dc21b
[WIP] save development state
onur-ozkan Nov 23, 2023
58f35c5
implement scripthash channels between electrum and utxo coin
onur-ozkan Nov 23, 2023
6ab27b6
subscribe to scripthashes
onur-ozkan Nov 24, 2023
5866d3e
[wip] fetch balances when scripthash triggered
onur-ozkan Nov 24, 2023
7f9cb9b
[wip] broadcast balances when they change
onur-ozkan Nov 24, 2023
1abd13f
proper balance event errors on UTXO activations
onur-ozkan Nov 27, 2023
88894e7
create ScripthashNotification types
onur-ozkan Nov 27, 2023
fb864df
add doc-comment for scripthash_notification_sender
onur-ozkan Nov 27, 2023
4d00657
avoid unwrap in `fn scripthash_notification_sender`
onur-ozkan Nov 27, 2023
4831076
do pass by ref for ScripthashNotificationSender
onur-ozkan Nov 27, 2023
a4563f8
impl `get_scripthash_notification_handlers` for MmArc
onur-ozkan Nov 27, 2023
6ab47b2
fix WASM functions
onur-ozkan Nov 27, 2023
4bac4fa
broadcast multiple balances at once
onur-ozkan Nov 27, 2023
37e8a8d
move `SubscriptionNotification` to top in `ElectrumRpcResponseEnum`
onur-ozkan Nov 27, 2023
98d6553
fix review notes
onur-ozkan Nov 29, 2023
aa11435
handle disconnections, new addresses and mpsc capacity
onur-ozkan Nov 30, 2023
c3e264d
fix new address handling TODO
onur-ozkan Nov 30, 2023
255b27e
fix `test_get_new_address` and `test_scan_for_new_addresses` tests
onur-ozkan Nov 30, 2023
681f039
handle subscriptions from rpc_client connections
onur-ozkan Dec 1, 2023
2c3d03c
create utxo common function `address_to_scripthash`
onur-ozkan Dec 5, 2023
cc3dfac
add `prepare_addresses_for_balance_stream_if_enabled` into `HDWalletB…
onur-ozkan Dec 5, 2023
8357033
update `utxo_prepare_addresses_for_balance_stream_if_enabled`
onur-ozkan Dec 5, 2023
a290522
send scripthash event instead of calling rpcs
onur-ozkan Dec 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions mm2src/coins/tendermint/tendermint_balance_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ impl EventBehaviour for TendermintCoin {
})
.collect();

let mut balance_updates = vec![];
for denom in denoms {
if let Some((ticker, decimals)) = self.active_ticker_and_decimals_from_denom(&denom) {
let balance_denom = match self.account_balance_for_denom(&self.account_id, denom).await {
Expand All @@ -139,17 +140,22 @@ impl EventBehaviour for TendermintCoin {
}

if broadcast {
let payload = json!({
balance_updates.push(json!({
"ticker": ticker,
"balance": { "spendable": balance_decimal, "unspendable": BigDecimal::default() }
});

ctx.stream_channel_controller
.broadcast(Event::new(Self::EVENT_NAME.to_string(), payload.to_string()))
.await;
}));
}
}
}

if !balance_updates.is_empty() {
ctx.stream_channel_controller
.broadcast(Event::new(
Self::EVENT_NAME.to_string(),
json!(balance_updates).to_string(),
))
.await;
}
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion mm2src/coins/utxo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub mod rpc_clients;
pub mod slp;
pub mod spv;
pub mod swap_proto_v2_scripts;
pub mod utxo_balance_events;
pub mod utxo_block_header_storage;
pub mod utxo_builder;
pub mod utxo_common;
Expand Down Expand Up @@ -65,7 +66,7 @@ pub use keys::{Address, AddressFormat as UtxoAddressFormat, AddressHashEnum, Key
Type as ScriptType};
#[cfg(not(target_arch = "wasm32"))]
use lightning_invoice::Currency as LightningCurrency;
use mm2_core::mm_ctx::MmArc;
use mm2_core::mm_ctx::{MmArc, MmWeak};
use mm2_err_handle::prelude::*;
use mm2_metrics::MetricsArc;
use mm2_number::BigDecimal;
Expand Down Expand Up @@ -141,6 +142,9 @@ pub type HistoryUtxoTxMap = HashMap<H256Json, HistoryUtxoTx>;
pub type MatureUnspentMap = HashMap<Address, MatureUnspentList>;
pub type RecentlySpentOutPointsGuard<'a> = AsyncMutexGuard<'a, RecentlySpentOutPoints>;
pub type UtxoHDAddress = HDAddress<Address, Public>;
pub type ScripthashNotificationSender = Option<Arc<AsyncMutex<AsyncSender<String>>>>;
onur-ozkan marked this conversation as resolved.
Show resolved Hide resolved

type ScripthashNotificationReceiver = Option<Arc<AsyncMutex<AsyncReceiver<String>>>>;

#[cfg(windows)]
#[cfg(not(target_arch = "wasm32"))]
Expand Down Expand Up @@ -610,6 +614,11 @@ pub struct UtxoCoinFields {
/// This abortable system is used to spawn coin's related futures that should be aborted on coin deactivation
/// and on [`MmArc::stop`].
pub abortable_system: AbortableQueue,
pub(crate) ctx: MmWeak,
/// This is used for balance event streaming implementation for UTXOs.
/// If balance event streaming isn't enabled, this value will always be `None`; otherwise,
/// it will be used for receiving scripthash notifications to re-fetch balances.
scripthash_notification_receiver: ScripthashNotificationReceiver,
}

#[derive(Debug, Display)]
Expand Down
Loading
Loading