Skip to content

Commit 3e0ea5c

Browse files
committed
add some utilities
1 parent 2c3abf9 commit 3e0ea5c

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/commands/cmdFuncMap.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ import { getProperty, inspect } from './object'
88
import { wallet } from './wallet'
99
import {
1010
connectedChain,
11+
formatEther,
12+
formatUnits,
1113
getAddress,
1214
getBalance,
1315
getBalanceDetails,
1416
getBlock,
1517
getProof,
1618
getTransactionReceipt,
19+
parseEther,
1720
parseUnits,
1821
supportedChains,
1922
switchNetwork,
@@ -397,6 +400,37 @@ const cmdFuncMap: Record<string, CmdFunc> = {
397400
'value => String representation of a number',
398401
'decimals => Exponent of base 10'
399402
].join('<br>')
403+
},
404+
['formatUnits']: {
405+
exec: formatUnits,
406+
help: [
407+
'Divides a number by a given exponent of base 10, and formats it into a string representation of the number.',
408+
'Usage: formatUnits 420000000000n 9',
409+
'Output: 420',
410+
'Params:',
411+
'value => BigNumber to format as a string, number or bigint',
412+
'decimals => Exponent of base 10'
413+
].join('<br>')
414+
},
415+
['parseEther']: {
416+
exec: parseEther,
417+
help: [
418+
'Converts a string representation of ether to numerical wei.',
419+
'Usage: parseEther 420',
420+
'Output: 420000000000000000000n',
421+
'Params:',
422+
'value => String representation of ether'
423+
].join('<br>')
424+
},
425+
['formatEther']: {
426+
exec: formatEther,
427+
help: [
428+
'Converts numerical wei to a string representation of ether.',
429+
'Usage: formatEther 1000000000000000000n',
430+
'Output: 1',
431+
'Params:',
432+
'value => The wei value',
433+
].join('<br>')
400434
}
401435
}
402436

src/commands/web3.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ import {
1111
} from '@wagmi/core'
1212
import checkConnected from '../utils/checkConnected'
1313
import { checkSupportedChain } from '../web3/wagmi'
14-
import { type GetBlockParameters, type Hex, parseUnits as viemParseUnits } from 'viem'
14+
import {
15+
type GetBlockParameters,
16+
type Hex,
17+
parseUnits as viemParseUnits,
18+
formatUnits as viemFormatUnits,
19+
parseEther as viemParseEther,
20+
formatEther as viemFormatEther
21+
} from 'viem'
1522
import getPublicClient from '../utils/getPublicClient'
1623

1724
export function getAddress() {
@@ -151,3 +158,16 @@ export const parseUnits = (value: string | number, decimals: number) => {
151158
// doesn't support numbers as input
152159
return viemParseUnits(`${value}`, decimals)
153160
}
161+
162+
export const formatUnits = (value: string | number | bigint, decimals: number) => {
163+
return viemFormatUnits(BigInt(value), decimals)
164+
}
165+
166+
export const parseEther = (value: string | number) => {
167+
// Converting to string for support of numbers as input
168+
return viemParseEther(`${value}`)
169+
}
170+
171+
export const formatEther = (value: string | number | bigint) => {
172+
return viemFormatEther(BigInt(value))
173+
}

0 commit comments

Comments
 (0)