-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding basic metrics for decoding signatures
- Loading branch information
Showing
4 changed files
with
123 additions
and
0 deletions.
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
55 changes: 55 additions & 0 deletions
55
ui/pages/confirmations/components/simulation-details/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,55 @@ | ||
import { getMockTypedSignConfirmStateForRequest } from '../../../../../test/data/confirmations/helper'; | ||
import { permitSignatureMsg } from '../../../../../test/data/confirmations/typed_sign'; | ||
import { renderHookWithConfirmContextProvider } from '../../../../../test/lib/confirmations/render-helpers'; | ||
import * as SignatureEventFragment from '../../hooks/useSignatureEventFragment'; | ||
import { useDecodedSignatureMetrics } from './useDecodedSignatureMetrics'; | ||
|
||
describe('useDecodedSignatureMetrics', () => { | ||
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', 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', | ||
}, | ||
}); | ||
}); | ||
}); |
45 changes: 45 additions & 0 deletions
45
ui/pages/confirmations/components/simulation-details/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,45 @@ | ||
import { useEffect } from 'react'; | ||
import { DecodingDataStateChange } from '@metamask/signature-controller'; | ||
|
||
import { SignatureRequestType } from '../../types/confirm'; | ||
import { useConfirmContext } from '../../context/confirm'; | ||
import { useSignatureEventFragment } from '../../hooks/useSignatureEventFragment'; | ||
|
||
enum DecodingResponse { | ||
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 | ||
? DecodingResponse.Change | ||
: DecodingResponse.NoChange); | ||
|
||
useEffect(() => { | ||
if (decodingLoading) { | ||
return; | ||
} | ||
|
||
updateSignatureEventFragment({ | ||
properties: { | ||
decoding_response: decodingResponse, | ||
decoding_change_types: decodingChangeTypes, | ||
}, | ||
}); | ||
}, [ | ||
decodingResponse, | ||
decodingLoading, | ||
decodingChangeTypes, | ||
updateSignatureEventFragment, | ||
]); | ||
} |