aftermath-ts-sdk 1.3.23-perps.21 → 1.3.23-wc-0.1
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/general/apiHelpers/transactionsApiHelpers.d.ts +0 -3
- package/dist/general/apiHelpers/transactionsApiHelpers.d.ts.map +1 -1
- package/dist/general/apiHelpers/transactionsApiHelpers.js +0 -12
- package/dist/general/providers/aftermath.d.ts +5 -0
- package/dist/general/providers/aftermath.d.ts.map +1 -1
- package/dist/general/providers/aftermath.js +6 -1
- package/dist/general/providers/aftermathApi.d.ts +5 -0
- package/dist/general/providers/aftermathApi.d.ts.map +1 -1
- package/dist/general/providers/aftermathApi.js +5 -0
- package/dist/general/types/castingTypes.d.ts +2 -2
- package/dist/general/types/castingTypes.d.ts.map +1 -1
- package/dist/general/types/configTypes.d.ts +10 -0
- package/dist/general/types/configTypes.d.ts.map +1 -1
- package/dist/general/types/generalTypes.d.ts +0 -4
- package/dist/general/types/generalTypes.d.ts.map +1 -1
- package/dist/general/types/moveErrorsInterface.d.ts +1 -1
- package/dist/general/types/moveErrorsInterface.d.ts.map +1 -1
- package/dist/general/utils/caller.d.ts +2 -17
- package/dist/general/utils/caller.d.ts.map +1 -1
- package/dist/general/utils/caller.js +2 -35
- package/dist/general/utils/casting.d.ts +5 -4
- package/dist/general/utils/casting.d.ts.map +1 -1
- package/dist/general/utils/casting.js +5 -4
- package/dist/general/utils/helpers.d.ts +0 -1
- package/dist/general/utils/helpers.d.ts.map +1 -1
- package/dist/general/utils/helpers.js +6 -35
- package/dist/general/wallet/wallet.d.ts +1 -1
- package/dist/general/wallet/wallet.d.ts.map +1 -1
- package/dist/packages/coin/coin.d.ts +1 -1
- package/dist/packages/coin/coin.d.ts.map +1 -1
- package/dist/packages/coin/coin.js +1 -3
- package/dist/packages/farms/api/farmsApiCasting.js +27 -27
- package/dist/packages/farms/farms.d.ts +1 -1
- package/dist/packages/farms/farms.d.ts.map +1 -1
- package/dist/packages/farms/farmsStakedPosition.d.ts +1 -1
- package/dist/packages/farms/farmsStakedPosition.d.ts.map +1 -1
- package/dist/packages/farms/farmsStakingPool.d.ts +1 -1
- package/dist/packages/farms/farmsStakingPool.d.ts.map +1 -1
- package/dist/packages/faucet/api/faucetApi.d.ts +17 -9
- package/dist/packages/faucet/api/faucetApi.d.ts.map +1 -1
- package/dist/packages/faucet/api/faucetApi.js +49 -44
- package/dist/packages/faucet/api/faucetApiCasting.d.ts.map +1 -1
- package/dist/packages/faucet/api/faucetApiCasting.js +7 -10
- package/dist/packages/faucet/api/faucetApiCastingTypes.d.ts +4 -1
- package/dist/packages/faucet/api/faucetApiCastingTypes.d.ts.map +1 -1
- package/dist/packages/faucet/faucet.d.ts +1 -1
- package/dist/packages/faucet/faucet.d.ts.map +1 -1
- package/dist/packages/faucet/faucet.js +1 -1
- package/dist/packages/faucet/faucetTypes.d.ts +3 -2
- package/dist/packages/faucet/faucetTypes.d.ts.map +1 -1
- package/dist/packages/leveragedStaking/api/leveragedStakingApiCasting.js +3 -3
- package/dist/packages/multisig/multisig.d.ts +1 -1
- package/dist/packages/multisig/multisig.d.ts.map +1 -1
- package/dist/packages/nftAmm/nftAmm.d.ts +1 -1
- package/dist/packages/nftAmm/nftAmm.d.ts.map +1 -1
- package/dist/packages/oracle/api/oracleApi.d.ts +21 -0
- package/dist/packages/oracle/api/oracleApi.d.ts.map +1 -0
- package/dist/packages/oracle/api/oracleApi.js +76 -0
- package/dist/packages/oracle/api/oracleApiCasting.d.ts +6 -0
- package/dist/packages/oracle/api/oracleApiCasting.d.ts.map +1 -0
- package/dist/packages/oracle/api/oracleApiCasting.js +25 -0
- package/dist/packages/oracle/oracle.d.ts +15 -0
- package/dist/packages/oracle/oracle.d.ts.map +1 -0
- package/dist/packages/oracle/oracle.js +46 -0
- package/dist/packages/oracle/oracleCastingTypes.d.ts +11 -0
- package/dist/packages/oracle/oracleCastingTypes.d.ts.map +1 -0
- package/dist/packages/oracle/oracleCastingTypes.js +2 -0
- package/dist/packages/oracle/oracleTypes.d.ts +10 -0
- package/dist/packages/oracle/oracleTypes.d.ts.map +1 -0
- package/dist/packages/oracle/oracleTypes.js +2 -0
- package/dist/packages/perpetuals/api/perpetualsApi.d.ts +203 -11
- package/dist/packages/perpetuals/api/perpetualsApi.d.ts.map +1 -1
- package/dist/packages/perpetuals/api/perpetualsApi.js +615 -924
- package/dist/packages/perpetuals/api/perpetualsApiCasting.d.ts +21 -10
- package/dist/packages/perpetuals/api/perpetualsApiCasting.d.ts.map +1 -1
- package/dist/packages/perpetuals/api/perpetualsApiCasting.js +238 -260
- package/dist/packages/perpetuals/index.d.ts +0 -1
- package/dist/packages/perpetuals/index.d.ts.map +1 -1
- package/dist/packages/perpetuals/index.js +0 -1
- package/dist/packages/perpetuals/perpetuals.d.ts +14 -83
- package/dist/packages/perpetuals/perpetuals.d.ts.map +1 -1
- package/dist/packages/perpetuals/perpetuals.js +59 -158
- package/dist/packages/perpetuals/perpetualsAccount.d.ts +86 -158
- package/dist/packages/perpetuals/perpetualsAccount.d.ts.map +1 -1
- package/dist/packages/perpetuals/perpetualsAccount.js +141 -481
- package/dist/packages/perpetuals/perpetualsCastingTypes.d.ts +149 -82
- package/dist/packages/perpetuals/perpetualsCastingTypes.d.ts.map +1 -1
- package/dist/packages/perpetuals/perpetualsMarket.d.ts +20 -55
- package/dist/packages/perpetuals/perpetualsMarket.d.ts.map +1 -1
- package/dist/packages/perpetuals/perpetualsMarket.js +57 -114
- package/dist/packages/perpetuals/perpetualsTypes.d.ts +280 -586
- package/dist/packages/perpetuals/perpetualsTypes.d.ts.map +1 -1
- package/dist/packages/perpetuals/perpetualsTypes.js +81 -17
- package/dist/packages/pools/api/poolsApiCasting.js +3 -3
- package/dist/packages/pools/pool.d.ts +1 -1
- package/dist/packages/pools/pool.d.ts.map +1 -1
- package/dist/packages/pools/pools.d.ts +1 -1
- package/dist/packages/pools/pools.d.ts.map +1 -1
- package/dist/packages/referralVault/referralVault.d.ts +1 -1
- package/dist/packages/referralVault/referralVault.d.ts.map +1 -1
- package/dist/packages/router/api/routerApiCasting.js +1 -1
- package/dist/packages/staking/api/stakingApiCasting.js +3 -3
- package/dist/packages/staking/staking.d.ts +1 -1
- package/dist/packages/staking/staking.d.ts.map +1 -1
- package/dist/packages/sui/sui.d.ts +1 -1
- package/dist/packages/sui/sui.d.ts.map +1 -1
- package/dist/packages/suiFrens/api/suiFrensApiCasting.js +4 -4
- package/dist/packages/suiFrens/stakedSuiFren.d.ts +1 -1
- package/dist/packages/suiFrens/stakedSuiFren.d.ts.map +1 -1
- package/dist/packages/suiFrens/suiFren.d.ts +1 -1
- package/dist/packages/suiFrens/suiFren.d.ts.map +1 -1
- package/dist/packages/suiFrens/suiFrens.d.ts +1 -1
- package/dist/packages/suiFrens/suiFrens.d.ts.map +1 -1
- package/dist/types.d.ts +1 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -0
- package/package.json +2 -2
- package/dist/packages/perpetuals/perpetualsVault.d.ts +0 -109
- package/dist/packages/perpetuals/perpetualsVault.d.ts.map +0 -1
- package/dist/packages/perpetuals/perpetualsVault.js +0 -290
|
@@ -23,39 +23,29 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
23
23
|
exports.PerpetualsAccount = void 0;
|
|
24
24
|
const caller_1 = require("../../general/utils/caller");
|
|
25
25
|
const types_1 = require("../../types");
|
|
26
|
+
const iFixedUtils_1 = require("../../general/utils/iFixedUtils");
|
|
26
27
|
const utils_1 = require("../../general/utils");
|
|
27
28
|
const perpetuals_1 = require("./perpetuals");
|
|
28
29
|
const __1 = require("..");
|
|
29
|
-
const transactions_1 = require("@mysten/sui/transactions");
|
|
30
|
-
// TODO: create refresh account positions function ?
|
|
31
30
|
class PerpetualsAccount extends caller_1.Caller {
|
|
32
31
|
// =========================================================================
|
|
33
32
|
// Constructor
|
|
34
33
|
// =========================================================================
|
|
35
|
-
constructor(account, accountCap, config
|
|
36
|
-
const vaultId = "vaultId" in accountCap ? accountCap.vaultId : undefined;
|
|
34
|
+
constructor(account, accountCap, config) {
|
|
37
35
|
super(config, "perpetuals");
|
|
38
36
|
this.account = account;
|
|
39
37
|
this.accountCap = accountCap;
|
|
40
|
-
this.Provider = Provider;
|
|
41
|
-
// public async getOwnedWithdrawRequests() {
|
|
42
|
-
// return new Perpetuals(
|
|
43
|
-
// this.config,
|
|
44
|
-
// this.Provider
|
|
45
|
-
// ).getOwnedWithdrawRequests({
|
|
46
|
-
// walletAddress: this.ownerAddress(),
|
|
47
|
-
// });
|
|
48
|
-
// }
|
|
49
38
|
// =========================================================================
|
|
50
39
|
// Calculations
|
|
51
40
|
// =========================================================================
|
|
52
41
|
this.calcFreeCollateralForPosition = (inputs) => {
|
|
53
42
|
var _a, _b;
|
|
54
43
|
const marketId = inputs.market.marketId;
|
|
55
|
-
const position = (_b = (_a = inputs.position) !== null && _a !== void 0 ? _a : this.positionForMarketId({ marketId })) !== null && _b !== void 0 ? _b : inputs.market
|
|
44
|
+
const position = (_b = (_a = inputs.position) !== null && _a !== void 0 ? _a : this.positionForMarketId({ marketId })) !== null && _b !== void 0 ? _b : this.emptyPosition({ market: inputs.market });
|
|
56
45
|
const funding = this.calcUnrealizedFundingsForPosition(inputs);
|
|
57
46
|
const { pnl, minInitialMargin } = this.calcPnLAndMarginForPosition(inputs);
|
|
58
|
-
let collateralUsd = position.collateral *
|
|
47
|
+
let collateralUsd = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.collateral) *
|
|
48
|
+
inputs.collateralPrice;
|
|
59
49
|
collateralUsd += funding;
|
|
60
50
|
let cappedMargin;
|
|
61
51
|
if (pnl < 0) {
|
|
@@ -74,12 +64,14 @@ class PerpetualsAccount extends caller_1.Caller {
|
|
|
74
64
|
var _a, _b;
|
|
75
65
|
const { market, indexPrice, collateralPrice } = inputs;
|
|
76
66
|
const marketId = market.marketId;
|
|
77
|
-
const position = (_b = (_a = inputs.position) !== null && _a !== void 0 ? _a : this.positionForMarketId({ marketId })) !== null && _b !== void 0 ? _b :
|
|
67
|
+
const position = (_b = (_a = inputs.position) !== null && _a !== void 0 ? _a : this.positionForMarketId({ marketId })) !== null && _b !== void 0 ? _b : this.emptyPosition({ market });
|
|
78
68
|
const funding = this.calcUnrealizedFundingsForPosition({
|
|
79
69
|
market,
|
|
80
70
|
position,
|
|
81
71
|
});
|
|
82
|
-
const collateralUsd = (position === null || position === void 0 ? void 0 : position.collateral) *
|
|
72
|
+
const collateralUsd = iFixedUtils_1.IFixedUtils.numberFromIFixed(position === null || position === void 0 ? void 0 : position.collateral) *
|
|
73
|
+
collateralPrice +
|
|
74
|
+
funding;
|
|
83
75
|
const { pnl, netAbsBaseValue } = this.calcPnLAndMarginForPosition({
|
|
84
76
|
market,
|
|
85
77
|
indexPrice,
|
|
@@ -104,31 +96,31 @@ class PerpetualsAccount extends caller_1.Caller {
|
|
|
104
96
|
this.calcUnrealizedFundingsForPosition = (inputs) => {
|
|
105
97
|
var _a, _b;
|
|
106
98
|
const marketId = inputs.market.marketId;
|
|
107
|
-
const position = (_b = (_a = inputs.position) !== null && _a !== void 0 ? _a : this.positionForMarketId({ marketId })) !== null && _b !== void 0 ? _b : inputs.market
|
|
108
|
-
const baseAmount = position.baseAssetAmount;
|
|
99
|
+
const position = (_b = (_a = inputs.position) !== null && _a !== void 0 ? _a : this.positionForMarketId({ marketId })) !== null && _b !== void 0 ? _b : this.emptyPosition({ market: inputs.market });
|
|
100
|
+
const baseAmount = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.baseAssetAmount);
|
|
109
101
|
const isLong = Math.sign(baseAmount);
|
|
110
102
|
if (isLong < 0) {
|
|
111
|
-
const fundingShort = position.cumFundingRateShort;
|
|
112
|
-
const marketFundingShort = inputs.market.marketState.cumFundingRateShort;
|
|
103
|
+
const fundingShort = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.cumFundingRateShort);
|
|
104
|
+
const marketFundingShort = iFixedUtils_1.IFixedUtils.numberFromIFixed(inputs.market.marketState.cumFundingRateShort);
|
|
113
105
|
return -baseAmount * (marketFundingShort - fundingShort);
|
|
114
106
|
}
|
|
115
107
|
else {
|
|
116
|
-
const fundingLong = position.cumFundingRateLong;
|
|
117
|
-
const marketFundingLong = inputs.market.marketState.cumFundingRateLong;
|
|
108
|
+
const fundingLong = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.cumFundingRateLong);
|
|
109
|
+
const marketFundingLong = iFixedUtils_1.IFixedUtils.numberFromIFixed(inputs.market.marketState.cumFundingRateLong);
|
|
118
110
|
return -baseAmount * (marketFundingLong - fundingLong);
|
|
119
111
|
}
|
|
120
112
|
};
|
|
121
113
|
this.calcPnLAndMarginForPosition = (inputs) => {
|
|
122
114
|
var _a, _b;
|
|
123
115
|
const marketId = inputs.market.marketId;
|
|
124
|
-
const position = (_b = (_a = inputs.position) !== null && _a !== void 0 ? _a : this.positionForMarketId({ marketId })) !== null && _b !== void 0 ? _b : inputs.market
|
|
125
|
-
const marginRatioInitial = 1 /
|
|
116
|
+
const position = (_b = (_a = inputs.position) !== null && _a !== void 0 ? _a : this.positionForMarketId({ marketId })) !== null && _b !== void 0 ? _b : this.emptyPosition({ market: inputs.market });
|
|
117
|
+
const marginRatioInitial = 1 / position.leverage;
|
|
126
118
|
// const marginRatioInitial = inputs.market.initialMarginRatio();
|
|
127
|
-
const marginRatioMaintenance = inputs.market.marketParams.marginRatioMaintenance;
|
|
128
|
-
const baseAssetAmount = position.baseAssetAmount;
|
|
129
|
-
const quoteAssetAmount = position.quoteAssetNotionalAmount;
|
|
130
|
-
const bidsQuantity = position.bidsQuantity;
|
|
131
|
-
const asksQuantity = position.asksQuantity;
|
|
119
|
+
const marginRatioMaintenance = iFixedUtils_1.IFixedUtils.numberFromIFixed(inputs.market.marketParams.marginRatioMaintenance);
|
|
120
|
+
const baseAssetAmount = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.baseAssetAmount);
|
|
121
|
+
const quoteAssetAmount = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.quoteAssetNotionalAmount);
|
|
122
|
+
const bidsQuantity = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.bidsQuantity);
|
|
123
|
+
const asksQuantity = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.asksQuantity);
|
|
132
124
|
const pnl = baseAssetAmount * inputs.indexPrice - quoteAssetAmount;
|
|
133
125
|
const netAbs = Math.max(Math.abs(baseAssetAmount + bidsQuantity), Math.abs(baseAssetAmount - asksQuantity));
|
|
134
126
|
const netAbsBaseValue = netAbs * inputs.indexPrice;
|
|
@@ -139,30 +131,33 @@ class PerpetualsAccount extends caller_1.Caller {
|
|
|
139
131
|
this.calcLiquidationPriceForPosition = (inputs) => {
|
|
140
132
|
var _a, _b;
|
|
141
133
|
const marketId = inputs.market.marketId;
|
|
142
|
-
const position = (_b = (_a = inputs.position) !== null && _a !== void 0 ? _a : this.positionForMarketId({ marketId })) !== null && _b !== void 0 ? _b : inputs.market
|
|
134
|
+
const position = (_b = (_a = inputs.position) !== null && _a !== void 0 ? _a : this.positionForMarketId({ marketId })) !== null && _b !== void 0 ? _b : this.emptyPosition({ market: inputs.market });
|
|
143
135
|
const funding = this.calcUnrealizedFundingsForPosition(inputs);
|
|
144
|
-
const
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
const MMR = inputs.market.marketParams.marginRatioMaintenance;
|
|
150
|
-
const
|
|
151
|
-
const
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
136
|
+
const collateralUsd = iFixedUtils_1.IFixedUtils.numberFromIFixed(position === null || position === void 0 ? void 0 : position.collateral) *
|
|
137
|
+
inputs.collateralPrice +
|
|
138
|
+
funding;
|
|
139
|
+
const baseAssetAmount = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.baseAssetAmount);
|
|
140
|
+
const quoteAssetAmount = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.quoteAssetNotionalAmount);
|
|
141
|
+
const MMR = iFixedUtils_1.IFixedUtils.numberFromIFixed(inputs.market.marketParams.marginRatioMaintenance);
|
|
142
|
+
const numerator = collateralUsd - quoteAssetAmount;
|
|
143
|
+
const price = (() => {
|
|
144
|
+
if (baseAssetAmount > 0) {
|
|
145
|
+
return numerator / ((1 - MMR) * -baseAssetAmount);
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
return numerator / ((1 + MMR) * -baseAssetAmount);
|
|
149
|
+
}
|
|
150
|
+
})();
|
|
151
|
+
return price < 0 ? 0 : price;
|
|
158
152
|
};
|
|
159
153
|
this.calcFreeMarginUsdForPosition = (inputs) => {
|
|
160
154
|
var _a, _b;
|
|
161
155
|
const marketId = inputs.market.marketId;
|
|
162
|
-
const position = (_b = (_a = inputs.position) !== null && _a !== void 0 ? _a : this.positionForMarketId({ marketId })) !== null && _b !== void 0 ? _b : inputs.market
|
|
156
|
+
const position = (_b = (_a = inputs.position) !== null && _a !== void 0 ? _a : this.positionForMarketId({ marketId })) !== null && _b !== void 0 ? _b : this.emptyPosition({ market: inputs.market });
|
|
163
157
|
const totalFunding = this.calcUnrealizedFundingsForPosition(inputs);
|
|
164
158
|
const { pnl, minInitialMargin } = this.calcPnLAndMarginForPosition(inputs);
|
|
165
|
-
let collateralUsd = position.collateral *
|
|
159
|
+
let collateralUsd = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.collateral) *
|
|
160
|
+
inputs.collateralPrice;
|
|
166
161
|
const margin = collateralUsd + totalFunding + pnl;
|
|
167
162
|
if (margin >= minInitialMargin) {
|
|
168
163
|
return margin - minInitialMargin;
|
|
@@ -179,7 +174,7 @@ class PerpetualsAccount extends caller_1.Caller {
|
|
|
179
174
|
zipped.forEach(([market, indexPrice]) => {
|
|
180
175
|
var _a;
|
|
181
176
|
const marketId = market.marketId;
|
|
182
|
-
const position = (_a = this.positionForMarketId({ marketId })) !== null && _a !== void 0 ? _a :
|
|
177
|
+
const position = (_a = this.positionForMarketId({ marketId })) !== null && _a !== void 0 ? _a : this.emptyPosition({ market });
|
|
183
178
|
const funding = this.calcUnrealizedFundingsForPosition({
|
|
184
179
|
market,
|
|
185
180
|
position,
|
|
@@ -189,7 +184,8 @@ class PerpetualsAccount extends caller_1.Caller {
|
|
|
189
184
|
indexPrice,
|
|
190
185
|
position,
|
|
191
186
|
});
|
|
192
|
-
let collateralUsd = position.collateral *
|
|
187
|
+
let collateralUsd = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.collateral) *
|
|
188
|
+
inputs.collateralPrice;
|
|
193
189
|
totalPnL += pnl;
|
|
194
190
|
totalFunding += funding;
|
|
195
191
|
totalCollateralAllocated += collateralUsd;
|
|
@@ -204,9 +200,9 @@ class PerpetualsAccount extends caller_1.Caller {
|
|
|
204
200
|
};
|
|
205
201
|
this.closePositionTxInputs = (inputs) => {
|
|
206
202
|
var _a;
|
|
207
|
-
const { size, market, orderDatas, collateralPrice } = inputs;
|
|
203
|
+
const { size, market, walletAddress, orderDatas, collateralPrice } = inputs;
|
|
208
204
|
const marketId = market.marketId;
|
|
209
|
-
const position = (_a = this.positionForMarketId({ marketId })) !== null && _a !== void 0 ? _a :
|
|
205
|
+
const position = (_a = this.positionForMarketId({ marketId })) !== null && _a !== void 0 ? _a : this.emptyPosition({ market });
|
|
210
206
|
// TODO: move conversion to helper function, since used often
|
|
211
207
|
const ordersCollateral = __1.Coin.normalizeBalance(utils_1.Helpers.sum(orderDatas
|
|
212
208
|
.filter((orderData) => orderData.marketId === market.marketId)
|
|
@@ -216,25 +212,40 @@ class PerpetualsAccount extends caller_1.Caller {
|
|
|
216
212
|
(1 -
|
|
217
213
|
PerpetualsAccount.constants
|
|
218
214
|
.closePositionMarginOfError))), BigInt(0)) * BigInt(-1);
|
|
219
|
-
const positionSize = BigInt(Math.round(Math.abs(position.baseAssetAmount /
|
|
220
|
-
|
|
221
|
-
const collateralChange =
|
|
222
|
-
(Number(size) / Number(positionSize))))
|
|
215
|
+
const positionSize = BigInt(Math.round(Math.abs(utils_1.Casting.IFixed.numberFromIFixed(position.baseAssetAmount) /
|
|
216
|
+
market.lotSize())));
|
|
217
|
+
const collateralChange = BigInt(Math.round(Number(fullPositionCollateralChange) *
|
|
218
|
+
(Number(size) / Number(positionSize))));
|
|
223
219
|
const positionSide = perpetuals_1.Perpetuals.positionSide(position);
|
|
224
220
|
return {
|
|
225
221
|
size,
|
|
226
222
|
marketId,
|
|
223
|
+
walletAddress,
|
|
227
224
|
collateralChange,
|
|
228
|
-
// leverage: position.leverage || 1,
|
|
229
|
-
// leverage: undefined,
|
|
230
225
|
side: positionSide === types_1.PerpetualsOrderSide.Bid
|
|
231
226
|
? types_1.PerpetualsOrderSide.Ask
|
|
232
227
|
: types_1.PerpetualsOrderSide.Bid,
|
|
233
|
-
|
|
234
|
-
|
|
228
|
+
hasPosition: this.positionForMarketId({ marketId }) !== undefined,
|
|
229
|
+
};
|
|
230
|
+
};
|
|
231
|
+
this.emptyPosition = (inputs) => {
|
|
232
|
+
const { market } = inputs;
|
|
233
|
+
return {
|
|
234
|
+
marketId: market.marketId,
|
|
235
|
+
collateralCoinType: this.accountCap.collateralCoinType,
|
|
236
|
+
collateral: BigInt(0),
|
|
237
|
+
baseAssetAmount: BigInt(0),
|
|
238
|
+
quoteAssetNotionalAmount: BigInt(0),
|
|
239
|
+
cumFundingRateLong: market.marketState.cumFundingRateLong,
|
|
240
|
+
cumFundingRateShort: market.marketState.cumFundingRateShort,
|
|
241
|
+
asksQuantity: BigInt(0),
|
|
242
|
+
bidsQuantity: BigInt(0),
|
|
243
|
+
pendingOrders: [],
|
|
244
|
+
makerFee: BigInt(1000000000000000000), // 100%
|
|
245
|
+
takerFee: BigInt(1000000000000000000), // 100%
|
|
246
|
+
leverage: 1,
|
|
235
247
|
};
|
|
236
248
|
};
|
|
237
|
-
this.vaultId = vaultId;
|
|
238
249
|
}
|
|
239
250
|
// =========================================================================
|
|
240
251
|
// Transactions
|
|
@@ -244,98 +255,29 @@ class PerpetualsAccount extends caller_1.Caller {
|
|
|
244
255
|
// =========================================================================
|
|
245
256
|
getDepositCollateralTx(inputs) {
|
|
246
257
|
return __awaiter(this, void 0, void 0, function* () {
|
|
247
|
-
|
|
248
|
-
const { tx } = inputs, otherInputs = __rest(inputs, ["tx"]);
|
|
249
|
-
// TODO: add vault support
|
|
250
|
-
if (this.vaultId)
|
|
251
|
-
throw new Error("this function is not supported for vaults");
|
|
252
|
-
return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
|
|
253
|
-
"transactions/deposit-collateral", Object.assign(Object.assign(Object.assign(Object.assign({}, otherInputs), { walletAddress: this.ownerAddress(), collateralCoinType: this.accountCap.collateralCoinType }), ("vaultId" in this.accountCap
|
|
254
|
-
? {
|
|
255
|
-
vaultId: this.accountCap.vaultId,
|
|
256
|
-
}
|
|
257
|
-
: {
|
|
258
|
-
accountId: this.accountCap.accountId,
|
|
259
|
-
})), { txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({
|
|
260
|
-
tx: tx !== null && tx !== void 0 ? tx : new transactions_1.Transaction(),
|
|
261
|
-
})) }), undefined, {
|
|
262
|
-
txKind: true,
|
|
263
|
-
});
|
|
258
|
+
return this.fetchApiTransaction("transactions/deposit-collateral", Object.assign(Object.assign({}, inputs), { collateralCoinType: this.accountCap.collateralCoinType, accountCapId: this.accountCap.objectId }));
|
|
264
259
|
});
|
|
265
260
|
}
|
|
266
261
|
getWithdrawCollateralTx(inputs) {
|
|
267
262
|
return __awaiter(this, void 0, void 0, function* () {
|
|
268
|
-
|
|
269
|
-
const { withdrawAmount, recipientAddress, tx: txFromInputs } = inputs;
|
|
270
|
-
// TODO: add vault support
|
|
271
|
-
if (this.vaultId)
|
|
272
|
-
throw new Error("this function is not supported for vaults");
|
|
273
|
-
return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
|
|
274
|
-
"transactions/withdraw-collateral", Object.assign(Object.assign({ withdrawAmount,
|
|
275
|
-
recipientAddress, walletAddress: this.ownerAddress(), collateralCoinType: this.accountCap.collateralCoinType }, ("vaultId" in this.accountCap
|
|
276
|
-
? {
|
|
277
|
-
vaultId: this.accountCap.vaultId,
|
|
278
|
-
}
|
|
279
|
-
: {
|
|
280
|
-
accountId: this.accountCap.accountId,
|
|
281
|
-
})), { txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({
|
|
282
|
-
tx: txFromInputs !== null && txFromInputs !== void 0 ? txFromInputs : new transactions_1.Transaction(),
|
|
283
|
-
})) }), undefined, {
|
|
284
|
-
txKind: true,
|
|
285
|
-
});
|
|
263
|
+
return this.fetchApiTransaction("transactions/withdraw-collateral", Object.assign(Object.assign({}, inputs), { collateralCoinType: this.accountCap.collateralCoinType, accountCapId: this.accountCap.objectId }));
|
|
286
264
|
});
|
|
287
265
|
}
|
|
288
266
|
getAllocateCollateralTx(inputs) {
|
|
289
267
|
return __awaiter(this, void 0, void 0, function* () {
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
|
|
293
|
-
"transactions/allocate-collateral", Object.assign(Object.assign({ marketId,
|
|
294
|
-
allocateAmount }, ("vaultId" in this.accountCap
|
|
295
|
-
? {
|
|
296
|
-
vaultId: this.accountCap.vaultId,
|
|
297
|
-
}
|
|
298
|
-
: {
|
|
299
|
-
accountId: this.accountCap.accountId,
|
|
300
|
-
})), { walletAddress: this.ownerAddress(), txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx: tx !== null && tx !== void 0 ? tx : new transactions_1.Transaction() })) }), undefined, {
|
|
301
|
-
txKind: true,
|
|
302
|
-
});
|
|
268
|
+
const { market } = inputs;
|
|
269
|
+
return this.fetchApiTransaction("transactions/allocate-collateral", Object.assign(Object.assign({}, inputs), { packageId: market.marketData.packageId, marketInitialSharedVersion: market.marketData.initialSharedVersion, marketId: market.marketId, collateralCoinType: this.accountCap.collateralCoinType, accountCapId: this.accountCap.objectId }));
|
|
303
270
|
});
|
|
304
271
|
}
|
|
305
272
|
getDeallocateCollateralTx(inputs) {
|
|
306
273
|
return __awaiter(this, void 0, void 0, function* () {
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
|
|
310
|
-
"transactions/deallocate-collateral", Object.assign(Object.assign({ marketId,
|
|
311
|
-
deallocateAmount }, ("vaultId" in this.accountCap
|
|
312
|
-
? {
|
|
313
|
-
vaultId: this.accountCap.vaultId,
|
|
314
|
-
}
|
|
315
|
-
: {
|
|
316
|
-
accountId: this.accountCap.accountId,
|
|
317
|
-
})), { walletAddress: this.ownerAddress(), txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx: tx !== null && tx !== void 0 ? tx : new transactions_1.Transaction() })) }), undefined, {
|
|
318
|
-
txKind: true,
|
|
319
|
-
});
|
|
274
|
+
const { market } = inputs;
|
|
275
|
+
return this.fetchApiTransaction("transactions/deallocate-collateral", Object.assign(Object.assign({}, inputs), { packageId: market.marketData.packageId, marketInitialSharedVersion: market.marketData.initialSharedVersion, marketId: market.marketId, basePriceFeedId: market.marketParams.basePriceFeedId, collateralPriceFeedId: market.marketParams.collateralPriceFeedId, collateralCoinType: this.accountCap.collateralCoinType, accountCapId: this.accountCap.objectId }));
|
|
320
276
|
});
|
|
321
277
|
}
|
|
322
278
|
getTransferCollateralTx(inputs) {
|
|
323
279
|
return __awaiter(this, void 0, void 0, function* () {
|
|
324
|
-
|
|
325
|
-
const { transferAmount, toAccountId, tx } = inputs;
|
|
326
|
-
if ("vaultId" in this.accountCap)
|
|
327
|
-
throw new Error("`getTransferCollateralTx` not supported by vault accounts");
|
|
328
|
-
return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
|
|
329
|
-
"transactions/transfer-collateral", {
|
|
330
|
-
transferAmount,
|
|
331
|
-
toAccountId,
|
|
332
|
-
walletAddress: this.ownerAddress(),
|
|
333
|
-
collateralCoinType: this.accountCap.collateralCoinType,
|
|
334
|
-
fromAccountId: this.accountCap.accountId,
|
|
335
|
-
txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx: tx !== null && tx !== void 0 ? tx : new transactions_1.Transaction() })),
|
|
336
|
-
}, undefined, {
|
|
337
|
-
txKind: true,
|
|
338
|
-
});
|
|
280
|
+
return this.fetchApiTransaction("transactions/transfer-collateral", Object.assign(Object.assign({}, inputs), { collateralCoinType: this.accountCap.collateralCoinType, fromAccountCapId: this.accountCap.objectId }));
|
|
339
281
|
});
|
|
340
282
|
}
|
|
341
283
|
// =========================================================================
|
|
@@ -343,167 +285,33 @@ class PerpetualsAccount extends caller_1.Caller {
|
|
|
343
285
|
// =========================================================================
|
|
344
286
|
getPlaceMarketOrderTx(inputs) {
|
|
345
287
|
return __awaiter(this, void 0, void 0, function* () {
|
|
346
|
-
|
|
347
|
-
const { tx: txFromInputs, slTp } = inputs, otherInputs = __rest(inputs, ["tx", "slTp"]);
|
|
348
|
-
const tx = txFromInputs !== null && txFromInputs !== void 0 ? txFromInputs : new transactions_1.Transaction();
|
|
349
|
-
// tx.setSender(this.ownerAddress());
|
|
350
|
-
return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
|
|
351
|
-
"transactions/place-market-order", Object.assign(Object.assign(Object.assign(Object.assign({}, otherInputs), { slTp: slTp
|
|
352
|
-
? Object.assign({ walletAddress: this.ownerAddress() }, slTp) : undefined, txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx })), walletAddress: this.ownerAddress() }), ("vaultId" in this.accountCap
|
|
353
|
-
? {
|
|
354
|
-
vaultId: this.accountCap.vaultId,
|
|
355
|
-
}
|
|
356
|
-
: {
|
|
357
|
-
accountId: this.accountCap.accountId,
|
|
358
|
-
})), { hasPosition: this.positionForMarketId(otherInputs) !== undefined }), undefined, {
|
|
359
|
-
txKind: true,
|
|
360
|
-
});
|
|
288
|
+
return this.fetchApiTransaction("transactions/market-order", Object.assign(Object.assign({}, inputs), { accountObjectId: this.accountCap.objectId, accountObjectVersion: this.accountCap.objectVersion, accountObjectDigest: this.accountCap.objectDigest, hasPosition: this.positionForMarketId(inputs) !== undefined }));
|
|
361
289
|
});
|
|
362
290
|
}
|
|
363
291
|
getPlaceLimitOrderTx(inputs) {
|
|
364
292
|
return __awaiter(this, void 0, void 0, function* () {
|
|
365
|
-
|
|
366
|
-
const { tx: txFromInputs, slTp } = inputs, otherInputs = __rest(inputs, ["tx", "slTp"]);
|
|
367
|
-
const tx = txFromInputs !== null && txFromInputs !== void 0 ? txFromInputs : new transactions_1.Transaction();
|
|
368
|
-
// tx.setSender(this.ownerAddress());
|
|
369
|
-
return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
|
|
370
|
-
"transactions/place-limit-order", Object.assign(Object.assign(Object.assign(Object.assign({}, otherInputs), { slTp: slTp
|
|
371
|
-
? Object.assign({ walletAddress: this.ownerAddress() }, slTp) : undefined, txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx })), walletAddress: this.ownerAddress() }), ("vaultId" in this.accountCap
|
|
372
|
-
? {
|
|
373
|
-
vaultId: this.accountCap.vaultId,
|
|
374
|
-
}
|
|
375
|
-
: {
|
|
376
|
-
accountId: this.accountCap.accountId,
|
|
377
|
-
})), { hasPosition: this.positionForMarketId(otherInputs) !== undefined }), undefined, {
|
|
378
|
-
txKind: true,
|
|
379
|
-
});
|
|
380
|
-
});
|
|
381
|
-
}
|
|
382
|
-
getCancelOrdersTx(inputs) {
|
|
383
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
384
|
-
var _a;
|
|
385
|
-
const { tx } = inputs, otherInputs = __rest(inputs, ["tx"]);
|
|
386
|
-
return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
|
|
387
|
-
"transactions/cancel-orders", Object.assign(Object.assign(Object.assign({}, otherInputs), { txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx })), walletAddress: this.ownerAddress() }), ("vaultId" in this.accountCap
|
|
388
|
-
? {
|
|
389
|
-
vaultId: this.accountCap.vaultId,
|
|
390
|
-
}
|
|
391
|
-
: {
|
|
392
|
-
accountId: this.accountCap.accountId,
|
|
393
|
-
})), undefined, {
|
|
394
|
-
txKind: true,
|
|
395
|
-
});
|
|
293
|
+
return this.fetchApiTransaction("transactions/limit-order", Object.assign(Object.assign({}, inputs), { accountObjectId: this.accountCap.objectId, accountObjectVersion: this.accountCap.objectVersion, accountObjectDigest: this.accountCap.objectDigest, hasPosition: this.positionForMarketId(inputs) !== undefined }));
|
|
396
294
|
});
|
|
397
295
|
}
|
|
398
|
-
|
|
296
|
+
getPlaceSLTPOrder(inputs) {
|
|
399
297
|
return __awaiter(this, void 0, void 0, function* () {
|
|
400
|
-
|
|
401
|
-
const { tx } = inputs, otherInputs = __rest(inputs, ["tx"]);
|
|
402
|
-
return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
|
|
403
|
-
"transactions/cancel-stop-orders", Object.assign(Object.assign(Object.assign({}, otherInputs), { txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx })), walletAddress: this.ownerAddress() }), ("vaultId" in this.accountCap
|
|
404
|
-
? {
|
|
405
|
-
vaultId: this.accountCap.vaultId,
|
|
406
|
-
}
|
|
407
|
-
: {
|
|
408
|
-
accountId: this.accountCap.accountId,
|
|
409
|
-
})), undefined, {
|
|
410
|
-
txKind: true,
|
|
411
|
-
});
|
|
298
|
+
return this.fetchApiTransaction("transactions/sltp-order", Object.assign(Object.assign({}, inputs), { accountObjectId: this.accountCap.objectId, accountObjectVersion: this.accountCap.objectVersion, accountObjectDigest: this.accountCap.objectDigest }));
|
|
412
299
|
});
|
|
413
300
|
}
|
|
414
|
-
|
|
301
|
+
getCancelOrderTx(inputs) {
|
|
415
302
|
return __awaiter(this, void 0, void 0, function* () {
|
|
416
|
-
|
|
417
|
-
const { tx: txFromInputs, isSponsoredTx, stopOrders, gasCoinArg, } = inputs;
|
|
418
|
-
const tx = txFromInputs !== null && txFromInputs !== void 0 ? txFromInputs : new transactions_1.Transaction();
|
|
419
|
-
// tx.setSender(this.ownerAddress());
|
|
420
|
-
return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
|
|
421
|
-
"transactions/place-stop-orders", Object.assign({ stopOrders,
|
|
422
|
-
gasCoinArg,
|
|
423
|
-
isSponsoredTx, txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx })), walletAddress: this.ownerAddress() }, ("vaultId" in this.accountCap
|
|
424
|
-
? {
|
|
425
|
-
vaultId: this.accountCap.vaultId,
|
|
426
|
-
}
|
|
427
|
-
: {
|
|
428
|
-
accountId: this.accountCap.accountId,
|
|
429
|
-
})), undefined, {
|
|
430
|
-
txKind: true,
|
|
431
|
-
});
|
|
432
|
-
});
|
|
433
|
-
}
|
|
434
|
-
getPlaceSlTpOrdersTx(inputs) {
|
|
435
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
436
|
-
var _a;
|
|
437
|
-
const { tx: txFromInputs, isSponsoredTx, marketId } = inputs, slTpInputs = __rest(inputs, ["tx", "isSponsoredTx", "marketId"]);
|
|
438
|
-
const position = this.positionForMarketId({ marketId });
|
|
439
|
-
if (!position)
|
|
440
|
-
throw new Error("you have no position for this market");
|
|
441
|
-
const tx = txFromInputs !== null && txFromInputs !== void 0 ? txFromInputs : new transactions_1.Transaction();
|
|
442
|
-
// tx.setSender(this.ownerAddress());
|
|
443
|
-
return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
|
|
444
|
-
"transactions/place-sl-tp-orders", Object.assign(Object.assign(Object.assign(Object.assign({}, slTpInputs), { marketId, txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx })), walletAddress: this.ownerAddress() }), ("vaultId" in this.accountCap
|
|
445
|
-
? {
|
|
446
|
-
vaultId: this.accountCap.vaultId,
|
|
447
|
-
}
|
|
448
|
-
: {
|
|
449
|
-
accountId: this.accountCap.accountId,
|
|
450
|
-
})), { positionSide: perpetuals_1.Perpetuals.positionSide({
|
|
451
|
-
baseAssetAmount: position.baseAssetAmount,
|
|
452
|
-
}) }), undefined, {
|
|
453
|
-
txKind: true,
|
|
454
|
-
});
|
|
455
|
-
});
|
|
456
|
-
}
|
|
457
|
-
getEditStopOrdersTx(inputs) {
|
|
458
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
459
|
-
var _a;
|
|
460
|
-
const { tx: txFromInputs, stopOrders } = inputs;
|
|
461
|
-
const tx = txFromInputs !== null && txFromInputs !== void 0 ? txFromInputs : new transactions_1.Transaction();
|
|
462
|
-
// tx.setSender(this.ownerAddress());
|
|
463
|
-
return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
|
|
464
|
-
"transactions/edit-stop-orders", Object.assign({ stopOrders, txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx })), walletAddress: this.ownerAddress() }, ("vaultId" in this.accountCap
|
|
465
|
-
? {
|
|
466
|
-
vaultId: this.accountCap.vaultId,
|
|
467
|
-
}
|
|
468
|
-
: {
|
|
469
|
-
accountId: this.accountCap.accountId,
|
|
470
|
-
})), undefined, {
|
|
471
|
-
txKind: true,
|
|
472
|
-
});
|
|
303
|
+
return this.fetchApiTransaction("transactions/cancel-order", Object.assign(Object.assign({}, inputs), { collateralCoinType: this.accountCap.collateralCoinType, accountCapId: this.accountCap.objectId }));
|
|
473
304
|
});
|
|
474
305
|
}
|
|
475
|
-
|
|
306
|
+
getCancelOrdersTx(inputs) {
|
|
476
307
|
return __awaiter(this, void 0, void 0, function* () {
|
|
477
|
-
|
|
478
|
-
const { tx } = inputs, otherInputs = __rest(inputs, ["tx"]);
|
|
479
|
-
return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
|
|
480
|
-
"transactions/reduce-order", Object.assign(Object.assign(Object.assign({}, otherInputs), { txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx })), walletAddress: this.ownerAddress() }), ("vaultId" in this.accountCap
|
|
481
|
-
? {
|
|
482
|
-
vaultId: this.accountCap.vaultId,
|
|
483
|
-
}
|
|
484
|
-
: {
|
|
485
|
-
accountId: this.accountCap.accountId,
|
|
486
|
-
})), undefined, {
|
|
487
|
-
txKind: true,
|
|
488
|
-
});
|
|
308
|
+
return this.fetchApiTransaction("transactions/cancel-orders", Object.assign(Object.assign({}, inputs), { collateralCoinType: this.accountCap.collateralCoinType, accountCapId: this.accountCap.objectId }));
|
|
489
309
|
});
|
|
490
310
|
}
|
|
491
|
-
|
|
311
|
+
getReduceOrdersTx(inputs) {
|
|
492
312
|
return __awaiter(this, void 0, void 0, function* () {
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
|
|
496
|
-
"transactions/set-leverage", Object.assign({ leverage,
|
|
497
|
-
marketId,
|
|
498
|
-
collateralChange, txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx })), walletAddress: this.ownerAddress() }, ("vaultId" in this.accountCap
|
|
499
|
-
? {
|
|
500
|
-
vaultId: this.accountCap.vaultId,
|
|
501
|
-
}
|
|
502
|
-
: {
|
|
503
|
-
accountId: this.accountCap.accountId,
|
|
504
|
-
})), undefined, {
|
|
505
|
-
txKind: true,
|
|
506
|
-
});
|
|
313
|
+
const { market, orderDatas } = inputs;
|
|
314
|
+
return this.fetchApiTransaction("transactions/reduce-orders", Object.assign(Object.assign({}, inputs), { orderIds: orderDatas.map((order) => order.orderId), sizesToSubtract: orderDatas.map((order) => order.sizeToSubtract), packageId: market.marketData.packageId, marketInitialSharedVersion: market.marketData.initialSharedVersion, marketId: market.marketId, basePriceFeedId: market.marketParams.basePriceFeedId, collateralPriceFeedId: market.marketParams.collateralPriceFeedId, collateralCoinType: this.accountCap.collateralCoinType, accountCapId: this.accountCap.objectId }));
|
|
507
315
|
});
|
|
508
316
|
}
|
|
509
317
|
// =========================================================================
|
|
@@ -511,117 +319,77 @@ class PerpetualsAccount extends caller_1.Caller {
|
|
|
511
319
|
// =========================================================================
|
|
512
320
|
getClosePositionTx(inputs) {
|
|
513
321
|
return __awaiter(this, void 0, void 0, function* () {
|
|
514
|
-
return this.getPlaceMarketOrderTx(
|
|
322
|
+
return this.getPlaceMarketOrderTx(this.closePositionTxInputs(inputs));
|
|
515
323
|
});
|
|
516
324
|
}
|
|
517
325
|
// =========================================================================
|
|
518
|
-
//
|
|
326
|
+
// Inspections
|
|
519
327
|
// =========================================================================
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
account_id: this.accountCap.accountId
|
|
525
|
-
.toString()
|
|
526
|
-
.replaceAll("n", ""),
|
|
527
|
-
clearing_house_ids: (_a = inputs === null || inputs === void 0 ? void 0 : inputs.marketIds) !== null && _a !== void 0 ? _a : [],
|
|
528
|
-
};
|
|
328
|
+
setPositionLeverage(inputs) {
|
|
329
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
330
|
+
return this.fetchApi(`${this.accountCap.collateralCoinType}/accounts/${this.accountCap.accountId}/set-position-leverage`, inputs);
|
|
331
|
+
});
|
|
529
332
|
}
|
|
530
|
-
// public async
|
|
531
|
-
//
|
|
532
|
-
//
|
|
533
|
-
//
|
|
534
|
-
//
|
|
535
|
-
// error: string;
|
|
536
|
-
// }
|
|
537
|
-
// | {
|
|
538
|
-
// positionAfterOrder: PerpetualsPosition;
|
|
539
|
-
// priceSlippage: number;
|
|
540
|
-
// percentSlippage: Percentage;
|
|
541
|
-
// filledSize: number;
|
|
542
|
-
// filledSizeUsd: number;
|
|
543
|
-
// postedSize: number;
|
|
544
|
-
// postedSizeUsd: number;
|
|
545
|
-
// collateralChange: number;
|
|
546
|
-
// executionPrice: number;
|
|
547
|
-
// }
|
|
333
|
+
// public async getAllPositionLeverages(): Promise<
|
|
334
|
+
// {
|
|
335
|
+
// marketId: PerpetualsMarketId;
|
|
336
|
+
// leverage: number;
|
|
337
|
+
// }[]
|
|
548
338
|
// > {
|
|
549
|
-
// return this.fetchApi
|
|
550
|
-
//
|
|
551
|
-
// ApiPerpetualsPreviewOrderBody
|
|
552
|
-
// >(
|
|
553
|
-
// `${this.vaultId ? "vault" : "account"}/` +"previews/place-order",
|
|
554
|
-
// {
|
|
555
|
-
// ...inputs,
|
|
556
|
-
// accountId: this.accountCap.accountId,
|
|
557
|
-
// collateralCoinType: this.accountCap.collateralCoinType,
|
|
558
|
-
// },
|
|
559
|
-
// abortSignal
|
|
339
|
+
// return this.fetchApi(
|
|
340
|
+
// `${this.accountCap.collateralCoinType}/accounts/${this.accountCap.accountId}/position-leverages`
|
|
560
341
|
// );
|
|
561
342
|
// }
|
|
562
|
-
|
|
343
|
+
getPositionLeverages(inputs) {
|
|
563
344
|
return __awaiter(this, void 0, void 0, function* () {
|
|
564
|
-
return this.fetchApi(`${this.
|
|
565
|
-
"previews/place-market-order", Object.assign(Object.assign(Object.assign({}, inputs), ("vaultId" in this.accountCap
|
|
566
|
-
? {
|
|
567
|
-
vaultId: this.accountCap.vaultId,
|
|
568
|
-
}
|
|
569
|
-
: {
|
|
570
|
-
accountId: this.accountCap.accountId,
|
|
571
|
-
})), { collateralCoinType: this.accountCap.collateralCoinType }), abortSignal);
|
|
345
|
+
return this.fetchApi(`${this.accountCap.collateralCoinType}/accounts/${this.accountCap.accountId}/position-leverages/${JSON.stringify(inputs.marketIds)}`);
|
|
572
346
|
});
|
|
573
347
|
}
|
|
574
|
-
|
|
348
|
+
setPositionLeverageMessageToSign(inputs) {
|
|
349
|
+
return {
|
|
350
|
+
account_id: Number(this.accountCap.accountId),
|
|
351
|
+
market_id: inputs.marketId,
|
|
352
|
+
leverage: inputs.leverage,
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
getOrderPreview(inputs, abortSignal) {
|
|
575
356
|
return __awaiter(this, void 0, void 0, function* () {
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
: {
|
|
582
|
-
accountId: this.accountCap.accountId,
|
|
583
|
-
})), { collateralCoinType: this.accountCap.collateralCoinType }), abortSignal);
|
|
357
|
+
const response = yield this.fetchApi("preview-order", Object.assign(Object.assign({}, inputs), { accountId: this.accountCap.accountId, collateralCoinType: this.accountCap.collateralCoinType }), abortSignal);
|
|
358
|
+
if ("error" in response)
|
|
359
|
+
return response;
|
|
360
|
+
const { collateralChange } = response, remainingResponse = __rest(response, ["collateralChange"]);
|
|
361
|
+
return Object.assign(Object.assign({}, remainingResponse), { collateralChange: __1.Coin.normalizeBalance(collateralChange, this.collateralDecimals()) });
|
|
584
362
|
});
|
|
585
363
|
}
|
|
586
|
-
getCancelOrdersPreview(inputs
|
|
364
|
+
getCancelOrdersPreview(inputs) {
|
|
587
365
|
return __awaiter(this, void 0, void 0, function* () {
|
|
588
366
|
// NOTE: should this case return an error instead ?
|
|
589
367
|
if (Object.keys(inputs.marketIdsToData).length <= 0)
|
|
590
368
|
return {
|
|
591
|
-
collateralChange: 0,
|
|
369
|
+
collateralChange: BigInt(0),
|
|
592
370
|
marketIdsToPositionAfterCancelOrders: {},
|
|
593
371
|
};
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
:
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
});
|
|
602
|
-
}
|
|
603
|
-
getReduceOrderPreview(inputs, abortSignal) {
|
|
604
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
605
|
-
return this.fetchApi(`${this.vaultId ? "vault" : "account"}/` + "previews/reduce-order", Object.assign(Object.assign(Object.assign({}, inputs), ("vaultId" in this.accountCap
|
|
606
|
-
? {
|
|
607
|
-
vaultId: this.accountCap.vaultId,
|
|
608
|
-
}
|
|
609
|
-
: {
|
|
610
|
-
accountId: this.accountCap.accountId,
|
|
611
|
-
})), { collateralCoinType: this.accountCap.collateralCoinType }), abortSignal);
|
|
372
|
+
const response = yield this.fetchApi("preview-cancel-orders", Object.assign(Object.assign({}, inputs), { accountId: this.accountCap.accountId, collateralCoinType: this.accountCap.collateralCoinType }));
|
|
373
|
+
if ("error" in response)
|
|
374
|
+
return response;
|
|
375
|
+
return {
|
|
376
|
+
marketIdsToPositionAfterCancelOrders: response.marketIdsToPositionAfterCancelOrders,
|
|
377
|
+
collateralChange: __1.Coin.normalizeBalance(response.collateralChange, this.collateralDecimals()),
|
|
378
|
+
};
|
|
612
379
|
});
|
|
613
380
|
}
|
|
614
|
-
|
|
381
|
+
getReduceOrdersPreview(inputs) {
|
|
615
382
|
return __awaiter(this, void 0, void 0, function* () {
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
383
|
+
// NOTE: should this case not throw an error instead ?
|
|
384
|
+
if (Object.keys(inputs.orderDatas).length <= 0)
|
|
385
|
+
throw new Error("no orderDatas provided");
|
|
386
|
+
const response = yield this.fetchApi("preview-reduce-orders", Object.assign(Object.assign({}, inputs), { orderIds: inputs.orderDatas.map((order) => order.orderId), sizesToSubtract: inputs.orderDatas.map((order) => order.sizeToSubtract), accountId: this.accountCap.accountId, collateralCoinType: this.accountCap.collateralCoinType }));
|
|
387
|
+
if ("error" in response)
|
|
388
|
+
return response;
|
|
389
|
+
return {
|
|
390
|
+
positionAfterReduceOrders: response.positionAfterReduceOrders,
|
|
391
|
+
collateralChange: __1.Coin.normalizeBalance(response.collateralChange, this.collateralDecimals()),
|
|
392
|
+
};
|
|
625
393
|
});
|
|
626
394
|
}
|
|
627
395
|
getOrderDatas() {
|
|
@@ -635,40 +403,19 @@ class PerpetualsAccount extends caller_1.Caller {
|
|
|
635
403
|
], []);
|
|
636
404
|
if (orderDatas.length <= 0)
|
|
637
405
|
return [];
|
|
638
|
-
return this.fetchApi(
|
|
639
|
-
accountId: this.accountCap.accountId,
|
|
406
|
+
return this.fetchApi(`${this.accountCap.collateralCoinType}/accounts/${this.accountCap.accountId}/order-datas`, {
|
|
640
407
|
orderDatas,
|
|
641
408
|
});
|
|
642
409
|
});
|
|
643
410
|
}
|
|
644
|
-
getStopOrderDatas(inputs) {
|
|
645
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
646
|
-
const { bytes, signature, marketIds } = inputs;
|
|
647
|
-
return this.fetchApi("account/stop-order-datas", {
|
|
648
|
-
bytes,
|
|
649
|
-
signature,
|
|
650
|
-
accountId: this.accountCap.accountId,
|
|
651
|
-
walletAddress: this.ownerAddress(),
|
|
652
|
-
marketIds: marketIds !== null && marketIds !== void 0 ? marketIds : [],
|
|
653
|
-
});
|
|
654
|
-
});
|
|
655
|
-
}
|
|
656
411
|
getCollateralHistory(inputs) {
|
|
657
412
|
return __awaiter(this, void 0, void 0, function* () {
|
|
658
|
-
return this.fetchApi(
|
|
413
|
+
return this.fetchApi(`${this.accountCap.collateralCoinType}/accounts/${this.accountCap.accountId}/collateral-history`, inputs);
|
|
659
414
|
});
|
|
660
415
|
}
|
|
661
416
|
getOrderHistory(inputs) {
|
|
662
417
|
return __awaiter(this, void 0, void 0, function* () {
|
|
663
|
-
return this.fetchApi(
|
|
664
|
-
});
|
|
665
|
-
}
|
|
666
|
-
getMarginHistory() {
|
|
667
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
668
|
-
return this.fetchApi("account/margin-history", {
|
|
669
|
-
accountId: this.accountCap.accountId,
|
|
670
|
-
collateralCoinType: this.accountCap.collateralCoinType,
|
|
671
|
-
});
|
|
418
|
+
return this.fetchApi(`${this.accountCap.collateralCoinType}/accounts/${this.accountCap.accountId}/order-history`, inputs);
|
|
672
419
|
});
|
|
673
420
|
}
|
|
674
421
|
// =========================================================================
|
|
@@ -682,74 +429,8 @@ class PerpetualsAccount extends caller_1.Caller {
|
|
|
682
429
|
return undefined;
|
|
683
430
|
}
|
|
684
431
|
}
|
|
685
|
-
nonSlTpStopOrderDatas(inputs) {
|
|
686
|
-
const { stopOrderDatas } = inputs;
|
|
687
|
-
const slTpOrders = this.slTpStopOrderDatas(inputs);
|
|
688
|
-
const stopOrders = stopOrderDatas.filter((stopOrder) => !(slTpOrders !== null && slTpOrders !== void 0 ? slTpOrders : [])
|
|
689
|
-
.map((slTpOrder) => JSON.stringify(slTpOrder))
|
|
690
|
-
.includes(JSON.stringify(stopOrder)));
|
|
691
|
-
return stopOrders.length <= 0 ? undefined : stopOrders;
|
|
692
|
-
}
|
|
693
|
-
slTpStopOrderDatas(inputs) {
|
|
694
|
-
const { stopOrderDatas } = inputs;
|
|
695
|
-
let slTpOrders = [];
|
|
696
|
-
for (const { marketId } of this.account.positions) {
|
|
697
|
-
const { fullSlTpOrder, partialSlTpOrders } = this.slTpStopOrderDatasForMarketId({
|
|
698
|
-
marketId,
|
|
699
|
-
stopOrderDatas,
|
|
700
|
-
});
|
|
701
|
-
slTpOrders = [
|
|
702
|
-
...slTpOrders,
|
|
703
|
-
...(fullSlTpOrder ? [fullSlTpOrder] : []),
|
|
704
|
-
...(partialSlTpOrders !== null && partialSlTpOrders !== void 0 ? partialSlTpOrders : []),
|
|
705
|
-
];
|
|
706
|
-
}
|
|
707
|
-
return slTpOrders.length <= 0 ? undefined : slTpOrders;
|
|
708
|
-
}
|
|
709
|
-
nonSlTpStopOrderDatasForMarketId(inputs) {
|
|
710
|
-
const { marketId, stopOrderDatas } = inputs;
|
|
711
|
-
const position = this.positionForMarketId({ marketId });
|
|
712
|
-
if (!position)
|
|
713
|
-
return undefined;
|
|
714
|
-
const { fullSlTpOrder, partialSlTpOrders } = this.slTpStopOrderDatasForMarketId(inputs);
|
|
715
|
-
const stopOrders = stopOrderDatas.filter((stopOrder) => ![
|
|
716
|
-
...(fullSlTpOrder ? [fullSlTpOrder] : []),
|
|
717
|
-
...(partialSlTpOrders !== null && partialSlTpOrders !== void 0 ? partialSlTpOrders : []),
|
|
718
|
-
]
|
|
719
|
-
.map((slTpOrder) => JSON.stringify(slTpOrder))
|
|
720
|
-
.includes(JSON.stringify(stopOrder)));
|
|
721
|
-
return stopOrders.length <= 0 ? undefined : stopOrders;
|
|
722
|
-
}
|
|
723
|
-
slTpStopOrderDatasForMarketId(inputs) {
|
|
724
|
-
const { marketId, stopOrderDatas } = inputs;
|
|
725
|
-
const position = this.positionForMarketId({ marketId });
|
|
726
|
-
if (!position || position.baseAssetAmount === 0) {
|
|
727
|
-
return {
|
|
728
|
-
fullSlTpOrder: undefined,
|
|
729
|
-
partialSlTpOrders: undefined,
|
|
730
|
-
};
|
|
731
|
-
}
|
|
732
|
-
const side = !position ? undefined : perpetuals_1.Perpetuals.positionSide(position);
|
|
733
|
-
// TODO: clean this up
|
|
734
|
-
const fullSlTpOrder = stopOrderDatas.find((order) => order.marketId === marketId &&
|
|
735
|
-
order.slTp &&
|
|
736
|
-
order.side !== side &&
|
|
737
|
-
(order.slTp.stopLossIndexPrice ||
|
|
738
|
-
order.slTp.takeProfitIndexPrice) &&
|
|
739
|
-
order.size >= utils_1.Casting.i64MaxBigInt);
|
|
740
|
-
const partialSlTpOrders = stopOrderDatas.filter((order) => order.marketId === marketId &&
|
|
741
|
-
order.slTp &&
|
|
742
|
-
order.side !== side &&
|
|
743
|
-
(order.slTp.stopLossIndexPrice ||
|
|
744
|
-
order.slTp.takeProfitIndexPrice) &&
|
|
745
|
-
order.size < utils_1.Casting.i64MaxBigInt);
|
|
746
|
-
return {
|
|
747
|
-
fullSlTpOrder,
|
|
748
|
-
partialSlTpOrders: partialSlTpOrders.length <= 0 ? undefined : partialSlTpOrders,
|
|
749
|
-
};
|
|
750
|
-
}
|
|
751
432
|
collateral() {
|
|
752
|
-
return this.
|
|
433
|
+
return utils_1.Casting.IFixed.numberFromIFixed(this.accountCap.collateral);
|
|
753
434
|
}
|
|
754
435
|
collateralDecimals() {
|
|
755
436
|
return this.accountCap.collateralDecimals;
|
|
@@ -757,27 +438,6 @@ class PerpetualsAccount extends caller_1.Caller {
|
|
|
757
438
|
collateralBalance() {
|
|
758
439
|
return __1.Coin.normalizeBalance(this.collateral(), this.collateralDecimals());
|
|
759
440
|
}
|
|
760
|
-
isVault() {
|
|
761
|
-
return this.isVault !== undefined;
|
|
762
|
-
}
|
|
763
|
-
ownerAddress() {
|
|
764
|
-
return "walletAddress" in this.accountCap
|
|
765
|
-
? // TODO: change to ownerAddress ?
|
|
766
|
-
this.accountCap.walletAddress
|
|
767
|
-
: this.accountCap.ownerAddress;
|
|
768
|
-
}
|
|
769
|
-
accountObjectId() {
|
|
770
|
-
return this.accountCap.accountObjectId;
|
|
771
|
-
}
|
|
772
|
-
accountId() {
|
|
773
|
-
return this.accountCap.accountId;
|
|
774
|
-
}
|
|
775
|
-
// TODO: make this work with vaults
|
|
776
|
-
accountCapId() {
|
|
777
|
-
if ("vaultId" in this.accountCap)
|
|
778
|
-
throw new Error("not account cap id present on vault owned account");
|
|
779
|
-
return this.accountCap.objectId;
|
|
780
|
-
}
|
|
781
441
|
}
|
|
782
442
|
exports.PerpetualsAccount = PerpetualsAccount;
|
|
783
443
|
// =========================================================================
|