Skip to content

Commit 43ac57a

Browse files
📔 Add more documentation (#69)
* Add more documentation Co-authored-by: Justyna Broniszewska <33961199+JustynaBroniszewska@users.noreply.github.com>
1 parent a9768c8 commit 43ac57a

File tree

3 files changed

+164
-24
lines changed

3 files changed

+164
-24
lines changed

docs/source/core.rst

Lines changed: 154 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,51 +66,141 @@ useBlock
6666
useBlockMeta
6767
============
6868

69+
useChainCall
70+
============
71+
72+
Makes a call to a specific contract and returns the value. The hook will cause the component to refresh whenever a new block is mined and the value is changed.
73+
74+
Calls will be combined into a single multicall across all uses of *useChainCall* and *useChainCalls*.
75+
76+
It is recommended to use `useContractCall`_ where applicable instead of this method.
77+
78+
*Parameters*
79+
80+
- ``call: ChainCall | Falsy`` - a single call, also see `ChainCall`_. A call can be `Falsy`, as it is important to keep the same ordering of hooks even if in a given render cycle there might be not enough information to perform a call.
81+
82+
6983
useChainCalls
7084
=============
7185

86+
Makes multiple calls to specific contracts and returns values. The hook will cause the component to refresh when values change.
87+
88+
Calls will be combined into a single multicall across all uses of *useChainCall* and *useChainCalls*.
89+
It is recommended to use `useContractCall`_ where applicable instead of this method.
90+
7291
*Parameters*
7392

74-
- ``calls: ChainCall[]``
93+
- ``calls: ChainCall[]`` - list of calls, also see `ChainCall`_. Calls need to be in the same order across component renders.
94+
95+
useContractCall
96+
===============
97+
Makes a call to a specific contract and returns the value. The hook will cause the component to refresh when a new block is mined and the return value changes.
98+
A syntax sugar for `useChainCall`_ that uses ABI, function name, and arguments instead of raw data.
99+
100+
**Parameters**
101+
102+
- ``calls: ContractCall | Falsy`` - a single call to a contract , also see `ContractCall`_
103+
104+
**Returns**
105+
106+
- ``any[] | undefined`` - the result of a call or undefined if call didn't return yet
107+
108+
useContractCalls
109+
===============
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+
A syntax sugar for `useChainCalls`_ that uses ABI, function name, and arguments instead of raw data.
112+
113+
**Parameters**
75114

115+
- ``calls: ContractCall[]`` - a single call to a contract , also see `ContractCall`_
116+
117+
**Returns**
118+
119+
- ``any[] | undefined`` - array of results of undefined if call didn't return yet
76120

77121
useConfig
78122
=========
79123

124+
Returns singleton instance of `Config`_.
125+
126+
Function takes no parameters.
80127

81128

82129
useDebounce
83130
===========
84131

132+
Debounce a value of type T.
133+
It stores a single value but returns after debounced time unless a new value is assigned before the debounce time elapses, in which case the process restarts.
134+
85135
**Generic parameters**
86136

87-
- ``T``
137+
- ``T`` - type of stored value
88138

89139
**Parameters**
90140

91-
- delay: number
141+
- ``value: T`` - variable to be debounced
142+
- ``delay: number`` - debounce time - amount of time in ms
92143

93144
**Returns**
94145

95-
- ``T``
146+
- ``T`` - debounced value
147+
148+
**Example**
149+
150+
.. code-block:: javascript
151+
152+
const [someValue, setValue] = useState(...)
153+
const debouncedValue = useDebounce(value, 1000)
154+
96155
97156
useDebouncePair
98157
===============
99158

159+
Debounce a pair of values of types T and U.
160+
It stores a single value but returns after debounced time unless a new value is assigned before the debounce time elapses, in which case the process restarts.
161+
162+
This function is used for debouncing multicall until enough calls are aggregated.
163+
164+
100165
**Generic parameters**
101166

102-
- ``T``
103-
- ``U``
167+
- ``T`` - type of first stored value
168+
- ``U`` - type of second stored value
104169

105170
**Parameters**
106171

107-
- ``first: T``
108-
- ``second: U``
109-
- ``delay: number``
172+
- ``first: T`` - first variable to be debounced
173+
- ``second: U`` - second variable to be debounced
174+
- ``delay: number`` - deboune time - amount of time in ms
110175

111176
**Returns**
112177

113-
- ``[T, U]``
178+
- ``[T, U]`` - debounced values
179+
180+
useEtherBalance
181+
===============
182+
183+
Returns ether balance of a given account.
184+
185+
**Parameters**
186+
187+
- ``address: string | Falsy`` - address of an account
188+
189+
**Returns**
190+
191+
- ``balance: BigNumber | undefined`` - a balance of the account which is BigNumber or *undefined* if not connected to network or address is a falsy value
192+
193+
**Example**
194+
195+
.. code-block:: javascript
196+
197+
const { account } = useEthers()
198+
const etherBalance = useEtherBalance(account)
199+
200+
return (
201+
{etherBalance && <p>Ether balance: {formatEther(etherBalance)} ETH </p>}
202+
)
203+
114204
115205
useEthers
116206
=========
@@ -134,6 +224,34 @@ Returns connection state and functions that allow to manipulate the state.
134224
useMulticallAddress
135225
===================
136226

227+
228+
229+
useTokenBalance
230+
===============
231+
232+
Returns a balance of a given token for a given address.
233+
234+
**Parameters**
235+
236+
- ``address: string | Falsy`` - address of an account
237+
- ``tokenAddress: string | Falsy`` - address of a token contract
238+
239+
**Returns**
240+
241+
- ``balance: BigNumber | undefined`` - a balance which is BigNumber or undefined if address or token is *Falsy* or not connected
242+
243+
**Example**
244+
245+
.. code-block:: javascript
246+
247+
const DAI_ADDRESSES = '0x6b175474e89094c44da98b954eedeac495271d0f'
248+
const { account } = useEthers()
249+
const daiBalance = useTokenBalance(account, chainId && DAI_ADDRESSES)
250+
251+
return (
252+
{daiBalance && <p>Dai balance: {formatUnits(daiBalance, 18)} DAI</p>}
253+
)
254+
137255
Models
138256
******
139257

@@ -162,7 +280,6 @@ Mapping of ``ChainId``'s to node URLs to use in read-only mode.
162280
}
163281
}
164282
165-
166283
**multicallAddresses**
167284

