-
-
Notifications
You must be signed in to change notification settings - Fork 290
feat(assets-controller): add getAsset #9521
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -181,6 +181,7 @@ | |
| 'getAssets', | ||
| 'getAssetsBalance', | ||
| 'getAssetMetadata', | ||
| 'getAsset', | ||
| 'getAssetsPrice', | ||
| 'getExchangeRatesForBridge', | ||
| 'getStateForTransactionPay', | ||
|
|
@@ -1775,6 +1776,51 @@ | |
| return this.state.assetsInfo[assetId] as AssetMetadata | undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Get a single combined asset (balance + metadata + price + computed | ||
| * `fiatValue`) for an account directly from controller state. | ||
| * | ||
| * Reuses the same state-composition and filtering logic as `getAssets` | ||
| * (balance and metadata are required, a missing price falls back to | ||
| * `{ price: 0, lastUpdated: 0 }` with `fiatValue: 0`, and hidden or | ||
| * otherwise filtered assets are excluded) so the returned shape never | ||
| * drifts from `getAssets`. Reads from current state only and does not | ||
| * trigger a data-source refresh. | ||
| * | ||
| * @param accountId - The account ID (`InternalAccount.id`, not an address). | ||
| * @param assetId - The CAIP-19 asset ID including chain scope | ||
| * (e.g. `eip155:1/erc20:0x...`). | ||
| * @returns The combined `Asset`, or `undefined` when no complete | ||
| * renderable asset (balance + metadata) exists for the account/asset pair. | ||
| * @throws If `accountId` is empty or `assetId` is not a valid CAIP-19 asset ID. | ||
| */ | ||
| getAsset(accountId: AccountId, assetId: Caip19AssetId): Asset | undefined { | ||
| if (typeof accountId !== 'string' || accountId.length === 0) { | ||
| throw new Error( | ||
| 'AssetsController.getAsset: accountId must be a non-empty string', | ||
| ); | ||
| } | ||
|
|
||
| let normalizedAssetId: Caip19AssetId; | ||
| try { | ||
| normalizedAssetId = normalizeAssetId(assetId); | ||
| } catch { | ||
| throw new Error( | ||
| `AssetsController.getAsset: invalid CAIP-19 assetId "${assetId}"`, | ||
| ); | ||
| } | ||
|
|
||
| const chainId = extractChainId(normalizedAssetId); | ||
|
|
||
| const assets = this.#getAssetsFromState( | ||
| [{ id: accountId }], | ||
| [chainId], | ||
| ['fungible'], | ||
| ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Skips enabled-chain filteringMedium Severity
Reviewed by Cursor Bugbot for commit 6aa280d. Configure here.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well this function will be used by Snaps independently from the enabledChains
Comment on lines
+1815
to
+1819
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit - A tad nuts that we are calculating all assets, only to return a single asset here. This is okay for now, we can raise/improve moving forward. |
||
|
|
||
| return assets[accountId]?.[normalizedAssetId]; | ||
| } | ||
|
|
||
| async getAssetsPrice( | ||
| accounts: InternalAccount[], | ||
| options?: { | ||
|
|
@@ -2698,7 +2744,7 @@ | |
| } | ||
|
|
||
| #getAssetsFromState( | ||
| accounts: InternalAccount[], | ||
| accounts: Pick<InternalAccount, 'id'>[], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very good! Keep interface minimal! |
||
| chainIds: ChainId[], | ||
| assetTypes: AssetType[], | ||
| ): Record<AccountId, Record<Caip19AssetId, Asset>> { | ||
|
|
@@ -2725,7 +2771,7 @@ | |
| const metadata = metadataRaw; | ||
|
|
||
| // Skip hidden assets (assetPreferences) | ||
| const prefs = this.state.assetPreferences[typedAssetId]; | ||
Check warningCode scanning / CodeQL Prototype-polluting assignment Medium
This assignment may alter Object.prototype if a malicious '__proto__' string is injected from
library input Error loading related location Loading |
||
| if (prefs?.hidden) { | ||
| continue; | ||
| } | ||
|
|
@@ -3671,6 +3717,7 @@ | |
| this.messenger.unregisterActionHandler('AssetsController:getAssets'); | ||
| this.messenger.unregisterActionHandler('AssetsController:getAssetsBalance'); | ||
| this.messenger.unregisterActionHandler('AssetsController:getAssetMetadata'); | ||
| this.messenger.unregisterActionHandler('AssetsController:getAsset'); | ||
| this.messenger.unregisterActionHandler('AssetsController:getAssetsPrice'); | ||
| this.messenger.unregisterActionHandler( | ||
| 'AssetsController:getExchangeRatesForBridge', | ||
|
|
||


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.
🌶️ this test file is too big, impossible to read!
Thoughts on:
it.each)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.
I'm fine keeping this as is... but I'd like to pencil in test cleanup.