Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4312d15
commit e08022e
Showing
11 changed files
with
151 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { AssetProvider, Cardano } from '@cardano-sdk/core'; | ||
import { Assets } from '../types'; | ||
import { Balance, TransactionalTracker } from './types'; | ||
import { RetryBackoffConfig } from 'backoff-rxjs'; | ||
import { coldObservableProvider } from './util'; | ||
import { distinct, from, mergeMap, of, scan, startWith } from 'rxjs'; | ||
|
||
export const createGetAssetProvider = | ||
(assetProvider: AssetProvider, retryBackoffConfig: RetryBackoffConfig) => (assetId: Cardano.AssetId) => | ||
coldObservableProvider( | ||
() => assetProvider.getAsset(assetId), | ||
retryBackoffConfig, | ||
of(true) // fetch only once | ||
); | ||
export type GetAssetProvider = ReturnType<typeof createGetAssetProvider>; | ||
|
||
export interface AssetsTrackerProps { | ||
balanceTracker: TransactionalTracker<Balance>; | ||
assetProvider: AssetProvider; | ||
retryBackoffConfig: RetryBackoffConfig; | ||
} | ||
|
||
interface AssetsTrackerInternals { | ||
getAssetProvider?: GetAssetProvider; | ||
} | ||
|
||
export const createAssetsTracker = ( | ||
{ assetProvider, balanceTracker, retryBackoffConfig }: AssetsTrackerProps, | ||
{ getAssetProvider = createGetAssetProvider(assetProvider, retryBackoffConfig) }: AssetsTrackerInternals = {} | ||
) => | ||
balanceTracker.total$.pipe( | ||
mergeMap(({ assets }) => from(Object.keys(assets || {}))), | ||
distinct(), | ||
mergeMap((assetId) => getAssetProvider(assetId)), | ||
scan( | ||
(assets, asset) => ({ | ||
...assets, | ||
[asset.assetId]: asset | ||
}), | ||
{} as Assets | ||
), | ||
startWith({} as Assets) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './mockWalletProvider'; | ||
export * from './testKeyManager'; | ||
export * from './mockAssetProvider'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Cardano } from '@cardano-sdk/core'; | ||
|
||
export const asset = { | ||
assetId: '659f2917fb63f12b33667463ee575eeac1845bbc736b9c0bbc40ba8254534c41', | ||
fingerprint: 'asset...', | ||
history: [{ action: Cardano.AssetProvisioning.Mint, quantity: 1000n, transactionId: 'some-tx-id...' }], | ||
name: 'TSLA', | ||
policyId: '659f2917fb63f12b33667463ee575eeac1845bbc736b9c0bbc40ba82', | ||
quantity: 1000n | ||
} as Cardano.Asset; | ||
|
||
export const mockAssetProvider = () => ({ | ||
getAsset: jest.fn().mockResolvedValue(asset) | ||
}); | ||
|
||
export type MockAssetProvider = ReturnType<typeof mockAssetProvider>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { AssetId } from '@cardano-sdk/util-dev'; | ||
import { AssetsTrackerProps, Balance, TransactionalTracker, createAssetsTracker } from '../../src/services'; | ||
import { Cardano } from '@cardano-sdk/core'; | ||
import { createTestScheduler } from '../testScheduler'; | ||
import { of } from 'rxjs'; | ||
|
||
describe('createAssetsTracker', () => { | ||
it('fetches asset info for every asset in total balance', () => { | ||
createTestScheduler().run(({ cold, expectObservable }) => { | ||
const balanceTracker = { | ||
total$: cold('a-b-c', { | ||
a: {} as Balance, | ||
b: { assets: { [AssetId.TSLA]: 1n } as Cardano.TokenMap } as Balance, | ||
c: { | ||
assets: { | ||
[AssetId.TSLA]: 1n, | ||
[AssetId.PXL]: 2n | ||
} as Cardano.TokenMap | ||
} as Balance | ||
}) | ||
} as unknown as TransactionalTracker<Balance>; | ||
const asset1 = { assetId: AssetId.TSLA, name: 'TSLA' } as Cardano.Asset; | ||
const asset2 = { assetId: AssetId.PXL, name: 'PXL' } as Cardano.Asset; | ||
const getAssetProvider = jest.fn().mockReturnValueOnce(of(asset1)).mockReturnValueOnce(of(asset2)); | ||
const target$ = createAssetsTracker({ balanceTracker } as AssetsTrackerProps, { getAssetProvider }); | ||
expectObservable(target$).toBe('a-b-c', { | ||
a: {}, | ||
b: { | ||
[AssetId.TSLA]: asset1 | ||
}, | ||
c: { | ||
[AssetId.TSLA]: asset1, | ||
[AssetId.PXL]: asset2 | ||
} | ||
}); | ||
}); | ||
}); | ||
}); |