Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[duplicate] 1231 onboarding numeraire selection2 #1282

Merged
merged 25 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e62fc8a
use numeraire from local storage
Valentine1898 Jun 11, 2024
a7065d5
save multiple numeraires in local storage
Valentine1898 Jun 11, 2024
9d37828
added NumerairesSlice
Valentine1898 Jun 11, 2024
78e5838
add ChangeNumeraires service message
Valentine1898 Jun 12, 2024
eed82c8
numeraires persist
Valentine1898 Jun 12, 2024
c73fcc8
add icons
Valentine1898 Jun 12, 2024
49ca07b
remove onboardNumeraires
Valentine1898 Jun 12, 2024
554bd0f
delete prices when numeraire is changed
Valentine1898 Jun 12, 2024
3d97c0c
refactor & fix numeraires update bug
Valentine1898 Jun 12, 2024
4b521b0
add idb tests
Valentine1898 Jun 13, 2024
6419e7a
fix chain-id
Valentine1898 Jun 13, 2024
f92493b
Update apps/extension/src/shared/components/numeraires-form.tsx
Valentine1898 Jun 14, 2024
f9479fc
Update apps/extension/src/shared/components/numeraires-form.tsx
Valentine1898 Jun 14, 2024
dcc80e3
Update apps/extension/src/routes/page/onboarding/set-numeraire.tsx
Valentine1898 Jun 14, 2024
6430b45
Update apps/extension/src/routes/page/onboarding/set-numeraire.tsx
Valentine1898 Jun 14, 2024
562478a
fix lint
Valentine1898 Jun 14, 2024
2af5fd2
add chainId comment
Valentine1898 Jun 14, 2024
275af0d
Revert "add chainId comment"
Valentine1898 Jun 14, 2024
e2bade0
Revert "fix lint"
Valentine1898 Jun 14, 2024
8f9fd1a
Revert "Update apps/extension/src/routes/page/onboarding/set-numerair…
Valentine1898 Jun 14, 2024
c0dbe4a
Revert "Update apps/extension/src/routes/page/onboarding/set-numerair…
Valentine1898 Jun 14, 2024
f1e76d8
Revert "Update apps/extension/src/shared/components/numeraires-form.tsx"
Valentine1898 Jun 14, 2024
175ea6b
Revert "Update apps/extension/src/shared/components/numeraires-form.tsx"
Valentine1898 Jun 14, 2024
dafeade
add chainId comment
Valentine1898 Jun 14, 2024
b0c8c84
revert extension changes
Valentine1898 Jun 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading