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

fix: handle multicall revert in TokenBalancesController #5083

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
46 changes: 46 additions & 0 deletions packages/assets-controllers/src/multicall.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,50 @@ describe('multicall', () => {
]);
});
});

describe('error handling of reverts', () => {
const call = {
contract: new Contract(
'0x0000000000000000000000000000000000000001',
abiERC20,
provider,
),
functionSignature: 'balanceOf(address)',
arguments: ['0x0000000000000000000000000000000000000000'],
};

it('should fall back to parallel calls when multicall reverts', async () => {
jest.spyOn(provider, 'call').mockImplementationOnce(() => {
const error = { code: 'CALL_EXCEPTION' };
return Promise.reject(error);
});

jest
.spyOn(provider, 'call')
.mockImplementationOnce(() =>
Promise.resolve(defaultAbiCoder.encode(['uint256'], [1])),
);

const results = await multicallOrFallback([call], '0x1', provider);

expect(results).toMatchObject([
{
success: true,
// eslint-disable-next-line @typescript-eslint/naming-convention
value: { _hex: '0x01' },
},
]);
});

it('should throw rpc errors other than revert', async () => {
const error = { code: 'network error' };
jest.spyOn(provider, 'call').mockImplementationOnce(() => {
return Promise.reject(error);
});

await expect(
multicallOrFallback([call], '0x1', provider),
).rejects.toMatchObject(error);
});
});
});
26 changes: 23 additions & 3 deletions packages/assets-controllers/src/multicall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,27 @@ export const multicallOrFallback = async (
}

const multicallAddress = MULTICALL_CONTRACT_BY_CHAINID[chainId];
return await (multicallAddress
? multicall(calls, multicallAddress, provider, maxCallsPerMulticall)
: fallback(calls, maxCallsParallel));
if (multicallAddress) {
try {
return await multicall(
calls,
multicallAddress,
provider,
maxCallsPerMulticall,
);
} catch (error: unknown) {
// Fallback only on revert
// https://docs.ethers.org/v5/troubleshooting/errors/#help-CALL_EXCEPTION
if (
!error ||
typeof error !== 'object' ||
!('code' in error) ||
error.code !== 'CALL_EXCEPTION'
Comment on lines +407 to +410
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a complicated condition. Could you elaborate a bit on your code comment on line 404 to better explain what these conditions represent?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to only retry if the call reverted, and not retry on other kinds of errors that could occur. I don't know exactly what I expect those errors to be, but could be like a dead rpc connection or something.

) {
throw error;
}
}
}

return await fallback(calls, maxCallsParallel);
};
Loading