Skip to content

Commit d064801

Browse files
Guides for transactions (#187)
1 parent fabf972 commit d064801

File tree

1 file changed

+122
-3
lines changed

1 file changed

+122
-3
lines changed

docs/source/guide.rst

Lines changed: 122 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,15 @@ There is a number of useful hooks that you can use to read blockchain state:
116116

117117
- ``useBlockMeta()`` - return meta information (``timestamp`` and ``difficulty``) about most recent block mined
118118
- ``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)
119+
- ``useTokenBalance(tokenAddress, address)`` - returns balance of a given token as BigNumber for given address (or undefined)
120120
- ``useTokenAllowance(tokenAddress, ownerAddress, spenderAddress)`` - returns allowance of a given token as BigNumber for given owner and spender address pair (or undefined)
121121

122122
Sooner or later you will want to make a custom call to a smart contract. Use ``useContractCall`` and ``useContractCalls`` for that purpose.
123123
See section below on creating custom hooks.
124124

125125

126126
Custom hooks
127-
************
127+
============
128128

129129
Creating a custom hook with the use of our core hooks is straightforward, for example let’s examine the *useTokenBalance* hook.
130130

@@ -189,7 +189,7 @@ If that is too complex consider using a custom backend or `The Graph <https://th
189189
190190
191191
Testing hooks
192-
*************
192+
=============
193193
194194
Let's take ``useTokenAllowance`` as an example.
195195
@@ -281,3 +281,122 @@ Then we can check if our result is correct. ``result.current`` is a value return
281281
})
282282
283283
284+
Transactions
285+
************
286+
287+
Sending
288+
=======
289+
290+
To send transations use ``useContractFunction`` hook. Hook returns an object with two variables: ``state`` and ``send``.
291+
292+
The former represents the state of transaction. Transaction state always contains ``status``, which can be one of the following:
293+
294+
- **None** - before a transaction is created.
295+
- **Mining** - when a transaction is sent to the network, but not yet mined. In this state ``transaction: TransactionResponse`` is available.
296+
- **Success** - when a transaction has been mined successfully. In this state ``transaction: TransactionResponse`` and ``receipt: TransactionReceipt`` are available.
297+
- **Failed** - when a transaction has been mined, but ended up reverted. Again ``transaction: TransactionResponse`` and ``receipt: TransactionReceipt`` are available.
298+
- **Exception** - when a transaction hasn't started, due to the exception that was thrown before the transaction was propagated to the network. The exception can come from application/library code (e.g. unexpected exception like malformed arguments) or externally (e.g user discarded transaction in Metamask). In this state the ``errorMessage: string`` is available (as well as exception object).
299+
300+
Additionally all states except ``None``, contain ``chainId: ChainId``.
301+
302+
To send a transaction use ``send`` function returned by ``useContractFunction``.
303+
The function forwards arguments to ethers.js contract object, so that arguments map 1 to 1 with Solidity function arguments.
304+
Additionally, there can be one extra argument - `TransactionOverrides <https://docs.ethers.io/v5/api/contract/contract/#Contract-functionsCall>`_, which can be used to manipulate transaction parameters like gasPrice, nonce, etc
305+
306+
Take a look at examples below:
307+
308+
.. code-block:: javascript
309+
310+
const { state, send } = useContractFunction(contract, 'deposit', { transactionName: 'Wrap' })
311+
...
312+
send({ value })
313+
314+
315+
.. code-block:: javascript
316+
317+
const { state, send } = useContractFunction(contract, 'withdraw', { transactionName: 'Unwrap' })
318+
319+
...
320+
send(utils.parseEther(value))
321+
322+
323+
The code snippets above will wrap and unwrap Ether into WETH using Wrapped Ether `contract <https://etherscan.io/address/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2#code>`_ respectively.
324+
325+
326+
History
327+
=======
328+
329+
To access list of user's transactions (with all statuses) use ``useTransactions`` hook.
330+
Transactions are stored in local storage and the status is rechecked on every new block.
331+
332+
Take a look at example usage below:
333+
334+
.. code-block:: javascript
335+
336+
const { transactions } = useTransactions()
337+
338+
339+
Transaction has following type:
340+
341+
.. code-block:: javascript
342+
343+
export interface StoredTransaction {
344+
transaction: TransactionResponse
345+
submittedAt: number
346+
receipt?: TransactionReceipt
347+
lastCheckedBlockNumber?: number
348+
transactionName?: string
349+
}
350+
351+
352+
353+
Notifications
354+
=============
355+
356+
Additonally, you can access notifications via ``useNotifications`` hook.
357+
Notifications include information about: new transactions, transaction success or failure, as well as connection to a new wallet.
358+
359+
Take a look at example usage below:
360+
361+
.. code-block:: javascript
362+
363+
const { notifications } = useNotifications()
364+
365+
366+
``notifications`` are arrays of ``NotificationPayload``. Each can be one of the following:
367+
368+
.. code-block:: javascript
369+
370+
{
371+
type: 'walletConnected';
372+
address: string
373+
}
374+
375+
.. code-block:: javascript
376+
377+
{
378+
type: 'transactionStarted';
379+
submittedAt: number
380+
transaction: TransactionResponse;
381+
transactionName?: string
382+
}
383+
384+
.. code-block:: javascript
385+
386+
{
387+
type: 'transactionSucceed'
388+
transaction: TransactionResponse
389+
receipt: TransactionReceipt
390+
transactionName?: string
391+
}
392+
393+
.. code-block:: javascript
394+
395+
{
396+
type: 'transactionFailed'
397+
transaction: TransactionResponse
398+
receipt: TransactionReceipt
399+
transactionName?: string
400+
}
401+
402+

0 commit comments

Comments
 (0)