Skip to content

Commit 4ce6567

Browse files
authored
Merge pull request #19 from Odrin/feat/token-wallet-optional-preload
feat: `TokenWallet` optional transaction preload
2 parents 561b60e + 1eaede2 commit 4ce6567

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

src/core/token_wallet/mod.rs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ impl TokenWallet {
3535
owner: MsgAddressInt,
3636
root_token_contract: MsgAddressInt,
3737
handler: Arc<dyn TokenWalletSubscriptionHandler>,
38+
preload_transactions: bool,
3839
) -> Result<TokenWallet> {
3940
let state = match transport.get_contract_state(&root_token_contract).await? {
4041
RawContractState::Exists(state) => state,
@@ -52,16 +53,35 @@ impl TokenWallet {
5253
} = state.guess_details()?;
5354

5455
let address = state.get_wallet_address(version, &owner)?;
55-
5656
let mut balance = Default::default();
57-
let contract_subscription = ContractSubscription::subscribe(
58-
clock.clone(),
59-
transport,
60-
address,
61-
&mut make_contract_state_handler(clock.clone(), version, &mut balance),
62-
Some(&mut make_transactions_handler(handler.as_ref(), version)),
63-
)
64-
.await?;
57+
58+
let contract_subscription = {
59+
let handler = handler.as_ref();
60+
61+
// NOTE: create handler beforehead to prevent lifetime issues
62+
let mut on_transactions_found = match preload_transactions {
63+
true => Some(make_transactions_handler(handler, version)),
64+
false => None,
65+
};
66+
67+
// Manual map is used here due to unsoundness
68+
// See issue: https://github.com/rust-lang/rust/issues/84305
69+
#[allow(trivial_casts)]
70+
#[allow(clippy::manual_map)]
71+
let on_transactions_found = match &mut on_transactions_found {
72+
Some(handler) => Some(handler as _),
73+
None => None,
74+
};
75+
76+
ContractSubscription::subscribe(
77+
clock.clone(),
78+
transport,
79+
address,
80+
&mut make_contract_state_handler(clock.clone(), version, &mut balance),
81+
on_transactions_found,
82+
)
83+
.await?
84+
};
6585

6686
handler.on_balance_changed(balance.clone());
6787

0 commit comments

Comments
 (0)