Skip to content

Commit eeafdd9

Browse files
committed
Use viem utils in common
1 parent 51c60b5 commit eeafdd9

File tree

4 files changed

+26
-75
lines changed

4 files changed

+26
-75
lines changed

packages/common/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"ethers": "5.5.4"
7676
},
7777
"dependencies": {
78-
"joi": "17.9.1"
78+
"joi": "17.9.1",
79+
"viem": "2.10.2"
7980
}
8081
}

packages/common/src/utils.ts

Lines changed: 9 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,19 @@
1+
import { formatEther, hexToBigInt, numberToHex, parseEther } from 'viem'
12
import type { Address } from './types.js'
23

3-
const addressRegex = /^0x[a-fA-F0-9]{40}$/
4-
54
export const isAddress = (address: string): address is Address => {
6-
return addressRegex.test(address)
5+
return isAddress(address)
76
}
87

9-
export const weiHexToEth = (wei: string): string => {
10-
const weiBigInt = BigInt(parseInt(wei, 16))
11-
const ethBalance = divideBigIntWithDecimalResolution(
12-
weiBigInt,
13-
BigInt('1000000000000000000')
14-
)
15-
16-
return ethBalance.toString()
8+
export const weiHexToEth = (wei: `0x${string}`): string => {
9+
const weiBigInt = hexToBigInt(wei)
10+
return formatEther(weiBigInt)
1711
}
12+
1813
export const weiToEth = (wei: string): string => {
1914
if (!wei) return wei
2015
const weiBigInt = BigInt(parseInt(wei))
21-
const ethBalance = divideBigIntWithDecimalResolution(
22-
weiBigInt,
23-
BigInt('1000000000000000000')
24-
)
25-
26-
return ethBalance.toString()
27-
}
28-
29-
const divideBigIntWithDecimalResolution = (
30-
dividend: bigint,
31-
divisor: bigint,
32-
decimalPlaces = 8
33-
): number => {
34-
if (typeof dividend !== 'bigint' || typeof divisor !== 'bigint') {
35-
throw new Error('dividend and divisor must be BigInt values')
36-
}
37-
38-
if (typeof decimalPlaces !== 'number' || decimalPlaces < 0) {
39-
throw new Error('decimalPlaces must be a non-negative number')
40-
}
41-
42-
// Multiply the dividend by 10 ** decimalPlaces to add precision
43-
const adjustedDividend = dividend * BigInt(10 ** decimalPlaces)
44-
45-
const quotient = adjustedDividend / divisor
46-
47-
// Convert the result back to a decimal number
48-
const decimalPart = String(quotient % BigInt(10 ** decimalPlaces)).padStart(
49-
decimalPlaces,
50-
'0'
51-
)
52-
const integerPart = quotient / BigInt(10 ** decimalPlaces)
53-
const result = `${integerPart}.${decimalPart}`
54-
55-
return parseFloat(result)
16+
return formatEther(weiBigInt)
5617
}
5718

5819
export const ethToWeiBigInt = (eth: string | number): bigint => {
@@ -62,20 +23,9 @@ export const ethToWeiBigInt = (eth: string | number): bigint => {
6223

6324
const ethString = typeof eth === 'number' ? eth.toString() : eth
6425

65-
const decimalSplit = ethString.split('.')
66-
const integerPart = BigInt(decimalSplit[0])
67-
const decimalPart =
68-
decimalSplit.length > 1 ? BigInt(decimalSplit[1]) : BigInt(0)
69-
70-
const decimalLength = decimalSplit.length > 1 ? decimalSplit[1].length : 0
71-
const weiFactor = BigInt(10 ** (18 - decimalLength))
72-
73-
// Perform the conversion from Eth to Wei
74-
const weiValue = integerPart * BigInt(10 ** 18) + decimalPart * weiFactor
75-
76-
return BigInt(weiValue)
26+
return parseEther(ethString)
7727
}
7828

7929
export const bigIntToHex = (value: bigint): string => {
80-
return `0x${value.toString(16)}`
30+
return numberToHex(value)
8131
}

packages/core/src/provider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ export async function getBalance(
455455
const balanceHex = await provider.request({
456456
method: 'eth_getBalance',
457457
params: [address, 'latest']
458-
})
458+
}) as `0x${string}`
459459
return balanceHex
460460
? { [chain.token || 'eth']: weiHexToEth(balanceHex) }
461461
: null

yarn.lock

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15534,6 +15534,20 @@ verror@1.10.0:
1553415534
core-util-is "1.0.2"
1553515535
extsprintf "^1.2.0"
1553615536

15537+
viem@2.10.2, viem@^2.10.2:
15538+
version "2.10.2"
15539+
resolved "https://registry.yarnpkg.com/viem/-/viem-2.10.2.tgz#65c2ca82b64e6fb9ee84c02b95dcf2c270ffe651"
15540+
integrity sha512-gcOL+XxA0UWDarli856OEgumaBz4df/qNMpgno4NTSSZtJSC1XixIb3gWjVBei6Vx085ivw/U9ZE8gdniIo7fA==
15541+
dependencies:
15542+
"@adraffy/ens-normalize" "1.10.0"
15543+
"@noble/curves" "1.2.0"
15544+
"@noble/hashes" "1.3.2"
15545+
"@scure/bip32" "1.3.2"
15546+
"@scure/bip39" "1.2.1"
15547+
abitype "1.0.0"
15548+
isows "1.0.3"
15549+
ws "8.13.0"
15550+
1553715551
viem@^1.0.0:
1553815552
version "1.1.4"
1553915553
resolved "https://registry.yarnpkg.com/viem/-/viem-1.1.4.tgz#21be6d235fb387a391d4abdbbf95cd93bd066ffb"
@@ -15563,20 +15577,6 @@ viem@^1.1.4:
1556315577
isows "1.0.3"
1556415578
ws "8.13.0"
1556515579

15566-
viem@^2.10.2:
15567-
version "2.10.2"
15568-
resolved "https://registry.yarnpkg.com/viem/-/viem-2.10.2.tgz#65c2ca82b64e6fb9ee84c02b95dcf2c270ffe651"
15569-
integrity sha512-gcOL+XxA0UWDarli856OEgumaBz4df/qNMpgno4NTSSZtJSC1XixIb3gWjVBei6Vx085ivw/U9ZE8gdniIo7fA==
15570-
dependencies:
15571-
"@adraffy/ens-normalize" "1.10.0"
15572-
"@noble/curves" "1.2.0"
15573-
"@noble/hashes" "1.3.2"
15574-
"@scure/bip32" "1.3.2"
15575-
"@scure/bip39" "1.2.1"
15576-
abitype "1.0.0"
15577-
isows "1.0.3"
15578-
ws "8.13.0"
15579-
1558015580
viem@^2.9.15:
1558115581
version "2.9.21"
1558215582
resolved "https://registry.yarnpkg.com/viem/-/viem-2.9.21.tgz#a7dd3d4c827088e5336e5a6b35ec0283d2938595"

0 commit comments

Comments
 (0)