@0xmonaco/core 0.7.7 → 0.7.9
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 +301 -0
- package/dist/api/trading/api.js +497 -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 +560 -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,191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Profile API Implementation
|
|
3
|
+
*
|
|
4
|
+
* Handles user profile operations including fetching profile information.
|
|
5
|
+
* All operations use JWT authentication and go through the API Gateway.
|
|
6
|
+
*
|
|
7
|
+
* This class provides a complete interface for profile operations on the Monaco protocol,
|
|
8
|
+
* including fetching user profile data from the /api/v1/accounts/me endpoint.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const profileAPI = new ProfileAPIImpl(apiUrl);
|
|
13
|
+
* profileAPI.setAccessToken(jwtToken);
|
|
14
|
+
*
|
|
15
|
+
* // Get user profile
|
|
16
|
+
* const profile = await profileAPI.getProfile();
|
|
17
|
+
* console.log(`User: ${profile.username} (${profile.address})`);
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
import type { AccountBalance, GetPaginatedUserMovementsResponse, GetUserBalancesParams, GetUserBalancesResponse, GetUserMovementsParams, GetUserTradesParams, GetUserTradesResponse, PortfolioChartResponse, PortfolioMetric, PortfolioPeriod, PortfolioStats, ProfileAPI, UserProfile } from "@0xmonaco/types";
|
|
21
|
+
import { BaseAPI } from "../base";
|
|
22
|
+
export declare class ProfileAPIImpl extends BaseAPI implements ProfileAPI {
|
|
23
|
+
/**
|
|
24
|
+
* Get the current user's profile information.
|
|
25
|
+
*
|
|
26
|
+
* Fetches the profile data for the authenticated user using the /api/v1/accounts/me endpoint.
|
|
27
|
+
* Requires a valid access token to be set.
|
|
28
|
+
*
|
|
29
|
+
* @returns Promise resolving to the user's profile information
|
|
30
|
+
* @throws {APIError} When the request fails or user is not authenticated
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* // Set access token first
|
|
35
|
+
* profileAPI.setAccessToken(authResult.accessToken);
|
|
36
|
+
*
|
|
37
|
+
* // Get profile
|
|
38
|
+
* const profile = await profileAPI.getProfile();
|
|
39
|
+
* console.log(`User ID: ${profile.id}`);
|
|
40
|
+
* console.log(`Address: ${profile.address}`);
|
|
41
|
+
* console.log(`Username: ${profile.username}`);
|
|
42
|
+
* console.log(`Account type: ${profile.account_type}`);
|
|
43
|
+
* console.log(`Can withdraw: ${profile.can_withdraw}`);
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
getProfile(): Promise<UserProfile>;
|
|
47
|
+
/**
|
|
48
|
+
* Get the current user's ledger movements (transaction history) with pagination.
|
|
49
|
+
*
|
|
50
|
+
* Fetches the user's transaction history from the /api/v1/accounts/movements endpoint.
|
|
51
|
+
* Requires a valid access token to be set.
|
|
52
|
+
*
|
|
53
|
+
* @param params - Optional query parameters for pagination and filtering
|
|
54
|
+
* @param params.page - Page number (starts from 1, default: 1)
|
|
55
|
+
* @param params.limit - Number of items per page (default: 20, max: 100)
|
|
56
|
+
* @param params.entry_type - Filter by entry type (CREDIT, DEBIT, LOCK, UNLOCK, FEE)
|
|
57
|
+
* @param params.transaction_type - Filter by transaction type (DEPOSIT, WITHDRAWAL, TRADE, FEE, FUNDING, LIQUIDATION, INTEREST, REWARD)
|
|
58
|
+
* @param params.asset_id - Filter by asset ID (UUID)
|
|
59
|
+
* @returns Promise resolving to paginated movements response
|
|
60
|
+
* @throws {APIError} When the request fails or user is not authenticated
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* // Get first page with default page_size (20)
|
|
65
|
+
* const movements = await profileAPI.getPaginatedUserMovements();
|
|
66
|
+
* console.log(`Total movements: ${movements.total}`);
|
|
67
|
+
*
|
|
68
|
+
* // Get second page with custom page_size
|
|
69
|
+
* const page2 = await profileAPI.getPaginatedUserMovements({ page: 2, page_size: 50 });
|
|
70
|
+
* console.log(`Page ${page2.page} of ${page2.total_pages}`);
|
|
71
|
+
*
|
|
72
|
+
* // Filter by entry type and transaction type
|
|
73
|
+
* const deposits = await profileAPI.getPaginatedUserMovements({ entry_type: "CREDIT", transaction_type: "DEPOSIT" });
|
|
74
|
+
*
|
|
75
|
+
* // Filter by asset
|
|
76
|
+
* const usdcMovements = await profileAPI.getPaginatedUserMovements({ asset_id: "550e8400-e29b-41d4-a716-446655440000" });
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
getPaginatedUserMovements(params?: GetUserMovementsParams): Promise<GetPaginatedUserMovementsResponse>;
|
|
80
|
+
/**
|
|
81
|
+
* Get the current user's token balances with pagination.
|
|
82
|
+
*
|
|
83
|
+
* Fetches the user's token balances from the /api/v1/accounts/balances endpoint.
|
|
84
|
+
* Requires a valid access token to be set.
|
|
85
|
+
*
|
|
86
|
+
* @param params - Optional query parameters for pagination
|
|
87
|
+
* @param params.page - Page number (starts from 1, default: 1)
|
|
88
|
+
* @param params.page_size - Number of items per page (default: 20, max: 100)
|
|
89
|
+
* @returns Promise resolving to paginated balances response
|
|
90
|
+
* @throws {APIError} When the request fails or user is not authenticated
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```typescript
|
|
94
|
+
* // Get first 20 balances (default limit)
|
|
95
|
+
* const balances = await profileAPI.getUserBalances();
|
|
96
|
+
* console.log(`Total balances: ${balances.total}`);
|
|
97
|
+
* balances.balances.forEach(balance => {
|
|
98
|
+
* console.log(`${balance.symbol}: ${balance.available_balance}`);
|
|
99
|
+
* });
|
|
100
|
+
*
|
|
101
|
+
* // Get page 2 with 50 items per page
|
|
102
|
+
* const nextBalances = await profileAPI.getUserBalances({ page: 2, page_size: 50 });
|
|
103
|
+
* console.log(`Returned ${nextBalances.balances.length} balances`);
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
getUserBalances(params?: GetUserBalancesParams): Promise<GetUserBalancesResponse>;
|
|
107
|
+
/**
|
|
108
|
+
* Get the current user's balance for a specific asset.
|
|
109
|
+
*
|
|
110
|
+
* Fetches the user's balance for a specific asset from the /api/v1/accounts/balances/{asset_id} endpoint.
|
|
111
|
+
* Requires a valid access token to be set. If no balance exists for a valid asset, returns zero balances.
|
|
112
|
+
*
|
|
113
|
+
* @param assetId - The asset identifier (UUID)
|
|
114
|
+
* @returns Promise resolving to the account balance for the asset
|
|
115
|
+
* @throws {APIError} When the request fails, user is not authenticated, or asset not found (404)
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* // Get balance for a specific asset
|
|
120
|
+
* const balance = await profileAPI.getUserBalanceByAssetId('123e4567-e89b-12d3-a456-426614174000');
|
|
121
|
+
* console.log(`${balance.symbol}: ${balance.available_balance}`);
|
|
122
|
+
* console.log(`Locked: ${balance.locked_balance}`);
|
|
123
|
+
* console.log(`Total: ${balance.total_balance}`);
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
getUserBalanceByAssetId(assetId: string): Promise<AccountBalance>;
|
|
127
|
+
/**
|
|
128
|
+
* Get the current user's portfolio statistics for a given time period.
|
|
129
|
+
*
|
|
130
|
+
* Fetches aggregate stats from the /api/v1/accounts/me/portfolio endpoint.
|
|
131
|
+
* Requires a valid access token to be set.
|
|
132
|
+
*
|
|
133
|
+
* @param period - Time period to scope the stats ("24h" | "7d" | "30d" | "all")
|
|
134
|
+
* @returns Promise resolving to the user's portfolio statistics
|
|
135
|
+
* @throws {APIError} When the request fails or user is not authenticated
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* ```typescript
|
|
139
|
+
* const stats = await profileAPI.getPortfolioStats("30d");
|
|
140
|
+
* console.log(`PnL: ${stats.pnl}`);
|
|
141
|
+
* console.log(`Volume: ${stats.volume}`);
|
|
142
|
+
* console.log(`Total equity: ${stats.total_equity}`);
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
getPortfolioStats(period: PortfolioPeriod): Promise<PortfolioStats>;
|
|
146
|
+
/**
|
|
147
|
+
* Get the current user's portfolio chart time series for a given period and metric.
|
|
148
|
+
*
|
|
149
|
+
* Fetches bucketed time series data from the /api/v1/accounts/me/portfolio/chart endpoint.
|
|
150
|
+
* Requires a valid access token to be set.
|
|
151
|
+
*
|
|
152
|
+
* @param period - Time period ("24h" | "7d" | "30d" | "all")
|
|
153
|
+
* @param metric - Metric to chart ("volume" | "pnl")
|
|
154
|
+
* @returns Promise resolving to the portfolio chart response
|
|
155
|
+
* @throws {APIError} When the request fails or user is not authenticated
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* ```typescript
|
|
159
|
+
* const chart = await profileAPI.getPortfolioChart("7d", "pnl");
|
|
160
|
+
* chart.data.forEach(point => {
|
|
161
|
+
* console.log(`${point.timestamp}: ${point.value}`);
|
|
162
|
+
* });
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
getPortfolioChart(period: PortfolioPeriod, metric: PortfolioMetric): Promise<PortfolioChartResponse>;
|
|
166
|
+
/**
|
|
167
|
+
* Get the current user's trade history with pagination.
|
|
168
|
+
*
|
|
169
|
+
* Fetches the user's executed trades from the /api/v1/accounts/trades endpoint.
|
|
170
|
+
* Requires a valid access token to be set.
|
|
171
|
+
*
|
|
172
|
+
* @param params - Optional query parameters for pagination and filtering
|
|
173
|
+
* @param params.page - Page number (starts from 1, default: 1)
|
|
174
|
+
* @param params.page_size - Number of items per page (default: 20, max: 100)
|
|
175
|
+
* @param params.trading_pair_id - Filter by trading pair ID (UUID)
|
|
176
|
+
* @returns Promise resolving to paginated trades response
|
|
177
|
+
* @throws {ValidationError} When the provided params fail client-side validation
|
|
178
|
+
* @throws {APIError} When the request fails or user is not authenticated
|
|
179
|
+
*
|
|
180
|
+
* @example
|
|
181
|
+
* ```typescript
|
|
182
|
+
* // Get first page of trades
|
|
183
|
+
* const trades = await profileAPI.getUserTrades();
|
|
184
|
+
* console.log(`Total trades: ${trades.total}`);
|
|
185
|
+
*
|
|
186
|
+
* // Filter by trading pair
|
|
187
|
+
* const pairTrades = await profileAPI.getUserTrades({ trading_pair_id: "550e8400-e29b-41d4-a716-446655440000" });
|
|
188
|
+
* ```
|
|
189
|
+
*/
|
|
190
|
+
getUserTrades(params?: GetUserTradesParams): Promise<GetUserTradesResponse>;
|
|
191
|
+
}
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Profile API Implementation
|
|
3
|
+
*
|
|
4
|
+
* Handles user profile operations including fetching profile information.
|
|
5
|
+
* All operations use JWT authentication and go through the API Gateway.
|
|
6
|
+
*
|
|
7
|
+
* This class provides a complete interface for profile operations on the Monaco protocol,
|
|
8
|
+
* including fetching user profile data from the /api/v1/accounts/me endpoint.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const profileAPI = new ProfileAPIImpl(apiUrl);
|
|
13
|
+
* profileAPI.setAccessToken(jwtToken);
|
|
14
|
+
*
|
|
15
|
+
* // Get user profile
|
|
16
|
+
* const profile = await profileAPI.getProfile();
|
|
17
|
+
* console.log(`User: ${profile.username} (${profile.address})`);
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
import { GetUserMovementsSchema, GetUserTradesSchema, validate } from "@0xmonaco/types";
|
|
21
|
+
import { APIError } from "../../errors";
|
|
22
|
+
import { BaseAPI } from "../base";
|
|
23
|
+
export class ProfileAPIImpl extends BaseAPI {
|
|
24
|
+
/**
|
|
25
|
+
* Get the current user's profile information.
|
|
26
|
+
*
|
|
27
|
+
* Fetches the profile data for the authenticated user using the /api/v1/accounts/me endpoint.
|
|
28
|
+
* Requires a valid access token to be set.
|
|
29
|
+
*
|
|
30
|
+
* @returns Promise resolving to the user's profile information
|
|
31
|
+
* @throws {APIError} When the request fails or user is not authenticated
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* // Set access token first
|
|
36
|
+
* profileAPI.setAccessToken(authResult.accessToken);
|
|
37
|
+
*
|
|
38
|
+
* // Get profile
|
|
39
|
+
* const profile = await profileAPI.getProfile();
|
|
40
|
+
* console.log(`User ID: ${profile.id}`);
|
|
41
|
+
* console.log(`Address: ${profile.address}`);
|
|
42
|
+
* console.log(`Username: ${profile.username}`);
|
|
43
|
+
* console.log(`Account type: ${profile.account_type}`);
|
|
44
|
+
* console.log(`Can withdraw: ${profile.can_withdraw}`);
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
async getProfile() {
|
|
48
|
+
return await this.makeAuthenticatedRequest("/api/v1/accounts/me");
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Get the current user's ledger movements (transaction history) with pagination.
|
|
52
|
+
*
|
|
53
|
+
* Fetches the user's transaction history from the /api/v1/accounts/movements endpoint.
|
|
54
|
+
* Requires a valid access token to be set.
|
|
55
|
+
*
|
|
56
|
+
* @param params - Optional query parameters for pagination and filtering
|
|
57
|
+
* @param params.page - Page number (starts from 1, default: 1)
|
|
58
|
+
* @param params.limit - Number of items per page (default: 20, max: 100)
|
|
59
|
+
* @param params.entry_type - Filter by entry type (CREDIT, DEBIT, LOCK, UNLOCK, FEE)
|
|
60
|
+
* @param params.transaction_type - Filter by transaction type (DEPOSIT, WITHDRAWAL, TRADE, FEE, FUNDING, LIQUIDATION, INTEREST, REWARD)
|
|
61
|
+
* @param params.asset_id - Filter by asset ID (UUID)
|
|
62
|
+
* @returns Promise resolving to paginated movements response
|
|
63
|
+
* @throws {APIError} When the request fails or user is not authenticated
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* // Get first page with default page_size (20)
|
|
68
|
+
* const movements = await profileAPI.getPaginatedUserMovements();
|
|
69
|
+
* console.log(`Total movements: ${movements.total}`);
|
|
70
|
+
*
|
|
71
|
+
* // Get second page with custom page_size
|
|
72
|
+
* const page2 = await profileAPI.getPaginatedUserMovements({ page: 2, page_size: 50 });
|
|
73
|
+
* console.log(`Page ${page2.page} of ${page2.total_pages}`);
|
|
74
|
+
*
|
|
75
|
+
* // Filter by entry type and transaction type
|
|
76
|
+
* const deposits = await profileAPI.getPaginatedUserMovements({ entry_type: "CREDIT", transaction_type: "DEPOSIT" });
|
|
77
|
+
*
|
|
78
|
+
* // Filter by asset
|
|
79
|
+
* const usdcMovements = await profileAPI.getPaginatedUserMovements({ asset_id: "550e8400-e29b-41d4-a716-446655440000" });
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
async getPaginatedUserMovements(params) {
|
|
83
|
+
if (params) {
|
|
84
|
+
validate(GetUserMovementsSchema, params);
|
|
85
|
+
}
|
|
86
|
+
const searchParams = new URLSearchParams();
|
|
87
|
+
if (params?.page !== undefined) {
|
|
88
|
+
searchParams.append("page", params.page.toString());
|
|
89
|
+
}
|
|
90
|
+
if (params?.page_size !== undefined) {
|
|
91
|
+
searchParams.append("page_size", params.page_size.toString());
|
|
92
|
+
}
|
|
93
|
+
if (params?.entry_type !== undefined) {
|
|
94
|
+
searchParams.append("entry_type", params.entry_type);
|
|
95
|
+
}
|
|
96
|
+
if (params?.transaction_type !== undefined) {
|
|
97
|
+
searchParams.append("transaction_type", params.transaction_type);
|
|
98
|
+
}
|
|
99
|
+
if (params?.asset_id !== undefined) {
|
|
100
|
+
searchParams.append("asset_id", params.asset_id);
|
|
101
|
+
}
|
|
102
|
+
const queryString = searchParams.toString();
|
|
103
|
+
const url = queryString ? `/api/v1/accounts/movements?${queryString}` : "/api/v1/accounts/movements";
|
|
104
|
+
return await this.makeAuthenticatedRequest(url);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Get the current user's token balances with pagination.
|
|
108
|
+
*
|
|
109
|
+
* Fetches the user's token balances from the /api/v1/accounts/balances endpoint.
|
|
110
|
+
* Requires a valid access token to be set.
|
|
111
|
+
*
|
|
112
|
+
* @param params - Optional query parameters for pagination
|
|
113
|
+
* @param params.page - Page number (starts from 1, default: 1)
|
|
114
|
+
* @param params.page_size - Number of items per page (default: 20, max: 100)
|
|
115
|
+
* @returns Promise resolving to paginated balances response
|
|
116
|
+
* @throws {APIError} When the request fails or user is not authenticated
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```typescript
|
|
120
|
+
* // Get first 20 balances (default limit)
|
|
121
|
+
* const balances = await profileAPI.getUserBalances();
|
|
122
|
+
* console.log(`Total balances: ${balances.total}`);
|
|
123
|
+
* balances.balances.forEach(balance => {
|
|
124
|
+
* console.log(`${balance.symbol}: ${balance.available_balance}`);
|
|
125
|
+
* });
|
|
126
|
+
*
|
|
127
|
+
* // Get page 2 with 50 items per page
|
|
128
|
+
* const nextBalances = await profileAPI.getUserBalances({ page: 2, page_size: 50 });
|
|
129
|
+
* console.log(`Returned ${nextBalances.balances.length} balances`);
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
async getUserBalances(params) {
|
|
133
|
+
const searchParams = new URLSearchParams();
|
|
134
|
+
if (params?.page !== undefined) {
|
|
135
|
+
searchParams.append("page", params.page.toString());
|
|
136
|
+
}
|
|
137
|
+
if (params?.page_size !== undefined) {
|
|
138
|
+
searchParams.append("page_size", params.page_size.toString());
|
|
139
|
+
}
|
|
140
|
+
const queryString = searchParams.toString();
|
|
141
|
+
const url = queryString ? `/api/v1/accounts/balances?${queryString}` : "/api/v1/accounts/balances";
|
|
142
|
+
return await this.makeAuthenticatedRequest(url);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Get the current user's balance for a specific asset.
|
|
146
|
+
*
|
|
147
|
+
* Fetches the user's balance for a specific asset from the /api/v1/accounts/balances/{asset_id} endpoint.
|
|
148
|
+
* Requires a valid access token to be set. If no balance exists for a valid asset, returns zero balances.
|
|
149
|
+
*
|
|
150
|
+
* @param assetId - The asset identifier (UUID)
|
|
151
|
+
* @returns Promise resolving to the account balance for the asset
|
|
152
|
+
* @throws {APIError} When the request fails, user is not authenticated, or asset not found (404)
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* ```typescript
|
|
156
|
+
* // Get balance for a specific asset
|
|
157
|
+
* const balance = await profileAPI.getUserBalanceByAssetId('123e4567-e89b-12d3-a456-426614174000');
|
|
158
|
+
* console.log(`${balance.symbol}: ${balance.available_balance}`);
|
|
159
|
+
* console.log(`Locked: ${balance.locked_balance}`);
|
|
160
|
+
* console.log(`Total: ${balance.total_balance}`);
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
async getUserBalanceByAssetId(assetId) {
|
|
164
|
+
if (!assetId || assetId.trim() === "") {
|
|
165
|
+
throw new APIError("assetId is required and cannot be empty", {
|
|
166
|
+
endpoint: "/api/v1/accounts/balances/{asset_id}",
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
const url = `/api/v1/accounts/balances/${assetId}`;
|
|
170
|
+
return await this.makeAuthenticatedRequest(url);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Get the current user's portfolio statistics for a given time period.
|
|
174
|
+
*
|
|
175
|
+
* Fetches aggregate stats from the /api/v1/accounts/me/portfolio endpoint.
|
|
176
|
+
* Requires a valid access token to be set.
|
|
177
|
+
*
|
|
178
|
+
* @param period - Time period to scope the stats ("24h" | "7d" | "30d" | "all")
|
|
179
|
+
* @returns Promise resolving to the user's portfolio statistics
|
|
180
|
+
* @throws {APIError} When the request fails or user is not authenticated
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* ```typescript
|
|
184
|
+
* const stats = await profileAPI.getPortfolioStats("30d");
|
|
185
|
+
* console.log(`PnL: ${stats.pnl}`);
|
|
186
|
+
* console.log(`Volume: ${stats.volume}`);
|
|
187
|
+
* console.log(`Total equity: ${stats.total_equity}`);
|
|
188
|
+
* ```
|
|
189
|
+
*/
|
|
190
|
+
async getPortfolioStats(period) {
|
|
191
|
+
const searchParams = new URLSearchParams({ period });
|
|
192
|
+
return await this.makeAuthenticatedRequest(`/api/v1/accounts/me/portfolio?${searchParams}`);
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Get the current user's portfolio chart time series for a given period and metric.
|
|
196
|
+
*
|
|
197
|
+
* Fetches bucketed time series data from the /api/v1/accounts/me/portfolio/chart endpoint.
|
|
198
|
+
* Requires a valid access token to be set.
|
|
199
|
+
*
|
|
200
|
+
* @param period - Time period ("24h" | "7d" | "30d" | "all")
|
|
201
|
+
* @param metric - Metric to chart ("volume" | "pnl")
|
|
202
|
+
* @returns Promise resolving to the portfolio chart response
|
|
203
|
+
* @throws {APIError} When the request fails or user is not authenticated
|
|
204
|
+
*
|
|
205
|
+
* @example
|
|
206
|
+
* ```typescript
|
|
207
|
+
* const chart = await profileAPI.getPortfolioChart("7d", "pnl");
|
|
208
|
+
* chart.data.forEach(point => {
|
|
209
|
+
* console.log(`${point.timestamp}: ${point.value}`);
|
|
210
|
+
* });
|
|
211
|
+
* ```
|
|
212
|
+
*/
|
|
213
|
+
async getPortfolioChart(period, metric) {
|
|
214
|
+
const searchParams = new URLSearchParams({ period, metric });
|
|
215
|
+
return await this.makeAuthenticatedRequest(`/api/v1/accounts/me/portfolio/chart?${searchParams}`);
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Get the current user's trade history with pagination.
|
|
219
|
+
*
|
|
220
|
+
* Fetches the user's executed trades from the /api/v1/accounts/trades endpoint.
|
|
221
|
+
* Requires a valid access token to be set.
|
|
222
|
+
*
|
|
223
|
+
* @param params - Optional query parameters for pagination and filtering
|
|
224
|
+
* @param params.page - Page number (starts from 1, default: 1)
|
|
225
|
+
* @param params.page_size - Number of items per page (default: 20, max: 100)
|
|
226
|
+
* @param params.trading_pair_id - Filter by trading pair ID (UUID)
|
|
227
|
+
* @returns Promise resolving to paginated trades response
|
|
228
|
+
* @throws {ValidationError} When the provided params fail client-side validation
|
|
229
|
+
* @throws {APIError} When the request fails or user is not authenticated
|
|
230
|
+
*
|
|
231
|
+
* @example
|
|
232
|
+
* ```typescript
|
|
233
|
+
* // Get first page of trades
|
|
234
|
+
* const trades = await profileAPI.getUserTrades();
|
|
235
|
+
* console.log(`Total trades: ${trades.total}`);
|
|
236
|
+
*
|
|
237
|
+
* // Filter by trading pair
|
|
238
|
+
* const pairTrades = await profileAPI.getUserTrades({ trading_pair_id: "550e8400-e29b-41d4-a716-446655440000" });
|
|
239
|
+
* ```
|
|
240
|
+
*/
|
|
241
|
+
async getUserTrades(params) {
|
|
242
|
+
if (params) {
|
|
243
|
+
validate(GetUserTradesSchema, params);
|
|
244
|
+
}
|
|
245
|
+
const searchParams = new URLSearchParams();
|
|
246
|
+
if (params?.page !== undefined) {
|
|
247
|
+
searchParams.append("page", params.page.toString());
|
|
248
|
+
}
|
|
249
|
+
if (params?.page_size !== undefined) {
|
|
250
|
+
searchParams.append("page_size", params.page_size.toString());
|
|
251
|
+
}
|
|
252
|
+
if (params?.trading_pair_id !== undefined) {
|
|
253
|
+
searchParams.append("trading_pair_id", params.trading_pair_id);
|
|
254
|
+
}
|
|
255
|
+
const queryString = searchParams.toString();
|
|
256
|
+
const url = queryString ? `/api/v1/accounts/trades?${queryString}` : "/api/v1/accounts/trades";
|
|
257
|
+
return await this.makeAuthenticatedRequest(url);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { TradeEvent } from "@0xmonaco/types";
|
|
2
|
+
import { BaseAPI } from "../base";
|
|
3
|
+
/**
|
|
4
|
+
* Raw trade event from the API (snake_case) - matches websocket structure
|
|
5
|
+
*/
|
|
6
|
+
interface RawTradeEvent {
|
|
7
|
+
event_type: string;
|
|
8
|
+
trading_pair_id: string;
|
|
9
|
+
trading_mode: string;
|
|
10
|
+
data: {
|
|
11
|
+
trade_id: string;
|
|
12
|
+
price: string;
|
|
13
|
+
quantity: string;
|
|
14
|
+
maker_side: string;
|
|
15
|
+
executed_at: string;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Convert a raw trade event (snake_case) to a TradeEvent (camelCase)
|
|
20
|
+
*/
|
|
21
|
+
export declare function parseRawTradeEvent(raw: RawTradeEvent): TradeEvent;
|
|
22
|
+
/**
|
|
23
|
+
* Options for fetching trades
|
|
24
|
+
*/
|
|
25
|
+
export interface GetTradesOptions {
|
|
26
|
+
/** Page number (starts from 1, default: 1) */
|
|
27
|
+
page?: number;
|
|
28
|
+
/** Maximum number of records to return (default: 25, max: 100) */
|
|
29
|
+
page_size?: number;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Trades API for fetching historical trade data
|
|
33
|
+
*/
|
|
34
|
+
export declare class TradesAPIImpl extends BaseAPI {
|
|
35
|
+
/**
|
|
36
|
+
* Get historical trades for a trading pair
|
|
37
|
+
*
|
|
38
|
+
* @param tradingPairId - The trading pair UUID
|
|
39
|
+
* @param options - Pagination options
|
|
40
|
+
* @returns Array of TradeEvent records sorted by executed_at descending (newest first)
|
|
41
|
+
*/
|
|
42
|
+
getTrades(tradingPairId: string, options?: GetTradesOptions): Promise<TradeEvent[]>;
|
|
43
|
+
}
|
|
44
|
+
export {};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { BaseAPI } from "../base";
|
|
2
|
+
/**
|
|
3
|
+
* Convert a raw trade event (snake_case) to a TradeEvent (camelCase)
|
|
4
|
+
*/
|
|
5
|
+
export function parseRawTradeEvent(raw) {
|
|
6
|
+
return {
|
|
7
|
+
eventType: "trade",
|
|
8
|
+
tradingPairId: raw.trading_pair_id,
|
|
9
|
+
tradingMode: raw.trading_mode.toUpperCase(),
|
|
10
|
+
data: {
|
|
11
|
+
tradeId: raw.data.trade_id,
|
|
12
|
+
price: raw.data.price,
|
|
13
|
+
quantity: raw.data.quantity,
|
|
14
|
+
makerSide: raw.data.maker_side.toUpperCase(),
|
|
15
|
+
executedAt: raw.data.executed_at,
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Trades API for fetching historical trade data
|
|
21
|
+
*/
|
|
22
|
+
export class TradesAPIImpl extends BaseAPI {
|
|
23
|
+
/**
|
|
24
|
+
* Get historical trades for a trading pair
|
|
25
|
+
*
|
|
26
|
+
* @param tradingPairId - The trading pair UUID
|
|
27
|
+
* @param options - Pagination options
|
|
28
|
+
* @returns Array of TradeEvent records sorted by executed_at descending (newest first)
|
|
29
|
+
*/
|
|
30
|
+
async getTrades(tradingPairId, options = {}) {
|
|
31
|
+
const { page = 1 } = options;
|
|
32
|
+
// Ensure page_size is a positive number between 1 and 100, defaulting to 25
|
|
33
|
+
const page_size = options.page_size != null && options.page_size > 0 ? Math.min(options.page_size, 100) : 25;
|
|
34
|
+
const params = new URLSearchParams();
|
|
35
|
+
if (page > 1) {
|
|
36
|
+
params.set("page", String(page));
|
|
37
|
+
}
|
|
38
|
+
params.set("page_size", String(page_size));
|
|
39
|
+
const response = await this.makePublicRequest(`/api/v1/trades/${encodeURIComponent(tradingPairId)}?${params.toString()}`);
|
|
40
|
+
return response.trades.map(parseRawTradeEvent);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./api";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./api";
|