-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat: use migration selectors for new assets controller #26975
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
66 commits
Select commit
Hold shift + click to select a range
1413fd1
account tracker controller selector
bergarces 421281a
Merge branch 'main' into assets-selectors-account-tracker-controller
bergarces ed99a09
correct state path
bergarces a16c340
replace state references
bergarces aa59559
more references
bergarces 718d22e
further references
bergarces 5e42214
fix name
bergarces 240a360
missing references
bergarces 17bd96a
fix selector
bergarces 8c616ac
tests
bergarces 0fc975e
Merge branch 'main' into assets-selectors-account-tracker-controller
bergarces 1f1041d
tests
bergarces 5f85916
Merge branch 'main' into assets-selectors-account-tracker-controller
bergarces 3adacc4
fix test
bergarces 2439059
tests
bergarces 1321917
fix for test
bergarces 4b2ea5c
Merge branch 'main' into assets-selectors-account-tracker-controller
bergarces 72ac618
update packages
bergarces 42ec772
lints
bergarces cfd055b
Merge branch 'main' into assets-selectors-account-tracker-controller
bergarces d808acc
Merge branch 'main' into assets-selectors-account-tracker-controller
bergarces d8596ce
fixes
bergarces b402a2c
remove root messenger
bergarces e29fa67
fix engine tests
bergarces 39672c6
Merge branch 'main' into assets-selectors-account-tracker-controller
bergarces 304823b
revert change
bergarces 0af852b
Merge branch 'main' into assets-selectors-account-tracker-controller
bergarces 8f98854
lockfile
bergarces 9d4c0cf
change import type
bergarces 30017e4
Merge branch 'main' into assets-selectors-account-tracker-controller
bergarces 2bc5ccd
stop test error
bergarces dd53b5e
Merge branch 'main' into assets-selectors-account-tracker-controller
bergarces 4d78cdc
fix dependency
bergarces a479c0d
Merge branch 'main' into assets-selectors-account-tracker-controller
bergarces c10b528
Merge branch 'main' into assets-selectors-account-tracker-controller
bergarces 4a26175
Merge branch 'main' into assets-selectors-account-tracker-controller
bergarces 8c40516
update package versions
bergarces 94cc0cf
feature flag simplification
bergarces ece6d5d
Merge branch 'main' into assets-selectors-account-tracker-controller
bergarces 5c7adf8
Merge branch 'main' into assets-selectors-account-tracker-controller
bergarces 5e9cbc8
remove default state
bergarces 0546702
remove import
bergarces ea78721
remove unnecessary tests
bergarces e75f6d8
Merge branch 'main' into assets-selectors-account-tracker-controller
bergarces d07c864
fix mocks
bergarces 286b2a4
fix test
bergarces 87a8198
changes to selectors
bergarces 0ca3f90
fix tests
bergarces 476000a
Merge branch 'main' into assets-selectors-account-tracker-controller
bergarces 8f9ee3d
upgrade packages
bergarces 7f58b60
Merge branch 'main' into assets-selectors-account-tracker-controller
bergarces 21a8ab1
bump assets controllers
bergarces 115a9f3
Merge branch 'main' into bump-assets-controllers-105
bergarces 5391c7e
unit test
bergarces f675d67
remove type inference
bergarces f5b2613
test fix
bergarces fb32480
migration selectors
bergarces 0246d21
revert change
bergarces 03d43c2
fix test
bergarces cae99de
Merge branch 'main' into add-assets-migration-selectors
bergarces c7dbb43
fix tests
bergarces 8c1e847
deprecation notices
bergarces 74c0c36
Merge branch 'add-assets-migration-selectors' into assets-selectors-a…
bergarces cb66d5e
missing
bergarces e47db1d
Merge branch 'main' into assets-selectors-account-tracker-controller
bergarces e6e8a54
Merge branch 'main' into assets-selectors-account-tracker-controller
bergarces 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import { createDeepEqualSelector } from '../util'; | ||
| import { selectIsAssetsUnifyStateEnabled } from '../featureFlagController/assetsUnifyState'; | ||
| import { AccountTrackerControllerState } from '@metamask/assets-controllers'; | ||
| import { isEvmAccountType } from '@metamask/keyring-api'; | ||
| import { toChecksumHexAddress } from '@metamask/controller-utils'; | ||
| import { | ||
| bigIntToHex, | ||
| CaipAssetType, | ||
| Hex, | ||
| parseCaipAssetType, | ||
| } from '@metamask/utils'; | ||
| import { decimalToPrefixedHex } from '../../util/conversions'; | ||
| import { AssetsControllerState } from '@metamask/assets-controller'; | ||
| import { AccountsControllerState } from '@metamask/accounts-controller'; | ||
|
|
||
| // ChainId (hex) -> AccountAddress (hex checksummed) -> Balance (hex) | ||
| export const getAccountTrackerControllerAccountsByChainId = | ||
| createDeepEqualSelector( | ||
| [ | ||
| selectIsAssetsUnifyStateEnabled, | ||
| (state) => state.AccountTrackerController?.accountsByChainId ?? {}, | ||
| (state) => state.AssetsController?.assetsBalance ?? {}, | ||
| (state) => state.AssetsController?.assetsInfo ?? {}, | ||
| (state) => state.AccountsController?.internalAccounts?.accounts ?? {}, | ||
| ], | ||
| ( | ||
| isAssetsUnifyStateEnabled: boolean, | ||
| accountsByChainId: AccountTrackerControllerState['accountsByChainId'], | ||
| assetsBalance: AssetsControllerState['assetsBalance'], | ||
| assetsInfo: AssetsControllerState['assetsInfo'], | ||
| internalAccountsById: AccountsControllerState['internalAccounts']['accounts'], | ||
| ) => { | ||
| if (!isAssetsUnifyStateEnabled) { | ||
| return accountsByChainId; | ||
| } | ||
|
|
||
| const result: AccountTrackerControllerState['accountsByChainId'] = {}; | ||
|
|
||
| for (const [accountId, accountBalances] of Object.entries( | ||
| assetsBalance, | ||
| )) { | ||
| const internalAccount = internalAccountsById[accountId]; | ||
| if (!internalAccount || !isEvmAccountType(internalAccount.type)) { | ||
| continue; | ||
| } | ||
|
|
||
| const checksummedAddress = toChecksumHexAddress( | ||
| internalAccount.address, | ||
| ); | ||
|
|
||
| for (const [assetId, balanceData] of Object.entries(accountBalances)) { | ||
| const metadata = assetsInfo[assetId]; | ||
| if (metadata?.type !== 'native') { | ||
| continue; | ||
| } | ||
|
|
||
| const { chain: parsedChain } = parseCaipAssetType( | ||
| assetId as CaipAssetType, | ||
| ); | ||
|
|
||
| // No need to check if the chain is EVM, we already filtered out non-EVM accounts | ||
| const hexChainId = decimalToPrefixedHex(parsedChain.reference); | ||
| const amount = balanceData?.amount ?? '0'; | ||
|
|
||
| result[hexChainId] ??= {}; | ||
| result[hexChainId][checksummedAddress] = { | ||
| // TODO: Use raw value from state when available | ||
| balance: parseBalanceWithDecimals(amount, metadata.decimals), | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| }, | ||
| ); | ||
|
|
||
| function parseBalanceWithDecimals( | ||
| balanceString: string, | ||
| decimals: number, | ||
| ): Hex { | ||
| const [integerPart, fractionalPart = ''] = balanceString.split('.'); | ||
|
|
||
| if (decimals === 0) { | ||
| return bigIntToHex(BigInt(integerPart)); | ||
| } | ||
|
|
||
| if (fractionalPart.length >= decimals) { | ||
| return bigIntToHex( | ||
| BigInt(`${integerPart}${fractionalPart.slice(0, decimals)}`), | ||
| ); | ||
| } | ||
|
|
||
| return bigIntToHex( | ||
| BigInt( | ||
| `${integerPart}${fractionalPart}${'0'.repeat( | ||
| decimals - fractionalPart.length, | ||
| )}`, | ||
| ), | ||
| ); | ||
| } | ||
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
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.