Skip to content

Commit 2209923

Browse files
authored
fix: Resolve provider creation error & add fallback for wallets without wallet_getAssets support (#822)
* chore:remove unnecessary lockfile * chore: update deps * chore:rename file * allow adding donut more than balance with warning * update lock file * refactor: remove disabled state from max button for better UX
1 parent b127dde commit 2209923

File tree

5 files changed

+2544
-9219
lines changed

5 files changed

+2544
-9219
lines changed

advanced/dapps/chain-abstraction-demo/components/GiftDonutModalTrigger.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useEffect, useState } from "react";
66
import { Button } from "./ui/button";
77
import GiftDonutModal from "./GiftDonutModal";
88
import { GiftDonutModalViews } from "./gift-donut-modal-views";
9-
import { useWalletGetAssets } from "@/context/WalletAssetsProvider";
9+
import { useWalletGetAssets } from "@/hooks/useWalletGetAssets";
1010
import { useAppKitNetwork } from "@reown/appkit/react";
1111
import { supportedNetworks } from "@/data/EIP155Data";
1212

advanced/dapps/chain-abstraction-demo/components/gift-donut-modal-views/CheckoutView.tsx

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Button } from "../ui/button";
66
import Image from "next/image";
77
import React from "react";
88
import { cn } from "@/lib/utils";
9-
import { ArrowRight, ChevronRight, X } from "lucide-react";
9+
import { AlertTriangle, ArrowRight, ChevronRight, X } from "lucide-react";
1010
import CoinSVG from "../assets/CoinSVG";
1111
import NetworkSVG from "../assets/NetworkSVG";
1212
import {
@@ -39,18 +39,19 @@ function GiftDonutForm({
3939

4040
const selectedToken = giftDonutModalManager.getToken();
4141
const selectedNetwork = giftDonutModalManager.getNetwork();
42-
const tokenBalance = giftDonutModalManager.getBalanceBySymbol(
43-
selectedToken.name,
44-
);
42+
const tokenBalance = giftDonutModalManager.getBalanceBySymbol(selectedToken.name);
4543
const maxDonutPurchasable = Math.trunc(parseFloat(tokenBalance) / 1.0);
4644

45+
// Allow any count >= 0.
4746
const setDonutCount = (newCount: number) => {
4847
if (newCount < 0) return;
49-
if (newCount > maxDonutPurchasable) return;
5048
setCount(newCount);
5149
giftDonutModalManager.setDonutCount(newCount);
5250
};
5351

52+
// Check whether the selected count exceeds the available balance.
53+
const isExceeded = count > maxDonutPurchasable;
54+
5455
return (
5556
<div className={cn("flex flex-col items-start gap-4", className)}>
5657
<div className="grid grid-cols-3 items-center w-full">
@@ -63,7 +64,7 @@ function GiftDonutForm({
6364
</Button>
6465
</div>
6566
</div>
66-
<div className="flex items-center gap-2 w-full bg-primary-foreground p-4 rounded-3xl ">
67+
<div className="flex items-center gap-2 w-full bg-primary-foreground p-4 rounded-3xl">
6768
<Image src="/donut-cover.png" alt="Gift Donut" width={80} height={80} />
6869
<div className="flex flex-col gap-2 w-full h-full">
6970
<p className="text-primary font-bold">Donut #1</p>
@@ -74,7 +75,6 @@ function GiftDonutForm({
7475
variant="link"
7576
size="sm"
7677
onClick={() => setDonutCount(maxDonutPurchasable)}
77-
disabled={count >= maxDonutPurchasable}
7878
className="text-xs h-auto p-0 text-secondary hover:text-primary"
7979
>
8080
Max: {maxDonutPurchasable}
@@ -98,12 +98,12 @@ function GiftDonutForm({
9898
onClick={() => setDonutCount(count + 1)}
9999
style={{ backgroundColor: "var(--tertiary-foreground)" }}
100100
className="h-8 w-8 rounded-full p-0"
101-
disabled={count >= maxDonutPurchasable}
102101
>
103102
+
104103
</Button>
105104
</div>
106105
</div>
106+
{/* Removed inline error message block */}
107107
</div>
108108
</div>
109109
</div>
@@ -112,8 +112,7 @@ function GiftDonutForm({
112112
<div
113113
className="w-10 h-10 rounded-full flex items-center justify-center"
114114
style={{
115-
background:
116-
"var(--foreground-foreground-secondary, rgba(42, 42, 42, 1))",
115+
background: "var(--foreground-foreground-secondary, rgba(42, 42, 42, 1))",
117116
}}
118117
>
119118
<CoinSVG />
@@ -147,8 +146,7 @@ function GiftDonutForm({
147146
<div
148147
className="w-10 h-10 rounded-full flex items-center justify-center"
149148
style={{
150-
background:
151-
"var(--foreground-foreground-secondary, rgba(42, 42, 42, 1))",
149+
background: "var(--foreground-foreground-secondary, rgba(42, 42, 42, 1))",
152150
}}
153151
>
154152
<NetworkSVG />
@@ -179,28 +177,47 @@ function GiftDonutForm({
179177
</div>
180178
</div>
181179
</div>
180+
{/* Total Section Updated with Tooltip and Warning Icon */}
182181
<div className="flex justify-between w-full items-center">
183182
<p className="text-secondary">Total</p>
184-
<p className="text-md font-bold text-primary">
185-
${(count * 1.0).toFixed(2)}
186-
</p>
183+
{isExceeded ? (
184+
<TooltipProvider>
185+
<Tooltip>
186+
<TooltipTrigger asChild>
187+
<div className="flex items-center gap-2">
188+
<AlertTriangle className="h-4 w-4 text-yellow-500" />
189+
<p className="text-md font-bold text-yellow-500">
190+
${(count * 1.0).toFixed(2)}
191+
</p>
192+
</div>
193+
</TooltipTrigger>
194+
<TooltipContent className="bg-primary-foreground">
195+
<p className="text-xs text-primary">
196+
Warning: Your selected count exceeds your token balance
197+
</p>
198+
</TooltipContent>
199+
</Tooltip>
200+
</TooltipProvider>
201+
) : (
202+
<p className="text-md font-bold text-primary">
203+
${(count * 1.0).toFixed(2)}
204+
</p>
205+
)}
187206
</div>
188207
<div className="flex gap-2 w-full">
189208
<button
190209
onClick={onClose}
191210
type="button"
192211
style={{
193-
border:
194-
"1px solid var(--border-border-secondary, rgba(79, 79, 79, 1))",
212+
border: "1px solid var(--border-border-secondary, rgba(79, 79, 79, 1))",
195213
}}
196214
className="flex flex-1 text-primary items-center justify-center border-secondary rounded-lg"
197215
>
198-
Cancle
216+
Cancel
199217
</button>
200218
<Button
201219
style={{
202-
background:
203-
"var(--foreground-foreground-accent-primary-010, rgba(9, 136, 240, 0.1))",
220+
background: "var(--foreground-foreground-accent-primary-010, rgba(9, 136, 240, 0.1))",
204221
}}
205222
onClick={() => onViewChange("CheckoutReceipentAddress")}
206223
type="button"
@@ -209,9 +226,7 @@ function GiftDonutForm({
209226
>
210227
<p
211228
className="flex items-center"
212-
style={{
213-
color: "var(--text-text-accent-primary, rgba(9, 136, 240, 1))",
214-
}}
229+
style={{ color: "var(--text-text-accent-primary, rgba(9, 136, 240, 1))" }}
215230
>
216231
Next <ArrowRight className="w-4 h-4" />
217232
</p>

advanced/dapps/chain-abstraction-demo/context/WalletAssetsProvider.tsx renamed to advanced/dapps/chain-abstraction-demo/hooks/useWalletGetAssets.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,6 @@ import {
1717
} from "@/types/ERC7811";
1818
import { Capabilities } from "@/types/ERC5792";
1919

20-
interface WalletAssetsContextType {
21-
balances: TokenBalance[];
22-
isLoading: boolean;
23-
refetch: () => Promise<void>;
24-
getBalanceBySymbol: (symbol: string) => string;
25-
getBalanceByAddress: (address: `0x${string}`) => string;
26-
}
27-
const WalletAssetsContext = React.createContext<
28-
WalletAssetsContextType | undefined
29-
>(undefined);
30-
3120
async function getAssetDiscoveryCapabilities({
3221
provider,
3322
chainIdAsHex,

0 commit comments

Comments
 (0)