@deriverse/kit 1.0.38 → 1.0.41
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/auto_buffer.d.ts +0 -2
- package/dist/auto_data.d.ts +0 -2
- package/dist/constants.d.ts +12 -0
- package/dist/constants.js +26 -0
- package/dist/engine/account-helpers.d.ts +59 -0
- package/dist/engine/account-helpers.js +177 -0
- package/dist/engine/account-helpers.test.d.ts +1 -0
- package/dist/engine/account-helpers.test.js +199 -0
- package/dist/engine/client-queries.d.ts +36 -0
- package/dist/engine/client-queries.js +498 -0
- package/dist/engine/client-queries.test.d.ts +1 -0
- package/dist/engine/client-queries.test.js +341 -0
- package/dist/engine/context-builders.d.ts +16 -0
- package/dist/engine/context-builders.js +158 -0
- package/dist/engine/context-builders.test.d.ts +1 -0
- package/dist/engine/context-builders.test.js +156 -0
- package/dist/engine/index.d.ts +101 -0
- package/dist/engine/index.js +745 -0
- package/dist/engine/index.test.d.ts +1 -0
- package/dist/engine/index.test.js +663 -0
- package/dist/engine/logs-decoder.d.ts +18 -0
- package/dist/engine/logs-decoder.js +414 -0
- package/dist/engine/logs-decoder.test.d.ts +1 -0
- package/dist/engine/logs-decoder.test.js +836 -0
- package/dist/engine/perp-instructions.d.ts +68 -0
- package/dist/engine/perp-instructions.js +478 -0
- package/dist/engine/perp-instructions.test.d.ts +1 -0
- package/dist/engine/perp-instructions.test.js +296 -0
- package/dist/engine/spot-instructions.d.ts +52 -0
- package/dist/engine/spot-instructions.js +376 -0
- package/dist/engine/spot-instructions.test.d.ts +1 -0
- package/dist/engine/spot-instructions.test.js +221 -0
- package/dist/engine/utils.d.ts +23 -0
- package/dist/engine/utils.js +329 -0
- package/dist/engine/utils.test.d.ts +1 -0
- package/dist/engine/utils.test.js +120 -0
- package/dist/index.d.ts +6 -247
- package/dist/index.js +14 -2925
- package/dist/instruction_models.d.ts +0 -2
- package/dist/instruction_models.js +39 -40
- package/dist/logs_models.d.ts +0 -2
- package/dist/types/engine-args.d.ts +32 -0
- package/dist/types/engine-args.js +2 -0
- package/dist/types/enums.d.ts +43 -0
- package/dist/{types.js → types/enums.js} +3 -5
- package/dist/types/index.d.ts +8 -0
- package/dist/types/index.js +38 -0
- package/dist/types/log-message.d.ts +2 -0
- package/dist/types/log-message.js +2 -0
- package/dist/types/responses.d.ts +248 -0
- package/dist/types/responses.js +2 -0
- package/dist/types/schemas.d.ts +165 -0
- package/dist/types/schemas.js +254 -0
- package/dist/types/schemas.test.d.ts +1 -0
- package/dist/types/schemas.test.js +94 -0
- package/dist/utils.d.ts +0 -0
- package/dist/utils.js +1 -0
- package/package.json +26 -6
- package/dist/types.d.ts +0 -565
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.getClientData = getClientData;
|
|
13
|
+
exports.getClientSpotOrdersInfo = getClientSpotOrdersInfo;
|
|
14
|
+
exports.getClientPerpOrdersInfo = getClientPerpOrdersInfo;
|
|
15
|
+
exports.getClientSpotOrders = getClientSpotOrders;
|
|
16
|
+
exports.getClientPerpOrders = getClientPerpOrders;
|
|
17
|
+
const kit_1 = require("@solana/kit");
|
|
18
|
+
const buffer_1 = require("buffer");
|
|
19
|
+
const enums_1 = require("../types/enums");
|
|
20
|
+
const constants_1 = require("../constants");
|
|
21
|
+
const utils_1 = require("./utils");
|
|
22
|
+
const structure_models_1 = require("../structure_models");
|
|
23
|
+
const account_helpers_1 = require("./account-helpers");
|
|
24
|
+
/**
|
|
25
|
+
* Unpack client accounts
|
|
26
|
+
*/
|
|
27
|
+
function getClientData(ctx) {
|
|
28
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
29
|
+
if (!ctx.clientPrimaryAccount) {
|
|
30
|
+
throw new Error('Client primary account not found');
|
|
31
|
+
}
|
|
32
|
+
if (!ctx.clientCommunityAccount) {
|
|
33
|
+
throw new Error('Client community account not found');
|
|
34
|
+
}
|
|
35
|
+
const infos = yield ctx.rpc
|
|
36
|
+
.getMultipleAccounts([ctx.clientPrimaryAccount, ctx.clientCommunityAccount], {
|
|
37
|
+
commitment: ctx.commitment,
|
|
38
|
+
encoding: 'base64',
|
|
39
|
+
})
|
|
40
|
+
.send();
|
|
41
|
+
if (infos.value == null || infos.value[0] == null || infos.value[1] == null) {
|
|
42
|
+
throw new Error('GetClientData: GetAccountInfo failed');
|
|
43
|
+
}
|
|
44
|
+
const clientPrimaryAccountHeaderModel = structure_models_1.ClientPrimaryAccountHeaderModel.fromBuffer(infos.value[0].data);
|
|
45
|
+
let clientCommunityAccountHeaderModel = structure_models_1.ClientCommunityAccountHeaderModel.fromBuffer(infos.value[1].data);
|
|
46
|
+
const primaryData = buffer_1.Buffer.from((0, kit_1.getBase64Encoder)().encode(infos.value[0].data[0]));
|
|
47
|
+
let tokens = new Map();
|
|
48
|
+
let lp = new Map();
|
|
49
|
+
let spot = new Map();
|
|
50
|
+
let perp = new Map();
|
|
51
|
+
for (let i = 0; i < clientPrimaryAccountHeaderModel.assetsCount; ++i) {
|
|
52
|
+
const offset = structure_models_1.ClientPrimaryAccountHeaderModel.LENGTH + i * 16;
|
|
53
|
+
const assetInfo = primaryData.readUint32LE(offset);
|
|
54
|
+
const tag = assetInfo >> 28;
|
|
55
|
+
const id = assetInfo & 0xfffffff;
|
|
56
|
+
if (tag == 1) {
|
|
57
|
+
tokens.set(id, {
|
|
58
|
+
tokenId: id,
|
|
59
|
+
amount: Number(primaryData.readBigInt64LE(offset + 8)) / (0, utils_1.tokenDec)(ctx.tokens, id, ctx.uiNumbers),
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
else if (tag == 2) {
|
|
63
|
+
lp.set(id, { instrId: id, amount: Number(primaryData.readBigInt64LE(offset + 8)) / constants_1.lpDec });
|
|
64
|
+
}
|
|
65
|
+
else if (tag == 3) {
|
|
66
|
+
const clientId = primaryData.readUint32LE(offset + 4);
|
|
67
|
+
const slot = primaryData.readUint32LE(offset + 8);
|
|
68
|
+
spot.set(id, { instrId: id, clientId: clientId, slot: slot });
|
|
69
|
+
}
|
|
70
|
+
else if (tag == 4) {
|
|
71
|
+
const clientId = primaryData.readUint32LE(offset + 4);
|
|
72
|
+
const slot = primaryData.readUint32LE(offset + 8);
|
|
73
|
+
perp.set(id, { instrId: id, clientId: clientId, slot: slot });
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
let clientCommunityRecords = new Map();
|
|
77
|
+
for (let i = 0; i < clientCommunityAccountHeaderModel.count; ++i) {
|
|
78
|
+
const offset = structure_models_1.ClientCommunityAccountHeaderModel.LENGTH + structure_models_1.ClientCommunityRecordModel.LENGTH * i;
|
|
79
|
+
let clientCommunityRecordModel = structure_models_1.ClientCommunityRecordModel.fromBuffer(infos.value[1].data, offset);
|
|
80
|
+
const crncyDec = (0, utils_1.tokenDec)(ctx.tokens, clientCommunityRecordModel.crncyTokenId, ctx.uiNumbers);
|
|
81
|
+
clientCommunityRecordModel.dividendsValue /= crncyDec;
|
|
82
|
+
clientCommunityRecordModel.feesPrepayment /= crncyDec;
|
|
83
|
+
clientCommunityRecordModel.refPayments /= crncyDec;
|
|
84
|
+
clientCommunityRecords.set(clientCommunityRecordModel.crncyTokenId, clientCommunityRecordModel);
|
|
85
|
+
}
|
|
86
|
+
const drvsDec = (0, utils_1.tokenDec)(ctx.tokens, 0, ctx.uiNumbers);
|
|
87
|
+
clientCommunityAccountHeaderModel.drvsTokens /= drvsDec;
|
|
88
|
+
clientCommunityAccountHeaderModel.currentVotingTokens /= drvsDec;
|
|
89
|
+
clientCommunityAccountHeaderModel.lastVotingTokens /= drvsDec;
|
|
90
|
+
return {
|
|
91
|
+
clientId: clientPrimaryAccountHeaderModel.id,
|
|
92
|
+
mask: clientPrimaryAccountHeaderModel.mask,
|
|
93
|
+
points: clientPrimaryAccountHeaderModel.points,
|
|
94
|
+
slot: clientPrimaryAccountHeaderModel.slot,
|
|
95
|
+
spotTrades: clientPrimaryAccountHeaderModel.spotTrades,
|
|
96
|
+
lpTrades: clientPrimaryAccountHeaderModel.lpTrades,
|
|
97
|
+
perpTrades: clientPrimaryAccountHeaderModel.perpTrades,
|
|
98
|
+
tokens: tokens,
|
|
99
|
+
lp: lp,
|
|
100
|
+
spot: spot,
|
|
101
|
+
perp: perp,
|
|
102
|
+
refProgram: {
|
|
103
|
+
address: clientPrimaryAccountHeaderModel.refAddress,
|
|
104
|
+
expiration: clientPrimaryAccountHeaderModel.refProgramExpiration,
|
|
105
|
+
clientId: clientPrimaryAccountHeaderModel.refClientId,
|
|
106
|
+
discount: clientPrimaryAccountHeaderModel.refProgramDiscount,
|
|
107
|
+
ratio: clientPrimaryAccountHeaderModel.refProgramRatio,
|
|
108
|
+
},
|
|
109
|
+
community: { header: clientCommunityAccountHeaderModel, data: clientCommunityRecords },
|
|
110
|
+
refLinks: [
|
|
111
|
+
{
|
|
112
|
+
id: clientPrimaryAccountHeaderModel.firstRefLinkId,
|
|
113
|
+
expiration: clientPrimaryAccountHeaderModel.firstRefLinkExpiration,
|
|
114
|
+
discount: clientPrimaryAccountHeaderModel.firstRefLinkDiscount,
|
|
115
|
+
ratio: clientPrimaryAccountHeaderModel.firstRefLinkRatio,
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
id: clientPrimaryAccountHeaderModel.secondRefLinkId,
|
|
119
|
+
expiration: clientPrimaryAccountHeaderModel.secondRefLinkExpiration,
|
|
120
|
+
discount: clientPrimaryAccountHeaderModel.secondRefLinkDiscount,
|
|
121
|
+
ratio: clientPrimaryAccountHeaderModel.secondRefLinkRatio,
|
|
122
|
+
},
|
|
123
|
+
],
|
|
124
|
+
};
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Get general information about open orders in particular instrument (spot)
|
|
129
|
+
*/
|
|
130
|
+
function getClientSpotOrdersInfo(ctx, args) {
|
|
131
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
132
|
+
const instr = ctx.instruments.get(args.instrId);
|
|
133
|
+
if (!instr) {
|
|
134
|
+
throw new Error('Invalid Instrument ID');
|
|
135
|
+
}
|
|
136
|
+
const clientInfosAccount = yield (0, account_helpers_1.getInstrAccountByTag)(ctx, {
|
|
137
|
+
assetTokenId: instr.header.assetTokenId,
|
|
138
|
+
crncyTokenId: instr.header.crncyTokenId,
|
|
139
|
+
tag: enums_1.AccountType.SPOT_CLIENT_INFOS,
|
|
140
|
+
});
|
|
141
|
+
const clientInfos2Account = yield (0, account_helpers_1.getInstrAccountByTag)(ctx, {
|
|
142
|
+
assetTokenId: instr.header.assetTokenId,
|
|
143
|
+
crncyTokenId: instr.header.crncyTokenId,
|
|
144
|
+
tag: enums_1.AccountType.SPOT_CLIENT_INFOS2,
|
|
145
|
+
});
|
|
146
|
+
const infos = yield ctx.rpc
|
|
147
|
+
.getMultipleAccounts([clientInfosAccount, clientInfos2Account], {
|
|
148
|
+
commitment: ctx.commitment,
|
|
149
|
+
encoding: 'base64',
|
|
150
|
+
dataSlice: { offset: structure_models_1.SpotTradeAccountHeaderModel.LENGTH + 32 * args.clientId, length: 32 },
|
|
151
|
+
})
|
|
152
|
+
.send();
|
|
153
|
+
if (infos.value[0] == null || infos.value[1] == null) {
|
|
154
|
+
throw new Error('Orders Info Not Found');
|
|
155
|
+
}
|
|
156
|
+
const data = buffer_1.Buffer.from((0, kit_1.getBase64Encoder)().encode(infos.value[0].data[0]));
|
|
157
|
+
const data1 = buffer_1.Buffer.from((0, kit_1.getBase64Encoder)().encode(infos.value[1].data[0]));
|
|
158
|
+
return {
|
|
159
|
+
contextSlot: Number(infos.context.slot),
|
|
160
|
+
bidSlot: data1.readUint32LE(structure_models_1.SpotClientInfo2Model.OFFSET_BID_SLOT),
|
|
161
|
+
askSlot: data1.readUint32LE(structure_models_1.SpotClientInfo2Model.OFFSET_ASK_SLOT),
|
|
162
|
+
bidsEntry: data.readUint16LE(structure_models_1.SpotClientInfoModel.OFFSET_BIDS_ENTRY),
|
|
163
|
+
bidsCount: data.readUint16LE(structure_models_1.SpotClientInfoModel.OFFSET_BIDS_ENTRY + 2),
|
|
164
|
+
asksEntry: data.readUint16LE(structure_models_1.SpotClientInfoModel.OFFSET_ASKS_ENTRY),
|
|
165
|
+
asksCount: data.readUint16LE(structure_models_1.SpotClientInfoModel.OFFSET_ASKS_ENTRY + 2),
|
|
166
|
+
tempAssetTokens: Number(data.readBigInt64LE(structure_models_1.SpotClientInfoModel.OFFSET_AVAIL_ASSET_TOKENS)) /
|
|
167
|
+
(0, utils_1.tokenDec)(ctx.tokens, instr.header.assetTokenId, ctx.uiNumbers),
|
|
168
|
+
tempCrncyTokens: Number(data.readBigInt64LE(structure_models_1.SpotClientInfoModel.OFFSET_AVAIL_CRNCY_TOKENS)) /
|
|
169
|
+
(0, utils_1.tokenDec)(ctx.tokens, instr.header.crncyTokenId, ctx.uiNumbers),
|
|
170
|
+
inOrdersAssetTokens: Number(data1.readBigInt64LE(structure_models_1.SpotClientInfo2Model.OFFSET_IN_ORDERS_ASSET_TOKENS)) /
|
|
171
|
+
(0, utils_1.tokenDec)(ctx.tokens, instr.header.assetTokenId, ctx.uiNumbers),
|
|
172
|
+
inOrdersCrncyTokens: Number(data1.readBigInt64LE(structure_models_1.SpotClientInfo2Model.OFFSET_IN_ORDERS_CRNCY_TOKENS)) /
|
|
173
|
+
(0, utils_1.tokenDec)(ctx.tokens, instr.header.crncyTokenId, ctx.uiNumbers),
|
|
174
|
+
};
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Get general information about open orders in particular instrument (perp)
|
|
179
|
+
*/
|
|
180
|
+
function getClientPerpOrdersInfo(ctx, args) {
|
|
181
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
182
|
+
const instr = ctx.instruments.get(args.instrId);
|
|
183
|
+
if (!instr) {
|
|
184
|
+
throw new Error('Invalid Instrument ID');
|
|
185
|
+
}
|
|
186
|
+
const clientInfosAccount = yield (0, account_helpers_1.getInstrAccountByTag)(ctx, {
|
|
187
|
+
assetTokenId: instr.header.assetTokenId,
|
|
188
|
+
crncyTokenId: instr.header.crncyTokenId,
|
|
189
|
+
tag: enums_1.AccountType.PERP_CLIENT_INFOS,
|
|
190
|
+
});
|
|
191
|
+
const clientInfos2Account = yield (0, account_helpers_1.getInstrAccountByTag)(ctx, {
|
|
192
|
+
assetTokenId: instr.header.assetTokenId,
|
|
193
|
+
crncyTokenId: instr.header.crncyTokenId,
|
|
194
|
+
tag: enums_1.AccountType.PERP_CLIENT_INFOS2,
|
|
195
|
+
});
|
|
196
|
+
const clientInfos3Account = yield (0, account_helpers_1.getInstrAccountByTag)(ctx, {
|
|
197
|
+
assetTokenId: instr.header.assetTokenId,
|
|
198
|
+
crncyTokenId: instr.header.crncyTokenId,
|
|
199
|
+
tag: enums_1.AccountType.PERP_CLIENT_INFOS3,
|
|
200
|
+
});
|
|
201
|
+
const clientInfos4Account = yield (0, account_helpers_1.getInstrAccountByTag)(ctx, {
|
|
202
|
+
assetTokenId: instr.header.assetTokenId,
|
|
203
|
+
crncyTokenId: instr.header.crncyTokenId,
|
|
204
|
+
tag: enums_1.AccountType.PERP_CLIENT_INFOS4,
|
|
205
|
+
});
|
|
206
|
+
const clientInfos5Account = yield (0, account_helpers_1.getInstrAccountByTag)(ctx, {
|
|
207
|
+
assetTokenId: instr.header.assetTokenId,
|
|
208
|
+
crncyTokenId: instr.header.crncyTokenId,
|
|
209
|
+
tag: enums_1.AccountType.PERP_CLIENT_INFOS5,
|
|
210
|
+
});
|
|
211
|
+
const infos = yield ctx.rpc
|
|
212
|
+
.getMultipleAccounts([clientInfosAccount, clientInfos2Account, clientInfos3Account, clientInfos4Account, clientInfos5Account], {
|
|
213
|
+
commitment: ctx.commitment,
|
|
214
|
+
encoding: 'base64',
|
|
215
|
+
dataSlice: { offset: structure_models_1.SpotTradeAccountHeaderModel.LENGTH + 32 * args.clientId, length: 32 },
|
|
216
|
+
})
|
|
217
|
+
.send();
|
|
218
|
+
if (infos.value[0] == null ||
|
|
219
|
+
infos.value[1] == null ||
|
|
220
|
+
infos.value[2] == null ||
|
|
221
|
+
infos.value[3] == null ||
|
|
222
|
+
infos.value[4] == null) {
|
|
223
|
+
throw new Error('Orders Info Not Found');
|
|
224
|
+
}
|
|
225
|
+
const clientInfoModel = structure_models_1.PerpClientInfoModel.fromBuffer(infos.value[0].data);
|
|
226
|
+
const clientInfo2Model = structure_models_1.PerpClientInfo2Model.fromBuffer(infos.value[1].data);
|
|
227
|
+
const clientInfo3Model = structure_models_1.PerpClientInfo3Model.fromBuffer(infos.value[2].data);
|
|
228
|
+
const clientInfo4Model = structure_models_1.PerpClientInfo4Model.fromBuffer(infos.value[3].data);
|
|
229
|
+
const clientInfo5Model = structure_models_1.PerpClientInfo5Model.fromBuffer(infos.value[4].data);
|
|
230
|
+
return {
|
|
231
|
+
contextSlot: Number(infos.context.slot),
|
|
232
|
+
bidSlot: clientInfo2Model.bidSlot,
|
|
233
|
+
askSlot: clientInfo2Model.askSlot,
|
|
234
|
+
bidsEntry: clientInfo3Model.bidsEntry & 0xffff,
|
|
235
|
+
bidsCount: clientInfo3Model.bidsEntry >> 16,
|
|
236
|
+
asksEntry: clientInfo3Model.asksEntry & 0xffff,
|
|
237
|
+
asksCount: clientInfo3Model.asksEntry >> 16,
|
|
238
|
+
perps: clientInfoModel.perps,
|
|
239
|
+
funds: clientInfoModel.funds,
|
|
240
|
+
inOrdersPerps: clientInfoModel.inOrdersPerps,
|
|
241
|
+
inOrdersFunds: clientInfoModel.inOrdersFunds,
|
|
242
|
+
fees: clientInfo3Model.fees,
|
|
243
|
+
rebates: clientInfo3Model.rebates,
|
|
244
|
+
result: clientInfo2Model.result,
|
|
245
|
+
cost: clientInfo2Model.cost,
|
|
246
|
+
mask: clientInfo2Model.mask,
|
|
247
|
+
socLossFunds: clientInfo4Model.socLossFunds,
|
|
248
|
+
fundingFunds: clientInfo5Model.fundingFunds,
|
|
249
|
+
lossCoverage: clientInfo4Model.lossCoverage,
|
|
250
|
+
};
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Get list of open orders (spot) in particular instrument
|
|
255
|
+
*/
|
|
256
|
+
function getClientSpotOrders(ctx, args) {
|
|
257
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
258
|
+
if (ctx.originalClientId === null) {
|
|
259
|
+
throw new Error('Original client ID not found');
|
|
260
|
+
}
|
|
261
|
+
const instr = ctx.instruments.get(args.instrId);
|
|
262
|
+
if (!instr) {
|
|
263
|
+
throw new Error('Invalid Instrument ID');
|
|
264
|
+
}
|
|
265
|
+
const bidOrdersAccount = yield (0, account_helpers_1.getInstrAccountByTag)(ctx, {
|
|
266
|
+
assetTokenId: instr.header.assetTokenId,
|
|
267
|
+
crncyTokenId: instr.header.crncyTokenId,
|
|
268
|
+
tag: enums_1.AccountType.SPOT_BID_ORDERS,
|
|
269
|
+
});
|
|
270
|
+
const askOrdersAccount = yield (0, account_helpers_1.getInstrAccountByTag)(ctx, {
|
|
271
|
+
assetTokenId: instr.header.assetTokenId,
|
|
272
|
+
crncyTokenId: instr.header.crncyTokenId,
|
|
273
|
+
tag: enums_1.AccountType.SPOT_ASK_ORDERS,
|
|
274
|
+
});
|
|
275
|
+
const assetTokenDec = (0, utils_1.tokenDec)(ctx.tokens, instr.header.assetTokenId, ctx.uiNumbers);
|
|
276
|
+
const crncyTokenDec = (0, utils_1.tokenDec)(ctx.tokens, instr.header.crncyTokenId, ctx.uiNumbers);
|
|
277
|
+
if (args.bidsCount > 1 && args.asksCount > 1) {
|
|
278
|
+
let infos = yield ctx.rpc
|
|
279
|
+
.getMultipleAccounts([bidOrdersAccount, askOrdersAccount], {
|
|
280
|
+
commitment: ctx.commitment,
|
|
281
|
+
encoding: 'base64',
|
|
282
|
+
})
|
|
283
|
+
.send();
|
|
284
|
+
if (infos.value[0] == null || infos.value[1] == null) {
|
|
285
|
+
throw new Error('Orders account not found');
|
|
286
|
+
}
|
|
287
|
+
let bids = (0, utils_1.getMultipleSpotOrders)(infos.value[0].data, args.bidsEntry, ctx.originalClientId);
|
|
288
|
+
for (let i = 0; i < bids.length; ++i) {
|
|
289
|
+
bids[i].qty /= assetTokenDec;
|
|
290
|
+
bids[i].sum /= crncyTokenDec;
|
|
291
|
+
}
|
|
292
|
+
let asks = (0, utils_1.getMultipleSpotOrders)(infos.value[1].data, args.asksEntry, ctx.originalClientId);
|
|
293
|
+
for (let i = 0; i < asks.length; ++i) {
|
|
294
|
+
asks[i].qty /= assetTokenDec;
|
|
295
|
+
asks[i].sum /= crncyTokenDec;
|
|
296
|
+
}
|
|
297
|
+
return { contextSlot: Number(infos.context.slot), bids: bids, asks: asks };
|
|
298
|
+
}
|
|
299
|
+
let bids = [];
|
|
300
|
+
let asks = [];
|
|
301
|
+
let bidContextSlot = 0;
|
|
302
|
+
let askContextSlot = 0;
|
|
303
|
+
if (args.bidsCount > 1) {
|
|
304
|
+
let info = yield ctx.rpc
|
|
305
|
+
.getAccountInfo(bidOrdersAccount, { commitment: ctx.commitment, encoding: 'base64' })
|
|
306
|
+
.send();
|
|
307
|
+
if (info.value == null)
|
|
308
|
+
throw new Error('Bid orders account not found');
|
|
309
|
+
bidContextSlot = Number(info.context.slot);
|
|
310
|
+
bids = (0, utils_1.getMultipleSpotOrders)(info.value.data, args.bidsEntry, ctx.originalClientId);
|
|
311
|
+
}
|
|
312
|
+
else if (args.bidsCount == 1) {
|
|
313
|
+
let info = yield ctx.rpc
|
|
314
|
+
.getAccountInfo(bidOrdersAccount, {
|
|
315
|
+
commitment: ctx.commitment,
|
|
316
|
+
encoding: 'base64',
|
|
317
|
+
dataSlice: { offset: args.bidsEntry * 64 + structure_models_1.SpotTradeAccountHeaderModel.LENGTH, length: 64 },
|
|
318
|
+
})
|
|
319
|
+
.send();
|
|
320
|
+
if (info.value == null)
|
|
321
|
+
throw new Error('Bid orders account not found');
|
|
322
|
+
const order = structure_models_1.OrderModel.fromBuffer(info.value.data);
|
|
323
|
+
if (order.origClientId == ctx.originalClientId) {
|
|
324
|
+
bids = [order];
|
|
325
|
+
}
|
|
326
|
+
bidContextSlot = Number(info.context.slot);
|
|
327
|
+
}
|
|
328
|
+
if (args.asksCount > 1) {
|
|
329
|
+
let info = yield ctx.rpc
|
|
330
|
+
.getAccountInfo(askOrdersAccount, { commitment: ctx.commitment, encoding: 'base64' })
|
|
331
|
+
.send();
|
|
332
|
+
if (info.value == null)
|
|
333
|
+
throw new Error('Ask orders account not found');
|
|
334
|
+
askContextSlot = Number(info.context.slot);
|
|
335
|
+
asks = (0, utils_1.getMultipleSpotOrders)(info.value.data, args.asksEntry, ctx.originalClientId);
|
|
336
|
+
}
|
|
337
|
+
else if (args.asksCount == 1) {
|
|
338
|
+
let info = yield ctx.rpc
|
|
339
|
+
.getAccountInfo(askOrdersAccount, {
|
|
340
|
+
commitment: ctx.commitment,
|
|
341
|
+
encoding: 'base64',
|
|
342
|
+
dataSlice: { offset: args.asksEntry * 64 + structure_models_1.SpotTradeAccountHeaderModel.LENGTH, length: 64 },
|
|
343
|
+
})
|
|
344
|
+
.send();
|
|
345
|
+
if (info.value == null)
|
|
346
|
+
throw new Error('Ask orders account not found');
|
|
347
|
+
const order = structure_models_1.OrderModel.fromBuffer(info.value.data);
|
|
348
|
+
if (order.origClientId == ctx.originalClientId) {
|
|
349
|
+
asks = [order];
|
|
350
|
+
}
|
|
351
|
+
askContextSlot = Number(info.context.slot);
|
|
352
|
+
}
|
|
353
|
+
for (let i = 0; i < bids.length; ++i) {
|
|
354
|
+
bids[i].qty /= assetTokenDec;
|
|
355
|
+
bids[i].sum /= crncyTokenDec;
|
|
356
|
+
}
|
|
357
|
+
for (let i = 0; i < asks.length; ++i) {
|
|
358
|
+
asks[i].qty /= assetTokenDec;
|
|
359
|
+
asks[i].sum /= crncyTokenDec;
|
|
360
|
+
}
|
|
361
|
+
let contextSlot = 0;
|
|
362
|
+
if (bidContextSlot > 0 || askContextSlot > 0) {
|
|
363
|
+
if (bidContextSlot == 0) {
|
|
364
|
+
contextSlot = askContextSlot;
|
|
365
|
+
}
|
|
366
|
+
else if (askContextSlot == 0) {
|
|
367
|
+
contextSlot = bidContextSlot;
|
|
368
|
+
}
|
|
369
|
+
else {
|
|
370
|
+
contextSlot = Math.min(bidContextSlot, askContextSlot);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
return { contextSlot: contextSlot, bids: bids, asks: asks };
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Get list of open orders (perp) in particular instrument
|
|
378
|
+
*/
|
|
379
|
+
function getClientPerpOrders(ctx, args) {
|
|
380
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
381
|
+
if (ctx.originalClientId === null) {
|
|
382
|
+
throw new Error('Original client ID not found');
|
|
383
|
+
}
|
|
384
|
+
const instr = ctx.instruments.get(args.instrId);
|
|
385
|
+
if (!instr) {
|
|
386
|
+
throw new Error('Invalid Instrument ID');
|
|
387
|
+
}
|
|
388
|
+
const bidOrdersAccount = yield (0, account_helpers_1.getInstrAccountByTag)(ctx, {
|
|
389
|
+
assetTokenId: instr.header.assetTokenId,
|
|
390
|
+
crncyTokenId: instr.header.crncyTokenId,
|
|
391
|
+
tag: enums_1.AccountType.PERP_BID_ORDERS,
|
|
392
|
+
});
|
|
393
|
+
const askOrdersAccount = yield (0, account_helpers_1.getInstrAccountByTag)(ctx, {
|
|
394
|
+
assetTokenId: instr.header.assetTokenId,
|
|
395
|
+
crncyTokenId: instr.header.crncyTokenId,
|
|
396
|
+
tag: enums_1.AccountType.PERP_ASK_ORDERS,
|
|
397
|
+
});
|
|
398
|
+
const assetTokenDec = (0, utils_1.tokenDec)(ctx.tokens, instr.header.assetTokenId, ctx.uiNumbers);
|
|
399
|
+
const crncyTokenDec = (0, utils_1.tokenDec)(ctx.tokens, instr.header.crncyTokenId, ctx.uiNumbers);
|
|
400
|
+
if (args.bidsCount > 1 && args.asksCount > 1) {
|
|
401
|
+
let infos = yield ctx.rpc
|
|
402
|
+
.getMultipleAccounts([bidOrdersAccount, askOrdersAccount], {
|
|
403
|
+
commitment: ctx.commitment,
|
|
404
|
+
encoding: 'base64',
|
|
405
|
+
})
|
|
406
|
+
.send();
|
|
407
|
+
if (infos.value[0] == null || infos.value[1] == null) {
|
|
408
|
+
throw new Error('Orders account not found');
|
|
409
|
+
}
|
|
410
|
+
let bids = (0, utils_1.getMultiplePerpOrders)(infos.value[0].data, args.bidsEntry, ctx.originalClientId);
|
|
411
|
+
for (let i = 0; i < bids.length; ++i) {
|
|
412
|
+
bids[i].qty /= assetTokenDec;
|
|
413
|
+
bids[i].sum /= crncyTokenDec;
|
|
414
|
+
}
|
|
415
|
+
let asks = (0, utils_1.getMultiplePerpOrders)(infos.value[1].data, args.asksEntry, ctx.originalClientId);
|
|
416
|
+
for (let i = 0; i < asks.length; ++i) {
|
|
417
|
+
asks[i].qty /= assetTokenDec;
|
|
418
|
+
asks[i].sum /= crncyTokenDec;
|
|
419
|
+
}
|
|
420
|
+
return { contextSlot: Number(infos.context.slot), bids: bids, asks: asks };
|
|
421
|
+
}
|
|
422
|
+
let bids = [];
|
|
423
|
+
let asks = [];
|
|
424
|
+
let bidContextSlot = 0;
|
|
425
|
+
let askContextSlot = 0;
|
|
426
|
+
if (args.bidsCount > 1) {
|
|
427
|
+
let info = yield ctx.rpc
|
|
428
|
+
.getAccountInfo(bidOrdersAccount, { commitment: ctx.commitment, encoding: 'base64' })
|
|
429
|
+
.send();
|
|
430
|
+
if (info.value == null)
|
|
431
|
+
throw new Error('Bid orders account not found');
|
|
432
|
+
bidContextSlot = Number(info.context.slot);
|
|
433
|
+
bids = (0, utils_1.getMultiplePerpOrders)(info.value.data, args.bidsEntry, ctx.originalClientId);
|
|
434
|
+
}
|
|
435
|
+
else if (args.bidsCount == 1) {
|
|
436
|
+
let info = yield ctx.rpc
|
|
437
|
+
.getAccountInfo(bidOrdersAccount, {
|
|
438
|
+
commitment: ctx.commitment,
|
|
439
|
+
encoding: 'base64',
|
|
440
|
+
dataSlice: { offset: args.bidsEntry * 64 + structure_models_1.PerpTradeAccountHeaderModel.LENGTH, length: 64 },
|
|
441
|
+
})
|
|
442
|
+
.send();
|
|
443
|
+
if (info.value == null)
|
|
444
|
+
throw new Error('Bid orders account not found');
|
|
445
|
+
const order = structure_models_1.OrderModel.fromBuffer(info.value.data);
|
|
446
|
+
if (order.origClientId == ctx.originalClientId) {
|
|
447
|
+
bids = [order];
|
|
448
|
+
}
|
|
449
|
+
bidContextSlot = Number(info.context.slot);
|
|
450
|
+
}
|
|
451
|
+
if (args.asksCount > 1) {
|
|
452
|
+
let info = yield ctx.rpc
|
|
453
|
+
.getAccountInfo(askOrdersAccount, { commitment: ctx.commitment, encoding: 'base64' })
|
|
454
|
+
.send();
|
|
455
|
+
if (info.value == null)
|
|
456
|
+
throw new Error('Ask orders account not found');
|
|
457
|
+
askContextSlot = Number(info.context.slot);
|
|
458
|
+
asks = (0, utils_1.getMultiplePerpOrders)(info.value.data, args.asksEntry, ctx.originalClientId);
|
|
459
|
+
}
|
|
460
|
+
else if (args.asksCount == 1) {
|
|
461
|
+
let info = yield ctx.rpc
|
|
462
|
+
.getAccountInfo(askOrdersAccount, {
|
|
463
|
+
commitment: ctx.commitment,
|
|
464
|
+
encoding: 'base64',
|
|
465
|
+
dataSlice: { offset: args.asksEntry * 64 + structure_models_1.PerpTradeAccountHeaderModel.LENGTH, length: 64 },
|
|
466
|
+
})
|
|
467
|
+
.send();
|
|
468
|
+
if (info.value == null)
|
|
469
|
+
throw new Error('Ask orders account not found');
|
|
470
|
+
const order = structure_models_1.OrderModel.fromBuffer(info.value.data);
|
|
471
|
+
if (order.origClientId == ctx.originalClientId) {
|
|
472
|
+
asks = [order];
|
|
473
|
+
}
|
|
474
|
+
askContextSlot = Number(info.context.slot);
|
|
475
|
+
}
|
|
476
|
+
for (let i = 0; i < bids.length; ++i) {
|
|
477
|
+
bids[i].qty /= assetTokenDec;
|
|
478
|
+
bids[i].sum /= crncyTokenDec;
|
|
479
|
+
}
|
|
480
|
+
for (let i = 0; i < asks.length; ++i) {
|
|
481
|
+
asks[i].qty /= assetTokenDec;
|
|
482
|
+
asks[i].sum /= crncyTokenDec;
|
|
483
|
+
}
|
|
484
|
+
let contextSlot = 0;
|
|
485
|
+
if (bidContextSlot > 0 || askContextSlot > 0) {
|
|
486
|
+
if (bidContextSlot == 0) {
|
|
487
|
+
contextSlot = askContextSlot;
|
|
488
|
+
}
|
|
489
|
+
else if (askContextSlot == 0) {
|
|
490
|
+
contextSlot = bidContextSlot;
|
|
491
|
+
}
|
|
492
|
+
else {
|
|
493
|
+
contextSlot = Math.min(bidContextSlot, askContextSlot);
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
return { contextSlot: contextSlot, bids: bids, asks: asks };
|
|
497
|
+
});
|
|
498
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|