@pear-protocol/hyperliquid-sdk 0.0.73-beta.6 → 0.0.74
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/dist/clients/auth.d.ts +1 -1
- package/dist/hooks/useAuth.d.ts +0 -2
- package/dist/index.d.ts +0 -2
- package/dist/index.js +108 -155
- package/dist/store/userDataStore.d.ts +0 -2
- package/dist/utils/http.d.ts +2 -2
- package/package.json +1 -1
package/dist/clients/auth.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ApiResponse, GetEIP712MessageResponse, AuthenticateRequest, AuthenticateResponse, RefreshTokenResponse, LogoutResponse } from
|
|
1
|
+
import type { ApiResponse, GetEIP712MessageResponse, AuthenticateRequest, AuthenticateResponse, RefreshTokenResponse, LogoutResponse } from '../types';
|
|
2
2
|
export declare function getEIP712Message(baseUrl: string, address: string, clientId: string): Promise<ApiResponse<GetEIP712MessageResponse>>;
|
|
3
3
|
export declare function authenticate(baseUrl: string, body: AuthenticateRequest): Promise<ApiResponse<AuthenticateResponse>>;
|
|
4
4
|
/**
|
package/dist/hooks/useAuth.d.ts
CHANGED
|
@@ -9,6 +9,4 @@ export declare function useAuth(): {
|
|
|
9
9
|
readonly loginWithPrivyToken: (address: string, appId: string, privyAccessToken: string) => Promise<void>;
|
|
10
10
|
readonly refreshTokens: () => Promise<import("../types").RefreshTokenResponse>;
|
|
11
11
|
readonly logout: () => Promise<void>;
|
|
12
|
-
readonly setAddress: (address: string | null) => void;
|
|
13
|
-
readonly address: string | null;
|
|
14
12
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1271,8 +1271,6 @@ declare function useAuth(): {
|
|
|
1271
1271
|
readonly loginWithPrivyToken: (address: string, appId: string, privyAccessToken: string) => Promise<void>;
|
|
1272
1272
|
readonly refreshTokens: () => Promise<RefreshTokenResponse>;
|
|
1273
1273
|
readonly logout: () => Promise<void>;
|
|
1274
|
-
readonly setAddress: (address: string | null) => void;
|
|
1275
|
-
readonly address: string | null;
|
|
1276
1274
|
};
|
|
1277
1275
|
|
|
1278
1276
|
interface AllUserBalances {
|
package/dist/index.js
CHANGED
|
@@ -55,7 +55,6 @@ const useUserData = create((set) => ({
|
|
|
55
55
|
accessToken: null,
|
|
56
56
|
refreshToken: null,
|
|
57
57
|
isAuthenticated: false,
|
|
58
|
-
isReady: false,
|
|
59
58
|
address: null,
|
|
60
59
|
tradeHistories: null,
|
|
61
60
|
rawOpenPositions: null,
|
|
@@ -68,10 +67,17 @@ const useUserData = create((set) => ({
|
|
|
68
67
|
setAccessToken: (token) => set({ accessToken: token }),
|
|
69
68
|
setRefreshToken: (token) => set({ refreshToken: token }),
|
|
70
69
|
setIsAuthenticated: (value) => set({ isAuthenticated: value }),
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
70
|
+
setAddress: (address) => set(() => {
|
|
71
|
+
if (typeof window !== 'undefined') {
|
|
72
|
+
if (address) {
|
|
73
|
+
window.localStorage.setItem('address', address);
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
window.localStorage.removeItem('address');
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return { address };
|
|
80
|
+
}),
|
|
75
81
|
setTradeHistories: (value) => set({ tradeHistories: value }),
|
|
76
82
|
setRawOpenPositions: (value) => set({ rawOpenPositions: value }),
|
|
77
83
|
setOpenOrders: (value) => set({ openOrders: value }),
|
|
@@ -80,11 +86,10 @@ const useUserData = create((set) => ({
|
|
|
80
86
|
setNotifications: (value) => set({ notifications: value }),
|
|
81
87
|
setSpotState: (value) => set({ spotState: value }),
|
|
82
88
|
clean: () => set({
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
// address: null,
|
|
89
|
+
accessToken: null,
|
|
90
|
+
refreshToken: null,
|
|
91
|
+
isAuthenticated: false,
|
|
92
|
+
address: null,
|
|
88
93
|
tradeHistories: null,
|
|
89
94
|
rawOpenPositions: null,
|
|
90
95
|
openOrders: null,
|
|
@@ -1509,6 +1514,20 @@ const useWebData = () => {
|
|
|
1509
1514
|
};
|
|
1510
1515
|
};
|
|
1511
1516
|
|
|
1517
|
+
/**
|
|
1518
|
+
* Check if two symbols match, handling kPEPE/KPEPE variations
|
|
1519
|
+
* Returns true if symbols match (case-insensitive for k-prefix tokens)
|
|
1520
|
+
*/
|
|
1521
|
+
function symbolsMatch(assetName, searchSymbol) {
|
|
1522
|
+
// Exact match
|
|
1523
|
+
if (assetName === searchSymbol)
|
|
1524
|
+
return true;
|
|
1525
|
+
// Try case-insensitive match for k-prefix tokens (kPEPE vs KPEPE)
|
|
1526
|
+
if (assetName.toUpperCase() === searchSymbol.toUpperCase()) {
|
|
1527
|
+
return true;
|
|
1528
|
+
}
|
|
1529
|
+
return false;
|
|
1530
|
+
}
|
|
1512
1531
|
/**
|
|
1513
1532
|
* Extracts token metadata from aggregated WebData3 contexts and AllMids data
|
|
1514
1533
|
*/
|
|
@@ -1529,8 +1548,9 @@ class TokenMetadataExtractor {
|
|
|
1529
1548
|
}
|
|
1530
1549
|
// Find token index in aggregated universe
|
|
1531
1550
|
// For HIP3 assets, match both name AND marketPrefix
|
|
1551
|
+
// Uses symbolsMatch to handle kPEPE/KPEPE case variations
|
|
1532
1552
|
const universeIndex = perpMetaAssets.findIndex((asset) => {
|
|
1533
|
-
if (asset.name
|
|
1553
|
+
if (!symbolsMatch(asset.name, symbol))
|
|
1534
1554
|
return false;
|
|
1535
1555
|
// If marketPrefix is specified, match it; otherwise match assets without prefix
|
|
1536
1556
|
if (marketPrefix) {
|
|
@@ -1549,15 +1569,20 @@ class TokenMetadataExtractor {
|
|
|
1549
1569
|
}
|
|
1550
1570
|
// Get current price - prefer assetCtx.midPx as it's already index-matched,
|
|
1551
1571
|
// fall back to allMids lookup if midPx is null
|
|
1552
|
-
const
|
|
1572
|
+
const actualSymbol = universeAsset.name; // Use actual symbol from universe (handles kPEPE vs KPEPE)
|
|
1573
|
+
const prefixedKeyColon = marketPrefix
|
|
1574
|
+
? `${marketPrefix}:${actualSymbol}`
|
|
1575
|
+
: null;
|
|
1553
1576
|
let currentPrice = 0;
|
|
1554
1577
|
// Primary source: assetCtx.midPx (already properly indexed)
|
|
1555
1578
|
if (assetCtx.midPx) {
|
|
1556
1579
|
currentPrice = parseFloat(assetCtx.midPx);
|
|
1557
1580
|
}
|
|
1558
1581
|
// Fallback: allMids lookup with multiple key formats for HIP3 markets
|
|
1582
|
+
// Try actual symbol from universe first, then input symbol
|
|
1559
1583
|
if (!currentPrice || isNaN(currentPrice)) {
|
|
1560
1584
|
const currentPriceStr = (prefixedKeyColon && allMids.mids[prefixedKeyColon]) ||
|
|
1585
|
+
allMids.mids[actualSymbol] ||
|
|
1561
1586
|
allMids.mids[symbol];
|
|
1562
1587
|
currentPrice = currentPriceStr ? parseFloat(currentPriceStr) : 0;
|
|
1563
1588
|
}
|
|
@@ -1571,10 +1596,12 @@ class TokenMetadataExtractor {
|
|
|
1571
1596
|
const markPrice = parseFloat(assetCtx.markPx);
|
|
1572
1597
|
const oraclePrice = parseFloat(assetCtx.oraclePx);
|
|
1573
1598
|
// Extract leverage info from activeAssetData if available
|
|
1574
|
-
// Try prefixed key first (e.g., "xyz:TSLA"), then
|
|
1599
|
+
// Try prefixed key first (e.g., "xyz:TSLA"), then actual symbol, then input symbol
|
|
1575
1600
|
const activeDataKey = prefixedKeyColon && (activeAssetData === null || activeAssetData === void 0 ? void 0 : activeAssetData[prefixedKeyColon])
|
|
1576
1601
|
? prefixedKeyColon
|
|
1577
|
-
:
|
|
1602
|
+
: (activeAssetData === null || activeAssetData === void 0 ? void 0 : activeAssetData[actualSymbol])
|
|
1603
|
+
? actualSymbol
|
|
1604
|
+
: symbol;
|
|
1578
1605
|
const tokenActiveData = activeAssetData === null || activeAssetData === void 0 ? void 0 : activeAssetData[activeDataKey];
|
|
1579
1606
|
const leverage = tokenActiveData === null || tokenActiveData === void 0 ? void 0 : tokenActiveData.leverage;
|
|
1580
1607
|
const maxTradeSzs = tokenActiveData === null || tokenActiveData === void 0 ? void 0 : tokenActiveData.maxTradeSzs;
|
|
@@ -1626,7 +1653,7 @@ class TokenMetadataExtractor {
|
|
|
1626
1653
|
static isTokenAvailable(symbol, perpMetaAssets) {
|
|
1627
1654
|
if (!perpMetaAssets)
|
|
1628
1655
|
return false;
|
|
1629
|
-
return perpMetaAssets.some((asset) => asset.name
|
|
1656
|
+
return perpMetaAssets.some((asset) => symbolsMatch(asset.name, symbol));
|
|
1630
1657
|
}
|
|
1631
1658
|
}
|
|
1632
1659
|
|
|
@@ -5910,10 +5937,10 @@ function toApiError(error) {
|
|
|
5910
5937
|
var _a;
|
|
5911
5938
|
const axiosError = error;
|
|
5912
5939
|
const payload = (axiosError && axiosError.response ? axiosError.response.data : undefined);
|
|
5913
|
-
const message = typeof payload ===
|
|
5940
|
+
const message = typeof payload === 'object' && payload && 'message' in payload
|
|
5914
5941
|
? String(payload.message)
|
|
5915
|
-
: (axiosError === null || axiosError === void 0 ? void 0 : axiosError.message) ||
|
|
5916
|
-
const errField = typeof payload ===
|
|
5942
|
+
: (axiosError === null || axiosError === void 0 ? void 0 : axiosError.message) || 'Request failed';
|
|
5943
|
+
const errField = typeof payload === 'object' && payload && 'error' in payload
|
|
5917
5944
|
? String(payload.error)
|
|
5918
5945
|
: undefined;
|
|
5919
5946
|
return {
|
|
@@ -5923,8 +5950,8 @@ function toApiError(error) {
|
|
|
5923
5950
|
};
|
|
5924
5951
|
}
|
|
5925
5952
|
function joinUrl(baseUrl, path) {
|
|
5926
|
-
const cleanBase = baseUrl.replace(/\/$/,
|
|
5927
|
-
const cleanPath = path.startsWith(
|
|
5953
|
+
const cleanBase = baseUrl.replace(/\/$/, '');
|
|
5954
|
+
const cleanPath = path.startsWith('/') ? path : `/${path}`;
|
|
5928
5955
|
return `${cleanBase}${cleanPath}`;
|
|
5929
5956
|
}
|
|
5930
5957
|
/**
|
|
@@ -5953,7 +5980,7 @@ function addAuthInterceptors(params) {
|
|
|
5953
5980
|
pendingRequests = [];
|
|
5954
5981
|
}
|
|
5955
5982
|
const isOurApiUrl = (url) => Boolean(url && url.startsWith(apiBaseUrl));
|
|
5956
|
-
const isRefreshUrl = (url) => Boolean(url && url.startsWith(joinUrl(apiBaseUrl,
|
|
5983
|
+
const isRefreshUrl = (url) => Boolean(url && url.startsWith(joinUrl(apiBaseUrl, '/auth/refresh')));
|
|
5957
5984
|
const reqId = apiClient.interceptors.request.use((config) => {
|
|
5958
5985
|
var _a;
|
|
5959
5986
|
try {
|
|
@@ -5961,12 +5988,11 @@ function addAuthInterceptors(params) {
|
|
|
5961
5988
|
const token = getAccessToken();
|
|
5962
5989
|
if (token) {
|
|
5963
5990
|
config.headers = (_a = config.headers) !== null && _a !== void 0 ? _a : {};
|
|
5964
|
-
config.headers[
|
|
5991
|
+
(config.headers)['Authorization'] = `Bearer ${token}`;
|
|
5965
5992
|
}
|
|
5966
5993
|
}
|
|
5967
5994
|
}
|
|
5968
|
-
catch (
|
|
5969
|
-
console.error("[Auth Interceptor] Request interceptor error:", err);
|
|
5995
|
+
catch (_b) {
|
|
5970
5996
|
}
|
|
5971
5997
|
return config;
|
|
5972
5998
|
});
|
|
@@ -5977,36 +6003,22 @@ function addAuthInterceptors(params) {
|
|
|
5977
6003
|
const url = originalRequest === null || originalRequest === void 0 ? void 0 : originalRequest.url;
|
|
5978
6004
|
// If not our API or not 401, just reject
|
|
5979
6005
|
if (!status || status !== 401 || !isOurApiUrl(url)) {
|
|
5980
|
-
if (status === 401) {
|
|
5981
|
-
console.warn("[Auth Interceptor] 401 received but URL check failed:", {
|
|
5982
|
-
url,
|
|
5983
|
-
apiBaseUrl,
|
|
5984
|
-
isOurApiUrl: isOurApiUrl(url),
|
|
5985
|
-
});
|
|
5986
|
-
}
|
|
5987
6006
|
return Promise.reject(error);
|
|
5988
6007
|
}
|
|
5989
|
-
console.log("[Auth Interceptor] 401 detected, attempting token refresh for URL:", url);
|
|
5990
6008
|
// If the 401 is from refresh endpoint itself -> force logout
|
|
5991
6009
|
if (isRefreshUrl(url)) {
|
|
5992
|
-
console.warn("[Auth Interceptor] Refresh endpoint returned 401, logging out");
|
|
5993
6010
|
try {
|
|
5994
6011
|
await logout();
|
|
5995
6012
|
}
|
|
5996
|
-
catch (
|
|
5997
|
-
console.error("[Auth Interceptor] Logout failed:", err);
|
|
5998
|
-
}
|
|
6013
|
+
catch (_d) { }
|
|
5999
6014
|
return Promise.reject(error);
|
|
6000
6015
|
}
|
|
6001
6016
|
// Prevent infinite loop
|
|
6002
6017
|
if (originalRequest && originalRequest._retry) {
|
|
6003
|
-
console.warn("[Auth Interceptor] Request already retried, logging out");
|
|
6004
6018
|
try {
|
|
6005
6019
|
await logout();
|
|
6006
6020
|
}
|
|
6007
|
-
catch (
|
|
6008
|
-
console.error("[Auth Interceptor] Logout failed:", err);
|
|
6009
|
-
}
|
|
6021
|
+
catch (_e) { }
|
|
6010
6022
|
return Promise.reject(error);
|
|
6011
6023
|
}
|
|
6012
6024
|
// Mark so we don't retry twice
|
|
@@ -6020,45 +6032,31 @@ function addAuthInterceptors(params) {
|
|
|
6020
6032
|
if (!newToken || !originalRequest)
|
|
6021
6033
|
return reject(error);
|
|
6022
6034
|
originalRequest.headers = (_a = originalRequest.headers) !== null && _a !== void 0 ? _a : {};
|
|
6023
|
-
originalRequest.headers[
|
|
6024
|
-
`Bearer ${newToken}`;
|
|
6035
|
+
originalRequest.headers['Authorization'] = `Bearer ${newToken}`;
|
|
6025
6036
|
resolve(apiClient.request(originalRequest));
|
|
6026
6037
|
});
|
|
6027
6038
|
});
|
|
6028
6039
|
}
|
|
6029
6040
|
isRefreshing = true;
|
|
6030
6041
|
try {
|
|
6031
|
-
console.log("[Auth Interceptor] Refreshing tokens...");
|
|
6032
6042
|
const refreshed = await refreshTokens();
|
|
6033
|
-
const newAccessToken = (_b = (refreshed &&
|
|
6034
|
-
(refreshed.accessToken || ((_a = refreshed.data) === null || _a === void 0 ? void 0 : _a.accessToken)))) !== null && _b !== void 0 ? _b : null;
|
|
6035
|
-
if (!newAccessToken) {
|
|
6036
|
-
console.error("[Auth Interceptor] Token refresh succeeded but no access token in response:", refreshed);
|
|
6037
|
-
}
|
|
6038
|
-
else {
|
|
6039
|
-
console.log("[Auth Interceptor] Token refresh successful");
|
|
6040
|
-
}
|
|
6043
|
+
const newAccessToken = (_b = (refreshed && (refreshed.accessToken || ((_a = refreshed.data) === null || _a === void 0 ? void 0 : _a.accessToken)))) !== null && _b !== void 0 ? _b : null;
|
|
6041
6044
|
resolvePendingRequests(newAccessToken);
|
|
6042
6045
|
if (originalRequest) {
|
|
6043
6046
|
originalRequest.headers = (_c = originalRequest.headers) !== null && _c !== void 0 ? _c : {};
|
|
6044
6047
|
if (newAccessToken)
|
|
6045
|
-
originalRequest.headers[
|
|
6046
|
-
`Bearer ${newAccessToken}`;
|
|
6047
|
-
console.log("[Auth Interceptor] Retrying original request with new token");
|
|
6048
|
+
(originalRequest.headers)['Authorization'] = `Bearer ${newAccessToken}`;
|
|
6048
6049
|
const resp = await apiClient.request(originalRequest);
|
|
6049
6050
|
return resp;
|
|
6050
6051
|
}
|
|
6051
6052
|
return Promise.reject(error);
|
|
6052
6053
|
}
|
|
6053
6054
|
catch (refreshErr) {
|
|
6054
|
-
console.error("[Auth Interceptor] Token refresh failed:", refreshErr);
|
|
6055
6055
|
resolvePendingRequests(null);
|
|
6056
6056
|
try {
|
|
6057
6057
|
await logout();
|
|
6058
6058
|
}
|
|
6059
|
-
catch (
|
|
6060
|
-
console.error("[Auth Interceptor] Logout failed:", err);
|
|
6061
|
-
}
|
|
6059
|
+
catch (_f) { }
|
|
6062
6060
|
return Promise.reject(refreshErr);
|
|
6063
6061
|
}
|
|
6064
6062
|
finally {
|
|
@@ -6069,15 +6067,11 @@ function addAuthInterceptors(params) {
|
|
|
6069
6067
|
try {
|
|
6070
6068
|
apiClient.interceptors.request.eject(reqId);
|
|
6071
6069
|
}
|
|
6072
|
-
catch (
|
|
6073
|
-
console.error("[Auth Interceptor] Failed to eject request interceptor:", err);
|
|
6074
|
-
}
|
|
6070
|
+
catch (_a) { }
|
|
6075
6071
|
try {
|
|
6076
6072
|
apiClient.interceptors.response.eject(resId);
|
|
6077
6073
|
}
|
|
6078
|
-
catch (
|
|
6079
|
-
console.error("[Auth Interceptor] Failed to eject response interceptor:", err);
|
|
6080
|
-
}
|
|
6074
|
+
catch (_b) { }
|
|
6081
6075
|
};
|
|
6082
6076
|
}
|
|
6083
6077
|
|
|
@@ -8079,34 +8073,20 @@ function usePortfolio() {
|
|
|
8079
8073
|
}
|
|
8080
8074
|
|
|
8081
8075
|
async function getEIP712Message(baseUrl, address, clientId) {
|
|
8082
|
-
const url = joinUrl(baseUrl,
|
|
8076
|
+
const url = joinUrl(baseUrl, '/auth/eip712-message');
|
|
8083
8077
|
try {
|
|
8084
|
-
const resp = await
|
|
8085
|
-
|
|
8086
|
-
timeout: 30000,
|
|
8087
|
-
});
|
|
8088
|
-
return {
|
|
8089
|
-
data: resp.data,
|
|
8090
|
-
status: resp.status,
|
|
8091
|
-
headers: resp.headers,
|
|
8092
|
-
};
|
|
8078
|
+
const resp = await axios$1.get(url, { params: { address, clientId }, timeout: 30000 });
|
|
8079
|
+
return { data: resp.data, status: resp.status, headers: resp.headers };
|
|
8093
8080
|
}
|
|
8094
8081
|
catch (error) {
|
|
8095
8082
|
throw toApiError(error);
|
|
8096
8083
|
}
|
|
8097
8084
|
}
|
|
8098
8085
|
async function authenticate(baseUrl, body) {
|
|
8099
|
-
const url = joinUrl(baseUrl,
|
|
8086
|
+
const url = joinUrl(baseUrl, '/auth/login');
|
|
8100
8087
|
try {
|
|
8101
|
-
const resp = await
|
|
8102
|
-
|
|
8103
|
-
timeout: 30000,
|
|
8104
|
-
});
|
|
8105
|
-
return {
|
|
8106
|
-
data: resp.data,
|
|
8107
|
-
status: resp.status,
|
|
8108
|
-
headers: resp.headers,
|
|
8109
|
-
};
|
|
8088
|
+
const resp = await axios$1.post(url, body, { headers: { 'Content-Type': 'application/json' }, timeout: 30000 });
|
|
8089
|
+
return { data: resp.data, status: resp.status, headers: resp.headers };
|
|
8110
8090
|
}
|
|
8111
8091
|
catch (error) {
|
|
8112
8092
|
throw toApiError(error);
|
|
@@ -8117,7 +8097,7 @@ async function authenticate(baseUrl, body) {
|
|
|
8117
8097
|
*/
|
|
8118
8098
|
async function authenticateWithPrivy(baseUrl, params) {
|
|
8119
8099
|
const body = {
|
|
8120
|
-
method:
|
|
8100
|
+
method: 'privy_access_token',
|
|
8121
8101
|
address: params.address,
|
|
8122
8102
|
clientId: params.clientId,
|
|
8123
8103
|
details: { appId: params.appId, accessToken: params.accessToken },
|
|
@@ -8125,28 +8105,20 @@ async function authenticateWithPrivy(baseUrl, params) {
|
|
|
8125
8105
|
return authenticate(baseUrl, body);
|
|
8126
8106
|
}
|
|
8127
8107
|
async function refreshToken(baseUrl, refreshTokenVal) {
|
|
8128
|
-
const url = joinUrl(baseUrl,
|
|
8108
|
+
const url = joinUrl(baseUrl, '/auth/refresh');
|
|
8129
8109
|
try {
|
|
8130
|
-
const resp = await
|
|
8131
|
-
return {
|
|
8132
|
-
data: resp.data,
|
|
8133
|
-
status: resp.status,
|
|
8134
|
-
headers: resp.headers,
|
|
8135
|
-
};
|
|
8110
|
+
const resp = await axios$1.post(url, { refreshToken: refreshTokenVal }, { headers: { 'Content-Type': 'application/json' }, timeout: 30000 });
|
|
8111
|
+
return { data: resp.data, status: resp.status, headers: resp.headers };
|
|
8136
8112
|
}
|
|
8137
8113
|
catch (error) {
|
|
8138
8114
|
throw toApiError(error);
|
|
8139
8115
|
}
|
|
8140
8116
|
}
|
|
8141
8117
|
async function logout(baseUrl, refreshTokenVal) {
|
|
8142
|
-
const url = joinUrl(baseUrl,
|
|
8118
|
+
const url = joinUrl(baseUrl, '/auth/logout');
|
|
8143
8119
|
try {
|
|
8144
|
-
const resp = await
|
|
8145
|
-
return {
|
|
8146
|
-
data: resp.data,
|
|
8147
|
-
status: resp.status,
|
|
8148
|
-
headers: resp.headers,
|
|
8149
|
-
};
|
|
8120
|
+
const resp = await axios$1.post(url, { refreshToken: refreshTokenVal }, { headers: { 'Content-Type': 'application/json' }, timeout: 30000 });
|
|
8121
|
+
return { data: resp.data, status: resp.status, headers: resp.headers };
|
|
8150
8122
|
}
|
|
8151
8123
|
catch (error) {
|
|
8152
8124
|
throw toApiError(error);
|
|
@@ -8156,50 +8128,38 @@ async function logout(baseUrl, refreshTokenVal) {
|
|
|
8156
8128
|
function useAuth() {
|
|
8157
8129
|
const context = useContext(PearHyperliquidContext);
|
|
8158
8130
|
if (!context) {
|
|
8159
|
-
throw new Error("
|
|
8131
|
+
throw new Error("usePortfolio must be used within a PearHyperliquidProvider");
|
|
8160
8132
|
}
|
|
8161
8133
|
const { apiBaseUrl, clientId } = context;
|
|
8134
|
+
const [isReady, setIsReady] = useState(false);
|
|
8162
8135
|
const accessToken = useUserData((s) => s.accessToken);
|
|
8163
8136
|
const refreshToken$1 = useUserData((s) => s.refreshToken);
|
|
8164
|
-
const isReady = useUserData((s) => s.isReady);
|
|
8165
|
-
const isAuthenticated = useUserData((s) => s.isAuthenticated);
|
|
8166
|
-
const address = useUserData((s) => s.address);
|
|
8167
8137
|
const setAccessToken = useUserData((s) => s.setAccessToken);
|
|
8168
8138
|
const setRefreshToken = useUserData((s) => s.setRefreshToken);
|
|
8169
|
-
const
|
|
8139
|
+
const isAuthenticated = useUserData((s) => s.isAuthenticated);
|
|
8170
8140
|
const setIsAuthenticated = useUserData((s) => s.setIsAuthenticated);
|
|
8171
8141
|
const setAddress = useUserData((s) => s.setAddress);
|
|
8172
8142
|
useEffect(() => {
|
|
8173
8143
|
if (typeof window == "undefined") {
|
|
8174
8144
|
return;
|
|
8175
8145
|
}
|
|
8176
|
-
|
|
8177
|
-
|
|
8178
|
-
|
|
8179
|
-
|
|
8180
|
-
|
|
8181
|
-
|
|
8182
|
-
|
|
8183
|
-
|
|
8184
|
-
setRefreshToken(storedRefreshToken);
|
|
8185
|
-
setIsAuthenticated(true);
|
|
8186
|
-
}
|
|
8187
|
-
else {
|
|
8188
|
-
setAccessToken(null);
|
|
8189
|
-
setRefreshToken(null);
|
|
8190
|
-
setIsAuthenticated(false);
|
|
8191
|
-
}
|
|
8192
|
-
}
|
|
8146
|
+
const access = localStorage.getItem("accessToken");
|
|
8147
|
+
const refresh = localStorage.getItem("refreshToken");
|
|
8148
|
+
const addr = localStorage.getItem("address");
|
|
8149
|
+
setAccessToken(access);
|
|
8150
|
+
setRefreshToken(refresh);
|
|
8151
|
+
setAddress(addr);
|
|
8152
|
+
const authed = Boolean(access && addr);
|
|
8153
|
+
setIsAuthenticated(authed);
|
|
8193
8154
|
setIsReady(true);
|
|
8194
|
-
}, [
|
|
8155
|
+
}, [setAccessToken, setRefreshToken, setIsAuthenticated, setAddress]);
|
|
8195
8156
|
useEffect(() => {
|
|
8196
8157
|
const cleanup = addAuthInterceptors({
|
|
8197
8158
|
apiBaseUrl,
|
|
8198
8159
|
getAccessToken: () => {
|
|
8199
|
-
|
|
8200
|
-
|
|
8201
|
-
|
|
8202
|
-
return useUserData.getState().accessToken;
|
|
8160
|
+
return typeof window !== "undefined"
|
|
8161
|
+
? window.localStorage.getItem("accessToken")
|
|
8162
|
+
: null;
|
|
8203
8163
|
},
|
|
8204
8164
|
refreshTokens: async () => {
|
|
8205
8165
|
const data = await refreshTokens();
|
|
@@ -8225,12 +8185,12 @@ function useAuth() {
|
|
|
8225
8185
|
clientId,
|
|
8226
8186
|
details: { signature, timestamp },
|
|
8227
8187
|
});
|
|
8228
|
-
|
|
8229
|
-
|
|
8230
|
-
window.localStorage.setItem(
|
|
8231
|
-
window.localStorage.setItem(refreshTokenKey, data.refreshToken);
|
|
8188
|
+
window.localStorage.setItem("accessToken", data.accessToken);
|
|
8189
|
+
window.localStorage.setItem("refreshToken", data.refreshToken);
|
|
8190
|
+
window.localStorage.setItem("address", address);
|
|
8232
8191
|
setAccessToken(data.accessToken);
|
|
8233
8192
|
setRefreshToken(data.refreshToken);
|
|
8193
|
+
setAddress(address);
|
|
8234
8194
|
setIsAuthenticated(true);
|
|
8235
8195
|
}
|
|
8236
8196
|
catch (e) {
|
|
@@ -8245,12 +8205,12 @@ function useAuth() {
|
|
|
8245
8205
|
appId,
|
|
8246
8206
|
accessToken: privyAccessToken,
|
|
8247
8207
|
});
|
|
8248
|
-
|
|
8249
|
-
|
|
8250
|
-
window.localStorage.setItem(
|
|
8251
|
-
window.localStorage.setItem(refreshTokenKey, data.refreshToken);
|
|
8208
|
+
window.localStorage.setItem("accessToken", data.accessToken);
|
|
8209
|
+
window.localStorage.setItem("refreshToken", data.refreshToken);
|
|
8210
|
+
window.localStorage.setItem("address", address);
|
|
8252
8211
|
setAccessToken(data.accessToken);
|
|
8253
8212
|
setRefreshToken(data.refreshToken);
|
|
8213
|
+
setAddress(address);
|
|
8254
8214
|
setIsAuthenticated(true);
|
|
8255
8215
|
}
|
|
8256
8216
|
catch (e) {
|
|
@@ -8258,38 +8218,30 @@ function useAuth() {
|
|
|
8258
8218
|
}
|
|
8259
8219
|
}
|
|
8260
8220
|
async function refreshTokens() {
|
|
8261
|
-
const
|
|
8262
|
-
|
|
8263
|
-
if (!currentRefresh || !currentAddress)
|
|
8221
|
+
const refresh = window.localStorage.getItem("refreshToken");
|
|
8222
|
+
if (!refresh)
|
|
8264
8223
|
throw new Error("No refresh token");
|
|
8265
|
-
const { data } = await refreshToken(apiBaseUrl,
|
|
8266
|
-
|
|
8267
|
-
|
|
8268
|
-
const refreshTokenKey = `${currentAddress}_refreshToken`;
|
|
8269
|
-
window.localStorage.setItem(accessTokenKey, data.accessToken);
|
|
8270
|
-
window.localStorage.setItem(refreshTokenKey, data.refreshToken);
|
|
8224
|
+
const { data } = await refreshToken(apiBaseUrl, refresh);
|
|
8225
|
+
window.localStorage.setItem("accessToken", data.accessToken);
|
|
8226
|
+
window.localStorage.setItem("refreshToken", data.refreshToken);
|
|
8271
8227
|
setAccessToken(data.accessToken);
|
|
8272
8228
|
setRefreshToken(data.refreshToken);
|
|
8273
8229
|
setIsAuthenticated(true);
|
|
8274
8230
|
return data;
|
|
8275
8231
|
}
|
|
8276
8232
|
async function logout$1() {
|
|
8277
|
-
const
|
|
8278
|
-
|
|
8279
|
-
if (currentRefresh) {
|
|
8233
|
+
const refresh = window.localStorage.getItem("refreshToken");
|
|
8234
|
+
if (refresh) {
|
|
8280
8235
|
try {
|
|
8281
|
-
await logout(apiBaseUrl,
|
|
8236
|
+
await logout(apiBaseUrl, refresh);
|
|
8282
8237
|
}
|
|
8283
8238
|
catch (_a) {
|
|
8284
8239
|
/* ignore */
|
|
8285
8240
|
}
|
|
8286
8241
|
}
|
|
8287
|
-
|
|
8288
|
-
|
|
8289
|
-
|
|
8290
|
-
window.localStorage.removeItem(accessTokenKey);
|
|
8291
|
-
window.localStorage.removeItem(refreshTokenKey);
|
|
8292
|
-
}
|
|
8242
|
+
window.localStorage.removeItem("accessToken");
|
|
8243
|
+
window.localStorage.removeItem("refreshToken");
|
|
8244
|
+
window.localStorage.removeItem("address");
|
|
8293
8245
|
setAccessToken(null);
|
|
8294
8246
|
setRefreshToken(null);
|
|
8295
8247
|
setAddress(null);
|
|
@@ -8305,8 +8257,6 @@ function useAuth() {
|
|
|
8305
8257
|
loginWithPrivyToken,
|
|
8306
8258
|
refreshTokens,
|
|
8307
8259
|
logout: logout$1,
|
|
8308
|
-
setAddress,
|
|
8309
|
-
address,
|
|
8310
8260
|
};
|
|
8311
8261
|
}
|
|
8312
8262
|
|
|
@@ -8409,6 +8359,7 @@ const PearHyperliquidContext = createContext(undefined);
|
|
|
8409
8359
|
*/
|
|
8410
8360
|
const PearHyperliquidProvider = ({ children, apiBaseUrl = 'https://hl-ui.pearprotocol.io', clientId = 'PEARPROTOCOLUI', wsUrl = 'wss://hl-ui.pearprotocol.io/ws', }) => {
|
|
8411
8361
|
const address = useUserData((s) => s.address);
|
|
8362
|
+
const setAddress = useUserData((s) => s.setAddress);
|
|
8412
8363
|
const perpsMetaAssets = useHyperliquidData((state) => state.perpMetaAssets);
|
|
8413
8364
|
const setPerpMetaAssets = useHyperliquidData((state) => state.setPerpMetaAssets);
|
|
8414
8365
|
const setAllPerpMetaAssets = useHyperliquidData((state) => state.setAllPerpMetaAssets);
|
|
@@ -8550,6 +8501,8 @@ const PearHyperliquidProvider = ({ children, apiBaseUrl = 'https://hl-ui.pearpro
|
|
|
8550
8501
|
}), [
|
|
8551
8502
|
apiBaseUrl,
|
|
8552
8503
|
wsUrl,
|
|
8504
|
+
address,
|
|
8505
|
+
setAddress,
|
|
8553
8506
|
isConnected,
|
|
8554
8507
|
lastError,
|
|
8555
8508
|
nativeIsConnected,
|
|
@@ -3,7 +3,6 @@ interface UserDataState {
|
|
|
3
3
|
accessToken: string | null;
|
|
4
4
|
refreshToken: string | null;
|
|
5
5
|
isAuthenticated: boolean;
|
|
6
|
-
isReady: boolean;
|
|
7
6
|
address: string | null;
|
|
8
7
|
tradeHistories: TradeHistoryDataDto[] | null;
|
|
9
8
|
rawOpenPositions: RawPositionDto[] | null;
|
|
@@ -16,7 +15,6 @@ interface UserDataState {
|
|
|
16
15
|
setAccessToken: (token: string | null) => void;
|
|
17
16
|
setRefreshToken: (token: string | null) => void;
|
|
18
17
|
setIsAuthenticated: (value: boolean) => void;
|
|
19
|
-
setIsReady: (value: boolean) => void;
|
|
20
18
|
setAddress: (address: string | null) => void;
|
|
21
19
|
setTradeHistories: (value: TradeHistoryDataDto[] | null) => void;
|
|
22
20
|
setRawOpenPositions: (value: RawPositionDto[] | null) => void;
|
package/dist/utils/http.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { AxiosInstance } from
|
|
2
|
-
import { ApiErrorResponse } from
|
|
1
|
+
import type { AxiosInstance } from 'axios';
|
|
2
|
+
import { ApiErrorResponse } from '../types';
|
|
3
3
|
export declare function toApiError(error: unknown): ApiErrorResponse;
|
|
4
4
|
export declare function joinUrl(baseUrl: string, path: string): string;
|
|
5
5
|
/**
|