Skip to content

Commit b66e93a

Browse files
authored
Refactor useTransaction hook's execute fn's API (#234)
* Refactor `useTransaction` hook's `execute` fn's API * Update stories
1 parent 84d4b6d commit b66e93a

File tree

4 files changed

+22
-14
lines changed

4 files changed

+22
-14
lines changed

.changeset/tidy-goats-rule.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@web3-ui/hooks': minor
3+
---
4+
5+
You can now pass in your arguments directly to the execute function in useTransaction.

packages/hooks/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ if (isReady) {
8989

9090
### useTransaction
9191

92-
The `useTransaction` hook takes in a contract function and a list of arguments to pass to it. It returns an array of three elements:
92+
The `useTransaction` hook takes in a contract function. It returns an array of three elements:
9393

94-
- `execute`: Calling this function will execute the transaction.
94+
- `execute`: Calling this function will execute the transaction. You should pass your arguments/parameters to this function as an array.
9595

9696
- `loading`: Will be true when the transaction is executing and will be false once the transaction has been confirmed.
9797

@@ -101,14 +101,14 @@ The `useTransaction` hook takes in a contract function and a list of arguments t
101101
import { useTransaction, useContract } from '@web3-ui/hooks';
102102

103103
const greeterContract = useContract('CONTRACT_ADDRESS', 'CONTRACT_ABI');
104-
const [execute, loading, error] = useTransaction(greeter.setGreeting, [
104+
const [execute, loading, error] = useTransaction(greeter.setGreeting);
105+
106+
await execute([
105107
'Hello, world!',
106108
{
107109
value: ethers.utils.parseEther('0.1') // you can also use this for payable transactions
108110
}
109-
]);
110-
111-
execute(); // will execute the transaction
111+
]); // will execute the transaction
112112
```
113113

114114
---

packages/hooks/src/hooks/useTransaction.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ import React from 'react';
33
/**
44
* @dev Hook to get the loading status, error, and data of a function call.
55
* @param method The contract function you want to call
6-
* @param args an array of arguments to pass to the function.
76
* @returns {
8-
* execute: () => Promise<any>,
7+
* execute: (args: any[]) => Promise<any>,
98
* loading: boolean,
109
* error: null | Error,
1110
* } {
@@ -15,11 +14,13 @@ import React from 'react';
1514
* }
1615
*/
1716

18-
export function useTransaction(method, args: any[] = []) {
17+
export function useTransaction(
18+
method
19+
): [(args: any[]) => Promise<any>, boolean, any] {
1920
const [loading, setLoading] = React.useState<boolean>(false);
2021
const [error, setError] = React.useState<any>(null);
2122

22-
const execute = async () => {
23+
const execute = async (args: any[]) => {
2324
setLoading(true);
2425
setError(null);
2526
try {

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ const UsingUseContract = () => {
8181
};
8282

8383
// @ts-expect-error
84-
const [setGreeting, loading, error] = useTransaction(contract.setGreeting, [
85-
value
86-
]);
84+
const [setGreeting, loading, error] = useTransaction(contract.setGreeting);
8785

8886
if (connected) {
8987
return (
@@ -99,7 +97,11 @@ const UsingUseContract = () => {
9997
value={value}
10098
onChange={e => setValue(e.target.value)}
10199
/>
102-
<Button type="submit" isLoading={loading} onClick={setGreeting}>
100+
<Button
101+
type="submit"
102+
isLoading={loading}
103+
onClick={() => setGreeting([value])}
104+
>
103105
Set Greeting
104106
</Button>
105107
<FormErrorMessage>{error && error.message}</FormErrorMessage>

0 commit comments

Comments
 (0)