Skip to content

Commit

Permalink
add idb tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentine1898 committed Jun 13, 2024
1 parent bcd6c0b commit b60e273
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
14 changes: 10 additions & 4 deletions packages/storage/src/indexed-db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -669,12 +669,18 @@ export class IndexedDb implements IndexedDbInterface {
}

async clearSwapBasedPrices(): Promise<void> {
for await (const cursor of this.db.transaction('PRICES').store) {
const tx = this.db.transaction('PRICES', 'readwrite');
const store = tx.objectStore('PRICES');

let cursor = await store.openCursor();
while (cursor) {
const price = EstimatedPrice.fromJson(cursor.value);
// Do not delete prices for delegation assets
if (price.numeraire?.equals(this.stakingTokenAssetId)) continue;
await this.db.delete('PRICES', cursor.key);
if (!price.numeraire?.equals(this.stakingTokenAssetId)) {
cursor.delete();

Check failure on line 679 in packages/storage/src/indexed-db/index.ts

View workflow job for this annotation

GitHub Actions / Lint

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
}
cursor = await cursor.continue();
}
await tx.done;
}

private determinePriceRelevanceThresholdForAsset(assetMetadata: Metadata): number {
Expand Down
10 changes: 9 additions & 1 deletion packages/storage/src/indexed-db/indexed-db.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,9 +594,12 @@ describe('IndexedDb', () => {

const numeraireAssetId = new AssetId({ inner: new Uint8Array([5, 6, 7, 8]) });

const stakingAssetId = AssetId.fromJson({
inner: 'KeqcLzNx9qSH5+lcJHBB9KNW+YPrBk5dKzvPMiypahA=',
});
beforeEach(async () => {
db = await IndexedDb.initialize({ ...generateInitialProps() });
await db.updatePrice(delegationMetadataA.penumbraAssetId!, numeraireAssetId, 1.23, 50n);
await db.updatePrice(delegationMetadataA.penumbraAssetId!, stakingAssetId, 1.23, 50n);
await db.updatePrice(metadataA.penumbraAssetId!, numeraireAssetId, 22.15, 40n);
});

Expand Down Expand Up @@ -628,6 +631,11 @@ describe('IndexedDb', () => {
}),
]);
});

it('should delete only prices with a numeraires different from the staking token', async () => {
await db.clearSwapBasedPrices();
await expect(db.getPricesForAsset(metadataA, 50n)).resolves.toEqual([]);
});
});

describe('upsertAuction()', () => {
Expand Down

0 comments on commit b60e273

Please sign in to comment.