Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 46 additions & 31 deletions apps/playground-web/src/components/ui/TokenSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@ import {
import { shortenAddress } from "thirdweb/utils";
import { Badge } from "@/components/ui/badge";
import { Img } from "@/components/ui/Img";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { SelectWithSearch } from "@/components/ui/select-with-search";
import { useAllChainsData } from "@/hooks/chains";
import { useTokensData } from "@/hooks/useTokensData";
import type { TokenMetadata } from "@/lib/types";
Expand Down Expand Up @@ -85,12 +79,41 @@ export function TokenSelector(props: {
return value;
}, [tokens]);

const options = useMemo(() => {
return tokens.map((token) => ({
label: token.symbol,
value: `${token.chainId}:${token.address}`,
}));
}, [tokens]);

const selectedValue = props.selectedToken
? `${props.selectedToken.chainId}:${props.selectedToken.address}`
: undefined;

const searchFn = useCallback(
(option: { label: string; value: string }, searchValue: string) => {
const token = addressChainToToken.get(option.value);
if (!token) {
return false;
}

const searchLower = searchValue.toLowerCase();
return (
token.symbol.toLowerCase().includes(searchLower) ||
token.name.toLowerCase().includes(searchLower) ||
token.address.toLowerCase().includes(searchLower)
);
},
[addressChainToToken],
);

const renderTokenOption = useCallback(
(token: TokenMetadata) => {
(option: { label: string; value: string }) => {
const token = addressChainToToken.get(option.value);
if (!token) {
return option.label;
}

const resolvedSrc = token.iconUri
? replaceIpfsUrl(token.iconUri, props.client)
: fallbackChainIcon;
Expand Down Expand Up @@ -121,11 +144,13 @@ export function TokenSelector(props: {
</div>
);
},
[props.disableAddress, props.client],
[props.disableAddress, props.client, addressChainToToken],
);

return (
<Select
<SelectWithSearch
className={cn("w-full", props.className)}
closeOnSelect={true}
disabled={tokensQuery.isLoading || props.disabled}
onValueChange={(tokenAddress) => {
const token = addressChainToToken.get(tokenAddress);
Expand All @@ -134,27 +159,17 @@ export function TokenSelector(props: {
}
props.onChange(token);
}}
options={options}
overrideSearchFn={searchFn}
placeholder={
tokensQuery.isLoading
? "Loading Tokens..."
: props.placeholder || "Select Token"
}
renderOption={renderTokenOption}
searchPlaceholder="Search by Symbol, Name or Address"
showCheck={false}
value={selectedValue}
>
<SelectTrigger className={cn("w-full", props.className)}>
<SelectValue
placeholder={
tokensQuery.isLoading
? "Loading Tokens..."
: props.placeholder || "Select Token"
}
/>
</SelectTrigger>
<SelectContent>
{tokens.map((token) => {
const value = `${token.chainId}:${token.address}`;
return (
<SelectItem key={value} value={value}>
{renderTokenOption(token)}
</SelectItem>
);
})}
</SelectContent>
</Select>
/>
);
}
28 changes: 26 additions & 2 deletions packages/thirdweb/src/react/core/hooks/usePaymentMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { toTokens, toUnits } from "../../../utils/units.js";
import type { Wallet } from "../../../wallets/interfaces/wallet.js";
import type { PaymentMethod } from "../machines/paymentMachine.js";
import type { SupportedTokens } from "../utils/defaultTokens.js";
import { useActiveWallet } from "./wallets/useActiveWallet.js";

/**
Expand All @@ -33,13 +34,15 @@
client: ThirdwebClient;
payerWallet?: Wallet;
includeDestinationToken?: boolean;
supportedTokens?: SupportedTokens;
}) {
const {
destinationToken,
destinationAmount,
client,
payerWallet,
includeDestinationToken,
supportedTokens,

Check warning on line 45 in packages/thirdweb/src/react/core/hooks/usePaymentMethods.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/core/hooks/usePaymentMethods.ts#L45

Added line #L45 was not covered by tests
} = options;
const localWallet = useActiveWallet(); // TODO (bridge): get all connected wallets
const wallet = payerWallet || localWallet;
Expand Down Expand Up @@ -118,8 +121,28 @@
(a.originToken.prices.USD || 1)
);
});
// Move all sufficient balance quotes to the top
return [...sufficientBalanceQuotes, ...insufficientBalanceQuotes];

// Filter out quotes that are not included in the supportedTokens (if provided)
const tokensToInclude = supportedTokens
? Object.keys(supportedTokens).flatMap(
(c: string) =>
supportedTokens[Number(c)]?.map((t) => ({
chainId: Number(c),
address: t.address,
})) ?? [],
)
: [];
const finalQuotes = supportedTokens
? [...sufficientBalanceQuotes, ...insufficientBalanceQuotes].filter(
(q) =>
tokensToInclude.find(
(t) =>
t.chainId === q.originToken.chainId &&
t.address === q.originToken.address,
),
)
: [...sufficientBalanceQuotes, ...insufficientBalanceQuotes];
return finalQuotes;

Check warning on line 145 in packages/thirdweb/src/react/core/hooks/usePaymentMethods.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/core/hooks/usePaymentMethods.ts#L126-L145

Added lines #L126 - L145 were not covered by tests
},
queryKey: [
"payment-methods",
Expand All @@ -128,6 +151,7 @@
destinationAmount,
payerWallet?.getAccount()?.address,
includeDestinationToken,
supportedTokens,

Check warning on line 154 in packages/thirdweb/src/react/core/hooks/usePaymentMethods.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/core/hooks/usePaymentMethods.ts#L154

Added line #L154 was not covered by tests
], // 5 minutes
refetchOnWindowFocus: false,
staleTime: 5 * 60 * 1000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
type PaymentMethod,
usePaymentMachine,
} from "../../../core/machines/paymentMachine.js";
import type { SupportedTokens } from "../../../core/utils/defaultTokens.js";
import { webWindowAdapter } from "../../adapters/WindowAdapter.js";
import en from "../ConnectWallet/locale/en.js";
import type { ConnectLocale } from "../ConnectWallet/locale/types.js";
Expand Down Expand Up @@ -128,6 +129,7 @@
* @default true
*/
showThirdwebBranding?: boolean;
supportedTokens?: SupportedTokens;
}

export function BridgeOrchestrator({
Expand All @@ -144,6 +146,7 @@
presetOptions,
paymentMethods = ["crypto", "card"],
showThirdwebBranding = true,
supportedTokens,

Check warning on line 149 in packages/thirdweb/src/react/web/ui/Bridge/BridgeOrchestrator.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/Bridge/BridgeOrchestrator.tsx#L149

Added line #L149 was not covered by tests
}: BridgeOrchestratorProps) {
// Initialize adapters
const adapters = useMemo(
Expand Down Expand Up @@ -313,6 +316,7 @@
paymentMethods={paymentMethods}
receiverAddress={state.context.receiverAddress}
currency={uiOptions.currency}
supportedTokens={supportedTokens}

Check warning on line 319 in packages/thirdweb/src/react/web/ui/Bridge/BridgeOrchestrator.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/Bridge/BridgeOrchestrator.tsx#L319

Added line #L319 was not covered by tests
/>
)}

Expand Down
1 change: 1 addition & 0 deletions packages/thirdweb/src/react/web/ui/Bridge/BuyWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@
// Show normal bridge orchestrator
content = (
<BridgeOrchestrator
supportedTokens={props.supportedTokens}

Check warning on line 401 in packages/thirdweb/src/react/web/ui/Bridge/BuyWidget.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/Bridge/BuyWidget.tsx#L401

Added line #L401 was not covered by tests
client={props.client}
connectLocale={localeQuery.data}
connectOptions={props.connectOptions}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@
receiverAddress={props.seller}
showThirdwebBranding={props.showThirdwebBranding}
uiOptions={bridgeDataQuery.data.data}
supportedTokens={props.supportedTokens}

Check warning on line 372 in packages/thirdweb/src/react/web/ui/Bridge/CheckoutWidget.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/Bridge/CheckoutWidget.tsx#L372

Added line #L372 was not covered by tests
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@
// Show normal bridge orchestrator
content = (
<BridgeOrchestrator
supportedTokens={props.supportedTokens}

Check warning on line 412 in packages/thirdweb/src/react/web/ui/Bridge/TransactionWidget.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/Bridge/TransactionWidget.tsx#L412

Added line #L412 was not covered by tests
client={props.client}
connectLocale={localeQuery.data}
connectOptions={props.connectOptions}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import { useActiveWallet } from "../../../../core/hooks/wallets/useActiveWallet.js";
import { useConnectedWallets } from "../../../../core/hooks/wallets/useConnectedWallets.js";
import type { PaymentMethod } from "../../../../core/machines/paymentMachine.js";
import type { SupportedTokens } from "../../../../core/utils/defaultTokens.js";
import type { ConnectLocale } from "../../ConnectWallet/locale/types.js";
import { WalletSwitcherConnectionScreen } from "../../ConnectWallet/screens/WalletSwitcherConnectionScreen.js";
import { Container, ModalHeader } from "../../components/basic.js";
Expand Down Expand Up @@ -89,6 +90,8 @@
* @default "USD"
*/
currency?: SupportedFiatCurrency;

supportedTokens?: SupportedTokens;
}

type Step =
Expand All @@ -109,6 +112,7 @@
connectLocale,
includeDestinationToken,
paymentMethods = ["crypto", "card"],
supportedTokens,

Check warning on line 115 in packages/thirdweb/src/react/web/ui/Bridge/payment-selection/PaymentSelection.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/Bridge/payment-selection/PaymentSelection.tsx#L115

Added line #L115 was not covered by tests
feePayer,
currency,
}: PaymentSelectionProps) {
Expand Down Expand Up @@ -149,6 +153,7 @@
receiverAddress?.toLowerCase() !==
payerWallet?.getAccount()?.address?.toLowerCase(),
payerWallet,
supportedTokens,

Check warning on line 156 in packages/thirdweb/src/react/web/ui/Bridge/payment-selection/PaymentSelection.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/Bridge/payment-selection/PaymentSelection.tsx#L156

Added line #L156 was not covered by tests
});

// Handle error from usePaymentMethods
Expand Down
Loading