@one_deploy/sdk 1.0.0
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.
- package/.turbo/turbo-build.log +0 -0
- package/.turbo/turbo-type-check.log +0 -0
- package/dist/config/index.d.mts +74 -0
- package/dist/config/index.d.ts +74 -0
- package/dist/config/index.js +242 -0
- package/dist/config/index.js.map +1 -0
- package/dist/config/index.mjs +224 -0
- package/dist/config/index.mjs.map +1 -0
- package/dist/engine-5ndtBaCr.d.ts +1039 -0
- package/dist/engine-CrlhH0nw.d.mts +1039 -0
- package/dist/hooks/index.d.mts +56 -0
- package/dist/hooks/index.d.ts +56 -0
- package/dist/hooks/index.js +1360 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/index.mjs +1356 -0
- package/dist/hooks/index.mjs.map +1 -0
- package/dist/index.d.mts +356 -0
- package/dist/index.d.ts +356 -0
- package/dist/index.js +5068 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +4949 -0
- package/dist/index.mjs.map +1 -0
- package/dist/price-CgqXPnT3.d.ts +13 -0
- package/dist/price-ClbLHHjv.d.mts +13 -0
- package/dist/providers/index.d.mts +121 -0
- package/dist/providers/index.d.ts +121 -0
- package/dist/providers/index.js +1642 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/index.mjs +1600 -0
- package/dist/providers/index.mjs.map +1 -0
- package/dist/react-native.d.mts +120 -0
- package/dist/react-native.d.ts +120 -0
- package/dist/react-native.js +1792 -0
- package/dist/react-native.js.map +1 -0
- package/dist/react-native.mjs +1755 -0
- package/dist/react-native.mjs.map +1 -0
- package/dist/services/index.d.mts +85 -0
- package/dist/services/index.d.ts +85 -0
- package/dist/services/index.js +1466 -0
- package/dist/services/index.js.map +1 -0
- package/dist/services/index.mjs +1458 -0
- package/dist/services/index.mjs.map +1 -0
- package/dist/types/index.d.mts +759 -0
- package/dist/types/index.d.ts +759 -0
- package/dist/types/index.js +4 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/index.mjs +3 -0
- package/dist/types/index.mjs.map +1 -0
- package/dist/utils/index.d.mts +36 -0
- package/dist/utils/index.d.ts +36 -0
- package/dist/utils/index.js +164 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/index.mjs +142 -0
- package/dist/utils/index.mjs.map +1 -0
- package/package.json +101 -0
- package/src/components/OneConnectButton.tsx +143 -0
- package/src/components/OneNFTGallery.tsx +324 -0
- package/src/components/OneOfframpWidget.tsx +660 -0
- package/src/components/OneOnrampWidget.tsx +596 -0
- package/src/components/OnePayWidget.tsx +160 -0
- package/src/components/OneReceiveWidget.tsx +272 -0
- package/src/components/OneSendWidget.tsx +248 -0
- package/src/components/OneSwapWidget.tsx +715 -0
- package/src/components/OneTransactionButton.tsx +150 -0
- package/src/components/OneWalletBalance.tsx +354 -0
- package/src/components/index.ts +24 -0
- package/src/config/index.ts +299 -0
- package/src/hooks/index.ts +2 -0
- package/src/hooks/useTokenPrice.ts +162 -0
- package/src/hooks/useWalletBalance.ts +98 -0
- package/src/index.ts +193 -0
- package/src/providers/OneProvider.tsx +452 -0
- package/src/providers/ThirdwebProvider.tsx +203 -0
- package/src/providers/index.ts +26 -0
- package/src/react-native.ts +378 -0
- package/src/services/engine.ts +1854 -0
- package/src/services/index.ts +30 -0
- package/src/services/price.ts +164 -0
- package/src/services/supabase.ts +180 -0
- package/src/types/index.ts +887 -0
- package/src/utils/index.ts +200 -0
- package/tsconfig.json +22 -0
- package/tsup.config.ts +25 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
// ===== Configuration =====
|
|
2
|
+
export {
|
|
3
|
+
initOneSDK,
|
|
4
|
+
getConfig,
|
|
5
|
+
getEngineUrl,
|
|
6
|
+
isInitialized,
|
|
7
|
+
// Chain functions (fetch from Engine API)
|
|
8
|
+
fetchChains,
|
|
9
|
+
getChains,
|
|
10
|
+
getChainById,
|
|
11
|
+
getChainByName,
|
|
12
|
+
getRecommendedChains,
|
|
13
|
+
getSmartWalletChains,
|
|
14
|
+
// Constants
|
|
15
|
+
CHAIN_IDS,
|
|
16
|
+
DEFAULT_CHAIN_ID,
|
|
17
|
+
TOKEN_NAMES,
|
|
18
|
+
COINGECKO_IDS,
|
|
19
|
+
// @deprecated - use getChains() instead
|
|
20
|
+
CHAIN_CONFIGS,
|
|
21
|
+
SUPPORTED_CHAINS,
|
|
22
|
+
getChainConfig,
|
|
23
|
+
} from './config';
|
|
24
|
+
export type { OneConfig } from './config';
|
|
25
|
+
|
|
26
|
+
// ===== Types =====
|
|
27
|
+
export * from './types';
|
|
28
|
+
|
|
29
|
+
// ===== ONE Engine Client (Main API Gateway) =====
|
|
30
|
+
export {
|
|
31
|
+
OneEngineClient,
|
|
32
|
+
createOneEngineClient,
|
|
33
|
+
// Supabase (for direct DB if needed)
|
|
34
|
+
SupabaseService,
|
|
35
|
+
createSupabaseClient,
|
|
36
|
+
// Price (for offline/cache)
|
|
37
|
+
PriceService,
|
|
38
|
+
priceService,
|
|
39
|
+
} from './services';
|
|
40
|
+
|
|
41
|
+
// Export all Engine types
|
|
42
|
+
export type {
|
|
43
|
+
// Auth
|
|
44
|
+
EngineAuthResponse,
|
|
45
|
+
// Wallet
|
|
46
|
+
EngineWalletBalance,
|
|
47
|
+
EngineTransactionRequest,
|
|
48
|
+
EngineTransactionResponse,
|
|
49
|
+
// Onramp
|
|
50
|
+
OnrampSessionRequest,
|
|
51
|
+
OnrampSession,
|
|
52
|
+
OnrampQuote,
|
|
53
|
+
OnrampTransaction,
|
|
54
|
+
// Swap
|
|
55
|
+
SwapQuoteRequest,
|
|
56
|
+
SwapQuote,
|
|
57
|
+
SwapExecuteRequest,
|
|
58
|
+
SwapResult,
|
|
59
|
+
} from './services';
|
|
60
|
+
|
|
61
|
+
// ===== React Providers & Hooks =====
|
|
62
|
+
export {
|
|
63
|
+
// Original ONE Provider (API-based)
|
|
64
|
+
OneProvider,
|
|
65
|
+
useOne,
|
|
66
|
+
useOneAuth,
|
|
67
|
+
useOneWallet,
|
|
68
|
+
useOneOnramp,
|
|
69
|
+
useOneSwap,
|
|
70
|
+
useOneTrading,
|
|
71
|
+
useOneEngine,
|
|
72
|
+
OneContext,
|
|
73
|
+
// Thirdweb Integration Provider
|
|
74
|
+
OneThirdwebProvider,
|
|
75
|
+
useThirdwebClient,
|
|
76
|
+
inAppWallet,
|
|
77
|
+
smartWallet,
|
|
78
|
+
base,
|
|
79
|
+
ethereum,
|
|
80
|
+
polygon,
|
|
81
|
+
arbitrum,
|
|
82
|
+
optimism,
|
|
83
|
+
} from './providers';
|
|
84
|
+
|
|
85
|
+
export type { OneThirdwebConfig, OneThirdwebProviderProps } from './providers';
|
|
86
|
+
|
|
87
|
+
// ===== UI Components (Wrapped Thirdweb) =====
|
|
88
|
+
export {
|
|
89
|
+
// Wallet Connection
|
|
90
|
+
OneConnectButton,
|
|
91
|
+
OneConnectButtonSimple,
|
|
92
|
+
OneConnectButtonFull,
|
|
93
|
+
// Payment Widgets
|
|
94
|
+
OnePayWidget,
|
|
95
|
+
OneFundWalletWidget,
|
|
96
|
+
OneDirectPayWidget,
|
|
97
|
+
OneCryptoOnlyPayWidget,
|
|
98
|
+
OneFiatOnlyPayWidget,
|
|
99
|
+
// Transaction Buttons
|
|
100
|
+
OneTransactionButton,
|
|
101
|
+
OneSendETHButton,
|
|
102
|
+
OneApproveButton,
|
|
103
|
+
// Send Widgets
|
|
104
|
+
OneSendWidget,
|
|
105
|
+
OneSendETHWidget,
|
|
106
|
+
OneSendUSDCWidget,
|
|
107
|
+
// Fiat On/Off Ramp Widgets
|
|
108
|
+
OneOnrampWidget,
|
|
109
|
+
OneBuyUSDTWidget,
|
|
110
|
+
OneBuyUSDCWidget,
|
|
111
|
+
OneBuyETHWidget,
|
|
112
|
+
OneBuyBTCWidget,
|
|
113
|
+
OneOfframpWidget,
|
|
114
|
+
OneSellUSDTWidget,
|
|
115
|
+
OneSellUSDCWidget,
|
|
116
|
+
OneSellETHWidget,
|
|
117
|
+
// Swap Widget
|
|
118
|
+
OneSwapWidget,
|
|
119
|
+
OneSameChainSwap,
|
|
120
|
+
OneCrossChainSwap,
|
|
121
|
+
// Balance & Assets
|
|
122
|
+
OneWalletBalance,
|
|
123
|
+
OneBalanceDisplay,
|
|
124
|
+
// NFT Gallery
|
|
125
|
+
OneNFTGallery,
|
|
126
|
+
// Receive Widget
|
|
127
|
+
OneReceiveWidget,
|
|
128
|
+
} from './components';
|
|
129
|
+
|
|
130
|
+
export type {
|
|
131
|
+
OneConnectButtonProps,
|
|
132
|
+
OnePayWidgetProps,
|
|
133
|
+
PayMode,
|
|
134
|
+
OneTransactionButtonProps,
|
|
135
|
+
OneSendETHButtonProps,
|
|
136
|
+
OneApproveButtonProps,
|
|
137
|
+
OneSendWidgetProps,
|
|
138
|
+
// Onramp/Offramp Types
|
|
139
|
+
OneOnrampWidgetProps,
|
|
140
|
+
OneOfframpWidgetProps,
|
|
141
|
+
OfframpQuote,
|
|
142
|
+
OfframpTransaction,
|
|
143
|
+
// Swap
|
|
144
|
+
OneSwapWidgetProps,
|
|
145
|
+
SwapToken,
|
|
146
|
+
SwapRoute,
|
|
147
|
+
SwapStep,
|
|
148
|
+
OneWalletBalanceProps,
|
|
149
|
+
TokenBalance,
|
|
150
|
+
OneNFTGalleryProps,
|
|
151
|
+
NFTItem,
|
|
152
|
+
OneReceiveWidgetProps,
|
|
153
|
+
} from './components';
|
|
154
|
+
|
|
155
|
+
// ===== Standalone Hooks (for use outside OneProvider) =====
|
|
156
|
+
export {
|
|
157
|
+
useWalletBalance,
|
|
158
|
+
useTokenPrice,
|
|
159
|
+
useTokenPrices,
|
|
160
|
+
} from './hooks';
|
|
161
|
+
|
|
162
|
+
// ===== Utilities =====
|
|
163
|
+
export {
|
|
164
|
+
// Address
|
|
165
|
+
shortenAddress,
|
|
166
|
+
isValidAddress,
|
|
167
|
+
checksumAddress,
|
|
168
|
+
// Numbers
|
|
169
|
+
formatNumber,
|
|
170
|
+
formatUSD,
|
|
171
|
+
formatPercent,
|
|
172
|
+
formatTokenAmount,
|
|
173
|
+
// Date/Time
|
|
174
|
+
formatDate,
|
|
175
|
+
formatDateTime,
|
|
176
|
+
formatRelativeTime,
|
|
177
|
+
// Validation
|
|
178
|
+
isValidEmail,
|
|
179
|
+
isValidPhone,
|
|
180
|
+
// Strings
|
|
181
|
+
capitalize,
|
|
182
|
+
truncate,
|
|
183
|
+
slugify,
|
|
184
|
+
// Async
|
|
185
|
+
sleep,
|
|
186
|
+
retry,
|
|
187
|
+
// Objects
|
|
188
|
+
omit,
|
|
189
|
+
pick,
|
|
190
|
+
// Errors
|
|
191
|
+
OneSDKError,
|
|
192
|
+
isOneSDKError,
|
|
193
|
+
} from './utils';
|
|
@@ -0,0 +1,452 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import React, {
|
|
4
|
+
createContext,
|
|
5
|
+
useContext,
|
|
6
|
+
useEffect,
|
|
7
|
+
useState,
|
|
8
|
+
useCallback,
|
|
9
|
+
useMemo,
|
|
10
|
+
type ReactNode,
|
|
11
|
+
} from 'react';
|
|
12
|
+
import { initOneSDK, type OneConfig } from '../config';
|
|
13
|
+
import {
|
|
14
|
+
OneEngineClient,
|
|
15
|
+
createOneEngineClient,
|
|
16
|
+
type EngineAuthResponse,
|
|
17
|
+
type EngineWalletBalance,
|
|
18
|
+
type OnrampSession,
|
|
19
|
+
type OnrampSessionRequest,
|
|
20
|
+
type SwapQuote,
|
|
21
|
+
type SwapQuoteRequest,
|
|
22
|
+
} from '../services/engine';
|
|
23
|
+
import type { User, Token, AIStrategy, AIOrder } from '../types';
|
|
24
|
+
|
|
25
|
+
// ===== Context Types =====
|
|
26
|
+
|
|
27
|
+
interface AuthContextValue {
|
|
28
|
+
user: User | null;
|
|
29
|
+
isAuthenticated: boolean;
|
|
30
|
+
isLoading: boolean;
|
|
31
|
+
accessToken: string | null;
|
|
32
|
+
sendOtp: (email: string) => Promise<{ success: boolean; error?: string }>;
|
|
33
|
+
verifyOtp: (email: string, otp: string) => Promise<{ success: boolean; error?: string }>;
|
|
34
|
+
signOut: () => Promise<void>;
|
|
35
|
+
refreshUser: () => Promise<void>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface WalletContextValue {
|
|
39
|
+
address: string | null;
|
|
40
|
+
balance: EngineWalletBalance | null;
|
|
41
|
+
tokens: Token[];
|
|
42
|
+
totalUsd: number;
|
|
43
|
+
isLoading: boolean;
|
|
44
|
+
error: string | null;
|
|
45
|
+
setAddress: (address: string | null) => void;
|
|
46
|
+
fetchBalance: (chains?: number[]) => Promise<void>;
|
|
47
|
+
refreshBalance: () => Promise<void>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface OnrampContextValue {
|
|
51
|
+
isOpen: boolean;
|
|
52
|
+
widgetUrl: string | null;
|
|
53
|
+
sessionId: string | null;
|
|
54
|
+
openOnramp: (options?: Partial<OnrampSessionRequest>) => Promise<void>;
|
|
55
|
+
closeOnramp: () => void;
|
|
56
|
+
getQuote: (fiatCurrency: string, fiatAmount: number, cryptoCurrency: string) => Promise<any>;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
interface SwapContextValue {
|
|
60
|
+
getQuote: (request: SwapQuoteRequest) => Promise<SwapQuote | null>;
|
|
61
|
+
executeSwap: (quoteId: string) => Promise<any>;
|
|
62
|
+
getSupportedTokens: (chainId?: number) => Promise<Token[]>;
|
|
63
|
+
getSupportedChains: () => Promise<{ id: number; name: string }[]>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
interface TradingContextValue {
|
|
67
|
+
strategies: AIStrategy[];
|
|
68
|
+
orders: AIOrder[];
|
|
69
|
+
portfolioStats: {
|
|
70
|
+
totalInvested: number;
|
|
71
|
+
totalValue: number;
|
|
72
|
+
totalPnl: number;
|
|
73
|
+
totalPnlPercent: number;
|
|
74
|
+
activePositions: number;
|
|
75
|
+
} | null;
|
|
76
|
+
isLoading: boolean;
|
|
77
|
+
fetchStrategies: () => Promise<void>;
|
|
78
|
+
fetchOrders: () => Promise<void>;
|
|
79
|
+
fetchPortfolio: () => Promise<void>;
|
|
80
|
+
createOrder: (strategyId: string, amount: number, currency: string) => Promise<any>;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
interface OneContextValue {
|
|
84
|
+
isInitialized: boolean;
|
|
85
|
+
config: OneConfig | null;
|
|
86
|
+
engine: OneEngineClient;
|
|
87
|
+
auth: AuthContextValue;
|
|
88
|
+
wallet: WalletContextValue;
|
|
89
|
+
onramp: OnrampContextValue;
|
|
90
|
+
swap: SwapContextValue;
|
|
91
|
+
trading: TradingContextValue;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const OneContext = createContext<OneContextValue | null>(null);
|
|
95
|
+
|
|
96
|
+
// ===== Provider Props =====
|
|
97
|
+
interface OneProviderProps {
|
|
98
|
+
children: ReactNode;
|
|
99
|
+
config: OneConfig;
|
|
100
|
+
autoFetchBalance?: boolean;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// ===== Provider Component =====
|
|
104
|
+
export function OneProvider({
|
|
105
|
+
children,
|
|
106
|
+
config,
|
|
107
|
+
autoFetchBalance = true,
|
|
108
|
+
}: OneProviderProps) {
|
|
109
|
+
// Initialize SDK and Engine Client
|
|
110
|
+
const [isInitialized, setIsInitialized] = useState(false);
|
|
111
|
+
const engine = useMemo(() => createOneEngineClient({
|
|
112
|
+
baseUrl: config.oneEngineUrl,
|
|
113
|
+
clientId: config.oneClientId,
|
|
114
|
+
secretKey: config.oneSecretKey,
|
|
115
|
+
}), [config]);
|
|
116
|
+
|
|
117
|
+
useEffect(() => {
|
|
118
|
+
initOneSDK(config);
|
|
119
|
+
setIsInitialized(true);
|
|
120
|
+
}, [config]);
|
|
121
|
+
|
|
122
|
+
// ===== Auth State =====
|
|
123
|
+
const [user, setUser] = useState<User | null>(null);
|
|
124
|
+
const [accessToken, setAccessToken] = useState<string | null>(null);
|
|
125
|
+
const [authLoading, setAuthLoading] = useState(true);
|
|
126
|
+
|
|
127
|
+
const sendOtp = useCallback(async (email: string) => {
|
|
128
|
+
const result = await engine.sendEmailOtp(email);
|
|
129
|
+
if (!result.success) {
|
|
130
|
+
return { success: false, error: result.error?.message };
|
|
131
|
+
}
|
|
132
|
+
return { success: true };
|
|
133
|
+
}, [engine]);
|
|
134
|
+
|
|
135
|
+
const verifyOtp = useCallback(async (email: string, otp: string) => {
|
|
136
|
+
const result = await engine.verifyEmailOtp(email, otp);
|
|
137
|
+
if (!result.success || !result.data) {
|
|
138
|
+
return { success: false, error: result.error?.message };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const { user: authUser, accessToken: token } = result.data;
|
|
142
|
+
setUser(authUser);
|
|
143
|
+
setAccessToken(token);
|
|
144
|
+
engine.setAccessToken(token);
|
|
145
|
+
|
|
146
|
+
return { success: true };
|
|
147
|
+
}, [engine]);
|
|
148
|
+
|
|
149
|
+
const signOut = useCallback(async () => {
|
|
150
|
+
await engine.signOut();
|
|
151
|
+
setUser(null);
|
|
152
|
+
setAccessToken(null);
|
|
153
|
+
engine.clearAccessToken();
|
|
154
|
+
}, [engine]);
|
|
155
|
+
|
|
156
|
+
const refreshUser = useCallback(async () => {
|
|
157
|
+
try {
|
|
158
|
+
if (accessToken) {
|
|
159
|
+
engine.setAccessToken(accessToken);
|
|
160
|
+
const result = await engine.getCurrentUser();
|
|
161
|
+
if (result.success && result.data) {
|
|
162
|
+
setUser(result.data);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
} finally {
|
|
166
|
+
setAuthLoading(false);
|
|
167
|
+
}
|
|
168
|
+
}, [engine, accessToken]);
|
|
169
|
+
|
|
170
|
+
useEffect(() => {
|
|
171
|
+
refreshUser();
|
|
172
|
+
}, []);
|
|
173
|
+
|
|
174
|
+
// ===== Wallet State =====
|
|
175
|
+
const [walletAddress, setWalletAddress] = useState<string | null>(null);
|
|
176
|
+
const [walletBalance, setWalletBalance] = useState<EngineWalletBalance | null>(null);
|
|
177
|
+
const [walletLoading, setWalletLoading] = useState(false);
|
|
178
|
+
const [walletError, setWalletError] = useState<string | null>(null);
|
|
179
|
+
|
|
180
|
+
const fetchBalance = useCallback(async (chains?: number[]) => {
|
|
181
|
+
if (!walletAddress) return;
|
|
182
|
+
|
|
183
|
+
setWalletLoading(true);
|
|
184
|
+
setWalletError(null);
|
|
185
|
+
|
|
186
|
+
try {
|
|
187
|
+
const result = await engine.getWalletBalance(walletAddress, chains);
|
|
188
|
+
if (result.success && result.data) {
|
|
189
|
+
setWalletBalance(result.data);
|
|
190
|
+
} else {
|
|
191
|
+
setWalletError(result.error?.message || 'Failed to fetch balance');
|
|
192
|
+
}
|
|
193
|
+
} catch (error) {
|
|
194
|
+
setWalletError(error instanceof Error ? error.message : 'Failed to fetch balance');
|
|
195
|
+
} finally {
|
|
196
|
+
setWalletLoading(false);
|
|
197
|
+
}
|
|
198
|
+
}, [walletAddress, engine]);
|
|
199
|
+
|
|
200
|
+
const refreshBalance = useCallback(async () => {
|
|
201
|
+
await fetchBalance();
|
|
202
|
+
}, [fetchBalance]);
|
|
203
|
+
|
|
204
|
+
useEffect(() => {
|
|
205
|
+
if (autoFetchBalance && walletAddress && isInitialized) {
|
|
206
|
+
fetchBalance();
|
|
207
|
+
}
|
|
208
|
+
}, [walletAddress, autoFetchBalance, isInitialized, fetchBalance]);
|
|
209
|
+
|
|
210
|
+
// ===== Onramp State =====
|
|
211
|
+
const [onrampOpen, setOnrampOpen] = useState(false);
|
|
212
|
+
const [onrampUrl, setOnrampUrl] = useState<string | null>(null);
|
|
213
|
+
const [onrampSessionId, setOnrampSessionId] = useState<string | null>(null);
|
|
214
|
+
|
|
215
|
+
const openOnramp = useCallback(async (options?: Partial<OnrampSessionRequest>) => {
|
|
216
|
+
if (!walletAddress) return;
|
|
217
|
+
|
|
218
|
+
const result = await engine.createOnrampSession({
|
|
219
|
+
walletAddress,
|
|
220
|
+
fiatCurrency: 'USD',
|
|
221
|
+
cryptoCurrency: 'ETH',
|
|
222
|
+
...options,
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
if (result.success && result.data) {
|
|
226
|
+
setOnrampUrl(result.data.widgetUrl);
|
|
227
|
+
setOnrampSessionId(result.data.sessionId);
|
|
228
|
+
setOnrampOpen(true);
|
|
229
|
+
}
|
|
230
|
+
}, [walletAddress, engine]);
|
|
231
|
+
|
|
232
|
+
const closeOnramp = useCallback(() => {
|
|
233
|
+
setOnrampOpen(false);
|
|
234
|
+
setOnrampUrl(null);
|
|
235
|
+
}, []);
|
|
236
|
+
|
|
237
|
+
const getOnrampQuote = useCallback(async (
|
|
238
|
+
fiatCurrency: string,
|
|
239
|
+
fiatAmount: number,
|
|
240
|
+
cryptoCurrency: string
|
|
241
|
+
) => {
|
|
242
|
+
const result = await engine.getOnrampQuote(fiatCurrency, fiatAmount, cryptoCurrency);
|
|
243
|
+
return result.success ? result.data : null;
|
|
244
|
+
}, [engine]);
|
|
245
|
+
|
|
246
|
+
// ===== Swap =====
|
|
247
|
+
const getSwapQuote = useCallback(async (request: SwapQuoteRequest) => {
|
|
248
|
+
const result = await engine.getSwapQuote(request);
|
|
249
|
+
return result.success ? result.data || null : null;
|
|
250
|
+
}, [engine]);
|
|
251
|
+
|
|
252
|
+
const executeSwap = useCallback(async (quoteId: string) => {
|
|
253
|
+
if (!walletAddress) return null;
|
|
254
|
+
const result = await engine.executeSwap({ quoteId, walletAddress });
|
|
255
|
+
return result.success ? result.data : null;
|
|
256
|
+
}, [engine, walletAddress]);
|
|
257
|
+
|
|
258
|
+
const getSupportedTokens = useCallback(async (chainId?: number) => {
|
|
259
|
+
const result = await engine.getSupportedSwapTokens(chainId);
|
|
260
|
+
return result.success && result.data ? result.data.tokens : [];
|
|
261
|
+
}, [engine]);
|
|
262
|
+
|
|
263
|
+
const getSupportedChains = useCallback(async () => {
|
|
264
|
+
const result = await engine.getSupportedSwapChains();
|
|
265
|
+
return result.success && result.data ? result.data.chains : [];
|
|
266
|
+
}, [engine]);
|
|
267
|
+
|
|
268
|
+
// ===== Trading State =====
|
|
269
|
+
const [strategies, setStrategies] = useState<AIStrategy[]>([]);
|
|
270
|
+
const [orders, setOrders] = useState<AIOrder[]>([]);
|
|
271
|
+
const [portfolioStats, setPortfolioStats] = useState<TradingContextValue['portfolioStats']>(null);
|
|
272
|
+
const [tradingLoading, setTradingLoading] = useState(false);
|
|
273
|
+
|
|
274
|
+
const fetchStrategies = useCallback(async () => {
|
|
275
|
+
setTradingLoading(true);
|
|
276
|
+
try {
|
|
277
|
+
const result = await engine.getStrategies();
|
|
278
|
+
if (result.success && result.data) {
|
|
279
|
+
setStrategies(result.data);
|
|
280
|
+
}
|
|
281
|
+
} finally {
|
|
282
|
+
setTradingLoading(false);
|
|
283
|
+
}
|
|
284
|
+
}, [engine]);
|
|
285
|
+
|
|
286
|
+
const fetchOrders = useCallback(async () => {
|
|
287
|
+
const result = await engine.getUserOrders();
|
|
288
|
+
if (result.success && result.data) {
|
|
289
|
+
setOrders(result.data);
|
|
290
|
+
}
|
|
291
|
+
}, [engine]);
|
|
292
|
+
|
|
293
|
+
const fetchPortfolio = useCallback(async () => {
|
|
294
|
+
const result = await engine.getPortfolioStats();
|
|
295
|
+
if (result.success && result.data) {
|
|
296
|
+
setPortfolioStats(result.data);
|
|
297
|
+
}
|
|
298
|
+
}, [engine]);
|
|
299
|
+
|
|
300
|
+
const createOrder = useCallback(async (
|
|
301
|
+
strategyId: string,
|
|
302
|
+
amount: number,
|
|
303
|
+
currency: string
|
|
304
|
+
) => {
|
|
305
|
+
const result = await engine.createOrder(strategyId, amount, currency);
|
|
306
|
+
if (result.success) {
|
|
307
|
+
await fetchOrders();
|
|
308
|
+
await fetchPortfolio();
|
|
309
|
+
}
|
|
310
|
+
return result;
|
|
311
|
+
}, [engine, fetchOrders, fetchPortfolio]);
|
|
312
|
+
|
|
313
|
+
// ===== Context Value =====
|
|
314
|
+
const contextValue: OneContextValue = useMemo(() => ({
|
|
315
|
+
isInitialized,
|
|
316
|
+
config: isInitialized ? config : null,
|
|
317
|
+
engine,
|
|
318
|
+
|
|
319
|
+
auth: {
|
|
320
|
+
user,
|
|
321
|
+
isAuthenticated: !!user,
|
|
322
|
+
isLoading: authLoading,
|
|
323
|
+
accessToken,
|
|
324
|
+
sendOtp,
|
|
325
|
+
verifyOtp,
|
|
326
|
+
signOut,
|
|
327
|
+
refreshUser,
|
|
328
|
+
},
|
|
329
|
+
|
|
330
|
+
wallet: {
|
|
331
|
+
address: walletAddress,
|
|
332
|
+
balance: walletBalance,
|
|
333
|
+
tokens: walletBalance?.tokens || [],
|
|
334
|
+
totalUsd: walletBalance?.totalUsd || 0,
|
|
335
|
+
isLoading: walletLoading,
|
|
336
|
+
error: walletError,
|
|
337
|
+
setAddress: setWalletAddress,
|
|
338
|
+
fetchBalance,
|
|
339
|
+
refreshBalance,
|
|
340
|
+
},
|
|
341
|
+
|
|
342
|
+
onramp: {
|
|
343
|
+
isOpen: onrampOpen,
|
|
344
|
+
widgetUrl: onrampUrl,
|
|
345
|
+
sessionId: onrampSessionId,
|
|
346
|
+
openOnramp,
|
|
347
|
+
closeOnramp,
|
|
348
|
+
getQuote: getOnrampQuote,
|
|
349
|
+
},
|
|
350
|
+
|
|
351
|
+
swap: {
|
|
352
|
+
getQuote: getSwapQuote,
|
|
353
|
+
executeSwap,
|
|
354
|
+
getSupportedTokens,
|
|
355
|
+
getSupportedChains,
|
|
356
|
+
},
|
|
357
|
+
|
|
358
|
+
trading: {
|
|
359
|
+
strategies,
|
|
360
|
+
orders,
|
|
361
|
+
portfolioStats,
|
|
362
|
+
isLoading: tradingLoading,
|
|
363
|
+
fetchStrategies,
|
|
364
|
+
fetchOrders,
|
|
365
|
+
fetchPortfolio,
|
|
366
|
+
createOrder,
|
|
367
|
+
},
|
|
368
|
+
}), [
|
|
369
|
+
isInitialized,
|
|
370
|
+
config,
|
|
371
|
+
engine,
|
|
372
|
+
user,
|
|
373
|
+
authLoading,
|
|
374
|
+
accessToken,
|
|
375
|
+
sendOtp,
|
|
376
|
+
verifyOtp,
|
|
377
|
+
signOut,
|
|
378
|
+
refreshUser,
|
|
379
|
+
walletAddress,
|
|
380
|
+
walletBalance,
|
|
381
|
+
walletLoading,
|
|
382
|
+
walletError,
|
|
383
|
+
fetchBalance,
|
|
384
|
+
refreshBalance,
|
|
385
|
+
onrampOpen,
|
|
386
|
+
onrampUrl,
|
|
387
|
+
onrampSessionId,
|
|
388
|
+
openOnramp,
|
|
389
|
+
closeOnramp,
|
|
390
|
+
getOnrampQuote,
|
|
391
|
+
getSwapQuote,
|
|
392
|
+
executeSwap,
|
|
393
|
+
getSupportedTokens,
|
|
394
|
+
getSupportedChains,
|
|
395
|
+
strategies,
|
|
396
|
+
orders,
|
|
397
|
+
portfolioStats,
|
|
398
|
+
tradingLoading,
|
|
399
|
+
fetchStrategies,
|
|
400
|
+
fetchOrders,
|
|
401
|
+
fetchPortfolio,
|
|
402
|
+
createOrder,
|
|
403
|
+
]);
|
|
404
|
+
|
|
405
|
+
return (
|
|
406
|
+
<OneContext.Provider value={contextValue}>
|
|
407
|
+
{children}
|
|
408
|
+
</OneContext.Provider>
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
// ===== Hooks =====
|
|
413
|
+
export function useOne(): OneContextValue {
|
|
414
|
+
const context = useContext(OneContext);
|
|
415
|
+
if (!context) {
|
|
416
|
+
throw new Error('useOne must be used within a OneProvider');
|
|
417
|
+
}
|
|
418
|
+
return context;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
export function useOneAuth() {
|
|
422
|
+
const { auth } = useOne();
|
|
423
|
+
return auth;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
export function useOneWallet() {
|
|
427
|
+
const { wallet } = useOne();
|
|
428
|
+
return wallet;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
export function useOneOnramp() {
|
|
432
|
+
const { onramp } = useOne();
|
|
433
|
+
return onramp;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
export function useOneSwap() {
|
|
437
|
+
const { swap } = useOne();
|
|
438
|
+
return swap;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
export function useOneTrading() {
|
|
442
|
+
const { trading } = useOne();
|
|
443
|
+
return trading;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
export function useOneEngine() {
|
|
447
|
+
const { engine } = useOne();
|
|
448
|
+
return engine;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// Export context for advanced usage
|
|
452
|
+
export { OneContext };
|