Skip to content

Commit 8282d9c

Browse files
authored
any type in asset-controller classes have been fixed with a proper types (#3818)
## Explanation <!-- Thanks for your contribution! Take a moment to answer these questions so that reviewers have the information they need to properly understand your changes: * What is the current state of things and why does it need to change? * What is the solution your changes offer and how does it work? * Are there any changes whose purpose might not obvious to those unfamiliar with the domain? * If your primary goal was to update one package but you found you had to update another one along the way, why did you do so? * If you had to upgrade a dependency, why did you do so? --> Replace use of `any` with proper types (non-test files only) in asset-controller. ## References <!-- Are there any issues that this pull request is tied to? Are there other links that reviewers should consult to understand these changes better? For example: * Fixes #12345 * Related to #67890 --> * Fixes #3714
1 parent fd07735 commit 8282d9c

9 files changed

+82
-74
lines changed

packages/assets-controllers/jest.config.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ module.exports = merge(baseConfig, {
1717
// An object that configures minimum threshold enforcement for coverage results
1818
coverageThreshold: {
1919
global: {
20-
branches: 88.22,
21-
functions: 95.32,
22-
lines: 96.68,
23-
statements: 96.68,
20+
branches: 88.47,
21+
functions: 95.89,
22+
lines: 96.87,
23+
statements: 96.87,
2424
},
2525
},
2626

packages/assets-controllers/src/AssetsContractController.test.ts

+37
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,43 @@ describe('AssetsContractController', () => {
942942
messenger.clearEventSubscriptions('NetworkController:networkDidChange');
943943
});
944944

945+
it('should throw when ERC1155 function transferSingle is not defined', async () => {
946+
const { assetsContract, messenger, provider, networkClientConfiguration } =
947+
await setupAssetContractControllers();
948+
assetsContract.configure({ provider });
949+
mockNetworkWithDefaultChainId({
950+
networkClientConfiguration,
951+
mocks: [
952+
{
953+
request: {
954+
method: 'eth_call',
955+
params: [
956+
{
957+
to: ERC1155_ADDRESS,
958+
data: '0x00fdd58e0000000000000000000000005a3ca5cd63807ce5e4d7841ab32ce6b6d9bbba2d5a3ca5cd63807ce5e4d7841ab32ce6b6d9bbba2d000000000000010000000001',
959+
},
960+
'latest',
961+
],
962+
},
963+
response: {
964+
result:
965+
'0x0000000000000000000000000000000000000000000000000000000000000001',
966+
},
967+
},
968+
],
969+
});
970+
await expect(
971+
assetsContract.transferSingleERC1155(
972+
ERC1155_ADDRESS,
973+
'0x0',
974+
TEST_ACCOUNT_PUBLIC_ADDRESS,
975+
ERC1155_ID,
976+
'1',
977+
),
978+
).rejects.toThrow('contract.transferSingle is not a function');
979+
messenger.clearEventSubscriptions('NetworkController:networkDidChange');
980+
});
981+
945982
it('should get the balance of a ERC-1155 NFT for a given address', async () => {
946983
const { assetsContract, messenger, provider, networkClientConfiguration } =
947984
await setupAssetContractControllers();

packages/assets-controllers/src/AssetsContractController.ts

+5-9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
NetworkClientId,
88
NetworkState,
99
NetworkController,
10+
Provider,
1011
} from '@metamask/network-controller';
1112
import type { PreferencesState } from '@metamask/preferences-controller';
1213
import type { Hex } from '@metamask/utils';
@@ -62,9 +63,7 @@ export const MISSING_PROVIDER_ERROR =
6263
// Convert to a `type` in a future major version.
6364
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
6465
export interface AssetsContractConfig extends BaseConfig {
65-
// TODO: Replace `any` with type
66-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
67-
provider: any;
66+
provider: Provider | undefined;
6867
ipfsGateway: string;
6968
chainId: Hex;
7069
}
@@ -89,9 +88,7 @@ export class AssetsContractController extends BaseControllerV1<
8988
AssetsContractConfig,
9089
BaseState
9190
> {
92-
// TODO: Replace `any` with type
93-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
94-
private _provider?: any;
91+
private _provider?: Provider;
9592

9693
/**
9794
* Name of this controller used during composition
@@ -159,9 +156,7 @@ export class AssetsContractController extends BaseControllerV1<
159156
*
160157
* @property provider - Provider used to create a new underlying Web3 instance
161158
*/
162-
// TODO: Replace `any` with type
163-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
164-
set provider(provider: any) {
159+
set provider(provider: Provider) {
165160
this._provider = provider;
166161
}
167162

@@ -184,6 +179,7 @@ export class AssetsContractController extends BaseControllerV1<
184179
throw new Error(MISSING_PROVIDER_ERROR);
185180
}
186181

182+
// @ts-expect-error TODO: remove this annotation once the `Eip1193Provider` class is released
187183
return new Web3Provider(provider);
188184
}
189185

packages/assets-controllers/src/NftController.ts

+13-15
Original file line numberDiff line numberDiff line change
@@ -903,17 +903,14 @@ export class NftController extends BaseControllerV1<NftConfig, NftState> {
903903
// If the nft is auto-detected we want some valid metadata to be present
904904
if (
905905
source === Source.Detected &&
906-
// TODO: Replace `any` with type
907-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
908-
Object.entries(contractInformation).every(([k, v]: [string, any]) => {
909-
if (k === 'address') {
910-
return true; // address will always be present
911-
}
912-
// collection will always be an object, we need to check the internal values
913-
if (k === 'collection') {
914-
return v?.name === null && v?.image_url === null;
915-
}
916-
return !v;
906+
'address' in contractInformation &&
907+
typeof contractInformation.address === 'string' &&
908+
'collection' in contractInformation &&
909+
contractInformation.collection.name === null &&
910+
'image_url' in contractInformation.collection &&
911+
contractInformation.collection.image_url === null &&
912+
Object.entries(contractInformation).every(([key, value]) => {
913+
return key === 'address' || key === 'collection' || !value;
917914
})
918915
) {
919916
return nftContracts;
@@ -1239,11 +1236,12 @@ export class NftController extends BaseControllerV1<NftConfig, NftState> {
12391236
'Suggested NFT is not owned by the selected account',
12401237
);
12411238
}
1242-
// TODO: Replace `any` with type
1243-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1244-
} catch (error: any) {
1239+
} catch (error) {
12451240
// error thrown here: "Unable to verify ownership. Possibly because the standard is not supported or the user's currently selected network does not match the chain of the asset in question."
1246-
throw rpcErrors.resourceUnavailable(error.message);
1241+
if (error instanceof Error) {
1242+
throw rpcErrors.resourceUnavailable(error.message);
1243+
}
1244+
throw error;
12471245
}
12481246
}
12491247

packages/assets-controllers/src/Standards/ERC20Standard.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ export class ERC20Standard {
4040
try {
4141
const decimals = await contract.decimals();
4242
return decimals.toString();
43-
// TODO: Replace `any` with type
44-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
45-
} catch (err: any) {
43+
} catch (err) {
4644
// Mirror previous implementation
47-
if (err.message.includes('call revert exception')) {
45+
if (
46+
err instanceof Error &&
47+
err.message.includes('call revert exception')
48+
) {
4849
throw new Error('Failed to parse token decimals');
4950
}
5051
throw err;
@@ -62,11 +63,12 @@ export class ERC20Standard {
6263
try {
6364
const name = await contract.name();
6465
return name.toString();
65-
// TODO: Replace `any` with type
66-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
67-
} catch (err: any) {
66+
} catch (err) {
6867
// Mirror previous implementation
69-
if (err.message.includes('call revert exception')) {
68+
if (
69+
err instanceof Error &&
70+
err.message.includes('call revert exception')
71+
) {
7072
throw new Error('Failed to parse token name');
7173
}
7274
throw err;

packages/assets-controllers/src/Standards/NftStandards/ERC721/ERC721Standard.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,12 @@ export class ERC721Standard {
144144
const contract = new Contract(address, abiERC721, this.provider);
145145
try {
146146
return await contract.supportsInterface(interfaceId);
147-
// TODO: Replace `any` with type
148-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
149-
} catch (err: any) {
147+
} catch (err) {
150148
// Mirror previous implementation
151-
if (err.message.includes('call revert exception')) {
149+
if (
150+
err instanceof Error &&
151+
err.message.includes('call revert exception')
152+
) {
152153
return false;
153154
}
154155
throw err;

packages/assets-controllers/src/Standards/standards-types.ts

-24
This file was deleted.

packages/assets-controllers/src/TokensController.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ describe('TokensController', () => {
111111
messenger.publish('PreferencesController:stateChange', state, []);
112112
};
113113

114+
const fakeProvider = new FakeProvider();
115+
114116
beforeEach(async () => {
115117
const defaultSelectedAddress = '0x1';
116118
messenger = new ControllerMessenger();
@@ -139,7 +141,7 @@ describe('TokensController', () => {
139141
chainId: ChainId.mainnet,
140142
config: {
141143
selectedAddress: defaultSelectedAddress,
142-
provider: sinon.stub(),
144+
provider: fakeProvider,
143145
},
144146
messenger: tokensControllerMessenger,
145147
});
@@ -1565,9 +1567,7 @@ describe('TokensController', () => {
15651567
expect(networkClientId).toBe('networkClientId1');
15661568
return {
15671569
configuration: { chainId: '0x5' },
1568-
provider: new FakeProvider({
1569-
stubs: [],
1570-
}),
1570+
provider: fakeProvider,
15711571
blockTracker: new FakeBlockTracker(),
15721572
destroy: jest.fn(),
15731573
} as unknown as ReturnType<NetworkController['getNetworkClientById']>;

packages/assets-controllers/src/TokensController.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import type {
2626
NetworkClientId,
2727
NetworkControllerGetNetworkClientByIdAction,
2828
NetworkControllerNetworkDidChangeEvent,
29+
Provider,
2930
} from '@metamask/network-controller';
3031
import type { PreferencesControllerStateChangeEvent } from '@metamask/preferences-controller';
3132
import { rpcErrors } from '@metamask/rpc-errors';
@@ -60,9 +61,7 @@ import type { Token } from './TokenRatesController';
6061
export interface TokensConfig extends BaseConfig {
6162
selectedAddress: string;
6263
chainId: Hex;
63-
// TODO: Replace `any` with type
64-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
65-
provider: any;
64+
provider: Provider | undefined;
6665
}
6766

6867
/**
@@ -705,9 +704,7 @@ export class TokensController extends BaseControllerV1<
705704
);
706705
try {
707706
return await tokenContract.supportsInterface(ERC721_INTERFACE_ID);
708-
// TODO: Replace `any` with type
709-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
710-
} catch (error: any) {
707+
} catch (error) {
711708
// currently we see a variety of errors across different networks when
712709
// token contracts are not ERC721 compatible. We need to figure out a better
713710
// way of differentiating token interface types but for now if we get an error
@@ -718,6 +715,7 @@ export class TokensController extends BaseControllerV1<
718715

719716
_getProvider(networkClientId?: NetworkClientId): Web3Provider {
720717
return new Web3Provider(
718+
// @ts-expect-error TODO: remove this annotation once the `Eip1193Provider` class is released
721719
networkClientId
722720
? this.messagingSystem.call(
723721
'NetworkController:getNetworkClientById',

0 commit comments

Comments
 (0)