Skip to content

Commit 0c0b747

Browse files
committed
fix: respect updated amount
1 parent 337f52f commit 0c0b747

File tree

7 files changed

+54
-104
lines changed

7 files changed

+54
-104
lines changed

packages/thirdweb/src/react/core/machines/paymentMachine.ts

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ export interface PaymentMachineContext {
3636
// Flow configuration
3737
mode: PaymentMode;
3838

39-
// Target requirements (resolved in resolveRequirements state)
40-
destinationChainId?: number;
41-
destinationTokenAddress?: string;
39+
// Target requirements (resolved in init state)
4240
destinationAmount?: string;
41+
destinationToken?: Token;
4342
receiverAddress?: Address;
4443

4544
// User selections (set in methodSelection state)
@@ -67,9 +66,8 @@ export interface PaymentMachineContext {
6766
*/
6867
export type PaymentMachineEvent =
6968
| {
70-
type: "REQUIREMENTS_RESOLVED";
71-
destinationChainId: number;
72-
destinationTokenAddress: string;
69+
type: "DESTINATION_CONFIRMED";
70+
destinationToken: Token;
7371
destinationAmount: string;
7472
receiverAddress: Address;
7573
}
@@ -83,7 +81,7 @@ export type PaymentMachineEvent =
8381
| { type: "BACK" };
8482

8583
type PaymentMachineState =
86-
| "resolveRequirements"
84+
| "init"
8785
| "methodSelection"
8886
| "quote"
8987
| "preview"
@@ -98,9 +96,7 @@ export function usePaymentMachine(
9896
adapters: PaymentMachineContext["adapters"],
9997
mode: PaymentMode = "fund_wallet",
10098
) {
101-
const [currentState, setCurrentState] = useState<PaymentMachineState>(
102-
"resolveRequirements",
103-
);
99+
const [currentState, setCurrentState] = useState<PaymentMachineState>("init");
104100
const [context, setContext] = useState<PaymentMachineContext>({
105101
mode,
106102
adapters,
@@ -111,20 +107,19 @@ export function usePaymentMachine(
111107
setCurrentState((state) => {
112108
setContext((ctx) => {
113109
switch (state) {
114-
case "resolveRequirements":
115-
if (event.type === "REQUIREMENTS_RESOLVED") {
110+
case "init":
111+
if (event.type === "DESTINATION_CONFIRMED") {
116112
return {
117113
...ctx,
118-
destinationChainId: event.destinationChainId,
119-
destinationTokenAddress: event.destinationTokenAddress,
114+
destinationToken: event.destinationToken,
120115
destinationAmount: event.destinationAmount,
121116
receiverAddress: event.receiverAddress,
122117
};
123118
} else if (event.type === "ERROR_OCCURRED") {
124119
return {
125120
...ctx,
126121
currentError: event.error,
127-
retryState: "resolveRequirements",
122+
retryState: "init",
128123
};
129124
}
130125
break;
@@ -209,15 +204,15 @@ export function usePaymentMachine(
209204

210205
// State transitions
211206
switch (state) {
212-
case "resolveRequirements":
213-
if (event.type === "REQUIREMENTS_RESOLVED")
207+
case "init":
208+
if (event.type === "DESTINATION_CONFIRMED")
214209
return "methodSelection";
215210
if (event.type === "ERROR_OCCURRED") return "error";
216211
break;
217212

218213
case "methodSelection":
219214
if (event.type === "PAYMENT_METHOD_SELECTED") return "quote";
220-
if (event.type === "BACK") return "resolveRequirements";
215+
if (event.type === "BACK") return "init";
221216
if (event.type === "ERROR_OCCURRED") return "error";
222217
break;
223218

@@ -240,15 +235,15 @@ export function usePaymentMachine(
240235
break;
241236

242237
case "success":
243-
if (event.type === "RESET") return "resolveRequirements";
238+
if (event.type === "RESET") return "init";
244239
break;
245240

246241
case "error":
247242
if (event.type === "RETRY") {
248-
return context.retryState ?? "resolveRequirements";
243+
return context.retryState ?? "init";
249244
}
250245
if (event.type === "RESET") {
251-
return "resolveRequirements";
246+
return "init";
252247
}
253248
break;
254249
}

packages/thirdweb/src/react/core/utils/persist.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ export async function saveSnapshot(
3434
value: state,
3535
context: {
3636
mode: context.mode,
37-
destinationChainId: context.destinationChainId,
38-
destinationTokenAddress: context.destinationTokenAddress,
37+
destinationToken: context.destinationToken,
3938
destinationAmount: context.destinationAmount,
4039
selectedPaymentMethod: context.selectedPaymentMethod,
4140
preparedQuote: context.preparedQuote,

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

Lines changed: 30 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"use client";
22
import { useCallback, useMemo } from "react";
33
import type { Token } from "../../../../bridge/types/Token.js";
4-
import type { Chain } from "../../../../chains/types.js";
54
import type { ThirdwebClient } from "../../../../client/client.js";
65
import type { PreparedTransaction } from "../../../../transaction/prepare-transaction.js";
76
import type { Address } from "../../../../utils/address.js";
@@ -123,30 +122,6 @@ export function BridgeOrchestrator({
123122
// Use the payment machine hook
124123
const [state, send] = usePaymentMachine(adapters, uiOptions.mode);
125124

126-
// Get destination token and amount based on mode
127-
const getDestinationInfo = () => {
128-
switch (uiOptions.mode) {
129-
case "fund_wallet":
130-
return {
131-
token: uiOptions.destinationToken,
132-
amount: uiOptions.initialAmount,
133-
};
134-
case "direct_payment":
135-
return {
136-
token: uiOptions.paymentInfo.token,
137-
amount: uiOptions.paymentInfo.amount,
138-
};
139-
case "transaction":
140-
// For transaction mode, we'll need to define what token/amount to use
141-
return {
142-
token: undefined,
143-
amount: undefined,
144-
};
145-
}
146-
};
147-
148-
const destinationInfo = getDestinationInfo();
149-
150125
// Handle completion
151126
const handleComplete = useCallback(() => {
152127
onComplete?.();
@@ -198,13 +173,12 @@ export function BridgeOrchestrator({
198173

199174
// Handle requirements resolved from FundWallet and DirectPayment
200175
const handleRequirementsResolved = useCallback(
201-
(amount: string, token: Token, chain: Chain, receiverAddress: Address) => {
176+
(amount: string, token: Token, receiverAddress: Address) => {
202177
send({
203-
type: "REQUIREMENTS_RESOLVED",
204-
destinationChainId: chain.id,
205-
destinationTokenAddress: token.address,
178+
type: "DESTINATION_CONFIRMED",
179+
destinationToken: token,
180+
receiverAddress,
206181
destinationAmount: amount,
207-
receiverAddress: receiverAddress,
208182
});
209183
},
210184
[send],
@@ -222,35 +196,33 @@ export function BridgeOrchestrator({
222196
)}
223197

224198
{/* Render current screen based on state */}
225-
{state.value === "resolveRequirements" &&
226-
uiOptions.mode === "fund_wallet" && (
227-
<FundWallet
228-
token={uiOptions.destinationToken}
229-
receiverAddress={receiverAddress}
230-
initialAmount={uiOptions.initialAmount}
231-
client={client}
232-
onContinue={handleRequirementsResolved}
233-
connectOptions={connectOptions}
234-
/>
235-
)}
199+
{state.value === "init" && uiOptions.mode === "fund_wallet" && (
200+
<FundWallet
201+
token={uiOptions.destinationToken}
202+
receiverAddress={receiverAddress}
203+
initialAmount={uiOptions.initialAmount}
204+
client={client}
205+
onContinue={handleRequirementsResolved}
206+
connectOptions={connectOptions}
207+
/>
208+
)}
236209

237-
{state.value === "resolveRequirements" &&
238-
uiOptions.mode === "direct_payment" && (
239-
<DirectPayment
240-
paymentInfo={uiOptions.paymentInfo}
241-
client={client}
242-
onContinue={handleRequirementsResolved}
243-
connectOptions={connectOptions}
244-
/>
245-
)}
210+
{state.value === "init" && uiOptions.mode === "direct_payment" && (
211+
<DirectPayment
212+
paymentInfo={uiOptions.paymentInfo}
213+
client={client}
214+
onContinue={handleRequirementsResolved}
215+
connectOptions={connectOptions}
216+
/>
217+
)}
246218

247219
{state.value === "methodSelection" &&
248-
destinationInfo.token &&
249-
destinationInfo.amount && (
220+
state.context.destinationToken &&
221+
state.context.destinationAmount && (
250222
<PaymentSelection
251-
destinationToken={destinationInfo.token}
223+
destinationToken={state.context.destinationToken}
252224
client={client}
253-
destinationAmount={destinationInfo.amount}
225+
destinationAmount={state.context.destinationAmount}
254226
onPaymentMethodSelected={handlePaymentMethodSelected}
255227
onError={handleError}
256228
onBack={() => {
@@ -264,15 +236,15 @@ export function BridgeOrchestrator({
264236
{state.value === "quote" &&
265237
state.context.selectedPaymentMethod &&
266238
state.context.receiverAddress &&
267-
destinationInfo.token &&
268-
destinationInfo.amount && (
239+
state.context.destinationToken &&
240+
state.context.destinationAmount && (
269241
<QuoteLoader
270-
destinationToken={destinationInfo.token}
242+
destinationToken={state.context.destinationToken}
271243
purchaseData={purchaseData}
272244
paymentLinkId={paymentLinkId}
273245
paymentMethod={state.context.selectedPaymentMethod}
274246
receiver={state.context.receiverAddress}
275-
amount={destinationInfo.amount}
247+
amount={state.context.destinationAmount}
276248
client={client}
277249
onQuoteReceived={handleQuoteReceived}
278250
onError={handleError}

packages/thirdweb/src/react/web/ui/Bridge/DirectPayment.tsx

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"use client";
22
import type { Token } from "../../../../bridge/types/Token.js";
3-
import type { Chain } from "../../../../chains/types.js";
43
import { defineChain } from "../../../../chains/utils.js";
54
import type { ThirdwebClient } from "../../../../client/client.js";
65
import { type Address, shortenAddress } from "../../../../utils/address.js";
@@ -42,12 +41,7 @@ export interface DirectPaymentProps {
4241
/**
4342
* Called when user continues with the payment
4443
*/
45-
onContinue: (
46-
amount: string,
47-
token: Token,
48-
chain: Chain,
49-
receiverAddress: Address,
50-
) => void;
44+
onContinue: (amount: string, token: Token, receiverAddress: Address) => void;
5145

5246
/**
5347
* Connect options for wallet connection
@@ -68,7 +62,6 @@ export function DirectPayment({
6862
onContinue(
6963
paymentInfo.amount,
7064
paymentInfo.token,
71-
chain,
7265
paymentInfo.sellerAddress,
7366
);
7467
};

packages/thirdweb/src/react/web/ui/Bridge/FundWallet.tsx

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
"use client";
22
import { useState } from "react";
33
import type { Token } from "../../../../bridge/types/Token.js";
4-
import type { Chain } from "../../../../chains/types.js";
5-
import { getCachedChain } from "../../../../chains/utils.js";
64
import type { ThirdwebClient } from "../../../../client/client.js";
75
import { type Address, getAddress } from "../../../../utils/address.js";
86
import { useCustomTheme } from "../../../core/design-system/CustomThemeProvider.js";
@@ -47,12 +45,7 @@ export interface FundWalletProps {
4745
/**
4846
* Called when continue is clicked with the resolved requirements
4947
*/
50-
onContinue: (
51-
amount: string,
52-
token: Token,
53-
chain: Chain,
54-
receiverAddress: Address,
55-
) => void;
48+
onContinue: (amount: string, token: Token, receiverAddress: Address) => void;
5649

5750
/**
5851
* Connect options for wallet connection
@@ -69,7 +62,6 @@ export function FundWallet({
6962
connectOptions,
7063
}: FundWalletProps) {
7164
const [amount, setAmount] = useState(initialAmount);
72-
const chain = getCachedChain(token.chainId);
7365
const theme = useCustomTheme();
7466
const account = useActiveAccount();
7567
const receiver = receiverAddress ?? account?.address;
@@ -324,7 +316,7 @@ export function FundWallet({
324316
disabled={!isValidAmount}
325317
onClick={() => {
326318
if (isValidAmount) {
327-
onContinue(amount, token, chain, getAddress(receiver));
319+
onContinue(amount, token, getAddress(receiver));
328320
}
329321
}}
330322
style={{

packages/thirdweb/src/stories/Bridge/DirectPayment.stories.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,10 @@ const meta = {
4343
tags: ["autodocs"],
4444
args: {
4545
client: storyClient,
46-
onContinue: (amount, token, chain, receiverAddress) =>
46+
onContinue: (amount, token, receiverAddress) =>
4747
console.log("Continue with payment:", {
4848
amount,
4949
token,
50-
chain,
5150
receiverAddress,
5251
}),
5352
theme: "dark",

packages/thirdweb/src/stories/Bridge/FundWallet.stories.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ const meta = {
3636
args: {
3737
token: ETH,
3838
client: storyClient,
39-
onContinue: (amount, token, chain) => {
40-
console.log("Continue clicked:", { amount, token, chain });
41-
alert(`Continue with ${amount} ${token.symbol} on ${chain.name}`);
39+
onContinue: (amount, token, receiverAddress) => {
40+
console.log("Continue clicked:", { amount, token, receiverAddress });
41+
alert(`Continue with ${amount} ${token.symbol} to ${receiverAddress}`);
4242
},
4343
receiverAddress: "0x2247d5d238d0f9d37184d8332aE0289d1aD9991b",
4444
theme: "dark",

0 commit comments

Comments
 (0)