@carbon-terminal/trading-sdk 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/README.md +6 -0
  2. package/dist/client/index.d.ts +1 -0
  3. package/dist/client/index.js +17 -0
  4. package/dist/client/trading-sdk.d.ts +42 -0
  5. package/dist/client/trading-sdk.js +99 -0
  6. package/dist/client/types.d.ts +9 -0
  7. package/dist/client/types.js +18 -0
  8. package/dist/common/exceptions.d.ts +21 -0
  9. package/dist/common/exceptions.js +51 -0
  10. package/dist/common/index.d.ts +1 -0
  11. package/dist/common/index.js +17 -0
  12. package/dist/entities/account-entity.d.ts +6 -0
  13. package/dist/entities/account-entity.js +10 -0
  14. package/dist/entities/cancel-close-request.entity.d.ts +22 -0
  15. package/dist/entities/cancel-close-request.entity.js +54 -0
  16. package/dist/entities/cancel-request.entity.d.ts +22 -0
  17. package/dist/entities/cancel-request.entity.js +54 -0
  18. package/dist/entities/close-request.entity.d.ts +23 -0
  19. package/dist/entities/close-request.entity.js +63 -0
  20. package/dist/entities/index.d.ts +5 -0
  21. package/dist/entities/index.js +21 -0
  22. package/dist/entities/library/hookable-entity.d.ts +9 -0
  23. package/dist/entities/library/hookable-entity.js +20 -0
  24. package/dist/entities/position-entity.d.ts +29 -0
  25. package/dist/entities/position-entity.js +110 -0
  26. package/dist/index.d.ts +3 -0
  27. package/dist/index.js +19 -0
  28. package/dist/modules/account-manager/index.d.ts +9 -0
  29. package/dist/modules/account-manager/index.js +43 -0
  30. package/dist/modules/auth-manager/index.d.ts +44 -0
  31. package/dist/modules/auth-manager/index.js +188 -0
  32. package/dist/modules/auth-manager/types.d.ts +7 -0
  33. package/dist/modules/auth-manager/types.js +2 -0
  34. package/dist/modules/index.d.ts +0 -0
  35. package/dist/modules/index.js +0 -0
  36. package/dist/modules/markets-manager/index.d.ts +23 -0
  37. package/dist/modules/markets-manager/index.js +119 -0
  38. package/dist/modules/trade-manager/index.d.ts +52 -0
  39. package/dist/modules/trade-manager/index.js +157 -0
  40. package/dist/modules/trade-manager/types.d.ts +0 -0
  41. package/dist/modules/trade-manager/types.js +0 -0
  42. package/dist/utils/api/getApiUrl.d.ts +1 -0
  43. package/dist/utils/api/getApiUrl.js +8 -0
  44. package/dist/utils/auth/apiAuth.d.ts +5 -0
  45. package/dist/utils/auth/apiAuth.js +23 -0
  46. package/dist/utils/auth/siweAuth.d.ts +20 -0
  47. package/dist/utils/auth/siweAuth.js +106 -0
  48. package/package.json +47 -0
