@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.
- package/README.md +6 -0
- package/dist/client/index.d.ts +1 -0
- package/dist/client/index.js +17 -0
- package/dist/client/trading-sdk.d.ts +42 -0
- package/dist/client/trading-sdk.js +99 -0
- package/dist/client/types.d.ts +9 -0
- package/dist/client/types.js +18 -0
- package/dist/common/exceptions.d.ts +21 -0
- package/dist/common/exceptions.js +51 -0
- package/dist/common/index.d.ts +1 -0
- package/dist/common/index.js +17 -0
- package/dist/entities/account-entity.d.ts +6 -0
- package/dist/entities/account-entity.js +10 -0
- package/dist/entities/cancel-close-request.entity.d.ts +22 -0
- package/dist/entities/cancel-close-request.entity.js +54 -0
- package/dist/entities/cancel-request.entity.d.ts +22 -0
- package/dist/entities/cancel-request.entity.js +54 -0
- package/dist/entities/close-request.entity.d.ts +23 -0
- package/dist/entities/close-request.entity.js +63 -0
- package/dist/entities/index.d.ts +5 -0
- package/dist/entities/index.js +21 -0
- package/dist/entities/library/hookable-entity.d.ts +9 -0
- package/dist/entities/library/hookable-entity.js +20 -0
- package/dist/entities/position-entity.d.ts +29 -0
- package/dist/entities/position-entity.js +110 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +19 -0
- package/dist/modules/account-manager/index.d.ts +9 -0
- package/dist/modules/account-manager/index.js +43 -0
- package/dist/modules/auth-manager/index.d.ts +44 -0
- package/dist/modules/auth-manager/index.js +188 -0
- package/dist/modules/auth-manager/types.d.ts +7 -0
- package/dist/modules/auth-manager/types.js +2 -0
- package/dist/modules/index.d.ts +0 -0
- package/dist/modules/index.js +0 -0
- package/dist/modules/markets-manager/index.d.ts +23 -0
- package/dist/modules/markets-manager/index.js +119 -0
- package/dist/modules/trade-manager/index.d.ts +52 -0
- package/dist/modules/trade-manager/index.js +157 -0
- package/dist/modules/trade-manager/types.d.ts +0 -0
- package/dist/modules/trade-manager/types.js +0 -0
- package/dist/utils/api/getApiUrl.d.ts +1 -0
- package/dist/utils/api/getApiUrl.js +8 -0
- package/dist/utils/auth/apiAuth.d.ts +5 -0
- package/dist/utils/auth/apiAuth.js +23 -0
- package/dist/utils/auth/siweAuth.d.ts +20 -0
- package/dist/utils/auth/siweAuth.js +106 -0
- package/package.json +47 -0
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PositionEntity = exports.PositionEntityHooks = void 0;
|
|
4
|
+
const core_1 = require("@carbon-terminal/core");
|
|
5
|
+
const hookable_entity_1 = require("./library/hookable-entity");
|
|
6
|
+
var PositionEntityHooks;
|
|
7
|
+
(function (PositionEntityHooks) {
|
|
8
|
+
PositionEntityHooks["OPEN_PRICE_SETTLED"] = "open-price-settled";
|
|
9
|
+
PositionEntityHooks["OPEN_REJECTED"] = "open-rejected";
|
|
10
|
+
PositionEntityHooks["OPEN_SUCCESS"] = "open-success";
|
|
11
|
+
PositionEntityHooks["RESERVED"] = "reserved";
|
|
12
|
+
PositionEntityHooks["EXPIRED"] = "expired";
|
|
13
|
+
})(PositionEntityHooks || (exports.PositionEntityHooks = PositionEntityHooks = {}));
|
|
14
|
+
class PositionEntity extends hookable_entity_1.HookableEntity {
|
|
15
|
+
constructor(tradingSdk, position) {
|
|
16
|
+
super();
|
|
17
|
+
this.tradingSdk = tradingSdk;
|
|
18
|
+
this.internalPosition = position;
|
|
19
|
+
}
|
|
20
|
+
getStatus() {
|
|
21
|
+
return this.internalPosition.status;
|
|
22
|
+
}
|
|
23
|
+
getDetails() {
|
|
24
|
+
return this.internalPosition;
|
|
25
|
+
}
|
|
26
|
+
async update() {
|
|
27
|
+
const positionInfo = await this.tradingSdk.tradeManager.getPositionInfo(this.internalPosition.tempId?.toString() ??
|
|
28
|
+
this.internalPosition.id.toString(), this.internalPosition.chainId);
|
|
29
|
+
this.internalPosition = positionInfo;
|
|
30
|
+
if (this.internalPosition.avgOpenFillPrice &&
|
|
31
|
+
this.internalPosition.openFillQuantity) {
|
|
32
|
+
if (this.lastFillQuantity !== this.internalPosition.openFillQuantity) {
|
|
33
|
+
this.lastFillQuantity = this.internalPosition.openFillQuantity;
|
|
34
|
+
this.trigger(PositionEntityHooks.OPEN_PRICE_SETTLED, this.internalPosition);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
else if (this.internalPosition.status === core_1.PositionStatus.FAILED) {
|
|
38
|
+
if (!this.isHookTriggered(PositionEntityHooks.OPEN_REJECTED)) {
|
|
39
|
+
this.trigger(PositionEntityHooks.OPEN_REJECTED, this.internalPosition);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
else if (this.internalPosition.status === core_1.PositionStatus.OPENED) {
|
|
43
|
+
if (!this.isHookTriggered(PositionEntityHooks.OPEN_SUCCESS)) {
|
|
44
|
+
this.trigger(PositionEntityHooks.OPEN_SUCCESS, this.internalPosition);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
else if (this.internalPosition.status === core_1.PositionStatus.LOCKED) {
|
|
48
|
+
if (!this.isHookTriggered(PositionEntityHooks.RESERVED)) {
|
|
49
|
+
this.trigger(PositionEntityHooks.RESERVED, this.internalPosition);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
else if (this.internalPosition.status === core_1.PositionStatus.EXPIRED) {
|
|
53
|
+
if (!this.isHookTriggered(PositionEntityHooks.EXPIRED)) {
|
|
54
|
+
this.trigger(PositionEntityHooks.EXPIRED, this.internalPosition);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return this.internalPosition;
|
|
58
|
+
}
|
|
59
|
+
isConfirmedOnChain() {
|
|
60
|
+
return this.internalPosition.status === core_1.PositionStatus.OPENED;
|
|
61
|
+
}
|
|
62
|
+
isPriceSettled() {
|
|
63
|
+
return (this.internalPosition.avgOpenFillPrice ||
|
|
64
|
+
this.internalPosition.status === core_1.PositionStatus.OPENED);
|
|
65
|
+
}
|
|
66
|
+
isFailed() {
|
|
67
|
+
return this.internalPosition.status === core_1.PositionStatus.FAILED;
|
|
68
|
+
}
|
|
69
|
+
// Trading functions
|
|
70
|
+
async close(quantityToClose, closePrice, orderType) {
|
|
71
|
+
if (this.internalPosition.status === core_1.PositionStatus.INSTANT_OPEN_SENT) {
|
|
72
|
+
throw new Error("The position can't be closed before it's confirmed on-chain");
|
|
73
|
+
}
|
|
74
|
+
if (this.internalPosition.status !== core_1.PositionStatus.OPENED) {
|
|
75
|
+
throw new Error("Position is not opened");
|
|
76
|
+
}
|
|
77
|
+
if (!this.internalPosition.id ||
|
|
78
|
+
isNaN(Number(this.internalPosition.id)) ||
|
|
79
|
+
Number(this.internalPosition.id < 0)) {
|
|
80
|
+
throw new Error("Position is not suitable for close (id not found)");
|
|
81
|
+
}
|
|
82
|
+
const closeResult = await this.tradingSdk.tradeManager.closePosition({
|
|
83
|
+
quoteId: this.internalPosition.id,
|
|
84
|
+
quantityToClose: quantityToClose,
|
|
85
|
+
chainId: this.internalPosition.chainId,
|
|
86
|
+
orderType,
|
|
87
|
+
});
|
|
88
|
+
return closeResult;
|
|
89
|
+
}
|
|
90
|
+
// Hookability
|
|
91
|
+
async waitForCompletion() {
|
|
92
|
+
while (this.internalPosition.status !== core_1.PositionStatus.OPENED &&
|
|
93
|
+
this.internalPosition.status !== core_1.PositionStatus.FAILED
|
|
94
|
+
// this.internalPosition.status !== PositionStatus.LOCKED
|
|
95
|
+
) {
|
|
96
|
+
await this.update();
|
|
97
|
+
// 3s polling
|
|
98
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
async waitForOnChainConfirmation() {
|
|
102
|
+
while (this.internalPosition.status !== core_1.PositionStatus.OPENED &&
|
|
103
|
+
this.internalPosition.status !== core_1.PositionStatus.FAILED &&
|
|
104
|
+
this.internalPosition.status !== core_1.PositionStatus.LOCKED) {
|
|
105
|
+
await this.update();
|
|
106
|
+
await new Promise((resolve) => setTimeout(resolve, 3000));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
exports.PositionEntity = PositionEntity;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
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("./client"), exports);
|
|
18
|
+
__exportStar(require("./common"), exports);
|
|
19
|
+
__exportStar(require("./entities"), exports);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { AccountBalanceDetails, SubaccountDetails } from "@carbon-terminal/core";
|
|
2
|
+
import { TradingSDK } from "src/client";
|
|
3
|
+
export declare class AccountManager {
|
|
4
|
+
private readonly tradingSdk;
|
|
5
|
+
constructor(tradingSdk: TradingSDK);
|
|
6
|
+
listSubaccounts(userAddress: string): Promise<SubaccountDetails[]>;
|
|
7
|
+
getSubaccountDetails(subaccountAddress: string, chainId: number): Promise<SubaccountDetails>;
|
|
8
|
+
getSubaccountBalanceDetails(subaccountAddress: string, chainId: number): Promise<AccountBalanceDetails>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AccountManager = void 0;
|
|
4
|
+
const exceptions_1 = require("../../common/exceptions");
|
|
5
|
+
class AccountManager {
|
|
6
|
+
constructor(tradingSdk) {
|
|
7
|
+
this.tradingSdk = tradingSdk;
|
|
8
|
+
}
|
|
9
|
+
async listSubaccounts(userAddress) {
|
|
10
|
+
const response = await this.tradingSdk.get(`/accounts/subaccounts?userAddress=${userAddress}`);
|
|
11
|
+
if (!response.success) {
|
|
12
|
+
throw new exceptions_1.APIException({
|
|
13
|
+
message: response.error?.message || "Failed to list subaccounts",
|
|
14
|
+
code: response.error?.code,
|
|
15
|
+
httpStatusCode: response.httpStatusCode,
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
return response.data;
|
|
19
|
+
}
|
|
20
|
+
async getSubaccountDetails(subaccountAddress, chainId) {
|
|
21
|
+
const response = await this.tradingSdk.get(`/accounts/subaccount/details?subaccountAddress=${subaccountAddress}&chainId=${chainId}`);
|
|
22
|
+
if (!response.success) {
|
|
23
|
+
throw new exceptions_1.APIException({
|
|
24
|
+
message: response.error?.message || "Failed to get subaccount details",
|
|
25
|
+
code: response.error?.code,
|
|
26
|
+
httpStatusCode: response.httpStatusCode,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
return response.data;
|
|
30
|
+
}
|
|
31
|
+
async getSubaccountBalanceDetails(subaccountAddress, chainId) {
|
|
32
|
+
const response = await this.tradingSdk.get(`/accounts/subaccount/balance?subaccountAddress=${subaccountAddress}&chainId=${chainId}`);
|
|
33
|
+
if (!response.success) {
|
|
34
|
+
throw new exceptions_1.APIException({
|
|
35
|
+
message: response.error?.message || "Failed to get subaccount balance details",
|
|
36
|
+
code: response.error?.code,
|
|
37
|
+
httpStatusCode: response.httpStatusCode,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
return response.data;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.AccountManager = AccountManager;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { AllowedPermission, AvailableSolvers } from "@carbon-terminal/core";
|
|
2
|
+
import { TradingSDK } from "../../client";
|
|
3
|
+
import { ApiKey } from "./types";
|
|
4
|
+
interface EnableInstantActionParams {
|
|
5
|
+
subaccountAddress: string;
|
|
6
|
+
chainId: number;
|
|
7
|
+
solver: AvailableSolvers;
|
|
8
|
+
}
|
|
9
|
+
interface ToggleSubaccountParams {
|
|
10
|
+
subaccountAddress: string;
|
|
11
|
+
chainId: number;
|
|
12
|
+
enable: boolean;
|
|
13
|
+
}
|
|
14
|
+
declare class AuthManager {
|
|
15
|
+
private tradingSdk;
|
|
16
|
+
private token;
|
|
17
|
+
private privateKey;
|
|
18
|
+
constructor(tradingSdk: TradingSDK);
|
|
19
|
+
loginWithPrivateKey(privateKey: string): Promise<void>;
|
|
20
|
+
allowInstantAction(params: EnableInstantActionParams): Promise<void>;
|
|
21
|
+
toggleSubaccount(params: ToggleSubaccountParams): Promise<void>;
|
|
22
|
+
createApiKey(): Promise<string>;
|
|
23
|
+
listApiKeys(): Promise<ApiKey[]>;
|
|
24
|
+
validateSubaccountAccess(subaccountAddress: string): Promise<{
|
|
25
|
+
hasAccess: boolean;
|
|
26
|
+
instantActionAccesses: {
|
|
27
|
+
solver: AvailableSolvers;
|
|
28
|
+
expirationTimestamp: number;
|
|
29
|
+
}[];
|
|
30
|
+
}>;
|
|
31
|
+
verifyInstantActionAccess(subaccountAddress: string, solver: AvailableSolvers): Promise<boolean>;
|
|
32
|
+
/**
|
|
33
|
+
* Verify that the API key that has been configured to the SDK has access to operate on the account with the given permissions
|
|
34
|
+
* @param subaccountAddress
|
|
35
|
+
* @param permissionsToCheck
|
|
36
|
+
*/
|
|
37
|
+
verifyPermissionsToSubaccount(subaccountAddress: string, permissionsToCheck?: AllowedPermission): Promise<boolean>;
|
|
38
|
+
validateApiKey(apiKey: string): Promise<{
|
|
39
|
+
apiKeyIdentifier: string;
|
|
40
|
+
isValid: boolean;
|
|
41
|
+
}>;
|
|
42
|
+
delegateSubaccountApiPermission(subaccountAddress: string, permissions: AllowedPermission[], toApiKeyIdentifier: string): Promise<unknown>;
|
|
43
|
+
}
|
|
44
|
+
export { AuthManager };
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AuthManager = void 0;
|
|
4
|
+
const ethers_1 = require("ethers");
|
|
5
|
+
const exceptions_1 = require("../../common/exceptions");
|
|
6
|
+
const getApiUrl_1 = require("../../utils/api/getApiUrl");
|
|
7
|
+
const apiAuth_1 = require("../../utils/auth/apiAuth");
|
|
8
|
+
const siweAuth_1 = require("../../utils/auth/siweAuth");
|
|
9
|
+
class AuthManager {
|
|
10
|
+
constructor(tradingSdk) {
|
|
11
|
+
this.tradingSdk = tradingSdk;
|
|
12
|
+
this.token = null;
|
|
13
|
+
this.privateKey = null;
|
|
14
|
+
}
|
|
15
|
+
async loginWithPrivateKey(privateKey) {
|
|
16
|
+
this.privateKey = privateKey;
|
|
17
|
+
const { token } = await (0, apiAuth_1.authenticateWithPrivateKey)((0, getApiUrl_1.getApiUrl)(), privateKey);
|
|
18
|
+
this.token = token;
|
|
19
|
+
}
|
|
20
|
+
async allowInstantAction(params) {
|
|
21
|
+
if (!this.token)
|
|
22
|
+
throw new Error("Not authenticated, please, authenticate with private key");
|
|
23
|
+
if (!this.privateKey)
|
|
24
|
+
throw new Error("No private key, please, authenticate with private key");
|
|
25
|
+
const siweSignature = await (0, siweAuth_1.createSiweSignatureWithPrivateKey)(params.subaccountAddress, params.chainId, params.solver, this.privateKey);
|
|
26
|
+
// Wait 2 seconds to make sure nonce is propagated
|
|
27
|
+
await new Promise((resolve) => setTimeout(resolve, 5000));
|
|
28
|
+
const siweToken = await (0, siweAuth_1.siweGetAccessToken)(params.subaccountAddress, params.chainId, params.solver, siweSignature);
|
|
29
|
+
const expirationTimestamp = Math.floor(Date.now() / 1000) + 3600 * 24 * 365;
|
|
30
|
+
const response = await this.tradingSdk.post("/account-auth/instant-action/token", {
|
|
31
|
+
subaccountAddress: (0, ethers_1.getAddress)(params.subaccountAddress),
|
|
32
|
+
chainId: params.chainId,
|
|
33
|
+
solver: params.solver,
|
|
34
|
+
token: siweToken,
|
|
35
|
+
expirationTimestamp,
|
|
36
|
+
}, {
|
|
37
|
+
headers: {
|
|
38
|
+
Authorization: `Bearer ${this.token}`,
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
if (!response.success) {
|
|
42
|
+
throw new exceptions_1.APIException({
|
|
43
|
+
message: response.error?.message,
|
|
44
|
+
code: response.error?.code,
|
|
45
|
+
httpStatusCode: response.httpStatusCode,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
async toggleSubaccount(params) {
|
|
50
|
+
if (!this.token)
|
|
51
|
+
throw new Error("Not authenticated");
|
|
52
|
+
const response = await this.tradingSdk.post("/account-auth/subaccount/toggle-access", {
|
|
53
|
+
subaccountAddress: (0, ethers_1.getAddress)(params.subaccountAddress),
|
|
54
|
+
chainId: params.chainId,
|
|
55
|
+
enable: params.enable,
|
|
56
|
+
}, {
|
|
57
|
+
headers: {
|
|
58
|
+
Authorization: `Bearer ${this.token}`,
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
if (!response.success) {
|
|
62
|
+
throw new exceptions_1.APIException({
|
|
63
|
+
message: response.error?.message,
|
|
64
|
+
code: response.error?.code,
|
|
65
|
+
httpStatusCode: response.httpStatusCode,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
async createApiKey() {
|
|
70
|
+
if (!this.token)
|
|
71
|
+
throw new Error("Not authenticated");
|
|
72
|
+
const response = await this.tradingSdk.post("/api-keys/create", {
|
|
73
|
+
name: "API Key",
|
|
74
|
+
}, {
|
|
75
|
+
headers: {
|
|
76
|
+
Authorization: `Bearer ${this.token}`,
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
if (!response.success) {
|
|
80
|
+
throw new exceptions_1.APIException({
|
|
81
|
+
message: response.error?.message,
|
|
82
|
+
code: response.error?.code,
|
|
83
|
+
httpStatusCode: response.httpStatusCode,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
return response.data.apiKey;
|
|
87
|
+
}
|
|
88
|
+
async listApiKeys() {
|
|
89
|
+
if (!this.token)
|
|
90
|
+
throw new Error("Not authenticated with JWT");
|
|
91
|
+
const response = await this.tradingSdk.get("/api-keys/list", {
|
|
92
|
+
headers: {
|
|
93
|
+
Authorization: `Bearer ${this.token}`,
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
if (!response.success) {
|
|
97
|
+
throw new exceptions_1.APIException({
|
|
98
|
+
message: response.error?.message,
|
|
99
|
+
code: response.error?.code,
|
|
100
|
+
httpStatusCode: response.httpStatusCode,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
return response.data;
|
|
104
|
+
}
|
|
105
|
+
async validateSubaccountAccess(subaccountAddress) {
|
|
106
|
+
const response = await this.tradingSdk.get("/account-auth/verify?subaccountAddress=" + subaccountAddress, {
|
|
107
|
+
headers: {
|
|
108
|
+
Authorization: `Bearer ${this.token}`,
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
if (!response.success) {
|
|
112
|
+
throw new exceptions_1.APIException({
|
|
113
|
+
message: response.error?.message,
|
|
114
|
+
code: response.error?.code,
|
|
115
|
+
httpStatusCode: response.httpStatusCode,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
return response.data;
|
|
119
|
+
}
|
|
120
|
+
async verifyInstantActionAccess(subaccountAddress, solver) {
|
|
121
|
+
const response = await this.tradingSdk.get(`/account-auth/verify-instant-action-api-access`, {
|
|
122
|
+
params: {
|
|
123
|
+
subaccountAddress,
|
|
124
|
+
solver,
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
if (!response.success) {
|
|
128
|
+
throw new exceptions_1.APIException({
|
|
129
|
+
message: response.error?.message,
|
|
130
|
+
code: response.error?.code,
|
|
131
|
+
httpStatusCode: response.httpStatusCode,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
return response.data;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Verify that the API key that has been configured to the SDK has access to operate on the account with the given permissions
|
|
138
|
+
* @param subaccountAddress
|
|
139
|
+
* @param permissionsToCheck
|
|
140
|
+
*/
|
|
141
|
+
async verifyPermissionsToSubaccount(subaccountAddress, permissionsToCheck) {
|
|
142
|
+
const response = await this.tradingSdk.post("/api-keys/check-permission", {
|
|
143
|
+
subaccountAddress,
|
|
144
|
+
permissionsToCheck,
|
|
145
|
+
});
|
|
146
|
+
if (!response.success) {
|
|
147
|
+
throw new exceptions_1.APIException({
|
|
148
|
+
message: response.error?.message,
|
|
149
|
+
code: response.error?.code,
|
|
150
|
+
httpStatusCode: response.httpStatusCode,
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
return response.data;
|
|
154
|
+
}
|
|
155
|
+
async validateApiKey(apiKey) {
|
|
156
|
+
const response = await this.tradingSdk.post("/api-keys/validate", {
|
|
157
|
+
apiKey,
|
|
158
|
+
});
|
|
159
|
+
if (!response.success) {
|
|
160
|
+
throw new exceptions_1.APIException({
|
|
161
|
+
message: response.error?.message,
|
|
162
|
+
code: response.error?.code,
|
|
163
|
+
httpStatusCode: response.httpStatusCode,
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
return response.data;
|
|
167
|
+
}
|
|
168
|
+
async delegateSubaccountApiPermission(subaccountAddress, permissions, toApiKeyIdentifier) {
|
|
169
|
+
const response = await this.tradingSdk.post("/account-auth/delegate-access", {
|
|
170
|
+
subaccountAddress,
|
|
171
|
+
permissions,
|
|
172
|
+
apiKeyIdentifier: toApiKeyIdentifier,
|
|
173
|
+
}, {
|
|
174
|
+
headers: {
|
|
175
|
+
Authorization: `Bearer ${this.token}`,
|
|
176
|
+
},
|
|
177
|
+
});
|
|
178
|
+
if (!response.success) {
|
|
179
|
+
throw new exceptions_1.APIException({
|
|
180
|
+
message: response.error?.message,
|
|
181
|
+
code: response.error?.code,
|
|
182
|
+
httpStatusCode: response.httpStatusCode,
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
return response.data;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
exports.AuthManager = AuthManager;
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { AggregatedMarketsData, AvailableSolvers, Market, MarketTicker, SupportedChainId } from "@carbon-terminal/core";
|
|
2
|
+
import { TradingSDK } from "src/client";
|
|
3
|
+
export declare class MarketsManager {
|
|
4
|
+
private tradingSdk;
|
|
5
|
+
constructor(tradingSdk: TradingSDK);
|
|
6
|
+
getAggregatedMarkets(chainId: SupportedChainId): Promise<AggregatedMarketsData>;
|
|
7
|
+
getMarketTickerById(marketId: number, chainId: SupportedChainId): Promise<MarketTicker | null>;
|
|
8
|
+
getMarketTickerBySymbol(symbol: string, chainId: SupportedChainId): Promise<MarketTicker | null>;
|
|
9
|
+
getSolverMarketBySymbol(symbol: string, chainId: SupportedChainId, solver: AvailableSolvers): Promise<Market | null>;
|
|
10
|
+
getSolverMarketById(marketId: number, chainId: SupportedChainId, solver: AvailableSolvers): Promise<Market | null>;
|
|
11
|
+
getMarkPriceForSymbol(symbol: string): Promise<number>;
|
|
12
|
+
getLastPriceForSymbol(symbol: string): Promise<number>;
|
|
13
|
+
getMarkPricesForSymbols(symbols: string[]): Promise<Record<string, number>>;
|
|
14
|
+
getLastPricesForSymbols(symbols: string[]): Promise<Record<string, number>>;
|
|
15
|
+
getSolverOpenInterest(solver: AvailableSolvers, chainId: SupportedChainId): Promise<{
|
|
16
|
+
used: number;
|
|
17
|
+
total: number;
|
|
18
|
+
}>;
|
|
19
|
+
getSolverNotionalCapForMarket(solver: AvailableSolvers, chainId: SupportedChainId, marketId: number): Promise<{
|
|
20
|
+
used: number;
|
|
21
|
+
total: number;
|
|
22
|
+
}>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MarketsManager = void 0;
|
|
4
|
+
const exceptions_1 = require("../../common/exceptions");
|
|
5
|
+
class MarketsManager {
|
|
6
|
+
constructor(tradingSdk) {
|
|
7
|
+
this.tradingSdk = tradingSdk;
|
|
8
|
+
}
|
|
9
|
+
async getAggregatedMarkets(chainId) {
|
|
10
|
+
const response = await this.tradingSdk.get(`/markets/aggregated/${chainId}`);
|
|
11
|
+
if (!response.success) {
|
|
12
|
+
throw new exceptions_1.APIException({
|
|
13
|
+
message: response.error?.message,
|
|
14
|
+
code: response.error?.code,
|
|
15
|
+
httpStatusCode: response.httpStatusCode,
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
return response.data;
|
|
19
|
+
}
|
|
20
|
+
async getMarketTickerById(marketId, chainId) {
|
|
21
|
+
const response = await this.tradingSdk.get(`/markets/${chainId}/by-id/${marketId}`);
|
|
22
|
+
if (!response.success) {
|
|
23
|
+
throw new exceptions_1.NotFoundException(response.error?.message);
|
|
24
|
+
}
|
|
25
|
+
return response.data;
|
|
26
|
+
}
|
|
27
|
+
async getMarketTickerBySymbol(symbol, chainId) {
|
|
28
|
+
const response = await this.tradingSdk.get(`/markets/${chainId}/by-symbol/${symbol}`);
|
|
29
|
+
if (!response.success) {
|
|
30
|
+
throw new exceptions_1.NotFoundException(response.error?.message);
|
|
31
|
+
}
|
|
32
|
+
return response.data;
|
|
33
|
+
}
|
|
34
|
+
async getSolverMarketBySymbol(symbol, chainId, solver) {
|
|
35
|
+
const response = await this.tradingSdk.get(`/markets/${chainId}/solver/${solver}/by-symbol/${symbol}`);
|
|
36
|
+
if (!response.success) {
|
|
37
|
+
throw new exceptions_1.APIException({
|
|
38
|
+
message: response.error?.message,
|
|
39
|
+
code: response.error?.code,
|
|
40
|
+
httpStatusCode: response.httpStatusCode,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
return response.data;
|
|
44
|
+
}
|
|
45
|
+
async getSolverMarketById(marketId, chainId, solver) {
|
|
46
|
+
const response = await this.tradingSdk.get(`/markets/${chainId}/solver/${solver}/by-id/${marketId}`);
|
|
47
|
+
if (!response.success) {
|
|
48
|
+
throw new exceptions_1.APIException({
|
|
49
|
+
message: response.error?.message,
|
|
50
|
+
code: response.error?.code,
|
|
51
|
+
httpStatusCode: response.httpStatusCode,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
return response.data;
|
|
55
|
+
}
|
|
56
|
+
async getMarkPriceForSymbol(symbol) {
|
|
57
|
+
const response = await this.tradingSdk.get(`/pricing/mark-price/${symbol}`);
|
|
58
|
+
if (!response.success) {
|
|
59
|
+
throw new exceptions_1.APIException({
|
|
60
|
+
message: response.error?.message,
|
|
61
|
+
code: response.error?.code,
|
|
62
|
+
httpStatusCode: response.httpStatusCode,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
return response.data;
|
|
66
|
+
}
|
|
67
|
+
async getLastPriceForSymbol(symbol) {
|
|
68
|
+
const response = await this.tradingSdk.get(`/pricing/last-price/${symbol}`);
|
|
69
|
+
if (!response.success) {
|
|
70
|
+
throw new exceptions_1.APIException({
|
|
71
|
+
message: response.error?.message,
|
|
72
|
+
code: response.error?.code,
|
|
73
|
+
httpStatusCode: response.httpStatusCode,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
return response.data;
|
|
77
|
+
}
|
|
78
|
+
async getMarkPricesForSymbols(symbols) {
|
|
79
|
+
const response = await this.tradingSdk.get(`/pricing/mark-prices?symbols=${symbols.join(",")}`);
|
|
80
|
+
if (!response.success) {
|
|
81
|
+
throw new exceptions_1.APIException({
|
|
82
|
+
message: response.error?.message,
|
|
83
|
+
code: response.error?.code,
|
|
84
|
+
httpStatusCode: response.httpStatusCode,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
return response.data;
|
|
88
|
+
}
|
|
89
|
+
async getLastPricesForSymbols(symbols) {
|
|
90
|
+
const response = await this.tradingSdk.get(`/pricing/last-prices?symbols=${symbols.join(",")}`);
|
|
91
|
+
if (!response.success) {
|
|
92
|
+
throw new exceptions_1.NotFoundException("");
|
|
93
|
+
}
|
|
94
|
+
return response.data;
|
|
95
|
+
}
|
|
96
|
+
async getSolverOpenInterest(solver, chainId) {
|
|
97
|
+
const response = await this.tradingSdk.get(`/solvers/open-interest?solver=${solver}&chainId=${chainId}`);
|
|
98
|
+
if (!response.success) {
|
|
99
|
+
throw new exceptions_1.APIException({
|
|
100
|
+
message: response.error?.message,
|
|
101
|
+
code: response.error?.code,
|
|
102
|
+
httpStatusCode: response.httpStatusCode,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
return response.data;
|
|
106
|
+
}
|
|
107
|
+
async getSolverNotionalCapForMarket(solver, chainId, marketId) {
|
|
108
|
+
const response = await this.tradingSdk.get(`/solvers/notional-cap?solver=${solver}&chainId=${chainId}&marketId=${marketId}`);
|
|
109
|
+
if (!response.success) {
|
|
110
|
+
throw new exceptions_1.APIException({
|
|
111
|
+
message: response.error?.message,
|
|
112
|
+
code: response.error?.code,
|
|
113
|
+
httpStatusCode: response.httpStatusCode,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
return response.data;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
exports.MarketsManager = MarketsManager;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { AvailableSolvers, CancelCloseRequestState, CancelQuoteRequestState, CloseRequestState, ListTpSlResponse, OrderType, Position, PositionType, SolverLockedParams, SupportedChainId } from "@carbon-terminal/core";
|
|
2
|
+
import { TradingSDK } from "../../client";
|
|
3
|
+
import { CancelCloseRequestEntity, CancelRequestEntity, CloseRequestEntity, PositionEntity } from "../../entities";
|
|
4
|
+
export declare class TradeManager {
|
|
5
|
+
private readonly tradingSdk;
|
|
6
|
+
constructor(tradingSdk: TradingSDK);
|
|
7
|
+
getPositionInfo(positionId: string, chainId: SupportedChainId): Promise<Position>;
|
|
8
|
+
getOpenPositionsForSubaccount(subaccountAddress: string, chainId: SupportedChainId): Promise<Position[]>;
|
|
9
|
+
getCloseRequestInfo(closeRequestId: string, chainId: SupportedChainId): Promise<CloseRequestState>;
|
|
10
|
+
createPosition(params: {
|
|
11
|
+
marketId: number;
|
|
12
|
+
subaccountAddress: string;
|
|
13
|
+
chainId: SupportedChainId;
|
|
14
|
+
positionType: PositionType;
|
|
15
|
+
orderType: OrderType;
|
|
16
|
+
limitPrice?: number;
|
|
17
|
+
leverage: number;
|
|
18
|
+
quantity: number;
|
|
19
|
+
deadline?: number;
|
|
20
|
+
slippage?: number;
|
|
21
|
+
solver: AvailableSolvers;
|
|
22
|
+
useInstantActions?: boolean;
|
|
23
|
+
}): Promise<PositionEntity>;
|
|
24
|
+
closePosition(params: {
|
|
25
|
+
quoteId: number;
|
|
26
|
+
quantityToClose: number;
|
|
27
|
+
chainId: number;
|
|
28
|
+
slippage?: number;
|
|
29
|
+
orderType: OrderType;
|
|
30
|
+
useInstantActions?: boolean;
|
|
31
|
+
}): Promise<CloseRequestEntity>;
|
|
32
|
+
setTpSlForPosition(params: {
|
|
33
|
+
chainId: number;
|
|
34
|
+
userAddress: string;
|
|
35
|
+
accountAddress: string;
|
|
36
|
+
quoteId: string;
|
|
37
|
+
tpPrice: number;
|
|
38
|
+
slPrice: number;
|
|
39
|
+
}): Promise<void>;
|
|
40
|
+
listTpSlForPositions(chainId: SupportedChainId, quoteIds: string[]): Promise<ListTpSlResponse>;
|
|
41
|
+
getLockedParameters(chainId: SupportedChainId, solver: AvailableSolvers, marketName: string, leverage: number): Promise<SolverLockedParams>;
|
|
42
|
+
createCancelQuoteRequest(params: {
|
|
43
|
+
chainId: SupportedChainId;
|
|
44
|
+
quoteId: string;
|
|
45
|
+
}): Promise<CancelRequestEntity>;
|
|
46
|
+
getCancelQuoteRequestInfo(cancelRequestId: string, chainId: SupportedChainId): Promise<CancelQuoteRequestState>;
|
|
47
|
+
cancelCloseRequest(params: {
|
|
48
|
+
chainId: SupportedChainId;
|
|
49
|
+
quoteId: number;
|
|
50
|
+
}): Promise<CancelCloseRequestEntity>;
|
|
51
|
+
getCancelCloseRequestInfo(cancelRequestId: string, chainId: SupportedChainId): Promise<CancelCloseRequestState>;
|
|
52
|
+
}
|