Skip to content

Commit 2ad799c

Browse files
Update guides (#174)
* Update guides
1 parent bb0ff89 commit 2ad799c

File tree

2 files changed

+138
-22
lines changed

2 files changed

+138
-22
lines changed

docs/source/core.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ A syntax sugar for `useChainCall`_ that uses ABI, function name, and arguments i
106106
- ``any[] | undefined`` - the result of a call or undefined if call didn't return yet
107107

108108
useContractCalls
109-
===============
109+
================
110110
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.
111111
A syntax sugar for `useChainCalls`_ that uses ABI, function name, and arguments instead of raw data.
112112

@@ -253,7 +253,7 @@ Returns a balance of a given token for a given address.
253253
)
254254
255255
useTokenAllowance
256-
===============
256+
=================
257257

258258
Returns allowance (tokens left to use by spender) for given tokenOwner - spender relationship.
259259

@@ -518,7 +518,7 @@ Returns short representation of address or throws an error if address is incorre
518518
// TypeError("Invalid input, address can't be parsed")
519519
520520
shortenIfAddress
521-
==============
521+
================
522522

523523
Returns short representation of address or throws an error if address is incorrent.
524524
Returns empty string if no address is provided.

docs/source/guide.rst

Lines changed: 135 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,130 @@ Guides
33

44
Connecting to network
55
*********************
6-
Coming soon...
6+
7+
Read-only
8+
=========
9+
To connect to the network in read-only mode, provide ``readOnlyChainId`` and ``readOnlyUrls`` fileds in application configuration.
10+
11+
See example configuration below:
12+
13+
.. code-block:: javascript
14+
15+
const config = {
16+
readOnlyChainId: ChainId.Mainnet,
17+
readOnlyUrls: {
18+
[ChainId.Mainnet]: 'https://mainnet.infura.io/v3/62687d1a985d4508b2b7a24827551934',
19+
},
20+
}
21+
22+
23+
Browser wallet
24+
==============
25+
26+
To connect to a wallet in a web3-enabled browser, use ``activateBrowserWallet`` returned by ``useEthers()``.
27+
Once connected ``account`` variable will be availabe.
28+
29+
See example below:
30+
31+
.. code-block:: javascript
32+
33+
export function App() {
34+
const { activateBrowserWallet, account } = useEthers()
35+
return (
36+
<div>
37+
{!account && <button onClick={activateBrowserWallet}> Connect </button>}
38+
{account && <p>Account: {account}</p>}
39+
</div>
40+
)
41+
}
42+
43+
44+
useEthers
45+
=========
46+
47+
``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>`_.
74+
75+
.. code-block:: javascript
76+
77+
const config = {
78+
readOnlyChainId: ChainId.Mainnet,
79+
readOnlyUrls: {
80+
[ChainId.Mainnet]: 'https://mainnet.infura.io/v3/62687d1a985d4508b2b7a24827551934',
81+
},
82+
}
83+
84+
ReactDOM.render(
85+
<DAppProvider config={config}>
86+
<App />
87+
</DAppProvider>
88+
document.getElementById('root')
89+
)
90+
91+
const STAKING_CONTRACT = '0x00000000219ab540356cBB839Cbe05303d7705Fa'
92+
93+
export function App() {
94+
const { activateBrowserWallet, deactivate, account } = useEthers()
95+
const userBalance = useEtherBalance(account)
96+
const stakingBalance = useEtherBalance(STAKING_CONTRACT)
97+
98+
return (
99+
<div>
100+
{!account && <button onClick={activateBrowserWallet}> Connect </button>}
101+
{account && <button onClick={deactivate}> Disconnect </button>}
102+
103+
{stakingBalance && <p>ETH2 staking balance: {formatEther(stakingBalance)} ETH </p>}
104+
{account && <p>Account: {account}</p>}
105+
{userBalance && <p>Ether balance: {formatEther(userBalance)} ETH </p>}
106+
</div>
107+
)
108+
}
109+
110+
7111
8112
Reading from blockchain
9113
***********************
10114

11-
When using hooks based on `useChainCall`, `useChainCalls` and `useContractCalls` there are important considerations;
115+
There is a number of useful hooks that you can use to read blockchain state:
12116

13-
Avoid using the result of one hook in another
14-
==================================================
117+
- ``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.
15124

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.
22125

23126
Custom hooks
24127
************
25128

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.
27130

28131
The hook will retrieve a balance of an ERC20 token of the provided address.
29132

@@ -39,7 +142,7 @@ The hook will retrieve a balance of an ERC20 token of the provided address.
39142
return tokenBalance
40143
}
41144
42-
Another example is useTokenAllowance hook. Instead of balanceOf we use allowance on ERC20 interface.
145+
Another example is useTokenAllowance hook. Instead of balanceOf, we use allowance on ERC20 interface.
43146
44147
.. code-block:: javascript
45148
@@ -69,13 +172,26 @@ The results are deferred so that the hook does not update too frequently.
69172
In our custom hooks we can use any standard react hooks, custom react hooks and useDApp hooks.
70173
Rules of hooks apply.
71174
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/>`_.
73189
74190
75191
Testing hooks
76192
*************
77193
78-
Let's take useTokenAllowance as an example.
194+
Let's take ``useTokenAllowance`` as an example.
79195
80196
To write a test, start with a setup code that will create a mock provider and test wallets.
81197
@@ -84,7 +200,7 @@ To write a test, start with a setup code that will create a mock provider and te
84200
const mockProvider = new MockProvider()
85201
const [deployer, spender] = mockProvider.getWallets()
86202
87-
Before each test deploy an ERC20 contract. It's important as otherwise the result of one
203+
Before each test, deploy an ERC20 contract. It's important as otherwise the result of one
88204
test could break the other one.
89205
90206
.. code-block:: javascript
@@ -113,15 +229,15 @@ After setup, we have to test the hook.
113229
expect(result.error).to.be.undefined
114230
expect(result.current).to.eq(utils.parseEther('1'))
115231
116-
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.
117233
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/>`_,
119235
but it wraps the hook into additional providers.
120236
121237
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``.
123239
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.
125241
126242
127243
**Full example**

0 commit comments

Comments
 (0)