Skip to content

Commit

Permalink
fix _handleChainChanged in MetaMaskInpageProvider to handle networkVe…
Browse files Browse the repository at this point in the history
…rsion (#171)

fix _handleChainChanged in MetaMaskInpageProvider to handle networkVersion

Co-authored-by: Erik Marks <25517051+rekmarks@users.noreply.github.com>
  • Loading branch information
shanejonas and rekmarks authored May 12, 2021
1 parent c6a1f46 commit 87f33cf
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/BaseProvider.rpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,25 @@ describe('BaseProvider: RPC', () => {
});
});
});
describe('provider events', () => {
it('calls chainChanged when it chainId changes ', async () => {
const mockStream = new MockDuplexStream();
const baseProvider = new BaseProvider(mockStream);
(baseProvider as any)._state.initialized = true;
await new Promise((resolve) => {
baseProvider.on('chainChanged', (changed) => {
expect(changed).toBeDefined();
resolve(undefined);
});
mockStream.push({
name: 'metamask-provider',
data: {
jsonrpc: '2.0',
method: 'metamask_chainChanged',
params: { chainId: '0x1', networkVersion: '0x1' },
},
});
});
});
});
});
42 changes: 42 additions & 0 deletions src/MetaMaskInpageProvider.rpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -636,4 +636,46 @@ describe('MetaMaskInpageProvider: RPC', () => {
);
});
});

describe('provider events', () => {
it('calls chainChanged when it chainId changes ', async () => {
const mockStream = new MockDuplexStream();
const inpageProvider = new MetaMaskInpageProvider(mockStream);
(inpageProvider as any)._state.initialized = true;
await new Promise((resolve) => {
inpageProvider.on('chainChanged', (changed) => {
expect(changed).toBe('0x1');
resolve(undefined);
});
mockStream.push({
name: 'metamask-provider',
data: {
jsonrpc: '2.0',
method: 'metamask_chainChanged',
params: { chainId: '0x1', networkVersion: '0x1' },
},
});
});
});

it('calls networkChanged when it networkVersion changes ', async () => {
const mockStream = new MockDuplexStream();
const inpageProvider = new MetaMaskInpageProvider(mockStream);
(inpageProvider as any)._state.initialized = true;
await new Promise((resolve) => {
inpageProvider.on('networkChanged', (changed) => {
expect(changed).toBe('0x1');
resolve(undefined);
});
mockStream.push({
name: 'metamask-provider',
data: {
jsonrpc: '2.0',
method: 'metamask_chainChanged',
params: { chainId: '0x1', networkVersion: '0x1' },
},
});
});
});
});
});
30 changes: 30 additions & 0 deletions src/MetaMaskInpageProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,4 +395,34 @@ export default class MetaMaskInpageProvider extends BaseProvider {
},
);
}

/**
* Upon receipt of a new chainId and networkVersion, emits corresponding
* events and sets relevant public state.
* Does nothing if neither the chainId nor the networkVersion are different
* from existing values.
*
* @emits MetamaskInpageProvider#chainChanged
* @emits MetamaskInpageProvider#networkChanged
* @param networkInfo - An object with network info.
* @param networkInfo.chainId - The latest chain ID.
* @param networkInfo.networkVersion - The latest network ID.
*/
protected _handleChainChanged({
chainId,
networkVersion,
}: { chainId?: string; networkVersion?: string } = {}) {
super._handleChainChanged({ chainId, networkVersion });

if (
networkVersion &&
networkVersion !== 'loading' &&
networkVersion !== this.networkVersion
) {
this.networkVersion = networkVersion;
if (this._state.initialized) {
this.emit('networkChanged', this.networkVersion);
}
}
}
}

0 comments on commit 87f33cf

Please sign in to comment.