Skip to content

Commit 67db44c

Browse files
committed
upgrade sdk & use claim condition reasons
1 parent 62ad4f3 commit 67db44c

File tree

5 files changed

+85
-73
lines changed

5 files changed

+85
-73
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ node_modules
33
.DS_Store
44
dist/
55
esout/
6-
yarn-error.log
6+
yarn-error.log
7+
.yalc/
8+
yalc.lock

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"typescript": "^4.4.4"
3939
},
4040
"dependencies": {
41-
"@3rdweb/sdk": "^2.0.0-25",
41+
"@3rdweb/sdk": "^2.0.0-26",
4242
"@chakra-ui/icons": "^1.1.1",
4343
"@chakra-ui/react": "^1.7.2",
4444
"@emotion/react": "^11",
@@ -55,5 +55,8 @@
5555
"resolutions": {
5656
"ansi-regex": "^5.0.1",
5757
"@3rdweb/contracts": "2.0.1-36"
58+
},
59+
"alias": {
60+
"process": false
5861
}
5962
}

src/widgets/bundledrop.tsx

+42-34
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
} from "@chakra-ui/react";
2323
import { css, Global } from "@emotion/react";
2424
import { BigNumber, BigNumberish } from "ethers";
25+
import { formatUnits, parseUnits } from "ethers/lib/utils";
2526
import React, { useEffect, useMemo, useState } from "react";
2627
import ReactDOM from "react-dom";
2728
import { IoDiamondOutline } from "react-icons/io5";
@@ -31,19 +32,18 @@ import {
3132
useMutation,
3233
useQuery,
3334
} from "react-query";
34-
import { ConnectedWallet } from "../shared/connected-wallet";
3535
import { Provider, useNetwork } from "wagmi";
3636
import { ConnectWalletButton } from "../shared/connect-wallet-button";
37+
import { ConnectedWallet } from "../shared/connected-wallet";
3738
import { Footer } from "../shared/footer";
3839
import { NftCarousel } from "../shared/nft-carousel";
40+
import { parseError } from "../shared/parseError";
3941
import { DropSvg } from "../shared/svg/drop";
4042
import chakraTheme from "../shared/theme";
4143
import { fontsizeCss } from "../shared/theme/typography";
42-
import { useFormatedValue } from "../shared/tokenHooks";
4344
import { useAddress } from "../shared/useAddress";
4445
import { useConnectors } from "../shared/useConnectors";
4546
import { useSDKWithSigner } from "../shared/useSdkWithSigner";
46-
import { parseError } from "../shared/parseError";
4747

