-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
refactor(activity): migrate non-EVM transaction mapping #34682
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
Open
n3ps
wants to merge
9
commits into
main
Choose a base branch
from
n3ps/client-utils-keyring-transaction-mapper-947d
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
feb08e8
chore(activity): bump @metamask/client-utils to ^2.0.1
n3ps 780ac04
refactor(activity): call mapKeyringTransaction from client-utils dire…
n3ps 2429c81
chore: refresh yarn.lock after rebase onto main
cursoragent a795c9d
fix(activity): format keyring mapper and harden fees handling
cursoragent 9788cd6
test(activity): restore real timers and update zero-amount expectation
cursoragent 2bc4182
refactor(activity): alias activity types from @metamask/client-utils
cursoragent 04724d3
cleanup
n3ps 401308d
typescript
n3ps 812cb3a
lint
n3ps 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
126 changes: 126 additions & 0 deletions
126
app/components/Views/ActivityList/helpers/apply-bridge-quote.test.ts
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,126 @@ | ||
| import { StatusTypes } from '@metamask/bridge-controller'; | ||
| import type { BridgeHistoryItem } from '@metamask/bridge-status-controller'; | ||
| import { | ||
| TransactionStatus, | ||
| TransactionType, | ||
| type Transaction, | ||
| } from '@metamask/keyring-api'; | ||
| import { mapKeyringTransaction } from '@metamask/client-utils'; | ||
| import type { ActivityListItem } from '../../../../util/activity-adapters'; | ||
| import { applyBridgeQuote } from './apply-bridge-quote'; | ||
|
|
||
| const solanaChainId = | ||
| 'solana:4sGjMW1sUnHzSxGspuhpqLDx6wiyjNtZ' as Transaction['chain']; | ||
|
|
||
| describe('applyBridgeQuote', () => { | ||
| const makeKeyringTx = (overrides: Partial<Transaction> = {}): Transaction => | ||
| ({ | ||
| id: 'bridge-id', | ||
| chain: solanaChainId, | ||
| account: '00000000-0000-4000-8000-000000000000', | ||
| status: TransactionStatus.Confirmed, | ||
| timestamp: 1716367781, | ||
| type: TransactionType.Send, | ||
| from: [ | ||
| { | ||
| address: 'from-address', | ||
| asset: { | ||
| fungible: true, | ||
| type: `${solanaChainId}/slip44:501`, | ||
| unit: 'SOL', | ||
| amount: '1', | ||
| }, | ||
| }, | ||
| ], | ||
| to: [{ address: 'to-address', asset: null }], | ||
| fees: [], | ||
| events: [], | ||
| ...overrides, | ||
| }) as Transaction; | ||
|
|
||
| const makeBridgeHistory = ({ | ||
| destChainId = 1, | ||
| bridgeStatus = StatusTypes.PENDING, | ||
| destChainAmount, | ||
| }: { | ||
| destChainId?: string | number; | ||
| bridgeStatus?: StatusTypes; | ||
| destChainAmount?: string; | ||
| } = {}): BridgeHistoryItem => | ||
| ({ | ||
| quote: { | ||
| srcChainId: solanaChainId, | ||
| destChainId, | ||
| srcAsset: { | ||
| assetId: `${solanaChainId}/slip44:501`, | ||
| decimals: 9, | ||
| symbol: 'SOL', | ||
| }, | ||
| srcTokenAmount: '1000000000', | ||
| destAsset: { | ||
| assetId: 'eip155:1/slip44:60', | ||
| decimals: 18, | ||
| symbol: 'ETH', | ||
| }, | ||
| destTokenAmount: '5000000000000000', | ||
| }, | ||
| status: { | ||
| status: bridgeStatus, | ||
| srcChain: { txHash: 'bridge-id' }, | ||
| ...(destChainAmount | ||
| ? { destChain: { txHash: '0xdest', amount: destChainAmount } } | ||
| : {}), | ||
| }, | ||
| }) as unknown as BridgeHistoryItem; | ||
|
|
||
| it('maps a cross-chain bridge to a pending bridge item with quote-derived tokens', () => { | ||
| const item = applyBridgeQuote( | ||
| mapKeyringTransaction({ | ||
| transaction: makeKeyringTx(), | ||
| }) as ActivityListItem, | ||
| makeBridgeHistory(), | ||
| 'from-address', | ||
| ); | ||
|
|
||
| expect(item).toMatchObject({ | ||
| type: 'bridge', | ||
| status: 'pending', | ||
| data: { | ||
| from: 'from-address', | ||
| sourceToken: { symbol: 'SOL', direction: 'out' }, | ||
| destinationToken: { symbol: 'ETH', direction: 'in' }, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('marks the bridge successful once the destination leg lands', () => { | ||
| const item = applyBridgeQuote( | ||
| mapKeyringTransaction({ | ||
| transaction: makeKeyringTx({ type: TransactionType.Swap }), | ||
| }) as ActivityListItem, | ||
| makeBridgeHistory({ | ||
| bridgeStatus: StatusTypes.COMPLETE, | ||
| destChainAmount: '4990000000000000', | ||
| }), | ||
| ); | ||
|
|
||
| expect(item).toMatchObject({ | ||
| type: 'bridge', | ||
| status: 'success', | ||
| data: { | ||
| destinationToken: { amount: '4990000000000000' }, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('leaves same-chain swaps with bridge history on the regular keyring mapping', () => { | ||
| const item = applyBridgeQuote( | ||
| mapKeyringTransaction({ | ||
| transaction: makeKeyringTx({ type: TransactionType.Swap }), | ||
| }) as ActivityListItem, | ||
| makeBridgeHistory({ destChainId: solanaChainId }), | ||
| ); | ||
|
|
||
| expect(item).toMatchObject({ type: 'swap', status: 'success' }); | ||
| }); | ||
| }); |
76 changes: 76 additions & 0 deletions
76
app/components/Views/ActivityList/helpers/apply-bridge-quote.ts
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,76 @@ | ||
| import { | ||
| isCrossChain, | ||
| StatusTypes as BridgeStatusTypes, | ||
| } from '@metamask/bridge-controller'; | ||
| import type { BridgeHistoryItem } from '@metamask/bridge-status-controller'; | ||
| import type { ActivityListItem } from '../../../../util/activity-adapters'; | ||
|
|
||
| function getBridgeActivityStatus(bridgeHistoryItem: BridgeHistoryItem) { | ||
| const { | ||
| quote, | ||
| status: { status }, | ||
| } = bridgeHistoryItem; | ||
|
|
||
| if (status === BridgeStatusTypes.FAILED) { | ||
| return 'failed'; | ||
| } | ||
|
|
||
| if (status === BridgeStatusTypes.COMPLETE) { | ||
| return 'success'; | ||
| } | ||
|
|
||
| if ( | ||
| isCrossChain(quote.srcChainId, quote.destChainId) && | ||
| (status === BridgeStatusTypes.PENDING || | ||
| status === BridgeStatusTypes.SUBMITTED) | ||
| ) { | ||
| return 'pending'; | ||
| } | ||
|
|
||
| return undefined; | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| export function applyBridgeQuote( | ||
| activity: ActivityListItem, | ||
| bridgeHistory?: BridgeHistoryItem, | ||
| subjectAddress?: string, | ||
| ) { | ||
| const quote = bridgeHistory?.quote; | ||
| if ( | ||
| !bridgeHistory || | ||
| !quote || | ||
| !isCrossChain(quote.srcChainId, quote.destChainId) | ||
| ) { | ||
| return activity; | ||
| } | ||
|
|
||
| const fees = 'fees' in activity.data ? activity.data.fees : undefined; | ||
| const status = | ||
| activity.status === 'failed' | ||
| ? 'failed' | ||
| : getBridgeActivityStatus(bridgeHistory); | ||
|
|
||
| return { | ||
| ...activity, | ||
| type: 'bridge', | ||
| ...(status ? { status } : {}), | ||
| data: { | ||
| from: subjectAddress, | ||
| sourceToken: { | ||
| amount: quote.srcTokenAmount, | ||
| assetId: quote.srcAsset.assetId, | ||
| decimals: quote.srcAsset.decimals, | ||
| direction: 'out', | ||
| symbol: quote.srcAsset.symbol, | ||
| }, | ||
| destinationToken: { | ||
| amount: bridgeHistory.status.destChain?.amount ?? quote.destTokenAmount, | ||
| assetId: quote.destAsset.assetId, | ||
| decimals: quote.destAsset.decimals, | ||
| direction: 'in', | ||
| symbol: quote.destAsset.symbol, | ||
| }, | ||
| ...(fees === undefined ? {} : { fees }), | ||
| }, | ||
| } as ActivityListItem; | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -3,21 +3,23 @@ | |
| * Produces ActivityListItem[] from API EVM and non-EVM transaction sources. | ||
| * Local EVM transactions are handled separately by useLocalActivityItems. | ||
| */ | ||
| import { mapKeyringTransaction } from '@metamask/client-utils'; | ||
|
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. Point of interest |
||
| import { | ||
| type V1TransactionByHashResponse, | ||
| type V4MultiAccountTransactionsResponse, | ||
| } from '@metamask/core-backend'; | ||
| import { isCrossChain } from '@metamask/bridge-controller'; | ||
| import type { BridgeHistoryItem } from '@metamask/bridge-status-controller'; | ||
| import type { Transaction as NonEvmTransaction } from '@metamask/keyring-api'; | ||
| import type { InfiniteData } from '@tanstack/react-query'; | ||
| import { | ||
| mapApiEvmTransactions, | ||
| mapKeyringTransaction, | ||
| type ActivityListItem, | ||
| type ActivityAdapterEnvironment, | ||
| } from '../../../../util/activity-adapters'; | ||
| import { mergeActivityItems } from '../../../../util/activity-adapters/adapters/dedup'; | ||
| import { equalsIgnoreCase } from '../../../../util/string'; | ||
| import { applyBridgeQuote } from './apply-bridge-quote'; | ||
|
|
||
| export type { ActivityListItem }; | ||
|
|
||
|
|
@@ -187,13 +189,29 @@ export function selectApiEvmTransactions({ | |
| export function mapNonEvmTransactions( | ||
| transactions: NonEvmTransaction[], | ||
| getBridgeHistoryItem?: (txId: string) => BridgeHistoryItem | undefined, | ||
| getSubjectAddress?: (transaction: NonEvmTransaction) => string | undefined, | ||
| ): ActivityListItem[] { | ||
| return transactions.map((transaction) => | ||
| mapKeyringTransaction({ | ||
| transaction, | ||
| bridgeHistory: getBridgeHistoryItem?.(transaction.id), | ||
| }), | ||
| ); | ||
| return transactions.map((transaction) => { | ||
| const subjectAddress = getSubjectAddress?.(transaction); | ||
| const activity = { | ||
| ...mapKeyringTransaction({ | ||
| transaction: { | ||
| ...transaction, | ||
| fees: transaction.fees ?? [], | ||
| }, | ||
| subjectAddress, | ||
| }), | ||
| raw: { type: 'keyringTransaction' as const, data: transaction }, | ||
| } as ActivityListItem; | ||
| const bridgeHistoryItem = getBridgeHistoryItem?.(transaction.id); | ||
| const quote = bridgeHistoryItem?.quote; | ||
|
|
||
| if (quote && isCrossChain(quote.srcChainId, quote.destChainId)) { | ||
| return applyBridgeQuote(activity, bridgeHistoryItem, subjectAddress); | ||
| } | ||
|
|
||
| return activity; | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
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.