Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(wallet): implement BalanceTracker, move all test mocks under ./m…
…ocks
- Loading branch information
1 parent
ecdcfb3
commit 98849d2
Showing
16 changed files
with
141 additions
and
45 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
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,44 @@ | ||
import { Ogmios } from '@cardano-sdk/core'; | ||
import { MockUtxoRepository } from './mocks'; | ||
import { BalanceTracker, UtxoRepositoryEvent, UtxoRepositoryFields } from '../src'; | ||
|
||
const numAssets = (tokenMap?: Ogmios.TokenMap) => Object.keys(tokenMap || {}).length; | ||
|
||
describe('BalanceTracker', () => { | ||
let balanceTracker: BalanceTracker; | ||
let utxoRepository: MockUtxoRepository; | ||
|
||
beforeEach(() => { | ||
utxoRepository = new MockUtxoRepository(); | ||
balanceTracker = new BalanceTracker(utxoRepository); | ||
}); | ||
|
||
it('constructor balances from utxo repository', () => { | ||
expect(balanceTracker.total.coins).toBeGreaterThan(0n); | ||
expect(balanceTracker.total.assets).toBeTruthy(); | ||
expect(balanceTracker.total.rewards).toBeGreaterThan(0n); | ||
}); | ||
|
||
it('updates balances on UtxoRepositoryEvent.Changed', async () => { | ||
const totalBefore = balanceTracker.total; | ||
|
||
const allRewards = utxoRepository.allRewards - 1n; | ||
const availableRewards = utxoRepository.allRewards - 2n; | ||
await utxoRepository.emit(UtxoRepositoryEvent.Changed, { | ||
allUtxos: utxoRepository.allUtxos.slice(1), | ||
availableUtxos: utxoRepository.allUtxos.slice(2), | ||
allRewards, | ||
availableRewards, | ||
delegation: null | ||
} as UtxoRepositoryFields); | ||
|
||
expect(balanceTracker.total.coins).toBeLessThan(totalBefore.coins); | ||
expect(balanceTracker.total.coins).toBeGreaterThan(balanceTracker.available.coins); | ||
|
||
expect(numAssets(balanceTracker.total.assets)).toBeLessThan(numAssets(totalBefore.assets)); | ||
expect(numAssets(balanceTracker.total.assets)).toBeGreaterThan(numAssets(balanceTracker.available.assets)); | ||
|
||
expect(balanceTracker.total.rewards).toBe(allRewards); | ||
expect(balanceTracker.available.rewards).toBe(availableRewards); | ||
}); | ||
}); |
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
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
2 changes: 1 addition & 1 deletion
2
...ges/wallet/test/mockTransactionTracker.ts → ...llet/test/mocks/MockTransactionTracker.ts
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,13 @@ | ||
import Emittery from 'emittery'; | ||
import { delegate, rewards, utxo } from './ProviderStub'; | ||
import { UtxoRepository, UtxoRepositoryEvents } from '../../src'; | ||
|
||
export class MockUtxoRepository extends Emittery<UtxoRepositoryEvents> implements UtxoRepository { | ||
sync = jest.fn().mockResolvedValue(void 0); | ||
selectInputs = jest.fn(); | ||
allUtxos = utxo; | ||
availableUtxos = utxo; | ||
allRewards = rewards; | ||
availableRewards = rewards; | ||
delegation = delegate; | ||
} |
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,4 @@ | ||
export * from './ProviderStub'; | ||
export * from './MockUtxoRepository'; | ||
export * from './MockTransactionTracker'; | ||
export * from './testKeyManager'; |
2 changes: 1 addition & 1 deletion
2
packages/wallet/test/testKeyManager.ts → packages/wallet/test/mocks/testKeyManager.ts
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