Skip to content

Commit 94c36a9

Browse files
authored
Add getAssetMetadata action to MultichainAssetsController (#5430)
## Explanation This adds an action to the `MultichainAssetsController` called `getAssetMetada` which allows to get a specific asset Metadata. This allows us to get a specific metadata without having to pull the entire controller state. ## References * Related to MetaMask/snaps#3166 ## Changelog <!-- If you're making any consumer-facing changes, list those changes here as if you were updating a changelog, using the template below as a guide. (CATEGORY is one of BREAKING, ADDED, CHANGED, DEPRECATED, REMOVED, or FIXED. For security-related issues, follow the Security Advisory process.) Please take care to name the exact pieces of the API you've added or changed (e.g. types, interfaces, functions, or methods). If there are any breaking changes, make sure to offer a solution for consumers to follow once they upgrade to the changes. Finally, if you're only making changes to development scripts or tests, you may replace the template below with "None". --> ### `@metamask/assets-controller` - **ADDED**: Add `getAssetMetadata` action to `MultichainAssetsController`. ## Checklist - [ ] I've updated the test suite for new or updated code as appropriate - [ ] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [ ] I've highlighted breaking changes using the "BREAKING" category above as appropriate - [ ] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes
1 parent 7523eaf commit 94c36a9

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

packages/assets-controllers/src/MultichainAssetsController/MultichainAssetsController.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,4 +773,49 @@ describe('MultichainAssetsController', () => {
773773
});
774774
});
775775
});
776+
777+
describe('getAssetMetadata', () => {
778+
it('returns the metadata for a given asset', async () => {
779+
const { messenger } = setupController({
780+
state: {
781+
accountsAssets: {
782+
[mockSolanaAccount.id]: mockHandleRequestOnAssetsLookupReturnValue,
783+
},
784+
assetsMetadata: mockGetMetadataReturnValue.assets,
785+
} as MultichainAssetsControllerState,
786+
});
787+
788+
const assetId = 'solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1/slip44:501';
789+
790+
const metadata = messenger.call(
791+
'MultichainAssetsController:getAssetMetadata',
792+
assetId,
793+
);
794+
795+
expect(metadata).toStrictEqual(
796+
mockGetMetadataReturnValue.assets[assetId],
797+
);
798+
});
799+
800+
it('returns undefined if the asset metadata is not found', async () => {
801+
const { messenger } = setupController({
802+
state: {
803+
accountsAssets: {
804+
[mockSolanaAccount.id]: mockHandleRequestOnAssetsLookupReturnValue,
805+
},
806+
assetsMetadata: mockGetMetadataReturnValue.assets,
807+
} as MultichainAssetsControllerState,
808+
});
809+
810+
const assetId =
811+
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/token:EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v';
812+
813+
const metadata = messenger.call(
814+
'MultichainAssetsController:getAssetMetadata',
815+
assetId,
816+
);
817+
818+
expect(metadata).toBeUndefined();
819+
});
820+
});
776821
});

packages/assets-controllers/src/MultichainAssetsController/MultichainAssetsController.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ export function getDefaultMultichainAssetsControllerState(): MultichainAssetsCon
6969
return { accountsAssets: {}, assetsMetadata: {} };
7070
}
7171

72+
export type MultichainAssetsControllerGetAssetMetadataAction = {
73+
type: `${typeof controllerName}:getAssetMetadata`;
74+
handler: MultichainAssetsController['getAssetMetadata'];
75+
};
76+
7277
/**
7378
* Returns the state of the {@link MultichainAssetsController}.
7479
*/
@@ -90,7 +95,8 @@ export type MultichainAssetsControllerStateChangeEvent =
9095
* Actions exposed by the {@link MultichainAssetsController}.
9196
*/
9297
export type MultichainAssetsControllerActions =
93-
MultichainAssetsControllerGetStateAction;
98+
| MultichainAssetsControllerGetStateAction
99+
| MultichainAssetsControllerGetAssetMetadataAction;
94100

95101
/**
96102
* Events emitted by {@link MultichainAssetsController}.
@@ -199,6 +205,8 @@ export class MultichainAssetsController extends BaseController<
199205
'AccountsController:accountAssetListUpdated',
200206
async (event) => await this.#handleAccountAssetListUpdatedEvent(event),
201207
);
208+
209+
this.#registerMessageHandlers();
202210
}
203211

204212
async #handleAccountAssetListUpdatedEvent(
@@ -215,6 +223,27 @@ export class MultichainAssetsController extends BaseController<
215223
);
216224
}
217225

226+
/**
227+
* Constructor helper for registering the controller's messaging system
228+
* actions.
229+
*/
230+
#registerMessageHandlers() {
231+
this.messagingSystem.registerActionHandler(
232+
'MultichainAssetsController:getAssetMetadata',
233+
this.getAssetMetadata.bind(this),
234+
);
235+
}
236+
237+
/**
238+
* Returns the metadata for the given asset
239+
*
240+
* @param asset - The asset to get metadata for
241+
* @returns The metadata for the asset or undefined if not found.
242+
*/
243+
getAssetMetadata(asset: CaipAssetType): FungibleAssetMetadata | undefined {
244+
return this.state.assetsMetadata[asset];
245+
}
246+
218247
/**
219248
* Function to update the assets list for an account
220249
*

0 commit comments

Comments
 (0)