Skip to content

Commit 169021d

Browse files
authored
Fix loading state of useTransaction and refactor story (#146)
* Fix `loading` state of `useTransaction` and refactor story * Add changeset * Update docstrings
1 parent 89faa5f commit 169021d

File tree

3 files changed

+41
-19
lines changed

3 files changed

+41
-19
lines changed

.changeset/lemon-donkeys-battle.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+
Wait for tx to be confirmed inside `useTransaction`

packages/hooks/src/hooks/useTransaction.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ import React from 'react';
22

33
/**
44
* @dev Hook to get the loading status, error, and data of a function call.
5-
* @param method Function to call
6-
* @param args an array of arguments to pass to the function
5+
* @param method The contract function you want to call
6+
* @param args an array of arguments to pass to the function.
77
* @returns {
88
* execute: () => Promise<any>,
99
* loading: boolean,
1010
* error: null | Error,
1111
* } {
12-
* execute: On calling this method, the function is executed with the passed arguments and the loading status is set to true.
13-
* loading: this is true while the function is executing and will be false when the function has finished executing.,
14-
* error: this will be null when there is no error and in case of error, it will contain the error object.
12+
* execute: Executes the transaction.
13+
* loading: True until the the transaction is confirmed, false otherwise.
14+
* error: Contains the error object if the transaction failed, null otherwise.
1515
* }
1616
*/
1717

@@ -24,6 +24,8 @@ export function useTransaction(method, args: any[] = []) {
2424
setError(null);
2525
try {
2626
const response = await method(...args);
27+
// wait for the transaction to be confirmed
28+
await response.wait();
2729
setError(null);
2830
setLoading(false);
2931
return response;

packages/hooks/src/stories/UseTransaction.stories.tsx

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import { Button, Text, VStack, Input } from '@chakra-ui/react';
1+
import {
2+
Button,
3+
Text,
4+
VStack,
5+
Input,
6+
FormControl,
7+
FormLabel,
8+
FormErrorMessage,
9+
} from '@chakra-ui/react';
210
import React from 'react';
311
import { NETWORKS, Provider, useWallet, useContract } from '..';
412
import { useTransaction } from '../hooks';
@@ -66,28 +74,35 @@ const UsingUseContract = () => {
6674
const contract = useContract(ADDRESS, ABI);
6775
const [value, setValue] = React.useState('');
6876

69-
const [greet, greetLoading, greetError] = useTransaction(async () =>
77+
const greet = async () => {
7078
// @ts-expect-error
71-
alert(await contract.greet())
72-
);
79+
const greeting = await contract.greet();
80+
alert(greeting);
81+
};
7382

7483
// @ts-expect-error
7584
const [setGreeting, loading, error] = useTransaction(contract.setGreeting, [value]);
7685

7786
if (connected) {
7887
return (
7988
<VStack>
80-
<Button isLoading={greetLoading} onClick={greet}>
81-
Greet
82-
</Button>
83-
{greetError && <Text color='red'>Error while greeting: {greetError.message}</Text>}
89+
<Button onClick={greet}>Greet</Button>
8490
<Button onClick={disconnectWallet}>Disconnect Wallet</Button>
85-
<Text fontSize='lg'>Set Greeting</Text>
86-
<Input isDisabled={loading} value={value} onChange={e => setValue(e.target.value)} />
87-
<Button isLoading={loading} onClick={setGreeting}>
88-
Set Greeting
89-
</Button>
90-
{error && <Text color='red'>Error while setting greeting: {error.message}</Text>}
91+
<FormControl isInvalid={!!error}>
92+
<VStack>
93+
<FormLabel htmlFor='setGreeting'>Set greeting</FormLabel>
94+
<Input
95+
id='setGreeting'
96+
isDisabled={loading}
97+
value={value}
98+
onChange={e => setValue(e.target.value)}
99+
/>
100+
<Button type='submit' isLoading={loading} onClick={setGreeting}>
101+
Set Greeting
102+
</Button>
103+
<FormErrorMessage>{error && error.message}</FormErrorMessage>
104+
</VStack>
105+
</FormControl>
91106
</VStack>
92107
);
93108
}

0 commit comments

Comments
 (0)