168285
**supportedChains**
@@ -174,6 +291,32 @@ List of intended supported chains. If a user tries to connect to an unsupported
174291
**pollingInterval**
175292
Polling interval for a new block.
176293

294+
ChainCall
295+
=========
296+
297+
Represents a single call on the blockchain that can be included in multicall.
298+
299+
Fields:
300+
301+
- ``address: string`` - address of a contract to call
302+
303+
- ``data: string`` - calldata of the call that encodes function call
304+
305+
ContractCall
306+
============
307+
Represents a single call to a contract that can be included in multicall.
308+
309+
Fields:
310+
311+
- ``abi: Interface`` - ABI of a contract, see `Interface <https://docs.ethers.io/v5/api/utils/abi/interface/>`_
312+
313+
- ``address: string`` - address of a contract to call
314+
315+
- ``method: string`` - function name
316+
317+
- ``args: any[]`` - arguments for the function
318+
319+
177320
Currency
178321
========
179322

docs/source/getting-started.rst

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ To get started, add following npm package :code:`@usedapp/core` to your project:
2323
npm install @usedapp/core
2424
2525
Example
26-
-----------------------
26+
-------
2727

2828
Below is a simple example:
2929

@@ -59,10 +59,12 @@ Below is a simple example:
5959
}
6060
6161
62-
Full example code is available `here <https://github.com/EthWorks/useDapp/tree/master/packages/example>`_.
62+
Example is available `here <https://usedapp-example.netlify.app/>`_ and full example code is available `here <https://github.com/EthWorks/useDapp/tree/master/packages/example>`_.
6363

64+
Connecting to a network
65+
-----------------------
6466

65-
First thing you need to do is set up **DAppPRovider** with optional config and wrap your whole App in it. You can read about config :ref:`here<config>`
67+
The first thing you need to do is set up **DAppProvider** with optional config and wrap your whole App in it. You can read about config :ref:`here<config>`
6668

6769
.. code-block:: jsx
6870
@@ -133,8 +135,3 @@ Can be used to fetch balance of ERC20 token specified by ``tokenAddress`` for pr
133135
</div>
134136
)
135137
}
136-
137-
Read-only provider
138-
------------------
139-
140-
TODO

docs/source/guide.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
Performant idiomatic React
2-
##############################
3-
4-
51
Guides
62
######
73

4+
Connecting to network
5+
*********************
6+
7+
88
Reading from blockchain
99
***********************
1010

11-
When using hooks based on useChainCall there are important considerations;
11+
When using hooks based on `useChainCall`, `useChainCalls` and `useContractCalls` there are important considerations;
1212

1313
Avoid using the result of one hook in another
1414
==================================================

0 commit comments

Comments
 (0)