Skip to content

Commit

Permalink
Fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmire committed Dec 16, 2022
1 parent e51455a commit 469bbed
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 34 deletions.
15 changes: 1 addition & 14 deletions app/scripts/controllers/network/createInfuraClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import {

import { createInfuraMiddleware } from '@metamask/eth-json-rpc-infura';
import { PollingBlockTracker } from 'eth-block-tracker';
import createFilterMiddleware from 'eth-json-rpc-filters';
import createSubscriptionManager from 'eth-json-rpc-filters/subscriptionManager';

import { BUILT_IN_NETWORKS } from '../../../../shared/constants/network';

Expand All @@ -25,15 +23,6 @@ export default function createInfuraClient({ network, projectId }) {
const infuraProvider = providerFromMiddleware(infuraMiddleware);
const blockTracker = new PollingBlockTracker({ provider: infuraProvider });

const filterMiddleware = createFilterMiddleware({
infuraProvider,
blockTracker,
});
const subscriptionManager = createSubscriptionManager({
infuraProvider,
blockTracker,
});

const networkMiddleware = mergeMiddleware([
createNetworkAndChainIdMiddleware({ network }),
createBlockCacheMiddleware({ blockTracker }),
Expand All @@ -42,10 +31,8 @@ export default function createInfuraClient({ network, projectId }) {
createRetryOnEmptyMiddleware({ blockTracker, provider: infuraProvider }),
createBlockTrackerInspectorMiddleware({ blockTracker }),
infuraMiddleware,
filterMiddleware,
subscriptionManager.middleware,
]);
return { networkMiddleware, blockTracker, subscriptionManager };
return { networkMiddleware, blockTracker };
}

function createNetworkAndChainIdMiddleware({ network }) {
Expand Down
16 changes: 1 addition & 15 deletions app/scripts/controllers/network/createJsonRpcClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import {
providerFromMiddleware,
} from 'eth-json-rpc-middleware';
import { PollingBlockTracker } from 'eth-block-tracker';
import createFilterMiddleware from 'eth-json-rpc-filters';
import createSubscriptionManager from 'eth-json-rpc-filters/subscriptionManager';
import { SECOND } from '../../../../shared/constants/time';

const inTest = process.env.IN_TEST;
Expand All @@ -21,21 +19,11 @@ const getTestMiddlewares = () => {
export default function createJsonRpcClient({ rpcUrl, chainId }) {
const fetchMiddleware = createFetchMiddleware({ rpcUrl });
const blockProvider = providerFromMiddleware(fetchMiddleware);

const blockTracker = new PollingBlockTracker({
...blockTrackerOpts,
provider: blockProvider,
});

const filterMiddleware = createFilterMiddleware({
blockProvider,
blockTracker,
});
const subscriptionManager = createSubscriptionManager({
blockProvider,
blockTracker,
});

const networkMiddleware = mergeMiddleware([
...getTestMiddlewares(),
createChainIdMiddleware(chainId),
Expand All @@ -44,11 +32,9 @@ export default function createJsonRpcClient({ rpcUrl, chainId }) {
createInflightCacheMiddleware(),
createBlockTrackerInspectorMiddleware({ blockTracker }),
fetchMiddleware,
filterMiddleware,
subscriptionManager.middleware,
]);

return { networkMiddleware, blockTracker, subscriptionManager };
return { networkMiddleware, blockTracker };
}

function createChainIdMiddleware(chainId) {
Expand Down
25 changes: 21 additions & 4 deletions app/scripts/controllers/network/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ import { strict as assert } from 'assert';
import EventEmitter from 'events';
import { ComposedStore, ObservableStore } from '@metamask/obs-store';
import { JsonRpcEngine } from 'json-rpc-engine';
import { providerFromEngine } from 'eth-json-rpc-middleware';
import {
providerFromEngine,
providerFromMiddleware,
} from 'eth-json-rpc-middleware';
import log from 'loglevel';
import {
createSwappableProxy,
createEventEmitterProxy,
} from 'swappable-obj-proxy';
import EthQuery from 'eth-query';
import createFilterMiddleware from 'eth-json-rpc-filters';
import createSubscriptionManager from 'eth-json-rpc-filters/subscriptionManager';
import {
INFURA_PROVIDER_TYPES,
BUILT_IN_NETWORKS,
Expand Down Expand Up @@ -424,19 +429,31 @@ export default class NetworkController extends EventEmitter {
this._setNetworkClient(networkClient);
}

_setNetworkClient({ networkMiddleware, blockTracker, subscriptionManager }) {
_setNetworkClient({ networkMiddleware, blockTracker }) {
const networkProvider = providerFromMiddleware(networkMiddleware);
const filterMiddleware = createFilterMiddleware({
provider: networkProvider,
blockTracker,
});
const subscriptionManager = createSubscriptionManager({
provider: networkProvider,
blockTracker,
});
const metamaskMiddleware = createMetamaskMiddleware(
this._baseProviderParams,
);
const engine = new JsonRpcEngine();

const engine = new JsonRpcEngine();
subscriptionManager.events.on('notification', (message) =>
engine.emit('notification', message),
);

engine.push(filterMiddleware);
engine.push(subscriptionManager.middleware);
engine.push(metamaskMiddleware);
engine.push(networkMiddleware);

const provider = providerFromEngine(engine);

this._setProviderAndBlockTracker({ provider, blockTracker });
}

Expand Down
2 changes: 2 additions & 0 deletions app/scripts/controllers/permissions/specifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,12 @@ export const unrestrictedMethods = Object.freeze([
'eth_signTypedData_v1',
'eth_signTypedData_v3',
'eth_signTypedData_v4',
'eth_subscribe',
'eth_submitHashrate',
'eth_submitWork',
'eth_syncing',
'eth_uninstallFilter',
'eth_unsubscribe',
'metamask_getProviderState',
'metamask_watchAsset',
'net_listening',
Expand Down
12 changes: 11 additions & 1 deletion app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3732,9 +3732,19 @@ export default class MetamaskController extends EventEmitter {
* @param {tabId} [options.tabId] - The tab ID of the sender - if the sender is within a tab
*/
setupProviderEngine({ origin, subjectType, sender, tabId }) {
const { provider } = this;

// setup json rpc engine stack
const engine = new JsonRpcEngine();
const { provider } = this;

// forward notifications from network provider
provider.on('data', (error, message) => {
if (error) {
// This should never happen, this error parameter is never set
throw error;
}
engine.emit('notification', message);
});

if (isManifestV3) {
engine.push(createDupeReqFilterMiddleware());
Expand Down

0 comments on commit 469bbed

Please sign in to comment.