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
5 changes: 5 additions & 0 deletions .changeset/silly-times-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Always fetch fresh quotes before post onramp step in PayEmbed
10 changes: 10 additions & 0 deletions apps/playground-web/src/components/pay/embed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export function StyledPayEmbedPreview() {
<div className="flex flex-col items-center justify-center">
<StyledConnectButton
chains={[base, defineChain(466), arbitrum, treasure, arbitrumNova]}
accountAbstraction={{
sponsorGas: true,
chain: base,
}}
supportedTokens={{
466: [
{
Expand Down Expand Up @@ -50,6 +54,12 @@ export function StyledPayEmbedPreview() {
<PayEmbed
client={THIRDWEB_CLIENT}
theme={theme === "light" ? "light" : "dark"}
connectOptions={{
accountAbstraction: {
sponsorGas: true,
chain: base,
},
}}
payOptions={{
mode: "fund_wallet",
metadata: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { getContract } from "../../../../../../../contract/contract.js";
import { allowance } from "../../../../../../../extensions/erc20/__generated__/IERC20/read/allowance.js";
import { approve } from "../../../../../../../extensions/erc20/write/approve.js";
import type { BuyWithCryptoQuote } from "../../../../../../../pay/buyWithCrypto/getQuote.js";
import { getBuyWithCryptoQuote } from "../../../../../../../pay/buyWithCrypto/getQuote.js";
import type { BuyWithCryptoStatus } from "../../../../../../../pay/buyWithCrypto/getStatus.js";
import type { BuyWithFiatQuote } from "../../../../../../../pay/buyWithFiat/getQuote.js";
import {
Expand All @@ -17,6 +17,7 @@
type OnRampStep,
getOnRampSteps,
} from "../../../../../../../pay/buyWithFiat/isSwapRequiredPostOnramp.js";
import type { PayTokenInfo } from "../../../../../../../pay/utils/commonTypes.js";
import { sendBatchTransaction } from "../../../../../../../transaction/actions/send-batch-transaction.js";
import { sendTransaction } from "../../../../../../../transaction/actions/send-transaction.js";
import type { WaitForReceiptOptions } from "../../../../../../../transaction/actions/wait-for-tx-receipt.js";
Expand All @@ -28,7 +29,6 @@
import { isSmartWallet } from "../../../../../../../wallets/smart/is-smart-wallet.js";
import { spacing } from "../../../../../../core/design-system/index.js";
import { useChainName } from "../../../../../../core/hooks/others/useChainQuery.js";
import { useBuyWithCryptoQuote } from "../../../../../../core/hooks/pay/useBuyWithCryptoQuote.js";
import { useBuyWithCryptoStatus } from "../../../../../../core/hooks/pay/useBuyWithCryptoStatus.js";
import { useBuyWithFiatStatus } from "../../../../../../core/hooks/pay/useBuyWithFiatStatus.js";
import { useConnectedWallets } from "../../../../../../core/hooks/wallets/useConnectedWallets.js";
Expand Down Expand Up @@ -319,20 +319,6 @@
// Get quote for current swap/bridge step if needed
const previousStep = onRampSteps[currentStepIndex - 1];
const currentStep = onRampSteps[currentStepIndex];
const swapQuoteQuery = useBuyWithCryptoQuote(
previousStep && currentStep
? {
fromChainId: previousStep.token.chainId,
fromTokenAddress: previousStep.token.tokenAddress,
toAmount: currentStep.amount,
toChainId: currentStep.token.chainId,
toTokenAddress: currentStep.token.tokenAddress,
fromAddress: props.payer.account.address,
toAddress: props.payer.account.address,
client: props.client,
}
: undefined,
);

// Handle swap execution
const swapMutation = useSwapMutation({
Expand Down Expand Up @@ -375,9 +361,9 @@
status = "completed";
} else if (index === currentStepIndex) {
// Current step - could be swap or bridge
if (swapQuoteQuery.isLoading || swapMutation.isPending) {
if (swapMutation.isPending) {

Check warning on line 364 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx#L364

Added line #L364 was not covered by tests
status = "pending";
} else if (swapQuoteQuery.error || swapMutation.error) {
} else if (swapMutation.error) {

Check warning on line 366 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx#L366

Added line #L366 was not covered by tests
status = "failed";
} else if (swapTxHash) {
status = swapStatus;
Expand Down Expand Up @@ -418,11 +404,13 @@
type: "fiat",
intentId: props.quote.intentId,
});
} else if (swapQuoteQuery.data && !swapTxHash) {
} else if (previousStep && currentStep && !swapTxHash) {

Check warning on line 407 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx#L407

Added line #L407 was not covered by tests
// Execute swap/bridge
try {
const result = await swapMutation.mutateAsync({
quote: swapQuoteQuery.data,
fromToken: previousStep.token,
toToken: currentStep.token,
amount: currentStep.amount,

Check warning on line 413 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx#L411-L413

Added lines #L411 - L413 were not covered by tests
});
setSwapTxHash({
hash: result.transactionHash,
Expand All @@ -435,23 +423,22 @@
// retry the quote step
setSwapTxHash(undefined);
swapMutation.reset();
await swapQuoteQuery.refetch();
}
}, [
isDone,
currentStepIndex,
swapQuoteQuery.data,
swapTxHash,
props.quote,
props.onDone,
swapMutation,
props.theme,
isFailed,
swapQuoteQuery.refetch,
swapMutation.reset,
props.client,
props.payer.account.address,
props.payer.wallet.id,
currentStep,
previousStep,

Check warning on line 441 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx#L440-L441

Added lines #L440 - L441 were not covered by tests
]);

// Auto-progress effect
Expand All @@ -467,15 +454,13 @@
!isFailed &&
currentStepIndex > 0 &&
currentStepIndex < onRampSteps.length &&
swapQuoteQuery.data &&
!swapTxHash
) {
handleContinue();
}
}, [
props.isAutoMode,
currentStepIndex,
swapQuoteQuery.data,
swapTxHash,
onRampSteps.length,
handleContinue,
Expand Down Expand Up @@ -643,8 +628,24 @@
}) {
const queryClient = useQueryClient();
return useMutation({
mutationFn: async (input: { quote: BuyWithCryptoQuote }) => {
const { quote } = input;
mutationFn: async (input: {

Check warning on line 631 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx#L631

Added line #L631 was not covered by tests
fromToken: PayTokenInfo;
toToken: PayTokenInfo;
amount: string;
}) => {
const { fromToken, toToken, amount } = input;

Check warning on line 636 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx#L635-L636

Added lines #L635 - L636 were not covered by tests
// always get a fresh quote before executing
const quote = await getBuyWithCryptoQuote({
fromChainId: fromToken.chainId,
fromTokenAddress: fromToken.tokenAddress,
toAmount: amount,
toChainId: toToken.chainId,
toTokenAddress: toToken.tokenAddress,
fromAddress: props.payer.account.address,
toAddress: props.payer.account.address,
client: props.client,
});

Check warning on line 647 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx#L638-L647

Added lines #L638 - L647 were not covered by tests

const canBatch = props.payer.account.sendBatchTransaction;
const tokenContract = getContract({
client: props.client,
Expand Down