@gmx-io/sdk 1.6.2 → 1.6.3
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/build/cjs/src/clients/v2/__tests__/subaccount.spec.js +270 -0
- package/build/cjs/src/clients/v2/__tests__/testUtil.js +92 -1
- package/build/cjs/src/clients/v2/index.js +34 -57
- package/build/cjs/src/utils/subaccount/index.js +11 -1
- package/build/cjs/src/utils/subaccount/sdkClient.js +367 -0
- package/build/cjs/src/utils/subaccount/state.js +76 -0
- package/build/esm/src/clients/v2/__tests__/subaccount.spec.js +271 -1
- package/build/esm/src/clients/v2/__tests__/testUtil.js +87 -0
- package/build/esm/src/clients/v2/index.js +33 -56
- package/build/esm/src/utils/subaccount/index.js +1 -0
- package/build/esm/src/utils/subaccount/sdkClient.js +352 -0
- package/build/esm/src/utils/subaccount/state.js +64 -0
- package/build/tsconfig.tsbuildinfo +1 -1
- package/build/types/src/clients/v2/__tests__/testUtil.d.ts +37 -1
- package/build/types/src/clients/v2/index.d.ts +8 -2
- package/build/types/src/utils/subaccount/index.d.ts +1 -0
- package/build/types/src/utils/subaccount/sdkClient.d.ts +36 -0
- package/build/types/src/utils/subaccount/state.d.ts +11 -0
- package/build/types/src/utils/subaccount/types.d.ts +18 -0
- package/package.json +1 -1
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { IHttp } from "../../../utils/http/types";
|
|
2
|
+
import type { PrepareCancelOrderRequest, PrepareCollateralRequest, PrepareEditOrderRequest, OrderStatusResponse, PrepareOrderRequest, PrepareOrderResponse, SubmitOrderRequest, SubmitOrderResponse } from "../../../utils/orderTransactions/api";
|
|
2
3
|
import { PrivateKeySigner } from "../../../utils/signer";
|
|
3
4
|
import type { IAbstractSigner } from "../../../utils/signer";
|
|
5
|
+
import type { SubaccountStatusRequest, SubaccountStatusResponse } from "../../../utils/subaccount/api";
|
|
4
6
|
import { GmxApiSdk } from "../index";
|
|
5
7
|
export declare const TEST_CHAIN_ID = 42161;
|
|
6
8
|
export declare const TEST_SYMBOL = "ETH/USD [WETH-USDC]";
|
|
@@ -9,6 +11,40 @@ export declare const TEST_COLLATERAL: {
|
|
|
9
11
|
amount: bigint;
|
|
10
12
|
token: string;
|
|
11
13
|
};
|
|
14
|
+
export type RecordedOrderPrepareRequest = PrepareOrderRequest | PrepareEditOrderRequest | PrepareCancelOrderRequest | PrepareCollateralRequest;
|
|
15
|
+
export declare class RecordingApi implements IHttp {
|
|
16
|
+
private delegate;
|
|
17
|
+
url: string;
|
|
18
|
+
orderPrepareRequests: RecordedOrderPrepareRequest[];
|
|
19
|
+
orderSubmitRequests: SubmitOrderRequest[];
|
|
20
|
+
constructor(delegate: IHttp);
|
|
21
|
+
fetchJson<TResult>(path: string, opts?: {
|
|
22
|
+
query?: Record<string, any>;
|
|
23
|
+
transform?: (result: any) => TResult;
|
|
24
|
+
}): Promise<TResult>;
|
|
25
|
+
postJson<TResult>(path: string, body: unknown, opts?: {
|
|
26
|
+
transform?: (result: any) => TResult;
|
|
27
|
+
}): Promise<TResult>;
|
|
28
|
+
}
|
|
29
|
+
export declare function buildPreparedOrderResponse(requestId: string): PrepareOrderResponse;
|
|
30
|
+
export declare class ScriptedOrderApi implements IHttp {
|
|
31
|
+
private readonly prepareResponse;
|
|
32
|
+
private readonly submitResponses;
|
|
33
|
+
private readonly subaccountStatusResponses;
|
|
34
|
+
url: string;
|
|
35
|
+
prepareRequests: PrepareOrderRequest[];
|
|
36
|
+
submitRequests: SubmitOrderRequest[];
|
|
37
|
+
subaccountStatusRequests: SubaccountStatusRequest[];
|
|
38
|
+
constructor(prepareResponse: PrepareOrderResponse, submitResponses?: SubmitOrderResponse[], subaccountStatusResponses?: SubaccountStatusResponse[]);
|
|
39
|
+
fetchJson<TResult>(path: string, _opts?: {
|
|
40
|
+
query?: Record<string, any>;
|
|
41
|
+
transform?: (result: any) => TResult;
|
|
42
|
+
}): Promise<TResult>;
|
|
43
|
+
postJson<TResult>(path: string, body: unknown, opts?: {
|
|
44
|
+
transform?: (result: any) => TResult;
|
|
45
|
+
}): Promise<TResult>;
|
|
46
|
+
}
|
|
47
|
+
export declare function buildSubaccountStatusResponse(overrides?: Partial<SubaccountStatusResponse>): SubaccountStatusResponse;
|
|
12
48
|
export declare function getTestSdk(): GmxApiSdk;
|
|
13
49
|
export declare function getTestSigner(): PrivateKeySigner | undefined;
|
|
14
50
|
export declare function requireSigner(): PrivateKeySigner;
|
|
@@ -14,6 +14,7 @@ import { MarketRates, RatesParams } from "../../utils/rates/types";
|
|
|
14
14
|
import type { IAbstractSigner } from "../../utils/signer";
|
|
15
15
|
import { StakingPowerResponse } from "../../utils/staking/types";
|
|
16
16
|
import type { SubaccountStatusRequest, SubaccountStatusResponse, SubaccountApprovalPrepareRequest, SubaccountApprovalPrepareResponse } from "../../utils/subaccount/api";
|
|
17
|
+
import type { SdkSubaccountApproval, SdkSubaccountStatus } from "../../utils/subaccount/types";
|
|
17
18
|
import type { FetchTradesParams, SearchTradesParams, TradesListResponse } from "../../utils/trades/types";
|
|
18
19
|
export type { ApyEntry, ApyParams, ApyResponse } from "../../utils/apy/types";
|
|
19
20
|
export type { MarketTicker, MarketWithTiers } from "../../utils/markets/types";
|
|
@@ -36,20 +37,24 @@ export { HttpClientWithFallback } from "../../utils/http/httpFallback";
|
|
|
36
37
|
export type { IHttp } from "../../utils/http/types";
|
|
37
38
|
export { getGasPaymentTokens } from "../../configs/express";
|
|
38
39
|
export type { SubaccountStatusRequest, SubaccountStatusResponse, SubaccountApprovalPrepareRequest, SubaccountApprovalPrepareResponse, };
|
|
40
|
+
export type { SdkSubaccountStatus } from "../../utils/subaccount/types";
|
|
39
41
|
export declare class GmxApiSdk {
|
|
40
42
|
ctx: {
|
|
41
43
|
chainId: ContractsChainId;
|
|
42
44
|
api: IHttp;
|
|
43
45
|
};
|
|
44
46
|
private _subaccount;
|
|
47
|
+
private preparedSubaccountApprovals;
|
|
45
48
|
constructor({ chainId, apiUrl, api }: {
|
|
46
49
|
chainId: ContractsChainId;
|
|
47
50
|
apiUrl?: string;
|
|
48
51
|
api?: IHttp;
|
|
49
52
|
});
|
|
53
|
+
private get subaccountClient();
|
|
50
54
|
get subaccountAddress(): string | undefined;
|
|
55
|
+
get subaccountStatus(): SdkSubaccountStatus | undefined;
|
|
51
56
|
get hasActiveSubaccount(): boolean;
|
|
52
|
-
get subaccountApprovalMessage():
|
|
57
|
+
get subaccountApprovalMessage(): SdkSubaccountApproval | undefined;
|
|
53
58
|
fetchMarketsInfo(): Promise<import("../../utils/markets/types").RawMarketInfo[]>;
|
|
54
59
|
fetchMarkets(): Promise<MarketWithTiers[]>;
|
|
55
60
|
fetchMarketsTickers(params?: {
|
|
@@ -114,6 +119,7 @@ export declare class GmxApiSdk {
|
|
|
114
119
|
prepareCancelOrder(request: PrepareCancelOrderRequest): Promise<PrepareOrderResponse>;
|
|
115
120
|
prepareCollateral(request: PrepareCollateralRequest): Promise<PrepareOrderResponse>;
|
|
116
121
|
fetchSubaccountStatus(request: SubaccountStatusRequest): Promise<SubaccountStatusResponse>;
|
|
122
|
+
refreshSubaccountState(account: string): Promise<SdkSubaccountStatus | undefined>;
|
|
117
123
|
prepareSubaccountApproval(request: SubaccountApprovalPrepareRequest): Promise<SubaccountApprovalPrepareResponse>;
|
|
118
124
|
signSubaccountApproval(prepared: SubaccountApprovalPrepareResponse, signer: IAbstractSigner): Promise<string>;
|
|
119
125
|
/**
|
|
@@ -133,7 +139,7 @@ export declare class GmxApiSdk {
|
|
|
133
139
|
*/
|
|
134
140
|
activateSubaccount(mainSigner: IAbstractSigner, options?: {
|
|
135
141
|
expiresInSeconds?: number;
|
|
136
|
-
maxAllowedCount?: number;
|
|
142
|
+
maxAllowedCount?: number | bigint;
|
|
137
143
|
}): Promise<string>;
|
|
138
144
|
clearSubaccount(): void;
|
|
139
145
|
}
|
|
@@ -3,5 +3,6 @@ export { generateSubaccount, decryptSubaccountPrivateKey, createSubaccountSigner
|
|
|
3
3
|
export { hashSubaccountApproval } from "./hashSubaccountApproval";
|
|
4
4
|
export { getEmptySubaccountApproval } from "./getEmptySubaccountApproval";
|
|
5
5
|
export { getSubaccountApprovalTypedData, type SubaccountApprovalTypedData } from "./getSubaccountApprovalTypedData";
|
|
6
|
+
export { isEmptySubaccountApproval, isSameSubaccountApproval, isSubaccountApprovalExpired, isSubaccountApprovalNonceExpired, isSubaccountApprovalSynced, isSubaccountStatusUsable, parseSubaccountStatus, shouldRefreshExplicitSubaccountApproval, toSignedSubaccountApproval, } from "./state";
|
|
6
7
|
export { fetchSubaccountStatus, prepareSubaccountApproval, signSubaccountApproval } from "./api";
|
|
7
8
|
export type { SubaccountStatusRequest, SubaccountStatusResponse, SubaccountApprovalPrepareRequest, SubaccountApprovalPrepareResponse, } from "./api";
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { ContractsChainId } from "../../configs/chains";
|
|
2
|
+
import type { IHttp } from "../http/types";
|
|
3
|
+
import type { PrepareCancelOrderRequest, PrepareCollateralRequest, PrepareEditOrderRequest, PrepareOrderRequest, PrepareOrderResponse, SubmitOrderRequest, SubmitOrderResponse } from "../orderTransactions/api";
|
|
4
|
+
import type { IAbstractSigner } from "../signer";
|
|
5
|
+
import type { SdkSubaccountApproval, SdkSubaccountStatus, SubaccountState } from "./types";
|
|
6
|
+
export type SdkSubaccountClientContext = {
|
|
7
|
+
ctx: {
|
|
8
|
+
chainId: ContractsChainId;
|
|
9
|
+
api: IHttp;
|
|
10
|
+
};
|
|
11
|
+
owner: object;
|
|
12
|
+
preparedSubaccountApprovals: Map<string, SdkSubaccountApproval>;
|
|
13
|
+
getSubaccount: () => SubaccountState | undefined;
|
|
14
|
+
setSubaccount: (subaccount: SubaccountState | undefined) => void;
|
|
15
|
+
};
|
|
16
|
+
export type SubaccountPrepareRequest = PrepareOrderRequest | PrepareEditOrderRequest | PrepareCancelOrderRequest | PrepareCollateralRequest;
|
|
17
|
+
export type PrepareWithContext<TRequest extends SubaccountPrepareRequest> = (ctx: {
|
|
18
|
+
api: IHttp;
|
|
19
|
+
}, request: TRequest) => Promise<PrepareOrderResponse>;
|
|
20
|
+
export declare function hasActiveSdkSubaccount(client: SdkSubaccountClientContext): boolean;
|
|
21
|
+
export declare function getSdkSubaccountApprovalMessage(client: SdkSubaccountClientContext): SdkSubaccountApproval | undefined;
|
|
22
|
+
export declare function generateSdkSubaccount(client: SdkSubaccountClientContext, mainSigner: IAbstractSigner): Promise<string>;
|
|
23
|
+
export declare function reconcileSdkSubaccountApproval(subaccount: SubaccountState | undefined): void;
|
|
24
|
+
export declare function refreshSdkSubaccountState(client: SdkSubaccountClientContext, account: string): Promise<SdkSubaccountStatus | undefined>;
|
|
25
|
+
export declare function activateSdkSubaccount(client: SdkSubaccountClientContext, mainSigner: IAbstractSigner, options?: {
|
|
26
|
+
expiresInSeconds?: number;
|
|
27
|
+
maxAllowedCount?: number | bigint;
|
|
28
|
+
}, knownStatus?: SdkSubaccountStatus): Promise<string>;
|
|
29
|
+
export declare function getSdkSubaccountApprovalForOrder(client: SdkSubaccountClientContext, account: string, mainSigner?: IAbstractSigner, options?: {
|
|
30
|
+
forceRefresh?: boolean;
|
|
31
|
+
}): Promise<SdkSubaccountApproval | undefined>;
|
|
32
|
+
export declare function prepareWithSubaccount<TRequest extends SubaccountPrepareRequest>(client: SdkSubaccountClientContext, request: TRequest, prepareFn: PrepareWithContext<TRequest>, mainSigner?: IAbstractSigner): Promise<PrepareOrderResponse>;
|
|
33
|
+
export declare function signOrderWithSubaccount(client: SdkSubaccountClientContext, prepared: PrepareOrderResponse, signer: IAbstractSigner): Promise<string>;
|
|
34
|
+
export declare function submitOrderWithSubaccount(client: SdkSubaccountClientContext, request: SubmitOrderRequest): Promise<SubmitOrderResponse>;
|
|
35
|
+
export declare function executeExpressOrderWithSubaccount(client: SdkSubaccountClientContext, request: PrepareOrderRequest, signer: IAbstractSigner): Promise<SubmitOrderResponse>;
|
|
36
|
+
export declare function clearSdkSubaccount(client: SdkSubaccountClientContext): void;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { SubaccountApprovalPrepareResponse, SubaccountStatusResponse } from "./api";
|
|
2
|
+
import type { SdkSubaccountApproval, SdkSubaccountStatus, SignedSubaccountApproval, StoredSubaccountApproval } from "./types";
|
|
3
|
+
export declare function parseSubaccountStatus(status: SubaccountStatusResponse): SdkSubaccountStatus;
|
|
4
|
+
export declare function isSubaccountStatusUsable(status: SdkSubaccountStatus | undefined): boolean;
|
|
5
|
+
export declare function isEmptySubaccountApproval(approval: SdkSubaccountApproval): boolean;
|
|
6
|
+
export declare function isSameSubaccountApproval(a: SdkSubaccountApproval, b: SdkSubaccountApproval): boolean;
|
|
7
|
+
export declare function shouldRefreshExplicitSubaccountApproval(approval: SdkSubaccountApproval, status: SdkSubaccountStatus | undefined, pendingApproval: StoredSubaccountApproval | undefined): boolean;
|
|
8
|
+
export declare function isSubaccountApprovalNonceExpired(approval: SdkSubaccountApproval, status: SdkSubaccountStatus): boolean;
|
|
9
|
+
export declare function isSubaccountApprovalSynced(approval: SdkSubaccountApproval, status: SdkSubaccountStatus): boolean;
|
|
10
|
+
export declare function isSubaccountApprovalExpired(approval: SdkSubaccountApproval): boolean;
|
|
11
|
+
export declare function toSignedSubaccountApproval(prepared: SubaccountApprovalPrepareResponse, signature: string): SignedSubaccountApproval;
|
|
@@ -31,6 +31,24 @@ export type SignedSubaccountApproval = SubaccountApproval & {
|
|
|
31
31
|
signatureChainId: AnyChainId;
|
|
32
32
|
subaccountRouterAddress: string;
|
|
33
33
|
};
|
|
34
|
+
export type EmptySubaccountApproval = SubaccountApproval & {
|
|
35
|
+
signature: string;
|
|
36
|
+
};
|
|
37
|
+
export type SdkSubaccountApproval = SignedSubaccountApproval | EmptySubaccountApproval;
|
|
38
|
+
export type SdkSubaccountStatus = SubaccountOnchainData & {
|
|
39
|
+
remainingActions: bigint;
|
|
40
|
+
};
|
|
41
|
+
export type StoredSubaccountApproval = {
|
|
42
|
+
message: SignedSubaccountApproval;
|
|
43
|
+
submittedRequestId?: string;
|
|
44
|
+
};
|
|
45
|
+
export type SubaccountState = {
|
|
46
|
+
account?: string;
|
|
47
|
+
address: string;
|
|
48
|
+
approval?: StoredSubaccountApproval;
|
|
49
|
+
onchainData?: SdkSubaccountStatus;
|
|
50
|
+
lastStatusFetchedAt?: number;
|
|
51
|
+
};
|
|
34
52
|
export type Subaccount = {
|
|
35
53
|
address: string;
|
|
36
54
|
chainId: ContractsChainId;
|