Skip to content

Commit

Permalink
Merge pull request broxus#19 from Odrin/feat/token-wallet-optional-pr…
Browse files Browse the repository at this point in the history
…eload

feat: `TokenWallet` optional transaction preload
  • Loading branch information
Rexagon authored Dec 16, 2024
2 parents 561b60e + 1eaede2 commit 4ce6567
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions src/core/token_wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl TokenWallet {
owner: MsgAddressInt,
root_token_contract: MsgAddressInt,
handler: Arc<dyn TokenWalletSubscriptionHandler>,
preload_transactions: bool,
) -> Result<TokenWallet> {
let state = match transport.get_contract_state(&root_token_contract).await? {
RawContractState::Exists(state) => state,
Expand All @@ -52,16 +53,35 @@ impl TokenWallet {
} = state.guess_details()?;

let address = state.get_wallet_address(version, &owner)?;

let mut balance = Default::default();
let contract_subscription = ContractSubscription::subscribe(
clock.clone(),
transport,
address,
&mut make_contract_state_handler(clock.clone(), version, &mut balance),
Some(&mut make_transactions_handler(handler.as_ref(), version)),
)
.await?;

let contract_subscription = {
let handler = handler.as_ref();

// NOTE: create handler beforehead to prevent lifetime issues
let mut on_transactions_found = match preload_transactions {
true => Some(make_transactions_handler(handler, version)),
false => None,
};

// Manual map is used here due to unsoundness
// See issue: https://github.com/rust-lang/rust/issues/84305
#[allow(trivial_casts)]
#[allow(clippy::manual_map)]
let on_transactions_found = match &mut on_transactions_found {
Some(handler) => Some(handler as _),
None => None,
};

ContractSubscription::subscribe(
clock.clone(),
transport,
address,
&mut make_contract_state_handler(clock.clone(), version, &mut balance),
on_transactions_found,
)
.await?
};

handler.on_balance_changed(balance.clone());

Expand Down

0 comments on commit 4ce6567

Please sign in to comment.