Skip to content

Commit

Permalink
trying to cleanup and make more logical
Browse files Browse the repository at this point in the history
  • Loading branch information
adonesky1 committed Feb 19, 2024
1 parent ecb5e72 commit 88b679e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 73 deletions.
129 changes: 63 additions & 66 deletions packages/selected-network-controller/src/SelectedNetworkController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export const SelectedNetworkControllerActionTypes = {
getState: `${controllerName}:getState` as const,
getNetworkClientIdForDomain:
`${controllerName}:getNetworkClientIdForDomain` as const,
getNetworkClientIdForMetamask:
`${controllerName}:getNetworkClientIdForMetamask` as const,
// getNetworkClientIdForMetamask:
// `${controllerName}:getNetworkClientIdForMetamask` as const,
setNetworkClientIdForDomain:
`${controllerName}:setNetworkClientIdForDomain` as const,
};
Expand Down Expand Up @@ -66,10 +66,10 @@ export type SelectedNetworkControllerGetNetworkClientIdForDomainAction = {
handler: SelectedNetworkController['getNetworkClientIdForDomain'];
};

export type SelectedNetworkControllerGetNetworkClientIdForMetamaskAction = {
type: typeof SelectedNetworkControllerActionTypes.getNetworkClientIdForMetamask;
handler: SelectedNetworkController['getNetworkClientIdForMetamask'];
};
// export type SelectedNetworkControllerGetNetworkClientIdForMetamaskAction = {
// type: typeof SelectedNetworkControllerActionTypes.getNetworkClientIdForMetamask;
// handler: SelectedNetworkController['getNetworkClientIdForMetamask'];
// };

export type SelectedNetworkControllerSetNetworkClientIdForDomainAction = {
type: typeof SelectedNetworkControllerActionTypes.setNetworkClientIdForDomain;
Expand All @@ -84,7 +84,7 @@ type PermissionControllerHasPermissions = {
export type SelectedNetworkControllerActions =
| SelectedNetworkControllerGetSelectedNetworkStateAction
| SelectedNetworkControllerGetNetworkClientIdForDomainAction
| SelectedNetworkControllerGetNetworkClientIdForMetamaskAction
// | SelectedNetworkControllerGetNetworkClientIdForMetamaskAction
| SelectedNetworkControllerSetNetworkClientIdForDomainAction;

export type AllowedActions =
Expand Down Expand Up @@ -144,42 +144,42 @@ export class SelectedNetworkController extends BaseController<
});
this.#registerMessageHandlers();

// if the network for metamask is not set, set it to the currently selected network upon initialization
if (this.state.domains[METAMASK_DOMAIN] === undefined) {
const { selectedNetworkClientId } = this.messagingSystem.call(
'NetworkController:getState',
);
this.setNetworkClientIdForMetamask(selectedNetworkClientId);
}

this.messagingSystem.subscribe(
'NetworkController:stateChange',
({ selectedNetworkClientId }) => {
if (this.getNetworkClientIdForMetamask() !== selectedNetworkClientId) {
this.setNetworkClientIdForMetamask(selectedNetworkClientId);
}
},
);
// // if the network for metamask is not set, set it to the currently selected network upon initialization
// if (this.state.domains[METAMASK_DOMAIN] === undefined) {
// const { selectedNetworkClientId } = this.messagingSystem.call(
// 'NetworkController:getState',
// );
// this.setNetworkClientIdForMetamask(selectedNetworkClientId);
// }

// this.messagingSystem.subscribe(
// 'NetworkController:stateChange',
// ({ selectedNetworkClientId }) => {
// if (this.getNetworkClientIdForMetamask() !== selectedNetworkClientId) {
// this.setNetworkClientIdForMetamask(selectedNetworkClientId);
// }
// },
// );
}

#registerMessageHandlers(): void {
this.messagingSystem.registerActionHandler(
SelectedNetworkControllerActionTypes.getNetworkClientIdForDomain,
this.getNetworkClientIdForDomain.bind(this),
);
this.messagingSystem.registerActionHandler(
SelectedNetworkControllerActionTypes.getNetworkClientIdForMetamask,
this.getNetworkClientIdForMetamask.bind(this),
);
// this.messagingSystem.registerActionHandler(
// SelectedNetworkControllerActionTypes.getNetworkClientIdForMetamask,
// this.getNetworkClientIdForMetamask.bind(this),
// );
this.messagingSystem.registerActionHandler(
SelectedNetworkControllerActionTypes.setNetworkClientIdForDomain,
this.setNetworkClientIdForDomain.bind(this),
);
}

setNetworkClientIdForMetamask(networkClientId: NetworkClientId) {
this.setNetworkClientIdForDomain(METAMASK_DOMAIN, networkClientId);
}
// setNetworkClientIdForMetamask(networkClientId: NetworkClientId) {
// this.setNetworkClientIdForDomain(METAMASK_DOMAIN, networkClientId);
// }

