aftermath-ts-sdk 1.1.97 → 1.2.0-refactor.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/historicalData/historicalData.d.ts +1 -1
- package/dist/general/historicalData/historicalData.d.ts.map +1 -1
- package/dist/general/nfts/nftsApi.d.ts +1 -5
- package/dist/general/nfts/nftsApi.d.ts.map +1 -1
- package/dist/general/nfts/nftsApi.js +15 -29
- package/dist/general/nfts/nftsApiCasting.d.ts +0 -1
- package/dist/general/nfts/nftsApiCasting.d.ts.map +1 -1
- package/dist/general/nfts/nftsApiCasting.js +0 -10
- package/dist/general/nfts/nftsTypes.d.ts +0 -1
- package/dist/general/nfts/nftsTypes.d.ts.map +1 -1
- package/dist/general/prices/{coingecko/coinGeckoTypes.d.ts → coinGeckoTypes.d.ts} +1 -1
- package/dist/general/prices/coinGeckoTypes.d.ts.map +1 -0
- package/dist/general/providers/aftermath.d.ts.map +1 -1
- package/dist/general/providers/aftermath.js +1 -2
- package/dist/general/providers/aftermathApi.d.ts +1 -34
- package/dist/general/providers/aftermathApi.d.ts.map +1 -1
- package/dist/general/providers/aftermathApi.js +2 -90
- package/dist/general/types/castingTypes.d.ts +0 -7
- package/dist/general/types/castingTypes.d.ts.map +1 -1
- package/dist/general/types/configTypes.d.ts +0 -6
- package/dist/general/types/configTypes.d.ts.map +1 -1
- package/dist/general/types/generalTypes.d.ts +0 -3
- package/dist/general/types/generalTypes.d.ts.map +1 -1
- package/dist/general/types/index.d.ts +1 -0
- package/dist/general/types/index.d.ts.map +1 -1
- package/dist/general/types/index.js +1 -0
- package/dist/general/utils/helpers.d.ts +1 -16
- package/dist/general/utils/helpers.d.ts.map +1 -1
- package/dist/general/utils/helpers.js +2 -32
- package/dist/general/utils/index.d.ts +0 -1
- package/dist/general/utils/index.d.ts.map +1 -1
- package/dist/general/utils/index.js +0 -1
- package/dist/packages/coin/api/coinApi.d.ts +1 -12
- package/dist/packages/coin/api/coinApi.d.ts.map +1 -1
- package/dist/packages/coin/api/coinApi.js +0 -123
- package/dist/packages/dca/api/dcaApi.d.ts +1 -28
- package/dist/packages/dca/api/dcaApi.d.ts.map +1 -1
- package/dist/packages/dca/api/dcaApi.js +0 -172
- package/dist/packages/farms/api/farmsApi.d.ts +1 -24
- package/dist/packages/farms/api/farmsApi.d.ts.map +1 -1
- package/dist/packages/farms/api/farmsApi.js +0 -69
- package/dist/packages/farms/api/farmsApiCasting.d.ts +2 -3
- package/dist/packages/farms/api/farmsApiCasting.d.ts.map +1 -1
- package/dist/packages/farms/api/farmsApiCasting.js +0 -45
- package/dist/packages/farms/api/farmsApiCastingTypes.d.ts +0 -9
- package/dist/packages/farms/api/farmsApiCastingTypes.d.ts.map +1 -1
- package/dist/packages/faucet/api/faucetApi.d.ts +0 -5
- package/dist/packages/faucet/api/faucetApi.d.ts.map +1 -1
- package/dist/packages/faucet/api/faucetApi.js +2 -29
- package/dist/packages/faucet/faucet.d.ts +3 -2
- package/dist/packages/faucet/faucet.d.ts.map +1 -1
- package/dist/packages/faucet/faucet.js +2 -2
- package/dist/packages/leveragedStaking/api/leveragedStakingApi.d.ts.map +1 -1
- package/dist/packages/leveragedStaking/api/leveragedStakingApi.js +383 -298
- package/dist/packages/nftAmm/nftAmmMarket.d.ts +9 -5
- package/dist/packages/nftAmm/nftAmmMarket.d.ts.map +1 -1
- package/dist/packages/nftAmm/nftAmmMarket.js +34 -6
- package/dist/packages/oracle/api/oracleApi.d.ts +1 -10
- package/dist/packages/oracle/api/oracleApi.d.ts.map +1 -1
- package/dist/packages/oracle/api/oracleApi.js +0 -38
- package/dist/packages/perpetuals/api/perpetualsApi.d.ts +2 -58
- package/dist/packages/perpetuals/api/perpetualsApi.d.ts.map +1 -1
- package/dist/packages/perpetuals/api/perpetualsApi.js +0 -735
- package/dist/packages/pools/api/poolsApi.d.ts +2 -217
- package/dist/packages/pools/api/poolsApi.d.ts.map +1 -1
- package/dist/packages/pools/api/poolsApi.js +0 -752
- package/dist/packages/pools/api/poolsApiCasting.d.ts +3 -15
- package/dist/packages/pools/api/poolsApiCasting.d.ts.map +1 -1
- package/dist/packages/pools/api/poolsApiCasting.js +0 -86
- package/dist/packages/pools/api/poolsApiCastingTypes.d.ts +1 -15
- package/dist/packages/pools/api/poolsApiCastingTypes.d.ts.map +1 -1
- package/dist/packages/pools/pools.d.ts +4 -1
- package/dist/packages/pools/pools.d.ts.map +1 -1
- package/dist/packages/pools/pools.js +5 -0
- package/dist/packages/router/api/routerApi.d.ts +2 -116
- package/dist/packages/router/api/routerApi.d.ts.map +1 -1
- package/dist/packages/router/api/routerApi.js +3 -345
- package/dist/packages/router/api/routerApiCasting.d.ts +2 -5
- package/dist/packages/router/api/routerApiCasting.d.ts.map +1 -1
- package/dist/packages/router/api/routerApiCasting.js +0 -104
- package/dist/packages/router/api/routerApiCastingTypes.d.ts +1 -13
- package/dist/packages/router/api/routerApiCastingTypes.d.ts.map +1 -1
- package/dist/packages/router/routerTypes.d.ts +3 -35
- package/dist/packages/router/routerTypes.d.ts.map +1 -1
- package/dist/packages/staking/api/stakingApi.d.ts +2 -50
- package/dist/packages/staking/api/stakingApi.d.ts.map +1 -1
- package/dist/packages/staking/api/stakingApi.js +0 -226
- package/dist/packages/staking/api/stakingApiCasting.d.ts +2 -4
- package/dist/packages/staking/api/stakingApiCasting.d.ts.map +1 -1
- package/dist/packages/staking/api/stakingApiCasting.js +0 -21
- package/dist/packages/staking/api/stakingApiCastingTypes.d.ts +1 -8
- package/dist/packages/staking/api/stakingApiCastingTypes.d.ts.map +1 -1
- package/dist/packages/staking/stakingTypes.d.ts +2 -0
- package/dist/packages/staking/stakingTypes.d.ts.map +1 -1
- package/dist/packages/sui/api/suiApi.d.ts +1 -2
- package/dist/packages/sui/api/suiApi.d.ts.map +1 -1
- package/dist/packages/sui/api/suiApi.js +0 -3
- package/package.json +1 -1
- package/dist/general/dynamicGas/dynamicGasApi.d.ts +0 -15
- package/dist/general/dynamicGas/dynamicGasApi.d.ts.map +0 -1
- package/dist/general/dynamicGas/dynamicGasApi.js +0 -87
- package/dist/general/historicalData/historicalDataApi.d.ts +0 -9
- package/dist/general/historicalData/historicalDataApi.d.ts.map +0 -1
- package/dist/general/historicalData/historicalDataApi.js +0 -13
- package/dist/general/historicalData/historicalDataApiInterface.d.ts +0 -12
- package/dist/general/historicalData/historicalDataApiInterface.d.ts.map +0 -1
- package/dist/general/historicalData/historicalDataApiInterface.js +0 -2
- package/dist/general/historicalData/placeholderHistoricalDataApi.d.ts +0 -13
- package/dist/general/historicalData/placeholderHistoricalDataApi.d.ts.map +0 -1
- package/dist/general/historicalData/placeholderHistoricalDataApi.js +0 -26
- package/dist/general/prices/coingecko/coinGeckoApiHelpers.d.ts +0 -22
- package/dist/general/prices/coingecko/coinGeckoApiHelpers.d.ts.map +0 -1
- package/dist/general/prices/coingecko/coinGeckoApiHelpers.js +0 -179
- package/dist/general/prices/coingecko/coinGeckoPricesApi.d.ts +0 -23
- package/dist/general/prices/coingecko/coinGeckoPricesApi.d.ts.map +0 -1
- package/dist/general/prices/coingecko/coinGeckoPricesApi.js +0 -113
- package/dist/general/prices/coingecko/coinGeckoTypes.d.ts.map +0 -1
- package/dist/general/prices/placeholder/placeholderPricesApi.d.ts +0 -20
- package/dist/general/prices/placeholder/placeholderPricesApi.d.ts.map +0 -1
- package/dist/general/prices/placeholder/placeholderPricesApi.js +0 -85
- package/dist/general/prices/pricesApiInterface.d.ts +0 -13
- package/dist/general/prices/pricesApiInterface.d.ts.map +0 -1
- package/dist/general/prices/pricesApiInterface.js +0 -2
- package/dist/general/prices/pyth/pythPricesApi.d.ts +0 -2
- package/dist/general/prices/pyth/pythPricesApi.d.ts.map +0 -1
- package/dist/general/prices/pyth/pythPricesApi.js +0 -52
- package/dist/general/prices/pyth/pythPricesApiHelpers.d.ts +0 -13
- package/dist/general/prices/pyth/pythPricesApiHelpers.d.ts.map +0 -1
- package/dist/general/prices/pyth/pythPricesApiHelpers.js +0 -77
- package/dist/general/prices/router/routerPricesApi.d.ts +0 -18
- package/dist/general/prices/router/routerPricesApi.d.ts.map +0 -1
- package/dist/general/prices/router/routerPricesApi.js +0 -126
- package/dist/general/utils/indexerCaller.d.ts +0 -14
- package/dist/general/utils/indexerCaller.d.ts.map +0 -1
- package/dist/general/utils/indexerCaller.js +0 -129
- /package/dist/general/prices/{coingecko/coinGeckoTypes.js → coinGeckoTypes.js} +0 -0
|
@@ -19,20 +19,14 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
19
19
|
}
|
|
20
20
|
return t;
|
|
21
21
|
};
|
|
22
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
23
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
24
|
-
};
|
|
25
22
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
23
|
exports.PerpetualsApi = void 0;
|
|
27
24
|
const transactions_1 = require("@mysten/sui/transactions");
|
|
28
25
|
const utils_1 = require("../../../general/utils");
|
|
29
26
|
const sui_1 = require("../../sui");
|
|
30
27
|
const perpetualsTypes_1 = require("../perpetualsTypes");
|
|
31
|
-
const perpetualsApiCasting_1 = require("./perpetualsApiCasting");
|
|
32
28
|
const perpetuals_1 = require("../perpetuals");
|
|
33
29
|
const eventsApiHelpers_1 = require("../../../general/apiHelpers/eventsApiHelpers");
|
|
34
|
-
const dayjs_1 = __importDefault(require("dayjs"));
|
|
35
|
-
const duration_1 = __importDefault(require("dayjs/plugin/duration"));
|
|
36
30
|
const transactionsApiHelpers_1 = require("../../../general/apiHelpers/transactionsApiHelpers");
|
|
37
31
|
const bcs_1 = require("@mysten/sui/bcs");
|
|
38
32
|
class PerpetualsApi {
|
|
@@ -42,250 +36,6 @@ class PerpetualsApi {
|
|
|
42
36
|
constructor(Provider) {
|
|
43
37
|
this.Provider = Provider;
|
|
44
38
|
// =========================================================================
|
|
45
|
-
// Objects
|
|
46
|
-
// =========================================================================
|
|
47
|
-
this.fetchOwnedRawAccountCapsOfType = (inputs) => __awaiter(this, void 0, void 0, function* () {
|
|
48
|
-
const { walletAddress, collateralCoinType } = inputs;
|
|
49
|
-
const objectType = this.getAccountCapType({ collateralCoinType });
|
|
50
|
-
const objectResponse = yield this.Provider.Objects().fetchObjectsOfTypeOwnedByAddress({
|
|
51
|
-
objectType,
|
|
52
|
-
walletAddress,
|
|
53
|
-
options: {
|
|
54
|
-
showBcs: true,
|
|
55
|
-
showType: true,
|
|
56
|
-
},
|
|
57
|
-
});
|
|
58
|
-
const accCaps = objectResponse.map((accCap) => {
|
|
59
|
-
var _a, _b;
|
|
60
|
-
const accCapObj = perpetualsTypes_1.perpetualsRegistry.Account.fromBase64(utils_1.Casting.bcsBytesFromSuiObjectResponse(accCap));
|
|
61
|
-
return perpetualsApiCasting_1.PerpetualsApiCasting.rawAccountCapFromRaw(accCapObj, collateralCoinType, Number((_a = accCap.data) === null || _a === void 0 ? void 0 : _a.version), (_b = accCap.data) === null || _b === void 0 ? void 0 : _b.digest);
|
|
62
|
-
});
|
|
63
|
-
return accCaps;
|
|
64
|
-
});
|
|
65
|
-
this.fetchAccount = (inputs) => __awaiter(this, void 0, void 0, function* () {
|
|
66
|
-
const { accountId } = inputs;
|
|
67
|
-
const response = yield this.Provider.indexerCaller.fetchIndexer(`perpetuals/accounts/${accountId}/positions`, undefined, undefined, undefined, undefined, true);
|
|
68
|
-
return utils_1.Casting.perpetuals.accountObjectFromIndexerResponse(response, inputs.collateralCoinType);
|
|
69
|
-
});
|
|
70
|
-
this.fetchAccountOrderDatas = (inputs) => __awaiter(this, void 0, void 0, function* () {
|
|
71
|
-
const { accountCapId, orderDatas } = inputs;
|
|
72
|
-
const orders = yield this.Provider.indexerCaller.fetchIndexer(`perpetuals/accounts/orders`, {
|
|
73
|
-
account_id: utils_1.Helpers.addLeadingZeroesToType(accountCapId),
|
|
74
|
-
order_ids: orderDatas.map((orderData) => String(orderData.orderId).replaceAll("n", "")),
|
|
75
|
-
}, undefined, undefined, undefined, true);
|
|
76
|
-
if (orders.length !== orderDatas.length)
|
|
77
|
-
throw new Error("unable to find all orders");
|
|
78
|
-
return orders.map((order) => {
|
|
79
|
-
const orderId = BigInt(order.order_id);
|
|
80
|
-
const initialSize = BigInt(order.initial_size);
|
|
81
|
-
const orderData = orderDatas.find((orderData) => orderData.orderId === orderId);
|
|
82
|
-
if (!orderData)
|
|
83
|
-
throw new Error("unable to find all orders");
|
|
84
|
-
return {
|
|
85
|
-
marketId: utils_1.Helpers.addLeadingZeroesToType(order.ch_id),
|
|
86
|
-
side: perpetuals_1.Perpetuals.orderIdToSide(orderId),
|
|
87
|
-
filledSize: initialSize - orderData.currentSize,
|
|
88
|
-
initialSize,
|
|
89
|
-
orderId,
|
|
90
|
-
};
|
|
91
|
-
});
|
|
92
|
-
});
|
|
93
|
-
this.fetchHistoricalMarketData = (inputs) => __awaiter(this, void 0, void 0, function* () {
|
|
94
|
-
const { marketId, fromTimestamp, toTimestamp, intervalMs } = inputs;
|
|
95
|
-
const response = yield this.Provider.indexerCaller.fetchIndexer(`perpetuals/markets/candle-history`, {
|
|
96
|
-
ch_id: utils_1.Helpers.addLeadingZeroesToType(marketId),
|
|
97
|
-
timestamp_ms_from: fromTimestamp,
|
|
98
|
-
timestamp_ms_to: toTimestamp,
|
|
99
|
-
resolution_ms: intervalMs,
|
|
100
|
-
}, undefined, undefined, undefined, true);
|
|
101
|
-
return response.map((data) => ({
|
|
102
|
-
timestamp: data.timestamp_ms,
|
|
103
|
-
open: data.open,
|
|
104
|
-
close: data.close,
|
|
105
|
-
high: data.high,
|
|
106
|
-
low: data.low,
|
|
107
|
-
volume: data.volume,
|
|
108
|
-
}));
|
|
109
|
-
});
|
|
110
|
-
// =========================================================================
|
|
111
|
-
// Inspections
|
|
112
|
-
// =========================================================================
|
|
113
|
-
this.setPositionLeverage = (inputs) => __awaiter(this, void 0, void 0, function* () {
|
|
114
|
-
return this.Provider.indexerCaller.fetchIndexer(`perpetuals/account/set-position-leverage`, {
|
|
115
|
-
wallet_address: utils_1.Helpers.addLeadingZeroesToType(inputs.walletAddress),
|
|
116
|
-
signature: inputs.signature,
|
|
117
|
-
bytes: inputs.bytes,
|
|
118
|
-
}, undefined, undefined, undefined, true);
|
|
119
|
-
});
|
|
120
|
-
this.fetchAllPositionLeverages = (inputs) => __awaiter(this, void 0, void 0, function* () {
|
|
121
|
-
return (yield this.Provider.indexerCaller.fetchIndexer(`perpetuals/accounts/${inputs.accountId}/position-leverages`, undefined, undefined, undefined, undefined, true)).map((data) => ({
|
|
122
|
-
marketId: data.market_id,
|
|
123
|
-
leverage: data.leverage,
|
|
124
|
-
}));
|
|
125
|
-
});
|
|
126
|
-
this.fetchPositionLeverages = (inputs) => __awaiter(this, void 0, void 0, function* () {
|
|
127
|
-
const marketIds = inputs.marketIds.map((objectId) => utils_1.Helpers.addLeadingZeroesToType(objectId));
|
|
128
|
-
const leverages = yield this.Provider.indexerCaller.fetchIndexer(`perpetuals/accounts/${inputs.accountId}/position-leverages`, {
|
|
129
|
-
market_ids: marketIds,
|
|
130
|
-
}, undefined, undefined, undefined, true);
|
|
131
|
-
return marketIds.map((objectId) => {
|
|
132
|
-
var _a;
|
|
133
|
-
return ((_a = leverages.find((leverage) => leverage.market_id === objectId)) !== null && _a !== void 0 ? _a : {
|
|
134
|
-
leverage: 1,
|
|
135
|
-
}).leverage;
|
|
136
|
-
});
|
|
137
|
-
});
|
|
138
|
-
this.fetchPreviewOrder = (
|
|
139
|
-
// TODO: remove unused inputs
|
|
140
|
-
inputs) => __awaiter(this, void 0, void 0, function* () {
|
|
141
|
-
const { marketId, side, leverage, accountId } = inputs;
|
|
142
|
-
try {
|
|
143
|
-
const response = yield this.Provider.indexerCaller.fetchIndexer(`perpetuals/previews/${"price" in inputs ? "limit" : "market"}-order`, Object.assign({ leverage, ch_id: marketId, account_id: Number(accountId), side: Boolean(side), size: Number(inputs.size) }, ("price" in inputs
|
|
144
|
-
? {
|
|
145
|
-
// limit order
|
|
146
|
-
price: Number(inputs.price),
|
|
147
|
-
order_type: inputs.orderType,
|
|
148
|
-
}
|
|
149
|
-
: {
|
|
150
|
-
// market order
|
|
151
|
-
})), undefined, undefined, undefined, true);
|
|
152
|
-
const executionPrice = utils_1.Casting.IFixed.numberFromIFixed(utils_1.Casting.IFixed.iFixedFromStringBytes(response.execution_price));
|
|
153
|
-
const filledSize = utils_1.Casting.IFixed.numberFromIFixed(utils_1.Casting.IFixed.iFixedFromStringBytes(response.size_filled));
|
|
154
|
-
const filledSizeUsd = filledSize * executionPrice;
|
|
155
|
-
const postedSize = response.size_posted
|
|
156
|
-
? utils_1.Casting.IFixed.numberFromIFixed(utils_1.Casting.IFixed.iFixedFromStringBytes(response.size_posted))
|
|
157
|
-
: 0;
|
|
158
|
-
const postedSizeUsd = "price" in inputs
|
|
159
|
-
? // limit order
|
|
160
|
-
postedSize *
|
|
161
|
-
perpetuals_1.Perpetuals.orderPriceToPrice({
|
|
162
|
-
orderPrice: inputs.price,
|
|
163
|
-
lotSize: inputs.lotSize,
|
|
164
|
-
tickSize: inputs.tickSize,
|
|
165
|
-
})
|
|
166
|
-
: // market order
|
|
167
|
-
0;
|
|
168
|
-
const positionAfterOrder = utils_1.Casting.perpetuals.positionFromIndexerReponse({
|
|
169
|
-
position: response.position,
|
|
170
|
-
collateralCoinType: inputs.collateralCoinType,
|
|
171
|
-
marketId: inputs.marketId,
|
|
172
|
-
leverage,
|
|
173
|
-
});
|
|
174
|
-
return {
|
|
175
|
-
postedSize,
|
|
176
|
-
postedSizeUsd,
|
|
177
|
-
filledSize,
|
|
178
|
-
filledSizeUsd,
|
|
179
|
-
// NOTE: is this not needed ?
|
|
180
|
-
executionPrice,
|
|
181
|
-
positionAfterOrder,
|
|
182
|
-
priceSlippage: utils_1.Casting.IFixed.numberFromIFixed(utils_1.Casting.IFixed.iFixedFromStringBytes(response.price_slippage)),
|
|
183
|
-
percentSlippage: utils_1.Casting.IFixed.numberFromIFixed(utils_1.Casting.IFixed.iFixedFromStringBytes(response.percent_slippage)),
|
|
184
|
-
collateralChange: utils_1.Casting.IFixed.numberFromIFixed(utils_1.Casting.IFixed.iFixedFromStringBytes(response.collateral_change)),
|
|
185
|
-
};
|
|
186
|
-
}
|
|
187
|
-
catch (e1) {
|
|
188
|
-
try {
|
|
189
|
-
const splitErr = String(e1).split("500 Internal Server Error ");
|
|
190
|
-
return {
|
|
191
|
-
error: splitErr[splitErr.length - 1],
|
|
192
|
-
};
|
|
193
|
-
}
|
|
194
|
-
catch (e2) {
|
|
195
|
-
return {
|
|
196
|
-
error: "An error occurred.",
|
|
197
|
-
};
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
});
|
|
201
|
-
this.fetchOrderbookPrice = (inputs) => __awaiter(this, void 0, void 0, function* () {
|
|
202
|
-
const { collateralCoinType, marketId,
|
|
203
|
-
// marketInitialSharedVersion
|
|
204
|
-
} = inputs;
|
|
205
|
-
const tx = new transactions_1.Transaction();
|
|
206
|
-
this.getBookPriceTx({
|
|
207
|
-
tx,
|
|
208
|
-
marketId,
|
|
209
|
-
collateralCoinType,
|
|
210
|
-
// marketInitialSharedVersion,
|
|
211
|
-
});
|
|
212
|
-
const bytes = yield this.Provider.Inspections().fetchFirstBytesFromTxOutput({
|
|
213
|
-
tx,
|
|
214
|
-
});
|
|
215
|
-
return perpetualsApiCasting_1.PerpetualsApiCasting.orderbookPriceFromBytes(bytes);
|
|
216
|
-
});
|
|
217
|
-
this.fetchAllMarkets = (inputs) => __awaiter(this, void 0, void 0, function* () {
|
|
218
|
-
const { collateralCoinType } = inputs;
|
|
219
|
-
const response = yield this.Provider.indexerCaller.fetchIndexer(`perpetuals/markets/${utils_1.Helpers.stripLeadingZeroesFromType(collateralCoinType)}`, undefined, undefined, undefined, undefined, true);
|
|
220
|
-
const markets = Object.values(response);
|
|
221
|
-
// const priceFeedIds = markets
|
|
222
|
-
// .map((market) => [
|
|
223
|
-
// Casting.addressFromStringBytes(
|
|
224
|
-
// market.market_params.base_pfs_id
|
|
225
|
-
// ),
|
|
226
|
-
// Casting.addressFromStringBytes(
|
|
227
|
-
// market.market_params.collateral_pfs_id
|
|
228
|
-
// ),
|
|
229
|
-
// ])
|
|
230
|
-
// .reduce((acc, curr) => [...acc, ...curr], []);
|
|
231
|
-
const priceFeedIds = markets.map((market) => utils_1.Casting.addressFromStringBytes(market.object.market_params.base_pfs_id));
|
|
232
|
-
const symbols = yield this.Provider.Oracle().fetchPriceFeedSymbols({
|
|
233
|
-
priceFeedIds,
|
|
234
|
-
});
|
|
235
|
-
return markets.map((market, index) => utils_1.Casting.perpetuals.marketDataFromIndexerResponse(market, collateralCoinType, symbols[index].symbol));
|
|
236
|
-
});
|
|
237
|
-
// public fetchAllMarketIds = async (inputs: {
|
|
238
|
-
// collateralCoinType: CoinType;
|
|
239
|
-
// }): Promise<PerpetualsMarketId[]> => {
|
|
240
|
-
// const { collateralCoinType } = inputs;
|
|
241
|
-
// const marketIdsData = await this.Provider.indexerCaller.fetchIndexer<
|
|
242
|
-
// {
|
|
243
|
-
// marketId: ObjectId;
|
|
244
|
-
// }[]
|
|
245
|
-
// >(
|
|
246
|
-
// `perpetuals/markets/${Helpers.addLeadingZeroesToType(
|
|
247
|
-
// collateralCoinType
|
|
248
|
-
// )}`
|
|
249
|
-
// );
|
|
250
|
-
// return marketIdsData.map((data) =>
|
|
251
|
-
// Helpers.addLeadingZeroesToType(data.marketId)
|
|
252
|
-
// );
|
|
253
|
-
// };
|
|
254
|
-
this.fetchOrderbookState = (inputs) => __awaiter(this, void 0, void 0, function* () {
|
|
255
|
-
const { orderbookPrice, lotSize, tickSize } = inputs;
|
|
256
|
-
const PRICE_SCALE_BOUND = 10;
|
|
257
|
-
const midPrice = perpetuals_1.Perpetuals.priceToOrderPrice(Object.assign(Object.assign({}, inputs), { price: orderbookPrice }));
|
|
258
|
-
const lowPrice = perpetuals_1.Perpetuals.priceToOrderPrice(Object.assign(Object.assign({}, inputs), { price: orderbookPrice / PRICE_SCALE_BOUND }));
|
|
259
|
-
const highPrice = perpetuals_1.Perpetuals.priceToOrderPrice(Object.assign(Object.assign({}, inputs), { price: orderbookPrice * PRICE_SCALE_BOUND }));
|
|
260
|
-
const [bids, asks] = yield Promise.all([
|
|
261
|
-
this.fetchOrderbookOrders(Object.assign(Object.assign({}, inputs), { side: perpetualsTypes_1.PerpetualsOrderSide.Bid, fromPrice: midPrice, toPrice: lowPrice })),
|
|
262
|
-
this.fetchOrderbookOrders(Object.assign(Object.assign({}, inputs), { side: perpetualsTypes_1.PerpetualsOrderSide.Ask, fromPrice: midPrice, toPrice: highPrice })),
|
|
263
|
-
]);
|
|
264
|
-
const askPrices = asks.map((ask) => ask.price);
|
|
265
|
-
const bidPrices = bids.map((bid) => bid.price);
|
|
266
|
-
const minAskPrice = askPrices.length > 0 ? utils_1.Helpers.minBigInt(...askPrices) : BigInt(0);
|
|
267
|
-
const maxBidPrice = bidPrices.length > 0 ? utils_1.Helpers.maxBigInt(...bidPrices) : BigInt(0);
|
|
268
|
-
return {
|
|
269
|
-
bids: PerpetualsApi.bucketOrders(Object.assign(Object.assign({}, inputs), { side: perpetualsTypes_1.PerpetualsOrderSide.Bid, orders: bids })),
|
|
270
|
-
asks: PerpetualsApi.bucketOrders(Object.assign(Object.assign({}, inputs), { side: perpetualsTypes_1.PerpetualsOrderSide.Ask, orders: asks })),
|
|
271
|
-
minAskPrice: perpetuals_1.Perpetuals.orderPriceToPrice({
|
|
272
|
-
orderPrice: minAskPrice,
|
|
273
|
-
lotSize,
|
|
274
|
-
tickSize,
|
|
275
|
-
}),
|
|
276
|
-
maxBidPrice: perpetuals_1.Perpetuals.orderPriceToPrice({
|
|
277
|
-
orderPrice: maxBidPrice,
|
|
278
|
-
lotSize,
|
|
279
|
-
tickSize,
|
|
280
|
-
}),
|
|
281
|
-
};
|
|
282
|
-
});
|
|
283
|
-
this.fetchMaxOrderSize = (inputs) => __awaiter(this, void 0, void 0, function* () {
|
|
284
|
-
const { marketId, accountId, collateral, side, price, leverage } = inputs;
|
|
285
|
-
const maxSize = yield this.Provider.indexerCaller.fetchIndexer(`perpetuals/calculations/${inputs.price !== undefined ? "limit" : "market"}-order-max-size`, Object.assign({ leverage, ch_id: marketId, account_id: Number(accountId), collateral_to_allocate: Number(collateral), side: Boolean(side) }, (price !== undefined ? { price: Number(price) } : {})), undefined, undefined, undefined, true);
|
|
286
|
-
return BigInt(Math.floor(maxSize));
|
|
287
|
-
});
|
|
288
|
-
// =========================================================================
|
|
289
39
|
// Transaction Commands
|
|
290
40
|
// =========================================================================
|
|
291
41
|
this.depositCollateralTx = (inputs) => {
|
|
@@ -656,85 +406,6 @@ class PerpetualsApi {
|
|
|
656
406
|
coinId }, inputs));
|
|
657
407
|
return tx;
|
|
658
408
|
});
|
|
659
|
-
this.fetchBuildPlaceMarketOrderTx = (inputs) => __awaiter(this, void 0, void 0, function* () {
|
|
660
|
-
const { walletAddress, marketId, accountObjectId, accountObjectVersion, accountObjectDigest, side, size, collateralChange, hasPosition, } = inputs;
|
|
661
|
-
const { ptb: txKind } = yield this.Provider.indexerCaller.fetchIndexer(`perpetuals/transactions/market-order`, {
|
|
662
|
-
ch_id: marketId,
|
|
663
|
-
account_obj_id: accountObjectId,
|
|
664
|
-
account_obj_version: accountObjectVersion,
|
|
665
|
-
account_obj_digest: accountObjectDigest,
|
|
666
|
-
side: Boolean(side),
|
|
667
|
-
size: Number(size),
|
|
668
|
-
collateral_to_allocate: collateralChange > BigInt(0) ? Number(collateralChange) : 0,
|
|
669
|
-
collateral_to_deallocate: collateralChange < BigInt(0)
|
|
670
|
-
? Math.abs(Number(collateralChange))
|
|
671
|
-
: 0,
|
|
672
|
-
position_found: hasPosition,
|
|
673
|
-
}, undefined, undefined, undefined, true);
|
|
674
|
-
const tx = transactions_1.Transaction.fromKind(new Uint8Array(txKind.map((byte) => Number(byte))));
|
|
675
|
-
tx.setSender(walletAddress);
|
|
676
|
-
return tx;
|
|
677
|
-
// const { tx, sessionPotatoId } = this.createTxAndStartSession(inputs);
|
|
678
|
-
// this.placeMarketOrderTx({
|
|
679
|
-
// ...inputs,
|
|
680
|
-
// tx,
|
|
681
|
-
// sessionPotatoId,
|
|
682
|
-
// });
|
|
683
|
-
// this.endSessionAndShareMarket({
|
|
684
|
-
// ...inputs,
|
|
685
|
-
// tx,
|
|
686
|
-
// sessionPotatoId,
|
|
687
|
-
// });
|
|
688
|
-
// if (inputs.collateralChange < BigInt(0)) {
|
|
689
|
-
// this.deallocateCollateralTx({
|
|
690
|
-
// ...inputs,
|
|
691
|
-
// tx,
|
|
692
|
-
// amount: Helpers.absBigInt(inputs.collateralChange),
|
|
693
|
-
// });
|
|
694
|
-
// }
|
|
695
|
-
// return tx;
|
|
696
|
-
});
|
|
697
|
-
this.fetchBuildPlaceLimitOrderTx = (inputs) => __awaiter(this, void 0, void 0, function* () {
|
|
698
|
-
const { walletAddress, marketId, accountObjectId, accountObjectVersion, accountObjectDigest, side, size, orderType, price, collateralChange, hasPosition, } = inputs;
|
|
699
|
-
const { ptb: txKind } = yield this.Provider.indexerCaller.fetchIndexer(`perpetuals/transactions/limit-order`, {
|
|
700
|
-
ch_id: marketId,
|
|
701
|
-
account_obj_id: accountObjectId,
|
|
702
|
-
account_obj_version: accountObjectVersion,
|
|
703
|
-
account_obj_digest: accountObjectDigest,
|
|
704
|
-
side: Boolean(side),
|
|
705
|
-
size: Number(size),
|
|
706
|
-
price: Number(price),
|
|
707
|
-
order_type: orderType,
|
|
708
|
-
collateral_to_allocate: collateralChange > BigInt(0) ? Number(collateralChange) : 0,
|
|
709
|
-
collateral_to_deallocate: collateralChange < BigInt(0)
|
|
710
|
-
? Math.abs(Number(collateralChange))
|
|
711
|
-
: 0,
|
|
712
|
-
position_found: hasPosition,
|
|
713
|
-
}, undefined, undefined, undefined, true);
|
|
714
|
-
const tx = transactions_1.Transaction.fromKind(new Uint8Array(txKind.map((byte) => Number(byte))));
|
|
715
|
-
tx.setSender(walletAddress);
|
|
716
|
-
return tx;
|
|
717
|
-
// const { tx, sessionPotatoId } = this.createTxAndStartSession(inputs);
|
|
718
|
-
// // TODO: handle (de)allocations everywhere
|
|
719
|
-
// this.placeLimitOrderTx({
|
|
720
|
-
// ...inputs,
|
|
721
|
-
// tx,
|
|
722
|
-
// sessionPotatoId,
|
|
723
|
-
// });
|
|
724
|
-
// this.endSessionAndShareMarket({
|
|
725
|
-
// ...inputs,
|
|
726
|
-
// tx,
|
|
727
|
-
// sessionPotatoId,
|
|
728
|
-
// });
|
|
729
|
-
// if (inputs.collateralChange < BigInt(0)) {
|
|
730
|
-
// this.deallocateCollateralTx({
|
|
731
|
-
// ...inputs,
|
|
732
|
-
// tx,
|
|
733
|
-
// amount: Helpers.absBigInt(inputs.collateralChange),
|
|
734
|
-
// });
|
|
735
|
-
// }
|
|
736
|
-
// return tx;
|
|
737
|
-
});
|
|
738
409
|
this.buildCancelOrderTx = (inputs) => {
|
|
739
410
|
const { orderId, marketId, marketInitialSharedVersion, collateral, basePriceFeedId, collateralPriceFeedId } = inputs, otherInputs = __rest(inputs, ["orderId", "marketId", "marketInitialSharedVersion", "collateral", "basePriceFeedId", "collateralPriceFeedId"]);
|
|
740
411
|
return this.buildCancelOrdersTx(Object.assign(Object.assign({}, otherInputs), { orderDatas: [
|
|
@@ -839,51 +510,6 @@ class PerpetualsApi {
|
|
|
839
510
|
return `${this.addresses.perpetuals.packages.perpetuals}::${PerpetualsApi.constants.moduleNames.account}::Account<${inputs.collateralCoinType}>`;
|
|
840
511
|
};
|
|
841
512
|
// =========================================================================
|
|
842
|
-
// Private Helpers
|
|
843
|
-
// =========================================================================
|
|
844
|
-
this.fetchOrdersSizes = (inputs) => __awaiter(this, void 0, void 0, function* () {
|
|
845
|
-
const { orderIds, marketId, collateralCoinType } = inputs;
|
|
846
|
-
if (orderIds.length <= 0)
|
|
847
|
-
return [];
|
|
848
|
-
const tx = new transactions_1.Transaction();
|
|
849
|
-
const orderbookId = this.getOrderbookTx({
|
|
850
|
-
tx,
|
|
851
|
-
collateralCoinType,
|
|
852
|
-
marketId,
|
|
853
|
-
});
|
|
854
|
-
for (const orderId of orderIds) {
|
|
855
|
-
this.getOrderSizeTx({
|
|
856
|
-
tx,
|
|
857
|
-
orderId,
|
|
858
|
-
orderbookId,
|
|
859
|
-
});
|
|
860
|
-
}
|
|
861
|
-
const { allBytes } = yield this.Provider.Inspections().fetchAllBytesFromTx({
|
|
862
|
-
tx,
|
|
863
|
-
});
|
|
864
|
-
const sizes = allBytes
|
|
865
|
-
.slice(1)
|
|
866
|
-
.map((bytes) => utils_1.Casting.bigIntFromBytes(bytes[0]));
|
|
867
|
-
return sizes;
|
|
868
|
-
});
|
|
869
|
-
this.fetchOrderbookOrders = (inputs) => __awaiter(this, void 0, void 0, function* () {
|
|
870
|
-
const { collateralCoinType, marketId, side, fromPrice, toPrice } = inputs;
|
|
871
|
-
const tx = new transactions_1.Transaction();
|
|
872
|
-
const orderbookId = this.getOrderbookTx({
|
|
873
|
-
tx,
|
|
874
|
-
collateralCoinType,
|
|
875
|
-
marketId,
|
|
876
|
-
});
|
|
877
|
-
this.inspectOrdersTx({ tx, orderbookId, side, fromPrice, toPrice });
|
|
878
|
-
const bytes = yield this.Provider.Inspections().fetchFirstBytesFromTxOutput({
|
|
879
|
-
tx,
|
|
880
|
-
});
|
|
881
|
-
const orderInfos = bcs_1.bcs
|
|
882
|
-
.vector(perpetualsTypes_1.perpetualsRegistry.OrderInfo)
|
|
883
|
-
.parse(new Uint8Array(bytes));
|
|
884
|
-
return orderInfos.map((orderInfo) => utils_1.Casting.perpetuals.orderInfoFromRaw(orderInfo));
|
|
885
|
-
});
|
|
886
|
-
// =========================================================================
|
|
887
513
|
// Event Types
|
|
888
514
|
// =========================================================================
|
|
889
515
|
this.eventType = (eventName) => eventsApiHelpers_1.EventsApiHelpers.createEventType(this.addresses.perpetuals.packages.events, PerpetualsApi.constants.moduleNames.events, eventName);
|
|
@@ -1002,137 +628,6 @@ class PerpetualsApi {
|
|
|
1002
628
|
},
|
|
1003
629
|
};
|
|
1004
630
|
}
|
|
1005
|
-
// public fetchMarket = async (inputs: {
|
|
1006
|
-
// marketId: PerpetualsMarketId;
|
|
1007
|
-
// collateralCoinType: CoinType;
|
|
1008
|
-
// }): Promise<PerpetualsMarketData> => {
|
|
1009
|
-
// const { collateralCoinType } = inputs;
|
|
1010
|
-
// return this.Provider.Objects().fetchCastObject({
|
|
1011
|
-
// objectId: inputs.marketId,
|
|
1012
|
-
// objectFromSuiObjectResponse: (data) =>
|
|
1013
|
-
// Casting.perpetuals.clearingHouseFromOnChain(
|
|
1014
|
-
// data,
|
|
1015
|
-
// collateralCoinType
|
|
1016
|
-
// ),
|
|
1017
|
-
// });
|
|
1018
|
-
// };
|
|
1019
|
-
// =========================================================================
|
|
1020
|
-
// Events
|
|
1021
|
-
// =========================================================================
|
|
1022
|
-
fetchAccountCollateralHistory(inputs) {
|
|
1023
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1024
|
-
const { accountCapId, cursor, limit } = inputs;
|
|
1025
|
-
const response = yield this.Provider.indexerCaller.fetchIndexer(`perpetuals/accounts/collateral-history`, {
|
|
1026
|
-
account_id: utils_1.Helpers.addLeadingZeroesToType(accountCapId),
|
|
1027
|
-
timestamp_before_ms: cursor !== null && cursor !== void 0 ? cursor : new Date().valueOf(),
|
|
1028
|
-
limit: limit !== null && limit !== void 0 ? limit : PerpetualsApi.constants.defaultLimitStepSize,
|
|
1029
|
-
}, undefined, undefined, undefined, true);
|
|
1030
|
-
const collateralChanges = response.map((data) => (Object.assign({ eventName: data.event_type, timestamp: data.timestamp, txDigest: data.tx_digest }, (data.event_type === "Deposit" ||
|
|
1031
|
-
data.event_type === "Withdraw" ||
|
|
1032
|
-
data.event_type === "Allocate" ||
|
|
1033
|
-
data.event_type === "Deallocate"
|
|
1034
|
-
? {
|
|
1035
|
-
collateralChange: BigInt(data.collateral_change),
|
|
1036
|
-
}
|
|
1037
|
-
: {
|
|
1038
|
-
collateralChangeUsd: Number(data.collateral_change_usd),
|
|
1039
|
-
}))));
|
|
1040
|
-
return {
|
|
1041
|
-
collateralChanges,
|
|
1042
|
-
// TODO: move `nextCursor` finding pattern to helper ?
|
|
1043
|
-
nextCursor: collateralChanges.length > 0
|
|
1044
|
-
? collateralChanges[collateralChanges.length - 1].timestamp
|
|
1045
|
-
: undefined,
|
|
1046
|
-
};
|
|
1047
|
-
});
|
|
1048
|
-
}
|
|
1049
|
-
fetchAccountOrderHistory(inputs) {
|
|
1050
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1051
|
-
const { accountCapId, cursor, limit } = inputs;
|
|
1052
|
-
const response = yield this.Provider.indexerCaller.fetchIndexer(`perpetuals/accounts/trade-history`, {
|
|
1053
|
-
account_id: utils_1.Helpers.addLeadingZeroesToType(accountCapId),
|
|
1054
|
-
timestamp_before_ms: cursor !== null && cursor !== void 0 ? cursor : new Date().valueOf(),
|
|
1055
|
-
limit: limit !== null && limit !== void 0 ? limit : PerpetualsApi.constants.defaultLimitStepSize,
|
|
1056
|
-
}, undefined, undefined, undefined, true);
|
|
1057
|
-
const trades = response.map((data) => (Object.assign(Object.assign({ eventName: data.event_type, timestamp: data.timestamp, txDigest: data.tx_digest, marketId: utils_1.Helpers.addLeadingZeroesToType(data.ch_id), side: data.is_ask
|
|
1058
|
-
? perpetualsTypes_1.PerpetualsOrderSide.Ask
|
|
1059
|
-
: perpetualsTypes_1.PerpetualsOrderSide.Bid }, (data.event_type === "Canceled" ||
|
|
1060
|
-
data.event_type === "Posted" ||
|
|
1061
|
-
data.event_type === "FilledMaker"
|
|
1062
|
-
? {
|
|
1063
|
-
sizeLots: BigInt(data.size),
|
|
1064
|
-
}
|
|
1065
|
-
: {
|
|
1066
|
-
size: Number(data.size),
|
|
1067
|
-
})), (data.event_type === "Canceled" ||
|
|
1068
|
-
data.event_type === "Posted" ||
|
|
1069
|
-
data.event_type === "FilledMaker"
|
|
1070
|
-
? {
|
|
1071
|
-
orderPrice: BigInt(data.price),
|
|
1072
|
-
}
|
|
1073
|
-
: {
|
|
1074
|
-
price: Number(data.price),
|
|
1075
|
-
}))));
|
|
1076
|
-
return {
|
|
1077
|
-
trades,
|
|
1078
|
-
// TODO: move `nextCursor` finding pattern to helper ?
|
|
1079
|
-
nextCursor: trades.length > 0
|
|
1080
|
-
? trades[trades.length - 1].timestamp
|
|
1081
|
-
: undefined,
|
|
1082
|
-
};
|
|
1083
|
-
});
|
|
1084
|
-
}
|
|
1085
|
-
fetchMarketTradeHistory(inputs) {
|
|
1086
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1087
|
-
const { marketId, cursor, limit } = inputs;
|
|
1088
|
-
const response = yield this.Provider.indexerCaller.fetchIndexer(`perpetuals/markets/trade-history`, {
|
|
1089
|
-
ch_id: utils_1.Helpers.addLeadingZeroesToType(marketId),
|
|
1090
|
-
timestamp_before_ms: cursor !== null && cursor !== void 0 ? cursor : new Date().valueOf(),
|
|
1091
|
-
limit: limit !== null && limit !== void 0 ? limit : PerpetualsApi.constants.defaultLimitStepSize,
|
|
1092
|
-
}, undefined, undefined, undefined, true);
|
|
1093
|
-
const trades = response.map((data) => ({
|
|
1094
|
-
timestamp: data.timestamp,
|
|
1095
|
-
txDigest: data.tx_digest,
|
|
1096
|
-
side: data.is_ask
|
|
1097
|
-
? perpetualsTypes_1.PerpetualsOrderSide.Ask
|
|
1098
|
-
: perpetualsTypes_1.PerpetualsOrderSide.Bid,
|
|
1099
|
-
sizeFilled: data.size_filled,
|
|
1100
|
-
orderPrice: data.order_price,
|
|
1101
|
-
}));
|
|
1102
|
-
return {
|
|
1103
|
-
trades,
|
|
1104
|
-
nextCursor: trades.length > 0
|
|
1105
|
-
? trades[trades.length - 1].timestamp
|
|
1106
|
-
: undefined,
|
|
1107
|
-
};
|
|
1108
|
-
});
|
|
1109
|
-
}
|
|
1110
|
-
// =========================================================================
|
|
1111
|
-
// Indexer Data
|
|
1112
|
-
// =========================================================================
|
|
1113
|
-
fetchMarket24hrVolume(inputs) {
|
|
1114
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1115
|
-
const { marketId } = inputs;
|
|
1116
|
-
const response = yield this.Provider.indexerCaller.fetchIndexer(`perpetuals/markets/${marketId}/24hr-volume`, undefined, undefined, undefined, undefined, true);
|
|
1117
|
-
return {
|
|
1118
|
-
volumeBaseAssetAmount: response.market_volume,
|
|
1119
|
-
volumeUsd: response.market_volume_usd,
|
|
1120
|
-
};
|
|
1121
|
-
});
|
|
1122
|
-
}
|
|
1123
|
-
fetchMarketPrice24hrsAgo(inputs) {
|
|
1124
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1125
|
-
const { marketId } = inputs;
|
|
1126
|
-
dayjs_1.default.extend(duration_1.default);
|
|
1127
|
-
const timestamp = (0, dayjs_1.default)().valueOf() - dayjs_1.default.duration(24, "hours").asMilliseconds();
|
|
1128
|
-
const response = yield this.Provider.indexerCaller.fetchIndexer(`perpetuals/markets/${marketId}/first-historical-price`, undefined, {
|
|
1129
|
-
timestamp,
|
|
1130
|
-
});
|
|
1131
|
-
if (response.length === 0)
|
|
1132
|
-
return 0;
|
|
1133
|
-
return response[0].bookPrice;
|
|
1134
|
-
});
|
|
1135
|
-
}
|
|
1136
631
|
}
|
|
1137
632
|
exports.PerpetualsApi = PerpetualsApi;
|
|
1138
633
|
// =========================================================================
|
|
@@ -1146,237 +641,7 @@ PerpetualsApi.constants = {
|
|
|
1146
641
|
clearingHouse: "clearing_house",
|
|
1147
642
|
account: "account",
|
|
1148
643
|
},
|
|
1149
|
-
defaultLimitStepSize: 256,
|
|
1150
644
|
};
|
|
1151
|
-
// public fetchExecutionPrice = async (
|
|
1152
|
-
// inputs: ApiPerpetualsExecutionPriceBody & {
|
|
1153
|
-
// collateralCoinType: CoinType;
|
|
1154
|
-
// marketId: PerpetualsMarketId;
|
|
1155
|
-
// }
|
|
1156
|
-
// ): Promise<ApiPerpetualsExecutionPriceResponse> => {
|
|
1157
|
-
// const {
|
|
1158
|
-
// // collateral,
|
|
1159
|
-
// collateralCoinType,
|
|
1160
|
-
// marketId,
|
|
1161
|
-
// side,
|
|
1162
|
-
// size,
|
|
1163
|
-
// price,
|
|
1164
|
-
// lotSize,
|
|
1165
|
-
// basePriceFeedId,
|
|
1166
|
-
// collateralPriceFeedId,
|
|
1167
|
-
// } = inputs;
|
|
1168
|
-
// // TODO: change this
|
|
1169
|
-
// const collateral = BigInt(1000000000000000);
|
|
1170
|
-
// // const accountCapId = perpetualsBcsRegistry
|
|
1171
|
-
// // .ser(`Account<${collateralCoinType}>`, {
|
|
1172
|
-
// // id: {
|
|
1173
|
-
// // id: {
|
|
1174
|
-
// // bytes: "0x0000000000000000000000000000000000000000000000000000000000000321",
|
|
1175
|
-
// // },
|
|
1176
|
-
// // },
|
|
1177
|
-
// // accountId: 0,
|
|
1178
|
-
// // collateral,
|
|
1179
|
-
// // })
|
|
1180
|
-
// // .toBytes();
|
|
1181
|
-
// const depositCoinBytes = perpetualsRegistry.Coin.serialize({
|
|
1182
|
-
// id: "0x0000000000000000000000000000000000000000000000000000000000000123",
|
|
1183
|
-
// balance: {
|
|
1184
|
-
// value: BigInt(1000000000000000),
|
|
1185
|
-
// },
|
|
1186
|
-
// }).toBytes();
|
|
1187
|
-
// const walletAddress = InspectionsApiHelpers.constants.devInspectSigner;
|
|
1188
|
-
// const tx = new Transaction();
|
|
1189
|
-
// tx.setSender(walletAddress);
|
|
1190
|
-
// const accountCapId = this.createAccountTx({
|
|
1191
|
-
// ...inputs,
|
|
1192
|
-
// tx,
|
|
1193
|
-
// });
|
|
1194
|
-
// this.depositCollateralTx({
|
|
1195
|
-
// tx,
|
|
1196
|
-
// collateralCoinType,
|
|
1197
|
-
// accountCapId,
|
|
1198
|
-
// coinBytes: depositCoinBytes,
|
|
1199
|
-
// });
|
|
1200
|
-
// const { sessionPotatoId } = this.createTxAndStartSession({
|
|
1201
|
-
// tx,
|
|
1202
|
-
// accountCapId,
|
|
1203
|
-
// collateralCoinType,
|
|
1204
|
-
// marketId,
|
|
1205
|
-
// walletAddress,
|
|
1206
|
-
// basePriceFeedId,
|
|
1207
|
-
// collateralPriceFeedId,
|
|
1208
|
-
// collateralChange: collateral,
|
|
1209
|
-
// hasPosition: false,
|
|
1210
|
-
// });
|
|
1211
|
-
// this.placeLimitOrderTx({
|
|
1212
|
-
// tx,
|
|
1213
|
-
// side,
|
|
1214
|
-
// size,
|
|
1215
|
-
// collateralCoinType,
|
|
1216
|
-
// sessionPotatoId,
|
|
1217
|
-
// orderType: PerpetualsOrderType.Standard,
|
|
1218
|
-
// price:
|
|
1219
|
-
// price ??
|
|
1220
|
-
// (side === PerpetualsOrderSide.Bid
|
|
1221
|
-
// ? BigInt("0x7FFFFFFFFFFFFFFF") // 2^63 - 1
|
|
1222
|
-
// : BigInt(1)),
|
|
1223
|
-
// });
|
|
1224
|
-
// this.getHotPotatoFieldsTx({
|
|
1225
|
-
// tx,
|
|
1226
|
-
// collateralCoinType,
|
|
1227
|
-
// sessionPotatoId,
|
|
1228
|
-
// });
|
|
1229
|
-
// this.endSessionAndTransferAccount({
|
|
1230
|
-
// ...inputs,
|
|
1231
|
-
// tx,
|
|
1232
|
-
// sessionPotatoId,
|
|
1233
|
-
// walletAddress,
|
|
1234
|
-
// collateralChange: BigInt(0),
|
|
1235
|
-
// });
|
|
1236
|
-
// const { events } =
|
|
1237
|
-
// await this.Provider.Inspections().fetchAllBytesFromTx({
|
|
1238
|
-
// tx,
|
|
1239
|
-
// });
|
|
1240
|
-
// const filledTakerEvent = EventsApiHelpers.findCastEventOrUndefined({
|
|
1241
|
-
// events,
|
|
1242
|
-
// eventType: this.eventTypes.filledTakerOrder,
|
|
1243
|
-
// castFunction: Casting.perpetuals.filledTakerOrderEventFromOnChain,
|
|
1244
|
-
// });
|
|
1245
|
-
// const sizeNum = lotSize * Math.abs(Number(size));
|
|
1246
|
-
// if (!filledTakerEvent) {
|
|
1247
|
-
// return {
|
|
1248
|
-
// executionPrice: 0,
|
|
1249
|
-
// sizeFilled: 0,
|
|
1250
|
-
// sizePosted: sizeNum,
|
|
1251
|
-
// fills: [],
|
|
1252
|
-
// };
|
|
1253
|
-
// }
|
|
1254
|
-
// const filledOrderEvents =
|
|
1255
|
-
// Aftermath.helpers.events.findCastEventsOrUndefined({
|
|
1256
|
-
// events,
|
|
1257
|
-
// eventType: this.eventTypes.filledTakerOrder,
|
|
1258
|
-
// castFunction:
|
|
1259
|
-
// Casting.perpetuals.filledTakerOrderEventFromOnChain,
|
|
1260
|
-
// });
|
|
1261
|
-
// const fills: PerpetualsFilledOrderData[] = filledOrderEvents.map(
|
|
1262
|
-
// (event) => {
|
|
1263
|
-
// const size = Math.abs(
|
|
1264
|
-
// Casting.IFixed.numberFromIFixed(event.baseAssetDelta)
|
|
1265
|
-
// );
|
|
1266
|
-
// const sizeUsd = Math.abs(
|
|
1267
|
-
// Casting.IFixed.numberFromIFixed(event.quoteAssetDelta)
|
|
1268
|
-
// );
|
|
1269
|
-
// const price = sizeUsd / size;
|
|
1270
|
-
// return {
|
|
1271
|
-
// size,
|
|
1272
|
-
// price,
|
|
1273
|
-
// };
|
|
1274
|
-
// }
|
|
1275
|
-
// );
|
|
1276
|
-
// const executionPrice = Perpetuals.calcEntryPrice(filledTakerEvent);
|
|
1277
|
-
// const sizeFilled = Math.abs(
|
|
1278
|
-
// Casting.IFixed.numberFromIFixed(filledTakerEvent.baseAssetDelta)
|
|
1279
|
-
// );
|
|
1280
|
-
// const sizePosted = sizeNum - sizeFilled;
|
|
1281
|
-
// return {
|
|
1282
|
-
// executionPrice,
|
|
1283
|
-
// sizeFilled,
|
|
1284
|
-
// sizePosted,
|
|
1285
|
-
// fills,
|
|
1286
|
-
// };
|
|
1287
|
-
// // const { fillReceipts, postReceipt } =
|
|
1288
|
-
// // await this.fetchMarketOrderReceipts(inputs);
|
|
1289
|
-
// // const sizePosted = postReceipt !== undefined ? postReceipt.size : 0;
|
|
1290
|
-
// // if (fillReceipts.length <= 0)
|
|
1291
|
-
// // return price !== undefined
|
|
1292
|
-
// // ? // simulating limit order
|
|
1293
|
-
// // {
|
|
1294
|
-
// // executionPrice: Perpetuals.orderPriceToPrice({
|
|
1295
|
-
// // orderPrice: price,
|
|
1296
|
-
// // lotSize,
|
|
1297
|
-
// // tickSize,
|
|
1298
|
-
// // }),
|
|
1299
|
-
// // sizeFilled: 0,
|
|
1300
|
-
// // sizePosted: Number(sizePosted),
|
|
1301
|
-
// // }
|
|
1302
|
-
// // : // simulating market order
|
|
1303
|
-
// // {
|
|
1304
|
-
// // executionPrice: 0,
|
|
1305
|
-
// // sizeFilled: 0,
|
|
1306
|
-
// // sizePosted: 0,
|
|
1307
|
-
// // };
|
|
1308
|
-
// // const sizeFilled = Helpers.sumBigInt(
|
|
1309
|
-
// // fillReceipts.map((receipt) => receipt.size)
|
|
1310
|
-
// // );
|
|
1311
|
-
// // const executionPrice = fillReceipts.reduce((acc, receipt) => {
|
|
1312
|
-
// // const orderPrice = PerpetualsOrderUtils.price(
|
|
1313
|
-
// // receipt.orderId,
|
|
1314
|
-
// // inputs.side === PerpetualsOrderSide.Ask
|
|
1315
|
-
// // ? PerpetualsOrderSide.Bid
|
|
1316
|
-
// // : PerpetualsOrderSide.Ask
|
|
1317
|
-
// // );
|
|
1318
|
-
// // const orderPriceNum = Perpetuals.orderPriceToPrice({
|
|
1319
|
-
// // orderPrice,
|
|
1320
|
-
// // lotSize,
|
|
1321
|
-
// // tickSize,
|
|
1322
|
-
// // });
|
|
1323
|
-
// // return (
|
|
1324
|
-
// // acc +
|
|
1325
|
-
// // orderPriceNum * (Number(receipt.size) / Number(sizeFilled))
|
|
1326
|
-
// // );
|
|
1327
|
-
// // }, 0);
|
|
1328
|
-
// // return {
|
|
1329
|
-
// // executionPrice,
|
|
1330
|
-
// // sizeFilled: Number(sizeFilled),
|
|
1331
|
-
// // sizePosted: Number(sizePosted),
|
|
1332
|
-
// // };
|
|
1333
|
-
// };
|
|
1334
|
-
// private createTxAndStartSession = (inputs: {
|
|
1335
|
-
// tx?: Transaction;
|
|
1336
|
-
// collateralCoinType: CoinType;
|
|
1337
|
-
// accountCapId: ObjectId | TransactionArgument;
|
|
1338
|
-
// marketId: PerpetualsMarketId;
|
|
1339
|
-
// marketInitialSharedVersion: ObjectVersion;
|
|
1340
|
-
// basePriceFeedId: ObjectId;
|
|
1341
|
-
// collateralPriceFeedId: ObjectId;
|
|
1342
|
-
// walletAddress: SuiAddress;
|
|
1343
|
-
// collateralChange: Balance;
|
|
1344
|
-
// hasPosition: boolean;
|
|
1345
|
-
// }) => {
|
|
1346
|
-
// const { collateralChange, walletAddress, hasPosition } = inputs;
|
|
1347
|
-
// const { tx: inputsTx, ...nonTxInputs } = inputs;
|
|
1348
|
-
// const tx = inputsTx ?? new Transaction();
|
|
1349
|
-
// tx.setSender(walletAddress);
|
|
1350
|
-
// if (!hasPosition) {
|
|
1351
|
-
// this.createMarketPositionTx({
|
|
1352
|
-
// ...nonTxInputs,
|
|
1353
|
-
// tx,
|
|
1354
|
-
// });
|
|
1355
|
-
// }
|
|
1356
|
-
// if (collateralChange > BigInt(0)) {
|
|
1357
|
-
// this.allocateCollateralTx({
|
|
1358
|
-
// ...nonTxInputs,
|
|
1359
|
-
// tx,
|
|
1360
|
-
// amount: collateralChange,
|
|
1361
|
-
// });
|
|
1362
|
-
// }
|
|
1363
|
-
// const sessionPotatoId = this.startSessionTx({
|
|
1364
|
-
// ...nonTxInputs,
|
|
1365
|
-
// tx,
|
|
1366
|
-
// });
|
|
1367
|
-
// return { tx, sessionPotatoId };
|
|
1368
|
-
// };
|
|
1369
|
-
// private endSessionAndShareMarket = (inputs: {
|
|
1370
|
-
// tx: Transaction;
|
|
1371
|
-
// collateralCoinType: CoinType;
|
|
1372
|
-
// sessionPotatoId: ObjectId | TransactionArgument;
|
|
1373
|
-
// }) => {
|
|
1374
|
-
// const marketId = this.endSessionTx(inputs);
|
|
1375
|
-
// this.shareClearingHouseTx({
|
|
1376
|
-
// ...inputs,
|
|
1377
|
-
// marketId,
|
|
1378
|
-
// });
|
|
1379
|
-
// };
|
|
1380
645
|
// =========================================================================
|
|
1381
646
|
// Public Static Helpers
|
|
1382
647
|
// =========================================================================
|