@0xmonaco/core 0.7.7 → 0.7.8
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/api/applications/api.d.ts +43 -0
- package/dist/api/applications/api.js +54 -0
- package/dist/api/applications/index.d.ts +4 -0
- package/dist/api/applications/index.js +4 -0
- package/dist/api/auth/api.d.ts +206 -0
- package/dist/api/auth/api.js +305 -0
- package/dist/api/auth/index.d.ts +4 -0
- package/dist/api/auth/index.js +4 -0
- package/dist/api/base.d.ts +123 -0
- package/dist/api/base.js +286 -0
- package/dist/api/fees/api.d.ts +72 -0
- package/dist/api/fees/api.js +90 -0
- package/dist/api/fees/index.d.ts +6 -0
- package/dist/api/fees/index.js +6 -0
- package/dist/api/index.d.ts +18 -0
- package/dist/api/index.js +18 -0
- package/dist/api/margin-accounts/api.d.ts +12 -0
- package/dist/api/margin-accounts/api.js +69 -0
- package/dist/api/margin-accounts/index.d.ts +1 -0
- package/dist/api/margin-accounts/index.js +1 -0
- package/dist/api/market/api.d.ts +20 -0
- package/dist/api/market/api.js +97 -0
- package/dist/api/market/index.d.ts +1 -0
- package/dist/api/market/index.js +1 -0
- package/dist/api/orderbook/api.d.ts +15 -0
- package/dist/api/orderbook/api.js +37 -0
- package/dist/api/orderbook/index.d.ts +1 -0
- package/dist/api/orderbook/index.js +1 -0
- package/dist/api/perp/index.d.ts +1 -0
- package/dist/api/perp/index.js +1 -0
- package/dist/api/perp/routes.d.ts +133 -0
- package/dist/api/perp/routes.js +85 -0
- package/dist/api/positions/api.d.ts +12 -0
- package/dist/api/positions/api.js +86 -0
- package/dist/api/positions/index.d.ts +1 -0
- package/dist/api/positions/index.js +1 -0
- package/dist/api/profile/api.d.ts +191 -0
- package/dist/api/profile/api.js +259 -0
- package/dist/api/profile/index.d.ts +6 -0
- package/dist/api/profile/index.js +6 -0
- package/dist/api/trades/api.d.ts +44 -0
- package/dist/api/trades/api.js +42 -0
- package/dist/api/trades/index.d.ts +1 -0
- package/dist/api/trades/index.js +1 -0
- package/dist/api/trading/api.d.ts +297 -0
- package/dist/api/trading/api.js +481 -0
- package/dist/api/trading/index.d.ts +4 -0
- package/dist/api/trading/index.js +4 -0
- package/dist/api/vault/api.d.ts +261 -0
- package/dist/api/vault/api.js +506 -0
- package/dist/api/vault/index.d.ts +4 -0
- package/dist/api/vault/index.js +4 -0
- package/dist/api/websocket/index.d.ts +3 -0
- package/dist/api/websocket/index.js +3 -0
- package/dist/api/websocket/types.d.ts +41 -0
- package/dist/api/websocket/types.js +0 -0
- package/dist/api/websocket/utils.d.ts +8 -0
- package/dist/api/websocket/utils.js +22 -0
- package/dist/api/websocket/websocket.d.ts +5 -0
- package/dist/api/websocket/websocket.js +556 -0
- package/dist/errors/errors.d.ts +381 -0
- package/dist/errors/errors.js +815 -0
- package/dist/errors/index.d.ts +1 -0
- package/dist/errors/index.js +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/networks/index.d.ts +1 -0
- package/dist/networks/index.js +1 -0
- package/dist/networks/networks.d.ts +21 -0
- package/dist/networks/networks.js +46 -0
- package/dist/sdk.d.ts +134 -0
- package/dist/sdk.js +294 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/magnitude.d.ts +26 -0
- package/dist/utils/magnitude.js +31 -0
- package/package.json +3 -3
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./errors";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./errors";
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { createMonacoWebSocket, type MonacoWebSocket, type MonacoWebSocketOptions } from "./api/websocket";
|
|
2
|
+
export { APIError, ContractError, InvalidConfigError, InvalidStateError, MonacoCoreError } from "./errors";
|
|
3
|
+
export { resolveApiUrl, resolveWsUrl } from "./networks";
|
|
4
|
+
export { createMonacoSDK, MonacoSDKImpl as MonacoSDK } from "./sdk";
|
|
5
|
+
export { ALL_MAGNITUDES, calculateValidMagnitudes, MAX_BUCKETS_ALLOWED, MIN_BUCKETS_ALLOWED, } from "./utils";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { createMonacoWebSocket } from "./api/websocket";
|
|
2
|
+
export { APIError, ContractError, InvalidConfigError, InvalidStateError, MonacoCoreError } from "./errors";
|
|
3
|
+
export { resolveApiUrl, resolveWsUrl } from "./networks";
|
|
4
|
+
export { createMonacoSDK, MonacoSDKImpl as MonacoSDK } from "./sdk";
|
|
5
|
+
export { ALL_MAGNITUDES, calculateValidMagnitudes, MAX_BUCKETS_ALLOWED, MIN_BUCKETS_ALLOWED, } from "./utils";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./networks";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./networks";
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Monaco SDK Network Configuration
|
|
3
|
+
*
|
|
4
|
+
* Network configuration and endpoints with hardcoded defaults.
|
|
5
|
+
* No environment variables are required - all configuration is provided at runtime.
|
|
6
|
+
*/
|
|
7
|
+
import type { Network } from "@0xmonaco/types";
|
|
8
|
+
/**
|
|
9
|
+
* Resolve the API URL based on network
|
|
10
|
+
*
|
|
11
|
+
* @param network - Network preset name
|
|
12
|
+
* @returns The resolved API URL
|
|
13
|
+
*/
|
|
14
|
+
export declare function resolveApiUrl(network: Network): string;
|
|
15
|
+
/**
|
|
16
|
+
* Resolve the WebSocket URL based on network
|
|
17
|
+
*
|
|
18
|
+
* @param network - Network preset name
|
|
19
|
+
* @returns The resolved WebSocket URL
|
|
20
|
+
*/
|
|
21
|
+
export declare function resolveWsUrl(network: Network): string;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Monaco SDK Network Configuration
|
|
3
|
+
*
|
|
4
|
+
* Network configuration and endpoints with hardcoded defaults.
|
|
5
|
+
* No environment variables are required - all configuration is provided at runtime.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Default API URLs for preset networks
|
|
9
|
+
*/
|
|
10
|
+
const LOCAL_URL = "http://localhost:8080";
|
|
11
|
+
const DEVELOPMENT_URL = "https://develop.apimonaco.xyz";
|
|
12
|
+
const STAGING_URL = "https://staging.apimonaco.xyz";
|
|
13
|
+
const MAINNET_URL = "https://api.monaco.xyz";
|
|
14
|
+
const DEFAULT_API_URLS = {
|
|
15
|
+
local: LOCAL_URL,
|
|
16
|
+
development: DEVELOPMENT_URL,
|
|
17
|
+
staging: STAGING_URL,
|
|
18
|
+
mainnet: MAINNET_URL,
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Default WebSocket URLs for preset networks
|
|
22
|
+
*/
|
|
23
|
+
const DEFAULT_WS_URLS = {
|
|
24
|
+
local: "ws://localhost:8085/ws",
|
|
25
|
+
development: "wss://develop.apimonaco.xyz/ws",
|
|
26
|
+
staging: "wss://staging.apimonaco.xyz/ws",
|
|
27
|
+
mainnet: "wss://api.monaco.xyz/ws",
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Resolve the API URL based on network
|
|
31
|
+
*
|
|
32
|
+
* @param network - Network preset name
|
|
33
|
+
* @returns The resolved API URL
|
|
34
|
+
*/
|
|
35
|
+
export function resolveApiUrl(network) {
|
|
36
|
+
return DEFAULT_API_URLS[network];
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Resolve the WebSocket URL based on network
|
|
40
|
+
*
|
|
41
|
+
* @param network - Network preset name
|
|
42
|
+
* @returns The resolved WebSocket URL
|
|
43
|
+
*/
|
|
44
|
+
export function resolveWsUrl(network) {
|
|
45
|
+
return DEFAULT_WS_URLS[network];
|
|
46
|
+
}
|
package/dist/sdk.d.ts
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import type { ApplicationsAPI, AuthAPI, AuthState, FeesAPI, MarginAccountsAPI, MarketAPI, MonacoSDK, Network, PositionsAPI, ProfileAPI, SDKConfig, TradingAPI, VaultAPI } from "@0xmonaco/types";
|
|
2
|
+
import { type PublicClient, type TransactionReceipt, type WalletClient } from "viem";
|
|
3
|
+
import { type MonacoWebSocket, OrderbookAPIImpl, TradesAPIImpl } from "./api";
|
|
4
|
+
export declare class MonacoSDKImpl implements MonacoSDK {
|
|
5
|
+
auth: AuthAPI;
|
|
6
|
+
applications: ApplicationsAPI;
|
|
7
|
+
fees: FeesAPI;
|
|
8
|
+
vault: VaultAPI;
|
|
9
|
+
trading: TradingAPI;
|
|
10
|
+
market: MarketAPI;
|
|
11
|
+
marginAccounts: MarginAccountsAPI;
|
|
12
|
+
positions: PositionsAPI;
|
|
13
|
+
profile: ProfileAPI;
|
|
14
|
+
orderbook: OrderbookAPIImpl;
|
|
15
|
+
trades: TradesAPIImpl;
|
|
16
|
+
ws: MonacoWebSocket;
|
|
17
|
+
walletClient: WalletClient | undefined;
|
|
18
|
+
publicClient: PublicClient;
|
|
19
|
+
private authState?;
|
|
20
|
+
private readonly network;
|
|
21
|
+
private readonly chain;
|
|
22
|
+
/**
|
|
23
|
+
* Propagate the access token to all APIs and WebSocket
|
|
24
|
+
*/
|
|
25
|
+
private propagateAccessToken;
|
|
26
|
+
constructor(cfg: SDKConfig);
|
|
27
|
+
/**
|
|
28
|
+
* Authenticate the user
|
|
29
|
+
*
|
|
30
|
+
* Returns an AuthState object containing:
|
|
31
|
+
* - `accessToken`: For making authenticated API requests
|
|
32
|
+
* - `refreshToken`: For refreshing tokens AND revoking (logout)
|
|
33
|
+
* - `expiresAt`: When the access token expires
|
|
34
|
+
* - `user`: User information
|
|
35
|
+
*
|
|
36
|
+
* Note: Use `sdk.logout()` to revoke the token and clean up, or call
|
|
37
|
+
* `sdk.auth.revokeToken()` directly to just revoke.
|
|
38
|
+
*
|
|
39
|
+
* @param clientId - The client ID for authentication
|
|
40
|
+
* @param options - Optional configuration
|
|
41
|
+
* @param options.connectWebSocket - Auto-connect WebSocket after login (default: false)
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* // Login only
|
|
46
|
+
* const authState = await sdk.login(clientId);
|
|
47
|
+
*
|
|
48
|
+
* // Login and auto-connect WebSocket
|
|
49
|
+
* const authState = await sdk.login(clientId, { connectWebSocket: true });
|
|
50
|
+
*
|
|
51
|
+
* // Manual WebSocket connection
|
|
52
|
+
* await sdk.ws.connect();
|
|
53
|
+
*
|
|
54
|
+
* // Later, to revoke:
|
|
55
|
+
* await sdk.auth.revokeToken(); // ✅
|
|
56
|
+
* // Or revoke and disconnect WebSocket:
|
|
57
|
+
* await sdk.logout(); // ✅ Calls revokeToken internally and disconnects WebSocket
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
login(clientId: string, options?: {
|
|
61
|
+
connectWebSocket?: boolean;
|
|
62
|
+
}): Promise<AuthState>;
|
|
63
|
+
/**
|
|
64
|
+
* Get the current authentication state
|
|
65
|
+
*
|
|
66
|
+
* @returns The current authentication state or undefined if not authenticated
|
|
67
|
+
*/
|
|
68
|
+
getAuthState(): AuthState | undefined;
|
|
69
|
+
/**
|
|
70
|
+
* Set the authentication state directly
|
|
71
|
+
* Useful for sharing authentication across multiple SDK instances (e.g., in tests)
|
|
72
|
+
*
|
|
73
|
+
* @param authState - The authentication state to set
|
|
74
|
+
*/
|
|
75
|
+
setAuthState(authState: AuthState): void;
|
|
76
|
+
/**
|
|
77
|
+
* Log the user out
|
|
78
|
+
*
|
|
79
|
+
* This method revokes the refresh token (if available), disconnects all authenticated
|
|
80
|
+
* WebSocket channels, and clears the local auth state.
|
|
81
|
+
* It internally calls `auth.revokeToken()` to invalidate the token on the server.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```typescript
|
|
85
|
+
* await sdk.logout();
|
|
86
|
+
* // Token is revoked, authenticated WebSockets disconnected, and local state cleared
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
logout(): Promise<void>;
|
|
90
|
+
/**
|
|
91
|
+
* Refresh the access token
|
|
92
|
+
* @returns The updated authentication state
|
|
93
|
+
*/
|
|
94
|
+
refreshAuth(): Promise<AuthState>;
|
|
95
|
+
/**
|
|
96
|
+
* Check if the user is authenticated
|
|
97
|
+
*/
|
|
98
|
+
isAuthenticated(): boolean;
|
|
99
|
+
/**
|
|
100
|
+
* Check connection status
|
|
101
|
+
*/
|
|
102
|
+
isConnected(): boolean;
|
|
103
|
+
/**
|
|
104
|
+
* Set the wallet client
|
|
105
|
+
* Used by React SDK to update the wallet when wagmi becomes available
|
|
106
|
+
*/
|
|
107
|
+
setWalletClient(walletClient: WalletClient): void;
|
|
108
|
+
/**
|
|
109
|
+
* Get the current account address
|
|
110
|
+
*/
|
|
111
|
+
getAccountAddress(): string;
|
|
112
|
+
/**
|
|
113
|
+
* Get the current network
|
|
114
|
+
*/
|
|
115
|
+
getNetwork(): Network;
|
|
116
|
+
/**
|
|
117
|
+
* Get the current chain ID
|
|
118
|
+
*/
|
|
119
|
+
getChainId(): number;
|
|
120
|
+
/**
|
|
121
|
+
* Wait for a transaction to be confirmed
|
|
122
|
+
* @param hash - The transaction hash
|
|
123
|
+
* @param confirmations - Number of confirmations to wait for (optional)
|
|
124
|
+
* @param timeout - Timeout in milliseconds (optional)
|
|
125
|
+
* @returns The transaction receipt
|
|
126
|
+
*/
|
|
127
|
+
waitForTransaction(hash: string, confirmations?: number, timeout?: number): Promise<TransactionReceipt>;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Create a new Monaco SDK instance
|
|
131
|
+
* @param config - SDK configuration
|
|
132
|
+
* @returns Monaco SDK instance
|
|
133
|
+
*/
|
|
134
|
+
export declare function createMonacoSDK(config: SDKConfig): MonacoSDK;
|
package/dist/sdk.js
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { StatusCodes } from "http-status-codes";
|
|
2
|
+
import { createPublicClient, http } from "viem";
|
|
3
|
+
import { sei, seiTestnet } from "viem/chains";
|
|
4
|
+
import { ApplicationsAPIImpl, createMonacoWebSocket, OrderbookAPIImpl, TradesAPIImpl } from "./api";
|
|
5
|
+
import { AuthAPIImpl } from "./api/auth";
|
|
6
|
+
import { FeesAPIImpl } from "./api/fees";
|
|
7
|
+
import { MarginAccountsAPIImpl } from "./api/margin-accounts";
|
|
8
|
+
import { MarketAPIImpl } from "./api/market";
|
|
9
|
+
import { PositionsAPIImpl } from "./api/positions";
|
|
10
|
+
import { ProfileAPIImpl } from "./api/profile";
|
|
11
|
+
import { TradingAPIImpl } from "./api/trading";
|
|
12
|
+
import { VaultAPIImpl } from "./api/vault";
|
|
13
|
+
import { APIError, InvalidConfigError, InvalidStateError } from "./errors";
|
|
14
|
+
import { resolveApiUrl, resolveWsUrl } from "./networks";
|
|
15
|
+
export class MonacoSDKImpl {
|
|
16
|
+
auth;
|
|
17
|
+
applications;
|
|
18
|
+
fees;
|
|
19
|
+
vault;
|
|
20
|
+
trading;
|
|
21
|
+
market;
|
|
22
|
+
marginAccounts;
|
|
23
|
+
positions;
|
|
24
|
+
profile;
|
|
25
|
+
orderbook;
|
|
26
|
+
trades;
|
|
27
|
+
ws;
|
|
28
|
+
walletClient;
|
|
29
|
+
publicClient;
|
|
30
|
+
authState;
|
|
31
|
+
network;
|
|
32
|
+
chain;
|
|
33
|
+
/**
|
|
34
|
+
* Propagate the access token to all APIs and WebSocket
|
|
35
|
+
*/
|
|
36
|
+
propagateAccessToken(accessToken) {
|
|
37
|
+
this.auth.setAccessToken(accessToken);
|
|
38
|
+
this.applications.setAccessToken(accessToken);
|
|
39
|
+
this.fees.setAccessToken(accessToken);
|
|
40
|
+
this.vault.setAccessToken(accessToken);
|
|
41
|
+
this.trading.setAccessToken(accessToken);
|
|
42
|
+
this.market.setAccessToken(accessToken);
|
|
43
|
+
this.marginAccounts.setAccessToken(accessToken);
|
|
44
|
+
this.positions.setAccessToken(accessToken);
|
|
45
|
+
this.profile.setAccessToken(accessToken);
|
|
46
|
+
this.orderbook.setAccessToken(accessToken);
|
|
47
|
+
this.trades.setAccessToken(accessToken);
|
|
48
|
+
this.ws.setToken(accessToken);
|
|
49
|
+
}
|
|
50
|
+
constructor(cfg) {
|
|
51
|
+
// Validate network - must be a preset
|
|
52
|
+
const presetNetworks = ["mainnet", "development", "staging", "local"];
|
|
53
|
+
if (!cfg.network || !presetNetworks.includes(cfg.network)) {
|
|
54
|
+
throw new InvalidConfigError(`network must be one of: ${presetNetworks.join(", ")}. Got: ${cfg.network}`, "network");
|
|
55
|
+
}
|
|
56
|
+
this.network = cfg.network;
|
|
57
|
+
// Validate seiRpcUrl is provided
|
|
58
|
+
if (!cfg.seiRpcUrl) {
|
|
59
|
+
throw new InvalidConfigError("seiRpcUrl is required", "seiRpcUrl");
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
new URL(cfg.seiRpcUrl);
|
|
63
|
+
}
|
|
64
|
+
catch (_e) {
|
|
65
|
+
throw new InvalidConfigError(`seiRpcUrl must be a valid URL, got: ${cfg.seiRpcUrl}`, "seiRpcUrl");
|
|
66
|
+
}
|
|
67
|
+
// Infer WebSocket URL from network
|
|
68
|
+
const wsUrl = resolveWsUrl(this.network);
|
|
69
|
+
// Use Sei mainnet chain only for "mainnet" network, testnet for everything else
|
|
70
|
+
const chain = this.network === "mainnet" ? sei : seiTestnet;
|
|
71
|
+
this.chain = chain;
|
|
72
|
+
// Create public client with provided RPC URL
|
|
73
|
+
const transport = http(cfg.seiRpcUrl);
|
|
74
|
+
this.publicClient = createPublicClient({
|
|
75
|
+
chain,
|
|
76
|
+
transport,
|
|
77
|
+
});
|
|
78
|
+
// Resolve the API URL (from preset or custom URL)
|
|
79
|
+
const apiUrl = resolveApiUrl(this.network);
|
|
80
|
+
// Validate wallet client chain if provided
|
|
81
|
+
if (cfg.walletClient) {
|
|
82
|
+
if (cfg.walletClient.chain?.id !== chain.id) {
|
|
83
|
+
throw new InvalidConfigError(`Wallet client chain mismatch. Expected ${chain.id}, got ${cfg.walletClient.chain?.id}`, "walletClient");
|
|
84
|
+
}
|
|
85
|
+
this.walletClient = cfg.walletClient;
|
|
86
|
+
}
|
|
87
|
+
// Instantiate APIs (wallet-dependent APIs will be initialized lazily or error if wallet not set)
|
|
88
|
+
this.applications = new ApplicationsAPIImpl(apiUrl);
|
|
89
|
+
this.market = new MarketAPIImpl(apiUrl);
|
|
90
|
+
this.marginAccounts = new MarginAccountsAPIImpl(apiUrl);
|
|
91
|
+
this.positions = new PositionsAPIImpl(apiUrl);
|
|
92
|
+
this.auth = new AuthAPIImpl(this.walletClient, this.chain, apiUrl);
|
|
93
|
+
this.fees = new FeesAPIImpl(apiUrl);
|
|
94
|
+
this.profile = new ProfileAPIImpl(apiUrl);
|
|
95
|
+
this.vault = new VaultAPIImpl(this.publicClient, this.walletClient, this.chain, this.applications, this.profile, apiUrl);
|
|
96
|
+
this.trading = new TradingAPIImpl(apiUrl);
|
|
97
|
+
this.orderbook = new OrderbookAPIImpl(apiUrl);
|
|
98
|
+
this.trades = new TradesAPIImpl(apiUrl);
|
|
99
|
+
this.ws = createMonacoWebSocket(wsUrl);
|
|
100
|
+
// Auto-connect WebSocket, log errors
|
|
101
|
+
this.ws.connect().catch(console.error);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Authenticate the user
|
|
105
|
+
*
|
|
106
|
+
* Returns an AuthState object containing:
|
|
107
|
+
* - `accessToken`: For making authenticated API requests
|
|
108
|
+
* - `refreshToken`: For refreshing tokens AND revoking (logout)
|
|
109
|
+
* - `expiresAt`: When the access token expires
|
|
110
|
+
* - `user`: User information
|
|
111
|
+
*
|
|
112
|
+
* Note: Use `sdk.logout()` to revoke the token and clean up, or call
|
|
113
|
+
* `sdk.auth.revokeToken()` directly to just revoke.
|
|
114
|
+
*
|
|
115
|
+
* @param clientId - The client ID for authentication
|
|
116
|
+
* @param options - Optional configuration
|
|
117
|
+
* @param options.connectWebSocket - Auto-connect WebSocket after login (default: false)
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```typescript
|
|
121
|
+
* // Login only
|
|
122
|
+
* const authState = await sdk.login(clientId);
|
|
123
|
+
*
|
|
124
|
+
* // Login and auto-connect WebSocket
|
|
125
|
+
* const authState = await sdk.login(clientId, { connectWebSocket: true });
|
|
126
|
+
*
|
|
127
|
+
* // Manual WebSocket connection
|
|
128
|
+
* await sdk.ws.connect();
|
|
129
|
+
*
|
|
130
|
+
* // Later, to revoke:
|
|
131
|
+
* await sdk.auth.revokeToken(); // ✅
|
|
132
|
+
* // Or revoke and disconnect WebSocket:
|
|
133
|
+
* await sdk.logout(); // ✅ Calls revokeToken internally and disconnects WebSocket
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
async login(clientId, options) {
|
|
137
|
+
const response = await this.auth.authenticate(clientId);
|
|
138
|
+
this.authState = {
|
|
139
|
+
accessToken: response.accessToken,
|
|
140
|
+
refreshToken: response.refreshToken,
|
|
141
|
+
expiresAt: response.expiresAt,
|
|
142
|
+
user: response.user,
|
|
143
|
+
};
|
|
144
|
+
this.propagateAccessToken(this.authState.accessToken);
|
|
145
|
+
// Auto-connect WebSocket if requested
|
|
146
|
+
if (options?.connectWebSocket) {
|
|
147
|
+
await this.ws.connect();
|
|
148
|
+
}
|
|
149
|
+
return this.authState;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Get the current authentication state
|
|
153
|
+
*
|
|
154
|
+
* @returns The current authentication state or undefined if not authenticated
|
|
155
|
+
*/
|
|
156
|
+
getAuthState() {
|
|
157
|
+
return this.authState;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Set the authentication state directly
|
|
161
|
+
* Useful for sharing authentication across multiple SDK instances (e.g., in tests)
|
|
162
|
+
*
|
|
163
|
+
* @param authState - The authentication state to set
|
|
164
|
+
*/
|
|
165
|
+
setAuthState(authState) {
|
|
166
|
+
this.authState = authState;
|
|
167
|
+
this.propagateAccessToken(authState.accessToken);
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Log the user out
|
|
171
|
+
*
|
|
172
|
+
* This method revokes the refresh token (if available), disconnects all authenticated
|
|
173
|
+
* WebSocket channels, and clears the local auth state.
|
|
174
|
+
* It internally calls `auth.revokeToken()` to invalidate the token on the server.
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```typescript
|
|
178
|
+
* await sdk.logout();
|
|
179
|
+
* // Token is revoked, authenticated WebSockets disconnected, and local state cleared
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
async logout() {
|
|
183
|
+
if (this.authState?.refreshToken) {
|
|
184
|
+
try {
|
|
185
|
+
await this.auth.revokeToken();
|
|
186
|
+
}
|
|
187
|
+
catch (error) {
|
|
188
|
+
// Log but don't throw - we want to clear the local state regardless
|
|
189
|
+
console.warn("Failed to revoke token on logout:", error);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
this.authState = undefined;
|
|
193
|
+
this.propagateAccessToken("");
|
|
194
|
+
this.ws.disconnect();
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Refresh the access token
|
|
198
|
+
* @returns The updated authentication state
|
|
199
|
+
*/
|
|
200
|
+
async refreshAuth() {
|
|
201
|
+
if (!this.authState?.refreshToken) {
|
|
202
|
+
throw new APIError("No refresh token available", {
|
|
203
|
+
endpoint: "auth/refresh",
|
|
204
|
+
statusCode: StatusCodes.UNAUTHORIZED,
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
try {
|
|
208
|
+
const response = await this.auth.refreshToken(this.authState.refreshToken);
|
|
209
|
+
this.authState = {
|
|
210
|
+
...this.authState,
|
|
211
|
+
accessToken: response.accessToken,
|
|
212
|
+
expiresAt: response.expiresAt,
|
|
213
|
+
};
|
|
214
|
+
this.propagateAccessToken(this.authState.accessToken);
|
|
215
|
+
return this.authState;
|
|
216
|
+
}
|
|
217
|
+
catch (error) {
|
|
218
|
+
// If refresh fails, set the auth state to undefined
|
|
219
|
+
this.authState = undefined;
|
|
220
|
+
throw error;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Check if the user is authenticated
|
|
225
|
+
*/
|
|
226
|
+
isAuthenticated() {
|
|
227
|
+
return !!this.authState;
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Check connection status
|
|
231
|
+
*/
|
|
232
|
+
isConnected() {
|
|
233
|
+
return !!this.walletClient && !!this.publicClient;
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Set the wallet client
|
|
237
|
+
* Used by React SDK to update the wallet when wagmi becomes available
|
|
238
|
+
*/
|
|
239
|
+
setWalletClient(walletClient) {
|
|
240
|
+
if (walletClient.chain?.id !== this.chain.id) {
|
|
241
|
+
throw new InvalidConfigError(`Wallet client chain mismatch. Expected ${this.chain.id}, got ${walletClient.chain?.id}`, "walletClient");
|
|
242
|
+
}
|
|
243
|
+
this.walletClient = walletClient;
|
|
244
|
+
// Update APIs that depend on wallet client
|
|
245
|
+
this.auth.setWalletClient(walletClient);
|
|
246
|
+
this.vault.setWalletClient(walletClient);
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Get the current account address
|
|
250
|
+
*/
|
|
251
|
+
getAccountAddress() {
|
|
252
|
+
if (!this.walletClient) {
|
|
253
|
+
throw new InvalidStateError("Wallet client not set", "walletClient");
|
|
254
|
+
}
|
|
255
|
+
if (this.walletClient.account) {
|
|
256
|
+
return this.walletClient.account.address;
|
|
257
|
+
}
|
|
258
|
+
throw new InvalidStateError("No account available", "account");
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Get the current network
|
|
262
|
+
*/
|
|
263
|
+
getNetwork() {
|
|
264
|
+
return this.network;
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Get the current chain ID
|
|
268
|
+
*/
|
|
269
|
+
getChainId() {
|
|
270
|
+
return this.chain.id;
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Wait for a transaction to be confirmed
|
|
274
|
+
* @param hash - The transaction hash
|
|
275
|
+
* @param confirmations - Number of confirmations to wait for (optional)
|
|
276
|
+
* @param timeout - Timeout in milliseconds (optional)
|
|
277
|
+
* @returns The transaction receipt
|
|
278
|
+
*/
|
|
279
|
+
async waitForTransaction(hash, confirmations, timeout) {
|
|
280
|
+
return this.publicClient.waitForTransactionReceipt({
|
|
281
|
+
hash: hash,
|
|
282
|
+
confirmations,
|
|
283
|
+
timeout,
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Create a new Monaco SDK instance
|
|
289
|
+
* @param config - SDK configuration
|
|
290
|
+
* @returns Monaco SDK instance
|
|
291
|
+
*/
|
|
292
|
+
export function createMonacoSDK(config) {
|
|
293
|
+
return new MonacoSDKImpl(config);
|
|
294
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./magnitude";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./magnitude";
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimum number of price buckets allowed for a magnitude
|
|
3
|
+
*/
|
|
4
|
+
export declare const MIN_BUCKETS_ALLOWED = 10;
|
|
5
|
+
/**
|
|
6
|
+
* Maximum number of price buckets allowed for a magnitude
|
|
7
|
+
*/
|
|
8
|
+
export declare const MAX_BUCKETS_ALLOWED = 10000000;
|
|
9
|
+
/**
|
|
10
|
+
* All possible magnitude values we support (from 0.0001 to 10000)
|
|
11
|
+
*/
|
|
12
|
+
export declare const ALL_MAGNITUDES: number[];
|
|
13
|
+
/**
|
|
14
|
+
* Calculate which magnitude levels are valid for a given price.
|
|
15
|
+
*
|
|
16
|
+
* A magnitude is valid if it creates between MIN_BUCKETS_ALLOWED and MAX_BUCKETS_ALLOWED price buckets.
|
|
17
|
+
*
|
|
18
|
+
* @param currentPrice - The current market price (e.g., 50000 for BTC/USDC)
|
|
19
|
+
* @returns Array of valid magnitude values
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* calculateValidMagnitudes(112000); // [0.1, 1, 10, 100, 1000, 10000]
|
|
23
|
+
* calculateValidMagnitudes(4200); // [0.001, 0.01, 0.1, 1, 10, 100]
|
|
24
|
+
* calculateValidMagnitudes(0.5); // [0.0001, 0.001, 0.01]
|
|
25
|
+
*/
|
|
26
|
+
export declare const calculateValidMagnitudes: (currentPrice: number) => number[];
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimum number of price buckets allowed for a magnitude
|
|
3
|
+
*/
|
|
4
|
+
export const MIN_BUCKETS_ALLOWED = 10;
|
|
5
|
+
/**
|
|
6
|
+
* Maximum number of price buckets allowed for a magnitude
|
|
7
|
+
*/
|
|
8
|
+
export const MAX_BUCKETS_ALLOWED = 10_000_000;
|
|
9
|
+
/**
|
|
10
|
+
* All possible magnitude values we support (from 0.0001 to 10000)
|
|
11
|
+
*/
|
|
12
|
+
export const ALL_MAGNITUDES = [0.0001, 0.001, 0.01, 0.1, 1, 10, 100, 1000, 10000];
|
|
13
|
+
/**
|
|
14
|
+
* Calculate which magnitude levels are valid for a given price.
|
|
15
|
+
*
|
|
16
|
+
* A magnitude is valid if it creates between MIN_BUCKETS_ALLOWED and MAX_BUCKETS_ALLOWED price buckets.
|
|
17
|
+
*
|
|
18
|
+
* @param currentPrice - The current market price (e.g., 50000 for BTC/USDC)
|
|
19
|
+
* @returns Array of valid magnitude values
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* calculateValidMagnitudes(112000); // [0.1, 1, 10, 100, 1000, 10000]
|
|
23
|
+
* calculateValidMagnitudes(4200); // [0.001, 0.01, 0.1, 1, 10, 100]
|
|
24
|
+
* calculateValidMagnitudes(0.5); // [0.0001, 0.001, 0.01]
|
|
25
|
+
*/
|
|
26
|
+
export const calculateValidMagnitudes = (currentPrice) => {
|
|
27
|
+
return ALL_MAGNITUDES.filter((magnitude) => {
|
|
28
|
+
const bucketCount = Math.floor(currentPrice / magnitude);
|
|
29
|
+
return bucketCount >= MIN_BUCKETS_ALLOWED && bucketCount <= MAX_BUCKETS_ALLOWED;
|
|
30
|
+
});
|
|
31
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@0xmonaco/core",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.8",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -23,8 +23,8 @@
|
|
|
23
23
|
"viem": "^2.45.2"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@0xmonaco/contracts": "0.7.
|
|
27
|
-
"@0xmonaco/types": "0.7.
|
|
26
|
+
"@0xmonaco/contracts": "0.7.8",
|
|
27
|
+
"@0xmonaco/types": "0.7.8",
|
|
28
28
|
"http-status-codes": "^2.3.0"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|