-
Notifications
You must be signed in to change notification settings - Fork 5.1k
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
feat: adding metrics for signature decoding #28719
Merged
Merged
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
67403d5
Adding basic metrics for decoding signatures
jpuri d325631
update
jpuri f050ef2
update
jpuri b255951
Merge branch 'develop' into decoding_metrics
jpuri 64df0f5
update
jpuri 2a83626
Merge branch 'decoding_metrics' of github.com:MetaMask/metamask-exten…
jpuri acec502
update
jpuri 44c0235
Merge branch 'develop' into decoding_metrics
jpuri 1aec969
Merge branch 'develop' into decoding_metrics
jpuri 3bcd04e
Merge branch 'develop' into decoding_metrics
jpuri aab444e
update
jpuri b5831c7
Merge branch 'decoding_metrics' of github.com:MetaMask/metamask-exten…
jpuri 441a94e
Merge branch 'develop' into decoding_metrics
jpuri e0c73cf
merge
jpuri 1ea109e
update
jpuri e938f4e
Merge branch 'decoding_metrics' of github.com:MetaMask/metamask-exten…
jpuri 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 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 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 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 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 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
132 changes: 132 additions & 0 deletions
132
ui/pages/confirmations/hooks/useDecodedSignatureMetrics.test.ts
This file contains 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,132 @@ | ||
import { DecodingData, DecodingDataChangeType } from '@metamask/signature-controller'; | ||
|
||
import { getMockTypedSignConfirmStateForRequest } from '../../../../test/data/confirmations/helper'; | ||
import { renderHookWithConfirmContextProvider } from '../../../../test/lib/confirmations/render-helpers'; | ||
import { permitSignatureMsg } from '../../../../test/data/confirmations/typed_sign'; | ||
import * as SignatureEventFragment from './useSignatureEventFragment'; | ||
import { useDecodedSignatureMetrics } from './useDecodedSignatureMetrics'; | ||
|
||
const decodingData: DecodingData = { | ||
stateChanges: [ | ||
{ | ||
assetType: 'ERC20', | ||
changeType: DecodingDataChangeType.Approve, | ||
address: '0x3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad', | ||
amount: '1461501637330902918203684832716283019655932542975', | ||
contractAddress: '0x6b175474e89094c44da98b954eedeac495271d0f', | ||
}, | ||
], | ||
}; | ||
|
||
describe('useDecodedSignatureMetrics', () => { | ||
process.env.ENABLE_SIGNATURE_DECODING = 'true'; | ||
it('should not call updateSignatureEventFragment if decodingLoading is true', async () => { | ||
const state = getMockTypedSignConfirmStateForRequest({ | ||
...permitSignatureMsg, | ||
decodingLoading: true, | ||
}); | ||
|
||
const mockUpdateSignatureEventFragment = jest.fn(); | ||
jest | ||
.spyOn(SignatureEventFragment, 'useSignatureEventFragment') | ||
.mockImplementation(() => ({ | ||
updateSignatureEventFragment: mockUpdateSignatureEventFragment, | ||
})); | ||
|
||
renderHookWithConfirmContextProvider( | ||
() => useDecodedSignatureMetrics(), | ||
state, | ||
); | ||
|
||
expect(mockUpdateSignatureEventFragment).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
it('should call updateSignatureEventFragment with correct parameters if there are no state changes', async () => { | ||
const state = getMockTypedSignConfirmStateForRequest({ | ||
...permitSignatureMsg, | ||
decodingLoading: false, | ||
}); | ||
|
||
const mockUpdateSignatureEventFragment = jest.fn(); | ||
jest | ||
.spyOn(SignatureEventFragment, 'useSignatureEventFragment') | ||
.mockImplementation(() => ({ | ||
updateSignatureEventFragment: mockUpdateSignatureEventFragment, | ||
})); | ||
|
||
renderHookWithConfirmContextProvider( | ||
() => useDecodedSignatureMetrics(), | ||
state, | ||
); | ||
|
||
expect(mockUpdateSignatureEventFragment).toHaveBeenCalledTimes(1); | ||
expect(mockUpdateSignatureEventFragment).toHaveBeenLastCalledWith({ | ||
properties: { | ||
decoding_change_types: [], | ||
decoding_response: 'NO_CHANGE', | ||
}, | ||
}); | ||
}); | ||
|
||
it('should call updateSignatureEventFragment with correct parameters if there are no state changes', async () => { | ||
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. Minor, same name as the other? 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. fixed |
||
const state = getMockTypedSignConfirmStateForRequest({ | ||
...permitSignatureMsg, | ||
decodingLoading: false, | ||
decodingData | ||
}); | ||
|
||
const mockUpdateSignatureEventFragment = jest.fn(); | ||
jest | ||
.spyOn(SignatureEventFragment, 'useSignatureEventFragment') | ||
.mockImplementation(() => ({ | ||
updateSignatureEventFragment: mockUpdateSignatureEventFragment, | ||
})); | ||
|
||
renderHookWithConfirmContextProvider( | ||
() => useDecodedSignatureMetrics(), | ||
state, | ||
); | ||
|
||
expect(mockUpdateSignatureEventFragment).toHaveBeenCalledTimes(1); | ||
expect(mockUpdateSignatureEventFragment).toHaveBeenLastCalledWith({ | ||
properties: { | ||
decoding_change_types: ["APPROVE"], | ||
decoding_response: 'CHANGE', | ||
}, | ||
}); | ||
}); | ||
|
||
it('should call updateSignatureEventFragment with correct parameters if response has error', async () => { | ||
const state = getMockTypedSignConfirmStateForRequest({ | ||
...permitSignatureMsg, | ||
decodingLoading: false, | ||
decodingData: { | ||
stateChanges: [], | ||
error: { | ||
type: 'SOME_ERROR', | ||
message: 'some message' | ||
} | ||
} | ||
}); | ||
|
||
const mockUpdateSignatureEventFragment = jest.fn(); | ||
jest | ||
.spyOn(SignatureEventFragment, 'useSignatureEventFragment') | ||
.mockImplementation(() => ({ | ||
updateSignatureEventFragment: mockUpdateSignatureEventFragment, | ||
})); | ||
|
||
renderHookWithConfirmContextProvider( | ||
() => useDecodedSignatureMetrics(), | ||
state, | ||
); | ||
|
||
expect(mockUpdateSignatureEventFragment).toHaveBeenCalledTimes(1); | ||
expect(mockUpdateSignatureEventFragment).toHaveBeenLastCalledWith({ | ||
properties: { | ||
decoding_change_types: [], | ||
decoding_response: 'SOME_ERROR', | ||
}, | ||
}); | ||
}); | ||
}); |
46 changes: 46 additions & 0 deletions
46
ui/pages/confirmations/hooks/useDecodedSignatureMetrics.ts
This file contains 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,46 @@ | ||
import { DecodingDataStateChange } from '@metamask/signature-controller'; | ||
import { useEffect } from 'react'; | ||
|
||
import { SignatureRequestType } from '../types/confirm'; | ||
import { useConfirmContext } from '../context/confirm'; | ||
import { useSignatureEventFragment } from './useSignatureEventFragment'; | ||
|
||
|
||
enum DecodingResponseType { | ||
Change = 'CHANGE', | ||
NoChange = 'NO_CHANGE', | ||
} | ||
|
||
export function useDecodedSignatureMetrics() { | ||
const { updateSignatureEventFragment } = useSignatureEventFragment(); | ||
const { currentConfirmation } = useConfirmContext<SignatureRequestType>(); | ||
const { decodingLoading, decodingData } = currentConfirmation; | ||
|
||
const decodingChangeTypes = (decodingData?.stateChanges ?? []).map( | ||
(change: DecodingDataStateChange) => change.changeType, | ||
); | ||
|
||
const decodingResponse = | ||
decodingData?.error?.type ?? | ||
(decodingChangeTypes.length | ||
? DecodingResponseType.Change | ||
: DecodingResponseType.NoChange); | ||
|
||
useEffect(() => { | ||
if (decodingLoading || !process.env.ENABLE_SIGNATURE_DECODING) { | ||
return; | ||
} | ||
|
||
updateSignatureEventFragment({ | ||
properties: { | ||
decoding_response: decodingResponse, | ||
decoding_change_types: decodingChangeTypes, | ||
}, | ||
}); | ||
}, [ | ||
decodingResponse, | ||
decodingLoading, | ||
decodingChangeTypes, | ||
updateSignatureEventFragment, | ||
]); | ||
} |
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.
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.
Q: should we unset the ENV var after the tests are complete or is there another way to add ENV for builds?
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 is being removed in a following PR