forked from MetaMask/metamask-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Decoding Simulation Metrics (MetaMask#13041)
<!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** Add Decoding Simulation details to Signature request metrics Note: - Split useTypedSignSimulationEnabled logic into selectTypedSignSimulationEnabled to support React class components ## **Related issues** Fixes: MetaMask/MetaMask-planning#3858 ## **Manual testing steps** 1. Go to this page... 2. 3. ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> ## **Pre-merge author checklist** - [ ] I’ve followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile Coding Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [ ] I've completed the PR template to the best of my ability - [ ] I’ve included tests if applicable - [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [ ] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.
- Loading branch information
Showing
7 changed files
with
211 additions
and
21 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
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
121 changes: 121 additions & 0 deletions
121
app/components/Views/confirmations/utils/signatureMetrics.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,121 @@ | ||
import { Hex } from '@metamask/utils'; | ||
import { getSignatureDecodingEventProps } from './signatureMetrics'; | ||
import { DecodingDataChangeType, SignatureRequest, SignatureRequestStatus, SignatureRequestType } from '@metamask/signature-controller'; | ||
|
||
const mockSignatureRequest = { | ||
id: 'fb2029e1-b0ab-11ef-9227-05a11087c334', | ||
chainId: '0x1' as Hex, | ||
type: SignatureRequestType.TypedSign, | ||
messageParams: { | ||
data: '{"types":{"EIP712Domain":[{"name":"name","type":"string"},{"name":"version","type":"string"},{"name":"chainId","type":"uint256"},{"name":"verifyingContract","type":"address"}],"Permit":[{"name":"owner","type":"address"},{"name":"spender","type":"address"},{"name":"value","type":"uint256"},{"name":"nonce","type":"uint256"},{"name":"deadline","type":"uint256"}]},"primaryType":"Permit","domain":{"name":"MyToken","version":"1","verifyingContract":"0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC","chainId":1},"message":{"owner":"0x935e73edb9ff52e23bac7f7e043a1ecd06d05477","spender":"0x5B38Da6a701c568545dCfcB03FcB875f56beddC4","value":3000,"nonce":0,"deadline":50000000000}}', | ||
from: '0x935e73edb9ff52e23bac7f7e043a1ecd06d05477', | ||
version: 'V4', | ||
requestId: 14, | ||
origin: 'https://metamask.github.io', | ||
metamaskId: 'fb2029e0-b0ab-11ef-9227-05a11087c334', | ||
}, | ||
networkClientId: '1', | ||
status: SignatureRequestStatus.Unapproved, | ||
time: 1733143817088 | ||
} satisfies SignatureRequest; | ||
|
||
describe('signatureMetrics', () => { | ||
describe('getSignatureDecodingEventProps', () => { | ||
it('returns empty object when decoding API is disabled', () => { | ||
const mockRequest = { | ||
...mockSignatureRequest, | ||
decodingData: { | ||
stateChanges: [], | ||
error: undefined, | ||
}, | ||
} satisfies SignatureRequest; | ||
|
||
const result = getSignatureDecodingEventProps(mockRequest, false); | ||
expect(result).toEqual({}); | ||
}); | ||
|
||
it('returns empty object when no decodingData is present', () => { | ||
const mockRequest = {} as SignatureRequest; | ||
const result = getSignatureDecodingEventProps(mockRequest, true); | ||
expect(result).toEqual({}); | ||
}); | ||
|
||
it('returns no change response when stateChanges are empty', () => { | ||
const mockRequest = { | ||
...mockSignatureRequest, | ||
decodingData: { | ||
stateChanges: [], | ||
error: undefined, | ||
}, | ||
decodingLoading: false, | ||
} satisfies SignatureRequest; | ||
|
||
const result = getSignatureDecodingEventProps(mockRequest, true); | ||
expect(result).toEqual({ | ||
decoding_change_types: [], | ||
decoding_description: null, | ||
decoding_response: 'NO_CHANGE', | ||
}); | ||
}); | ||
|
||
it('returns loading response when decodingLoading is true', () => { | ||
const mockRequest = { | ||
...mockSignatureRequest, | ||
decodingData: { | ||
stateChanges: [], | ||
error: undefined, | ||
}, | ||
decodingLoading: true, | ||
} satisfies SignatureRequest; | ||
|
||
const result = getSignatureDecodingEventProps(mockRequest, true); | ||
expect(result).toEqual({ | ||
decoding_change_types: [], | ||
decoding_description: null, | ||
decoding_response: 'decoding_in_progress', | ||
}); | ||
}); | ||
|
||
it('returns error response when error exists', () => { | ||
const mockRequest = { | ||
...mockSignatureRequest, | ||
decodingData: { | ||
stateChanges: [], | ||
error: { | ||
type: 'ERROR_TYPE', | ||
message: 'Error message', | ||
}, | ||
}, | ||
decodingLoading: false, | ||
} satisfies SignatureRequest; | ||
|
||
const result = getSignatureDecodingEventProps(mockRequest, true); | ||
expect(result).toEqual({ | ||
decoding_change_types: [], | ||
decoding_description: 'Error message', | ||
decoding_response: 'ERROR_TYPE', | ||
}); | ||
}); | ||
|
||
it('returns change response when stateChanges exist', () => { | ||
const mockRequest = { | ||
...mockSignatureRequest, | ||
decodingData: { | ||
stateChanges: [ | ||
{ changeType: DecodingDataChangeType.Approve, assetType: 'ERC20', address: '0x935e73edb9ff52e23bac7f7e043a1ecd06d05477', amount: '12345', contractAddress: '0x6b175474e89094c44da98b954eedeac495271d0f' }, | ||
{ changeType: DecodingDataChangeType.Transfer, assetType: 'ERC20', address: '0x935e73edb9ff52e23bac7f7e043a1ecd06d05477', amount: '12345', contractAddress: '0x6b175474e89094c44da98b954eedeac495271d0f' }, | ||
], | ||
error: undefined, | ||
}, | ||
decodingLoading: false, | ||
} satisfies SignatureRequest; | ||
|
||
const result = getSignatureDecodingEventProps(mockRequest, true); | ||
expect(result).toEqual({ | ||
decoding_change_types: ['APPROVE', 'TRANSFER'], | ||
decoding_description: null, | ||
decoding_response: 'CHANGE', | ||
}); | ||
}); | ||
}); | ||
}); |
34 changes: 34 additions & 0 deletions
34
app/components/Views/confirmations/utils/signatureMetrics.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,34 @@ | ||
import { DecodingDataStateChange, SignatureRequest } from '@metamask/signature-controller'; | ||
|
||
enum DecodingResponseType { | ||
Change = 'CHANGE', | ||
NoChange = 'NO_CHANGE', | ||
Loading = 'decoding_in_progress', | ||
} | ||
|
||
export const getSignatureDecodingEventProps = (signatureRequest?: SignatureRequest, isDecodingAPIEnabled: boolean = false) => { | ||
const { decodingData, decodingLoading } = signatureRequest || {}; | ||
|
||
if (!isDecodingAPIEnabled || !decodingData) { | ||
return {}; | ||
} | ||
|
||
const { stateChanges, error } = decodingData; | ||
|
||
const changeTypes = (stateChanges ?? []).map( | ||
(change: DecodingDataStateChange) => change.changeType, | ||
); | ||
|
||
const responseType = error?.type ?? | ||
(changeTypes.length | ||
? DecodingResponseType.Change | ||
: DecodingResponseType.NoChange); | ||
|
||
return { | ||
decoding_change_types: changeTypes, | ||
decoding_description: decodingData?.error?.message ?? null, | ||
decoding_response: decodingLoading | ||
? DecodingResponseType.Loading | ||
: responseType, | ||
}; | ||
}; |