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

556 add prices table to indexeddb #764

Merged
merged 9 commits into from
Mar 19, 2024

Conversation

Valentine1898
Copy link
Contributor

@Valentine1898 Valentine1898 commented Mar 14, 2024

partially close #556

@Valentine1898 Valentine1898 force-pushed the 556-add-prices-table-to-indexeddb branch from 612b0e0 to 7e2ddeb Compare March 15, 2024 18:38
Comment on lines 14 to 18
export const NUMERAIRE_TOKEN = 'test_usd';
export const NUMERAIRE_TOKEN_ID = localAssets.find(
metadata => metadata.display === NUMERAIRE_TOKEN,
)!.penumbraAssetId!;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future we can easily change the NUMERAIRE_TOKEN if necessary

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See apps/extension/.env. We should try to use env variables for this given mainnet/testnet will have a different numeraire. Looks like we started this already with USDC_ASSET_ID. We should update or replace that.

let numerairePerUnit = 0;
let pricedAsset: AssetId | undefined = undefined;

// case for trading pair <pricedAsset,numéraire>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot know in advance the order of assets in a trading pair, so we must handle both cases

Comment on lines 449 to 455
const calculatePrice = (delta: Amount, unfilled: Amount, lambda: Amount): number => {
const filledAmount = subtractAmounts(delta, unfilled);
return isZero(delta) || isZero(lambda) || isZero(filledAmount)
? 0
: divideAmounts(lambda, filledAmount).toNumber();
};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -171,6 +178,10 @@ export interface PenumbraDb extends DBSchema {
key: string; // bech32-encoded validator identity key
value: Jsonified<ValidatorInfo>;
};
PRICES: {
key: string[]; // composite key [base64 EstimatedPrice['priced_asset'][inner'], base64 EstimatedPrice['numeraire']['inner']]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the composite key ensures that we will not have two records for the combination priced_asset:numeraire and will always store only the most recent value

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also do:

    key: [
      Jsonified<Required<EstimatedPrice>['pricedAsset']['inner']>,
      Jsonified<Required<EstimatedPrice>['numeraire']['inner']>,
    ];

@Valentine1898 Valentine1898 force-pushed the 556-add-prices-table-to-indexeddb branch from 8b85b79 to 7a10bda Compare March 18, 2024 12:30
@Valentine1898 Valentine1898 marked this pull request as ready for review March 18, 2024 12:30
@Valentine1898 Valentine1898 changed the title WIP: 556 add prices table to indexeddb 556 add prices table to indexeddb Mar 18, 2024
Comment on lines 14 to 18
export const NUMERAIRE_TOKEN = 'test_usd';
export const NUMERAIRE_TOKEN_ID = localAssets.find(
metadata => metadata.display === NUMERAIRE_TOKEN,
)!.penumbraAssetId!;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See apps/extension/.env. We should try to use env variables for this given mainnet/testnet will have a different numeraire. Looks like we started this already with USDC_ASSET_ID. We should update or replace that.

Comment on lines 253 to 254
if (compactBlock.swapOutputs.length)
await this.updatePrices(compactBlock.swapOutputs, compactBlock.height);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add brackets here

@@ -388,4 +409,46 @@ export class BlockProcessor implements BlockProcessorInterface {
await this.indexedDb.upsertValidatorInfo(validatorInfoResponse.validatorInfo);
}
}

private async updatePrices(swapOutputs: BatchSwapOutputData[], height: bigint) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add documentation (here or calculatePrice()) on some of these terms like delta/lambda/unfilled means?


await this.indexedDb.updatePrice(pricedAsset, NUMERAIRE_TOKEN_ID, numerairePerUnit, height);
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's worth adding some tests for this. Some ideas:

  • We move updatePrices into its own file as a separate function (not on the class). This would require passing in the deps (like storage) into the function. When separated, we can write tests.

  • If not, just start creating a test suite block-processor.test.ts passing in deps on the class constructor.

});

await this.u.update({
table: 'PRICES',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing the key right? or is it automatic?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works automatically if the key is specified via keypath

@@ -171,6 +178,10 @@ export interface PenumbraDb extends DBSchema {
key: string; // bech32-encoded validator identity key
value: Jsonified<ValidatorInfo>;
};
PRICES: {
key: string[]; // composite key [base64 EstimatedPrice['priced_asset'][inner'], base64 EstimatedPrice['numeraire']['inner']]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also do:

    key: [
      Jsonified<Required<EstimatedPrice>['pricedAsset']['inner']>,
      Jsonified<Required<EstimatedPrice>['numeraire']['inner']>,
    ];

@Valentine1898 Valentine1898 force-pushed the 556-add-prices-table-to-indexeddb branch from 7a10bda to a7a91cd Compare March 18, 2024 18:52
@Valentine1898 Valentine1898 force-pushed the 556-add-prices-table-to-indexeddb branch from c8a4dae to 6d6ebac Compare March 19, 2024 00:45
@Valentine1898 Valentine1898 requested a review from grod220 March 19, 2024 00:52
Copy link
Collaborator

@grod220 grod220 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job with those tests. Think this is fine for now, but we should have a follow up PR enabling multiple numeraires.

@@ -110,6 +115,7 @@ export class Services implements ServicesInterface {
viewServer,
querier: this.querier,
indexedDb,
numeraireAssetId: numeraireAssetId,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think you can do:

- numeraireAssetId: numeraireAssetId,
+ numeraireAssetId,

@Valentine1898 Valentine1898 self-assigned this Mar 19, 2024
@Valentine1898 Valentine1898 merged commit 958aaea into main Mar 19, 2024
6 checks passed
@Valentine1898 Valentine1898 deleted the 556-add-prices-table-to-indexeddb branch March 19, 2024 09:11
@Valentine1898 Valentine1898 mentioned this pull request Mar 19, 2024
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add prices table to IndexedDB
2 participants