-
Notifications
You must be signed in to change notification settings - Fork 639
feat: Add AssetSelector
#3166
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
Merged
Merged
feat: Add AssetSelector
#3166
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
c2e22af
Add `AssetSelector` to `snaps-sdk`
GuillaumeRx 14de4ec
Wire up to `SnapInterfaceController`
GuillaumeRx aab5155
update manifests
GuillaumeRx 0ff420d
fix `createInterface` tests
GuillaumeRx c57c39c
Fix issues
GuillaumeRx c94e7d0
add account handling
GuillaumeRx dd256eb
revert send flow snap
GuillaumeRx 4a0c7fe
refactor to only support non-EIP155 values
GuillaumeRx 55c2f06
address requested changes
GuillaumeRx bd0292b
update after rebase
GuillaumeRx 182ea69
address requested changes
GuillaumeRx 77cff65
update coverage
GuillaumeRx b94d265
update comment
GuillaumeRx 89f02b6
use an UUID to mock account ID
GuillaumeRx ae62d8b
validate that `AssetSelector` throws if different addresses are passed
GuillaumeRx 4e25d4e
fix comment
GuillaumeRx 0271d80
fix import
GuillaumeRx d37ac4f
Update JSDoc
GuillaumeRx 42e7ae1
Merge `NonEip155CaipAccountIdsMatchedByAddressAndNamespaceStruct` and…
GuillaumeRx 886264c
refactor utils for `AssetSelector`
GuillaumeRx 481b734
update comment
GuillaumeRx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| { | ||
| "branches": 93.31, | ||
| "functions": 97.05, | ||
| "lines": 98.25, | ||
| "statements": 97.98 | ||
| "branches": 93.46, | ||
| "functions": 97.36, | ||
| "lines": 98.33, | ||
| "statements": 98.06 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ import { | |
| ContentType, | ||
| } from '@metamask/snaps-sdk'; | ||
| import { | ||
| AssetSelector, | ||
| Box, | ||
| Field, | ||
| FileInput, | ||
|
|
@@ -159,6 +160,52 @@ describe('SnapInterfaceController', () => { | |
| expect(state).toStrictEqual({ foo: { bar: null } }); | ||
| }); | ||
|
|
||
| it('calls the multichain asset controller to construct an asset selector state', async () => { | ||
| const rootMessenger = getRootSnapInterfaceControllerMessenger(); | ||
| const controllerMessenger = | ||
| getRestrictedSnapInterfaceControllerMessenger(rootMessenger); | ||
|
|
||
| // eslint-disable-next-line no-new | ||
| new SnapInterfaceController({ | ||
| messenger: controllerMessenger, | ||
| }); | ||
|
|
||
| const components = ( | ||
| <AssetSelector | ||
| name="foo" | ||
| addresses={[ | ||
| 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp:7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKv', | ||
| ]} | ||
| value="solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:105" | ||
| /> | ||
| ); | ||
|
|
||
| const interfaceId = await rootMessenger.call( | ||
| 'SnapInterfaceController:createInterface', | ||
| MOCK_SNAP_ID, | ||
| components, | ||
| ); | ||
|
|
||
| expect(rootMessenger.call).toHaveBeenNthCalledWith( | ||
| 4, | ||
| 'MultichainAssetsController:getState', | ||
| ); | ||
GuillaumeRx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const { state } = rootMessenger.call( | ||
| 'SnapInterfaceController:getInterface', | ||
| MOCK_SNAP_ID, | ||
| interfaceId, | ||
| ); | ||
|
|
||
| expect(state).toStrictEqual({ | ||
| foo: { | ||
| asset: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:105', | ||
| name: 'Solana', | ||
| symbol: 'SOL', | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('can create a new interface from JSX', async () => { | ||
| const rootMessenger = getRootSnapInterfaceControllerMessenger(); | ||
| const controllerMessenger = | ||
|
|
@@ -412,6 +459,116 @@ describe('SnapInterfaceController', () => { | |
| ); | ||
| }); | ||
|
|
||
| it('throws if a link tries to navigate to a snap that is not installed', async () => { | ||
| const rootMessenger = getRootSnapInterfaceControllerMessenger(); | ||
| const controllerMessenger = getRestrictedSnapInterfaceControllerMessenger( | ||
| rootMessenger, | ||
| false, | ||
| ); | ||
|
|
||
| rootMessenger.registerActionHandler( | ||
| 'PhishingController:maybeUpdateState', | ||
| jest.fn(), | ||
| ); | ||
|
|
||
| rootMessenger.registerActionHandler( | ||
| 'SnapController:get', | ||
| () => undefined, | ||
| ); | ||
|
|
||
| // eslint-disable-next-line no-new | ||
| new SnapInterfaceController({ | ||
| messenger: controllerMessenger, | ||
| }); | ||
|
|
||
| const element = ( | ||
| <Box> | ||
| <Text> | ||
| Foo <Link href={`metamask://snap/${MOCK_SNAP_ID}/home`}>Bar</Link> | ||
| </Text> | ||
| </Box> | ||
| ); | ||
|
|
||
| await expect( | ||
| rootMessenger.call( | ||
| 'SnapInterfaceController:createInterface', | ||
| MOCK_SNAP_ID, | ||
| element, | ||
| ), | ||
| ).rejects.toThrow( | ||
| 'Invalid URL: The Snap being navigated to is not installed.', | ||
| ); | ||
|
|
||
| expect(rootMessenger.call).toHaveBeenNthCalledWith( | ||
| 3, | ||
| 'SnapController:get', | ||
| MOCK_SNAP_ID, | ||
| ); | ||
| }); | ||
|
Comment on lines
+462
to
+507
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. Added a missing test for a non-related feature because it was bothering me |
||
|
|
||
| it('throws if an address passed to an asset selector is not available in the client', async () => { | ||
| const rootMessenger = getRootSnapInterfaceControllerMessenger(); | ||
| const controllerMessenger = getRestrictedSnapInterfaceControllerMessenger( | ||
| rootMessenger, | ||
| false, | ||
| ); | ||
|
|
||
| rootMessenger.registerActionHandler( | ||
| 'PhishingController:maybeUpdateState', | ||
| jest.fn(), | ||
| ); | ||
|
|
||
| rootMessenger.registerActionHandler( | ||
| 'PhishingController:testOrigin', | ||
| () => ({ result: true, type: 'all' }), | ||
| ); | ||
|
|
||
| rootMessenger.registerActionHandler( | ||
| 'MultichainAssetsController:getState', | ||
| () => ({ | ||
| assetsMetadata: {}, | ||
| accountsAssets: {}, | ||
| }), | ||
| ); | ||
|
|
||
| rootMessenger.registerActionHandler( | ||
| 'AccountsController:getAccountByAddress', | ||
| () => undefined, | ||
| ); | ||
|
|
||
| // eslint-disable-next-line no-new | ||
| new SnapInterfaceController({ | ||
| messenger: controllerMessenger, | ||
| }); | ||
|
|
||
| const element = ( | ||
| <Box> | ||
| <AssetSelector | ||
| name="foo" | ||
| addresses={[ | ||
| 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp:7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKv', | ||
| ]} | ||
| /> | ||
| </Box> | ||
| ); | ||
|
|
||
| await expect( | ||
| rootMessenger.call( | ||
| 'SnapInterfaceController:createInterface', | ||
| MOCK_SNAP_ID, | ||
| element, | ||
| ), | ||
| ).rejects.toThrow( | ||
| 'Could not find account for address: solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp:7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKv', | ||
| ); | ||
|
|
||
| expect(rootMessenger.call).toHaveBeenNthCalledWith( | ||
| 3, | ||
| 'AccountsController:getAccountByAddress', | ||
| '7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKv', | ||
| ); | ||
| }); | ||
|
|
||
| it('throws if UI content is too large', async () => { | ||
| const rootMessenger = getRootSnapInterfaceControllerMessenger(); | ||
| const controllerMessenger = getRestrictedSnapInterfaceControllerMessenger( | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.