Skip to content

Commit

Permalink
Merge branch 'main' into bump-all-packages-to-node-18
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmire authored Feb 9, 2024
2 parents 65304db + 8282d9c commit 9933f8e
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 74 deletions.
8 changes: 4 additions & 4 deletions packages/assets-controllers/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ module.exports = merge(baseConfig, {
// An object that configures minimum threshold enforcement for coverage results
coverageThreshold: {
global: {
branches: 88.22,
functions: 95.32,
lines: 96.68,
statements: 96.68,
branches: 88.47,
functions: 95.89,
lines: 96.87,
statements: 96.87,
},
},

Expand Down
37 changes: 37 additions & 0 deletions packages/assets-controllers/src/AssetsContractController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,43 @@ describe('AssetsContractController', () => {
messenger.clearEventSubscriptions('NetworkController:networkDidChange');
});

it('should throw when ERC1155 function transferSingle is not defined', async () => {
const { assetsContract, messenger, provider, networkClientConfiguration } =
await setupAssetContractControllers();
assetsContract.configure({ provider });
mockNetworkWithDefaultChainId({
networkClientConfiguration,
mocks: [
{
request: {
method: 'eth_call',
params: [
{
to: ERC1155_ADDRESS,
data: '0x00fdd58e0000000000000000000000005a3ca5cd63807ce5e4d7841ab32ce6b6d9bbba2d5a3ca5cd63807ce5e4d7841ab32ce6b6d9bbba2d000000000000010000000001',
},
'latest',
],
},
response: {
result:
'0x0000000000000000000000000000000000000000000000000000000000000001',
},
},
],
});
await expect(
assetsContract.transferSingleERC1155(
ERC1155_ADDRESS,
'0x0',
TEST_ACCOUNT_PUBLIC_ADDRESS,
ERC1155_ID,
'1',
),
).rejects.toThrow('contract.transferSingle is not a function');
messenger.clearEventSubscriptions('NetworkController:networkDidChange');
});

it('should get the balance of a ERC-1155 NFT for a given address', async () => {
const { assetsContract, messenger, provider, networkClientConfiguration } =
await setupAssetContractControllers();
Expand Down
14 changes: 5 additions & 9 deletions packages/assets-controllers/src/AssetsContractController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
NetworkClientId,
NetworkState,
NetworkController,
Provider,
} from '@metamask/network-controller';
import type { PreferencesState } from '@metamask/preferences-controller';
import type { Hex } from '@metamask/utils';
Expand Down Expand Up @@ -62,9 +63,7 @@ export const MISSING_PROVIDER_ERROR =
// Convert to a `type` in a future major version.
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export interface AssetsContractConfig extends BaseConfig {
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
provider: any;
provider: Provider | undefined;
ipfsGateway: string;
chainId: Hex;
}
Expand All @@ -89,9 +88,7 @@ export class AssetsContractController extends BaseControllerV1<
AssetsContractConfig,
BaseState
> {
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private _provider?: any;
private _provider?: Provider;

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

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

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

Expand Down
28 changes: 13 additions & 15 deletions packages/assets-controllers/src/NftController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -903,17 +903,14 @@ export class NftController extends BaseControllerV1<NftConfig, NftState> {
// If the nft is auto-detected we want some valid metadata to be present
if (
source === Source.Detected &&
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Object.entries(contractInformation).every(([k, v]: [string, any]) => {
if (k === 'address') {
return true; // address will always be present
}
// collection will always be an object, we need to check the internal values
if (k === 'collection') {
return v?.name === null && v?.image_url === null;
}
return !v;
'address' in contractInformation &&
typeof contractInformation.address === 'string' &&
'collection' in contractInformation &&
contractInformation.collection.name === null &&
'image_url' in contractInformation.collection &&
contractInformation.collection.image_url === null &&
Object.entries(contractInformation).every(([key, value]) => {
return key === 'address' || key === 'collection' || !value;
})
) {
return nftContracts;
Expand Down Expand Up @@ -1239,11 +1236,12 @@ export class NftController extends BaseControllerV1<NftConfig, NftState> {
'Suggested NFT is not owned by the selected account',
);
}
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
} catch (error) {
// 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."
throw rpcErrors.resourceUnavailable(error.message);
if (error instanceof Error) {
throw rpcErrors.resourceUnavailable(error.message);
}
throw error;
}
}

Expand Down
18 changes: 10 additions & 8 deletions packages/assets-controllers/src/Standards/ERC20Standard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ export class ERC20Standard {
try {
const decimals = await contract.decimals();
return decimals.toString();
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (err: any) {
} catch (err) {
// Mirror previous implementation
if (err.message.includes('call revert exception')) {
if (
err instanceof Error &&
err.message.includes('call revert exception')
) {
throw new Error('Failed to parse token decimals');
}
throw err;
Expand All @@ -62,11 +63,12 @@ export class ERC20Standard {
try {
const name = await contract.name();
return name.toString();
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (err: any) {
} catch (err) {
// Mirror previous implementation
if (err.message.includes('call revert exception')) {
if (
err instanceof Error &&
err.message.includes('call revert exception')
) {
throw new Error('Failed to parse token name');
}
throw err;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,12 @@ export class ERC721Standard {
const contract = new Contract(address, abiERC721, this.provider);
try {
return await contract.supportsInterface(interfaceId);
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (err: any) {
} catch (err) {
// Mirror previous implementation
if (err.message.includes('call revert exception')) {
if (
err instanceof Error &&
err.message.includes('call revert exception')
) {
return false;
}
throw err;
Expand Down
24 changes: 0 additions & 24 deletions packages/assets-controllers/src/Standards/standards-types.ts

This file was deleted.

8 changes: 4 additions & 4 deletions packages/assets-controllers/src/TokensController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ describe('TokensController', () => {
messenger.publish('PreferencesController:stateChange', state, []);
};

const fakeProvider = new FakeProvider();

beforeEach(async () => {
const defaultSelectedAddress = '0x1';
messenger = new ControllerMessenger();
Expand Down Expand Up @@ -139,7 +141,7 @@ describe('TokensController', () => {
chainId: ChainId.mainnet,
config: {
selectedAddress: defaultSelectedAddress,
provider: sinon.stub(),
provider: fakeProvider,
},
messenger: tokensControllerMessenger,
});
Expand Down Expand Up @@ -1565,9 +1567,7 @@ describe('TokensController', () => {
expect(networkClientId).toBe('networkClientId1');
return {
configuration: { chainId: '0x5' },
provider: new FakeProvider({
stubs: [],
}),
provider: fakeProvider,
blockTracker: new FakeBlockTracker(),
destroy: jest.fn(),
} as unknown as ReturnType<NetworkController['getNetworkClientById']>;
Expand Down
10 changes: 4 additions & 6 deletions packages/assets-controllers/src/TokensController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type {
NetworkClientId,
NetworkControllerGetNetworkClientByIdAction,
NetworkControllerNetworkDidChangeEvent,
Provider,
} from '@metamask/network-controller';
import type { PreferencesControllerStateChangeEvent } from '@metamask/preferences-controller';
import { rpcErrors } from '@metamask/rpc-errors';
Expand Down Expand Up @@ -60,9 +61,7 @@ import type { Token } from './TokenRatesController';
export interface TokensConfig extends BaseConfig {
selectedAddress: string;
chainId: Hex;
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
provider: any;
provider: Provider | undefined;
}

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

_getProvider(networkClientId?: NetworkClientId): Web3Provider {
return new Web3Provider(
// @ts-expect-error TODO: remove this annotation once the `Eip1193Provider` class is released
networkClientId
? this.messagingSystem.call(
'NetworkController:getNetworkClientById',
Expand Down

0 comments on commit 9933f8e

Please sign in to comment.