@b3dotfun/sdk 0.0.5-alpha.5 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/dist/cjs/anyspend/constants/index.js +2 -2
  2. package/dist/cjs/anyspend/react/components/AnySpend.js +2 -2
  3. package/dist/cjs/anyspend/react/components/index.d.ts +5 -7
  4. package/dist/cjs/anyspend/react/components/index.js +11 -15
  5. package/dist/cjs/anyspend/react/hooks/useAnyspendCreateOnrampOrder.d.ts +0 -1
  6. package/dist/cjs/anyspend/react/hooks/useAnyspendCreateOnrampOrder.js +3 -4
  7. package/dist/cjs/anyspend/react/hooks/useAnyspendOrderAndTransactions.d.ts +0 -10
  8. package/dist/cjs/anyspend/react/hooks/useAnyspendOrderHistory.d.ts +0 -10
  9. package/dist/cjs/anyspend/services/anyspend.d.ts +1 -7
  10. package/dist/cjs/anyspend/services/anyspend.js +2 -3
  11. package/dist/cjs/anyspend/types/order.d.ts +0 -18
  12. package/dist/cjs/anyspend/types/order.js +0 -1
  13. package/dist/cjs/anyspend/types/req-res/createOrder.d.ts +0 -50
  14. package/dist/cjs/anyspend/types/req-res/createOrder.js +0 -1
  15. package/dist/cjs/anyspend/types/req-res/getOrderAndTransactions.d.ts +0 -35
  16. package/dist/cjs/anyspend/types/req-res/getOrderHistory.d.ts +0 -25
  17. package/dist/cjs/global-account/react/hooks/useTokenFromUrl.d.ts +1 -5
  18. package/dist/cjs/global-account/react/hooks/useTokenFromUrl.js +13 -74
  19. package/dist/cjs/shared/constants/chains/supported.d.ts +3 -13
  20. package/dist/cjs/shared/constants/chains/supported.js +9 -15
  21. package/dist/esm/anyspend/constants/index.js +2 -2
  22. package/dist/esm/anyspend/react/components/AnySpend.js +2 -2
  23. package/dist/esm/anyspend/react/components/index.d.ts +5 -7
  24. package/dist/esm/anyspend/react/components/index.js +5 -7
  25. package/dist/esm/anyspend/react/hooks/useAnyspendCreateOnrampOrder.d.ts +0 -1
  26. package/dist/esm/anyspend/react/hooks/useAnyspendCreateOnrampOrder.js +4 -5
  27. package/dist/esm/anyspend/react/hooks/useAnyspendOrderAndTransactions.d.ts +0 -10
  28. package/dist/esm/anyspend/react/hooks/useAnyspendOrderHistory.d.ts +0 -10
  29. package/dist/esm/anyspend/services/anyspend.d.ts +1 -7
  30. package/dist/esm/anyspend/services/anyspend.js +2 -3
  31. package/dist/esm/anyspend/types/order.d.ts +0 -18
  32. package/dist/esm/anyspend/types/order.js +0 -1
  33. package/dist/esm/anyspend/types/req-res/createOrder.d.ts +0 -50
  34. package/dist/esm/anyspend/types/req-res/createOrder.js +0 -1
  35. package/dist/esm/anyspend/types/req-res/getOrderAndTransactions.d.ts +0 -35
  36. package/dist/esm/anyspend/types/req-res/getOrderHistory.d.ts +0 -25
  37. package/dist/esm/global-account/react/hooks/useTokenFromUrl.d.ts +1 -5
  38. package/dist/esm/global-account/react/hooks/useTokenFromUrl.js +13 -73
  39. package/dist/esm/shared/constants/chains/supported.d.ts +3 -13
  40. package/dist/esm/shared/constants/chains/supported.js +8 -13
  41. package/dist/styles/index.css +1 -1
  42. package/dist/types/anyspend/react/components/index.d.ts +5 -7
  43. package/dist/types/anyspend/react/hooks/useAnyspendCreateOnrampOrder.d.ts +0 -1
  44. package/dist/types/anyspend/react/hooks/useAnyspendOrderAndTransactions.d.ts +0 -10
  45. package/dist/types/anyspend/react/hooks/useAnyspendOrderHistory.d.ts +0 -10
  46. package/dist/types/anyspend/services/anyspend.d.ts +1 -7
  47. package/dist/types/anyspend/types/order.d.ts +0 -18
  48. package/dist/types/anyspend/types/req-res/createOrder.d.ts +0 -50
  49. package/dist/types/anyspend/types/req-res/getOrderAndTransactions.d.ts +0 -35
  50. package/dist/types/anyspend/types/req-res/getOrderHistory.d.ts +0 -25
  51. package/dist/types/global-account/react/hooks/useTokenFromUrl.d.ts +1 -5
  52. package/dist/types/shared/constants/chains/supported.d.ts +3 -13
  53. package/package.json +3 -3
  54. package/src/anyspend/constants/index.ts +4 -2
  55. package/src/anyspend/react/components/AnySpend.tsx +2 -2
  56. package/src/anyspend/react/components/index.ts +5 -7
  57. package/src/anyspend/react/hooks/useAnyspendCreateOnrampOrder.ts +4 -7
  58. package/src/anyspend/services/anyspend.ts +2 -5
  59. package/src/anyspend/types/order.ts +0 -1
  60. package/src/anyspend/types/req-res/createOrder.ts +0 -1
  61. package/src/global-account/react/hooks/useTokenFromUrl.tsx +13 -95
  62. package/src/shared/constants/chains/supported.ts +8 -23
  63. package/dist/cjs/anyspend/react/components/webview/WebviewOnrampOrderStatus.d.ts +0 -6
  64. package/dist/cjs/anyspend/react/components/webview/WebviewOnrampOrderStatus.js +0 -37
  65. package/dist/cjs/anyspend/react/components/webview/WebviewOnrampPayment.d.ts +0 -12
  66. package/dist/cjs/anyspend/react/components/webview/WebviewOnrampPayment.js +0 -173
  67. package/dist/cjs/global-account/react/hooks/useB3EnsName.d.ts +0 -9
  68. package/dist/cjs/global-account/react/hooks/useB3EnsName.js +0 -39
  69. package/dist/cjs/shared/generated/coingecko-chains.json +0 -1072
  70. package/dist/esm/anyspend/react/components/webview/WebviewOnrampOrderStatus.d.ts +0 -6
  71. package/dist/esm/anyspend/react/components/webview/WebviewOnrampOrderStatus.js +0 -31
  72. package/dist/esm/anyspend/react/components/webview/WebviewOnrampPayment.d.ts +0 -12
  73. package/dist/esm/anyspend/react/components/webview/WebviewOnrampPayment.js +0 -167
  74. package/dist/esm/global-account/react/hooks/useB3EnsName.d.ts +0 -9
  75. package/dist/esm/global-account/react/hooks/useB3EnsName.js +0 -32
  76. package/dist/esm/shared/generated/coingecko-chains.json +0 -1072
  77. package/dist/types/anyspend/react/components/webview/WebviewOnrampOrderStatus.d.ts +0 -6
  78. package/dist/types/anyspend/react/components/webview/WebviewOnrampPayment.d.ts +0 -12
  79. package/dist/types/global-account/react/hooks/useB3EnsName.d.ts +0 -9
  80. package/src/anyspend/README.md +0 -626
  81. package/src/anyspend/react/components/webview/WebviewOnrampOrderStatus.tsx +0 -120
  82. package/src/anyspend/react/components/webview/WebviewOnrampPayment.tsx +0 -369
  83. package/src/global-account/react/hooks/useB3EnsName.ts +0 -45
  84. package/src/shared/generated/coingecko-chains.json +0 -1072
  85. /package/dist/cjs/{shared/generated → generated}/chain-networks.json +0 -0
  86. /package/dist/esm/{shared/generated → generated}/chain-networks.json +0 -0
  87. /package/src/{shared/generated → generated}/chain-networks.json +0 -0
