-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Daniel Sinclair <[email protected]>
- Loading branch information
1 parent
b2e0bb7
commit f9b793d
Showing
8 changed files
with
453 additions
and
152 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,170 @@ | ||
import { Address } from 'viem'; | ||
import { expect, test } from 'vitest'; | ||
|
||
import { DAI_ADDRESS, ETH_ADDRESS, OP_ADDRESS } from '~/core/references'; | ||
import { ChainId } from '~/core/types/chains'; | ||
import { TEST_ADDRESS_1, TEST_ADDRESS_2 } from '~/test/utils'; | ||
|
||
import { staleBalancesStore } from '.'; | ||
|
||
const THEN = Date.now() - 700000; | ||
const WHEN = Date.now() + 60000; | ||
|
||
test('should be able to add asset information to the staleBalances object', async () => { | ||
const { addStaleBalance, staleBalances } = staleBalancesStore.getState(); | ||
expect(staleBalances).toStrictEqual({}); | ||
addStaleBalance({ | ||
address: TEST_ADDRESS_1, | ||
chainId: ChainId.mainnet, | ||
info: { | ||
address: DAI_ADDRESS, | ||
transactionHash: '0xFOOBAR', | ||
expirationTime: THEN, | ||
}, | ||
}); | ||
addStaleBalance({ | ||
address: TEST_ADDRESS_1, | ||
chainId: ChainId.mainnet, | ||
info: { | ||
address: ETH_ADDRESS as Address, | ||
transactionHash: '0xFOOBAR', | ||
expirationTime: WHEN, | ||
}, | ||
}); | ||
const newStaleBalances = staleBalancesStore.getState().staleBalances; | ||
expect(newStaleBalances).toStrictEqual({ | ||
[TEST_ADDRESS_1]: { | ||
[ChainId.mainnet]: { | ||
[DAI_ADDRESS]: { | ||
address: DAI_ADDRESS, | ||
transactionHash: '0xFOOBAR', | ||
expirationTime: THEN, | ||
}, | ||
[ETH_ADDRESS]: { | ||
address: ETH_ADDRESS, | ||
transactionHash: '0xFOOBAR', | ||
expirationTime: WHEN, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
test('should generate accurate stale balance query params and clear expired data - case #1', async () => { | ||
const { getStaleBalancesQueryParam, clearExpiredData } = | ||
staleBalancesStore.getState(); | ||
clearExpiredData(TEST_ADDRESS_1); | ||
const queryParam = getStaleBalancesQueryParam(TEST_ADDRESS_1); | ||
expect(queryParam).toStrictEqual(`&token=${ChainId.mainnet}.${ETH_ADDRESS}`); | ||
}); | ||
|
||
test('should be able to remove expired stale balance and preserve unexpired data', async () => { | ||
const { addStaleBalance, clearExpiredData } = staleBalancesStore.getState(); | ||
addStaleBalance({ | ||
address: TEST_ADDRESS_1, | ||
chainId: ChainId.mainnet, | ||
info: { | ||
address: DAI_ADDRESS, | ||
transactionHash: '0xFOOBAR', | ||
expirationTime: THEN, | ||
}, | ||
}); | ||
addStaleBalance({ | ||
address: TEST_ADDRESS_1, | ||
chainId: ChainId.mainnet, | ||
info: { | ||
address: ETH_ADDRESS as Address, | ||
transactionHash: '0xFOOBAR', | ||
expirationTime: WHEN, | ||
}, | ||
}); | ||
clearExpiredData(TEST_ADDRESS_1); | ||
const newStaleBalances = staleBalancesStore.getState().staleBalances; | ||
expect(newStaleBalances).toStrictEqual({ | ||
[TEST_ADDRESS_1]: { | ||
[ChainId.mainnet]: { | ||
[ETH_ADDRESS]: { | ||
address: ETH_ADDRESS, | ||
transactionHash: '0xFOOBAR', | ||
expirationTime: WHEN, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
test('should preserve data from other addresses when clearing expired data', async () => { | ||
const { addStaleBalance, clearExpiredData } = staleBalancesStore.getState(); | ||
addStaleBalance({ | ||
address: TEST_ADDRESS_1, | ||
chainId: ChainId.mainnet, | ||
info: { | ||
address: DAI_ADDRESS, | ||
transactionHash: '0xFOOBAR', | ||
expirationTime: THEN, | ||
}, | ||
}); | ||
addStaleBalance({ | ||
address: TEST_ADDRESS_2, | ||
chainId: ChainId.mainnet, | ||
info: { | ||
address: ETH_ADDRESS as Address, | ||
transactionHash: '0xFOOBAR', | ||
expirationTime: WHEN, | ||
}, | ||
}); | ||
clearExpiredData(TEST_ADDRESS_1); | ||
const newStaleBalances = staleBalancesStore.getState().staleBalances; | ||
expect(newStaleBalances).toStrictEqual({ | ||
[TEST_ADDRESS_1]: { | ||
[ChainId.mainnet]: { | ||
[ETH_ADDRESS]: { | ||
address: ETH_ADDRESS, | ||
transactionHash: '0xFOOBAR', | ||
expirationTime: WHEN, | ||
}, | ||
}, | ||
}, | ||
[TEST_ADDRESS_2]: { | ||
[ChainId.mainnet]: { | ||
[ETH_ADDRESS]: { | ||
address: ETH_ADDRESS, | ||
transactionHash: '0xFOOBAR', | ||
expirationTime: WHEN, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
test('should generate accurate stale balance query params and clear expired data - case #2', async () => { | ||
const { getStaleBalancesQueryParam, clearExpiredData } = | ||
staleBalancesStore.getState(); | ||
clearExpiredData(TEST_ADDRESS_2); | ||
const queryParam = getStaleBalancesQueryParam(TEST_ADDRESS_2); | ||
expect(queryParam).toStrictEqual(`&token=${ChainId.mainnet}.${ETH_ADDRESS}`); | ||
}); | ||
|
||
test('should generate accurate stale balance query params and clear expired data - case #3', async () => { | ||
const { addStaleBalance, getStaleBalancesQueryParam, clearExpiredData } = | ||
staleBalancesStore.getState(); | ||
addStaleBalance({ | ||
address: TEST_ADDRESS_1, | ||
chainId: ChainId.optimism, | ||
info: { | ||
address: OP_ADDRESS, | ||
transactionHash: '0xFOOBAR', | ||
expirationTime: WHEN, | ||
}, | ||
}); | ||
|
||
clearExpiredData(TEST_ADDRESS_1); | ||
const queryParam = getStaleBalancesQueryParam(TEST_ADDRESS_1); | ||
expect(queryParam).toStrictEqual( | ||
`&token=${ChainId.mainnet}.${ETH_ADDRESS}&token=${ChainId.optimism}.${OP_ADDRESS}`, | ||
); | ||
|
||
clearExpiredData(TEST_ADDRESS_2); | ||
const queryParam2 = getStaleBalancesQueryParam(TEST_ADDRESS_2); | ||
expect(queryParam2).toStrictEqual(`&token=${ChainId.mainnet}.${ETH_ADDRESS}`); | ||
}); |
Oops, something went wrong.