Skip to content

Commit

Permalink
[duplicate] 1231 onboarding numeraire selection2 (#1282)
Browse files Browse the repository at this point in the history
* use numeraire from local storage

* save multiple numeraires in local storage

* added NumerairesSlice

* add ChangeNumeraires service message

* numeraires persist

* add icons

* remove onboardNumeraires

* delete prices when numeraire is changed

* refactor & fix numeraires update bug

* add idb tests

* fix chain-id

* Update apps/extension/src/shared/components/numeraires-form.tsx

Co-authored-by: Jesse Pinho <[email protected]>

* Update apps/extension/src/shared/components/numeraires-form.tsx

Co-authored-by: Jesse Pinho <[email protected]>

* Update apps/extension/src/routes/page/onboarding/set-numeraire.tsx

Co-authored-by: Jesse Pinho <[email protected]>

* Update apps/extension/src/routes/page/onboarding/set-numeraire.tsx

Co-authored-by: Jesse Pinho <[email protected]>

* fix lint

* add chainId comment

* Revert "add chainId comment"

This reverts commit d2f7230faaa1d725ada301d61fbf8cf73efe99b1.

* Revert "fix lint"

This reverts commit fabf6ae6ffccf9fcb0313c159b42959d174adb50.

* Revert "Update apps/extension/src/routes/page/onboarding/set-numeraire.tsx"

This reverts commit 40306ac13cd3e942674f0949d986026e041cca61.

* Revert "Update apps/extension/src/routes/page/onboarding/set-numeraire.tsx"

This reverts commit c17991e2516d0c748f6ba2670cf88cc9049f5c17.

* Revert "Update apps/extension/src/shared/components/numeraires-form.tsx"

This reverts commit b83c2aa38266b43851a6b3d25774999651b735fe.

* Revert "Update apps/extension/src/shared/components/numeraires-form.tsx"

This reverts commit c92c9e0faf8c94e84335e474df5de631089d4be5.

* add chainId comment

* revert extension changes

---------

Co-authored-by: Jesse Pinho <[email protected]>
  • Loading branch information
Valentine1898 and jessepinho authored Jun 14, 2024
1 parent 7ac610e commit 4e5adc0
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 16 deletions.
8 changes: 6 additions & 2 deletions packages/query/src/block-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ interface QueryClientProps {
querier: RootQuerier;
indexedDb: IndexedDbInterface;
viewServer: ViewServerInterface;
numeraires: Metadata[];
numeraires: AssetId[];
stakingTokenMetadata: Metadata;
}

Expand All @@ -76,7 +76,7 @@ export class BlockProcessor implements BlockProcessorInterface {
private readonly indexedDb: IndexedDbInterface;
private readonly viewServer: ViewServerInterface;
private readonly abortController: AbortController = new AbortController();
private readonly numeraires: Metadata[];
private numeraires: AssetId[];
private readonly stakingTokenMetadata: Metadata;
private syncPromise: Promise<void> | undefined;

Expand Down Expand Up @@ -116,6 +116,10 @@ export class BlockProcessor implements BlockProcessorInterface {

public stop = (r: string) => this.abortController.abort(`Sync stop ${r}`);

setNumeraires(numeraires: AssetId[]): void {
this.numeraires = numeraires;
}

private async syncAndStore() {
// start at next block, or genesis if height is undefined
let currentHeight = (await this.indexedDb.getFullSyncHeight()) ?? -1n;
Expand Down
11 changes: 3 additions & 8 deletions packages/query/src/price-indexer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { BatchSwapOutputData } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/component/dex/v1/dex_pb';
import { IndexedDbInterface } from '@penumbra-zone/types/indexed-db';
import { divideAmounts, isZero, subtractAmounts } from '@penumbra-zone/types/amount';
import {
AssetId,
Metadata,
} from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/asset/v1/asset_pb';
import { AssetId } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/asset/v1/asset_pb';
import { Amount } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/num/v1/num_pb';
import {
getDelta1Amount,
Expand All @@ -16,7 +13,6 @@ import {
getUnfilled1Amount,
getUnfilled2Amount,
} from '@penumbra-zone/getters/batch-swap-output-data';
import { getAssetId } from '@penumbra-zone/getters/metadata';

/**
*
Expand All @@ -41,12 +37,11 @@ export const calculatePrice = (delta: Amount, unfilled: Amount, lambda: Amount):

export const updatePricesFromSwaps = async (
indexedDb: IndexedDbInterface,
numeraires: Metadata[],
numeraires: AssetId[],
swapOutputs: BatchSwapOutputData[],
height: bigint,
) => {
for (const numeraireMetadata of numeraires) {
const numeraireAssetId = getAssetId(numeraireMetadata);
for (const numeraireAssetId of numeraires) {
await deriveAndSavePriceFromBSOD(indexedDb, numeraireAssetId, swapOutputs, height);
}
};
Expand Down
6 changes: 4 additions & 2 deletions packages/services-context/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import {
} from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/keys/v1/keys_pb';
import { ChainRegistryClient } from '@penumbra-labs/registry';
import { AppParameters } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/app/v1/app_pb';
import { AssetId } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/asset/v1/asset_pb';

export interface ServicesConfig {
readonly chainId: string;
readonly idbVersion: number;
readonly grpcEndpoint: string;
readonly walletId: WalletId;
readonly fullViewingKey: FullViewingKey;
readonly numeraires: AssetId[];
}

export class Services implements ServicesInterface {
Expand Down Expand Up @@ -82,7 +84,7 @@ export class Services implements ServicesInterface {
}

private async initializeWalletServices(): Promise<WalletServices> {
const { chainId, grpcEndpoint, walletId, fullViewingKey, idbVersion } = this.config;
const { chainId, grpcEndpoint, walletId, fullViewingKey, idbVersion, numeraires } = this.config;
const querier = new RootQuerier({ grpcEndpoint });
const registryClient = new ChainRegistryClient();
const indexedDb = await IndexedDb.initialize({
Expand All @@ -109,7 +111,7 @@ export class Services implements ServicesInterface {
querier,
indexedDb,
stakingTokenMetadata: registry.getMetadata(registry.stakingAssetId),
numeraires: registry.numeraires.map(numeraires => registry.getMetadata(numeraires)),
numeraires: numeraires,
});

return { viewServer, blockProcessor, indexedDb, querier };
Expand Down
24 changes: 23 additions & 1 deletion packages/storage/src/indexed-db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export class IndexedDb implements IndexedDbInterface {
private readonly u: IbdUpdater,
private readonly c: IdbConstants,
private readonly chainId: string,
private readonly stakingTokenAssetId: AssetId,
) {}

static async initialize({
Expand Down Expand Up @@ -134,7 +135,13 @@ export class IndexedDb implements IndexedDbInterface {
tables: IDB_TABLES,
} satisfies IdbConstants;

const instance = new this(db, new IbdUpdater(db), constants, chainId);
const instance = new this(
db,
new IbdUpdater(db),
constants,
chainId,
registryClient.get(chainId).stakingAssetId,
);
await instance.saveRegistryAssets(registryClient, chainId); // Pre-load asset metadata from registry

const existing0thEpoch = await instance.getEpochByHeight(0n);
Expand Down Expand Up @@ -661,6 +668,21 @@ export class IndexedDb implements IndexedDbInterface {
.filter(price => price.asOfHeight >= minHeight);
}

async clearSwapBasedPrices(): Promise<void> {
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);
if (!price.numeraire?.equals(this.stakingTokenAssetId)) {
await cursor.delete();
}
cursor = await cursor.continue();
}
await tx.done;
}

private determinePriceRelevanceThresholdForAsset(assetMetadata: Metadata): number {
if (assetPatterns.delegationToken.capture(assetMetadata.display)) {
return PRICE_RELEVANCE_THRESHOLDS.delegationToken;
Expand Down
14 changes: 11 additions & 3 deletions 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 All @@ -606,7 +609,7 @@ describe('IndexedDb', () => {
await expect(db.getPricesForAsset(delegationMetadataA, 50n)).resolves.toEqual([
new EstimatedPrice({
pricedAsset: delegationMetadataA.penumbraAssetId!,
numeraire: numeraireAssetId,
numeraire: stakingAssetId,
numerairePerUnit: 1.23,
asOfHeight: 50n,
}),
Expand All @@ -622,12 +625,17 @@ describe('IndexedDb', () => {
await expect(db.getPricesForAsset(delegationMetadataA, 241n)).resolves.toEqual([
new EstimatedPrice({
pricedAsset: delegationMetadataA.penumbraAssetId!,
numeraire: numeraireAssetId,
numeraire: stakingAssetId,
numerairePerUnit: 1.23,
asOfHeight: 50n,
}),
]);
});

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
3 changes: 3 additions & 0 deletions packages/types/src/block-processor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { AssetId } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/asset/v1/asset_pb';

export interface BlockProcessorInterface {
sync(): Promise<void>;
stop(r?: string): void;
setNumeraires(numeraires: AssetId[]): void;
}
1 change: 1 addition & 0 deletions packages/types/src/indexed-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export interface IndexedDbInterface {
height: bigint,
): Promise<void>;
getPricesForAsset(assetMetadata: Metadata, latestBlockHeight: bigint): Promise<EstimatedPrice[]>;
clearSwapBasedPrices(): Promise<void>;

// Add more auction union types as they are created
upsertAuction<T extends DutchAuctionDescription>(
Expand Down

0 comments on commit 4e5adc0

Please sign in to comment.