@@ -1,8 +1,6 @@
1
1
  "use client";
2
2
 
3
3
  import { Token } from "@b3dotfun/sdk/anyspend";
4
- import { getCoingeckoChainInfo } from "@b3dotfun/sdk/shared/constants/chains/supported";
5
- import { useQuery } from "@tanstack/react-query";
6
4
  import { useSearchParams } from "next/navigation";
7
5
 
8
6
  interface UseTokenFromUrlOptions {
@@ -16,113 +14,33 @@ interface UseTokenFromUrlOptions {
16
14
  */
17
15
  prefix: string;
18
16
  }
19
- interface TokenInfo {
20
- data: {
21
- attributes: {
22
- address: string;
23
- name: string;
24
- symbol: string;
25
- decimals: number;
26
- image_url: string;
27
- };
28
- };
29
- }
30
-
31
- async function fetchTokenInfo(network: string, address: string): Promise<TokenInfo> {
32
- const response = await fetch("https://api.b3.fun/tokens", {
33
- method: "POST",
34
- headers: {
35
- "Content-Type": "application/json",
36
- "X-Service-Method": "getCoinGeckoTokenInfo"
37
- },
38
- body: JSON.stringify({
39
- network,
40
- address
41
- })
42
- });
43
-
44
- if (!response.ok) {
45
- throw new Error("Failed to fetch token info");
46
- }
47
-
48
- return response.json();
49
- }
50
17
 
51
18
  /**
52
- * Hook to parse token data from URL parameters and fetch additional token info.
19
+ * Hook to parse token data from URL parameters.
53
20
  * Looks for parameters: [prefix]Currency
54
21
  */
55
22
  export function useTokenFromUrl({ defaultToken, prefix }: UseTokenFromUrlOptions): Token {
56
23
  const searchParams = useSearchParams();
57
24
 
58
- // Get parameters from URL
59
- const currencyParam = searchParams?.get(`${prefix}Currency`);
60
- const chainIdParam = searchParams?.get(`${prefix}ChainId`);
61
-
62
- // Determine if we should fetch token info
63
- const shouldFetchToken = Boolean(
64
- currencyParam && chainIdParam && currencyParam.toLowerCase() !== defaultToken.address.toLowerCase()
65
- );
66
-
67
- // Determine network based on chainId
68
- const network = chainIdParam ? getCoingeckoChainInfo(Number(chainIdParam)).coingecko_id : "";
69
-
70
- const { data: tokenInfo, isError } = useQuery({
71
- queryKey: ["tokenInfo", network, currencyParam],
72
- queryFn: () => fetchTokenInfo(network, currencyParam!),
73
- enabled: shouldFetchToken,
74
- staleTime: Infinity,
75
- gcTime: Infinity
76
- });
77
-
78
- // Return default token if no params or same as default
79
- if (!shouldFetchToken) {
25
+ if (!searchParams) {
80
26
  return defaultToken;
81
27
  }
82
28
 
83
- // Return basic token info if API call fails or while loading
84
- if (isError || !tokenInfo) {
85
- return {
86
- ...defaultToken,
87
- address: currencyParam!,
88
- chainId: Number(chainIdParam)
89
- };
29
+ // Get parameters from URL
30
+ const currencyParam = searchParams.get(`${prefix}Currency`);
31
+ const chainIdParam = searchParams.get(`${prefix}ChainId`);
32
+ if (!currencyParam || !chainIdParam) {
33
+ return defaultToken;
90
34
  }
91
35
 
92
- // Return enhanced token with API data
93
- return {
94
- address: tokenInfo.data.attributes.address,
95
- chainId: Number(chainIdParam),
96
- name: tokenInfo.data.attributes.name,
97
- symbol: tokenInfo.data.attributes.symbol,
98
- decimals: tokenInfo.data.attributes.decimals,
99
- metadata: {
100
- logoURI: tokenInfo.data.attributes.image_url
101
- }
102
- };
103
- }
104
-
105
- export function useTokenFromAddress({ address, chainId }: { address: string; chainId: number }): Token | undefined {
106
- const { data: tokenInfo, isError } = useQuery({
107
- queryKey: ["tokenInfo", address, chainId],
108
- queryFn: () => fetchTokenInfo(getCoingeckoChainInfo(chainId).coingecko_id, address),
109
- enabled: Boolean(address),
110
- staleTime: Infinity,
111
- gcTime: Infinity
112
- });
113
-
114
- if (isError || !tokenInfo) {
115
- return undefined;
36
+ // If the currency is the same as the default token, return that
37
+ if (currencyParam.toLowerCase() === defaultToken.address.toLowerCase()) {
38
+ return defaultToken;
116
39
  }
117
40
 
118
41
  return {
119
- address,
120
- chainId,
121
- name: tokenInfo?.data.attributes.name || "",
122
- symbol: tokenInfo?.data.attributes.symbol || "",
123
- decimals: tokenInfo?.data.attributes.decimals || 18,
124
- metadata: {
125
- logoURI: tokenInfo?.data.attributes.image_url
126
- }
42
+ ...defaultToken,
43
+ address: currencyParam,
44
+ chainId: Number(chainIdParam)
127
45
  };
128
46
  }
@@ -1,14 +1,10 @@
1
1
  import type { ChainNetworks } from "@b3dotfun/sdk/global-account/types/chain-networks";
2
2
  import { toThirdwebChain, toViemChain } from "@b3dotfun/sdk/shared/utils/chain-transformers";
3
- import invariant from "invariant";
4
-
5
3
  import type { Chain as ThirdwebChain } from "thirdweb";
6
4
  // Import the JSON directly
7
5
  // @ts-ignore
8
- import chainNetworksJSON from "../../generated/chain-networks.json" with { type: "json" };
9
- // @ts-ignore
10
- import coingeckoChainsJSON from "../../generated/coingecko-chains.json" with { type: "json" };
11
-
6
+ import chainNetworksJSON from "../../../generated/chain-networks.json" with { type: "json" };
7
+ import invariant from "invariant";
12
8
  const chainNetworks = chainNetworksJSON as ChainNetworks[];
13
9
 
14
10
  // Convert custom chains to Viem format
@@ -31,26 +27,15 @@ export const supportedChainsTW: ThirdwebChain[] = [
31
27
  // Original format from chain-networks.json
32
28
  export const supportedChainNetworks = chainNetworks;
33
29
 
34
- // CoinGecko chain mapping
35
- export const coingeckoChains = coingeckoChainsJSON as Record<
36
- number,
37
- {
38
- coingecko_id: string;
39
- name: string;
40
- native_coin_id: string;
41
- }
42
- >;
43
- // Helper function to get CoinGecko chain info
44
- export function getCoingeckoChainInfo(chainId: number) {
45
- return coingeckoChains[chainId];
46
- }
30
+ export const b3Mainnet = supportedChains.find(chain => chain.id === 8333)!;
31
+ invariant(b3Mainnet, "B3 mainnet chain not found in supported chains");
32
+ export const b3Testnet = supportedChains.find(chain => chain.id === 1993)!;
33
+ invariant(b3Testnet, "B3 testnet chain not found in supported chains");
34
+ export const baseMainnet = supportedChains.find(chain => chain.id === 8453);
35
+ invariant(baseMainnet, "Base mainnet chain not found in supported chains");
47
36
 
48
37
  export const b3MainnetThirdWeb: ThirdwebChain = supportedChainsTW.find(chain => chain.id === 8333)!;
49
38
  invariant(b3MainnetThirdWeb, "B3 mainnet chain not found in supported chains TW");
50
39
 
51
40
  export const b3TestnetThirdWeb: ThirdwebChain = supportedChainsTW.find(chain => chain.id === 1993)!;
52
41
  invariant(b3TestnetThirdWeb, "B3 testnet chain not found in supported chains TW");
53
-
54
- export const b3Mainnet = supportedChains.find(chain => chain.id === 8333);
55
- export const b3Testnet = supportedChains.find(chain => chain.id === 1993);
56
- export const baseMainnet = supportedChains.find(chain => chain.id === 8453);
@@ -1,6 +0,0 @@
1
- import { Order } from "../../../../anyspend";
2
- interface WebviewOnrampOrderStatusProps {
3
- order: Order;
4
- }
5
- export declare function WebviewOnrampOrderStatus({ order }: WebviewOnrampOrderStatusProps): import("react/jsx-runtime").JSX.Element;
6
- export {};
@@ -1,37 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.WebviewOnrampOrderStatus = WebviewOnrampOrderStatus;
7
- const jsx_runtime_1 = require("react/jsx-runtime");
8
- const anyspend_1 = require("../../../../anyspend");
9
- const react_1 = require("../../../../global-account/react");
10
- const centerTruncate_1 = __importDefault(require("../../../../shared/utils/centerTruncate"));
11
- const lucide_react_1 = require("lucide-react");
12
- const sonner_1 = require("sonner");
13
- const viem_1 = require("viem");
14
- function WebviewOnrampOrderStatus({ order }) {
15
- console.log(order.status);
16
- const isPending = order.status === anyspend_1.OrderStatus.WaitingStripePayment ||
17
- order.status === anyspend_1.OrderStatus.ScanningDepositTransaction ||
18
- order.status === anyspend_1.OrderStatus.SendingTokenFromVault ||
19
- order.status === anyspend_1.OrderStatus.ObtainToken ||
20
- order.status === anyspend_1.OrderStatus.Relay;
21
- const isExecuted = order.status === anyspend_1.OrderStatus.Executed;
22
- const isFailed = order.status === anyspend_1.OrderStatus.Failure;
23
- const isRefunded = order.status === anyspend_1.OrderStatus.Refunded;
24
- // Get token metadata
25
- const { data: tokenMetadata } = (0, react_1.useTokenData)(order.metadata.dstToken.chainId, order.metadata.dstToken.address);
26
- // Only show expected amount for swap orders
27
- const expectedAmount = order.type === anyspend_1.OrderType.Swap
28
- ? Number((0, viem_1.formatUnits)(BigInt(order.payload.expectedDstAmount), order.metadata.dstToken.decimals)).toFixed(2)
29
- : null;
30
- // Use token metadata logo if available
31
- const tokenLogoUrl = tokenMetadata?.logoURI || order.metadata.dstToken.metadata?.logoURI;
32
- const handleCopy = (text, label) => {
33
- navigator.clipboard.writeText(text);
34
- sonner_1.toast.success(`${label} copied to clipboard`);
35
- };
36
- return ((0, jsx_runtime_1.jsxs)("div", { className: "mx-auto flex w-full max-w-[460px] flex-col gap-6", children: [(0, jsx_runtime_1.jsx)("div", { className: "flex justify-center", children: (0, jsx_runtime_1.jsxs)(react_1.Badge, { variant: "default", className: "flex items-center gap-3 px-4 py-2 text-base", children: [isPending && (0, jsx_runtime_1.jsx)(lucide_react_1.Loader2, { className: "h-4 w-4 animate-spin" }), isExecuted && (0, jsx_runtime_1.jsx)(lucide_react_1.CheckIcon, { className: "h-4 w-4 text-green-500" }), isFailed && (0, jsx_runtime_1.jsx)(lucide_react_1.ExternalLink, { className: "h-4 w-4 text-red-500" }), (0, jsx_runtime_1.jsxs)("span", { children: [isPending && "Processing Payment...", isExecuted && "Payment Complete", isFailed && "Payment Failed", isRefunded && "Payment Refunded"] })] }) }), (0, jsx_runtime_1.jsx)("div", { className: "overflow-hidden rounded-xl bg-white", children: (0, jsx_runtime_1.jsxs)("div", { className: "px-6 py-4", children: [(0, jsx_runtime_1.jsx)("h2", { className: "mb-4 text-lg font-semibold", children: "Order Summary" }), (0, jsx_runtime_1.jsxs)("div", { className: "flex flex-col divide-y", children: [(0, jsx_runtime_1.jsxs)("div", { className: "flex items-center justify-between py-3", children: [(0, jsx_runtime_1.jsx)("span", { className: "text-gray-600", children: "Amount Paid" }), (0, jsx_runtime_1.jsxs)("span", { className: "text-lg font-semibold", children: ["$", Number((0, viem_1.formatUnits)(BigInt(order.srcAmount), order.metadata.srcToken.decimals)).toFixed(2)] })] }), order.type === anyspend_1.OrderType.Swap && expectedAmount && ((0, jsx_runtime_1.jsxs)("div", { className: "flex items-center justify-between py-3", children: [(0, jsx_runtime_1.jsx)("span", { className: "text-gray-600", children: "Est Token Amount" }), (0, jsx_runtime_1.jsxs)("div", { className: "flex items-center gap-2", children: [tokenLogoUrl && ((0, jsx_runtime_1.jsx)("img", { src: tokenLogoUrl, alt: order.metadata.dstToken.symbol, className: "h-5 w-5 rounded-full" })), (0, jsx_runtime_1.jsxs)("span", { className: "font-medium", children: [expectedAmount, " ", order.metadata.dstToken.symbol] })] })] })), (0, jsx_runtime_1.jsxs)("div", { className: "flex items-center justify-between py-3", children: [(0, jsx_runtime_1.jsx)("span", { className: "text-gray-600", children: "Network" }), (0, jsx_runtime_1.jsx)("span", { className: "font-medium", children: (0, anyspend_1.getChainName)(order.dstChain) })] }), (0, jsx_runtime_1.jsxs)("div", { className: "flex items-center justify-between py-3", children: [(0, jsx_runtime_1.jsx)("span", { className: "text-gray-600", children: "Recipient" }), (0, jsx_runtime_1.jsxs)("button", { onClick: () => handleCopy(order.recipientAddress, "Recipient address"), className: "flex items-center gap-2 font-medium transition-colors hover:text-blue-600", children: [(0, centerTruncate_1.default)(order.recipientAddress, 8), (0, jsx_runtime_1.jsx)(lucide_react_1.Copy, { className: "h-4 w-4" })] })] }), (0, jsx_runtime_1.jsxs)("div", { className: "flex items-center justify-between py-3", children: [(0, jsx_runtime_1.jsx)("span", { className: "text-gray-600", children: "Order ID" }), (0, jsx_runtime_1.jsxs)("button", { onClick: () => handleCopy(order.id, "Order ID"), className: "flex items-center gap-2 font-medium transition-colors hover:text-blue-600", children: [(0, centerTruncate_1.default)(order.id, 8), (0, jsx_runtime_1.jsx)(lucide_react_1.Copy, { className: "h-4 w-4" })] })] })] })] }) })] }));
37
- }
@@ -1,12 +0,0 @@
1
- import { GetQuoteResponse, Token } from "../../../../anyspend";
2
- interface WebviewOnrampPaymentProps {
3
- srcAmountOnRamp: string;
4
- recipientAddress?: string;
5
- destinationToken: Token;
6
- partnerId?: string;
7
- anyspendQuote: GetQuoteResponse | undefined;
8
- onPaymentSuccess: (orderId: string) => void;
9
- userId?: string;
10
- }
11
- export declare function WebviewOnrampPayment({ srcAmountOnRamp, recipientAddress, destinationToken, anyspendQuote, onPaymentSuccess, userId, partnerId }: WebviewOnrampPaymentProps): import("react/jsx-runtime").JSX.Element;
12
- export {};
@@ -1,173 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.WebviewOnrampPayment = WebviewOnrampPayment;
7
- const jsx_runtime_1 = require("react/jsx-runtime");
8
- const anyspend_1 = require("../../../../anyspend");
9
- const centerTruncate_1 = __importDefault(require("../../../../shared/utils/centerTruncate"));
10
- const react_stripe_js_1 = require("@stripe/react-stripe-js");
11
- const stripe_js_1 = require("@stripe/stripe-js");
12
- const framer_motion_1 = require("framer-motion");
13
- const lucide_react_1 = require("lucide-react");
14
- const react_1 = require("react");
15
- const sonner_1 = require("sonner");
16
- const viem_1 = require("viem");
17
- const stripePromise = (0, stripe_js_1.loadStripe)(anyspend_1.STRIPE_CONFIG.publishableKey);
18
- // Stripe Payment Form Component
19
- function StripePaymentForm({ order, onPaymentSuccess }) {
20
- const stripe = (0, react_stripe_js_1.useStripe)();
21
- const elements = (0, react_stripe_js_1.useElements)();
22
- const [isProcessing, setIsProcessing] = (0, react_1.useState)(false);
23
- const [error, setError] = (0, react_1.useState)(null);
24
- const handleSubmit = async (e) => {
25
- e.preventDefault();
26
- if (!stripe || !elements) {
27
- return;
28
- }
29
- setIsProcessing(true);
30
- setError(null);
31
- try {
32
- const { error: submitError } = await stripe.confirmPayment({
33
- elements,
34
- redirect: "if_required"
35
- });
36
- if (submitError) {
37
- setError(submitError.message || "An error occurred");
38
- console.error("Payment error:", submitError);
39
- }
40
- else {
41
- console.log("Payment successful");
42
- onPaymentSuccess(order.id);
43
- }
44
- }
45
- catch (err) {
46
- setError(err.message || "An error occurred");
47
- console.error("Payment error:", err);
48
- }
49
- finally {
50
- setIsProcessing(false);
51
- }
52
- };
53
- const stripeElementOptions = {
54
- layout: "tabs",
55
- defaultValues: {
56
- billingDetails: {
57
- name: "",
58
- email: ""
59
- }
60
- }
61
- };
62
- return ((0, jsx_runtime_1.jsx)("form", { onSubmit: handleSubmit, className: "w-full", children: (0, jsx_runtime_1.jsx)("div", { className: "overflow-hidden rounded-xl bg-white", children: (0, jsx_runtime_1.jsxs)("div", { className: "px-6 py-4", children: [(0, jsx_runtime_1.jsx)("h2", { className: "mb-4 text-lg font-semibold", children: "Payment Details" }), (0, jsx_runtime_1.jsx)(react_stripe_js_1.PaymentElement, { options: stripeElementOptions }), error && ((0, jsx_runtime_1.jsx)("div", { className: "mt-4 rounded-lg border border-red-200 bg-red-50 p-3 text-sm text-red-600", children: error })), (0, jsx_runtime_1.jsx)("button", { type: "submit", disabled: !stripe || isProcessing, className: "mt-6 w-full rounded-xl bg-blue-600 px-4 py-3 font-medium text-white hover:bg-blue-700 disabled:cursor-not-allowed disabled:opacity-50", children: isProcessing ? ((0, jsx_runtime_1.jsxs)("div", { className: "flex items-center justify-center gap-2", children: [(0, jsx_runtime_1.jsx)(lucide_react_1.Loader2, { className: "h-5 w-5 animate-spin" }), (0, jsx_runtime_1.jsx)("span", { children: "Processing..." })] })) : ((0, jsx_runtime_1.jsx)("span", { children: "Complete Payment" })) })] }) }) }));
63
- }
64
- function WebviewOnrampPayment({ srcAmountOnRamp, recipientAddress, destinationToken, anyspendQuote, onPaymentSuccess, userId, partnerId }) {
65
- const [stableAmountForGeo, setStableAmountForGeo] = (0, react_1.useState)(srcAmountOnRamp);
66
- const hasInitialized = (0, react_1.useRef)(false);
67
- const [createdOrder, setCreatedOrder] = (0, react_1.useState)(null);
68
- const orderCreationAttempted = (0, react_1.useRef)(false);
69
- // Only update the stable amount on first render or when explicitly needed
70
- (0, react_1.useEffect)(() => {
71
- if (!hasInitialized.current && srcAmountOnRamp) {
72
- setStableAmountForGeo(srcAmountOnRamp);
73
- hasInitialized.current = true;
74
- }
75
- }, [srcAmountOnRamp]);
76
- const { geoData, isStripeWeb2Supported, isLoading: isLoadingGeoOnramp } = (0, anyspend_1.useGeoOnrampOptions)(true, stableAmountForGeo);
77
- const { createOrder, isCreatingOrder } = (0, anyspend_1.useAnyspendCreateOnrampOrder)({
78
- onSuccess: data => {
79
- const orderId = data.data.id;
80
- setCreatedOrder(data.data);
81
- },
82
- onError: error => {
83
- console.error(error);
84
- sonner_1.toast.error("Failed to create order: " + error.message);
85
- }
86
- });
87
- const { clientSecret, isLoadingStripeClientSecret } = (0, anyspend_1.useStripeClientSecret)(true, createdOrder?.stripePaymentIntentId || "");
88
- // Create order when component mounts and all required data is available
89
- (0, react_1.useEffect)(() => {
90
- const createOrderIfPossible = async () => {
91
- if (!orderCreationAttempted.current &&
92
- recipientAddress &&
93
- srcAmountOnRamp &&
94
- parseFloat(srcAmountOnRamp) > 0 &&
95
- isStripeWeb2Supported &&
96
- anyspendQuote &&
97
- geoData) {
98
- orderCreationAttempted.current = true;
99
- try {
100
- const getDstToken = () => {
101
- return {
102
- ...destinationToken,
103
- chainId: destinationToken.chainId,
104
- address: destinationToken.address
105
- };
106
- };
107
- createOrder({
108
- isMainnet: true,
109
- recipientAddress,
110
- orderType: anyspend_1.OrderType.Swap,
111
- dstChain: getDstToken().chainId,
112
- dstToken: getDstToken(),
113
- srcFiatAmount: srcAmountOnRamp,
114
- onramp: {
115
- vendor: anyspend_1.OnrampVendor.StripeWeb2,
116
- paymentMethod: "",
117
- country: geoData.country || "US",
118
- ipAddress: geoData.ip,
119
- redirectUrl: `${window.location.origin}${userId ? `?userId=${userId}` : ""}`
120
- },
121
- expectedDstAmount: anyspendQuote.data?.currencyOut?.amount?.toString() || "0",
122
- partnerId
123
- });
124
- }
125
- catch (err) {
126
- console.error(err);
127
- sonner_1.toast.error("Failed to create order: " + err.message);
128
- }
129
- }
130
- };
131
- createOrderIfPossible();
132
- }, [
133
- recipientAddress,
134
- srcAmountOnRamp,
135
- isStripeWeb2Supported,
136
- anyspendQuote,
137
- geoData,
138
- createOrder,
139
- destinationToken,
140
- userId,
141
- partnerId
142
- ]);
143
- // Check if all required data is loaded
144
- const isLoading = isLoadingGeoOnramp || !anyspendQuote || !destinationToken.metadata?.logoURI;
145
- // Show loading state while data is being fetched or order is being created
146
- if (isLoading || isCreatingOrder || isLoadingStripeClientSecret) {
147
- return ((0, jsx_runtime_1.jsxs)("div", { className: "mx-auto flex w-full max-w-[460px] flex-col items-center justify-center gap-3 py-12", children: [(0, jsx_runtime_1.jsx)(lucide_react_1.Loader2, { className: "text-as-brand h-8 w-8 animate-spin" }), (0, jsx_runtime_1.jsx)("span", { className: "text-as-primary/70", children: isCreatingOrder
148
- ? "Creating payment session..."
149
- : isLoadingStripeClientSecret
150
- ? "Loading payment form..."
151
- : "Loading payment details..." })] }));
152
- }
153
- // If we have a created order and client secret, show the payment form
154
- if (createdOrder && clientSecret) {
155
- return ((0, jsx_runtime_1.jsxs)("div", { className: "mx-auto flex w-full max-w-[460px] flex-col gap-6", children: [(0, jsx_runtime_1.jsx)("div", { className: "overflow-hidden rounded-xl bg-white", children: (0, jsx_runtime_1.jsxs)("div", { className: "px-6 py-4", children: [(0, jsx_runtime_1.jsx)("h2", { className: "mb-4 text-lg font-semibold", children: "Order summary" }), (0, jsx_runtime_1.jsxs)("div", { className: "flex flex-col divide-y", children: [(0, jsx_runtime_1.jsxs)("div", { className: "flex items-center justify-between py-3", children: [(0, jsx_runtime_1.jsx)("span", { className: "text-gray-600", children: "Receiving" }), (0, jsx_runtime_1.jsxs)("div", { className: "flex items-center gap-2", children: [destinationToken.metadata?.logoURI && ((0, jsx_runtime_1.jsx)("img", { src: destinationToken.metadata.logoURI, alt: destinationToken.symbol, className: "h-5 w-5 rounded-full" })), (0, jsx_runtime_1.jsxs)("span", { className: "font-medium", children: [anyspendQuote?.data?.currencyOut?.amount
156
- ? Number((0, viem_1.formatUnits)(BigInt(anyspendQuote.data.currencyOut.amount), destinationToken.decimals)).toFixed(4)
157
- : "0", " ", destinationToken.symbol] })] })] }), (0, jsx_runtime_1.jsxs)("div", { className: "flex items-center justify-between py-3", children: [(0, jsx_runtime_1.jsx)("span", { className: "text-gray-600", children: "Network" }), (0, jsx_runtime_1.jsx)("span", { className: "font-medium", children: (0, anyspend_1.getChainName)(destinationToken.chainId) })] }), recipientAddress && ((0, jsx_runtime_1.jsxs)("div", { className: "flex items-center justify-between py-3", children: [(0, jsx_runtime_1.jsx)("span", { className: "text-gray-600", children: "Recipient" }), (0, jsx_runtime_1.jsx)("span", { className: "font-medium", children: (0, centerTruncate_1.default)(recipientAddress) })] })), (0, jsx_runtime_1.jsxs)("div", { className: "flex items-center justify-between py-3", children: [(0, jsx_runtime_1.jsx)("span", { className: "text-gray-600", children: "You Pay" }), (0, jsx_runtime_1.jsxs)("span", { className: "text-lg font-semibold", children: ["$", parseFloat(srcAmountOnRamp).toFixed(2)] })] })] })] }) }), (0, jsx_runtime_1.jsx)(react_stripe_js_1.Elements, { stripe: stripePromise, options: {
158
- clientSecret,
159
- appearance: {
160
- theme: "flat",
161
- variables: {
162
- colorPrimary: "#2563eb",
163
- colorBackground: "#ffffff",
164
- borderRadius: "12px"
165
- }
166
- }
167
- }, children: (0, jsx_runtime_1.jsx)(StripePaymentForm, { order: createdOrder, onPaymentSuccess: onPaymentSuccess }) })] }));
168
- }
169
- // Show initial order summary while waiting for order creation
170
- return ((0, jsx_runtime_1.jsx)(framer_motion_1.motion.div, { initial: { opacity: 0, y: 20 }, animate: { opacity: 1, y: 0 }, transition: { duration: 0.3 }, className: "mx-auto w-full max-w-[460px]", children: (0, jsx_runtime_1.jsx)("div", { className: "overflow-hidden rounded-xl bg-white", children: (0, jsx_runtime_1.jsxs)("div", { className: "px-6 py-4", children: [(0, jsx_runtime_1.jsx)("h2", { className: "mb-4 text-lg font-semibold", children: "Order summary" }), (0, jsx_runtime_1.jsxs)("div", { className: "flex flex-col divide-y", children: [(0, jsx_runtime_1.jsxs)("div", { className: "flex items-center justify-between py-3", children: [(0, jsx_runtime_1.jsx)("span", { className: "text-gray-600", children: "Receiving" }), (0, jsx_runtime_1.jsxs)("div", { className: "flex items-center gap-2", children: [destinationToken.metadata?.logoURI && ((0, jsx_runtime_1.jsx)("img", { src: destinationToken.metadata.logoURI, alt: destinationToken.symbol, className: "h-5 w-5 rounded-full" })), (0, jsx_runtime_1.jsxs)("span", { className: "font-medium", children: [anyspendQuote?.data?.currencyOut?.amount
171
- ? Number((0, viem_1.formatUnits)(BigInt(anyspendQuote.data.currencyOut.amount), destinationToken.decimals)).toFixed(4)
172
- : "0", " ", destinationToken.symbol] })] })] }), (0, jsx_runtime_1.jsxs)("div", { className: "flex items-center justify-between py-3", children: [(0, jsx_runtime_1.jsx)("span", { className: "text-gray-600", children: "Network" }), (0, jsx_runtime_1.jsx)("span", { className: "font-medium", children: (0, anyspend_1.getChainName)(destinationToken.chainId) })] }), recipientAddress && ((0, jsx_runtime_1.jsxs)("div", { className: "flex items-center justify-between py-3", children: [(0, jsx_runtime_1.jsx)("span", { className: "text-gray-600", children: "Recipient" }), (0, jsx_runtime_1.jsx)("span", { className: "font-medium", children: (0, centerTruncate_1.default)(recipientAddress) })] })), (0, jsx_runtime_1.jsxs)("div", { className: "flex items-center justify-between py-3", children: [(0, jsx_runtime_1.jsx)("span", { className: "text-gray-600", children: "You Pay" }), (0, jsx_runtime_1.jsxs)("span", { className: "text-lg font-semibold", children: ["$", parseFloat(srcAmountOnRamp).toFixed(2)] })] })] })] }) }) }));
173
- }
@@ -1,9 +0,0 @@
1
- export declare const useB3EnsName: () => {
2
- registerEns: (username: string, message: string, hash: string) => Promise<{
3
- success: boolean;
4
- error?: string;
5
- }>;
6
- getEns: (address: string) => Promise<{
7
- name: string;
8
- }>;
9
- };
@@ -1,39 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.useB3EnsName = void 0;
7
- const bsmnt_1 = __importDefault(require("../../../global-account/bsmnt"));
8
- const constants_1 = require("../../../shared/constants");
9
- const js_cookie_1 = __importDefault(require("js-cookie"));
10
- const react_1 = require("react");
11
- const useB3EnsName = () => {
12
- const registerEns = (0, react_1.useCallback)(async (username, message, hash) => {
13
- if (!bsmnt_1.default.authentication.authenticated) {
14
- await bsmnt_1.default.authentication.authenticate({
15
- strategy: "b3-jwt",
16
- accessToken: js_cookie_1.default.get(constants_1.B3_AUTH_COOKIE_NAME) || ""
17
- });
18
- }
19
- const response = await bsmnt_1.default.service("profiles").registerUsername({
20
- username,
21
- message,
22
- hash
23
- }, {});
24
- return response;
25
- }, [bsmnt_1.default.authentication.authenticated]);
26
- const getEns = (0, react_1.useCallback)(async (address) => {
27
- const response = await fetch(`https://ens-gateway.b3.fun/address/${address}`);
28
- if (!response.ok) {
29
- throw new Error(`Failed to fetch ENS name: ${response.statusText}`);
30
- }
31
- const data = await response.json();
32
- return data;
33
- }, []);
34
- return {
35
- registerEns,
36
- getEns
37
- };
38
- };
39
- exports.useB3EnsName = useB3EnsName;