Skip to content

Commit e49a4ea

Browse files
committed
feat(wallet): fetch asset info of all assets in balance
We have recently updated BaseWallet to operate with partial tx history. AssetsTracker is fetching AssetInfo only for assets found in tx history. Therefore it wouldn't fetch asset info for assets currently in balance.
1 parent e555950 commit e49a4ea

File tree

2 files changed

+62
-15
lines changed

2 files changed

+62
-15
lines changed

packages/wallet/src/services/AssetsTracker.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Logger } from 'ts-log';
55
import {
66
Observable,
77
buffer,
8+
combineLatest,
89
concat,
910
connect,
1011
debounceTime,
@@ -151,14 +152,19 @@ export const createAssetService =
151152
).pipe(map((arr) => arr.flat())); // Concatenate the chunk results
152153

153154
export type AssetService = ReturnType<typeof createAssetService>;
155+
export type UtxoTotalBalance = {
156+
utxo: {
157+
total$: BalanceTracker['utxo']['total$'];
158+
};
159+
};
154160

155161
export interface AssetsTrackerProps {
156162
transactionsTracker: TransactionsTracker;
157163
assetProvider: TrackedAssetProvider;
158164
retryBackoffConfig: RetryBackoffConfig;
159165
logger: Logger;
160166
assetsCache$: Observable<Assets>;
161-
balanceTracker: BalanceTracker;
167+
balanceTracker: UtxoTotalBalance;
162168
maxAssetInfoCacheAge?: Milliseconds;
163169
}
164170

@@ -198,13 +204,14 @@ export const createAssetsTracker = (
198204
const allAssetIds = new Set<Cardano.AssetId>();
199205
const sharedHistory$ = history$.pipe(share());
200206
return concat(
201-
sharedHistory$.pipe(
202-
map((historyTxs) => uniq(historyTxs.flatMap(uniqueAssetIds))),
207+
combineLatest([
208+
sharedHistory$.pipe(map((historyTxs) => uniq(historyTxs.flatMap(uniqueAssetIds)))),
209+
total$.pipe(map((balance) => [...(balance.assets?.keys() || [])]))
210+
]).pipe(
211+
map(([txAssets, balanceAssets]) => uniq([...txAssets, ...balanceAssets])),
203212
tap((assetIds) =>
204213
logger.debug(
205-
assetIds.length > 0
206-
? `Historical total assets: ${assetIds.length}`
207-
: 'Setting assetProvider stats as initialized'
214+
assetIds.length > 0 ? `Total assets: ${assetIds.length}` : 'Setting assetProvider stats as initialized'
208215
)
209216
),
210217
tap((assetIds) => assetIds.length === 0 && assetProvider.setStatInitialized(assetProvider.stats.getAsset$)),

packages/wallet/test/services/AssetsTracker.test.ts

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { AssetId, createTestScheduler, generateRandomHexString, logger } from '@
33
import {
44
AssetService,
55
AssetsTrackerProps,
6-
BalanceTracker,
76
TrackedAssetProvider,
87
TransactionsTracker,
8+
UtxoTotalBalance,
99
createAssetService,
1010
createAssetsTracker
1111
} from '../../src/services';
@@ -55,7 +55,7 @@ const assetInfo = {
5555
describe('createAssetsTracker', () => {
5656
let assetService: AssetService;
5757
let assetProvider: TrackedAssetProvider;
58-
let balanceTracker: BalanceTracker;
58+
let balanceTracker: UtxoTotalBalance;
5959
let assetsCache$: Observable<Map<Cardano.AssetId, Asset.AssetInfo>>;
6060
const retryBackoffConfig: RetryBackoffConfig = { initialInterval: 2 };
6161

@@ -86,14 +86,8 @@ describe('createAssetsTracker', () => {
8686
} as unknown as TrackedAssetProvider;
8787

8888
balanceTracker = {
89-
rewardAccounts: {
90-
deposit$: of(0n),
91-
rewards$: of(0n)
92-
},
9389
utxo: {
94-
available$: of({ coins: 0n }),
95-
total$: of({ coins: 0n }),
96-
unspendable$: of({ coins: 0n })
90+
total$: of({ coins: 0n })
9791
}
9892
};
9993

@@ -144,6 +138,52 @@ describe('createAssetsTracker', () => {
144138
});
145139
});
146140

141+
it('fetches asset info for assets in balance but not in transaction history', () => {
142+
createTestScheduler().run(({ cold, expectObservable, flush }) => {
143+
const transactionsTracker: Partial<TransactionsTracker> = {
144+
history$: cold('a', {
145+
a: [createTxWithValues([{ assets: new Map([[AssetId.TSLA, 1n]]) }])]
146+
})
147+
};
148+
149+
balanceTracker = {
150+
utxo: {
151+
total$: cold('a', {
152+
a: {
153+
assets: new Map([
154+
[AssetId.TSLA, 1n],
155+
[AssetId.PXL, 2n]
156+
]),
157+
coins: 1n
158+
}
159+
})
160+
}
161+
};
162+
163+
const target$ = createAssetsTracker(
164+
{
165+
assetProvider,
166+
assetsCache$,
167+
balanceTracker,
168+
logger,
169+
retryBackoffConfig,
170+
transactionsTracker
171+
} as unknown as AssetsTrackerProps,
172+
{
173+
assetService
174+
}
175+
);
176+
expectObservable(target$).toBe('a', {
177+
a: new Map([
178+
[AssetId.TSLA, assetInfo.TSLA],
179+
[AssetId.PXL, assetInfo.PXL]
180+
])
181+
});
182+
flush();
183+
expect(assetService).toHaveBeenCalledTimes(1);
184+
});
185+
});
186+
147187
it('re-fetches asset info when there is a cip68 reference nft in some tx history output', () => {
148188
createTestScheduler().run(({ cold, expectObservable, flush }) => {
149189
const transactionsTracker: Partial<TransactionsTracker> = {

0 commit comments

Comments
 (0)