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
16 changes: 14 additions & 2 deletions packages/keychain/src/components/purchasenew/review/cost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ export function OnchainCostBreakdown({
isFetchingConversion,
feeEstimationError,
quantity,
isApplePaySelected,
coinbaseQuote,
isFetchingCoinbaseQuote,
} = useOnchainPurchaseContext();
const { decimals } = quote.paymentTokenMetadata;

Expand Down Expand Up @@ -214,13 +217,22 @@ export function OnchainCostBreakdown({
quote={quote}
quantity={quantity}
layerswapFees={isUsingLayerswap ? layerswapFees : undefined}
coinbaseQuote={isApplePaySelected ? coinbaseQuote : undefined}
/>
</div>
{isFetchingConversion ? (
{isFetchingConversion || isFetchingCoinbaseQuote ? (
<Spinner />
) : (
<div className="flex items-center gap-1.5">
{isUsingLayerswap ? (
{isApplePaySelected ? (
coinbaseQuote ? (
<span className="text-foreground-100">
{`$${Number(coinbaseQuote.paymentTotal.amount).toFixed(2)}`}
</span>
) : (
<span className="text-foreground-400">—</span>
)
) : isUsingLayerswap ? (
feeEstimationError ? (
<span className="text-foreground-400">—</span>
) : layerswapTotal !== null && displayToken ? (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ const MockOnchainPurchaseProvider = ({ children }: { children: ReactNode }) => {
isApplePaySelected: false,
paymentLink: undefined,
isCreatingOrder: false,
coinbaseQuote: undefined,
isFetchingCoinbaseQuote: false,
onApplePaySelect: () => {},
onCreateCoinbaseOrder: async () => {},
getTransactions: async () => [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
TooltipTrigger,
} from "@cartridge/ui";
import type { Quote } from "@/context";
import type { CoinbaseQuoteResult } from "@/hooks/starterpack";

/**
* Format bigint token amount with symbol
Expand All @@ -25,18 +26,26 @@ export const OnchainFeesTooltip = ({
quote,
quantity = 1,
layerswapFees,
coinbaseQuote,
}: {
trigger: React.ReactNode;
defaultOpen?: boolean;
quote: Quote;
quantity?: number;
layerswapFees?: string;
coinbaseQuote?: CoinbaseQuoteResult;
}) => {
const { decimals, symbol } = quote.paymentTokenMetadata;

// Format layerswap fees in USDC (6 decimals)
const formattedBridgeFee = layerswapFees
? `${(Number(layerswapFees) / Math.pow(10, 6)).toFixed(2)} USDC`
: coinbaseQuote?.layerswapFees
? `${Number(coinbaseQuote.layerswapFees.amount).toFixed(2)} ${coinbaseQuote.layerswapFees.currency}`
: null;

const formattedCoinbaseFee = coinbaseQuote?.coinbaseFee
? `${Number(coinbaseQuote.coinbaseFee.amount).toFixed(2)} ${coinbaseQuote.coinbaseFee.currency}`
: null;

return (
Expand Down Expand Up @@ -81,23 +90,29 @@ export const OnchainFeesTooltip = ({
</div>
)}
{formattedBridgeFee && (
<>
<div className="flex flex-row justify-between text-foreground-300">
<span>Bridge Fee:</span>
<span>{formattedBridgeFee}</span>
</div>
</>
<div className="flex flex-row justify-between text-foreground-300">
<span>Bridge Fee:</span>
<span>{formattedBridgeFee}</span>
</div>
)}
{formattedCoinbaseFee && (
<div className="flex flex-row justify-between text-foreground-300">
<span>Coinbase Fee:</span>
<span>{formattedCoinbaseFee}</span>
</div>
)}
<Separator className="bg-background-125" />
<div className="flex flex-row justify-between text-foreground-100 font-medium">
<span>Total:</span>
<span>
{formatTokenAmount(
quote.totalCost * BigInt(quantity) +
(layerswapFees ? BigInt(layerswapFees) : 0n),
decimals,
symbol,
)}
{coinbaseQuote?.paymentTotal
? `${Number(coinbaseQuote.paymentTotal.amount).toFixed(2)} ${coinbaseQuote.paymentTotal.currency}`
: formatTokenAmount(
quote.totalCost * BigInt(quantity) +
(layerswapFees ? BigInt(layerswapFees) : 0n),
decimals,
symbol,
)}
</span>
</div>
</TooltipContent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ const mockOnchainPurchaseValue: OnchainPurchaseContextType = {
isApplePaySelected: false,
paymentLink: undefined,
isCreatingOrder: false,
coinbaseQuote: undefined,
isFetchingCoinbaseQuote: false,
onApplePaySelect: () => {},
onCreateCoinbaseOrder: async () => {},
getTransactions: async () => [],
Expand Down
29 changes: 29 additions & 0 deletions packages/keychain/src/context/starterpack/onchain-purchase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
useCoinbase,
type TokenOption,
type CoinbaseTransactionResult,
type CoinbaseQuoteResult,
} from "@/hooks/starterpack";
import { Explorer } from "@/hooks/starterpack/layerswap";

Expand Down Expand Up @@ -80,6 +81,8 @@ export interface OnchainPurchaseContextType {
isApplePaySelected: boolean;
paymentLink: string | undefined;
isCreatingOrder: boolean;
coinbaseQuote: CoinbaseQuoteResult | undefined;
isFetchingCoinbaseQuote: boolean;

// Actions
onOnchainPurchase: () => Promise<void>;
Expand Down Expand Up @@ -205,6 +208,9 @@ export const OnchainPurchaseProvider = ({
isCreatingOrder,
createOrder: createCoinbaseOrder,
getTransactions,
getQuote: getCoinbaseQuote,
coinbaseQuote,
isFetchingQuote: isFetchingCoinbaseQuote,
} = useCoinbase({
controller,
onError: setDisplayError,
Expand Down Expand Up @@ -291,6 +297,27 @@ export const OnchainPurchaseProvider = ({
setRequestedAmount(Number(convertedPrice.amount));
}, [selectedPlatform, convertedPrice, setRequestedAmount]);

// Fetch Coinbase quote when Apple Pay is selected or quantity changes
useEffect(() => {
if (!isApplePaySelected || !onchainDetails?.quote) {
return;
}

const purchaseAmount = onchainDetails.quote.totalCost * BigInt(quantity);
const purchaseUSDCAmount = (Number(purchaseAmount) / 1_000_000).toFixed(6);

getCoinbaseQuote({
purchaseUSDCAmount,
sandbox: !isMainnet,
});
}, [
isApplePaySelected,
onchainDetails,
quantity,
isMainnet,
getCoinbaseQuote,
]);

const onOnchainPurchase = useCallback(async () => {
if (!controller || !starterpackDetails) return;

Expand Down Expand Up @@ -502,6 +529,8 @@ export const OnchainPurchaseProvider = ({
isApplePaySelected,
paymentLink,
isCreatingOrder,
coinbaseQuote,
isFetchingCoinbaseQuote,
onOnchainPurchase,
onExternalConnect,
onSendDeposit,
Expand Down
52 changes: 52 additions & 0 deletions packages/keychain/src/hooks/starterpack/coinbase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
CoinbaseOnrampTransactionsQuery,
CreateCoinbaseOnRampOrderDocument,
CreateCoinbaseOnRampOrderMutation,
CoinbaseOnRampQuoteDocument,
CoinbaseOnRampQuoteQuery,
} from "@cartridge/ui/utils/api/cartridge";
import { request } from "@/utils/graphql";
import Controller from "@/utils/controller";
Expand All @@ -13,11 +15,18 @@ export type CoinbaseOrderResult =
CreateCoinbaseOnRampOrderMutation["createCoinbaseOnrampOrder"];
export type CoinbaseTransactionResult =
CoinbaseOnrampTransactionsQuery["coinbaseOnrampTransactions"]["transactions"][number];
export type CoinbaseQuoteResult =
CoinbaseOnRampQuoteQuery["coinbaseOnrampQuote"];

export interface CreateOrderInput {
purchaseUSDCAmount: string;
}

export interface CoinbaseQuoteInput {
purchaseUSDCAmount: string;
sandbox?: boolean;
}

export interface UseCoinbaseOptions {
controller: Controller | undefined;
onError?: (error: Error) => void;
Expand All @@ -29,10 +38,13 @@ export interface UseCoinbaseReturn {
paymentLink: string | undefined;
isCreatingOrder: boolean;
orderError: Error | null;
coinbaseQuote: CoinbaseQuoteResult | undefined;
isFetchingQuote: boolean;

// Actions
createOrder: (input: CreateOrderInput) => Promise<CoinbaseOrderResult>;
getTransactions: (username: string) => Promise<CoinbaseTransactionResult[]>;
getQuote: (input: CoinbaseQuoteInput) => Promise<CoinbaseQuoteResult>;
}

const createCoinbaseOrder = async (
Expand Down Expand Up @@ -62,6 +74,22 @@ const getCoinbaseTransactions = async (
return result.coinbaseOnrampTransactions.transactions;
};

const getCoinbaseQuote = async (
input: CoinbaseQuoteInput,
): Promise<CoinbaseQuoteResult> => {
const result = await request<CoinbaseOnRampQuoteQuery>(
CoinbaseOnRampQuoteDocument,
{
input: {
purchaseUSDCAmount: input.purchaseUSDCAmount,
sandbox: input.sandbox,
},
},
);

return result.coinbaseOnrampQuote;
};

/**
* Hook for managing Coinbase onramp functionality
*/
Expand All @@ -73,6 +101,10 @@ export function useCoinbase({
const [paymentLink, setPaymentLink] = useState<string | undefined>();
const [isCreatingOrder, setIsCreatingOrder] = useState(false);
const [orderError, setOrderError] = useState<Error | null>(null);
const [coinbaseQuote, setCoinbaseQuote] = useState<
CoinbaseQuoteResult | undefined
>();
const [isFetchingQuote, setIsFetchingQuote] = useState(false);

const createOrder = useCallback(
async (input: CreateOrderInput) => {
Expand Down Expand Up @@ -114,12 +146,32 @@ export function useCoinbase({
[onError],
);

const getQuote = useCallback(
async (input: CoinbaseQuoteInput) => {
try {
setIsFetchingQuote(true);
const quote = await getCoinbaseQuote(input);
setCoinbaseQuote(quote);
return quote;
} catch (err) {
onError?.(err as Error);
throw err;
} finally {
setIsFetchingQuote(false);
}
},
[onError],
);

return {
orderId,
paymentLink,
isCreatingOrder,
orderError,
coinbaseQuote,
isFetchingQuote,
createOrder,
getTransactions,
getQuote,
};
}
1 change: 1 addition & 0 deletions packages/keychain/src/hooks/starterpack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ export type {
UseCoinbaseReturn,
CoinbaseOrderResult,
CoinbaseTransactionResult,
CoinbaseQuoteResult,
} from "./coinbase";
Loading
Loading