From ecac0ba36243d94c9199d0bd21937104c835d9a0 Mon Sep 17 00:00:00 2001 From: Tom Meagher Date: Tue, 5 Nov 2024 18:36:10 -0500 Subject: [PATCH] fix: #4353 --- .changeset/three-sloths-beam.md | 5 ++ packages/core/src/actions/getBalance.test.ts | 74 +++++++++++--------- packages/core/src/actions/getBalance.ts | 18 ++--- 3 files changed, 54 insertions(+), 43 deletions(-) create mode 100644 .changeset/three-sloths-beam.md diff --git a/.changeset/three-sloths-beam.md b/.changeset/three-sloths-beam.md new file mode 100644 index 0000000000..3c3067c538 --- /dev/null +++ b/.changeset/three-sloths-beam.md @@ -0,0 +1,5 @@ +--- +"@wagmi/core": patch +--- + +Fixed `getBalance` symbol error handling. diff --git a/packages/core/src/actions/getBalance.test.ts b/packages/core/src/actions/getBalance.test.ts index f2d7354c36..954c09c2e6 100644 --- a/packages/core/src/actions/getBalance.test.ts +++ b/packages/core/src/actions/getBalance.test.ts @@ -1,6 +1,6 @@ import { accounts, chain, config, testClient } from '@wagmi/test' import { parseEther } from 'viem' -import { beforeEach, describe, expect, test } from 'vitest' +import { beforeEach, expect, test } from 'vitest' import { getBalance } from './getBalance.js' @@ -19,11 +19,8 @@ beforeEach(async () => { await testClient.mainnet2.mine({ blocks: 1 }) }) -describe('getBalance', () => { - test('default', async () => { - await expect( - getBalance(config, { address }), - ).resolves.toMatchInlineSnapshot(` +test('default', async () => { + await expect(getBalance(config, { address })).resolves.toMatchInlineSnapshot(` { "decimals": 18, "formatted": "10000", @@ -32,13 +29,11 @@ describe('getBalance', () => { } `) - await testClient.mainnet.setBalance({ - address, - value: parseEther('6969.12222215666'), - }) - await expect( - getBalance(config, { address }), - ).resolves.toMatchInlineSnapshot(` + await testClient.mainnet.setBalance({ + address, + value: parseEther('6969.12222215666'), + }) + await expect(getBalance(config, { address })).resolves.toMatchInlineSnapshot(` { "decimals": 18, "formatted": "6969.12222215666", @@ -46,12 +41,12 @@ describe('getBalance', () => { "value": 6969122222156660000000n, } `) - }) +}) - test('parameters: chainId', async () => { - await expect( - getBalance(config, { address, chainId: chain.mainnet2.id }), - ).resolves.toMatchInlineSnapshot(` +test('parameters: chainId', async () => { + await expect( + getBalance(config, { address, chainId: chain.mainnet2.id }), + ).resolves.toMatchInlineSnapshot(` { "decimals": 18, "formatted": "420", @@ -59,15 +54,15 @@ describe('getBalance', () => { "value": 420000000000000000000n, } `) - }) +}) - test('parameters: token', async () => { - await expect( - getBalance(config, { - address: '0x4557B18E779944BFE9d78A672452331C186a9f48', - token: '0x6B175474E89094C44Da98b954EedeAC495271d0F', - }), - ).resolves.toMatchInlineSnapshot(` +test('parameters: token', async () => { + await expect( + getBalance(config, { + address: '0x4557B18E779944BFE9d78A672452331C186a9f48', + token: '0x6B175474E89094C44Da98b954EedeAC495271d0F', + }), + ).resolves.toMatchInlineSnapshot(` { "decimals": 18, "formatted": "0.559062564299199392", @@ -75,12 +70,28 @@ describe('getBalance', () => { "value": 559062564299199392n, } `) - }) +}) - test('parameters: unit', async () => { - await expect( - getBalance(config, { address, unit: 'wei' }), - ).resolves.toMatchInlineSnapshot(` +test('parameters: token (bytes32 symbol)', async () => { + await expect( + getBalance(config, { + address: '0x4557B18E779944BFE9d78A672452331C186a9f48', + token: '0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2', + }), + ).resolves.toMatchInlineSnapshot(` + { + "decimals": 18, + "formatted": "0", + "symbol": "MKR", + "value": 0n, + } + `) +}) + +test('parameters: unit', async () => { + await expect( + getBalance(config, { address, unit: 'wei' }), + ).resolves.toMatchInlineSnapshot(` { "decimals": 18, "formatted": "10000000000000000000000", @@ -88,5 +99,4 @@ describe('getBalance', () => { "value": 10000000000000000000000n, } `) - }) }) diff --git a/packages/core/src/actions/getBalance.ts b/packages/core/src/actions/getBalance.ts index 63d079d25a..1aae5667b1 100644 --- a/packages/core/src/actions/getBalance.ts +++ b/packages/core/src/actions/getBalance.ts @@ -1,11 +1,4 @@ -import { - type Address, - ContractFunctionExecutionError, - type Hex, - formatUnits, - hexToString, - trim, -} from 'viem' +import { type Address, type Hex, formatUnits, hexToString, trim } from 'viem' import { type GetBalanceErrorType as viem_GetBalanceErrorType, type GetBalanceParameters as viem_GetBalanceParameters, @@ -18,7 +11,7 @@ import type { Unit } from '../types/unit.js' import type { Compute } from '../types/utils.js' import { getAction } from '../utils/getAction.js' import { getUnit } from '../utils/getUnit.js' -import { readContracts } from './readContracts.js' +import { type ReadContractsErrorType, readContracts } from './readContracts.js' export type GetBalanceParameters = Compute< ChainIdParameter & @@ -56,7 +49,7 @@ export async function getBalance( if (tokenAddress) { try { - return getTokenBalance(config, { + return await getTokenBalance(config, { balanceAddress: address, chainId, symbolType: 'string', @@ -66,7 +59,10 @@ export async function getBalance( // In the chance that there is an error upon decoding the contract result, // it could be likely that the contract data is represented as bytes32 instead // of a string. - if (error instanceof ContractFunctionExecutionError) { + if ( + (error as ReadContractsErrorType).name === + 'ContractFunctionExecutionError' + ) { const balance = await getTokenBalance(config, { balanceAddress: address, chainId,