Skip to content

Commit

Permalink
feat: Improve provider method metrics for add/switch chain (#28214)
Browse files Browse the repository at this point in the history
## **Description**

The provider metrics for `wallet_addEthereumChain` and
`wallet_switchEthereumChain` are tracked more effectively now. They are
no longer rate-limited to 0.1%, instead we track 100% of these calls.
They also now include the chain ID being added or switched to.

It was deemed safe to disable rate limiting here because historically
these methods have required user confirmation, so it's not likely that a
dapp would send them repeatedly in a short time frame. Volume should be
on a similar scale to other manual user confirmations like signatures.

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/28214?quickstart=1)

## **Related issues**

Fixes MetaMask/MetaMask-planning#3520

## **Manual testing steps**

* Build the wallet using the instructions listed here for debugging with
the mock Segment API:
https://github.com/MetaMask/metamask-extension/blob/develop/development/README.md#debugging-with-the-mock-segment-api
* Connect to the test-dapp
* Trigger `wallet_addEthereumChain` and `wallet_switchEthereumChain`,
and see that they are correctly captured as metric events.

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

N/A

### **After**


https://github.com/user-attachments/assets/910a1572-74fa-4ecf-8a5d-6f0856757207

Unfortunately the mock Segment server doesn't show the parameters. But
it does show that the event was received.

## **Pre-merge author checklist**

- [x] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.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
Gudahtt authored Oct 31, 2024
1 parent 630352a commit dd03cc2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/scripts/lib/createRPCMethodTrackingMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ const RATE_LIMIT_MAP = {
[MESSAGE_TYPE.ETH_DECRYPT]: RATE_LIMIT_TYPES.NON_RATE_LIMITED,
[MESSAGE_TYPE.ETH_GET_ENCRYPTION_PUBLIC_KEY]:
RATE_LIMIT_TYPES.NON_RATE_LIMITED,
[MESSAGE_TYPE.ADD_ETHEREUM_CHAIN]: RATE_LIMIT_TYPES.NON_RATE_LIMITED,
[MESSAGE_TYPE.SWITCH_ETHEREUM_CHAIN]: RATE_LIMIT_TYPES.NON_RATE_LIMITED,
[MESSAGE_TYPE.ETH_REQUEST_ACCOUNTS]: RATE_LIMIT_TYPES.TIMEOUT,
[MESSAGE_TYPE.WALLET_REQUEST_PERMISSIONS]: RATE_LIMIT_TYPES.TIMEOUT,
[MESSAGE_TYPE.SEND_METADATA]: RATE_LIMIT_TYPES.BLOCKED,
Expand Down Expand Up @@ -126,6 +128,8 @@ const EVENT_NAME_MAP = {
*/
const TRANSFORM_PARAMS_MAP = {
[MESSAGE_TYPE.WATCH_ASSET]: ({ type }) => ({ type }),
[MESSAGE_TYPE.ADD_ETHEREUM_CHAIN]: ([{ chainId }]) => ({ chainId }),
[MESSAGE_TYPE.SWITCH_ETHEREUM_CHAIN]: ([{ chainId }]) => ({ chainId }),
};

const rateLimitTimeoutsByMethod = {};
Expand Down
28 changes: 28 additions & 0 deletions app/scripts/lib/createRPCMethodTrackingMiddleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,34 @@ describe('createRPCMethodTrackingMiddleware', () => {
},
{ type: 'ERC20' },
],
[
'only the chain ID',
'wallet_addEthereumChain',
[
{
chainId: '0x64',
chainName: 'Gnosis',
rpcUrls: ['https://rpc.gnosischain.com'],
iconUrls: [
'https://xdaichain.com/fake/example/url/xdai.svg',
'https://xdaichain.com/fake/example/url/xdai.png',
],
nativeCurrency: {
name: 'XDAI',
symbol: 'XDAI',
decimals: 18,
},
blockExplorerUrls: ['https://blockscout.com/poa/xdai/'],
},
],
{ chainId: '0x64' },
],
[
'only the chain ID',
'wallet_switchEthereumChain',
[{ chainId: '0x123' }],
{ chainId: '0x123' },
],
])(
`should include %s in the '%s' tracked events params property`,
async (_, method, params, expected) => {
Expand Down

0 comments on commit dd03cc2

Please sign in to comment.