Skip to content
This repository was archived by the owner on Aug 30, 2022. It is now read-only.

Commit 64f33bf

Browse files
authored
Jd/add wallet balance hook (#66)
* add wallet balance hook * build * v2.3.11-0 * make useAuctionWinner not throw if auction is not close yet * v2.3.11-1 * update invalidation rules * v2.3.11-2 * also invalidate balances * v2.3.11-3 * v2.3.11-4
1 parent 51c084e commit 64f33bf

13 files changed

Lines changed: 181 additions & 187 deletions

docs/react.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { useAddress } from "@thirdweb-dev/react"
1717
```
1818
|
1919
| [useAuctionWinner(contract, listingId)](./react.useauctionwinner.md) | <b><i>(BETA)</i></b> Use this to get the winner of an auction listing from your marketplace contract. |
20+
| [useBalance(tokenAddress)](./react.usebalance.md) | |
2021
| [useBidBuffer(contract)](./react.usebidbuffer.md) | <b><i>(BETA)</i></b> Use this to get the buffer in basis points between offers from your marketplace contract. |
2122
| [useBuyNow(contract)](./react.usebuynow.md) | <b><i>(BETA)</i></b> Use this to buy out an auction listing from your marketplace contract. |
2223
| [useChainId()](./react.usechainid.md) | Hook for accessing the chain ID of the network the current wallet is connected to
@@ -88,7 +89,7 @@ import { useNetworkMistmatch } from "@thirdweb-dev/react"
8889
| [useResolvedMediaType(uri)](./react.useresolvedmediatype.md) | |
8990
| [useSplit(contractAddress)](./react.usesplit.md) | Hook for getting an instance of a <code>Split</code> contract. This contract supports fund distribution to multiple parties. |
9091
| [useToken(contractAddress)](./react.usetoken.md) | Hook for getting an instance of an <code>Token</code> contract. This contract supports ERC20 compliant tokens. |
91-
| [useTokenBalance(contract, address)](./react.usetokenbalance.md) | <b><i>(BETA)</i></b> Use this to get the balance of your Token contract for a given address. |
92+
| [useTokenBalance(contract, walletAddress)](./react.usetokenbalance.md) | <b><i>(BETA)</i></b> Use this to get the balance of your Token contract for a given address. |
9293
| [useTokenSupply(contract)](./react.usetokensupply.md) | <b><i>(BETA)</i></b> Use this to get a the total supply of your Token contract. |
9394
| [useTotalCirculatingSupply(contract)](./react.usetotalcirculatingsupply.md) | <b><i>(BETA)</i></b> Use this to get a the total (minted) supply of your [NFTContract](./react.nftcontract.md)<!-- -->. |
9495
| [useUnclaimedNFTs(contract, queryParams)](./react.useunclaimednfts.md) | <b><i>(BETA)</i></b> Use this to get a list of \*unclaimed\* NFT tokens of your ERC721 Drop contract. |

docs/react.useauctionwinner.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Use this to get the winner of an auction listing from your marketplace contract.
1212
<b>Signature:</b>
1313

1414
```typescript
15-
export declare function useAuctionWinner(contract: RequiredParam<Marketplace>, listingId: RequiredParam<BigNumberish>): import("react-query").UseQueryResult<string, unknown>;
15+
export declare function useAuctionWinner(contract: RequiredParam<Marketplace>, listingId: RequiredParam<BigNumberish>): import("react-query").UseQueryResult<string | undefined, unknown>;
1616
```
1717

1818
## Parameters
@@ -24,13 +24,9 @@ export declare function useAuctionWinner(contract: RequiredParam<Marketplace>, l
2424

2525
<b>Returns:</b>
2626

27-
import("react-query").UseQueryResult&lt;string, unknown&gt;
27+
import("react-query").UseQueryResult&lt;string \| undefined, unknown&gt;
2828

29-
a response object that includes an array of listings
30-
31-
## Exceptions
32-
33-
an error if the auction is not finished
29+
a response object that includes the address of the winner of the auction or undefined if there is no winner yet
3430

3531
## Example
3632

docs/react.usebalance.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
2+
3+
[Home](./index.md) &gt; [@thirdweb-dev/react](./react.md) &gt; [useBalance](./react.usebalance.md)
4+
5+
## useBalance() function
6+
7+
<b>Signature:</b>
8+
9+
```typescript
10+
export declare function useBalance(tokenAddress?: ContractAddress): import("react-query").UseQueryResult<{
11+
symbol: string;
12+
name: string;
13+
value: import("ethers").BigNumber;
14+
decimals: number;
15+
displayValue: string;
16+
}, unknown>;
17+
```
18+
19+
## Parameters
20+
21+
| Parameter | Type | Description |
22+
| --- | --- | --- |
23+
| tokenAddress | [ContractAddress](./react.contractaddress.md) | <i>(Optional)</i> |
24+
25+
<b>Returns:</b>
26+
27+
import("react-query").UseQueryResult&lt;{ symbol: string; name: string; value: import("ethers").BigNumber; decimals: number; displayValue: string; }, unknown&gt;
28+

docs/react.usetokenbalance.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Use this to get the balance of your Token contract for a given address.
1212
<b>Signature:</b>
1313

1414
```typescript
15-
export declare function useTokenBalance(contract: RequiredParam<Erc20>, address: RequiredParam<WalletAddress>): import("react-query").UseQueryResult<{
15+
export declare function useTokenBalance(contract: RequiredParam<Erc20>, walletAddress: RequiredParam<WalletAddress>): import("react-query").UseQueryResult<{
1616
symbol: string;
1717
name: string;
1818
value: import("ethers").BigNumber;
@@ -26,7 +26,7 @@ export declare function useTokenBalance(contract: RequiredParam<Erc20>, address:
2626
| Parameter | Type | Description |
2727
| --- | --- | --- |
2828
| contract | [RequiredParam](./react.requiredparam.md)<!-- -->&lt;Erc20&gt; | an instace of a Token contract. |
29-
| address | [RequiredParam](./react.requiredparam.md)<!-- -->&lt;[WalletAddress](./react.walletaddress.md)<!-- -->&gt; | |
29+
| walletAddress | [RequiredParam](./react.requiredparam.md)<!-- -->&lt;[WalletAddress](./react.walletaddress.md)<!-- -->&gt; | |
3030

3131
<b>Returns:</b>
3232

etc/react.api.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,18 @@ export function useActiveListings(contract: RequiredParam<Marketplace>, filter?:
320320
export function useAddress(): string | undefined;
321321

322322
// @beta
323-
export function useAuctionWinner(contract: RequiredParam<Marketplace>, listingId: RequiredParam<BigNumberish>): UseQueryResult<string, unknown>;
323+
export function useAuctionWinner(contract: RequiredParam<Marketplace>, listingId: RequiredParam<BigNumberish>): UseQueryResult<string | undefined, unknown>;
324+
325+
// Warning: (ae-incompatible-release-tags) The symbol "useBalance" is marked as @public, but its signature references "ContractAddress" which is marked as @beta
326+
//
327+
// @public (undocumented)
328+
export function useBalance(tokenAddress?: ContractAddress): UseQueryResult< {
329+
symbol: string;
330+
name: string;
331+
value: BigNumber;
332+
decimals: number;
333+
displayValue: string;
334+
}, unknown>;
324335

325336
// @beta
326337
export function useBidBuffer(contract: RequiredParam<Marketplace>): UseQueryResult<BigNumber, unknown>;
@@ -892,7 +903,7 @@ export function useSplit(contractAddress?: string): Split | undefined;
892903
export function useToken(contractAddress?: string): Token | undefined;
893904

894905
// @beta
895-
export function useTokenBalance(contract: RequiredParam<Erc20>, address: RequiredParam<WalletAddress>): UseQueryResult< {
906+
export function useTokenBalance(contract: RequiredParam<Erc20>, walletAddress: RequiredParam<WalletAddress>): UseQueryResult< {
896907
symbol: string;
897908
name: string;
898909
value: BigNumber;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@thirdweb-dev/react",
3-
"version": "2.3.10",
3+
"version": "2.3.11-4",
44
"repository": {
55
"type": "git",
66
"url": "git+https://github.com:thirdweb-dev/react.git"

src/hooks/async/drop.ts

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import {
55
DropContract,
66
RequiredParam,
77
} from "../../types";
8-
import { cacheKeys, createCacheKeyWithNetwork } from "../../utils/cache-keys";
8+
import {
9+
cacheKeys,
10+
invalidateContractAndBalances,
11+
} from "../../utils/cache-keys";
912
import { useQueryWithNetwork } from "../query-utils/useQueryWithNetwork";
1013
import { useNFTs } from "./nft";
1114
import { Erc1155, NFTDrop, QueryAllParams } from "@thirdweb-dev/sdk";
@@ -177,34 +180,12 @@ export function useClaimNFT<TContract extends DropContract>(
177180
)) as ClaimNFTReturnType<TContract>;
178181
},
179182
{
180-
onSuccess: () => {
181-
return Promise.all([
182-
queryClient.invalidateQueries(
183-
createCacheKeyWithNetwork(
184-
cacheKeys.contract.nft.query.all(contractAddress),
185-
activeChainId,
186-
),
187-
),
188-
queryClient.invalidateQueries(
189-
createCacheKeyWithNetwork(
190-
cacheKeys.contract.nft.drop.totalClaimedSupply(contractAddress),
191-
activeChainId,
192-
),
193-
),
194-
queryClient.invalidateQueries(
195-
createCacheKeyWithNetwork(
196-
cacheKeys.contract.nft.drop.totalUnclaimedSupply(contractAddress),
197-
activeChainId,
198-
),
199-
),
200-
queryClient.invalidateQueries(
201-
createCacheKeyWithNetwork(
202-
cacheKeys.contract.nft.drop.getAllUnclaimed(contractAddress),
203-
activeChainId,
204-
),
205-
),
206-
]);
207-
},
183+
onSuccess: () =>
184+
invalidateContractAndBalances(
185+
queryClient,
186+
contractAddress,
187+
activeChainId,
188+
),
208189
},
209190
);
210191
}

src/hooks/async/marketplace.ts

Lines changed: 41 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { useActiveChainId } from "../../Provider";
22
import { BuyNowParams, MakeBidParams, RequiredParam } from "../../types";
3-
import { cacheKeys, createCacheKeyWithNetwork } from "../../utils/cache-keys";
3+
import {
4+
cacheKeys,
5+
invalidateContractAndBalances,
6+
} from "../../utils/cache-keys";
47
import { useQueryWithNetwork } from "../query-utils/useQueryWithNetwork";
58
import { useAddress } from "../useAddress";
69
import type {
@@ -156,8 +159,7 @@ export function useWinningBid(
156159
*
157160
* @param contract - an instace of a marketplace contract
158161
* @param listingId - the listing id to check
159-
* @returns a response object that includes an array of listings
160-
* @throws an error if the auction is not finished
162+
* @returns a response object that includes the address of the winner of the auction or undefined if there is no winner yet
161163
* @beta
162164
*/
163165
export function useAuctionWinner(
@@ -170,9 +172,19 @@ export function useAuctionWinner(
170172
contractAddress,
171173
listingId,
172174
),
173-
() => {
175+
async () => {
174176
invariant(contract, "No Contract instance provided");
175-
return contract.auction.getWinner(BigNumber.from(listingId || 0));
177+
let winner: string | undefined;
178+
try {
179+
winner = await contract.auction.getWinner(
180+
BigNumber.from(listingId || 0),
181+
);
182+
} catch (err) {
183+
if (!(err as Error)?.message?.includes("Could not find auction")) {
184+
throw err;
185+
}
186+
}
187+
return winner;
176188
},
177189
{
178190
enabled: !!contract && listingId !== undefined,
@@ -257,22 +269,12 @@ export function useCreateDirectListing(contract: RequiredParam<Marketplace>) {
257269
return await contract.direct.createListing(data);
258270
},
259271
{
260-
onSuccess: () => {
261-
return Promise.all([
262-
queryClient.invalidateQueries(
263-
createCacheKeyWithNetwork(
264-
cacheKeys.contract.marketplace.getAllListings(contractAddress),
265-
activeChainId,
266-
),
267-
),
268-
queryClient.invalidateQueries(
269-
createCacheKeyWithNetwork(
270-
cacheKeys.contract.marketplace.getActiveListings(contractAddress),
271-
activeChainId,
272-
),
273-
),
274-
]);
275-
},
272+
onSuccess: () =>
273+
invalidateContractAndBalances(
274+
queryClient,
275+
contractAddress,
276+
activeChainId,
277+
),
276278
},
277279
);
278280
}
@@ -323,22 +325,12 @@ export function useCreateAuctionListing(contract: RequiredParam<Marketplace>) {
323325
return await contract.auction.createListing(data);
324326
},
325327
{
326-
onSuccess: () => {
327-
return Promise.all([
328-
queryClient.invalidateQueries(
329-
createCacheKeyWithNetwork(
330-
cacheKeys.contract.marketplace.getAllListings(contractAddress),
331-
activeChainId,
332-
),
333-
),
334-
queryClient.invalidateQueries(
335-
createCacheKeyWithNetwork(
336-
cacheKeys.contract.marketplace.getActiveListings(contractAddress),
337-
activeChainId,
338-
),
339-
),
340-
]);
341-
},
328+
onSuccess: () =>
329+
invalidateContractAndBalances(
330+
queryClient,
331+
contractAddress,
332+
activeChainId,
333+
),
342334
},
343335
);
344336
}
@@ -389,37 +381,12 @@ export function useMakeBid(contract: RequiredParam<Marketplace>) {
389381
return await contract.auction.makeBid(data.listingId, data.bid);
390382
},
391383
{
392-
onSuccess: (_d, variables) => {
393-
return Promise.all([
394-
queryClient.invalidateQueries(
395-
createCacheKeyWithNetwork(
396-
cacheKeys.contract.marketplace.getListing(
397-
contractAddress,
398-
variables.listingId,
399-
),
400-
activeChainId,
401-
),
402-
),
403-
queryClient.invalidateQueries(
404-
createCacheKeyWithNetwork(
405-
cacheKeys.contract.marketplace.auction.getWinningBid(
406-
contractAddress,
407-
variables.listingId,
408-
),
409-
activeChainId,
410-
),
411-
),
412-
queryClient.invalidateQueries(
413-
createCacheKeyWithNetwork(
414-
cacheKeys.contract.marketplace.auction.getWinner(
415-
contractAddress,
416-
variables.listingId,
417-
),
418-
activeChainId,
419-
),
420-
),
421-
]);
422-
},
384+
onSuccess: () =>
385+
invalidateContractAndBalances(
386+
queryClient,
387+
contractAddress,
388+
activeChainId,
389+
),
423390
},
424391
);
425392
}
@@ -482,37 +449,12 @@ export function useBuyNow(contract: RequiredParam<Marketplace>) {
482449
return await contract.auction.buyoutListing(data.id);
483450
},
484451
{
485-
onSuccess: (_d, params) => {
486-
return Promise.all([
487-
queryClient.invalidateQueries(
488-
createCacheKeyWithNetwork(
489-
cacheKeys.contract.marketplace.getListing(
490-
contractAddress,
491-
params.id,
492-
),
493-
activeChainId,
494-
),
495-
),
496-
queryClient.invalidateQueries(
497-
createCacheKeyWithNetwork(
498-
cacheKeys.contract.marketplace.auction.getWinningBid(
499-
contractAddress,
500-
params.id,
501-
),
502-
activeChainId,
503-
),
504-
),
505-
queryClient.invalidateQueries(
506-
createCacheKeyWithNetwork(
507-
cacheKeys.contract.marketplace.auction.getWinner(
508-
contractAddress,
509-
params.id,
510-
),
511-
activeChainId,
512-
),
513-
),
514-
]);
515-
},
452+
onSuccess: () =>
453+
invalidateContractAndBalances(
454+
queryClient,
455+
contractAddress,
456+
activeChainId,
457+
),
516458
},
517459
);
518460
}

0 commit comments

Comments
 (0)