Skip to content

Commit 2b39709

Browse files
Casuneanu CatalinCatalin CasuneanuDhaiwat10
authored
Add a contract loading status indicator for useContract (#185)
* create a status indicator for useContract * changeset * Remove unnecessary story Co-authored-by: Catalin Casuneanu <[email protected]> Co-authored-by: Dhaiwat Pandya <[email protected]>
1 parent 582efe6 commit 2b39709

File tree

4 files changed

+14
-43
lines changed

4 files changed

+14
-43
lines changed

.changeset/short-eggs-juggle.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+
useContract status indicator

packages/hooks/src/hooks/useContract.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Contract } from 'ethers';
55
export function useContract(address: string, abi) {
66
const context = React.useContext(Web3Context);
77
const [contract, setContract] = React.useState({});
8+
const [isReady, setIsReady] = React.useState(false);
89
React.useEffect(() => {
910
if (context) {
1011
const newContract = new Contract(
@@ -21,7 +22,8 @@ export function useContract(address: string, abi) {
2122
};
2223
}, {});
2324
setContract(contractInterface);
25+
setIsReady(true);
2426
}
2527
}, [context]);
26-
return contract;
28+
return [contract, isReady];
2729
}

packages/hooks/src/stories/UseContract.stories.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const ABI = [
6060

6161
const Default = () => {
6262
const { connectWallet, disconnectWallet, connected } = useWallet();
63-
const contract = useContract(ADDRESS, ABI);
63+
const [contract, isReady] = useContract(ADDRESS, ABI);
6464
const [state, setState] = React.useState({
6565
newGreeting: '',
6666
toAddress: '',
@@ -89,7 +89,10 @@ const Default = () => {
8989
<h3>Contract Methods</h3>
9090
<Button onClick={handleGreet}>greet</Button>
9191
<Divider />
92-
<Button disabled={!state.newGreeting} onClick={handleSetGreeting}>
92+
<Button
93+
disabled={!state.newGreeting || !isReady}
94+
onClick={handleSetGreeting}
95+
>
9396
setGreeting
9497
</Button>
9598
<Input

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

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const ABI = [
7171

7272
const UsingUseContract = () => {
7373
const { connectWallet, disconnectWallet, connected } = useWallet();
74-
const contract = useContract(ADDRESS, ABI);
74+
const [contract, _] = useContract(ADDRESS, ABI);
7575
const [value, setValue] = React.useState('');
7676

7777
const greet = async () => {
@@ -123,42 +123,3 @@ export const WithUseContract = () => {
123123
</Provider>
124124
);
125125
};
126-
127-
const UsingProvider = () => {
128-
const { connectWallet, connected, disconnectWallet } = useWallet();
129-
130-
const [execute, loading, error] = useTransaction(connectWallet);
131-
const [
132-
executeDisconnectWallet,
133-
disconnectWalletLoading,
134-
disconnectWalletError
135-
] = useTransaction(disconnectWallet);
136-
137-
if (connected) {
138-
return (
139-
<div>
140-
<Button onClick={executeDisconnectWallet}>Disconnect Wallet</Button>
141-
<Text>Loading: {disconnectWalletLoading.toString()}</Text>
142-
<Text color={disconnectWalletError ? 'red' : 'black'}>
143-
Error: {disconnectWalletError ? disconnectWalletError : 'No error'}
144-
</Text>
145-
</div>
146-
);
147-
}
148-
149-
return (
150-
<div>
151-
<Button onClick={execute}>Connect Wallet</Button>
152-
<Text>Loading: {loading.toString()}</Text>
153-
<Text color={error ? 'red' : 'black'}>
154-
Error: {error ? error : 'No error'}
155-
</Text>
156-
</div>
157-
);
158-
};
159-
160-
export const ConnectWallet = () => (
161-
<Provider network={NETWORKS.mainnet}>
162-
<UsingProvider />
163-
</Provider>
164-
);

0 commit comments

Comments
 (0)