#setNetworkClientIdForDomain(
domain: Domain,
Expand Down Expand Up @@ -218,50 +218,41 @@ export class SelectedNetworkController extends BaseController<
domain: Domain,
networkClientId: NetworkClientId,
) {
if (domain !== METAMASK_DOMAIN && !this.state.perDomainNetwork) {
return;
if (domain === METAMASK_DOMAIN) {
throw new Error(
'NetworkClientId for domain "metamask" cannot be set on the SelectedNetworkController ',
);
}

if (domain !== METAMASK_DOMAIN && !this.#domainHasPermissions(domain)) {
if (!this.#domainHasPermissions(domain) || !this.state.perDomainNetwork) {
return;
}

//

// If setting for Metamask and perDomainNetwork is disabled, update all domains to the Metamask networkClientId in order to keep the proxies in sync
if (domain === METAMASK_DOMAIN && !this.state.perDomainNetwork) {
Object.entries(this.state.domains).forEach(
([entryDomain, networkClientIdForDomain]) => {
if (
networkClientIdForDomain !== networkClientId &&
entryDomain !== domain
) {
this.#setNetworkClientIdForDomain(entryDomain, networkClientId);
}
},
);
}
// if (domain === METAMASK_DOMAIN && !this.state.perDomainNetwork) {
// Object.entries(this.state.domains).forEach(
// ([entryDomain, networkClientIdForDomain]) => {
// if (
// networkClientIdForDomain !== networkClientId &&
// entryDomain !== domain
// ) {
// this.#setNetworkClientIdForDomain(entryDomain, networkClientId);
// }
// },
// );
// }

this.#setNetworkClientIdForDomain(domain, networkClientId);
}

getNetworkClientIdForDomain(domain: Domain): NetworkClientId | undefined {
if (domain === METAMASK_DOMAIN) {
return this.getNetworkClientIdForMetamask();
}
getNetworkClientIdForDomain(domain: Domain): NetworkClientId {
const { selectedNetworkClientId: metamaskSelectedNetworkClientId } =
this.messagingSystem.call('NetworkController:getState');
if (!this.state.perDomainNetwork) {
return undefined;
return metamaskSelectedNetworkClientId;
}
return this.state.domains[domain];
}

getNetworkClientIdForMetamask(): NetworkClientId {
return this.state.domains[METAMASK_DOMAIN];
}

#getNetworkClientIdForDomainOrMetamask(domain: Domain): NetworkClientId {
return (
this.getNetworkClientIdForDomain(domain) ??
this.getNetworkClientIdForMetamask()
);
return this.state.domains[domain] ?? metamaskSelectedNetworkClientId;
}

/**
Expand All @@ -271,11 +262,17 @@ export class SelectedNetworkController extends BaseController<
* @returns The proxy and block tracker proxies.
*/
getProviderAndBlockTracker(domain: Domain): NetworkProxy {
const networkClientId = this.getNetworkClientIdForDomain(domain);
if (!networkClientId) {
throw new Error(
'NetworkClientId has not been set for the requested domain',
);
}
let networkProxy = this.#proxies.get(domain);
if (networkProxy === undefined) {
const networkClient = this.messagingSystem.call(
'NetworkController:getNetworkClientById',
this.#getNetworkClientIdForDomainOrMetamask(domain),
networkClientId,
);
networkProxy = {
provider: createEventEmitterProxy(networkClient.provider),
Expand All @@ -300,8 +297,8 @@ export class SelectedNetworkController extends BaseController<
Object.keys(this.state.domains).forEach((domain) => {
this.setNetworkClientIdForDomain(
domain,
// when perDomainNetwork is false, getNetworkClientIdForDomainOrMetamask always returns the networkClientId for the domain 'metamask'
this.#getNetworkClientIdForDomainOrMetamask(domain),
// when perDomainNetwork is false, getNetworkClientIdForDomain always returns the networkClientId for the domain 'metamask'
this.getNetworkClientIdForDomain(domain),
);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,13 @@ export const createSelectedNetworkMiddleware = (
SelectedNetworkControllerActionTypes.getNetworkClientIdForDomain,
origin,
);
const getNetworkClientIdForMetamask = () =>
messenger.call(
SelectedNetworkControllerActionTypes.getNetworkClientIdForMetamask,
);

return (req: SelectedNetworkMiddlewareJsonRpcRequest, _, next) => {
if (!req.origin) {
throw new Error("Request object is lacking an 'origin'");
}

req.networkClientId =
getNetworkClientIdForDomain(req.origin) ??
getNetworkClientIdForMetamask();
req.networkClientId = getNetworkClientIdForDomain(req.origin);
return next();
};
};

0 comments on commit 88b679e

Please sign in to comment.