Skip to content

Commit

Permalink
Merge pull request #3416 from Emurgo/fix/YOEXT-1001/balance-loading-2
Browse files Browse the repository at this point in the history
initial balance loading fix
  • Loading branch information
vsubhuman authored Feb 2, 2024
2 parents 4d05324 + 309eadb commit 7bd4266
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,7 @@ export default class AdaDelegationStore extends Store<StoresMap, ActionsMap> {
publicDeriver: withStakingKey,
rewardBalance: new MultiToken(
[{
amount: new BigNumber(stateForStakingKey == null
? 0
: stateForStakingKey.remainingAmount),
amount: new BigNumber(stateForStakingKey?.remainingAmount ?? 0),
networkId: defaultToken.defaultNetworkId,
identifier: defaultToken.defaultIdentifier,
}],
Expand Down
40 changes: 27 additions & 13 deletions packages/yoroi-extension/app/stores/toplevel/TransactionsStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,13 @@ export default class TransactionsStore extends Store<StoresMap, ActionsMap> {
) => Promise<void> = async (result: Array<WalletTransaction>, publicDeriver: PublicDeriver<>) => {
const timestamps: Set<number> = new Set();
const remoteTransactionIds: Set<string> = new Set();
let walletHasWithdrawal = false;
const withdrawalIds = new Set<string>();
for (const tx of result) {
const { txid, date } = tx;
timestamps.add(date.valueOf());
remoteTransactionIds.add(txid);
if (tx instanceof CardanoShelleyTransaction && tx.withdrawals.length > 0) {
walletHasWithdrawal = true;
withdrawalIds.add(txid);
}
}
const defaultTokenInfo = this.stores.tokenInfoStore.getDefaultTokenInfo(
Expand All @@ -309,12 +309,15 @@ export default class TransactionsStore extends Store<StoresMap, ActionsMap> {
});

let submittedTransactionsChanged = false;
let addedProcessedWithdrawal = false;
runInAction(() => {
for (let i = 0; i < this._submittedTransactions.length; ) {
if (remoteTransactionIds.has(this._submittedTransactions[i].transaction.txid)) {
if (walletHasWithdrawal) {
const txId = this._submittedTransactions[i].transaction.txid;
if (remoteTransactionIds.has(txId)) {
if (withdrawalIds.has(txId) && !addedProcessedWithdrawal) {
// Set local processed withdrawals only if there was a pending local transaction
this._processedWithdrawals.push(publicDeriver.publicDeriverId);
addedProcessedWithdrawal = true;
}
this._submittedTransactions.splice(i, 1);
submittedTransactionsChanged = true;
Expand Down Expand Up @@ -359,7 +362,10 @@ export default class TransactionsStore extends Store<StoresMap, ActionsMap> {
/*
* TAIL REQUEST IS USED WHEN FIRST SYNC OR EMPTY WALLET
*/
result = await this._internalTailRequestForTxs(request.publicDeriver);
result = await this._internalTailRequestForTxs({
publicDeriver: request.publicDeriver,
isLocalRequest: request.isLocalRequest,
});
} else {
/*
* HEAD REQUEST IS USED WITH `AFTER` REFERENCE
Expand All @@ -368,6 +374,7 @@ export default class TransactionsStore extends Store<StoresMap, ActionsMap> {
headRequest.invalidate({ immediately: false });
headRequest.execute({
publicDeriver,
// HEAD request is never local by logic
isLocalRequest: false,
afterTx: txHistoryState.txs[0],
});
Expand Down Expand Up @@ -472,10 +479,15 @@ export default class TransactionsStore extends Store<StoresMap, ActionsMap> {
return newMultiToken(defaultToken);
},
});
this.stores.substores.ada.delegation.refreshDelegation(request.publicDeriver);
const refreshDelegationPromise =
this.stores.substores.ada.delegation.refreshDelegation(request.publicDeriver);
if (!getBalanceRequest.promise || !getAssetDepositRequest.promise)
throw new Error('should never happen');
await Promise.all([getBalanceRequest.promise, getAssetDepositRequest.promise]);
await Promise.all([
getBalanceRequest.promise,
getAssetDepositRequest.promise,
refreshDelegationPromise,
]);
})();

await this._afterLoadingNewTxs(
Expand All @@ -484,11 +496,13 @@ export default class TransactionsStore extends Store<StoresMap, ActionsMap> {
);
}

_internalTailRequestForTxs: (
PublicDeriver<> & IGetLastSyncInfo,
) => Promise<GetTransactionsResponse> = async (
_internalTailRequestForTxs: ({|
publicDeriver: PublicDeriver<> & IGetLastSyncInfo,
) => {
isLocalRequest?: boolean,
|}) => Promise<GetTransactionsResponse> = async ({
publicDeriver,
isLocalRequest = false,
}) => {
const withLevels = asHasLevels<ConceptualWallet, IGetLastSyncInfo>(publicDeriver);
if (withLevels == null) {
throw new Error(`${nameof(this._loadMore)} no levels`);
Expand All @@ -501,7 +515,7 @@ export default class TransactionsStore extends Store<StoresMap, ActionsMap> {
tailRequest.invalidate({ immediately: false });
tailRequest.execute({
publicDeriver: withLevels,
isLocalRequest: false,
isLocalRequest,
beforeTx,
});
if (!tailRequest.promise) throw new Error('unexpected nullish tailRequest.promise');
Expand All @@ -518,7 +532,7 @@ export default class TransactionsStore extends Store<StoresMap, ActionsMap> {
) => Promise<void> = async (
publicDeriver: PublicDeriver<> & IGetLastSyncInfo,
) => {
const result = await this._internalTailRequestForTxs(publicDeriver);
const result = await this._internalTailRequestForTxs({ publicDeriver });
await this._afterLoadingNewTxs(result, publicDeriver);
}

Expand Down
5 changes: 2 additions & 3 deletions packages/yoroi-extension/app/stores/toplevel/WalletStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,8 @@ export default class WalletStore extends Store<StoresMap, ActionsMap> {
this.publicDerivers.push(...newWithCachedData);
});
setTimeout(async () => {
for (const publicDeriver of newWithCachedData) {
await this.refreshWalletFromLocalOnLaunch(publicDeriver);
}
await Promise.all(newWithCachedData
.map(w => this.refreshWalletFromLocalOnLaunch(w)));
// This should correctly handle both states of `paralletSync`, both
// initially and when it changes.
// The initial case is straightforward.
Expand Down

0 comments on commit 7bd4266

Please sign in to comment.