@storm-trade/trading-sdk 1.0.0 → 1.0.2
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-clients/utils/cache.d.ts +8 -0
- package/dist/api-clients/utils/cache.esm.js +22 -0
- package/dist/api-clients/utils/cache.esm.js.map +1 -0
- package/dist/api-clients/utils/cache.js +24 -0
- package/dist/api-clients/utils/cache.js.map +1 -0
- package/dist/sdk/sdk.d.ts +37 -9
- package/dist/sdk/sdk.esm.js +237 -106
- package/dist/sdk/sdk.esm.js.map +1 -1
- package/dist/sdk/sdk.js +237 -106
- package/dist/sdk/sdk.js.map +1 -1
- package/dist/sdk/sdk.types.d.ts +27 -0
- package/package.json +1 -1
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare class Cache<V> {
|
|
2
|
+
private readonly map;
|
|
3
|
+
private readonly cacheKeySeparator;
|
|
4
|
+
private checkFlatOrMap;
|
|
5
|
+
get(key: string[] | string): V | undefined;
|
|
6
|
+
getOrThrow(key: string[] | string): V & ({} | null);
|
|
7
|
+
set(key: string[] | string, value: V): void;
|
|
8
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
class Cache {
|
|
2
|
+
map = new Map();
|
|
3
|
+
cacheKeySeparator = ':';
|
|
4
|
+
checkFlatOrMap(key) {
|
|
5
|
+
return Array.isArray(key) ? key.join(this.cacheKeySeparator) : key;
|
|
6
|
+
}
|
|
7
|
+
get(key) {
|
|
8
|
+
return this.map.get(this.checkFlatOrMap(key));
|
|
9
|
+
}
|
|
10
|
+
getOrThrow(key) {
|
|
11
|
+
const v = this.get(key);
|
|
12
|
+
if (v === undefined)
|
|
13
|
+
throw new Error(`Could not find value for ${key} in ${this.map}`);
|
|
14
|
+
return v;
|
|
15
|
+
}
|
|
16
|
+
set(key, value) {
|
|
17
|
+
this.map.set(this.checkFlatOrMap(key), value);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export { Cache };
|
|
22
|
+
//# sourceMappingURL=cache.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache.esm.js","sources":["../../../src/api-clients/utils/cache.ts"],"sourcesContent":[null],"names":[],"mappings":"MAAa,KAAK,CAAA;AACC,IAAA,GAAG,GAAoB,IAAI,GAAG,EAAE;IAEhC,iBAAiB,GAAG,GAAG;AAEhC,IAAA,cAAc,CAAC,GAAsB,EAAA;QAC3C,OAAO,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,iBAAiB,CAAC,GAAG,GAAG;;AAG7D,IAAA,GAAG,CAAC,GAAsB,EAAA;AAC/B,QAAA,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC;;AAGxC,IAAA,UAAU,CAAC,GAAsB,EAAA;QACtC,MAAM,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC;QACvB,IAAI,CAAC,KAAK,SAAS;YAAE,MAAM,IAAI,KAAK,CAAC,CAA4B,yBAAA,EAAA,GAAG,CAAO,IAAA,EAAA,IAAI,CAAC,GAAG,CAAE,CAAA,CAAC;AACtF,QAAA,OAAO,CAAC;;IAGH,GAAG,CAAC,GAAsB,EAAE,KAAQ,EAAA;AACzC,QAAA,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC;;AAEhD;;;;"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
class Cache {
|
|
4
|
+
map = new Map();
|
|
5
|
+
cacheKeySeparator = ':';
|
|
6
|
+
checkFlatOrMap(key) {
|
|
7
|
+
return Array.isArray(key) ? key.join(this.cacheKeySeparator) : key;
|
|
8
|
+
}
|
|
9
|
+
get(key) {
|
|
10
|
+
return this.map.get(this.checkFlatOrMap(key));
|
|
11
|
+
}
|
|
12
|
+
getOrThrow(key) {
|
|
13
|
+
const v = this.get(key);
|
|
14
|
+
if (v === undefined)
|
|
15
|
+
throw new Error(`Could not find value for ${key} in ${this.map}`);
|
|
16
|
+
return v;
|
|
17
|
+
}
|
|
18
|
+
set(key, value) {
|
|
19
|
+
this.map.set(this.checkFlatOrMap(key), value);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
exports.Cache = Cache;
|
|
24
|
+
//# sourceMappingURL=cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache.js","sources":["../../../src/api-clients/utils/cache.ts"],"sourcesContent":[null],"names":[],"mappings":";;MAAa,KAAK,CAAA;AACC,IAAA,GAAG,GAAoB,IAAI,GAAG,EAAE;IAEhC,iBAAiB,GAAG,GAAG;AAEhC,IAAA,cAAc,CAAC,GAAsB,EAAA;QAC3C,OAAO,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,iBAAiB,CAAC,GAAG,GAAG;;AAG7D,IAAA,GAAG,CAAC,GAAsB,EAAA;AAC/B,QAAA,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC;;AAGxC,IAAA,UAAU,CAAC,GAAsB,EAAA;QACtC,MAAM,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC;QACvB,IAAI,CAAC,KAAK,SAAS;YAAE,MAAM,IAAI,KAAK,CAAC,CAA4B,yBAAA,EAAA,GAAG,CAAO,IAAA,EAAA,IAAI,CAAC,GAAG,CAAE,CAAA,CAAC;AACtF,QAAA,OAAO,CAAC;;IAGH,GAAG,CAAC,GAAsB,EAAE,KAAQ,EAAA;AACzC,QAAA,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC;;AAEhD;;;;"}
|
package/dist/sdk/sdk.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { TonClient, TonClient4, Address } from '@ton/ton';
|
|
1
|
+
import { Address, TonClient, TonClient4 } from '@ton/ton';
|
|
3
2
|
import { LiteClient } from 'ton-lite-client';
|
|
4
|
-
import {
|
|
5
|
-
import { AddMarginParams, CancelOrderParams, LimitOrderParams, MarketCloseOrderParams as ClosePositionOrderParams, MarketOpenOrderParams, ProvideLiquidityParams, RemoveMarginParams, StopLimitOrderParams, StopLossOrderParams, StopMarketOrderParams, TakeProfitOrderParams, WithdrawLiquidityParams } from './sdk.types';
|
|
3
|
+
import { StormClient } from '../api-clients';
|
|
6
4
|
import { PositionManagerData } from '../api-clients/contracts/position-manager/position-manager.types';
|
|
5
|
+
import { TXParams } from '../common-packers';
|
|
6
|
+
import { AddMarginParams, AddMarginWithOraclePayload, AssetsParams, CancelOrderParams, CollateralAssets, LimitOrderParams, MarketCloseOrderParams as ClosePositionOrderParams, MarketOpenOrderParams, ProvideLiquidityParams, RemoveMarginParams, RemoveMarginWithOraclePayload, StopLimitOrderParams, StopLossOrderParams, StopMarketOrderParams, TakeProfitOrderParams, WithdrawLiquidityParams } from './sdk.types';
|
|
7
7
|
export declare class StormTradingSdk {
|
|
8
8
|
private readonly stormClient;
|
|
9
9
|
private readonly tonClient;
|
|
@@ -14,28 +14,56 @@ export declare class StormTradingSdk {
|
|
|
14
14
|
private readonly jettonMasters;
|
|
15
15
|
private readonly vaults;
|
|
16
16
|
private readonly positionManagers;
|
|
17
|
+
private lpWalletsAddressCache;
|
|
17
18
|
constructor(stormClient: StormClient, tonClient: TonClient | TonClient4 | LiteClient, traderAddress: Address | string);
|
|
18
19
|
init(): Promise<void>;
|
|
19
|
-
getPositionManagerAddressByAssets(
|
|
20
|
+
getPositionManagerAddressByAssets(opts: AssetsParams): Promise<Address>;
|
|
20
21
|
getPositionManagerData(positionManagerAddress: Address): Promise<PositionManagerData | null>;
|
|
22
|
+
prefetchCreateMarketOpenOrder(opts: AssetsParams): Promise<void>;
|
|
23
|
+
syncCreateMarketOpenOrder(opts: MarketOpenOrderParams): TXParams;
|
|
21
24
|
createMarketOpenOrder(opts: MarketOpenOrderParams): Promise<TXParams>;
|
|
25
|
+
prefetchCreateClosePositionOrder: (opts: AssetsParams) => Promise<void>;
|
|
26
|
+
syncCreateClosePositionOrder(opts: ClosePositionOrderParams): TXParams;
|
|
22
27
|
createClosePositionOrder(opts: ClosePositionOrderParams): Promise<TXParams>;
|
|
23
|
-
|
|
24
|
-
|
|
28
|
+
prefetchCreateLimitOrder: (opts: AssetsParams) => Promise<void>;
|
|
29
|
+
syncCreateLimitOrder(opts: LimitOrderParams): TXParams;
|
|
25
30
|
createLimitOrder(opts: LimitOrderParams): Promise<TXParams>;
|
|
31
|
+
prefetchCreateStopLimitOrder: (opts: AssetsParams) => Promise<void>;
|
|
32
|
+
syncCreateStopLimitOrder(opts: StopLimitOrderParams): TXParams;
|
|
26
33
|
createStopLimitOrder(opts: StopLimitOrderParams): Promise<TXParams>;
|
|
34
|
+
prefetchCreateStopMarketOrder: (opts: AssetsParams) => Promise<void>;
|
|
35
|
+
syncCreateStopMarketOrder(opts: StopMarketOrderParams): TXParams;
|
|
27
36
|
createStopMarketOrder(opts: StopMarketOrderParams): Promise<TXParams>;
|
|
37
|
+
prefetchCreteStopLossOrder: (opts: AssetsParams) => Promise<void>;
|
|
38
|
+
syncCreateStopLossOrder(opts: StopLossOrderParams): TXParams;
|
|
39
|
+
createStopLossOrder(opts: StopLossOrderParams): Promise<TXParams>;
|
|
40
|
+
prefetchCreteTakeProfitOrder: (opts: AssetsParams) => Promise<void>;
|
|
41
|
+
syncCreateTakeProfitOrder(opts: TakeProfitOrderParams): TXParams;
|
|
42
|
+
createTakeProfitOrder(opts: TakeProfitOrderParams): Promise<TXParams>;
|
|
43
|
+
prefetchAddMarginCaches(opts: AssetsParams): Promise<void>;
|
|
44
|
+
syncAddMargin(opts: AddMarginWithOraclePayload): TXParams;
|
|
45
|
+
addMargin(opts: AddMarginParams): Promise<TXParams>;
|
|
46
|
+
prefetchRemoveMarginCaches(opts: AssetsParams): Promise<void>;
|
|
47
|
+
syncRemoveMargin(opts: RemoveMarginWithOraclePayload): TXParams;
|
|
48
|
+
removeMargin(opts: RemoveMarginParams): Promise<TXParams>;
|
|
49
|
+
prefetchCancelOrderCaches(opts: AssetsParams): Promise<void>;
|
|
50
|
+
syncCreateCancelOrder(opts: CancelOrderParams): TXParams;
|
|
28
51
|
cancelOrder(opts: CancelOrderParams): Promise<TXParams>;
|
|
52
|
+
prefetchProvideLiquidity(opts: ProvideLiquidityParams): Promise<void>;
|
|
53
|
+
syncProvideLiquidity(opts: ProvideLiquidityParams): TXParams;
|
|
29
54
|
provideLiquidity(opts: ProvideLiquidityParams): Promise<TXParams>;
|
|
55
|
+
prefetchWithdrawLiquidityCaches(assetName: CollateralAssets): Promise<void>;
|
|
56
|
+
syncWithdrawLiquidity(opts: WithdrawLiquidityParams): TXParams;
|
|
30
57
|
withdrawLiquidity(opts: WithdrawLiquidityParams): Promise<TXParams>;
|
|
31
|
-
addMargin(opts: AddMarginParams): Promise<TXParams>;
|
|
32
|
-
removeMargin(opts: RemoveMarginParams): Promise<TXParams>;
|
|
33
58
|
private getJettonWalletAddressByAssetName;
|
|
34
59
|
private isNativeVault;
|
|
35
60
|
private checkIsPositionManagerInitialized;
|
|
36
61
|
private getOraclePayloadByAssets;
|
|
37
62
|
private createSLTPOrder;
|
|
63
|
+
private syncCreateSLTPOrder;
|
|
64
|
+
private prefetchSLTPOrderCaches;
|
|
38
65
|
private internalCreateLimitOrder;
|
|
66
|
+
private syncInternalCreateLimitOrder;
|
|
39
67
|
private getVaultContract;
|
|
40
68
|
private getJettonMasterContract;
|
|
41
69
|
private getPositionManagerContract;
|
package/dist/sdk/sdk.esm.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import '@hastom/fetch';
|
|
2
1
|
import { Address } from '@ton/ton';
|
|
2
|
+
import '@hastom/fetch';
|
|
3
3
|
import { TonClientAbstract } from '../api-clients/utils/ton-client-abstract.esm.js';
|
|
4
4
|
import { JettonMasterContract } from '../api-clients/contracts/jetton-master.esm.js';
|
|
5
5
|
import { VaultContract } from '../api-clients/contracts/vault.esm.js';
|
|
6
6
|
import { PositionManagerContract } from '../api-clients/contracts/position-manager.esm.js';
|
|
7
|
-
import {
|
|
7
|
+
import { Cache } from '../api-clients/utils/cache.esm.js';
|
|
8
8
|
import { OrderType } from '../base-packers/order-packers.types.esm.js';
|
|
9
9
|
import '../base-packers/opcodes.esm.js';
|
|
10
|
+
import { createMarketOrderTx, createAddMarginTx, createRemoveMarginTx, createCancelOrderTx, createProvideLiquidityTx, createWithdrawLiquidityTx, createSLTPOrderTx, createLimitOrderTx } from '../common-packers/order-packers.esm.js';
|
|
10
11
|
|
|
11
12
|
const marketOpenDefaultExpiration = () => Math.floor(Date.now() / 1000) + 15 * 60;
|
|
12
13
|
const limitDefaultExpiration = () => Math.floor(Date.now() / 1000) + 60 * 24 * 60 * 60;
|
|
@@ -14,12 +15,13 @@ class StormTradingSdk {
|
|
|
14
15
|
stormClient;
|
|
15
16
|
tonClient;
|
|
16
17
|
traderAddress;
|
|
17
|
-
jettonWalletsAddressCache = new
|
|
18
|
-
positionManagerAddressCache = new
|
|
19
|
-
initializedPositionManagersCache = new
|
|
20
|
-
jettonMasters = new
|
|
21
|
-
vaults = new
|
|
22
|
-
positionManagers = new
|
|
18
|
+
jettonWalletsAddressCache = new Cache();
|
|
19
|
+
positionManagerAddressCache = new Cache();
|
|
20
|
+
initializedPositionManagersCache = new Cache();
|
|
21
|
+
jettonMasters = new Cache();
|
|
22
|
+
vaults = new Cache();
|
|
23
|
+
positionManagers = new Cache();
|
|
24
|
+
lpWalletsAddressCache = new Cache();
|
|
23
25
|
constructor(stormClient, tonClient, traderAddress) {
|
|
24
26
|
this.stormClient = stormClient;
|
|
25
27
|
this.traderAddress =
|
|
@@ -32,8 +34,12 @@ class StormTradingSdk {
|
|
|
32
34
|
this.stormClient.config.fetchAssetsConfig(),
|
|
33
35
|
]);
|
|
34
36
|
}
|
|
35
|
-
async getPositionManagerAddressByAssets(
|
|
36
|
-
const
|
|
37
|
+
async getPositionManagerAddressByAssets(opts) {
|
|
38
|
+
const { baseAssetName, collateralAssetName } = opts;
|
|
39
|
+
const positionAddressByAssetNameAndBaseAsset = this.positionManagerAddressCache.get([
|
|
40
|
+
baseAssetName,
|
|
41
|
+
collateralAssetName,
|
|
42
|
+
]);
|
|
37
43
|
if (positionAddressByAssetNameAndBaseAsset) {
|
|
38
44
|
return positionAddressByAssetNameAndBaseAsset;
|
|
39
45
|
}
|
|
@@ -49,127 +55,142 @@ class StormTradingSdk {
|
|
|
49
55
|
const positionManagerContract = this.getPositionManagerContract(positionManagerAddress.toRawString());
|
|
50
56
|
return positionManagerContract.getData();
|
|
51
57
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const
|
|
55
|
-
const positionManagerAddress = await this.getPositionManagerAddressByAssets(
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
58
|
+
// Market open
|
|
59
|
+
async prefetchCreateMarketOpenOrder(opts) {
|
|
60
|
+
const { collateralAssetName } = opts;
|
|
61
|
+
const positionManagerAddress = await this.getPositionManagerAddressByAssets(opts);
|
|
62
|
+
await this.checkIsPositionManagerInitialized(positionManagerAddress);
|
|
63
|
+
if (!this.isNativeVault(collateralAssetName)) {
|
|
64
|
+
await this.getJettonWalletAddressByAssetName(collateralAssetName);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
syncCreateMarketOpenOrder(opts) {
|
|
68
|
+
const positionManagerAddress = this.positionManagerAddressCache.getOrThrow([
|
|
69
|
+
opts.baseAssetName,
|
|
70
|
+
opts.collateralAssetName,
|
|
71
|
+
]);
|
|
72
|
+
const vault = this.stormClient.config.requireVaultConfigByAssetName(opts.collateralAssetName);
|
|
73
|
+
const vaultAddress = Address.parse(vault.vaultAddress);
|
|
74
|
+
const baseParams = {
|
|
75
|
+
traderAddress: this.traderAddress,
|
|
76
|
+
vaultAddress,
|
|
61
77
|
amount: opts.amount,
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
78
|
+
};
|
|
79
|
+
const initPositionManager = !this.initializedPositionManagersCache.get(positionManagerAddress.toRawString());
|
|
80
|
+
const orderParams = {
|
|
81
|
+
...opts,
|
|
65
82
|
limitPrice: 0n,
|
|
83
|
+
assetId: this.stormClient.config.requireAssetIndexByName(opts.baseAssetName),
|
|
84
|
+
initPositionManager,
|
|
85
|
+
expiration: marketOpenDefaultExpiration(),
|
|
66
86
|
minBaseAssetAmount: opts.minBaseAssetAmount ?? 0n,
|
|
67
87
|
stopTriggerPrice: opts.stopTriggerPrice ?? 0n,
|
|
68
88
|
takeTriggerPrice: opts.takeTriggerPrice ?? 0n,
|
|
69
89
|
};
|
|
70
|
-
|
|
71
|
-
traderAddress: this.traderAddress,
|
|
72
|
-
amount: opts.amount,
|
|
73
|
-
positionManagerAddress,
|
|
74
|
-
vaultAddress: Address.parse(vault.vaultAddress),
|
|
75
|
-
orderParams,
|
|
76
|
-
};
|
|
77
|
-
if (this.isNativeVault(collateralAssetName)) {
|
|
90
|
+
if (this.isNativeVault(opts.collateralAssetName)) {
|
|
78
91
|
return createMarketOrderTx({
|
|
92
|
+
orderParams,
|
|
79
93
|
...baseParams,
|
|
80
94
|
vaultType: 'native',
|
|
81
95
|
});
|
|
82
96
|
}
|
|
83
97
|
else {
|
|
84
|
-
const traderJettonWalletAddress =
|
|
98
|
+
const traderJettonWalletAddress = this.jettonWalletsAddressCache.getOrThrow(opts.collateralAssetName);
|
|
85
99
|
return createMarketOrderTx({
|
|
100
|
+
orderParams,
|
|
86
101
|
...baseParams,
|
|
87
102
|
vaultType: 'jetton',
|
|
88
103
|
traderJettonWalletAddress,
|
|
89
104
|
});
|
|
90
105
|
}
|
|
91
106
|
}
|
|
92
|
-
async
|
|
93
|
-
|
|
107
|
+
async createMarketOpenOrder(opts) {
|
|
108
|
+
await this.prefetchCreateMarketOpenOrder(opts);
|
|
109
|
+
return this.syncCreateMarketOpenOrder(opts);
|
|
110
|
+
}
|
|
111
|
+
// Market close
|
|
112
|
+
prefetchCreateClosePositionOrder = this.prefetchSLTPOrderCaches;
|
|
113
|
+
syncCreateClosePositionOrder(opts) {
|
|
114
|
+
return this.syncCreateTakeProfitOrder({
|
|
94
115
|
...opts,
|
|
95
116
|
amount: opts.size,
|
|
96
117
|
triggerPrice: 0n,
|
|
97
118
|
});
|
|
98
119
|
}
|
|
99
|
-
async
|
|
100
|
-
return this.
|
|
120
|
+
async createClosePositionOrder(opts) {
|
|
121
|
+
return this.createTakeProfitOrder({
|
|
101
122
|
...opts,
|
|
102
|
-
|
|
123
|
+
amount: opts.size,
|
|
124
|
+
triggerPrice: 0n,
|
|
103
125
|
});
|
|
104
126
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
});
|
|
127
|
+
// Limit order
|
|
128
|
+
prefetchCreateLimitOrder = this.prefetchCreateMarketOpenOrder;
|
|
129
|
+
syncCreateLimitOrder(opts) {
|
|
130
|
+
return this.syncInternalCreateLimitOrder({ ...opts, stopPrice: 0n });
|
|
110
131
|
}
|
|
111
132
|
async createLimitOrder(opts) {
|
|
112
133
|
return this.internalCreateLimitOrder({ ...opts, stopPrice: 0n });
|
|
113
134
|
}
|
|
135
|
+
// Stop limit order
|
|
136
|
+
prefetchCreateStopLimitOrder = this.prefetchCreateMarketOpenOrder;
|
|
137
|
+
syncCreateStopLimitOrder(opts) {
|
|
138
|
+
return this.syncInternalCreateLimitOrder(opts);
|
|
139
|
+
}
|
|
114
140
|
async createStopLimitOrder(opts) {
|
|
115
141
|
return this.internalCreateLimitOrder(opts);
|
|
116
142
|
}
|
|
143
|
+
// Stop market order
|
|
144
|
+
prefetchCreateStopMarketOrder = this.prefetchCreateMarketOpenOrder;
|
|
145
|
+
syncCreateStopMarketOrder(opts) {
|
|
146
|
+
return this.syncInternalCreateLimitOrder({ ...opts, limitPrice: 0n });
|
|
147
|
+
}
|
|
117
148
|
async createStopMarketOrder(opts) {
|
|
118
149
|
return this.internalCreateLimitOrder({ ...opts, limitPrice: 0n });
|
|
119
150
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
orderIndex: opts.orderIndex,
|
|
127
|
-
direction: opts.direction,
|
|
128
|
-
},
|
|
151
|
+
// Stop loss order
|
|
152
|
+
prefetchCreteStopLossOrder = this.prefetchSLTPOrderCaches;
|
|
153
|
+
syncCreateStopLossOrder(opts) {
|
|
154
|
+
return this.syncCreateSLTPOrder({
|
|
155
|
+
...opts,
|
|
156
|
+
type: OrderType.stopLoss,
|
|
129
157
|
});
|
|
130
158
|
}
|
|
131
|
-
async
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
amount: opts.amount,
|
|
137
|
-
vaultAddress: Address.parse(vault.vaultAddress),
|
|
138
|
-
};
|
|
139
|
-
if (this.isNativeVault(assetName)) {
|
|
140
|
-
return createProvideLiquidityTx({ vaultType: 'native', ...baseParams });
|
|
141
|
-
}
|
|
142
|
-
else {
|
|
143
|
-
const traderJettonWalletAddress = await this.getJettonWalletAddressByAssetName(assetName);
|
|
144
|
-
return createProvideLiquidityTx({
|
|
145
|
-
vaultType: 'jetton',
|
|
146
|
-
traderJettonWalletAddress,
|
|
147
|
-
...baseParams,
|
|
148
|
-
});
|
|
149
|
-
}
|
|
159
|
+
async createStopLossOrder(opts) {
|
|
160
|
+
return this.createSLTPOrder({
|
|
161
|
+
...opts,
|
|
162
|
+
type: OrderType.stopLoss,
|
|
163
|
+
});
|
|
150
164
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
lpWalletAddress,
|
|
158
|
-
amount: opts.amountOfSLP,
|
|
159
|
-
userAddress: this.traderAddress,
|
|
165
|
+
// Take profit order
|
|
166
|
+
prefetchCreteTakeProfitOrder = this.prefetchSLTPOrderCaches;
|
|
167
|
+
syncCreateTakeProfitOrder(opts) {
|
|
168
|
+
return this.syncCreateSLTPOrder({
|
|
169
|
+
...opts,
|
|
170
|
+
type: OrderType.takeProfit,
|
|
160
171
|
});
|
|
161
172
|
}
|
|
162
|
-
async
|
|
163
|
-
|
|
173
|
+
async createTakeProfitOrder(opts) {
|
|
174
|
+
return this.createSLTPOrder({
|
|
175
|
+
...opts,
|
|
176
|
+
type: OrderType.takeProfit,
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
// Add margin
|
|
180
|
+
async prefetchAddMarginCaches(opts) {
|
|
181
|
+
if (!this.isNativeVault(opts.collateralAssetName)) {
|
|
182
|
+
await this.getJettonWalletAddressByAssetName(opts.collateralAssetName);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
syncAddMargin(opts) {
|
|
186
|
+
const { collateralAssetName } = opts;
|
|
187
|
+
const vault = this.stormClient.config.requireVaultConfigByAssetName(collateralAssetName);
|
|
164
188
|
const assetId = this.stormClient.config.requireAssetIndexByName(opts.baseAssetName);
|
|
165
|
-
const oraclePayload = opts.oraclePayload
|
|
166
|
-
? opts.oraclePayload
|
|
167
|
-
: await this.getOraclePayloadByAssets(opts.baseAssetName, opts.collateralAssetName);
|
|
168
189
|
const orderParams = {
|
|
169
190
|
amount: opts.amount,
|
|
170
191
|
direction: opts.direction,
|
|
171
192
|
assetId,
|
|
172
|
-
oracle: oraclePayload,
|
|
193
|
+
oracle: opts.oraclePayload,
|
|
173
194
|
};
|
|
174
195
|
const baseParams = {
|
|
175
196
|
traderAddress: this.traderAddress,
|
|
@@ -177,11 +198,11 @@ class StormTradingSdk {
|
|
|
177
198
|
vaultAddress: Address.parse(vault.vaultAddress),
|
|
178
199
|
orderParams,
|
|
179
200
|
};
|
|
180
|
-
if (this.isNativeVault(
|
|
201
|
+
if (this.isNativeVault(collateralAssetName)) {
|
|
181
202
|
return createAddMarginTx({ vaultType: 'native', ...baseParams });
|
|
182
203
|
}
|
|
183
204
|
else {
|
|
184
|
-
const traderJettonWalletAddress =
|
|
205
|
+
const traderJettonWalletAddress = this.jettonWalletsAddressCache.getOrThrow(collateralAssetName);
|
|
185
206
|
return createAddMarginTx({
|
|
186
207
|
vaultType: 'jetton',
|
|
187
208
|
traderJettonWalletAddress,
|
|
@@ -189,17 +210,26 @@ class StormTradingSdk {
|
|
|
189
210
|
});
|
|
190
211
|
}
|
|
191
212
|
}
|
|
192
|
-
async
|
|
213
|
+
async addMargin(opts) {
|
|
214
|
+
await this.prefetchAddMarginCaches(opts);
|
|
215
|
+
const oraclePayload = await this.getOraclePayloadByAssets(opts);
|
|
216
|
+
return this.syncAddMargin({ ...opts, oraclePayload });
|
|
217
|
+
}
|
|
218
|
+
// Remove margin
|
|
219
|
+
async prefetchRemoveMarginCaches(opts) {
|
|
220
|
+
await this.getPositionManagerAddressByAssets(opts);
|
|
221
|
+
}
|
|
222
|
+
syncRemoveMargin(opts) {
|
|
193
223
|
const assetId = this.stormClient.config.requireAssetIndexByName(opts.baseAssetName);
|
|
194
|
-
const
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
224
|
+
const positionManagerAddress = this.positionManagerAddressCache.getOrThrow([
|
|
225
|
+
opts.baseAssetName,
|
|
226
|
+
opts.collateralAssetName,
|
|
227
|
+
]);
|
|
198
228
|
const orderParams = {
|
|
199
229
|
assetId,
|
|
200
230
|
amount: opts.amount,
|
|
201
231
|
direction: opts.direction,
|
|
202
|
-
oracle: oraclePayload,
|
|
232
|
+
oracle: opts.oraclePayload,
|
|
203
233
|
};
|
|
204
234
|
const baseParams = {
|
|
205
235
|
traderAddress: this.traderAddress,
|
|
@@ -209,6 +239,87 @@ class StormTradingSdk {
|
|
|
209
239
|
};
|
|
210
240
|
return createRemoveMarginTx({ ...baseParams });
|
|
211
241
|
}
|
|
242
|
+
async removeMargin(opts) {
|
|
243
|
+
await this.prefetchRemoveMarginCaches(opts);
|
|
244
|
+
const oraclePayload = await this.getOraclePayloadByAssets(opts);
|
|
245
|
+
return this.syncRemoveMargin({ ...opts, oraclePayload });
|
|
246
|
+
}
|
|
247
|
+
// Cancel order
|
|
248
|
+
async prefetchCancelOrderCaches(opts) {
|
|
249
|
+
await this.getPositionManagerAddressByAssets(opts);
|
|
250
|
+
}
|
|
251
|
+
syncCreateCancelOrder(opts) {
|
|
252
|
+
const positionManagerAddress = this.positionManagerAddressCache.getOrThrow([
|
|
253
|
+
opts.baseAssetName,
|
|
254
|
+
opts.collateralAssetName,
|
|
255
|
+
]);
|
|
256
|
+
return createCancelOrderTx({
|
|
257
|
+
positionManagerAddress,
|
|
258
|
+
orderParams: {
|
|
259
|
+
orderType: opts.orderType,
|
|
260
|
+
orderIndex: opts.orderIndex,
|
|
261
|
+
direction: opts.direction,
|
|
262
|
+
},
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
async cancelOrder(opts) {
|
|
266
|
+
await this.prefetchCancelOrderCaches(opts);
|
|
267
|
+
return this.syncCreateCancelOrder(opts);
|
|
268
|
+
}
|
|
269
|
+
// Provide liquidity
|
|
270
|
+
async prefetchProvideLiquidity(opts) {
|
|
271
|
+
if (!this.isNativeVault(opts.assetName)) {
|
|
272
|
+
await this.getJettonWalletAddressByAssetName(opts.assetName);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
syncProvideLiquidity(opts) {
|
|
276
|
+
const { assetName } = opts;
|
|
277
|
+
const vault = this.stormClient.config.requireVaultConfigByAssetName(assetName);
|
|
278
|
+
const baseParams = {
|
|
279
|
+
traderAddress: this.traderAddress,
|
|
280
|
+
amount: opts.amount,
|
|
281
|
+
vaultAddress: Address.parse(vault.vaultAddress),
|
|
282
|
+
};
|
|
283
|
+
if (this.isNativeVault(assetName)) {
|
|
284
|
+
return createProvideLiquidityTx({ vaultType: 'native', ...baseParams });
|
|
285
|
+
}
|
|
286
|
+
else {
|
|
287
|
+
const traderJettonWalletAddress = this.jettonWalletsAddressCache.getOrThrow(opts.assetName);
|
|
288
|
+
return createProvideLiquidityTx({
|
|
289
|
+
vaultType: 'jetton',
|
|
290
|
+
traderJettonWalletAddress,
|
|
291
|
+
...baseParams,
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
async provideLiquidity(opts) {
|
|
296
|
+
await this.prefetchProvideLiquidity(opts);
|
|
297
|
+
return this.syncProvideLiquidity(opts);
|
|
298
|
+
}
|
|
299
|
+
// Withdraw liquidity
|
|
300
|
+
async prefetchWithdrawLiquidityCaches(assetName) {
|
|
301
|
+
if (this.lpWalletsAddressCache.get(assetName)) {
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
const vault = this.stormClient.config.requireVaultConfigByAssetName(assetName);
|
|
305
|
+
const lpJettonMasterContract = this.getJettonMasterContract(vault.lpJettonMaster);
|
|
306
|
+
const lpWalletAddress = await lpJettonMasterContract.getJettonWalletAddress(this.traderAddress);
|
|
307
|
+
this.lpWalletsAddressCache.set(assetName, lpWalletAddress);
|
|
308
|
+
}
|
|
309
|
+
syncWithdrawLiquidity(opts) {
|
|
310
|
+
const lpWalletAddress = this.lpWalletsAddressCache.getOrThrow(opts.assetName);
|
|
311
|
+
return createWithdrawLiquidityTx({
|
|
312
|
+
lpWalletAddress,
|
|
313
|
+
amount: opts.amountOfSLP,
|
|
314
|
+
userAddress: this.traderAddress,
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
async withdrawLiquidity(opts) {
|
|
318
|
+
const { assetName } = opts;
|
|
319
|
+
await this.prefetchWithdrawLiquidityCaches(assetName);
|
|
320
|
+
return this.syncWithdrawLiquidity(opts);
|
|
321
|
+
}
|
|
322
|
+
//
|
|
212
323
|
async getJettonWalletAddressByAssetName(collateralAssetName) {
|
|
213
324
|
let jettonWalletAddress = this.jettonWalletsAddressCache.get(collateralAssetName);
|
|
214
325
|
if (jettonWalletAddress) {
|
|
@@ -224,13 +335,13 @@ class StormTradingSdk {
|
|
|
224
335
|
return collateralAssetName === 'TON';
|
|
225
336
|
}
|
|
226
337
|
async checkIsPositionManagerInitialized(positionManagerAddress) {
|
|
227
|
-
if (this.initializedPositionManagersCache.
|
|
338
|
+
if (this.initializedPositionManagersCache.get(positionManagerAddress.toRawString())) {
|
|
228
339
|
return true;
|
|
229
340
|
}
|
|
230
341
|
try {
|
|
231
342
|
const positionManagerData = await this.getPositionManagerData(positionManagerAddress);
|
|
232
343
|
if (!(positionManagerData === null || positionManagerData.referralData === null)) {
|
|
233
|
-
this.initializedPositionManagersCache.
|
|
344
|
+
this.initializedPositionManagersCache.set(positionManagerAddress.toRawString(), true);
|
|
234
345
|
return true;
|
|
235
346
|
}
|
|
236
347
|
else {
|
|
@@ -241,11 +352,15 @@ class StormTradingSdk {
|
|
|
241
352
|
return false;
|
|
242
353
|
}
|
|
243
354
|
}
|
|
244
|
-
async getOraclePayloadByAssets(
|
|
355
|
+
async getOraclePayloadByAssets(opts) {
|
|
356
|
+
const { baseAssetName, collateralAssetName } = opts;
|
|
245
357
|
const oraclePriceResp = await this.stormClient.oracleClient.getPrice(baseAssetName);
|
|
246
358
|
const oraclePayload = { ...oraclePriceResp.result_message };
|
|
247
359
|
if (collateralAssetName === 'USDT') {
|
|
248
|
-
return {
|
|
360
|
+
return {
|
|
361
|
+
...oraclePayload,
|
|
362
|
+
oraclePayloadKind: 'simple',
|
|
363
|
+
};
|
|
249
364
|
}
|
|
250
365
|
const { priceRef, signaturesRef } = oraclePayload;
|
|
251
366
|
if (collateralAssetName === baseAssetName) {
|
|
@@ -266,8 +381,14 @@ class StormTradingSdk {
|
|
|
266
381
|
};
|
|
267
382
|
}
|
|
268
383
|
async createSLTPOrder(opts) {
|
|
269
|
-
|
|
270
|
-
|
|
384
|
+
await this.prefetchSLTPOrderCaches(opts);
|
|
385
|
+
return this.syncCreateSLTPOrder(opts);
|
|
386
|
+
}
|
|
387
|
+
syncCreateSLTPOrder(opts) {
|
|
388
|
+
const positionManagerAddress = this.positionManagerAddressCache.getOrThrow([
|
|
389
|
+
opts.baseAssetName,
|
|
390
|
+
opts.collateralAssetName,
|
|
391
|
+
]);
|
|
271
392
|
return createSLTPOrderTx({
|
|
272
393
|
positionManagerAddress,
|
|
273
394
|
orderParams: {
|
|
@@ -279,15 +400,24 @@ class StormTradingSdk {
|
|
|
279
400
|
},
|
|
280
401
|
});
|
|
281
402
|
}
|
|
403
|
+
async prefetchSLTPOrderCaches(opts) {
|
|
404
|
+
await this.getPositionManagerAddressByAssets(opts);
|
|
405
|
+
}
|
|
282
406
|
async internalCreateLimitOrder(opts) {
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
const
|
|
407
|
+
await this.prefetchCreateMarketOpenOrder(opts);
|
|
408
|
+
return this.syncInternalCreateLimitOrder(opts);
|
|
409
|
+
}
|
|
410
|
+
syncInternalCreateLimitOrder(opts) {
|
|
411
|
+
const vault = this.stormClient.config.requireVaultConfigByAssetName(opts.collateralAssetName);
|
|
412
|
+
const assetId = this.stormClient.config.requireAssetIndexByName(opts.baseAssetName);
|
|
413
|
+
const positionManagerAddress = this.positionManagerAddressCache.getOrThrow([
|
|
414
|
+
opts.baseAssetName,
|
|
415
|
+
opts.collateralAssetName,
|
|
416
|
+
]);
|
|
417
|
+
const initPositionManager = !this.initializedPositionManagersCache.get(positionManagerAddress.toRawString());
|
|
288
418
|
const orderParams = {
|
|
289
419
|
assetId,
|
|
290
|
-
initPositionManager
|
|
420
|
+
initPositionManager,
|
|
291
421
|
amount: opts.amount,
|
|
292
422
|
leverage: opts.leverage,
|
|
293
423
|
stopPrice: opts.stopPrice,
|
|
@@ -296,6 +426,7 @@ class StormTradingSdk {
|
|
|
296
426
|
stopTriggerPrice: opts.stopTriggerPrice ?? 0n,
|
|
297
427
|
takeTriggerPrice: opts.takeTriggerPrice ?? 0n,
|
|
298
428
|
expiration: opts.expiration ?? limitDefaultExpiration(),
|
|
429
|
+
referralId: opts.referralId
|
|
299
430
|
};
|
|
300
431
|
const baseParams = {
|
|
301
432
|
traderAddress: this.traderAddress,
|
|
@@ -304,14 +435,14 @@ class StormTradingSdk {
|
|
|
304
435
|
positionManagerAddress,
|
|
305
436
|
vaultAddress: Address.parse(vault.vaultAddress),
|
|
306
437
|
};
|
|
307
|
-
if (this.isNativeVault(collateralAssetName)) {
|
|
438
|
+
if (this.isNativeVault(opts.collateralAssetName)) {
|
|
308
439
|
return createLimitOrderTx({
|
|
309
440
|
...baseParams,
|
|
310
441
|
vaultType: 'native',
|
|
311
442
|
});
|
|
312
443
|
}
|
|
313
444
|
else {
|
|
314
|
-
const traderJettonWalletAddress =
|
|
445
|
+
const traderJettonWalletAddress = this.jettonWalletsAddressCache.getOrThrow(opts.collateralAssetName);
|
|
315
446
|
return createLimitOrderTx({
|
|
316
447
|
...baseParams,
|
|
317
448
|
vaultType: 'jetton',
|