-
-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into brian/network-controller-race-condition
- Loading branch information
Showing
62 changed files
with
2,041 additions
and
238 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
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 |
---|---|---|
|
@@ -7,8 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 | |
|
||
## [Unreleased] | ||
|
||
## [7.1.2] | ||
|
||
### Changed | ||
|
||
- Bump `nanoid` from `^3.1.31` to `^3.3.8` ([#5073](https://github.com/MetaMask/core/pull/5073)) | ||
- Bump `@metamask/utils` from `^10.0.0` to `^11.0.1` ([#5080](https://github.com/MetaMask/core/pull/5080)) | ||
- Bump `@metamask/rpc-errors` from `^7.0.0` to `^7.0.2` ([#5080](https://github.com/MetaMask/core/pull/5080)) | ||
- Bump `@metamask/base-controller` from `^7.0.0` to `^7.1.0` ([#5079](https://github.com/MetaMask/core/pull/5079)) | ||
|
||
## [7.1.1] | ||
|
@@ -257,7 +262,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 | |
|
||
All changes listed after this point were applied to this package following the monorepo conversion. | ||
|
||
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/[email protected] | ||
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/[email protected] | ||
[7.1.2]: https://github.com/MetaMask/core/compare/@metamask/[email protected]...@metamask/[email protected] | ||
[7.1.1]: https://github.com/MetaMask/core/compare/@metamask/[email protected]...@metamask/[email protected] | ||
[7.1.0]: https://github.com/MetaMask/core/compare/@metamask/[email protected]...@metamask/[email protected] | ||
[7.0.4]: https://github.com/MetaMask/core/compare/@metamask/[email protected]...@metamask/[email protected] | ||
|
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
143 changes: 143 additions & 0 deletions
143
packages/assets-controllers/src/MultichainBalancesController/BalancesTracker.test.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import { BtcAccountType, BtcMethod } from '@metamask/keyring-api'; | ||
import { KeyringTypes } from '@metamask/keyring-controller'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
|
||
import { BalancesTracker } from './BalancesTracker'; | ||
import { Poller } from './Poller'; | ||
|
||
const MOCK_TIMESTAMP = 1709983353; | ||
|
||
const mockBtcAccount = { | ||
address: '', | ||
id: uuidv4(), | ||
metadata: { | ||
name: 'Bitcoin Account 1', | ||
importTime: Date.now(), | ||
keyring: { | ||
type: KeyringTypes.snap, | ||
}, | ||
snap: { | ||
id: 'mock-btc-snap', | ||
name: 'mock-btc-snap', | ||
enabled: true, | ||
}, | ||
lastSelected: 0, | ||
}, | ||
options: {}, | ||
methods: [BtcMethod.SendBitcoin], | ||
type: BtcAccountType.P2wpkh, | ||
}; | ||
|
||
/** | ||
* Sets up a BalancesTracker instance for testing. | ||
* @returns The BalancesTracker instance and a mock update balance function. | ||
*/ | ||
function setupTracker() { | ||
const mockUpdateBalance = jest.fn(); | ||
const tracker = new BalancesTracker(mockUpdateBalance); | ||
|
||
return { | ||
tracker, | ||
mockUpdateBalance, | ||
}; | ||
} | ||
|
||
describe('BalancesTracker', () => { | ||
it('starts polling when calling start', async () => { | ||
const { tracker } = setupTracker(); | ||
const spyPoller = jest.spyOn(Poller.prototype, 'start'); | ||
|
||
tracker.start(); | ||
expect(spyPoller).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('stops polling when calling stop', async () => { | ||
const { tracker } = setupTracker(); | ||
const spyPoller = jest.spyOn(Poller.prototype, 'stop'); | ||
|
||
tracker.start(); | ||
tracker.stop(); | ||
expect(spyPoller).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('is not tracking if none accounts have been registered', async () => { | ||
const { tracker, mockUpdateBalance } = setupTracker(); | ||
|
||
tracker.start(); | ||
await tracker.updateBalances(); | ||
|
||
expect(mockUpdateBalance).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('tracks account balances', async () => { | ||
const { tracker, mockUpdateBalance } = setupTracker(); | ||
|
||
tracker.start(); | ||
// We must track account IDs explicitly | ||
tracker.track(mockBtcAccount.id, 0); | ||
// Trigger balances refresh (not waiting for the Poller here) | ||
await tracker.updateBalances(); | ||
|
||
expect(mockUpdateBalance).toHaveBeenCalledWith(mockBtcAccount.id); | ||
}); | ||
|
||
it('untracks account balances', async () => { | ||
const { tracker, mockUpdateBalance } = setupTracker(); | ||
|
||
tracker.start(); | ||
tracker.track(mockBtcAccount.id, 0); | ||
await tracker.updateBalances(); | ||
expect(mockUpdateBalance).toHaveBeenCalledWith(mockBtcAccount.id); | ||
|
||
tracker.untrack(mockBtcAccount.id); | ||
await tracker.updateBalances(); | ||
expect(mockUpdateBalance).toHaveBeenCalledTimes(1); // No second call after untracking | ||
}); | ||
|
||
it('tracks account after being registered', async () => { | ||
const { tracker } = setupTracker(); | ||
|
||
tracker.start(); | ||
tracker.track(mockBtcAccount.id, 0); | ||
expect(tracker.isTracked(mockBtcAccount.id)).toBe(true); | ||
}); | ||
|
||
it('does not track account if not registered', async () => { | ||
const { tracker } = setupTracker(); | ||
|
||
tracker.start(); | ||
expect(tracker.isTracked(mockBtcAccount.id)).toBe(false); | ||
}); | ||
|
||
it('does not refresh balance if they are considered up-to-date', async () => { | ||
const { tracker, mockUpdateBalance } = setupTracker(); | ||
|
||
const blockTime = 10 * 60 * 1000; // 10 minutes in milliseconds. | ||
jest | ||
.spyOn(global.Date, 'now') | ||
.mockImplementation(() => new Date(MOCK_TIMESTAMP).getTime()); | ||
|
||
tracker.start(); | ||
tracker.track(mockBtcAccount.id, blockTime); | ||
await tracker.updateBalances(); | ||
expect(mockUpdateBalance).toHaveBeenCalledTimes(1); | ||
|
||
await tracker.updateBalances(); | ||
expect(mockUpdateBalance).toHaveBeenCalledTimes(1); // No second call since the balances is already still up-to-date | ||
|
||
jest | ||
.spyOn(global.Date, 'now') | ||
.mockImplementation(() => new Date(MOCK_TIMESTAMP + blockTime).getTime()); | ||
|
||
await tracker.updateBalances(); | ||
expect(mockUpdateBalance).toHaveBeenCalledTimes(2); // Now the balance will update | ||
}); | ||
|
||
it('throws an error if trying to update balance of an untracked account', async () => { | ||
const { tracker } = setupTracker(); | ||
|
||
await expect(tracker.updateBalance(mockBtcAccount.id)).rejects.toThrow( | ||
`Account is not being tracked: ${mockBtcAccount.id}`, | ||
); | ||
}); | ||
}); |
Oops, something went wrong.