-
-
Notifications
You must be signed in to change notification settings - Fork 166
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
feat: Implement market data by Chain ID #4206
base: main
Are you sure you want to change the base?
Conversation
f8009ff
to
69ff1aa
Compare
295320e
to
282e7b2
Compare
33e4ed1
to
4f1966f
Compare
@salimtb I spoke to the shared libraries devs at office hours. Their recommendation was to make a breaking change to move to the new structure, rather than maintain backwards compatibility with the old structure. Because having the same data in multiple places has been a source of bugs. That would mean removing I think it'd be good to keep This will probably be difficult to get on mobile, until it has upgraded to a more recent version of assets controllers. We may want to help accelerate this vs trying a tricky patch |
This comment was marked as outdated.
This comment was marked as outdated.
4f1966f
to
4ffca63
Compare
This comment was marked as outdated.
This comment was marked as outdated.
LGTM |
packages/assets-controllers/src/token-prices-service/codefi-v2.ts
Outdated
Show resolved
Hide resolved
packages/assets-controllers/src/token-prices-service/codefi-v2.ts
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay in reviewing. I made some suggestions below, mostly having to do with naming and updating of JSDocs.
packages/assets-controllers/src/token-prices-service/codefi-v2.ts
Outdated
Show resolved
Hide resolved
packages/assets-controllers/src/token-prices-service/codefi-v2.ts
Outdated
Show resolved
Hide resolved
packages/assets-controllers/src/token-prices-service/codefi-v2.ts
Outdated
Show resolved
Hide resolved
packages/assets-controllers/src/token-prices-service/codefi-v2.test.ts
Outdated
Show resolved
Hide resolved
0d7b2f6
to
4dbb8a5
Compare
Be sure to update PR description/title to reflect the broader market data being added. Since it's more than just the daily % change now. |
4dbb8a5
to
0b3af00
Compare
packages/assets-controllers/src/token-prices-service/codefi-v2.ts
Outdated
Show resolved
Hide resolved
094de65
to
cc0c0a2
Compare
(obj, [tokenAddress, tokenPrice]) => { | ||
return { | ||
// fetch for native token | ||
if (tokenAddresses.length === 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, but I'm still not sure why we're doing this.
The fetchTokenPrices
method always passes a zero address as the first argument. Therefore, it seems that even if tokenAddresses
is empty, tokenPricesByTokenAddress
will always contain a property which is the zero address. Therefore, it seems that we already have the functionality we need, and we don't need to do this explicitly. I would expect that this would be enough:
const tokenPricesByTokenAddress = await reduceInBatchesSerially<
Hex,
Awaited<ReturnType<AbstractTokenPricesService['fetchTokenPrices']>>
>({
values: [...tokenAddresses].sort(),
batchSize: TOKEN_PRICES_BATCH_SIZE,
eachBatch: async (allTokenPricesByTokenAddress, batch) => {
const tokenPricesByTokenAddressForBatch =
await this.#tokenPricesService.fetchTokenPrices({
tokenAddresses: batch,
chainId,
currency: nativeCurrency,
});
return {
...allTokenPricesByTokenAddress,
...tokenPricesByTokenAddressForBatch,
};
},
initialResult: {},
});
return Object.entries(tokenPricesByTokenAddress).reduce(
(obj, [tokenAddress, token]) => {
return {
...obj,
[tokenAddress.toLowerCase()]: token,
};
},
{},
);
Is this true?
One other thing that's interesting to me is that in this second call, we are passing this.config.chainId
. If we do truly want to keep this code, I feel that we should use chainId
instead. this.config.chainId
stores the globally selected chain ID, but chainId
may be a different chain than the globally selected version (side note: this controller uses two different ways of polling, and eventually updateExchangeRates
and this.config.chainId
will go away; this is because dapps may want to connect to a different chain than the user has selected in MetaMask).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hey @mcmire ,
sorry, maybe my explanation wasn't clear,
to elucidate why the fetch operation for the native token is maintained even when tokenAddresses
is an empty array, let's delve into the specifics of your proposal and clarify why it falls short in scenarios where a user hasn't imported any tokens and only holds the native token.
The reduceInBatchesSerially
function is designed to process a collection of values in batches, determined by the values
parameter. This batching mechanism is crucial for handling large datasets efficiently by breaking them down into manageable chunks.
In the scenario at hand, we encounter a situation where tokenAddresses
is an empty array. This directly impacts the reduceInBatchesSerially
function because it relies on the values array to partition the data into batches. With tokenAddresses
being empty, the function identifies zero batches to process. Consequently, it bypasses the loop entirely ( check the function reduceInBatchesSerially
), leading to the immediate return of the initialResult
, which, in this context, is an empty object. As a result, our intended API call to fetch token prices is never executed.
This behavior underscores the necessity of retaining the fetch operation for the native token outside of the reduceInBatchesSerially
function when tokenAddresses.length === 0
. By doing so, we ensure that essential market data for the native token is still retrieved and processed, even in the absence of any imported tokens. This approach guarantees that users solely holding the native token are not overlooked and that their token data is accurately reflected in the application.
In summary, when the list of token addresses is empty, our function reduceInBatchesSerially
skips processing and returns an empty result. This is why we must ensure that data for the native token is fetched separately, guaranteeing that users holding only the native token still receive relevant market information.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mcmire fixed for the chainId
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, gotcha. Thanks for the explanation. It sounds like a better fix would be to move the batching code into the service object. This way we can ensure that token addresses contains the zero address first before attempting to iterate over them. This is something that I had meant to do when I introduced that code but didn't get around to it.
I guess we don't have to do that here, though, we can do that in another PR.
2e06ec0
to
8271d75
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks for your changes.
Explanation
This pull request introduces a significant enhancement to our application's functionality by integrating a new feature that presents market data more comprehensively. This includes updates such as daily percentage changes and the fluctuation in amounts for a variety of assets. To accommodate this, we've expanded our state structure to encapsulate a broader array of data points. Below is a snippet illustrating the enriched data now available:
As a result, the TokenRatesController state has been meticulously updated to include this marketData, enriching our application with a more detailed and dynamic representation of asset performance. This enhancement not only broadens the scope of information we provide but also elevates the user experience by offering a granular view of market trends and asset valuations.
References
Changelog
We've introduced a new marketData property to the application's state, providing a comprehensive overview of market-related information for various assets. This enhancement brings a wealth of data directly into our application.
This update significantly enriches the data available within our application, offering users detailed insights into asset performance and market trends. The marketData property is meticulously structured to ensure easy access to a wide array of information, enhancing the overall user experience by providing a granular and dynamic view of the market.
@metamask/package-assets-controllers
marketData
to TokenRatesStatecontractExchangeRates
from TokenRatesStatecontractExchangeRatesByChainId
from TokenRatesStateChecklist