-
-
Notifications
You must be signed in to change notification settings - Fork 6
Introduce LegacyEthereumProvider interface #14
base: main
Are you sure you want to change the base?
Conversation
In some controller tests it's useful to create a fake provider that conforms to the same interface as a "real" provider but doesn't actually hit the network. To make such a fake provider, however, we must create a class that extends SafeEventEmitterProvider. This is unfortunate as it introduces the possibility that the network could be hit accidentally if not all of the methods in the interface are appropriately overridden. This commit introduces an interface that allows us to _implement_ the same interface without having to extend it.
It seems that we could already do this today just using the type. We can use that today without extending anything. I might be missing something though. |
@Gudahtt Right, so in the case of FakeProvider, the reason why I don't think using the class as an interface will work is that SafeEventEmitterProvider must be instantiated with a JsonRpcEngine, but I don't want FakeProvider to take those arguments. So if I just replace |
Oh I see. Hmm. In that case, it seems like we don't really want a SafeEventEmitterProvider type, but rather a Provider type. If we omit the event omitter methods and the constructor, what's left are the two provider methods. Perhaps the name of this type could reflect that? That would let us drop the leading |
@Gudahtt Ah, right. We care about the event emitter part of the API — for instance, our provider in NetworkController needs to be properly typed so that we can pass it to |
Alright, I've replaced SimpleEventEmitterProvider with JsonRpcProvider. By the way I wasn't sure what to name this type. I didn't want to name it Provider because it could clash with other Provider types that we have defined elsewhere (currently we have three types across this repo, |
I'd consider it an Ethereum provider; the loose conventions that it follows are specific to the Ethereum ecosystem.
|
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.
LGTM!
@Gudahtt I thought about it some more and I like LegacyEthereumProvider. I think it's important to remember that while using this interface. |
Ah, I'd forgotten about this PR. I'll try to get this updated today. |
Had completely forgot about the existence of this PR when making MetaMask/utils#140 Do I get this right?
|
I think Should we look back into phasing out send() and sendAsync()? Looks like we tried to mark them deprecated at some point but ended up backing out MetaMask/providers#29 |
Yes. Since we always control the provider anywhere it's used, ideally we should not be using a legacy provider anywhere, we should make our provider EIP-1193-compatible. From there, we can either modify |
## Explanation This PR implements the following incremental steps in the process for migrating `eth-json-rpc-provider` into the core monorepo: *** ### Phase B: Staging from `merged-packages/` #### 5. Port tags - See: #1800 <details> <summary>Push ported tags to core repo</summary> - [x] https://github.com/MetaMask/core/releases/tag/@metamask/[email protected] - [x] https://github.com/MetaMask/core/releases/tag/@metamask/[email protected] - [x] https://github.com/MetaMask/core/releases/tag/@metamask/[email protected] - [x] https://github.com/MetaMask/core/releases/tag/@metamask/[email protected] - [x] https://github.com/MetaMask/core/releases/tag/@metamask/[email protected] </details> <details> <summary>Verify that the tag diff links in CHANGELOG are working</summary> - [x] **WONTFIX**: https://github.com/MetaMask/core/compare/@metamask/[email protected] - [x] https://github.com/MetaMask/core/compare/@metamask/[email protected]...@metamask/[email protected] - [x] https://github.com/MetaMask/core/compare/@metamask/[email protected]...@metamask/[email protected] - [x] https://github.com/MetaMask/core/compare/@metamask/[email protected]...@metamask/[email protected] - [x] https://github.com/MetaMask/core/compare/@metamask/[email protected]...@metamask/[email protected] </details> ### Phase C: Integration into `packages/` #### 1. The big leap - [x] **Move migration target from `migrated-packages/` to `packages/`.** - [x] Run `yarn install` in the root directory. - [x] Check that all tests are passing in migration target by running `yarn workspace @metamask/<package-name> test`. #### 2. Update downstream repos - [x] Add tsconfig reference paths for migration target in downstream packages and root. - [x] Bump migration target version in downstream packages and root. #### 3. Linter fixes - [x] Apply yarn constraints fixes to migration target package.json file: `yarn constraints --fix` (run twice). - [x] Identify validator fixes for CHANGELOG using `yarn workspace @metamask/<package-name> changelog:validate` and apply the diffs. #### 4. Resolve downstream errors - [x] #1653 - If introducing the migration target breaks any downstream repos: - [x] Resolve simple errors - [x] Mark and ignore complex errors using `@ts-expect-error TODO:` annotations. - [x] Create a separate issue for resolving the marked errors as soon as the migration is completed. #### 5. Finalize merge - [x] Check that all tests are passing in all subpackages of core and CI. - [x] Merge `packages/<package-name>` directory into core main branch. *** See #1551 (comment) for an outline of the entire process. ## Next Steps - The next PR(s) will implement the final steps of the migration process (D-1 in the migration checklist). ## Blocked by - Dependencies: - [x] typescript bump: #1718 - [x] `@metamask/utils` bump: #1639 - Downstream type errors: - [x] #1653 - [ ] MetaMask/eth-json-rpc-provider#14 (ignored) - [ ] MetaMask/utils#140 (ignored) - Tag porting: - [x] #1802 - [x] "Unreleased" tag diff link shows entire history of core: https://github.com/MetaMask/core/compare/@metamask/[email protected] ## References - Contributes to #1685 - Contributes to #1551 ## Changelog <!-- If you're making any consumer-facing changes, list those changes here as if you were updating a changelog, using the template below as a guide. (CATEGORY is one of BREAKING, ADDED, CHANGED, DEPRECATED, REMOVED, or FIXED. For security-related issues, follow the Security Advisory process.) Please take care to name the exact pieces of the API you've added or changed (e.g. types, interfaces, functions, or methods). If there are any breaking changes, make sure to offer a solution for consumers to follow once they upgrade to the changes. Finally, if you're only making changes to development scripts or tests, you may replace the template below with "None". --> ### `@metamask/eth-json-rpc-provider` - **ADDED**: Migrated into the core monorepo. ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've highlighted breaking changes using the "BREAKING" category above as appropriate
I know we are working on creating unified types, but I think it would be worth it to have this type around in the meantime. My goal here isn't to unify anything, but merely to provide a lightweight version of the provider type that this package exports, so that if we want to make a fake provider in tests we can do so without having to subclass SafeEventEmitterProvider (which poses the risk that the network could actually be hit in tests). So I'm proposing that we merge this PR first. Any thoughts on this? /cc @legobeat |
This library has now been migrated into the core monorepo. This PR has been locked and this repo will be archived shortly. Going forward, any changes to this library will only be included in future releases if they are made in the core repo.
|
In some controller tests it's useful to create a fake provider that conforms to the same interface as a "real" provider but doesn't actually hit the network. To make such a fake provider, however, we must create a class that extends SafeEventEmitterProvider. This is unfortunate as it introduces the possibility that the network could be hit accidentally if not all of the methods in the interface are appropriately overridden.
This commit introduces an interface that allows us to implement the same interface without having to extend it.
For a real-world example of where an interface could be useful, see: https://github.com/MetaMask/core/blob/e506abc29fcddd74591ac32f32a3c2bb0712178e/packages/network-controller/tests/fake-provider.ts#L85
An alternative solution to MetaMask/core#1974.