Skip to content
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

Adapt to eip 1193 provider changes #4442

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { BUILT_IN_NETWORKS, NetworkType } from '@metamask/controller-utils';
import { promisify } from 'util';

import { mockNetwork } from '../../../tests/mock-network';
import { createAutoManagedNetworkClient } from './create-auto-managed-network-client';
Expand Down Expand Up @@ -69,6 +68,7 @@ describe('createAutoManagedNetworkClient', () => {
expect('eventNames' in provider).toBe(true);
expect('send' in provider).toBe(true);
expect('sendAsync' in provider).toBe(true);
expect('request' in provider).toBe(true);
});

it('returns a provider proxy that acts like a provider, forwarding requests to the network', async () => {
Expand All @@ -91,7 +91,7 @@ describe('createAutoManagedNetworkClient', () => {
networkClientConfiguration,
);

const { result } = await promisify(provider.sendAsync).call(provider, {
const result = await provider.request({
id: 1,
jsonrpc: '2.0',
method: 'test_method',
Expand Down Expand Up @@ -125,13 +125,13 @@ describe('createAutoManagedNetworkClient', () => {
networkClientConfiguration,
);

await promisify(provider.sendAsync).call(provider, {
await provider.request({
id: 1,
jsonrpc: '2.0',
method: 'test_method',
params: [],
});
await promisify(provider.sendAsync).call(provider, {
await provider.request({
id: 2,
jsonrpc: '2.0',
method: 'test_method',
Expand Down
120 changes: 42 additions & 78 deletions packages/network-controller/tests/NetworkController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,15 +308,12 @@ describe('NetworkController', () => {

const { provider } = controller.getProviderAndBlockTracker();
assert(provider, 'Provider is not set');
const { result } = await promisify(provider.sendAsync).call(
provider,
{
id: 1,
jsonrpc: '2.0',
method: 'test_method',
params: [],
},
);
const result = await provider.request({
id: 1,
jsonrpc: '2.0',
method: 'test_method',
params: [],
});
expect(result).toBe('test response');
},
);
Expand Down Expand Up @@ -413,15 +410,12 @@ describe('NetworkController', () => {

const { provider } = controller.getProviderAndBlockTracker();
assert(provider, 'Provider is not set');
const { result } = await promisify(provider.sendAsync).call(
provider,
{
id: 1,
jsonrpc: '2.0',
method: 'test_method',
params: [],
},
);
const result = await provider.request({
id: 1,
jsonrpc: '2.0',
method: 'test_method',
params: [],
});
expect(result).toBe('test response');
},
);
Expand All @@ -440,7 +434,7 @@ describe('NetworkController', () => {
const { provider, blockTracker } =
controller.getProviderAndBlockTracker();

expect(provider).toHaveProperty('sendAsync');
expect(provider).toHaveProperty('request');
expect(blockTracker).toHaveProperty('checkForLatestBlock');
});
});
Expand Down Expand Up @@ -524,26 +518,20 @@ describe('NetworkController', () => {
const { provider } = controller.getProviderAndBlockTracker();
assert(provider, 'Provider is somehow unset');

const promisifiedSendAsync1 = promisify(provider.sendAsync).bind(
provider,
);
const response1 = await promisifiedSendAsync1({
const result1 = await provider.request({
id: '1',
jsonrpc: '2.0',
method: 'test',
});
expect(response1.result).toBe('test response 1');
expect(result1).toBe('test response 1');

await controller.setProviderType(networkType);
const promisifiedSendAsync2 = promisify(provider.sendAsync).bind(
provider,
);
const response2 = await promisifiedSendAsync2({
const result2 = await provider.request({
id: '2',
jsonrpc: '2.0',
method: 'test',
});
expect(response2.result).toBe('test response 2');
expect(result2).toBe('test response 2');
},
);
});
Expand Down Expand Up @@ -614,26 +602,20 @@ describe('NetworkController', () => {
const { provider } = controller.getProviderAndBlockTracker();
assert(provider, 'Provider is somehow unset');

const promisifiedSendAsync1 = promisify(provider.sendAsync).bind(
provider,
);
const response1 = await promisifiedSendAsync1({
const result1 = await provider.request({
id: '1',
jsonrpc: '2.0',
method: 'test',
});
expect(response1.result).toBe('test response 1');
expect(result1).toBe('test response 1');

await controller.setActiveNetwork('testNetworkConfigurationId');
const promisifiedSendAsync2 = promisify(provider.sendAsync).bind(
provider,
);
const response2 = await promisifiedSendAsync2({
const result2 = await provider.request({
id: '2',
jsonrpc: '2.0',
method: 'test',
});
expect(response2.result).toBe('test response 2');
expect(result2).toBe('test response 2');
},
);
});
Expand Down Expand Up @@ -1017,7 +999,7 @@ describe('NetworkController', () => {
],
]);
for (const networkClient of Object.values(networkClients)) {
expect(networkClient.provider).toHaveProperty('sendAsync');
expect(networkClient.provider).toHaveProperty('request');
expect(networkClient.blockTracker).toHaveProperty(
'checkForLatestBlock',
);
Expand Down Expand Up @@ -2909,15 +2891,12 @@ describe('NetworkController', () => {

const { provider } = controller.getProviderAndBlockTracker();
assert(provider, 'Provider is not set');
const { result } = await promisify(provider.sendAsync).call(
provider,
{
id: 1,
jsonrpc: '2.0',
method: 'test_method',
params: [],
},
);
const result = await provider.request({
id: 1,
jsonrpc: '2.0',
method: 'test_method',
params: [],
});
expect(result).toBe('test response from built-in network');
},
);
Expand Down Expand Up @@ -3013,15 +2992,12 @@ describe('NetworkController', () => {

const { provider } = controller.getProviderAndBlockTracker();
assert(provider, 'Provider is not set');
const { result } = await promisify(provider.sendAsync).call(
provider,
{
id: 1,
jsonrpc: '2.0',
method: 'test_method',
params: [],
},
);
const result = await provider.request({
id: 1,
jsonrpc: '2.0',
method: 'test_method',
params: [],
});
expect(result).toBe('test response from built-in network');
},
);
Expand Down Expand Up @@ -4153,15 +4129,12 @@ describe('NetworkController', () => {

const { provider } = controller.getProviderAndBlockTracker();
assert(provider, 'Provider is somehow unset');
const promisifiedSendAsync = promisify(provider.sendAsync).bind(
provider,
);
const response = await promisifiedSendAsync({
const result = await provider.request({
id: '1',
jsonrpc: '2.0',
method: 'test',
});
expect(response.result).toBe('test response');
expect(result).toBe('test response');
},
);
});
Expand Down Expand Up @@ -4713,15 +4686,12 @@ describe('NetworkController', () => {

const { provider } = controller.getProviderAndBlockTracker();
assert(provider, 'Provider is somehow unset');
const promisifiedSendAsync = promisify(provider.sendAsync).bind(
provider,
);
const response = await promisifiedSendAsync({
const result = await provider.request({
id: '1',
jsonrpc: '2.0',
method: 'test',
});
expect(response.result).toBe('test response');
expect(result).toBe('test response');
},
);
});
Expand Down Expand Up @@ -5201,16 +5171,13 @@ function refreshNetworkTests({
});
const { provider } = controller.getProviderAndBlockTracker();
assert(provider);
const promisifiedSendAsync = promisify(provider.sendAsync).bind(
provider,
);
const chainIdResult = await promisifiedSendAsync({
const chainIdResult = await provider.request({
id: 1,
jsonrpc: '2.0',
method: 'eth_chainId',
params: [],
});
expect(chainIdResult.result).toBe(toHex(111));
expect(chainIdResult).toBe(toHex(111));
},
);
});
Expand Down Expand Up @@ -5245,16 +5212,13 @@ function refreshNetworkTests({
});
const { provider } = controller.getProviderAndBlockTracker();
assert(provider);
const promisifiedSendAsync = promisify(provider.sendAsync).bind(
provider,
);
const chainIdResult = await promisifiedSendAsync({
const chainIdResult = await provider.request({
id: 1,
jsonrpc: '2.0',
method: 'eth_chainId',
params: [],
});
expect(chainIdResult.result).toBe(toHex(1337));
expect(chainIdResult).toBe(toHex(1337));
},
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ export function buildSelectedNetworkControllerMessenger({
getSubjectNames?: string[];
} = {}) {
const mockGetNetworkClientById = jest.fn().mockReturnValue({
provider: { sendAsync: jest.fn() },
provider: { request: jest.fn() },
blockTracker: { getLatestBlock: jest.fn() },
});
messenger.registerActionHandler(
'NetworkController:getNetworkClientById',
mockGetNetworkClientById,
);
const mockGetSelectedNetworkClient = jest.fn().mockReturnValue({
provider: { sendAsync: jest.fn() },
provider: { request: jest.fn() },
blockTracker: { getLatestBlock: jest.fn() },
});
messenger.registerActionHandler(
Expand Down Expand Up @@ -154,7 +154,7 @@ const setup = ({
const createEventEmitterProxyMock = jest.mocked(createEventEmitterProxy);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
createEventEmitterProxyMock.mockImplementation((initialTarget: any) => {
if (initialTarget?.sendAsync !== undefined) {
if (initialTarget?.request !== undefined) {
return mockProviderProxy;
}
if (initialTarget?.getLatestBlock !== undefined) {
Expand Down Expand Up @@ -440,7 +440,7 @@ describe('SelectedNetworkController', () => {

expect(mockProviderProxy.setTarget).toHaveBeenNthCalledWith(
2,
expect.objectContaining({ sendAsync: expect.any(Function) }),
expect.objectContaining({ request: expect.any(Function) }),
);
expect(mockProviderProxy.setTarget).toHaveBeenCalledTimes(2);
});
Expand Down Expand Up @@ -785,7 +785,7 @@ describe('SelectedNetworkController', () => {
);

expect(mockProviderProxy.setTarget).toHaveBeenCalledWith(
expect.objectContaining({ sendAsync: expect.any(Function) }),
expect.objectContaining({ request: expect.any(Function) }),
);
expect(mockProviderProxy.setTarget).toHaveBeenCalledTimes(1);

Expand Down
Loading