Skip to content

Commit ecc0468

Browse files
committed
add fetchBalance
1 parent 008b224 commit ecc0468

File tree

5 files changed

+63
-2
lines changed

5 files changed

+63
-2
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Simple WalletConnect v2 integration package for Vue3 apps.
1414
- [Usage](#usage)
1515
- [Connect wallet button](#connect-wallet-button)
1616
- [Multicall](#multicall)
17+
- [FetchBalance](#fetchbalance)
1718
- [ReadContract](#readcontract)
1819
- [WriteContract](#writecontract)
1920
- [EstimateWriteContractGas](#estimatewritecontractgas)
@@ -106,6 +107,42 @@ let data = await multicall({
106107
*/
107108
```
108109

110+
## FetchBalance
111+
```js
112+
import {fetchBalance} from '@kolirt/vue-web3-auth'
113+
114+
let bnbBalance = await fetchBalance({
115+
address: '0x2D4C407BBe49438ED859fe965b140dcF1aaB71a9'
116+
})
117+
118+
/**
119+
* Result in bnbBalance
120+
*
121+
* {
122+
* decimals: 18,
123+
* formatted: '1.908669631824871303',
124+
* symbol: 'BNB',
125+
* value: 1908669631824871303n
126+
* }
127+
*/
128+
129+
let tokenBalance = await fetchBalance({
130+
address: '0x2D4C407BBe49438ED859fe965b140dcF1aaB71a9',
131+
token: '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c'
132+
})
133+
134+
/**
135+
* Result in tokenBalance
136+
*
137+
* {
138+
* decimals: 18,
139+
* formatted: '0',
140+
* symbol: 'WBNB',
141+
* value: 0n
142+
* }
143+
*/
144+
```
145+
109146
## ReadContract
110147

111148
```ts

lib/balance.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type {FetchBalance} from './types'
2+
import {fetchBalance as masterFetchBalance} from '@wagmi/core'
3+
import {chain} from './chain'
4+
5+
export function fetchBalance(data: FetchBalance) {
6+
return masterFetchBalance({
7+
chainId: data.chainId || chain.value.id,
8+
address: data.address,
9+
token: data.token,
10+
formatUnits: data.formatUnits
11+
})
12+
}

lib/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ export {
2424
chain
2525
} from './chain'
2626

27+
export {
28+
fetchBalance
29+
} from './balance'
30+
2731
export {
2832
readContract,
2933
writeContract,

lib/types.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type {Chain} from '@wagmi/core'
1+
import type {Chain, Unit} from '@wagmi/core'
22
import type {ThemeCtrlState} from '@web3modal/core'
33
import type {EthereumClient} from '@web3modal/ethereum'
44
import type {BlockTag} from 'viem/src/types/block'
@@ -83,4 +83,12 @@ export type WriteContract = {
8383
nonce?: number
8484
value?: bigint
8585
confirmations?: number
86+
}
87+
88+
export type FetchBalance = {
89+
chainId?: number
90+
address: `0x${string}`
91+
formatUnits?: Unit
92+
token?: `0x${string}`
93+
8694
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@kolirt/vue-web3-auth",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"description": "Web3 authentication for Vue3 apps based on WalletConnect v2",
55
"author": "kolirt",
66
"license": "MIT",

0 commit comments

Comments
 (0)