2021-05-07 21:38:24 +02:00
|
|
|
import { strict as assert } from 'assert';
|
2021-02-04 19:15:23 +01:00
|
|
|
import sinon from 'sinon';
|
2021-09-09 22:56:27 +02:00
|
|
|
import nock from 'nock';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { ObservableStore } from '@metamask/obs-store';
|
|
|
|
import BigNumber from 'bignumber.js';
|
2022-11-24 20:59:07 +01:00
|
|
|
import { ControllerMessenger } from '@metamask/base-controller';
|
2021-09-09 22:56:27 +02:00
|
|
|
import {
|
|
|
|
TokenListController,
|
2021-09-10 19:37:19 +02:00
|
|
|
TokensController,
|
2022-08-10 03:26:25 +02:00
|
|
|
AssetsContractController,
|
2022-11-24 20:59:07 +01:00
|
|
|
} from '@metamask/assets-controllers';
|
2023-06-09 22:48:48 +02:00
|
|
|
import { toHex } from '@metamask/controller-utils';
|
2023-06-22 20:46:09 +02:00
|
|
|
import { NetworkController } from '@metamask/network-controller';
|
2022-09-14 16:55:31 +02:00
|
|
|
import { NETWORK_TYPES } from '../../../shared/constants/network';
|
2021-09-10 19:37:19 +02:00
|
|
|
import { toChecksumHexAddress } from '../../../shared/modules/hexstring-utils';
|
2021-03-16 22:00:08 +01:00
|
|
|
import DetectTokensController from './detect-tokens';
|
|
|
|
import PreferencesController from './preferences';
|
2018-06-28 04:18:06 +02:00
|
|
|
|
2023-06-22 20:46:09 +02:00
|
|
|
function buildMessenger() {
|
|
|
|
return new ControllerMessenger().getRestricted({
|
|
|
|
name: 'DetectTokensController',
|
|
|
|
allowedEvents: ['NetworkController:stateChange'],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('DetectTokensController', function () {
|
NetworkController: Split `network` into `networkId` and `networkStatus` (#17556)
The `network` store of the network controller crams two types of data
into one place. It roughly tracks whether we have enough information to
make requests to the network and whether the network is capable of
receiving requests, but it also stores the ID of the network (as
obtained via `net_version`).
Generally we shouldn't be using the network ID for anything, as it has
been completely replaced by chain ID, which all custom RPC endpoints
have been required to support for over a year now. However, as the
network ID is used in various places within the extension codebase,
removing it entirely would be a non-trivial effort. So, minimally, this
commit splits `network` into two stores: `networkId` and
`networkStatus`. But it also expands the concept of network status.
Previously, the network was in one of two states: "loading" and
"not-loading". But now it can be in one of four states:
- `available`: The network is able to receive and respond to requests.
- `unavailable`: The network is not able to receive and respond to
requests for unknown reasons.
- `blocked`: The network is actively blocking requests based on the
user's geolocation. (This is specific to Infura.)
- `unknown`: We don't know whether the network can receive and respond
to requests, either because we haven't checked or we tried to check
and were unsuccessful.
This commit also changes how the network status is determined —
specifically, how many requests are used to determine that status, when
they occur, and whether they are awaited. Previously, the network
controller would make 2 to 3 requests during the course of running
`lookupNetwork`.
* First, if it was an Infura network, it would make a request for
`eth_blockNumber` to determine whether Infura was blocking requests or
not, then emit an appropriate event. This operation was not awaited.
* Then, regardless of the network, it would fetch the network ID via
`net_version`. This operation was awaited.
* Finally, regardless of the network, it would fetch the latest block
via `eth_getBlockByNumber`, then use the result to determine whether
the network supported EIP-1559. This operation was awaited.
Now:
* One fewer request is made, specifically `eth_blockNumber`, as we don't
need to make an extra request to determine whether Infura is blocking
requests; we can reuse `eth_getBlockByNumber`;
* All requests are awaited, which makes `lookupNetwork` run fully
in-band instead of partially out-of-band; and
* Both requests for `net_version` and `eth_getBlockByNumber` are
performed in parallel to make `lookupNetwork` run slightly faster.
2023-03-31 00:49:12 +02:00
|
|
|
let sandbox,
|
|
|
|
assetsContractController,
|
2022-08-10 03:26:25 +02:00
|
|
|
keyringMemStore,
|
|
|
|
network,
|
|
|
|
preferences,
|
|
|
|
provider,
|
|
|
|
tokensController,
|
|
|
|
tokenListController;
|
2018-07-30 15:26:34 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const noop = () => undefined;
|
2018-07-30 15:26:34 +02:00
|
|
|
|
|
|
|
const networkControllerProviderConfig = {
|
|
|
|
getAccounts: noop,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-07-30 15:26:34 +02:00
|
|
|
|
NetworkController: Split `network` into `networkId` and `networkStatus` (#17556)
The `network` store of the network controller crams two types of data
into one place. It roughly tracks whether we have enough information to
make requests to the network and whether the network is capable of
receiving requests, but it also stores the ID of the network (as
obtained via `net_version`).
Generally we shouldn't be using the network ID for anything, as it has
been completely replaced by chain ID, which all custom RPC endpoints
have been required to support for over a year now. However, as the
network ID is used in various places within the extension codebase,
removing it entirely would be a non-trivial effort. So, minimally, this
commit splits `network` into two stores: `networkId` and
`networkStatus`. But it also expands the concept of network status.
Previously, the network was in one of two states: "loading" and
"not-loading". But now it can be in one of four states:
- `available`: The network is able to receive and respond to requests.
- `unavailable`: The network is not able to receive and respond to
requests for unknown reasons.
- `blocked`: The network is actively blocking requests based on the
user's geolocation. (This is specific to Infura.)
- `unknown`: We don't know whether the network can receive and respond
to requests, either because we haven't checked or we tried to check
and were unsuccessful.
This commit also changes how the network status is determined —
specifically, how many requests are used to determine that status, when
they occur, and whether they are awaited. Previously, the network
controller would make 2 to 3 requests during the course of running
`lookupNetwork`.
* First, if it was an Infura network, it would make a request for
`eth_blockNumber` to determine whether Infura was blocking requests or
not, then emit an appropriate event. This operation was not awaited.
* Then, regardless of the network, it would fetch the network ID via
`net_version`. This operation was awaited.
* Finally, regardless of the network, it would fetch the latest block
via `eth_getBlockByNumber`, then use the result to determine whether
the network supported EIP-1559. This operation was awaited.
Now:
* One fewer request is made, specifically `eth_blockNumber`, as we don't
need to make an extra request to determine whether Infura is blocking
requests; we can reuse `eth_getBlockByNumber`;
* All requests are awaited, which makes `lookupNetwork` run fully
in-band instead of partially out-of-band; and
* Both requests for `net_version` and `eth_getBlockByNumber` are
performed in parallel to make `lookupNetwork` run slightly faster.
2023-03-31 00:49:12 +02:00
|
|
|
const infuraProjectId = 'infura-project-id';
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
beforeEach(async function () {
|
NetworkController: Split `network` into `networkId` and `networkStatus` (#17556)
The `network` store of the network controller crams two types of data
into one place. It roughly tracks whether we have enough information to
make requests to the network and whether the network is capable of
receiving requests, but it also stores the ID of the network (as
obtained via `net_version`).
Generally we shouldn't be using the network ID for anything, as it has
been completely replaced by chain ID, which all custom RPC endpoints
have been required to support for over a year now. However, as the
network ID is used in various places within the extension codebase,
removing it entirely would be a non-trivial effort. So, minimally, this
commit splits `network` into two stores: `networkId` and
`networkStatus`. But it also expands the concept of network status.
Previously, the network was in one of two states: "loading" and
"not-loading". But now it can be in one of four states:
- `available`: The network is able to receive and respond to requests.
- `unavailable`: The network is not able to receive and respond to
requests for unknown reasons.
- `blocked`: The network is actively blocking requests based on the
user's geolocation. (This is specific to Infura.)
- `unknown`: We don't know whether the network can receive and respond
to requests, either because we haven't checked or we tried to check
and were unsuccessful.
This commit also changes how the network status is determined —
specifically, how many requests are used to determine that status, when
they occur, and whether they are awaited. Previously, the network
controller would make 2 to 3 requests during the course of running
`lookupNetwork`.
* First, if it was an Infura network, it would make a request for
`eth_blockNumber` to determine whether Infura was blocking requests or
not, then emit an appropriate event. This operation was not awaited.
* Then, regardless of the network, it would fetch the network ID via
`net_version`. This operation was awaited.
* Finally, regardless of the network, it would fetch the latest block
via `eth_getBlockByNumber`, then use the result to determine whether
the network supported EIP-1559. This operation was awaited.
Now:
* One fewer request is made, specifically `eth_blockNumber`, as we don't
need to make an extra request to determine whether Infura is blocking
requests; we can reuse `eth_getBlockByNumber`;
* All requests are awaited, which makes `lookupNetwork` run fully
in-band instead of partially out-of-band; and
* Both requests for `net_version` and `eth_getBlockByNumber` are
performed in parallel to make `lookupNetwork` run slightly faster.
2023-03-31 00:49:12 +02:00
|
|
|
sandbox = sinon.createSandbox();
|
|
|
|
// Disable all requests, even those to localhost
|
|
|
|
nock.disableNetConnect();
|
|
|
|
nock('https://mainnet.infura.io')
|
|
|
|
.post(`/v3/${infuraProjectId}`)
|
|
|
|
.reply(200, (_uri, requestBody) => {
|
|
|
|
if (requestBody.method === 'eth_getBlockByNumber') {
|
|
|
|
return {
|
|
|
|
id: requestBody.id,
|
|
|
|
jsonrpc: '2.0',
|
|
|
|
result: {
|
|
|
|
number: '0x42',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2022-08-10 03:26:25 +02:00
|
|
|
|
NetworkController: Split `network` into `networkId` and `networkStatus` (#17556)
The `network` store of the network controller crams two types of data
into one place. It roughly tracks whether we have enough information to
make requests to the network and whether the network is capable of
receiving requests, but it also stores the ID of the network (as
obtained via `net_version`).
Generally we shouldn't be using the network ID for anything, as it has
been completely replaced by chain ID, which all custom RPC endpoints
have been required to support for over a year now. However, as the
network ID is used in various places within the extension codebase,
removing it entirely would be a non-trivial effort. So, minimally, this
commit splits `network` into two stores: `networkId` and
`networkStatus`. But it also expands the concept of network status.
Previously, the network was in one of two states: "loading" and
"not-loading". But now it can be in one of four states:
- `available`: The network is able to receive and respond to requests.
- `unavailable`: The network is not able to receive and respond to
requests for unknown reasons.
- `blocked`: The network is actively blocking requests based on the
user's geolocation. (This is specific to Infura.)
- `unknown`: We don't know whether the network can receive and respond
to requests, either because we haven't checked or we tried to check
and were unsuccessful.
This commit also changes how the network status is determined —
specifically, how many requests are used to determine that status, when
they occur, and whether they are awaited. Previously, the network
controller would make 2 to 3 requests during the course of running
`lookupNetwork`.
* First, if it was an Infura network, it would make a request for
`eth_blockNumber` to determine whether Infura was blocking requests or
not, then emit an appropriate event. This operation was not awaited.
* Then, regardless of the network, it would fetch the network ID via
`net_version`. This operation was awaited.
* Finally, regardless of the network, it would fetch the latest block
via `eth_getBlockByNumber`, then use the result to determine whether
the network supported EIP-1559. This operation was awaited.
Now:
* One fewer request is made, specifically `eth_blockNumber`, as we don't
need to make an extra request to determine whether Infura is blocking
requests; we can reuse `eth_getBlockByNumber`;
* All requests are awaited, which makes `lookupNetwork` run fully
in-band instead of partially out-of-band; and
* Both requests for `net_version` and `eth_getBlockByNumber` are
performed in parallel to make `lookupNetwork` run slightly faster.
2023-03-31 00:49:12 +02:00
|
|
|
if (requestBody.method === 'eth_blockNumber') {
|
|
|
|
return {
|
|
|
|
id: requestBody.id,
|
|
|
|
jsonrpc: '2.0',
|
|
|
|
result: '0x42',
|
|
|
|
};
|
|
|
|
}
|
2022-08-10 03:26:25 +02:00
|
|
|
|
NetworkController: Split `network` into `networkId` and `networkStatus` (#17556)
The `network` store of the network controller crams two types of data
into one place. It roughly tracks whether we have enough information to
make requests to the network and whether the network is capable of
receiving requests, but it also stores the ID of the network (as
obtained via `net_version`).
Generally we shouldn't be using the network ID for anything, as it has
been completely replaced by chain ID, which all custom RPC endpoints
have been required to support for over a year now. However, as the
network ID is used in various places within the extension codebase,
removing it entirely would be a non-trivial effort. So, minimally, this
commit splits `network` into two stores: `networkId` and
`networkStatus`. But it also expands the concept of network status.
Previously, the network was in one of two states: "loading" and
"not-loading". But now it can be in one of four states:
- `available`: The network is able to receive and respond to requests.
- `unavailable`: The network is not able to receive and respond to
requests for unknown reasons.
- `blocked`: The network is actively blocking requests based on the
user's geolocation. (This is specific to Infura.)
- `unknown`: We don't know whether the network can receive and respond
to requests, either because we haven't checked or we tried to check
and were unsuccessful.
This commit also changes how the network status is determined —
specifically, how many requests are used to determine that status, when
they occur, and whether they are awaited. Previously, the network
controller would make 2 to 3 requests during the course of running
`lookupNetwork`.
* First, if it was an Infura network, it would make a request for
`eth_blockNumber` to determine whether Infura was blocking requests or
not, then emit an appropriate event. This operation was not awaited.
* Then, regardless of the network, it would fetch the network ID via
`net_version`. This operation was awaited.
* Finally, regardless of the network, it would fetch the latest block
via `eth_getBlockByNumber`, then use the result to determine whether
the network supported EIP-1559. This operation was awaited.
Now:
* One fewer request is made, specifically `eth_blockNumber`, as we don't
need to make an extra request to determine whether Infura is blocking
requests; we can reuse `eth_getBlockByNumber`;
* All requests are awaited, which makes `lookupNetwork` run fully
in-band instead of partially out-of-band; and
* Both requests for `net_version` and `eth_getBlockByNumber` are
performed in parallel to make `lookupNetwork` run slightly faster.
2023-03-31 00:49:12 +02:00
|
|
|
throw new Error(`(Infura) Mock not defined for ${requestBody.method}`);
|
|
|
|
})
|
|
|
|
.persist();
|
|
|
|
nock('https://sepolia.infura.io')
|
|
|
|
.post(`/v3/${infuraProjectId}`)
|
|
|
|
.reply(200, (_uri, requestBody) => {
|
|
|
|
if (requestBody.method === 'eth_getBlockByNumber') {
|
|
|
|
return {
|
|
|
|
id: requestBody.id,
|
|
|
|
jsonrpc: '2.0',
|
|
|
|
result: {
|
|
|
|
number: '0x42',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2022-08-10 03:26:25 +02:00
|
|
|
|
NetworkController: Split `network` into `networkId` and `networkStatus` (#17556)
The `network` store of the network controller crams two types of data
into one place. It roughly tracks whether we have enough information to
make requests to the network and whether the network is capable of
receiving requests, but it also stores the ID of the network (as
obtained via `net_version`).
Generally we shouldn't be using the network ID for anything, as it has
been completely replaced by chain ID, which all custom RPC endpoints
have been required to support for over a year now. However, as the
network ID is used in various places within the extension codebase,
removing it entirely would be a non-trivial effort. So, minimally, this
commit splits `network` into two stores: `networkId` and
`networkStatus`. But it also expands the concept of network status.
Previously, the network was in one of two states: "loading" and
"not-loading". But now it can be in one of four states:
- `available`: The network is able to receive and respond to requests.
- `unavailable`: The network is not able to receive and respond to
requests for unknown reasons.
- `blocked`: The network is actively blocking requests based on the
user's geolocation. (This is specific to Infura.)
- `unknown`: We don't know whether the network can receive and respond
to requests, either because we haven't checked or we tried to check
and were unsuccessful.
This commit also changes how the network status is determined —
specifically, how many requests are used to determine that status, when
they occur, and whether they are awaited. Previously, the network
controller would make 2 to 3 requests during the course of running
`lookupNetwork`.
* First, if it was an Infura network, it would make a request for
`eth_blockNumber` to determine whether Infura was blocking requests or
not, then emit an appropriate event. This operation was not awaited.
* Then, regardless of the network, it would fetch the network ID via
`net_version`. This operation was awaited.
* Finally, regardless of the network, it would fetch the latest block
via `eth_getBlockByNumber`, then use the result to determine whether
the network supported EIP-1559. This operation was awaited.
Now:
* One fewer request is made, specifically `eth_blockNumber`, as we don't
need to make an extra request to determine whether Infura is blocking
requests; we can reuse `eth_getBlockByNumber`;
* All requests are awaited, which makes `lookupNetwork` run fully
in-band instead of partially out-of-band; and
* Both requests for `net_version` and `eth_getBlockByNumber` are
performed in parallel to make `lookupNetwork` run slightly faster.
2023-03-31 00:49:12 +02:00
|
|
|
if (requestBody.method === 'eth_blockNumber') {
|
|
|
|
return {
|
|
|
|
id: requestBody.id,
|
|
|
|
jsonrpc: '2.0',
|
|
|
|
result: '0x42',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(`(Infura) Mock not defined for ${requestBody.method}`);
|
|
|
|
})
|
|
|
|
.persist();
|
|
|
|
nock('http://localhost:8545')
|
|
|
|
.post('/')
|
|
|
|
.reply(200, (_uri, requestBody) => {
|
|
|
|
if (requestBody.method === 'eth_getBlockByNumber') {
|
|
|
|
return {
|
|
|
|
id: requestBody.id,
|
|
|
|
jsonrpc: '2.0',
|
|
|
|
result: {
|
|
|
|
number: '0x42',
|
2023-01-17 16:23:04 +01:00
|
|
|
},
|
|
|
|
};
|
NetworkController: Split `network` into `networkId` and `networkStatus` (#17556)
The `network` store of the network controller crams two types of data
into one place. It roughly tracks whether we have enough information to
make requests to the network and whether the network is capable of
receiving requests, but it also stores the ID of the network (as
obtained via `net_version`).
Generally we shouldn't be using the network ID for anything, as it has
been completely replaced by chain ID, which all custom RPC endpoints
have been required to support for over a year now. However, as the
network ID is used in various places within the extension codebase,
removing it entirely would be a non-trivial effort. So, minimally, this
commit splits `network` into two stores: `networkId` and
`networkStatus`. But it also expands the concept of network status.
Previously, the network was in one of two states: "loading" and
"not-loading". But now it can be in one of four states:
- `available`: The network is able to receive and respond to requests.
- `unavailable`: The network is not able to receive and respond to
requests for unknown reasons.
- `blocked`: The network is actively blocking requests based on the
user's geolocation. (This is specific to Infura.)
- `unknown`: We don't know whether the network can receive and respond
to requests, either because we haven't checked or we tried to check
and were unsuccessful.
This commit also changes how the network status is determined —
specifically, how many requests are used to determine that status, when
they occur, and whether they are awaited. Previously, the network
controller would make 2 to 3 requests during the course of running
`lookupNetwork`.
* First, if it was an Infura network, it would make a request for
`eth_blockNumber` to determine whether Infura was blocking requests or
not, then emit an appropriate event. This operation was not awaited.
* Then, regardless of the network, it would fetch the network ID via
`net_version`. This operation was awaited.
* Finally, regardless of the network, it would fetch the latest block
via `eth_getBlockByNumber`, then use the result to determine whether
the network supported EIP-1559. This operation was awaited.
Now:
* One fewer request is made, specifically `eth_blockNumber`, as we don't
need to make an extra request to determine whether Infura is blocking
requests; we can reuse `eth_getBlockByNumber`;
* All requests are awaited, which makes `lookupNetwork` run fully
in-band instead of partially out-of-band; and
* Both requests for `net_version` and `eth_getBlockByNumber` are
performed in parallel to make `lookupNetwork` run slightly faster.
2023-03-31 00:49:12 +02:00
|
|
|
}
|
2022-08-10 03:26:25 +02:00
|
|
|
|
NetworkController: Split `network` into `networkId` and `networkStatus` (#17556)
The `network` store of the network controller crams two types of data
into one place. It roughly tracks whether we have enough information to
make requests to the network and whether the network is capable of
receiving requests, but it also stores the ID of the network (as
obtained via `net_version`).
Generally we shouldn't be using the network ID for anything, as it has
been completely replaced by chain ID, which all custom RPC endpoints
have been required to support for over a year now. However, as the
network ID is used in various places within the extension codebase,
removing it entirely would be a non-trivial effort. So, minimally, this
commit splits `network` into two stores: `networkId` and
`networkStatus`. But it also expands the concept of network status.
Previously, the network was in one of two states: "loading" and
"not-loading". But now it can be in one of four states:
- `available`: The network is able to receive and respond to requests.
- `unavailable`: The network is not able to receive and respond to
requests for unknown reasons.
- `blocked`: The network is actively blocking requests based on the
user's geolocation. (This is specific to Infura.)
- `unknown`: We don't know whether the network can receive and respond
to requests, either because we haven't checked or we tried to check
and were unsuccessful.
This commit also changes how the network status is determined —
specifically, how many requests are used to determine that status, when
they occur, and whether they are awaited. Previously, the network
controller would make 2 to 3 requests during the course of running
`lookupNetwork`.
* First, if it was an Infura network, it would make a request for
`eth_blockNumber` to determine whether Infura was blocking requests or
not, then emit an appropriate event. This operation was not awaited.
* Then, regardless of the network, it would fetch the network ID via
`net_version`. This operation was awaited.
* Finally, regardless of the network, it would fetch the latest block
via `eth_getBlockByNumber`, then use the result to determine whether
the network supported EIP-1559. This operation was awaited.
Now:
* One fewer request is made, specifically `eth_blockNumber`, as we don't
need to make an extra request to determine whether Infura is blocking
requests; we can reuse `eth_getBlockByNumber`;
* All requests are awaited, which makes `lookupNetwork` run fully
in-band instead of partially out-of-band; and
* Both requests for `net_version` and `eth_getBlockByNumber` are
performed in parallel to make `lookupNetwork` run slightly faster.
2023-03-31 00:49:12 +02:00
|
|
|
if (requestBody.method === 'eth_blockNumber') {
|
|
|
|
return {
|
|
|
|
id: requestBody.id,
|
|
|
|
jsonrpc: '2.0',
|
|
|
|
result: '0x42',
|
|
|
|
};
|
|
|
|
}
|
2022-08-10 03:26:25 +02:00
|
|
|
|
NetworkController: Split `network` into `networkId` and `networkStatus` (#17556)
The `network` store of the network controller crams two types of data
into one place. It roughly tracks whether we have enough information to
make requests to the network and whether the network is capable of
receiving requests, but it also stores the ID of the network (as
obtained via `net_version`).
Generally we shouldn't be using the network ID for anything, as it has
been completely replaced by chain ID, which all custom RPC endpoints
have been required to support for over a year now. However, as the
network ID is used in various places within the extension codebase,
removing it entirely would be a non-trivial effort. So, minimally, this
commit splits `network` into two stores: `networkId` and
`networkStatus`. But it also expands the concept of network status.
Previously, the network was in one of two states: "loading" and
"not-loading". But now it can be in one of four states:
- `available`: The network is able to receive and respond to requests.
- `unavailable`: The network is not able to receive and respond to
requests for unknown reasons.
- `blocked`: The network is actively blocking requests based on the
user's geolocation. (This is specific to Infura.)
- `unknown`: We don't know whether the network can receive and respond
to requests, either because we haven't checked or we tried to check
and were unsuccessful.
This commit also changes how the network status is determined —
specifically, how many requests are used to determine that status, when
they occur, and whether they are awaited. Previously, the network
controller would make 2 to 3 requests during the course of running
`lookupNetwork`.
* First, if it was an Infura network, it would make a request for
`eth_blockNumber` to determine whether Infura was blocking requests or
not, then emit an appropriate event. This operation was not awaited.
* Then, regardless of the network, it would fetch the network ID via
`net_version`. This operation was awaited.
* Finally, regardless of the network, it would fetch the latest block
via `eth_getBlockByNumber`, then use the result to determine whether
the network supported EIP-1559. This operation was awaited.
Now:
* One fewer request is made, specifically `eth_blockNumber`, as we don't
need to make an extra request to determine whether Infura is blocking
requests; we can reuse `eth_getBlockByNumber`;
* All requests are awaited, which makes `lookupNetwork` run fully
in-band instead of partially out-of-band; and
* Both requests for `net_version` and `eth_getBlockByNumber` are
performed in parallel to make `lookupNetwork` run slightly faster.
2023-03-31 00:49:12 +02:00
|
|
|
if (requestBody.method === 'net_version') {
|
|
|
|
return {
|
|
|
|
id: requestBody.id,
|
|
|
|
jsonrpc: '2.0',
|
|
|
|
result: '1337',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(
|
|
|
|
`(localhost) Mock not defined for ${requestBody.method}`,
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.persist();
|
2021-09-09 22:56:27 +02:00
|
|
|
nock('https://token-api.metaswap.codefi.network')
|
|
|
|
.get(`/tokens/1`)
|
|
|
|
.reply(200, [
|
|
|
|
{
|
|
|
|
address: '0xc011a73ee8576fb46f5e1c5751ca3b9fe0af2a6f',
|
|
|
|
symbol: 'SNX',
|
|
|
|
decimals: 18,
|
|
|
|
occurrences: 11,
|
|
|
|
aggregators: [
|
|
|
|
'paraswap',
|
|
|
|
'pmm',
|
|
|
|
'airswapLight',
|
|
|
|
'zeroEx',
|
|
|
|
'bancor',
|
|
|
|
'coinGecko',
|
|
|
|
'zapper',
|
|
|
|
'kleros',
|
|
|
|
'zerion',
|
|
|
|
'cmc',
|
|
|
|
'oneInch',
|
|
|
|
],
|
|
|
|
name: 'Synthetix',
|
|
|
|
iconUrl: 'https://airswap-token-images.s3.amazonaws.com/SNX.png',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
address: '0x514910771af9ca656af840dff83e8264ecf986ca',
|
|
|
|
symbol: 'LINK',
|
|
|
|
decimals: 18,
|
|
|
|
occurrences: 11,
|
|
|
|
aggregators: [
|
|
|
|
'paraswap',
|
|
|
|
'pmm',
|
|
|
|
'airswapLight',
|
|
|
|
'zeroEx',
|
|
|
|
'bancor',
|
|
|
|
'coinGecko',
|
|
|
|
'zapper',
|
|
|
|
'kleros',
|
|
|
|
'zerion',
|
|
|
|
'cmc',
|
|
|
|
'oneInch',
|
|
|
|
],
|
|
|
|
name: 'Chainlink',
|
|
|
|
iconUrl: 'https://s3.amazonaws.com/airswap-token-images/LINK.png',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
address: '0x1f573d6fb3f13d689ff844b4ce37794d79a7ff1c',
|
|
|
|
symbol: 'BNT',
|
|
|
|
decimals: 18,
|
|
|
|
occurrences: 11,
|
|
|
|
aggregators: [
|
|
|
|
'paraswap',
|
|
|
|
'pmm',
|
|
|
|
'airswapLight',
|
|
|
|
'zeroEx',
|
|
|
|
'bancor',
|
|
|
|
'coinGecko',
|
|
|
|
'zapper',
|
|
|
|
'kleros',
|
|
|
|
'zerion',
|
|
|
|
'cmc',
|
|
|
|
'oneInch',
|
|
|
|
],
|
|
|
|
name: 'Bancor',
|
|
|
|
iconUrl: 'https://s3.amazonaws.com/airswap-token-images/BNT.png',
|
|
|
|
},
|
|
|
|
])
|
|
|
|
.get(`/tokens/3`)
|
|
|
|
.reply(200, { error: 'ChainId 3 is not supported' })
|
|
|
|
.persist();
|
NetworkController: Split `network` into `networkId` and `networkStatus` (#17556)
The `network` store of the network controller crams two types of data
into one place. It roughly tracks whether we have enough information to
make requests to the network and whether the network is capable of
receiving requests, but it also stores the ID of the network (as
obtained via `net_version`).
Generally we shouldn't be using the network ID for anything, as it has
been completely replaced by chain ID, which all custom RPC endpoints
have been required to support for over a year now. However, as the
network ID is used in various places within the extension codebase,
removing it entirely would be a non-trivial effort. So, minimally, this
commit splits `network` into two stores: `networkId` and
`networkStatus`. But it also expands the concept of network status.
Previously, the network was in one of two states: "loading" and
"not-loading". But now it can be in one of four states:
- `available`: The network is able to receive and respond to requests.
- `unavailable`: The network is not able to receive and respond to
requests for unknown reasons.
- `blocked`: The network is actively blocking requests based on the
user's geolocation. (This is specific to Infura.)
- `unknown`: We don't know whether the network can receive and respond
to requests, either because we haven't checked or we tried to check
and were unsuccessful.
This commit also changes how the network status is determined —
specifically, how many requests are used to determine that status, when
they occur, and whether they are awaited. Previously, the network
controller would make 2 to 3 requests during the course of running
`lookupNetwork`.
* First, if it was an Infura network, it would make a request for
`eth_blockNumber` to determine whether Infura was blocking requests or
not, then emit an appropriate event. This operation was not awaited.
* Then, regardless of the network, it would fetch the network ID via
`net_version`. This operation was awaited.
* Finally, regardless of the network, it would fetch the latest block
via `eth_getBlockByNumber`, then use the result to determine whether
the network supported EIP-1559. This operation was awaited.
Now:
* One fewer request is made, specifically `eth_blockNumber`, as we don't
need to make an extra request to determine whether Infura is blocking
requests; we can reuse `eth_getBlockByNumber`;
* All requests are awaited, which makes `lookupNetwork` run fully
in-band instead of partially out-of-band; and
* Both requests for `net_version` and `eth_getBlockByNumber` are
performed in parallel to make `lookupNetwork` run slightly faster.
2023-03-31 00:49:12 +02:00
|
|
|
|
|
|
|
keyringMemStore = new ObservableStore({ isUnlocked: false });
|
|
|
|
const networkControllerMessenger = new ControllerMessenger();
|
|
|
|
network = new NetworkController({
|
|
|
|
messenger: networkControllerMessenger,
|
|
|
|
infuraProjectId,
|
|
|
|
});
|
|
|
|
await network.initializeProvider(networkControllerProviderConfig);
|
|
|
|
provider = network.getProviderAndBlockTracker().provider;
|
|
|
|
|
|
|
|
const tokenListMessenger = new ControllerMessenger().getRestricted({
|
|
|
|
name: 'TokenListController',
|
|
|
|
});
|
|
|
|
tokenListController = new TokenListController({
|
2023-06-09 22:48:48 +02:00
|
|
|
chainId: toHex(1),
|
NetworkController: Split `network` into `networkId` and `networkStatus` (#17556)
The `network` store of the network controller crams two types of data
into one place. It roughly tracks whether we have enough information to
make requests to the network and whether the network is capable of
receiving requests, but it also stores the ID of the network (as
obtained via `net_version`).
Generally we shouldn't be using the network ID for anything, as it has
been completely replaced by chain ID, which all custom RPC endpoints
have been required to support for over a year now. However, as the
network ID is used in various places within the extension codebase,
removing it entirely would be a non-trivial effort. So, minimally, this
commit splits `network` into two stores: `networkId` and
`networkStatus`. But it also expands the concept of network status.
Previously, the network was in one of two states: "loading" and
"not-loading". But now it can be in one of four states:
- `available`: The network is able to receive and respond to requests.
- `unavailable`: The network is not able to receive and respond to
requests for unknown reasons.
- `blocked`: The network is actively blocking requests based on the
user's geolocation. (This is specific to Infura.)
- `unknown`: We don't know whether the network can receive and respond
to requests, either because we haven't checked or we tried to check
and were unsuccessful.
This commit also changes how the network status is determined —
specifically, how many requests are used to determine that status, when
they occur, and whether they are awaited. Previously, the network
controller would make 2 to 3 requests during the course of running
`lookupNetwork`.
* First, if it was an Infura network, it would make a request for
`eth_blockNumber` to determine whether Infura was blocking requests or
not, then emit an appropriate event. This operation was not awaited.
* Then, regardless of the network, it would fetch the network ID via
`net_version`. This operation was awaited.
* Finally, regardless of the network, it would fetch the latest block
via `eth_getBlockByNumber`, then use the result to determine whether
the network supported EIP-1559. This operation was awaited.
Now:
* One fewer request is made, specifically `eth_blockNumber`, as we don't
need to make an extra request to determine whether Infura is blocking
requests; we can reuse `eth_getBlockByNumber`;
* All requests are awaited, which makes `lookupNetwork` run fully
in-band instead of partially out-of-band; and
* Both requests for `net_version` and `eth_getBlockByNumber` are
performed in parallel to make `lookupNetwork` run slightly faster.
2023-03-31 00:49:12 +02:00
|
|
|
preventPollingOnNetworkRestart: false,
|
|
|
|
onNetworkStateChange: sinon.spy(),
|
|
|
|
onPreferencesStateChange: sinon.spy(),
|
|
|
|
messenger: tokenListMessenger,
|
|
|
|
});
|
|
|
|
await tokenListController.start();
|
|
|
|
|
|
|
|
preferences = new PreferencesController({
|
|
|
|
network,
|
|
|
|
provider,
|
|
|
|
tokenListController,
|
|
|
|
onInfuraIsBlocked: sinon.stub(),
|
|
|
|
onInfuraIsUnblocked: sinon.stub(),
|
|
|
|
});
|
|
|
|
preferences.setAddresses([
|
|
|
|
'0x7e57e2',
|
|
|
|
'0xbc86727e770de68b1060c91f6bb6945c73e10388',
|
|
|
|
]);
|
|
|
|
preferences.setUseTokenDetection(true);
|
|
|
|
|
|
|
|
tokensController = new TokensController({
|
|
|
|
config: { provider },
|
|
|
|
onPreferencesStateChange: preferences.store.subscribe.bind(
|
|
|
|
preferences.store,
|
|
|
|
),
|
2023-06-22 20:46:09 +02:00
|
|
|
onNetworkStateChange: networkControllerMessenger.subscribe.bind(
|
|
|
|
networkControllerMessenger,
|
|
|
|
'NetworkController:stateChange',
|
|
|
|
),
|
NetworkController: Split `network` into `networkId` and `networkStatus` (#17556)
The `network` store of the network controller crams two types of data
into one place. It roughly tracks whether we have enough information to
make requests to the network and whether the network is capable of
receiving requests, but it also stores the ID of the network (as
obtained via `net_version`).
Generally we shouldn't be using the network ID for anything, as it has
been completely replaced by chain ID, which all custom RPC endpoints
have been required to support for over a year now. However, as the
network ID is used in various places within the extension codebase,
removing it entirely would be a non-trivial effort. So, minimally, this
commit splits `network` into two stores: `networkId` and
`networkStatus`. But it also expands the concept of network status.
Previously, the network was in one of two states: "loading" and
"not-loading". But now it can be in one of four states:
- `available`: The network is able to receive and respond to requests.
- `unavailable`: The network is not able to receive and respond to
requests for unknown reasons.
- `blocked`: The network is actively blocking requests based on the
user's geolocation. (This is specific to Infura.)
- `unknown`: We don't know whether the network can receive and respond
to requests, either because we haven't checked or we tried to check
and were unsuccessful.
This commit also changes how the network status is determined —
specifically, how many requests are used to determine that status, when
they occur, and whether they are awaited. Previously, the network
controller would make 2 to 3 requests during the course of running
`lookupNetwork`.
* First, if it was an Infura network, it would make a request for
`eth_blockNumber` to determine whether Infura was blocking requests or
not, then emit an appropriate event. This operation was not awaited.
* Then, regardless of the network, it would fetch the network ID via
`net_version`. This operation was awaited.
* Finally, regardless of the network, it would fetch the latest block
via `eth_getBlockByNumber`, then use the result to determine whether
the network supported EIP-1559. This operation was awaited.
Now:
* One fewer request is made, specifically `eth_blockNumber`, as we don't
need to make an extra request to determine whether Infura is blocking
requests; we can reuse `eth_getBlockByNumber`;
* All requests are awaited, which makes `lookupNetwork` run fully
in-band instead of partially out-of-band; and
* Both requests for `net_version` and `eth_getBlockByNumber` are
performed in parallel to make `lookupNetwork` run slightly faster.
2023-03-31 00:49:12 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
assetsContractController = new AssetsContractController({
|
|
|
|
onPreferencesStateChange: preferences.store.subscribe.bind(
|
|
|
|
preferences.store,
|
|
|
|
),
|
2023-06-22 20:46:09 +02:00
|
|
|
onNetworkStateChange: networkControllerMessenger.subscribe.bind(
|
|
|
|
networkControllerMessenger,
|
|
|
|
'NetworkController:stateChange',
|
|
|
|
),
|
NetworkController: Split `network` into `networkId` and `networkStatus` (#17556)
The `network` store of the network controller crams two types of data
into one place. It roughly tracks whether we have enough information to
make requests to the network and whether the network is capable of
receiving requests, but it also stores the ID of the network (as
obtained via `net_version`).
Generally we shouldn't be using the network ID for anything, as it has
been completely replaced by chain ID, which all custom RPC endpoints
have been required to support for over a year now. However, as the
network ID is used in various places within the extension codebase,
removing it entirely would be a non-trivial effort. So, minimally, this
commit splits `network` into two stores: `networkId` and
`networkStatus`. But it also expands the concept of network status.
Previously, the network was in one of two states: "loading" and
"not-loading". But now it can be in one of four states:
- `available`: The network is able to receive and respond to requests.
- `unavailable`: The network is not able to receive and respond to
requests for unknown reasons.
- `blocked`: The network is actively blocking requests based on the
user's geolocation. (This is specific to Infura.)
- `unknown`: We don't know whether the network can receive and respond
to requests, either because we haven't checked or we tried to check
and were unsuccessful.
This commit also changes how the network status is determined —
specifically, how many requests are used to determine that status, when
they occur, and whether they are awaited. Previously, the network
controller would make 2 to 3 requests during the course of running
`lookupNetwork`.
* First, if it was an Infura network, it would make a request for
`eth_blockNumber` to determine whether Infura was blocking requests or
not, then emit an appropriate event. This operation was not awaited.
* Then, regardless of the network, it would fetch the network ID via
`net_version`. This operation was awaited.
* Finally, regardless of the network, it would fetch the latest block
via `eth_getBlockByNumber`, then use the result to determine whether
the network supported EIP-1559. This operation was awaited.
Now:
* One fewer request is made, specifically `eth_blockNumber`, as we don't
need to make an extra request to determine whether Infura is blocking
requests; we can reuse `eth_getBlockByNumber`;
* All requests are awaited, which makes `lookupNetwork` run fully
in-band instead of partially out-of-band; and
* Both requests for `net_version` and `eth_getBlockByNumber` are
performed in parallel to make `lookupNetwork` run slightly faster.
2023-03-31 00:49:12 +02:00
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-07-30 15:26:34 +02:00
|
|
|
|
NetworkController: Split `network` into `networkId` and `networkStatus` (#17556)
The `network` store of the network controller crams two types of data
into one place. It roughly tracks whether we have enough information to
make requests to the network and whether the network is capable of
receiving requests, but it also stores the ID of the network (as
obtained via `net_version`).
Generally we shouldn't be using the network ID for anything, as it has
been completely replaced by chain ID, which all custom RPC endpoints
have been required to support for over a year now. However, as the
network ID is used in various places within the extension codebase,
removing it entirely would be a non-trivial effort. So, minimally, this
commit splits `network` into two stores: `networkId` and
`networkStatus`. But it also expands the concept of network status.
Previously, the network was in one of two states: "loading" and
"not-loading". But now it can be in one of four states:
- `available`: The network is able to receive and respond to requests.
- `unavailable`: The network is not able to receive and respond to
requests for unknown reasons.
- `blocked`: The network is actively blocking requests based on the
user's geolocation. (This is specific to Infura.)
- `unknown`: We don't know whether the network can receive and respond
to requests, either because we haven't checked or we tried to check
and were unsuccessful.
This commit also changes how the network status is determined —
specifically, how many requests are used to determine that status, when
they occur, and whether they are awaited. Previously, the network
controller would make 2 to 3 requests during the course of running
`lookupNetwork`.
* First, if it was an Infura network, it would make a request for
`eth_blockNumber` to determine whether Infura was blocking requests or
not, then emit an appropriate event. This operation was not awaited.
* Then, regardless of the network, it would fetch the network ID via
`net_version`. This operation was awaited.
* Finally, regardless of the network, it would fetch the latest block
via `eth_getBlockByNumber`, then use the result to determine whether
the network supported EIP-1559. This operation was awaited.
Now:
* One fewer request is made, specifically `eth_blockNumber`, as we don't
need to make an extra request to determine whether Infura is blocking
requests; we can reuse `eth_getBlockByNumber`;
* All requests are awaited, which makes `lookupNetwork` run fully
in-band instead of partially out-of-band; and
* Both requests for `net_version` and `eth_getBlockByNumber` are
performed in parallel to make `lookupNetwork` run slightly faster.
2023-03-31 00:49:12 +02:00
|
|
|
afterEach(function () {
|
|
|
|
nock.enableNetConnect('localhost');
|
2021-02-04 19:15:23 +01:00
|
|
|
sandbox.restore();
|
|
|
|
});
|
2018-07-11 21:59:05 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should poll on correct interval', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const stub = sinon.stub(global, 'setInterval');
|
2023-06-22 20:46:09 +02:00
|
|
|
new DetectTokensController({ messenger: buildMessenger(), interval: 1337 }); // eslint-disable-line no-new
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.strictEqual(stub.getCall(0).args[1], 1337);
|
|
|
|
stub.restore();
|
|
|
|
});
|
2018-06-28 04:18:06 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should be called on every polling period', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const clock = sandbox.useFakeTimers();
|
2023-04-17 20:15:01 +02:00
|
|
|
await network.setProviderType(NETWORK_TYPES.MAINNET);
|
2020-11-03 00:41:28 +01:00
|
|
|
const controller = new DetectTokensController({
|
2023-06-22 20:46:09 +02:00
|
|
|
messenger: buildMessenger(),
|
2020-11-03 00:41:28 +01:00
|
|
|
preferences,
|
|
|
|
network,
|
|
|
|
keyringMemStore,
|
2021-09-09 22:56:27 +02:00
|
|
|
tokenList: tokenListController,
|
2021-09-10 19:37:19 +02:00
|
|
|
tokensController,
|
2022-08-10 03:26:25 +02:00
|
|
|
assetsContractController,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
controller.isOpen = true;
|
|
|
|
controller.isUnlocked = true;
|
|
|
|
|
|
|
|
const stub = sandbox.stub(controller, 'detectNewTokens');
|
|
|
|
|
|
|
|
clock.tick(1);
|
|
|
|
sandbox.assert.notCalled(stub);
|
|
|
|
clock.tick(180000);
|
|
|
|
sandbox.assert.called(stub);
|
|
|
|
clock.tick(180000);
|
|
|
|
sandbox.assert.calledTwice(stub);
|
|
|
|
clock.tick(180000);
|
|
|
|
sandbox.assert.calledThrice(stub);
|
|
|
|
});
|
2018-07-11 21:59:05 +02:00
|
|
|
|
2022-08-10 03:26:25 +02:00
|
|
|
it('should not check and add tokens while on unsupported networks', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
sandbox.useFakeTimers();
|
2023-04-17 20:15:01 +02:00
|
|
|
await network.setProviderType(NETWORK_TYPES.SEPOLIA);
|
2022-09-29 05:26:01 +02:00
|
|
|
const tokenListMessengerSepolia = new ControllerMessenger().getRestricted({
|
2021-09-09 22:56:27 +02:00
|
|
|
name: 'TokenListController',
|
|
|
|
});
|
|
|
|
tokenListController = new TokenListController({
|
2023-06-09 22:48:48 +02:00
|
|
|
chainId: toHex(11155111),
|
2021-09-09 22:56:27 +02:00
|
|
|
onNetworkStateChange: sinon.spy(),
|
|
|
|
onPreferencesStateChange: sinon.spy(),
|
2022-09-29 05:26:01 +02:00
|
|
|
messenger: tokenListMessengerSepolia,
|
2021-09-09 22:56:27 +02:00
|
|
|
});
|
|
|
|
await tokenListController.start();
|
2020-11-03 00:41:28 +01:00
|
|
|
const controller = new DetectTokensController({
|
2023-06-22 20:46:09 +02:00
|
|
|
messenger: buildMessenger(),
|
2020-11-03 00:41:28 +01:00
|
|
|
preferences,
|
|
|
|
network,
|
|
|
|
keyringMemStore,
|
2021-09-09 22:56:27 +02:00
|
|
|
tokenList: tokenListController,
|
2021-09-10 19:37:19 +02:00
|
|
|
tokensController,
|
2022-08-10 03:26:25 +02:00
|
|
|
assetsContractController,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
controller.isOpen = true;
|
|
|
|
controller.isUnlocked = true;
|
2018-07-11 21:59:05 +02:00
|
|
|
|
2022-08-10 03:26:25 +02:00
|
|
|
const stub = sandbox.stub(
|
|
|
|
assetsContractController,
|
|
|
|
'getBalancesInSingleCall',
|
|
|
|
);
|
2018-07-11 21:59:05 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
await controller.detectNewTokens();
|
|
|
|
sandbox.assert.notCalled(stub);
|
|
|
|
});
|
2018-06-28 04:18:06 +02:00
|
|
|
|
2022-08-10 03:26:25 +02:00
|
|
|
it('should skip adding tokens listed in ignoredTokens array', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
sandbox.useFakeTimers();
|
2023-04-17 20:15:01 +02:00
|
|
|
await network.setProviderType(NETWORK_TYPES.MAINNET);
|
2020-12-08 21:38:00 +01:00
|
|
|
const controller = new DetectTokensController({
|
2023-06-22 20:46:09 +02:00
|
|
|
messenger: buildMessenger(),
|
2020-12-08 21:38:00 +01:00
|
|
|
preferences,
|
|
|
|
network,
|
|
|
|
keyringMemStore,
|
2021-09-09 22:56:27 +02:00
|
|
|
tokenList: tokenListController,
|
2021-09-10 19:37:19 +02:00
|
|
|
tokensController,
|
2022-08-10 03:26:25 +02:00
|
|
|
assetsContractController,
|
|
|
|
trackMetaMetricsEvent: noop,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
controller.isOpen = true;
|
|
|
|
controller.isUnlocked = true;
|
2020-12-08 21:38:00 +01:00
|
|
|
|
2021-09-09 22:56:27 +02:00
|
|
|
const { tokenList } = tokenListController.state;
|
2022-08-10 03:26:25 +02:00
|
|
|
const tokenValues = Object.values(tokenList);
|
2020-12-08 21:38:00 +01:00
|
|
|
|
2022-08-10 03:26:25 +02:00
|
|
|
await tokensController.addDetectedTokens([
|
|
|
|
{
|
|
|
|
address: tokenValues[0].address,
|
|
|
|
symbol: tokenValues[0].symbol,
|
|
|
|
decimals: tokenValues[0].decimals,
|
2022-09-15 17:23:18 +02:00
|
|
|
aggregators: undefined,
|
2022-08-10 03:26:25 +02:00
|
|
|
image: undefined,
|
|
|
|
isERC721: undefined,
|
|
|
|
},
|
|
|
|
]);
|
2020-12-08 21:38:00 +01:00
|
|
|
|
|
|
|
sandbox
|
2022-08-10 03:26:25 +02:00
|
|
|
.stub(assetsContractController, 'getBalancesInSingleCall')
|
2020-12-08 21:38:00 +01:00
|
|
|
.callsFake((tokensToDetect) =>
|
|
|
|
tokensToDetect.map((token) =>
|
2022-08-10 03:26:25 +02:00
|
|
|
token.address === tokenValues[1].address ? new BigNumber(10) : 0,
|
2020-12-08 21:38:00 +01:00
|
|
|
),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2022-08-10 03:26:25 +02:00
|
|
|
await tokensController.ignoreTokens([tokenValues[1].address]);
|
2020-12-08 21:38:00 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
await controller.detectNewTokens();
|
2022-08-10 03:26:25 +02:00
|
|
|
assert.deepEqual(tokensController.state.detectedTokens, [
|
2020-12-08 21:38:00 +01:00
|
|
|
{
|
2022-08-10 03:26:25 +02:00
|
|
|
address: toChecksumHexAddress(tokenValues[0].address),
|
|
|
|
decimals: tokenValues[0].decimals,
|
|
|
|
symbol: tokenValues[0].symbol,
|
2022-09-15 17:23:18 +02:00
|
|
|
aggregators: undefined,
|
2022-08-10 03:26:25 +02:00
|
|
|
image: undefined,
|
|
|
|
isERC721: undefined,
|
2020-12-08 21:38:00 +01:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
]);
|
|
|
|
});
|
2020-12-08 21:38:00 +01:00
|
|
|
|
2022-08-10 03:26:25 +02:00
|
|
|
it('should check and add tokens while on supported networks', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
sandbox.useFakeTimers();
|
2023-04-17 20:15:01 +02:00
|
|
|
await network.setProviderType(NETWORK_TYPES.MAINNET);
|
2020-11-03 00:41:28 +01:00
|
|
|
const controller = new DetectTokensController({
|
2023-06-22 20:46:09 +02:00
|
|
|
messenger: buildMessenger(),
|
2020-11-03 00:41:28 +01:00
|
|
|
preferences,
|
|
|
|
network,
|
|
|
|
keyringMemStore,
|
2021-09-09 22:56:27 +02:00
|
|
|
tokenList: tokenListController,
|
2021-09-10 19:37:19 +02:00
|
|
|
tokensController,
|
2022-08-10 03:26:25 +02:00
|
|
|
assetsContractController,
|
|
|
|
trackMetaMetricsEvent: noop,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
controller.isOpen = true;
|
|
|
|
controller.isUnlocked = true;
|
2018-07-11 21:59:05 +02:00
|
|
|
|
2021-09-09 22:56:27 +02:00
|
|
|
const { tokenList } = tokenListController.state;
|
|
|
|
const erc20ContractAddresses = Object.keys(tokenList);
|
2018-07-11 21:59:05 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const existingTokenAddress = erc20ContractAddresses[0];
|
2021-09-09 22:56:27 +02:00
|
|
|
const existingToken = tokenList[existingTokenAddress];
|
2021-01-08 15:25:46 +01:00
|
|
|
|
2022-08-10 03:26:25 +02:00
|
|
|
await tokensController.addDetectedTokens([
|
2021-01-08 15:25:46 +01:00
|
|
|
{
|
2022-08-10 03:26:25 +02:00
|
|
|
address: existingToken.address,
|
2021-01-08 15:25:46 +01:00
|
|
|
symbol: existingToken.symbol,
|
2022-08-10 03:26:25 +02:00
|
|
|
decimals: existingToken.decimals,
|
2022-09-15 17:23:18 +02:00
|
|
|
aggregators: undefined,
|
2022-08-10 03:26:25 +02:00
|
|
|
image: undefined,
|
|
|
|
isERC721: undefined,
|
2021-01-08 15:25:46 +01:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
]);
|
|
|
|
const tokenAddressToAdd = erc20ContractAddresses[1];
|
2021-09-09 22:56:27 +02:00
|
|
|
const tokenToAdd = tokenList[tokenAddressToAdd];
|
2020-11-03 00:41:28 +01:00
|
|
|
sandbox
|
2022-08-10 03:26:25 +02:00
|
|
|
.stub(assetsContractController, 'getBalancesInSingleCall')
|
|
|
|
.callsFake(() =>
|
|
|
|
Promise.resolve({ [tokenAddressToAdd]: new BigNumber(10) }),
|
|
|
|
);
|
2021-02-04 19:15:23 +01:00
|
|
|
await controller.detectNewTokens();
|
2022-08-10 03:26:25 +02:00
|
|
|
assert.deepEqual(tokensController.state.detectedTokens, [
|
2020-11-03 00:41:28 +01:00
|
|
|
{
|
2021-09-10 19:37:19 +02:00
|
|
|
address: toChecksumHexAddress(existingTokenAddress),
|
2020-11-03 00:41:28 +01:00
|
|
|
decimals: existingToken.decimals,
|
|
|
|
symbol: existingToken.symbol,
|
2022-09-15 17:23:18 +02:00
|
|
|
aggregators: undefined,
|
2022-08-10 03:26:25 +02:00
|
|
|
image: undefined,
|
|
|
|
isERC721: undefined,
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
|
|
|
{
|
2021-09-10 19:37:19 +02:00
|
|
|
address: toChecksumHexAddress(tokenAddressToAdd),
|
2020-11-03 00:41:28 +01:00
|
|
|
decimals: tokenToAdd.decimals,
|
|
|
|
symbol: tokenToAdd.symbol,
|
2022-09-15 17:23:18 +02:00
|
|
|
aggregators: undefined,
|
2022-08-10 03:26:25 +02:00
|
|
|
image: undefined,
|
|
|
|
isERC721: undefined,
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
]);
|
|
|
|
});
|
2018-07-20 01:46:46 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should trigger detect new tokens when change address', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
sandbox.useFakeTimers();
|
2020-11-03 00:41:28 +01:00
|
|
|
const controller = new DetectTokensController({
|
2023-06-22 20:46:09 +02:00
|
|
|
messenger: buildMessenger(),
|
2020-11-03 00:41:28 +01:00
|
|
|
preferences,
|
|
|
|
network,
|
|
|
|
keyringMemStore,
|
2021-09-09 22:56:27 +02:00
|
|
|
tokenList: tokenListController,
|
2021-09-10 19:37:19 +02:00
|
|
|
tokensController,
|
2022-08-10 03:26:25 +02:00
|
|
|
assetsContractController,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
controller.isOpen = true;
|
|
|
|
controller.isUnlocked = true;
|
|
|
|
const stub = sandbox.stub(controller, 'detectNewTokens');
|
2020-11-03 00:41:28 +01:00
|
|
|
await preferences.setSelectedAddress(
|
|
|
|
'0xbc86727e770de68b1060c91f6bb6945c73e10388',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
sandbox.assert.called(stub);
|
|
|
|
});
|
2018-07-20 01:46:46 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should trigger detect new tokens when submit password', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
sandbox.useFakeTimers();
|
2020-11-03 00:41:28 +01:00
|
|
|
const controller = new DetectTokensController({
|
2023-06-22 20:46:09 +02:00
|
|
|
messenger: buildMessenger(),
|
2020-11-03 00:41:28 +01:00
|
|
|
preferences,
|
|
|
|
network,
|
|
|
|
keyringMemStore,
|
2021-09-09 22:56:27 +02:00
|
|
|
tokenList: tokenListController,
|
2021-09-10 19:37:19 +02:00
|
|
|
tokensController,
|
2022-08-10 03:26:25 +02:00
|
|
|
assetsContractController,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
controller.isOpen = true;
|
|
|
|
controller.selectedAddress = '0x0';
|
|
|
|
const stub = sandbox.stub(controller, 'detectNewTokens');
|
|
|
|
await controller._keyringMemStore.updateState({ isUnlocked: true });
|
|
|
|
sandbox.assert.called(stub);
|
|
|
|
});
|
2018-07-21 01:58:03 +02:00
|
|
|
|
2020-04-13 22:14:42 +02:00
|
|
|
it('should not trigger detect new tokens when not unlocked', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const clock = sandbox.useFakeTimers();
|
2023-04-17 20:15:01 +02:00
|
|
|
await network.setProviderType(NETWORK_TYPES.MAINNET);
|
2020-11-03 00:41:28 +01:00
|
|
|
const controller = new DetectTokensController({
|
2023-06-22 20:46:09 +02:00
|
|
|
messenger: buildMessenger(),
|
2020-11-03 00:41:28 +01:00
|
|
|
preferences,
|
|
|
|
network,
|
|
|
|
keyringMemStore,
|
2021-09-09 22:56:27 +02:00
|
|
|
tokenList: tokenListController,
|
2021-09-10 19:37:19 +02:00
|
|
|
tokensController,
|
2022-08-10 03:26:25 +02:00
|
|
|
assetsContractController,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
controller.isOpen = true;
|
|
|
|
controller.isUnlocked = false;
|
2022-08-10 03:26:25 +02:00
|
|
|
const stub = sandbox.stub(
|
|
|
|
assetsContractController,
|
|
|
|
'getBalancesInSingleCall',
|
|
|
|
);
|
2021-02-04 19:15:23 +01:00
|
|
|
clock.tick(180000);
|
|
|
|
sandbox.assert.notCalled(stub);
|
|
|
|
});
|
2020-04-13 22:14:42 +02:00
|
|
|
|
|
|
|
it('should not trigger detect new tokens when not open', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const clock = sandbox.useFakeTimers();
|
2023-04-17 20:15:01 +02:00
|
|
|
await network.setProviderType(NETWORK_TYPES.MAINNET);
|
2020-11-03 00:41:28 +01:00
|
|
|
const controller = new DetectTokensController({
|
2023-06-22 20:46:09 +02:00
|
|
|
messenger: buildMessenger(),
|
2020-11-03 00:41:28 +01:00
|
|
|
preferences,
|
|
|
|
network,
|
|
|
|
keyringMemStore,
|
2021-09-10 19:37:19 +02:00
|
|
|
tokensController,
|
2022-08-10 03:26:25 +02:00
|
|
|
assetsContractController,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-04-13 22:14:42 +02:00
|
|
|
// trigger state update from preferences controller
|
2020-11-03 00:41:28 +01:00
|
|
|
await preferences.setSelectedAddress(
|
|
|
|
'0xbc86727e770de68b1060c91f6bb6945c73e10388',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
controller.isOpen = false;
|
|
|
|
controller.isUnlocked = true;
|
2022-08-10 03:26:25 +02:00
|
|
|
const stub = sandbox.stub(
|
|
|
|
assetsContractController,
|
|
|
|
'getBalancesInSingleCall',
|
|
|
|
);
|
2021-02-04 19:15:23 +01:00
|
|
|
clock.tick(180000);
|
|
|
|
sandbox.assert.notCalled(stub);
|
|
|
|
});
|
|
|
|
});
|