Skip to content

Commit

Permalink
fix: #4353
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Nov 5, 2024
1 parent 038388f commit ecac0ba
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 43 deletions.
5 changes: 5 additions & 0 deletions .changeset/three-sloths-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wagmi/core": patch
---

Fixed `getBalance` symbol error handling.
74 changes: 42 additions & 32 deletions packages/core/src/actions/getBalance.test.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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",
Expand All @@ -32,61 +29,74 @@ 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",
"symbol": "ETH",
"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",
"symbol": "WAG",
"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",
"symbol": "DAI",
"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",
"symbol": "ETH",
"value": 10000000000000000000000n,
}
`)
})
})
18 changes: 7 additions & 11 deletions packages/core/src/actions/getBalance.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<config extends Config = Config> = Compute<
ChainIdParameter<config> &
Expand Down Expand Up @@ -56,7 +49,7 @@ export async function getBalance<config extends Config>(

if (tokenAddress) {
try {
return getTokenBalance(config, {
return await getTokenBalance(config, {
balanceAddress: address,
chainId,
symbolType: 'string',
Expand All @@ -66,7 +59,10 @@ export async function getBalance<config extends Config>(
// 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,
Expand Down

0 comments on commit ecac0ba

Please sign in to comment.