MetaMask / MetaMask/core

`Contract` class `balances` method calls are typed as implicit `any`

Open
#4,464 2 comments 0 reactions 0 assignees View on GitHub
bug team-assets
Dominant language
TypeScript
Stars
413
Forks
308
Avg merge
1d 4h
Merged PRs (30d)
253

Description

In the following, `contract.balances` and `result` are implicitly typed as `any`, because the `Contract` class doesn't have a `balances` method.

```ts
import { Contract } from '@ethersproject/contracts';
...
async getBalancesInSingleCall(
...
) {
...
const contract = new Contract(
contractAddress,
abiSingleCallBalancesContract,
provider,
);
const result = await contract.balances([selectedAddress], tokensToDetect);
const nonZeroBalances: BalanceMap = {};
/* istanbul ignore else */
if (result.length > 0) {
tokensToDetect.forEach((tokenAddress, index) => {
const balance: BN = result[index];
/* istanbul ignore else */
if (String(balance) !== '0') {
nonZeroBalances[tokenAddress] = balance;
}
});
}
return nonZeroBalances;
}
}
```
> https://github.com/MetaMask/core/blob/main/packages/assets-controllers/src/AssetsContractController.ts#L522-L554

All tests for `getBalancesInSingleCall` only check for `toBeDefined()` or `strictEquals({})`, with the exception of the following:

- `'should track and use the currently selected chain ID and provider when getting balances in a single call'`

The fact that this test passes at runtime suggests that either this bug is only an issue at the type level, or there is a fallback method being called instead.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at packages/assets-controllers/src/AssetsContractController.ts, especially getBalancesInSingleCall and its Contract construction. Run the getBalancesInSingleCall tests, including the selected chain ID and provider case, and inspect the Contract typing around balances. Done means the call and result have appropriate TypeScript types without weakening the runtime behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
blockchain
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.