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

794 improve pricing method #916

Merged
merged 11 commits into from
Apr 10, 2024
Merged

794 improve pricing method #916

merged 11 commits into from
Apr 10, 2024

Conversation

Valentine1898
Copy link
Contributor

@Valentine1898 Valentine1898 commented Apr 9, 2024

This PR contains some improvements on asset pricing

  • added support for multiple NUMERAIRES, allowing customers to determine for themselves which price estimate to use for display
  • added price relevance threshold depending on asset type. Crypto assets are very volatile and we can't use too old prices to display estimated values

Close #794

@Valentine1898 Valentine1898 linked an issue Apr 9, 2024 that may be closed by this pull request
2 tasks
@@ -6,6 +6,14 @@ export const localAssets: Metadata[] = LocalAssetRegistry.map(a =>
Metadata.fromJson(a as JsonValue),
);

export const NUMERAIRE_DENOMS: string[] = ['test_usd', 'usdc'];
export const NUMERAIRES: Metadata[] = localAssets.filter(m => NUMERAIRE_DENOMS.includes(m.display));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I assume we will have two different local assets registy for testnet and mainnet. And we will have to revise the definition of NUMERAIRES

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, I'm working on related registry stuff now


const aggregator = new BalancesAggregator(ctx, indexedDb);
const latestBlockHeight = await querier.tendermint.latestBlockHeight();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's better to use latestBlockHeight rather than fullSyncHeight to avoid showing old prices during the sync process

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, I see. Can you add a code comment above about this?

Comment on lines +603 to +609
return results
.map(price => EstimatedPrice.fromJson(price))
.filter(price => price.asOfHeight >= minHeight);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I decided to do this in-memory filtering because we won't have a lot of price records for each asset. And using a cursor or double index would make the code and its support much more complicated

@Valentine1898 Valentine1898 force-pushed the 794-improve-pricing-method branch from d932701 to a69f97a Compare April 9, 2024 22:45
@Valentine1898 Valentine1898 requested review from jessepinho and grod220 and removed request for jessepinho April 9, 2024 23:42
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! One more thing I noticed. It looks like a good number of prices in the db have 1 for the numerairePerUnit field. The remaining all appear to be between 1 and ~1.2.

Screenshot 2024-04-10 at 8 57 39 AM

Feel free to merge and follow up with another PR if it requires a good deal more of investigation.

@@ -6,6 +6,14 @@ export const localAssets: Metadata[] = LocalAssetRegistry.map(a =>
Metadata.fromJson(a as JsonValue),
);

export const NUMERAIRE_DENOMS: string[] = ['test_usd', 'usdc'];
export const NUMERAIRES: Metadata[] = localAssets.filter(m => NUMERAIRE_DENOMS.includes(m.display));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, I'm working on related registry stuff now


const aggregator = new BalancesAggregator(ctx, indexedDb);
const latestBlockHeight = await querier.tendermint.latestBlockHeight();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, I see. Can you add a code comment above about this?

const assetId = getAssetId.optional()(new Metadata(denomMetadata));
if (assetId?.inner && !this.estimatedPriceByPricedAsset[uint8ArrayToBase64(assetId.inner)]) {
const prices = await this.indexedDb.getPricesForAsset(new AssetId(assetId));
const assetMetadata = new Metadata(denomMetadata);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder why assetMetadataById() returns a partial 🤔 . You can also do:

const prices = await this.indexedDb.getPricesForAsset(
    denomMetadata as Metadata,  
    this.latestBlockHeight,
);

@Valentine1898
Copy link
Contributor Author

Nice job! One more thing I noticed. It looks like a good number of prices in the db have 1 for the numerairePerUnit field. The remaining all appear to be between 1 and ~1.2.

Screenshot 2024-04-10 at 8 57 39 AM Feel free to merge and follow up with another PR if it requires a good deal more of investigation.

These are all prices for delegation tokens, which are calculated based on the validator's exchange rate.
The fact that all these prices are in the range of 1.01 - 1.4 is exactly normal. As for price 1, I assume that if the validator is not in the active validator set, its ExchangeRate is equal to one. Going to check this out, and will open a new PR if necessary

@Valentine1898 Valentine1898 merged commit 6e87172 into main Apr 10, 2024
6 checks passed
@Valentine1898 Valentine1898 deleted the 794-improve-pricing-method branch April 10, 2024 09:45
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.

Improve pricing method
2 participants