You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/source/core.rst
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -106,7 +106,7 @@ A syntax sugar for `useChainCall`_ that uses ABI, function name, and arguments i
106
106
- ``any[] | undefined`` - the result of a call or undefined if call didn't return yet
107
107
108
108
useContractCalls
109
-
===============
109
+
================
110
110
Makes calls to specific contracts and returns values. The hook will cause the component to refresh when a new block is mined and the return values change.
111
111
A syntax sugar for `useChainCalls`_ that uses ABI, function name, and arguments instead of raw data.
112
112
@@ -253,7 +253,7 @@ Returns a balance of a given token for a given address.
253
253
)
254
254
255
255
useTokenAllowance
256
-
===============
256
+
=================
257
257
258
258
Returns allowance (tokens left to use by spender) for given tokenOwner - spender relationship.
259
259
@@ -518,7 +518,7 @@ Returns short representation of address or throws an error if address is incorre
518
518
// TypeError("Invalid input, address can't be parsed")
519
519
520
520
shortenIfAddress
521
-
==============
521
+
================
522
522
523
523
Returns short representation of address or throws an error if address is incorrent.
``useEthers`` hook returns a number of useful functions and variables, see below:
48
+
49
+
- ``account`` - current user account (or ``null`` if not connected or connected in read-only mode)
50
+
51
+
- ``chainId`` - current chainId (or ``undefined`` if not connected)
52
+
53
+
- ``library`` - an instance of ethers `Web3Provider <https://docs.ethers.io/v5/api/providers/other/#Web3Provider>`_ (or ``undefined`` if not connected). Read more about ethers providers `here <https://docs.ethers.io/v5/api/providers/>`_.
54
+
55
+
- ``active`` - boolean that indicates if provider is connected (read or write mode)
56
+
57
+
- ``activate`` - function that allows to connect to a wallet. This is a `web3-react <https://github.com/NoahZinsmeister/web3-react>`_ function that can take various connectors.
58
+
59
+
- ``deactivate`` - function that disconnects wallet
60
+
61
+
- ``error`` - an error that occurred during connecting (e.g. connection is broken, unsupported network)
62
+
63
+
64
+
65
+
Example
66
+
=======
67
+
68
+
Example below demonstrates how to manage and use connection.
69
+
70
+
Application allow to see the balance of Ethereum 2.0 staking contracts in read-only mode.
71
+
When wallet is connected additionaly it shows user's account along with it's balance.
72
+
73
+
Example is available `here <https://example.usedapp.io/balance>`_.
- ``useBlockMeta()`` - return meta information (``timestamp`` and ``difficulty``) about most recent block mined
118
+
- ``useEtherBalance(address)`` - returns ether balance as BigNumber for given address (or ``undefined``)
119
+
- ``useTokenBalance(tokenAddress, address)``- returns balance of a given token as BigNumber for given address (or undefined)
120
+
- ``useTokenAllowance(tokenAddress, ownerAddress, spenderAddress)`` - returns allowance of a given token as BigNumber for given owner and spender address pair (or undefined)
121
+
122
+
Sooner or later you will want to make a custom call to a smart contract. Use ``useContractCall`` and ``useContractCalls`` for that purpose.
123
+
See section below on creating custom hooks.
15
124
16
-
Avoid using the result of one hook in another.
17
-
This will break single multicall into multiple multicalls.
18
-
It will reduce performance, generate delays, and flickering for the user.
19
-
Instead, try to retrieve needed information in a single call or multiple parallel calls.
20
-
That might need to modify smart contracts.
21
-
If that is too complex consider using a custom backend or The Graph.
22
125
23
126
Custom hooks
24
127
************
25
128
26
-
Creating a custom hook with the use of our core hooks is straightforward, as an example let's write a *useTokenBalance* hook.
129
+
Creating a custom hook with the use of our core hooks is straightforward, for example let’s examine the *useTokenBalance* hook.
27
130
28
131
The hook will retrieve a balance of an ERC20 token of the provided address.
29
132
@@ -39,7 +142,7 @@ The hook will retrieve a balance of an ERC20 token of the provided address.
39
142
return tokenBalance
40
143
}
41
144
42
-
Another example is useTokenAllowance hook. Insteadof balanceOf we use allowance on ERC20interface.
145
+
Another example is useTokenAllowance hook. Insteadof balanceOf, we use allowance on ERC20interface.
43
146
44
147
.. code-block:: javascript
45
148
@@ -69,13 +172,26 @@ The results are deferred so that the hook does not update too frequently.
69
172
In our custom hooks we can use any standard react hooks, custom react hooks and useDApp hooks.
70
173
Rules of hooks apply.
71
174
72
-
All core hooks are available `here <https://github.com/EthWorks/useDapp/tree/master/packages/core/src/hooks>`_.
175
+
Documentation for hooks is available `here <file:///Users/marek/Projects/useDapp/docs/dist/html/core.html#hooks>`_.
176
+
177
+
178
+
Using hooks considerations
179
+
==========================
180
+
181
+
There are some important considerations when using hooks based on `useChainCall`, `useChainCalls` and `useContractCalls`.
182
+
183
+
Avoid using the result of one hook in another.
184
+
This will break single multicall into multiple multicalls.
185
+
It will reduce performance, generate delays, and flickering for the user.
186
+
Instead, try to retrieve needed information in a single call or multiple parallel calls.
187
+
That might require modification of smart contracts.
188
+
If that is too complex consider using a custom backend or `The Graph <https://thegraph.com/>`_.
73
189
74
190
75
191
Testing hooks
76
192
*************
77
193
78
-
Let's take useTokenAllowance as an example.
194
+
Let's take ``useTokenAllowance`` as an example.
79
195
80
196
To write a test, start with a setup code that will create a mock provider and test wallets.
81
197
@@ -84,7 +200,7 @@ To write a test, start with a setup code that will create a mock provider and te
To check if the hook reads data correctly we need to prepare it first. We approve the spender so that we can check that our hook returns the correct value.
232
+
To check if the hook reads data correctly, we need to prepare it first. We approve the spender so that we can check if our hook returns the correct value.
117
233
118
-
To test the hook we need to render it using renderWeb3Hook. It works like `renderHook` from the react-testing-library,
234
+
To test the hook we need to render it using ``renderWeb3Hook``. It works like ``renderHook`` from the `react-testing-library <https://testing-library.com/docs/react-testing-library/intro/>`_,
119
235
but it wraps the hook into additional providers.
120
236
121
237
React components update asynchronically. Reading data from the blockchain is also an async operation.
122
-
To get the return value from the hook, wait for the result to be set.
238
+
To get the return value from the hook, wait for the result to be set. You can do it with``waitForCurrent``.
123
239
124
-
Then we can check if our result is correct. `result.current` is a value returned from our hook. It should be equal to 1 Ether.
240
+
Then we can check if our result is correct. ``result.current`` is a value returned from our hook. It should be equal to 1 Ether.
0 commit comments