Skip to content

Commit

Permalink
use .env for numeraireAssetId
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentine1898 committed Mar 19, 2024
1 parent 82e357c commit 6d6ebac
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 28 deletions.
1 change: 1 addition & 0 deletions apps/extension/src/service-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const startServices = async () => {
grpcEndpoint,
walletId: wallet0.id,
fullViewingKey: wallet0.fullViewingKey,
numeraireAssetId: USDC_ASSET_ID,
});
await services.initialize();
return services;
Expand Down
5 changes: 0 additions & 5 deletions packages/constants/src/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ export const STAKING_TOKEN_METADATA = localAssets.find(
metadata => metadata.display === STAKING_TOKEN,
)!;

export const NUMERAIRE_TOKEN = 'test_usd';
export const NUMERAIRE_TOKEN_ID = localAssets.find(
metadata => metadata.display === NUMERAIRE_TOKEN,
)!.penumbraAssetId!;

export interface AssetPatterns {
lpNft: RegExp;
delegationToken: RegExp;
Expand Down
12 changes: 10 additions & 2 deletions packages/query/src/block-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ interface QueryClientProps {
querier: RootQuerier;
indexedDb: IndexedDbInterface;
viewServer: ViewServerInterface;
numeraireAssetId: string;
}

const blankTxSource = new CommitmentSource({
Expand All @@ -39,12 +40,14 @@ export class BlockProcessor implements BlockProcessorInterface {
private readonly indexedDb: IndexedDbInterface;
private readonly viewServer: ViewServerInterface;
private readonly abortController: AbortController = new AbortController();
private readonly numeraireAssetId: string;
private syncPromise: Promise<void> | undefined;

constructor({ indexedDb, viewServer, querier }: QueryClientProps) {
constructor({ indexedDb, viewServer, querier, numeraireAssetId }: QueryClientProps) {
this.indexedDb = indexedDb;
this.viewServer = viewServer;
this.querier = querier;
this.numeraireAssetId = numeraireAssetId;
}

// If syncBlocks() is called multiple times concurrently, they'll all wait for
Expand Down Expand Up @@ -237,7 +240,12 @@ export class BlockProcessor implements BlockProcessorInterface {
// so we have to get asset prices from swap results during block scans
// and store them locally in indexed-db.
if (compactBlock.swapOutputs.length) {
await updatePrices(this.indexedDb, compactBlock.swapOutputs, compactBlock.height);
await updatePrices(
this.indexedDb,
this.numeraireAssetId,
compactBlock.swapOutputs,
compactBlock.height,
);
}

// We only query Tendermint for the latest known block height once, when
Expand Down
33 changes: 18 additions & 15 deletions packages/query/src/price-indexer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ import { updatePrices } from './price-indexer';
import { BatchSwapOutputData } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/component/dex/v1/dex_pb';
import { beforeEach, describe, expect, it, Mock, vi } from 'vitest';
import { IndexedDbInterface } from '@penumbra-zone/types/src/indexed-db';
import { NUMERAIRE_TOKEN_ID } from '@penumbra-zone/constants/dist/assets';
import { AssetId } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/asset/v1/asset_pb';
import { base64ToUint8Array } from '@penumbra-zone/types/src/base64';

describe('update prices', () => {
let indexedDbMock: IndexedDbInterface;
const updatePriceMock: Mock = vi.fn();
const height = 123n;

const numeraireAssetId = 'reum7wQmk/owgvGMWMZn/6RFPV24zIKq3W6In/WwZgg=';
const numeraireAsset: AssetId = new AssetId({
inner: base64ToUint8Array(numeraireAssetId),
});
beforeEach(() => {
vi.clearAllMocks();

Expand All @@ -24,25 +27,25 @@ describe('update prices', () => {
new BatchSwapOutputData({
tradingPair: {
asset1: asset1,
asset2: NUMERAIRE_TOKEN_ID,
asset2: numeraireAsset,
},
delta1: { lo: 250n },
lambda2: { lo: 1200n },
unfilled1: { lo: 0n },
}),
];

await updatePrices(indexedDbMock, swapOutputs, height);
await updatePrices(indexedDbMock, numeraireAssetId, swapOutputs, height);
expect(updatePriceMock).toBeCalledTimes(1);
expect(updatePriceMock).toBeCalledWith(asset1, NUMERAIRE_TOKEN_ID, 4.8, height);
expect(updatePriceMock).toBeCalledWith(asset1, numeraireAsset, 4.8, height);
});

it('should update prices correctly for a swapOutput with NUMERAIRE as swapAsset1', async () => {
const asset1 = new AssetId({ inner: new Uint8Array(12) });
const swapOutputs: BatchSwapOutputData[] = [
new BatchSwapOutputData({
tradingPair: {
asset1: NUMERAIRE_TOKEN_ID,
asset1: numeraireAsset,
asset2: asset1,
},
delta2: { lo: 40n },
Expand All @@ -51,17 +54,17 @@ describe('update prices', () => {
}),
];

await updatePrices(indexedDbMock, swapOutputs, height);
await updatePrices(indexedDbMock, numeraireAssetId, swapOutputs, height);
expect(updatePriceMock).toBeCalledTimes(1);
expect(updatePriceMock).toBeCalledWith(asset1, NUMERAIRE_TOKEN_ID, 318.5, height);
expect(updatePriceMock).toBeCalledWith(asset1, numeraireAsset, 318.5, height);
});

it('should not update prices if delta is zero', async () => {
const asset1 = new AssetId({ inner: new Uint8Array(12) });
const swapOutputs: BatchSwapOutputData[] = [
new BatchSwapOutputData({
tradingPair: {
asset1: NUMERAIRE_TOKEN_ID,
asset1: numeraireAsset,
asset2: asset1,
},
delta2: { lo: 0n },
Expand All @@ -70,7 +73,7 @@ describe('update prices', () => {
}),
];

await updatePrices(indexedDbMock, swapOutputs, height);
await updatePrices(indexedDbMock, numeraireAssetId, swapOutputs, height);
expect(updatePriceMock).toBeCalledTimes(0);
});

Expand All @@ -80,24 +83,24 @@ describe('update prices', () => {
new BatchSwapOutputData({
tradingPair: {
asset1: asset1,
asset2: NUMERAIRE_TOKEN_ID,
asset2: numeraireAsset,
},
delta1: { lo: 250n },
lambda2: { lo: 1200n },
unfilled1: { lo: 100n },
}),
];
await updatePrices(indexedDbMock, swapOutputs, height);
await updatePrices(indexedDbMock, numeraireAssetId, swapOutputs, height);
expect(updatePriceMock).toBeCalledTimes(1);
expect(updatePriceMock).toBeCalledWith(asset1, NUMERAIRE_TOKEN_ID, 8, height);
expect(updatePriceMock).toBeCalledWith(asset1, numeraireAsset, 8, height);
});

it('should not update prices if swap is fully unfilled', async () => {
const asset1 = new AssetId({ inner: new Uint8Array(12) });
const swapOutputs: BatchSwapOutputData[] = [
new BatchSwapOutputData({
tradingPair: {
asset1: NUMERAIRE_TOKEN_ID,
asset1: numeraireAsset,
asset2: asset1,
},
delta2: { lo: 100n },
Expand All @@ -106,7 +109,7 @@ describe('update prices', () => {
}),
];

await updatePrices(indexedDbMock, swapOutputs, height);
await updatePrices(indexedDbMock, numeraireAssetId, swapOutputs, height);
expect(updatePriceMock).toBeCalledTimes(0);
});
});
13 changes: 9 additions & 4 deletions packages/query/src/price-indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { IndexedDbInterface } from '@penumbra-zone/types/src/indexed-db';
import { divideAmounts, isZero, subtractAmounts } from '@penumbra-zone/types/src/amount';
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 { NUMERAIRE_TOKEN_ID } from '@penumbra-zone/constants/src/assets';
import {
getDelta1Amount,
getDelta2Amount,
Expand All @@ -14,6 +13,7 @@ import {
getUnfilled1Amount,
getUnfilled2Amount,
} from '@penumbra-zone/getters/src/batch-swap-output-data';
import { base64ToUint8Array } from '@penumbra-zone/types/src/base64';

/**
*
Expand Down Expand Up @@ -48,9 +48,14 @@ export const calculatePrice = (delta: Amount, unfilled: Amount, lambda: Amount):
*/
export const updatePrices = async (
indexedDb: IndexedDbInterface,
numeraireAssetId: string,
swapOutputs: BatchSwapOutputData[],
height: bigint,
) => {
const numeraireAsset: AssetId = new AssetId({
inner: base64ToUint8Array(numeraireAssetId),
});

for (const swapOutput of swapOutputs) {
const swapAsset1 = getSwapAsset1(swapOutput);
const swapAsset2 = getSwapAsset2(swapOutput);
Expand All @@ -59,7 +64,7 @@ export const updatePrices = async (
let pricedAsset: AssetId | undefined = undefined;

// case for trading pair <pricedAsset,numéraire>
if (swapAsset2.equals(NUMERAIRE_TOKEN_ID)) {
if (swapAsset2.equals(numeraireAsset)) {
pricedAsset = swapAsset1;
// numerairePerUnit = lambda2/(delta1-unfilled1)
numerairePerUnit = calculatePrice(
Expand All @@ -69,7 +74,7 @@ export const updatePrices = async (
);
}
// case for trading pair <numéraire,pricedAsset>
else if (swapAsset1.equals(NUMERAIRE_TOKEN_ID)) {
else if (swapAsset1.equals(numeraireAsset)) {
pricedAsset = swapAsset2;
// numerairePerUnit = lambda1/(delta2-unfilled2)
numerairePerUnit = calculatePrice(
Expand All @@ -81,6 +86,6 @@ export const updatePrices = async (

if (pricedAsset === undefined || numerairePerUnit === 0) continue;

await indexedDb.updatePrice(pricedAsset, NUMERAIRE_TOKEN_ID, numerairePerUnit, height);
await indexedDb.updatePrice(pricedAsset, numeraireAsset, numerairePerUnit, height);
}
};
10 changes: 8 additions & 2 deletions packages/services/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ export interface ServicesConfig {
readonly grpcEndpoint?: string;
readonly walletId?: string;
readonly fullViewingKey?: string;
readonly numeraireAssetId: string;
}

const isCompleteServicesConfig = (c: Partial<ServicesConfig>): c is Required<ServicesConfig> =>
c.grpcEndpoint != null && c.idbVersion != null && c.walletId != null && c.fullViewingKey != null;
c.grpcEndpoint != null &&
c.idbVersion != null &&
c.walletId != null &&
c.fullViewingKey != null &&
c.numeraireAssetId != null;

export class Services implements ServicesInterface {
private walletServicesPromise: Promise<WalletServices> | undefined;
Expand Down Expand Up @@ -83,7 +88,7 @@ export class Services implements ServicesInterface {
}

private async initializeWalletServices(): Promise<WalletServices> {
const { walletId, fullViewingKey, idbVersion: dbVersion } = await this.config;
const { walletId, fullViewingKey, idbVersion: dbVersion, numeraireAssetId } = await this.config;
const params = await this.querier.app.appParams();
if (!params.sctParams?.epochDuration) throw new Error('Epoch duration unknown');
const {
Expand All @@ -110,6 +115,7 @@ export class Services implements ServicesInterface {
viewServer,
querier: this.querier,
indexedDb,
numeraireAssetId: numeraireAssetId,
});

return { viewServer, blockProcessor, indexedDb, querier: this.querier };
Expand Down

0 comments on commit 6d6ebac

Please sign in to comment.