Skip to content

Commit 3d7fd1e

Browse files
committed
use Sell/prepare when selling
1 parent a5011af commit 3d7fd1e

File tree

6 files changed

+79
-22
lines changed

6 files changed

+79
-22
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ export function usePaymentMethods(options: {
124124
),
125125
)
126126
: sufficientBalanceQuotes;
127-
return finalQuotes;
127+
return finalQuotes.map((x) => ({
128+
...x,
129+
action: "buy",
130+
}));
128131
},
129132
queryKey: [
130133
"payment-methods",

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type PaymentMode = "fund_wallet" | "direct_payment" | "transaction";
2222
export type PaymentMethod =
2323
| {
2424
type: "wallet";
25+
action: "buy" | "sell";
2526
payerWallet: Wallet;
2627
originToken: TokenWithPrices;
2728
balance: bigint;

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,10 @@ function getBridgeParams(args: {
228228
}
229229

230230
return {
231-
amount: toUnits(amount, destinationToken.decimals),
231+
amount:
232+
paymentMethod.action === "buy"
233+
? toUnits(amount, destinationToken.decimals)
234+
: toUnits(amount, paymentMethod.originToken.decimals),
232235
client,
233236
destinationChainId: destinationToken.chainId,
234237
destinationTokenAddress: destinationToken.address,
@@ -240,7 +243,7 @@ function getBridgeParams(args: {
240243
receiver,
241244
sender:
242245
sender || paymentMethod.payerWallet.getAccount()?.address || receiver,
243-
type: "buy",
246+
type: paymentMethod.action,
244247
};
245248
}
246249
}

packages/thirdweb/src/react/web/ui/Bridge/payment-details/PaymentDetails.tsx

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,43 @@ export function PaymentDetails({
184184
: undefined,
185185
};
186186
}
187+
188+
case "sell": {
189+
const method =
190+
paymentMethod.type === "wallet" ? paymentMethod : undefined;
191+
if (!method) {
192+
// can never happen
193+
onError(new Error("Invalid payment method"));
194+
return {
195+
destinationAmount: "0",
196+
destinationToken: undefined,
197+
estimatedTime: 0,
198+
originAmount: "0",
199+
originToken: undefined,
200+
};
201+
}
202+
203+
return {
204+
destinationAmount: formatTokenAmount(
205+
preparedQuote.destinationAmount,
206+
preparedQuote.steps[preparedQuote.steps.length - 1]
207+
?.destinationToken?.decimals ?? 18,
208+
),
209+
destinationToken:
210+
preparedQuote.steps[preparedQuote.steps.length - 1]
211+
?.destinationToken,
212+
estimatedTime: preparedQuote.estimatedExecutionTimeMs,
213+
originAmount: formatTokenAmount(
214+
preparedQuote.originAmount,
215+
method.originToken.decimals,
216+
),
217+
originToken:
218+
paymentMethod.type === "wallet"
219+
? paymentMethod.originToken
220+
: undefined,
221+
};
222+
}
223+
187224
case "onramp": {
188225
const method =
189226
paymentMethod.type === "fiat" ? paymentMethod : undefined;
@@ -214,7 +251,7 @@ export function PaymentDetails({
214251
}
215252
default: {
216253
throw new Error(
217-
`Unsupported bridge prepare type: ${preparedQuote.type}`,
254+
`Unsupported bridge prepare type: ${(preparedQuote as unknown as { type: string }).type}`,
218255
);
219256
}
220257
}

packages/thirdweb/src/react/web/ui/Bridge/swap-widget/SwapWidget.tsx

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ type SwapWidgetScreen =
195195
buyToken: TokenWithPrices;
196196
sellToken: TokenWithPrices;
197197
sellTokenBalance: bigint;
198+
mode: "buy" | "sell";
198199
}
199200
| {
200201
id: "3:preview";
@@ -204,6 +205,7 @@ type SwapWidgetScreen =
204205
buyToken: TokenWithPrices;
205206
sellToken: TokenWithPrices;
206207
sellTokenBalance: bigint;
208+
mode: "buy" | "sell";
207209
}
208210
| {
209211
id: "4:execute";
@@ -213,6 +215,7 @@ type SwapWidgetScreen =
213215
buyToken: TokenWithPrices;
214216
sellToken: TokenWithPrices;
215217
sellTokenBalance: bigint;
218+
mode: "buy" | "sell";
216219
}
217220
| {
218221
id: "5:success";
@@ -272,12 +275,16 @@ function SwapWidgetContent(props: SwapWidgetProps) {
272275
}
273276

274277
if (screen.id === "2:loading-quote") {
278+
console.log("screen", screen);
275279
return (
276280
<QuoteLoader
277-
amount={toTokens(
278-
screen.quote.destinationAmount,
279-
screen.buyToken.decimals,
280-
)}
281+
amount={
282+
// if buy mode, set destination amount
283+
// if sell mode, set origin amount
284+
screen.mode === "buy"
285+
? toTokens(screen.quote.destinationAmount, screen.buyToken.decimals)
286+
: toTokens(screen.quote.originAmount, screen.sellToken.decimals)
287+
}
281288
onError={handleError}
282289
onQuoteReceived={(preparedQuote, request) => {
283290
setScreen({
@@ -302,6 +309,7 @@ function SwapWidgetContent(props: SwapWidgetProps) {
302309
payerWallet: activeWalletInfo.activeWallet,
303310
balance: screen.sellTokenBalance,
304311
originToken: screen.sellToken,
312+
action: screen.mode,
305313
}}
306314
/>
307315
);
@@ -327,6 +335,7 @@ function SwapWidgetContent(props: SwapWidgetProps) {
327335
payerWallet: activeWalletInfo.activeWallet,
328336
balance: screen.sellTokenBalance,
329337
originToken: screen.sellToken,
338+
action: screen.mode,
330339
}}
331340
preparedQuote={screen.preparedQuote}
332341
uiOptions={{
@@ -352,6 +361,7 @@ function SwapWidgetContent(props: SwapWidgetProps) {
352361
}}
353362
onCancel={props.onCancel}
354363
onComplete={(completedStatuses) => {
364+
props.onSuccess?.();
355365
setScreen({
356366
...screen,
357367
id: "5:success",
@@ -386,17 +396,19 @@ function SwapWidgetContent(props: SwapWidgetProps) {
386396
}
387397

388398
if (screen.id === "error") {
389-
<ErrorBanner
390-
client={props.client}
391-
error={screen.error}
392-
onCancel={() => {
393-
setScreen({ id: "1:swap-ui" });
394-
props.onCancel?.();
395-
}}
396-
onRetry={() => {
397-
setScreen({ id: "1:swap-ui" });
398-
}}
399-
/>;
399+
return (
400+
<ErrorBanner
401+
client={props.client}
402+
error={screen.error}
403+
onCancel={() => {
404+
setScreen({ id: "1:swap-ui" });
405+
props.onCancel?.();
406+
}}
407+
onRetry={() => {
408+
setScreen({ id: "1:swap-ui" });
409+
}}
410+
/>
411+
);
400412
}
401413

402414
return null;

packages/thirdweb/src/react/web/ui/Bridge/swap-widget/swap-ui.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ type SwapUIProps = {
6464
buyToken: TokenWithPrices;
6565
sellTokenBalance: bigint;
6666
sellToken: TokenWithPrices;
67+
mode: "buy" | "sell";
6768
},
6869
) => void;
6970
prefill:
@@ -364,10 +365,9 @@ function SwapUIBase(
364365
sellTokenBalanceQuery.data?.value &&
365366
sellTokenWithPrices?.decimals &&
366367
props.amountSelection.amount &&
368+
!!sellTokenAmount &&
367369
sellTokenBalanceQuery.data.value <
368-
Number(
369-
toUnits(props.amountSelection.amount, sellTokenWithPrices.decimals),
370-
)
370+
Number(toUnits(sellTokenAmount, sellTokenWithPrices.decimals))
371371
);
372372

373373
return (
@@ -475,6 +475,7 @@ function SwapUIBase(
475475
buyToken: buyTokenWithPrices,
476476
sellToken: sellTokenWithPrices,
477477
sellTokenBalance: sellTokenBalanceQuery.data.value,
478+
mode: props.amountSelection.type,
478479
});
479480
}
480481
}}

0 commit comments

Comments
 (0)