4848
function parseHugeNumber(totalAvailable: BigNumberish = 0) {
4949
const bn = BigNumber.from(totalAvailable);
@@ -82,7 +82,7 @@ interface HeaderProps extends ModuleInProps {
8282
}
8383

8484
const Header: React.FC<HeaderProps> = ({
85-
sdk,
85+
sdk,
8686
tokenAddress,
8787
activeTab,
8888
setActiveTab,
@@ -94,7 +94,7 @@ const Header: React.FC<HeaderProps> = ({
9494
const [{ data: network }] = useNetwork();
9595
const chainId = useMemo(() => network?.chain?.id, [network]);
9696

97-
const isEnabled = !!module && !!address && chainId === expectedChainId
97+
const isEnabled = !!module && !!address && chainId === expectedChainId;
9898

9999
const activeButtonProps: ButtonProps = {
100100
borderBottom: "4px solid",
@@ -176,36 +176,38 @@ const ClaimButton: React.FC<ClaimPageProps> = ({
176176
const [{ data: network }] = useNetwork();
177177
const chainId = useMemo(() => network?.chain?.id, [network]);
178178
const [claimSuccess, setClaimSuccess] = useState(false);
179+
const [quantity, setQuantity] = useState(1);
179180

180-
const isEnabled = !!module && !!address && chainId === expectedChainId
181+
const isEnabled = !!module && !!address && chainId === expectedChainId;
181182

182183
const activeClaimCondition = useQuery(
183184
["claim-condition", { tokenId }],
184185
() => module?.claimConditions.getActive(tokenId),
185186
{ enabled: isEnabled && tokenId.length > 0 },
186187
);
187188

188-
const [quantity, setQuantity] = useState(1);
189-
const priceToMint = BigNumber.from(
190-
activeClaimCondition?.data?.price || 0,
191-
).mul(quantity);
192-
const currency = activeClaimCondition?.data?.currencyAddress;
193-
const claimed = activeClaimCondition.data?.availableSupply || "0";
194-
const totalAvailable = activeClaimCondition.data?.maxQuantity?.toString() || "0";
195-
196-
const tokenModule = useMemo(() => {
197-
if (!currency || !sdk) {
198-
return undefined;
199-
}
200-
return sdk.getTokenContract(currency);
201-
}, [currency, sdk]);
189+
const claimConditionReasons = useQuery(
190+
["ineligiblereasons", { tokenId, quantity, address }],
191+
() =>
192+
module?.claimConditions.getClaimIneligibilityReasons(
193+
tokenId,
194+
quantity,
195+
address,
196+
),
197+
{ enabled: !!module && !!address },
198+
);
202199

203-
const formatedPrice = useFormatedValue(
204-
priceToMint,
205-
tokenModule,
206-
expectedChainId,
200+
const bnPrice = parseUnits(
201+
activeClaimCondition.data?.currencyMetadata.displayValue || "0",
202+
activeClaimCondition.data?.currencyMetadata.decimals,
207203
);
208204

205+
const priceToMint = bnPrice.mul(quantity);
206+
207+
const claimed = activeClaimCondition.data?.availableSupply || "0";
208+
const totalAvailable =
209+
activeClaimCondition.data?.maxQuantity?.toString() || "0";
210+
209211
const isNotSoldOut = parseInt(claimed) < parseInt(totalAvailable);
210212

211213
useEffect(() => {
@@ -244,9 +246,11 @@ const ClaimButton: React.FC<ClaimPageProps> = ({
244246
},
245247
);
246248

247-
const isLoading = activeClaimCondition.isLoading;
249+
const isLoading =
250+
activeClaimCondition.isLoading || claimConditionReasons.isLoading;
248251

249-
const canClaim = isNotSoldOut && address;
252+
const canClaim =
253+
!!isNotSoldOut && !!address && !claimConditionReasons.data?.length;
250254

251255
const quantityLimit =
252256
activeClaimCondition?.data?.quantityLimitPerTransaction || 1;
@@ -269,9 +273,7 @@ const ClaimButton: React.FC<ClaimPageProps> = ({
269273
quantityLimitBigNumber.lte(1000);
270274

271275
if (!isEnabled) {
272-
return (
273-
<ConnectWalletButton expectedChainId={expectedChainId} />
274-
)
276+
return <ConnectWalletButton expectedChainId={expectedChainId} />;
275277
}
276278

277279
return (
@@ -311,13 +313,18 @@ const ClaimButton: React.FC<ClaimPageProps> = ({
311313
{!isNotSoldOut
312314
? "Sold out"
313315
: canClaim
314-
? `Mint${
315-
priceToMint.eq(0)
316+
? `Mint${showQuantityInput ? ` ${quantity}` : ""}${
317+
activeClaimCondition.data?.price.eq(0)
316318
? " (Free)"
317-
: formatedPrice
318-
? ` (${formatedPrice})`
319+
: activeClaimCondition.data?.currencyMetadata.displayValue
320+
? ` (${formatUnits(
321+
priceToMint,
322+
activeClaimCondition.data.currencyMetadata.decimals,
323+
)} ${activeClaimCondition.data?.currencyMetadata.symbol})`
319324
: ""
320325
}`
326+
: claimConditionReasons.data?.length
327+
? claimConditionReasons.data[0]
321328
: "Minting Unavailable"}
322329
</Button>
323330
</Flex>
@@ -496,7 +503,8 @@ const DropWidget: React.FC<DropWidgetProps> = ({
496503
);
497504

498505
const claimed = activeClaimCondition.data?.availableSupply || "0";
499-
const totalAvailable = BigNumber.from(activeClaimCondition.data?.maxQuantity).toString() || "0";
506+
const totalAvailable =
507+
BigNumber.from(activeClaimCondition.data?.maxQuantity).toString() || "0";
500508

501509
const owned = useQuery(
502510
["numbers", "owned", { address }],

src/widgets/drop.tsx

+29-23
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
} from "@chakra-ui/react";
2323
import { css, Global } from "@emotion/react";
2424
import { BigNumber } from "ethers";
25+
import { formatUnits, parseUnits } from "ethers/lib/utils";
2526
import React, { useEffect, useMemo, useState } from "react";
2627
import ReactDOM from "react-dom";
2728
import { IoDiamondOutline } from "react-icons/io5";
@@ -40,7 +41,6 @@ import { parseError } from "../shared/parseError";
4041
import { DropSvg } from "../shared/svg/drop";
4142
import chakraTheme from "../shared/theme";
4243
import { fontsizeCss } from "../shared/theme/typography";
43-
import { useFormatedValue } from "../shared/tokenHooks";
4444
import { useAddress } from "../shared/useAddress";
4545
import { useConnectors } from "../shared/useConnectors";
4646
import { useSDKWithSigner } from "../shared/useSdkWithSigner";
@@ -193,10 +193,19 @@ const ClaimButton: React.FC<ClaimPageProps> = ({
193193
{ enabled: !!module },
194194
);
195195

196-
const priceToMint = BigNumber.from(claimCondition?.data?.price || 0).mul(
197-
quantity,
196+
const claimConditionReasons = useQuery(
197+
["ineligiblereasons", { quantity, address }],
198+
() =>
199+
module?.claimConditions.getClaimIneligibilityReasons(quantity, address),
200+
{ enabled: !!module && !!address },
201+
);
202+
203+
const bnPrice = parseUnits(
204+
claimCondition.data?.currencyMetadata.displayValue || "0",
205+
claimCondition.data?.currencyMetadata.decimals,
198206
);
199-
const currency = claimCondition?.data?.currencyAddress;
207+
const priceToMint = bnPrice.mul(quantity);
208+
200209
const quantityLimit = claimCondition?.data?.quantityLimitPerTransaction || 1;
201210

202211
const quantityLimitBigNumber = useMemo(() => {
@@ -209,19 +218,6 @@ const ClaimButton: React.FC<ClaimPageProps> = ({
209218
return bn;
210219
}, [quantityLimit]);
211220

212-
const tokenModule = useMemo(() => {
213-
if (!currency || !sdk) {
214-
return undefined;
215-
}
216-
return sdk.getTokenContract(currency);
217-
}, [currency, sdk]);
218-
219-
const formatedPrice = useFormatedValue(
220-
priceToMint,
221-
tokenModule,
222-
expectedChainId,
223-
);
224-
225221
const isNotSoldOut = claimed.data?.lt(totalAvailable.data || 0);
226222

227223
useEffect(() => {
@@ -260,9 +256,14 @@ const ClaimButton: React.FC<ClaimPageProps> = ({
260256
},
261257
);
262258

263-
const isLoading = totalAvailable.isLoading || claimed.isLoading;
259+
const isLoading =
260+
totalAvailable.isLoading ||
261+
claimed.isLoading ||
262+
claimCondition.isLoading ||
263+
claimConditionReasons.isLoading;
264264

265-
const canClaim = !!isNotSoldOut && !!address;
265+
const canClaim =
266+
!!isNotSoldOut && !!address && !claimConditionReasons.data?.length;
266267

267268
const showQuantityInput =
268269
canClaim &&
@@ -276,7 +277,7 @@ const ClaimButton: React.FC<ClaimPageProps> = ({
276277
return (
277278
<Stack spacing={4} align="center" w="100%">
278279
<Flex w="100%" direction={{ base: "column", md: "row" }} gap={2}>
279-
{showQuantityInput && (
280+
{showQuantityInput && !isLoading && (
280281
<NumberInput
281282
inputMode="numeric"
282283
value={quantity}
@@ -311,12 +312,17 @@ const ClaimButton: React.FC<ClaimPageProps> = ({
311312
? "Sold out"
312313
: canClaim
313314
? `Mint${showQuantityInput ? ` ${quantity}` : ""}${
314-
priceToMint.eq(0)
315+
claimCondition.data?.price.eq(0)
315316
? " (Free)"
316-
: formatedPrice
317-
? ` (${formatedPrice})`
317+
: claimCondition.data?.currencyMetadata.displayValue
318+
? ` (${formatUnits(
319+
priceToMint,
320+
claimCondition.data.currencyMetadata.decimals,
321+
)} ${claimCondition.data?.currencyMetadata.symbol})`
318322
: ""
319323
}`
324+
: claimConditionReasons.data?.length
325+
? claimConditionReasons.data[0]
320326
: "Minting Unavailable"}
321327
</Button>
322328
</Flex>

yarn.lock

+7-14
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
# yarn lockfile v1
33

44

5-
"@3rdweb/[email protected]", "@3rdweb/contracts@^2.0.1-26":
5+
66
version "2.0.1-36"
77
resolved "https://registry.yarnpkg.com/@3rdweb/contracts/-/contracts-2.0.1-36.tgz#c75c45e2e18f3d89dcb21ba3679d6e495b11e15b"
88
integrity sha512-CC8mwjmHiyh0EG9eImapSU3Gkb1oKUxyHFh4RjZEMuYVbF5vVu0uVRNDCBpllQeMzXTIW2J0v//H6H0SvKGJYQ==
99

10-
"@3rdweb/sdk@^2.0.0-25":
11-
version "2.0.0-25"
12-
resolved "https://registry.yarnpkg.com/@3rdweb/sdk/-/sdk-2.0.0-25.tgz#9094200a4d8c69c72c71ad7f1fc7a1cb2aa4468b"
13-
integrity sha512-PRTUbETDAJHNK7TVcWUuVwnrm0s9H1OWt8EPQ0Rh2lfDly1KRcEN0NFJzzxNdJ5bO/gYC9uGRB6Hsd/U83rgKA==
10+
"@3rdweb/sdk@^2.0.0-26":
11+
version "2.0.0-26"
12+
resolved "https://registry.yarnpkg.com/@3rdweb/sdk/-/sdk-2.0.0-26.tgz#eb0990e19b4649f35e4b0953bd29c760cd678000"
13+
integrity sha512-fKMDFe/0MR0PtPXX1lKWy/XWp1DUU/mTj0L7KFyjc0au5e40fCr6PLcxhE8hjULvAGNC9/7dMosksPKoCbJy8g==
1414
dependencies:
15-
"@3rdweb/contracts" "^2.0.1-26"
15+
"@3rdweb/contracts" "2.0.1-36"
1616
"@ethersproject/bignumber" "^5.5.0"
1717
"@ethersproject/bytes" "^5.4.0"
1818
"@ethersproject/constants" "^5.5.0"
@@ -22,9 +22,7 @@
2222
"@web-std/file" "^3.0.0"
2323
deep-equal "^2.0.5"
2424
eventemitter2 "^6.4.5"
25-
events "^3.3.0"
2625
form-data "^4.0.0"
27-
json2typescript "^1.5.0-rc.0"
2826
keccak256 "^1.0.6"
2927
tiny-invariant "^1.2.0"
3028
uuid "^8.3.2"
@@ -3880,7 +3878,7 @@ [email protected], eventemitter3@^4.0.7:
38803878
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-4.0.7.tgz#2de9b68f6528d5644ef5c59526a1b4a07306169f"
38813879
integrity sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw==
38823880

3883-
events@^3.0.0, events@^3.3.0:
3881+
events@^3.0.0:
38843882
version "3.3.0"
38853883
resolved "https://registry.yarnpkg.com/events/-/events-3.3.0.tgz#31a95ad0a924e2d2c419a813aeb2c4e878ea7400"
38863884
integrity sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q==
@@ -4621,11 +4619,6 @@ json-stable-stringify@^1.0.1:
46214619
dependencies:
46224620
jsonify "~0.0.0"
46234621

4624-
json2typescript@^1.5.0-rc.0:
4625-
version "1.5.0-rc.0"
4626-
resolved "https://registry.yarnpkg.com/json2typescript/-/json2typescript-1.5.0-rc.0.tgz#73f2101b438a0ca3a393c54cbcb7c0508e73313a"
4627-
integrity sha512-9v0Q6P38nV9zznaZxHRA8THp5oEGzancSUGgm8NXZSVRU/X8h/RlpP5hutP15J5sMODdx2pz9Fs/SNEpCTFAfA==
4628-
46294622
json5@^1.0.1:
46304623
version "1.0.1"
46314624
resolved "https://registry.yarnpkg.com/json5/-/json5-1.0.1.tgz#779fb0018604fa854eacbf6252180d83543e3dbe"

0 commit comments

Comments
 (0)