package/README.md ADDED
@@ -0,0 +1,6 @@
1
+ # SDK for IntentX Trading API
2
+
3
+ This SDK is used to interact with the IntentX Trading API.
4
+
5
+ This SDK will use @intentx/core to define the types and interfaces.
6
+ On top of that, it will provide a client to interact with the API.
@@ -0,0 +1 @@
1
+ export * from "./trading-sdk";
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./trading-sdk"), exports);
@@ -0,0 +1,42 @@
1
+ import { APIResponse } from "@carbon-terminal/core";
2
+ import { AxiosRequestConfig } from "axios";
3
+ import { MarketsManager } from "..//modules/markets-manager";
4
+ import { AccountManager } from "../modules/account-manager";
5
+ import { AuthManager } from "../modules/auth-manager";
6
+ import { TradeManager } from "../modules/trade-manager";
7
+ import { TradingSDKConfig } from "./types";
8
+ export declare class TradingSDK {
9
+ private readonly client;
10
+ private readonly config;
11
+ private apiKey;
12
+ readonly authManager: AuthManager;
13
+ readonly accountManager: AccountManager;
14
+ readonly tradeManager: TradeManager;
15
+ readonly marketsManager: MarketsManager;
16
+ constructor(config?: TradingSDKConfig);
17
+ setApiKey(apiKey: string): void;
18
+ /**
19
+ * Validate the API key against the trading API
20
+ */
21
+ validateApiKey(): Promise<boolean>;
22
+ /**
23
+ * Make a GET request to the API
24
+ * @internal
25
+ */
26
+ get<T>(endpoint: string, config?: AxiosRequestConfig): Promise<APIResponse<T>>;
27
+ /**
28
+ * Make a POST request to the API
29
+ * @internal
30
+ */
31
+ post<T>(endpoint: string, data?: any, config?: AxiosRequestConfig): Promise<APIResponse<T>>;
32
+ /**
33
+ * Make a PUT request to the API
34
+ * @internal
35
+ */
36
+ put<T>(endpoint: string, data?: any, config?: AxiosRequestConfig): Promise<APIResponse<T>>;
37
+ /**
38
+ * Make a DELETE request to the API
39
+ * @internal
40
+ */
41
+ delete<T>(endpoint: string, config?: AxiosRequestConfig): Promise<APIResponse<T>>;
42
+ }
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.TradingSDK = void 0;
7
+ const axios_1 = __importDefault(require("axios"));
8
+ const markets_manager_1 = require("..//modules/markets-manager");
9
+ const exceptions_1 = require("../common/exceptions");
10
+ const account_manager_1 = require("../modules/account-manager");
11
+ const auth_manager_1 = require("../modules/auth-manager");
12
+ const trade_manager_1 = require("../modules/trade-manager");
13
+ const getApiUrl_1 = require("../utils/api/getApiUrl");
14
+ const types_1 = require("./types");
15
+ class TradingSDK {
16
+ //#endregion
17
+ constructor(config) {
18
+ this.apiKey = null;
19
+ this.config = config ?? {
20
+ baseUrl: (0, getApiUrl_1.getApiUrl)(),
21
+ };
22
+ if (config?.apiKey)
23
+ this.apiKey = config.apiKey;
24
+ this.client = axios_1.default.create({
25
+ baseURL: config?.baseUrl ?? (0, getApiUrl_1.getApiUrl)(),
26
+ headers: {
27
+ "Content-Type": "application/json",
28
+ ...(config?.apiKey && { "x-api-key": config.apiKey }),
29
+ },
30
+ });
31
+ this.authManager = new auth_manager_1.AuthManager(this);
32
+ this.accountManager = new account_manager_1.AccountManager(this);
33
+ this.tradeManager = new trade_manager_1.TradeManager(this);
34
+ this.marketsManager = new markets_manager_1.MarketsManager(this);
35
+ // Add response interceptor for error handling
36
+ this.client.interceptors.response.use((response) => response, (error) => {
37
+ if (error.response) {
38
+ throw new types_1.TradingSDKError(error.response?.data?.error?.message || "API request failed", error.response?.data?.error?.code, error.response?.data?.httpStatusCode);
39
+ }
40
+ throw new types_1.TradingSDKError("Network error");
41
+ });
42
+ }
43
+ setApiKey(apiKey) {
44
+ this.apiKey = apiKey;
45
+ this.client.defaults.headers["x-api-key"] = apiKey;
46
+ }
47
+ /**
48
+ * Validate the API key against the trading API
49
+ */
50
+ async validateApiKey() {
51
+ try {
52
+ const response = await this.post("/api-keys/validate");
53
+ if (!response.success) {
54
+ throw new exceptions_1.APIException({
55
+ message: response.error?.message || "Failed to validate API key",
56
+ code: response.error?.code,
57
+ httpStatusCode: response.httpStatusCode,
58
+ });
59
+ }
60
+ return response.data.isValid;
61
+ }
62
+ catch (error) {
63
+ return false;
64
+ }
65
+ }
66
+ /**
67
+ * Make a GET request to the API
68
+ * @internal
69
+ */
70
+ async get(endpoint, config) {
71
+ const response = await this.client.get(endpoint, config);
72
+ return response.data;
73
+ }
74
+ /**
75
+ * Make a POST request to the API
76
+ * @internal
77
+ */
78
+ async post(endpoint, data, config) {
79
+ const response = await this.client.post(endpoint, data, config);
80
+ return response.data;
81
+ }
82
+ /**
83
+ * Make a PUT request to the API
84
+ * @internal
85
+ */
86
+ async put(endpoint, data, config) {
87
+ const response = await this.client.put(endpoint, data, config);
88
+ return response.data;
89
+ }
90
+ /**
91
+ * Make a DELETE request to the API
92
+ * @internal
93
+ */
94
+ async delete(endpoint, config) {
95
+ const response = await this.client.delete(endpoint, config);
96
+ return response.data;
97
+ }
98
+ }
99
+ exports.TradingSDK = TradingSDK;
@@ -0,0 +1,9 @@
1
+ export interface TradingSDKConfig {
2
+ baseUrl?: string;
3
+ apiKey?: string;
4
+ }
5
+ export declare class TradingSDKError extends Error {
6
+ code?: string;
7
+ status?: number;
8
+ constructor(message: string, code?: string, status?: number);
9
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TradingSDKError = void 0;
4
+ class TradingSDKError extends Error {
5
+ constructor(message, code, status) {
6
+ super(message);
7
+ this.code = code;
8
+ this.status = status;
9
+ this.name = "TradingSDKError";
10
+ // Mantiene la cadena de prototipos correcta
11
+ Object.setPrototypeOf(this, TradingSDKError.prototype);
12
+ // Captura el stack trace correcto
13
+ if (Error.captureStackTrace) {
14
+ Error.captureStackTrace(this, TradingSDKError);
15
+ }
16
+ }
17
+ }
18
+ exports.TradingSDKError = TradingSDKError;
@@ -0,0 +1,21 @@
1
+ export declare class APIException extends Error {
2
+ readonly code: string;
3
+ readonly httpStatusCode: number;
4
+ constructor(params: {
5
+ message: string;
6
+ code?: string;
7
+ httpStatusCode?: number;
8
+ });
9
+ }
10
+ export declare class InstantActionNotAllowedException extends APIException {
11
+ constructor(message?: string, code?: string);
12
+ }
13
+ export declare class UnauthorizedException extends APIException {
14
+ constructor(message?: string, code?: string);
15
+ }
16
+ export declare class NotFoundException extends APIException {
17
+ constructor(message?: string, code?: string);
18
+ }
19
+ export declare class BadRequestException extends APIException {
20
+ constructor(message?: string, code?: string);
21
+ }
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BadRequestException = exports.NotFoundException = exports.UnauthorizedException = exports.InstantActionNotAllowedException = exports.APIException = void 0;
4
+ class APIException extends Error {
5
+ constructor(params) {
6
+ super(params.message);
7
+ this.code = params.code || "";
8
+ this.httpStatusCode = params.httpStatusCode || 200;
9
+ }
10
+ }
11
+ exports.APIException = APIException;
12
+ class InstantActionNotAllowedException extends APIException {
13
+ constructor(message = "Instant action not allowed", code = "INSTANT_ACTION_NOT_ALLOWED") {
14
+ super({
15
+ message,
16
+ code,
17
+ httpStatusCode: 403,
18
+ });
19
+ }
20
+ }
21
+ exports.InstantActionNotAllowedException = InstantActionNotAllowedException;
22
+ class UnauthorizedException extends APIException {
23
+ constructor(message = "Unauthorized", code = "NOT_AUTHORIZED") {
24
+ super({
25
+ message,
26
+ code,
27
+ httpStatusCode: 401,
28
+ });
29
+ }
30
+ }
31
+ exports.UnauthorizedException = UnauthorizedException;
32
+ class NotFoundException extends APIException {
33
+ constructor(message = "Not Found", code = "NOT_FOUND") {
34
+ super({
35
+ message,
36
+ code,
37
+ httpStatusCode: 404,
38
+ });
39
+ }
40
+ }
41
+ exports.NotFoundException = NotFoundException;
42
+ class BadRequestException extends APIException {
43
+ constructor(message = "Bad Request", code = "BAD_REQUEST") {
44
+ super({
45
+ message,
46
+ code,
47
+ httpStatusCode: 400,
48
+ });
49
+ }
50
+ }
51
+ exports.BadRequestException = BadRequestException;
@@ -0,0 +1 @@
1
+ export * from "./exceptions";
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./exceptions"), exports);
@@ -0,0 +1,6 @@
1
+ import { TradingSDK } from "src/client";
2
+ export declare class AccountEntity {
3
+ private readonly tradingSdk;
4
+ private readonly subaccountAddress;
5
+ constructor(tradingSdk: TradingSDK, subaccountAddress: string);
6
+ }
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AccountEntity = void 0;
4
+ class AccountEntity {
5
+ constructor(tradingSdk, subaccountAddress) {
6
+ this.tradingSdk = tradingSdk;
7
+ this.subaccountAddress = subaccountAddress;
8
+ }
9
+ }
10
+ exports.AccountEntity = AccountEntity;
@@ -0,0 +1,22 @@
1
+ import { CancelCloseRequestState, CancelCloseRequestStatus } from "@carbon-terminal/core";
2
+ import { TradingSDK } from "src/client";
3
+ import { HookableEntity } from "./library/hookable-entity";
4
+ export declare enum CancelCloseRequestHooks {
5
+ FAILED = "failed",
6
+ SUCCESS = "success"
7
+ }
8
+ export type CancelCloseRequestResultJustification = {
9
+ message: string;
10
+ code: string;
11
+ };
12
+ export declare class CancelCloseRequestEntity extends HookableEntity<CancelCloseRequestHooks, CancelCloseRequestState, CancelCloseRequestResultJustification> {
13
+ private readonly tradingSdk;
14
+ private internalCancelCloseRequestState;
15
+ constructor(tradingSdk: TradingSDK, cancelRequestState: CancelCloseRequestState);
16
+ private update;
17
+ getId(): string;
18
+ getStatus(): CancelCloseRequestStatus;
19
+ isFailed(): boolean;
20
+ isSuccess(): boolean;
21
+ waitForCompletion(): Promise<void>;
22
+ }
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CancelCloseRequestEntity = exports.CancelCloseRequestHooks = void 0;
4
+ const core_1 = require("@carbon-terminal/core");
5
+ const hookable_entity_1 = require("./library/hookable-entity");
6
+ var CancelCloseRequestHooks;
7
+ (function (CancelCloseRequestHooks) {
8
+ CancelCloseRequestHooks["FAILED"] = "failed";
9
+ CancelCloseRequestHooks["SUCCESS"] = "success";
10
+ })(CancelCloseRequestHooks || (exports.CancelCloseRequestHooks = CancelCloseRequestHooks = {}));
11
+ class CancelCloseRequestEntity extends hookable_entity_1.HookableEntity {
12
+ constructor(tradingSdk, cancelRequestState) {
13
+ super();
14
+ this.tradingSdk = tradingSdk;
15
+ this.internalCancelCloseRequestState = cancelRequestState;
16
+ }
17
+ async update() {
18
+ this.internalCancelCloseRequestState =
19
+ await this.tradingSdk.tradeManager.getCancelCloseRequestInfo(this.internalCancelCloseRequestState.id, this.internalCancelCloseRequestState.chainId);
20
+ if (this.internalCancelCloseRequestState.status ===
21
+ core_1.CancelCloseRequestStatus.CANCEL_SUCCESS) {
22
+ if (!this.isHookTriggered(CancelCloseRequestHooks.SUCCESS)) {
23
+ this.trigger(CancelCloseRequestHooks.SUCCESS, this.internalCancelCloseRequestState);
24
+ }
25
+ }
26
+ else if (this.internalCancelCloseRequestState.status ===
27
+ core_1.CancelCloseRequestStatus.CANCEL_FAILED) {
28
+ if (!this.isHookTriggered(CancelCloseRequestHooks.FAILED)) {
29
+ this.trigger(CancelCloseRequestHooks.FAILED, this.internalCancelCloseRequestState);
30
+ }
31
+ }
32
+ return this.internalCancelCloseRequestState;
33
+ }
34
+ getId() {
35
+ return this.internalCancelCloseRequestState.id;
36
+ }
37
+ getStatus() {
38
+ return this.internalCancelCloseRequestState.status;
39
+ }
40
+ isFailed() {
41
+ return this.getStatus() === core_1.CancelCloseRequestStatus.CANCEL_FAILED;
42
+ }
43
+ isSuccess() {
44
+ return this.getStatus() === core_1.CancelCloseRequestStatus.CANCEL_SUCCESS;
45
+ }
46
+ async waitForCompletion() {
47
+ while (this.getStatus() !== core_1.CancelCloseRequestStatus.CANCEL_SUCCESS &&
48
+ this.getStatus() !== core_1.CancelCloseRequestStatus.CANCEL_FAILED) {
49
+ await this.update();
50
+ await new Promise((resolve) => setTimeout(resolve, 3000));
51
+ }
52
+ }
53
+ }
54
+ exports.CancelCloseRequestEntity = CancelCloseRequestEntity;
@@ -0,0 +1,22 @@
1
+ import { CancelQuoteRequestState, CancelQuoteRequestStatus } from "@carbon-terminal/core";
2
+ import { TradingSDK } from "src/client";
3
+ import { HookableEntity } from "./library/hookable-entity";
4
+ export declare enum CancelRequestHooks {
5
+ FAILED = "failed",
6
+ SUCCESS = "success"
7
+ }
8
+ export type CancelRequestResultJustification = {
9
+ message: string;
10
+ code: string;
11
+ };
12
+ export declare class CancelRequestEntity extends HookableEntity<CancelRequestHooks, CancelQuoteRequestState, CancelRequestResultJustification> {
13
+ private readonly tradingSdk;
14
+ private internalCancelQuoteRequestState;
15
+ constructor(tradingSdk: TradingSDK, cancelRequestState: CancelQuoteRequestState);
16
+ private update;
17
+ getId(): string;
18
+ getStatus(): CancelQuoteRequestStatus;
19
+ isFailed(): boolean;
20
+ isSuccess(): boolean;
21
+ waitForCompletion(): Promise<void>;
22
+ }
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CancelRequestEntity = exports.CancelRequestHooks = void 0;
4
+ const core_1 = require("@carbon-terminal/core");
5
+ const hookable_entity_1 = require("./library/hookable-entity");
6
+ var CancelRequestHooks;
7
+ (function (CancelRequestHooks) {
8
+ CancelRequestHooks["FAILED"] = "failed";
9
+ CancelRequestHooks["SUCCESS"] = "success";
10
+ })(CancelRequestHooks || (exports.CancelRequestHooks = CancelRequestHooks = {}));
11
+ class CancelRequestEntity extends hookable_entity_1.HookableEntity {
12
+ constructor(tradingSdk, cancelRequestState) {
13
+ super();
14
+ this.tradingSdk = tradingSdk;
15
+ this.internalCancelQuoteRequestState = cancelRequestState;
16
+ }
17
+ async update() {
18
+ this.internalCancelQuoteRequestState =
19
+ await this.tradingSdk.tradeManager.getCancelQuoteRequestInfo(this.internalCancelQuoteRequestState.id, this.internalCancelQuoteRequestState.chainId);
20
+ if (this.internalCancelQuoteRequestState.status ===
21
+ core_1.CancelQuoteRequestStatus.CANCEL_SUCCESS) {
22
+ if (!this.isHookTriggered(CancelRequestHooks.SUCCESS)) {
23
+ this.trigger(CancelRequestHooks.SUCCESS, this.internalCancelQuoteRequestState);
24
+ }
25
+ }
26
+ else if (this.internalCancelQuoteRequestState.status ===
27
+ core_1.CancelQuoteRequestStatus.CANCEL_FAILED) {
28
+ if (!this.isHookTriggered(CancelRequestHooks.FAILED)) {
29
+ this.trigger(CancelRequestHooks.FAILED, this.internalCancelQuoteRequestState);
30
+ }
31
+ }
32
+ return this.internalCancelQuoteRequestState;
33
+ }
34
+ getId() {
35
+ return this.internalCancelQuoteRequestState.id;
36
+ }
37
+ getStatus() {
38
+ return this.internalCancelQuoteRequestState.status;
39
+ }
40
+ isFailed() {
41
+ return this.getStatus() === core_1.CancelQuoteRequestStatus.CANCEL_FAILED;
42
+ }
43
+ isSuccess() {
44
+ return this.getStatus() === core_1.CancelQuoteRequestStatus.CANCEL_SUCCESS;
45
+ }
46
+ async waitForCompletion() {
47
+ while (this.getStatus() !== core_1.CancelQuoteRequestStatus.CANCEL_SUCCESS &&
48
+ this.getStatus() !== core_1.CancelQuoteRequestStatus.CANCEL_FAILED) {
49
+ await this.update();
50
+ await new Promise((resolve) => setTimeout(resolve, 3000));
51
+ }
52
+ }
53
+ }
54
+ exports.CancelRequestEntity = CancelRequestEntity;
@@ -0,0 +1,23 @@
1
+ import { CloseRequestState, CloseRequestStatus } from "@carbon-terminal/core";
2
+ import { TradingSDK } from "src/client";
3
+ import { HookableEntity } from "./library/hookable-entity";
4
+ export declare enum CloseRequestHooks {
5
+ PRICE_SETTLED = "price-settled",
6
+ FAILED = "failed",
7
+ SUCCESS = "success"
8
+ }
9
+ export type CloseRequestResultJustification = {
10
+ message: string;
11
+ code: string;
12
+ };
13
+ export declare class CloseRequestEntity extends HookableEntity<CloseRequestHooks, CloseRequestState, CloseRequestResultJustification> {
14
+ private readonly tradingSdk;
15
+ private internalCloseRequestState;
16
+ constructor(tradingSdk: TradingSDK, closeRequestState: CloseRequestState);
17
+ private update;
18
+ getId(): string;
19
+ getStatus(): CloseRequestStatus;
20
+ isFailed(): boolean;
21
+ isSuccess(): boolean;
22
+ waitForCompletion(): Promise<CloseRequestStatus>;
23
+ }
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CloseRequestEntity = exports.CloseRequestHooks = void 0;
4
+ const core_1 = require("@carbon-terminal/core");
5
+ const hookable_entity_1 = require("./library/hookable-entity");
6
+ var CloseRequestHooks;
7
+ (function (CloseRequestHooks) {
8
+ CloseRequestHooks["PRICE_SETTLED"] = "price-settled";
9
+ CloseRequestHooks["FAILED"] = "failed";
10
+ CloseRequestHooks["SUCCESS"] = "success";
11
+ })(CloseRequestHooks || (exports.CloseRequestHooks = CloseRequestHooks = {}));
12
+ class CloseRequestEntity extends hookable_entity_1.HookableEntity {
13
+ constructor(tradingSdk, closeRequestState) {
14
+ super();
15
+ this.tradingSdk = tradingSdk;
16
+ this.internalCloseRequestState = closeRequestState;
17
+ }
18
+ async update() {
19
+ this.internalCloseRequestState =
20
+ await this.tradingSdk.tradeManager.getCloseRequestInfo(this.internalCloseRequestState.id, this.internalCloseRequestState.chainId);
21
+ if (this.internalCloseRequestState.status ===
22
+ core_1.CloseRequestStatus.CLOSE_REQUEST_COMPLETED) {
23
+ if (!this.isHookTriggered(CloseRequestHooks.SUCCESS)) {
24
+ this.trigger(CloseRequestHooks.SUCCESS, this.internalCloseRequestState);
25
+ }
26
+ }
27
+ else if (this.internalCloseRequestState.status === core_1.CloseRequestStatus.FAILED) {
28
+ if (!this.isHookTriggered(CloseRequestHooks.FAILED)) {
29
+ this.trigger(CloseRequestHooks.FAILED, this.internalCloseRequestState);
30
+ }
31
+ }
32
+ else if (this.internalCloseRequestState.avgFillPrice &&
33
+ this.internalCloseRequestState.filledQuantity) {
34
+ if (!this.isHookTriggered(CloseRequestHooks.PRICE_SETTLED)) {
35
+ this.trigger(CloseRequestHooks.PRICE_SETTLED, this.internalCloseRequestState);
36
+ }
37
+ }
38
+ return this.internalCloseRequestState;
39
+ }
40
+ getId() {
41
+ return this.internalCloseRequestState.id;
42
+ }
43
+ getStatus() {
44
+ return this.internalCloseRequestState.status;
45
+ }
46
+ isFailed() {
47
+ return this.getStatus() === core_1.CloseRequestStatus.FAILED;
48
+ }
49
+ isSuccess() {
50
+ return this.getStatus() === core_1.CloseRequestStatus.CLOSE_REQUEST_COMPLETED;
51
+ }
52
+ async waitForCompletion() {
53
+ // Poll the status until it is not pending
54
+ while (this.getStatus() !== core_1.CloseRequestStatus.CLOSE_REQUEST_COMPLETED &&
55
+ this.getStatus() !== core_1.CloseRequestStatus.FAILED) {
56
+ await this.update();
57
+ // 3s polling
58
+ await new Promise((resolve) => setTimeout(resolve, 3000));
59
+ }
60
+ return this.getStatus();
61
+ }
62
+ }
63
+ exports.CloseRequestEntity = CloseRequestEntity;
@@ -0,0 +1,5 @@
1
+ export * from "./account-entity";
2
+ export * from "./cancel-close-request.entity";
3
+ export * from "./cancel-request.entity";
4
+ export * from "./close-request.entity";
5
+ export * from "./position-entity";
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./account-entity"), exports);
18
+ __exportStar(require("./cancel-close-request.entity"), exports);
19
+ __exportStar(require("./cancel-request.entity"), exports);
20
+ __exportStar(require("./close-request.entity"), exports);
21
+ __exportStar(require("./position-entity"), exports);
@@ -0,0 +1,9 @@
1
+ export declare abstract class HookableEntity<T, S, J> {
2
+ private hooksTriggerRecords;
3
+ private hooksMap;
4
+ constructor();
5
+ on(hook: T, callback: (state: S, justification: J) => void): void;
6
+ protected trigger(hook: T, state: S, justification?: J): void;
7
+ protected isHookTriggered(hook: T): boolean;
8
+ abstract waitForCompletion(): any;
9
+ }
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HookableEntity = void 0;
4
+ class HookableEntity {
5
+ constructor() {
6
+ this.hooksTriggerRecords = new Map();
7
+ this.hooksMap = new Map();
8
+ }
9
+ on(hook, callback) {
10
+ this.hooksMap.set(hook, callback);
11
+ }
12
+ trigger(hook, state, justification) {
13
+ this.hooksTriggerRecords.set(hook, true);
14
+ this.hooksMap.get(hook)?.(state, justification);
15
+ }
16
+ isHookTriggered(hook) {
17
+ return this.hooksTriggerRecords.get(hook) ?? false;
18
+ }
19
+ }
20
+ exports.HookableEntity = HookableEntity;
@@ -0,0 +1,29 @@
1
+ import { OrderType, Position, PositionStatus } from "@carbon-terminal/core";
2
+ import { TradingSDK } from "src/client";
3
+ import { HookableEntity } from "./library/hookable-entity";
4
+ export declare enum PositionEntityHooks {
5
+ OPEN_PRICE_SETTLED = "open-price-settled",
6
+ OPEN_REJECTED = "open-rejected",
7
+ OPEN_SUCCESS = "open-success",
8
+ RESERVED = "reserved",
9
+ EXPIRED = "expired"
10
+ }
11
+ export type PositionResultJustification = {
12
+ message: string;
13
+ code: string;
14
+ };
15
+ export declare class PositionEntity extends HookableEntity<PositionEntityHooks, Position, PositionResultJustification> {
16
+ private readonly tradingSdk;
17
+ private internalPosition;
18
+ private lastFillQuantity;
19
+ constructor(tradingSdk: TradingSDK, position: Position);
20
+ getStatus(): PositionStatus;
21
+ getDetails(): Position;
22
+ update(): Promise<Position>;
23
+ isConfirmedOnChain(): boolean;
24
+ isPriceSettled(): number | boolean;
25
+ isFailed(): boolean;
26
+ close(quantityToClose: number, closePrice: number, orderType: OrderType): Promise<import("./close-request.entity").CloseRequestEntity>;
27
+ waitForCompletion(): Promise<void>;
28
+ waitForOnChainConfirmation(): Promise<void>;
29
+ }