Skip to content

Commit 90d43df

Browse files
naz3ehDhaiwat10
andauthored
Add docs for hooks (#192)
* `web3-ui/hooks` readme boilerplate * Update README.md * Add code snippets * Revert "Add code snippets" This reverts commit 9406130. * Add code snippets * Refactor * Add changeset Co-authored-by: Dhaiwat Pandya <[email protected]>
1 parent bdf61b9 commit 90d43df

File tree

2 files changed

+138
-1
lines changed

2 files changed

+138
-1
lines changed

.changeset/odd-crabs-repair.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@web3-ui/hooks': patch
3+
---
4+
5+
Add README

packages/hooks/README.md

Lines changed: 133 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,135 @@
11
# @web3-ui/hooks
22

3-
To be populated.
3+
A set of React hooks developed for web3 use cases.
4+
5+
```bash
6+
yarn add @web3-ui/hooks
7+
```
8+
9+
## Getting started
10+
11+
At the root of your React app, wrap your app in the <Provider> component:
12+
13+
```tsx
14+
// _app.tsx (or the root of your app)
15+
import { Provider } from '@web3-ui/hooks';
16+
17+
function MyApp({ Component, pageProps }) {
18+
return (
19+
<Provider>
20+
<Component {...pageProps} />
21+
</Provider>
22+
);
23+
}
24+
```
25+
26+
---
27+
28+
## Hooks
29+
30+
The following hooks are available:
31+
32+
- [useWallet](#usewallet)
33+
- [useContract](#usecontract)
34+
- [useTransaction](#usetransaction)
35+
- [useTokenBalance](#usetokenbalance)
36+
37+
---
38+
39+
### useWallet
40+
41+
The `useWallet` hook lets you request and interact with the active wallet connection.
42+
43+
- `connectWallet`: Calling this function will open up a modal that will let the user connect their wallet.
44+
- `disconnectWallet`
45+
- `connection`: This object contains the following properties:
46+
- `userAddress`
47+
- `network`
48+
- `signer`
49+
- `ens`
50+
- `connected`: a boolean indicating if the user has connected their wallet
51+
- `provider`
52+
- `correctNetwork`: a boolean indicating if the user is connected to the correct network
53+
- `switchToCorrectNetwork`: a function that will switch to the correct network. (only MetaMask supported)
54+
55+
```tsx
56+
import { useWallet } from '@web3-ui/hooks';
57+
58+
const {
59+
connection,
60+
connectWallet,
61+
disconnectWallet,
62+
connected,
63+
correctNetwork,
64+
switchToCorrectNetwork
65+
} = useWallet();
66+
```
67+
68+
---
69+
70+
### useContract
71+
72+
The `useContract` hook takes the ABI and address of a contract and returns the contract instance.
73+
74+
```tsx
75+
import { useContract } from '@web3-ui/hooks';
76+
77+
const [contract, isReady] = useContract('CONTRACT_ADDRESS', 'CONTRACT_ABI');
78+
79+
//check that the contract has been loaded
80+
if (isReady) {
81+
await contract.greeting();
82+
}
83+
```
84+
85+
---
86+
87+
### useTransaction
88+
89+
The `useTransaction` hook takes in a contract function and a list of arguments to pass to it. It returns an array of three elements:
90+
91+
- `execute`: Calling this function will execute the transaction.
92+
93+
- `loading`: Will be true when the transaction is executing and will be false once the transaction has been confirmed.
94+
95+
- `error`
96+
97+
```tsx
98+
import { useTransaction, useContract } from '@web3-ui/hooks';
99+
100+
const greeterContract = useContract('CONTRACT_ADDRESS', 'CONTRACT_ABI');
101+
const [execute, loading, error] = useTransaction(greeter.setGreeting, [
102+
'Hello, world!',
103+
{
104+
value: ethers.utils.parseEther('0.1') // you can also use this for payable transactions
105+
}
106+
]);
107+
108+
execute(); // will execute the transaction
109+
```
110+
111+
---
112+
113+
### useTokenBalance
114+
115+
The `useTokenBalance` hook takes in a ERC20 token contract address and an account address as arguments and returns an object with the following properties:
116+
117+
- `balance`: The balance of the token for the given account in wei.
118+
- `loading`
119+
- `error`
120+
- `decimals`: The number of decimals the token contract is using.
121+
- `formattedBalance`: Balance in ethers. eg. 20 GTC, 31.2 USDT, etc.
122+
- `balanceInBigNumber`
123+
124+
```tsx
125+
import { useTokenBalance } from '@web3-ui/hooks';
126+
127+
const {
128+
balance,
129+
loading,
130+
error,
131+
decimals,
132+
formattedBalance,
133+
balanceInBigNumber
134+
} = useTokenBalance('TOKEN_CONTRACT_ADDRESS', 'ACCOUNT_ADDRESS');
135+
```

0 commit comments

Comments
 (0)