-
-
Notifications
You must be signed in to change notification settings - Fork 203
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
Add getNetworkClientById
#1562
Add getNetworkClientById
#1562
Conversation
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.
I left a couple of comments.
Would you mind adding tests for getNetworkClientById
? (edit: Oh, I see you already marked that as a TODO — great :) )
/** | ||
* Returns the network client for a given network client id. | ||
* | ||
* @param networkClientId - The network client id for which to retrieve the network client. | ||
* @returns The network client. | ||
*/ | ||
getNetworkClientById( | ||
networkClientId: NetworkClientId, | ||
): AutoManagedNetworkClient<NetworkClientConfiguration> { | ||
const networkClients = this.getNetworkClients(); | ||
return networkClients[networkClientId]; | ||
} |
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.
Since we currently have a split between Infura and custom network clients, I think it would be a good idea for this method to return the right type depending on the ID given so that networkClient.configuration
will return a correctly typed configuration object. Instead of relying on getNetworkClients
, we can access the registry directly. Also, considering that CustomNetworkClientId is a string, this method can technically return undefined if that string isn't present as a property in the custom network client registry. So consumers don't have to deal with undefined, I think it would be a good idea to throw in that case.
With that in mind, what about this?
/** | |
* Returns the network client for a given network client id. | |
* | |
* @param networkClientId - The network client id for which to retrieve the network client. | |
* @returns The network client. | |
*/ | |
getNetworkClientById( | |
networkClientId: NetworkClientId, | |
): AutoManagedNetworkClient<NetworkClientConfiguration> { | |
const networkClients = this.getNetworkClients(); | |
return networkClients[networkClientId]; | |
} | |
/** | |
* Returns the Infura network client with the given ID. | |
* | |
* @param infuraNetworkClientId - An Infura network client ID. | |
* @returns The Infura network client. | |
* @throws If an Infura network client does not exist with the given ID. | |
*/ | |
getNetworkClientById( | |
infuraNetworkClientId: BuiltInNetworkClientId, | |
): AutoManagedNetworkClient<InfuraNetworkClientConfiguration>; | |
/** | |
* Returns the custom network client with the given ID. | |
* | |
* @param customNetworkClientId - A custom network client ID. | |
* @returns The custom network client. | |
* @throws If a custom network client does not exist with the given ID. | |
*/ | |
getNetworkClientById( | |
customNetworkClientId: CustomNetworkClientId, | |
): AutoManagedNetworkClient<CustomNetworkClientConfiguration>; | |
getNetworkClientById( | |
networkClientId: NetworkClientId, | |
): AutoManagedNetworkClient<NetworkClientConfiguration> { | |
const autoManagedNetworkClientRegistry = | |
this.#ensureAutoManagedNetworkClientRegistryPopulated(); | |
if (isInfuraProviderType(networkClientId)) { | |
const infuraNetworkClient = | |
autoManagedNetworkClientRegistry[NetworkClientType.Infura][ | |
networkClientId | |
]; | |
if (!infuraNetworkClient) { | |
throw new Error( | |
`No Infura network client was found with the ID "${networkClientId}".`, | |
); | |
} | |
return infuraNetworkClient; | |
} | |
const customNetworkClient = | |
autoManagedNetworkClientRegistry[NetworkClientType.Custom][ | |
networkClientId | |
]; | |
if (!customNetworkClient) { | |
throw new Error( | |
`No custom network client was found with the ID "${networkClientId}".`, | |
); | |
} | |
return customNetworkClient; | |
} |
With these changes, this should be the case:
const goerliNetworkClient = networkController.getNetworkClientById(InfuraNetworkType.goerli);
goerliNetworkClient.configuration //=> InfuraNetworkClientConfiguration
// Assuming that 'AAAA-AAAA-AAAA-AAAA' exists in the registry
const customNetworkClient = networkController.getNetworkClientById('AAAA-AAAA-AAAA-AAAA');
customNetworkClient.configuration //=> CustomNetworkClientConfiguration
// Assuming that 'BBBB-BBBB-BBBB-BBBB' does not exist in the registry
networkController.getNetworkClientById('BBBB-BBBB-BBBB-BBBB'); // throws
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.
if (!infuraNetworkClient) {
throw new Error(
No Infura network client was found with the ID "${networkClientId}".
,
);
}
I'm pretty sure this couldn't ever be hit the way the Controller is written today since if it entered the if block
if (isInfuraProviderType(networkClientId))
it must be a valid infura clientId and must therefore be included in the registry.
I suppose this could be refactored at some point such that this wouldn't be the case so I don't mind leaving the error in.
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.
cc @mcmire
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.
That's true. I've only added that check because eventually we want to enable TypeScript's exactOptionalPropertyTypes
option, and once that happen it'll add | undefined
to all index access, so it's good to keep that in mind now.
@@ -629,7 +629,7 @@ export class NetworkController extends BaseControllerV2< | |||
* | |||
* @returns The list of known network clients. | |||
*/ | |||
getNetworkClientsById(): AutoManagedBuiltInNetworkClientRegistry & | |||
getNetworkClients(): AutoManagedBuiltInNetworkClientRegistry & |
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.
I agree with removing ById
here since philosophically it conflicts with how we are naming getNetworkClientById
.
As discussed on Slack, if you assign this to a variable, but then you want an array version of this, the new name here forces you to name that array variable something else other than networkClients
, which might be the more obvious choice. To save an extra decision from having to be made, what do we think about naming this something like getNetworkClientRegistry
?
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.
sure I think that makes some sense!
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.
done here: c7be0e7
Co-authored-by: Elliot Winkler <elliot.winkler@gmail.com>
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.
Marking this as "changes requested" just as a reminder to add tests.
9e6ac22
to
346837c
Compare
Even if it's redundant, it'd be helpful to call this (Or more subjective preference: |
If we were gonna go this route wouldn't we want to do Personally I think |
It's true that there are a few different concepts that have IDs, so I understand how confusion would arise. I am generally a fan of being explicit where needed, but in this case I also believe that it's reasonable to assume that the ID in this case refers to the thing we're getting rather than some other object, as it's the simplest possibility. If there is confusion, then the method signature includes |
Looks good! |
* getNetworkClientById * Update packages/network-controller/src/NetworkController.ts Co-authored-by: Elliot Winkler <elliot.winkler@gmail.com> * rename getNetworkClients to getNetworkClientRegistry * lint * add tests --------- Co-authored-by: Elliot Winkler <elliot.winkler@gmail.com>
* getNetworkClientById * Update packages/network-controller/src/NetworkController.ts Co-authored-by: Elliot Winkler <elliot.winkler@gmail.com> * rename getNetworkClients to getNetworkClientRegistry * lint * add tests --------- Co-authored-by: Elliot Winkler <elliot.winkler@gmail.com>
getNetworkClientById
, a simple getter for the networkClient registry that takes anetworkClientId
and returns the corresponding networkClient.getNetworkClientsById
: this name is/was confusing. To me theById
implies you pass in an Id as an arg rather than that the return will be keyed by id. Open to discussion on this.