@pendle/sdk-boros 1.8.0 → 1.8.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/backend/index.d.ts +1 -0
- package/dist/backend/index.js +1 -0
- package/dist/backend/index.js.map +1 -1
- package/dist/backend/openApi/generated/OpenApiSdk.d.ts +1485 -0
- package/dist/backend/openApi/generated/OpenApiSdk.js +714 -0
- package/dist/backend/openApi/generated/OpenApiSdk.js.map +1 -0
- package/dist/backend/openApi/index.d.ts +1 -0
- package/dist/backend/openApi/index.js +18 -0
- package/dist/backend/openApi/index.js.map +1 -0
- package/dist/backend/openApi/module.d.ts +7 -0
- package/dist/backend/openApi/module.js +67 -0
- package/dist/backend/openApi/module.js.map +1 -0
- package/dist/backend/secrettune/module.d.ts +1 -0
- package/dist/backend/secrettune/module.js +4 -0
- package/dist/backend/secrettune/module.js.map +1 -1
- package/dist/entities/Calculator/exchangeFees.d.ts +3 -3
- package/dist/entities/Calculator/exchangeFees.js +21 -13
- package/dist/entities/Calculator/exchangeFees.js.map +1 -1
- package/dist/entities/Calculator/marginCalculator.d.ts +2 -2
- package/dist/entities/Calculator/marginCalculator.js.map +1 -1
- package/dist/entities/Calculator/strategyFinder.d.ts +2 -2
- package/dist/entities/Calculator/strategyFinder.js +15 -14
- package/dist/entities/Calculator/strategyFinder.js.map +1 -1
- package/dist/entities/Calculator/strategyUtils.d.ts +1 -1
- package/dist/entities/Calculator/strategyUtils.js +4 -4
- package/dist/entities/Calculator/strategyUtils.js.map +1 -1
- package/dist/entities/Calculator/types.d.ts +3 -3
- package/dist/entities/exchange/exchange.d.ts +30 -27
- package/dist/entities/exchange/exchange.js +212 -134
- package/dist/entities/exchange/exchange.js.map +1 -1
- package/dist/entities/token/Token.d.ts +3 -3
- package/dist/entities/token/Token.js +9 -3
- package/dist/entities/token/Token.js.map +1 -1
- package/package.json +2 -2
- package/CHANGELOG.md +0 -445
|
@@ -19,8 +19,7 @@ class Exchange {
|
|
|
19
19
|
walletClient;
|
|
20
20
|
root;
|
|
21
21
|
accountId;
|
|
22
|
-
|
|
23
|
-
borosSendTxsBotSdk;
|
|
22
|
+
openApiSdk;
|
|
24
23
|
contractsFactory;
|
|
25
24
|
publicClient;
|
|
26
25
|
agent;
|
|
@@ -28,8 +27,7 @@ class Exchange {
|
|
|
28
27
|
this.walletClient = walletClient;
|
|
29
28
|
this.root = root;
|
|
30
29
|
this.accountId = accountId;
|
|
31
|
-
this.
|
|
32
|
-
this.borosSendTxsBotSdk = backend_1.BorosBackend.getSendTxsBotSdk();
|
|
30
|
+
this.openApiSdk = backend_1.BorosBackend.getOpenApiSdk();
|
|
33
31
|
this.contractsFactory = new contracts_factory_1.ContractsFactory(rpcUrls);
|
|
34
32
|
this.publicClient = this.contractsFactory.getRpcClient();
|
|
35
33
|
this.agent = agent;
|
|
@@ -44,23 +42,19 @@ class Exchange {
|
|
|
44
42
|
return this.agent;
|
|
45
43
|
}
|
|
46
44
|
async enterMarkets(cross, marketIds, nonces) {
|
|
47
|
-
const
|
|
48
|
-
const { data: enterMarketsCalldataResponse } = await this.borosCoreSdk.calldata.calldataControllerGetEnterExitMarketsCalldata({
|
|
45
|
+
const { data: enterMarketsCalldataResponse } = await this.openApiSdk.calldataBuilderAgentExecutable.calldataBuilderAgentControllerBuildEnterMarkets({
|
|
49
46
|
isCross: cross,
|
|
50
|
-
marketIds
|
|
51
|
-
isEnter: true,
|
|
47
|
+
marketIds,
|
|
52
48
|
});
|
|
53
|
-
const enterMarketsResponses = await this.bulkSignAndExecute(enterMarketsCalldataResponse.
|
|
49
|
+
const enterMarketsResponses = await this.bulkSignAndExecute(enterMarketsCalldataResponse.calls.map((call) => call.calldata), nonces);
|
|
54
50
|
return enterMarketsResponses;
|
|
55
51
|
}
|
|
56
52
|
async exitMarkets(cross, marketIds, nonces) {
|
|
57
|
-
const
|
|
58
|
-
const { data: exitMarketsCalldataResponse } = await this.borosCoreSdk.calldata.calldataControllerGetEnterExitMarketsCalldata({
|
|
53
|
+
const { data: exitMarketsCalldataResponse } = await this.openApiSdk.calldataBuilderAgentExecutable.calldataBuilderAgentControllerBuildExitMarkets({
|
|
59
54
|
isCross: cross,
|
|
60
|
-
marketIds
|
|
61
|
-
isEnter: false,
|
|
55
|
+
marketIds,
|
|
62
56
|
});
|
|
63
|
-
const exitMarketsResponses = await this.bulkSignAndExecute(exitMarketsCalldataResponse.
|
|
57
|
+
const exitMarketsResponses = await this.bulkSignAndExecute(exitMarketsCalldataResponse.calls.map((call) => call.calldata), nonces);
|
|
64
58
|
return exitMarketsResponses;
|
|
65
59
|
}
|
|
66
60
|
async bulkSignAndExecute(calldatas, nonces, requireSuccess, skipReceipt) {
|
|
@@ -71,7 +65,7 @@ class Exchange {
|
|
|
71
65
|
agent: this.getAgentForSigning(),
|
|
72
66
|
nonces,
|
|
73
67
|
});
|
|
74
|
-
const { data: executeResponses } = await this.
|
|
68
|
+
const { data: executeResponses } = await this.openApiSdk.sendTxs.sendTxsControllerBulkCalls({
|
|
75
69
|
datas: signs.map((sign) => ({
|
|
76
70
|
...sign,
|
|
77
71
|
message: {
|
|
@@ -84,7 +78,9 @@ class Exchange {
|
|
|
84
78
|
});
|
|
85
79
|
const txHash = executeResponses.find((txResponse) => !txResponse.error)?.txHash;
|
|
86
80
|
if (txHash) {
|
|
87
|
-
const receipt = await publicClient_1.publicClient.waitForTransactionReceipt({
|
|
81
|
+
const receipt = await publicClient_1.publicClient.waitForTransactionReceipt({
|
|
82
|
+
hash: txHash,
|
|
83
|
+
});
|
|
88
84
|
const blockTimestamp = (await publicClient_1.publicClient.getBlock({ blockNumber: receipt.blockNumber })).timestamp;
|
|
89
85
|
const logGroups = [];
|
|
90
86
|
let events = [];
|
|
@@ -139,16 +135,22 @@ class Exchange {
|
|
|
139
135
|
}
|
|
140
136
|
async placeOrder(params) {
|
|
141
137
|
const { marketAcc, marketId, side, size, limitTick, tif, slippage, nonces } = params;
|
|
142
|
-
const { data: placeOrderCalldataResponse } = await this.
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
138
|
+
const { data: placeOrderCalldataResponse } = await this.openApiSdk.calldataBuilderAgentExecutable.calldataBuilderAgentControllerBuildPlaceOrders({
|
|
139
|
+
orderRequests: [
|
|
140
|
+
{
|
|
141
|
+
singleOrder: {
|
|
142
|
+
marketAcc,
|
|
143
|
+
marketId,
|
|
144
|
+
side,
|
|
145
|
+
size: size.toString(),
|
|
146
|
+
limitTick,
|
|
147
|
+
tif,
|
|
148
|
+
slippage,
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
],
|
|
150
152
|
});
|
|
151
|
-
const placeOrderResponses = await this.bulkSignAndExecute(placeOrderCalldataResponse.
|
|
153
|
+
const placeOrderResponses = await this.bulkSignAndExecute(placeOrderCalldataResponse.calls.map((call) => call.calldata), nonces);
|
|
152
154
|
const placeOrderResponse = placeOrderResponses[placeOrderResponses.length - 1];
|
|
153
155
|
if ('error' in placeOrderResponse) {
|
|
154
156
|
throw new Error(placeOrderResponse.error);
|
|
@@ -183,7 +185,7 @@ class Exchange {
|
|
|
183
185
|
return results;
|
|
184
186
|
}
|
|
185
187
|
async bulkPlaceOrdersV5(request, options) {
|
|
186
|
-
const { data: bulkPlaceOrderCalldataResponse } = await this.
|
|
188
|
+
const { data: bulkPlaceOrderCalldataResponse } = await this.openApiSdk.calldataBuilderAgentExecutable.calldataBuilderAgentControllerBuildPlaceOrders({
|
|
187
189
|
orderRequests: request.orderRequests.map((orderRequest) => {
|
|
188
190
|
if ('size' in orderRequest) {
|
|
189
191
|
return {
|
|
@@ -194,7 +196,7 @@ class Exchange {
|
|
|
194
196
|
};
|
|
195
197
|
}
|
|
196
198
|
return {
|
|
197
|
-
|
|
199
|
+
bulkOrders: {
|
|
198
200
|
cross: orderRequest.cross,
|
|
199
201
|
bulks: orderRequest.bulks.map((bulk) => ({
|
|
200
202
|
marketId: bulk.marketId,
|
|
@@ -206,13 +208,13 @@ class Exchange {
|
|
|
206
208
|
...bulk.cancelData,
|
|
207
209
|
ids: bulk.cancelData.ids.map((id) => id.toString()),
|
|
208
210
|
},
|
|
211
|
+
slippage: orderRequest.slippage,
|
|
209
212
|
})),
|
|
210
|
-
slippage: orderRequest.slippage,
|
|
211
213
|
},
|
|
212
214
|
};
|
|
213
215
|
}),
|
|
214
216
|
});
|
|
215
|
-
const responses = await this.bulkSignAndExecute(bulkPlaceOrderCalldataResponse.
|
|
217
|
+
const responses = await this.bulkSignAndExecute(bulkPlaceOrderCalldataResponse.calls.map((call) => call.calldata), request.nonces, false, options?.skipReceipt);
|
|
216
218
|
const parseSingleOrderResult = (index) => {
|
|
217
219
|
const singleOrderRequest = request.orderRequests[index];
|
|
218
220
|
const orderResponse = responses[index];
|
|
@@ -299,34 +301,34 @@ class Exchange {
|
|
|
299
301
|
return results;
|
|
300
302
|
}
|
|
301
303
|
async bulkPlaceOrdersV4(request) {
|
|
302
|
-
const
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
ids: bulk.cancelData.ids.map((id) => id.toString()),
|
|
323
|
-
},
|
|
324
|
-
})),
|
|
304
|
+
const singleOrderRequests = (request.singleOrders ?? []).map((singleOrder) => ({
|
|
305
|
+
singleOrder: {
|
|
306
|
+
...singleOrder,
|
|
307
|
+
size: singleOrder.size.toString(),
|
|
308
|
+
},
|
|
309
|
+
}));
|
|
310
|
+
const bulkOrderRequestsFiltered = (request.bulkOrders ?? []).filter((bulkOrder) => bulkOrder.bulks.length > 0);
|
|
311
|
+
const bulkOrderRequests = bulkOrderRequestsFiltered.map((bulkOrder) => ({
|
|
312
|
+
bulkOrders: {
|
|
313
|
+
cross: bulkOrder.cross,
|
|
314
|
+
bulks: bulkOrder.bulks.map((bulk) => ({
|
|
315
|
+
marketId: bulk.marketId,
|
|
316
|
+
orders: {
|
|
317
|
+
...bulk.orders,
|
|
318
|
+
sizes: bulk.orders.sizes.map((size) => size.toString()),
|
|
319
|
+
},
|
|
320
|
+
cancelData: {
|
|
321
|
+
...bulk.cancelData,
|
|
322
|
+
ids: bulk.cancelData.ids.map((id) => id.toString()),
|
|
323
|
+
},
|
|
325
324
|
slippage: bulkOrder.slippage,
|
|
326
|
-
}))
|
|
327
|
-
|
|
325
|
+
})),
|
|
326
|
+
},
|
|
327
|
+
}));
|
|
328
|
+
const { data: bulkPlaceOrderCalldataResponse } = await this.openApiSdk.calldataBuilderAgentExecutable.calldataBuilderAgentControllerBuildPlaceOrders({
|
|
329
|
+
orderRequests: [...singleOrderRequests, ...bulkOrderRequests],
|
|
328
330
|
});
|
|
329
|
-
const responses = await this.bulkSignAndExecute(bulkPlaceOrderCalldataResponse.
|
|
331
|
+
const responses = await this.bulkSignAndExecute(bulkPlaceOrderCalldataResponse.calls.map((call) => call.calldata), request.nonces);
|
|
330
332
|
const startBulkIndex = request.bulkOrders ? responses.length - request.bulkOrders.length : 0;
|
|
331
333
|
const bulkOrdersResponses = request.bulkOrders ? responses.slice(startBulkIndex) : undefined;
|
|
332
334
|
const singleOrdersResponses = request.singleOrders && request.bulkOrders
|
|
@@ -412,17 +414,21 @@ class Exchange {
|
|
|
412
414
|
};
|
|
413
415
|
}
|
|
414
416
|
async bulkPlaceOrdersV2(request) {
|
|
415
|
-
const { data: bulkPlaceOrderCalldataResponse } = await this.
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
417
|
+
const { data: bulkPlaceOrderCalldataResponse } = await this.openApiSdk.calldataBuilderAgentExecutable.calldataBuilderAgentControllerBuildPlaceOrders({
|
|
418
|
+
orderRequests: request.sides.map((side, index) => ({
|
|
419
|
+
singleOrder: {
|
|
420
|
+
marketAcc: request.marketAcc,
|
|
421
|
+
marketId: request.marketId,
|
|
422
|
+
side,
|
|
423
|
+
size: request.sizes[index].toString(),
|
|
424
|
+
limitTick: request.limitTicks[index],
|
|
425
|
+
tif: request.tif,
|
|
426
|
+
ammId: request.ammId,
|
|
427
|
+
slippage: request.slippage,
|
|
428
|
+
},
|
|
429
|
+
})),
|
|
430
|
+
});
|
|
431
|
+
const placeOrdersResponse = await this.bulkSignAndExecute(bulkPlaceOrderCalldataResponse.calls.map((call) => call.calldata), request.nonces);
|
|
426
432
|
return placeOrdersResponse.map((orderResponse, index) => {
|
|
427
433
|
if ('error' in orderResponse) {
|
|
428
434
|
return {
|
|
@@ -461,14 +467,17 @@ class Exchange {
|
|
|
461
467
|
}
|
|
462
468
|
async cancelOrders(params) {
|
|
463
469
|
const { marketAcc, marketId, cancelAll, orderIds, nonces } = params;
|
|
464
|
-
const
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
+
const { data: cancelOrderCalldataResponse } = await this.openApiSdk.calldataBuilderAgentExecutable.calldataBuilderAgentControllerBuildCancelOrders({
|
|
471
|
+
markets: [
|
|
472
|
+
{
|
|
473
|
+
marketAcc,
|
|
474
|
+
marketId,
|
|
475
|
+
cancelAll,
|
|
476
|
+
orderIds,
|
|
477
|
+
},
|
|
478
|
+
],
|
|
470
479
|
});
|
|
471
|
-
const cancelOrderResponses = await this.bulkSignAndExecute(cancelOrderCalldataResponse.
|
|
480
|
+
const cancelOrderResponses = await this.bulkSignAndExecute(cancelOrderCalldataResponse.calls.map((call) => call.calldata), nonces);
|
|
472
481
|
const cancelOrderResponse = cancelOrderResponses[cancelOrderResponses.length - 1];
|
|
473
482
|
if ('error' in cancelOrderResponse) {
|
|
474
483
|
throw new Error(cancelOrderResponse.error);
|
|
@@ -493,15 +502,29 @@ class Exchange {
|
|
|
493
502
|
return cancelOrderCalldataResponse;
|
|
494
503
|
}
|
|
495
504
|
async getGasBalance() {
|
|
496
|
-
const { data: getGasBalanceCalldataResponse } = await this.
|
|
497
|
-
|
|
505
|
+
const { data: getGasBalanceCalldataResponse } = await this.openApiSdk.accounts.accountsV2ControllerGetAccountGasBalance({
|
|
506
|
+
root: this.root,
|
|
498
507
|
});
|
|
499
508
|
return getGasBalanceCalldataResponse.balanceInUSD;
|
|
500
509
|
}
|
|
501
510
|
async payTreasury(params) {
|
|
502
|
-
const { nonces,
|
|
503
|
-
const
|
|
504
|
-
const
|
|
511
|
+
const { nonces, isCross, marketId, usdAmount } = params;
|
|
512
|
+
const [market, assets] = await Promise.all([this.getMarketInfo(marketId), this.getAssets()]);
|
|
513
|
+
const asset = assets.assets.find((a) => a.tokenId === market.tokenId);
|
|
514
|
+
if (!asset) {
|
|
515
|
+
throw new Error(`payTreasury: no asset found for market ${marketId} tokenId ${market.tokenId}`);
|
|
516
|
+
}
|
|
517
|
+
const usdPrice = Number(asset.usdPrice);
|
|
518
|
+
if (!Number.isFinite(usdPrice) || usdPrice <= 0) {
|
|
519
|
+
throw new Error(`payTreasury: invalid usdPrice "${asset.usdPrice}" for tokenId ${asset.tokenId}`);
|
|
520
|
+
}
|
|
521
|
+
const nativeAmount = BigInt(Math.floor((usdAmount / usdPrice) * 10 ** asset.decimals));
|
|
522
|
+
const { data: payTreasuryCalldataResponse } = await this.openApiSdk.calldataBuilderAgentExecutable.calldataBuilderAgentControllerBuildPayTreasury({
|
|
523
|
+
isCross,
|
|
524
|
+
marketId,
|
|
525
|
+
amount: nativeAmount.toString(),
|
|
526
|
+
});
|
|
527
|
+
const payTreasuryResponse = await this.bulkSignAndExecute(payTreasuryCalldataResponse.calls.map((call) => call.calldata), nonces);
|
|
505
528
|
return payTreasuryResponse;
|
|
506
529
|
}
|
|
507
530
|
async scheduleCancel(time) {
|
|
@@ -514,25 +537,23 @@ class Exchange {
|
|
|
514
537
|
}
|
|
515
538
|
const expiredTime = expiry_s ?? Math.floor(Date.now() / 1000) + 7 * 24 * 60 * 60;
|
|
516
539
|
const approveAgentData = await agentToUse.approveAgent(this.walletClient, expiredTime, nonce);
|
|
517
|
-
const { data: approveAgentResponse } = await this.
|
|
540
|
+
const { data: approveAgentResponse } = await this.openApiSdk.sendTxs.sendTxsControllerApprove({
|
|
518
541
|
approveAgentCalldata: approveAgentData,
|
|
519
542
|
});
|
|
520
543
|
return approveAgentResponse;
|
|
521
544
|
}
|
|
522
545
|
async deposit(params) {
|
|
523
546
|
const { userAddress, tokenId, amount, accountId, marketId } = params;
|
|
547
|
+
const marketAcc = utils_1.MarketAccLib.pack(userAddress, accountId, tokenId, marketId);
|
|
524
548
|
const [depositCalldataResponse, tokenAddress] = await Promise.all([
|
|
525
|
-
this.
|
|
526
|
-
|
|
527
|
-
tokenId,
|
|
549
|
+
this.openApiSdk.calldataBuilderUserSigned.calldataBuilderUserControllerBuildDeposit({
|
|
550
|
+
marketAcc,
|
|
528
551
|
amount: amount.toString(),
|
|
529
|
-
accountId,
|
|
530
|
-
marketId,
|
|
531
552
|
}),
|
|
532
553
|
params.tokenAddress
|
|
533
554
|
? params.tokenAddress
|
|
534
|
-
: this.
|
|
535
|
-
const tokenAddress = res.data.
|
|
555
|
+
: this.openApiSdk.assets.assetsControllerListAssets().then((res) => {
|
|
556
|
+
const tokenAddress = res.data.results.find((asset) => asset.tokenId === tokenId)?.address;
|
|
536
557
|
return tokenAddress;
|
|
537
558
|
}),
|
|
538
559
|
]);
|
|
@@ -546,50 +567,59 @@ class Exchange {
|
|
|
546
567
|
chain: this.walletClient.chain,
|
|
547
568
|
type: 'eip1559',
|
|
548
569
|
});
|
|
549
|
-
await publicClient_1.publicClient.waitForTransactionReceipt({
|
|
570
|
+
await publicClient_1.publicClient.waitForTransactionReceipt({
|
|
571
|
+
hash: approvalhash,
|
|
572
|
+
confirmations: 1,
|
|
573
|
+
});
|
|
550
574
|
const hash = await this.walletClient.sendTransaction({
|
|
551
575
|
to: depositCalldataResponse.data.to,
|
|
552
|
-
data: depositCalldataResponse.data.
|
|
576
|
+
data: depositCalldataResponse.data.calldata,
|
|
553
577
|
account: this.walletClient.account,
|
|
554
578
|
chain: this.walletClient.chain,
|
|
555
579
|
type: 'eip1559',
|
|
556
580
|
});
|
|
557
|
-
const receipt = await publicClient_1.publicClient.waitForTransactionReceipt({
|
|
581
|
+
const receipt = await publicClient_1.publicClient.waitForTransactionReceipt({
|
|
582
|
+
hash,
|
|
583
|
+
confirmations: 1,
|
|
584
|
+
});
|
|
558
585
|
return receipt;
|
|
559
586
|
}
|
|
560
587
|
async withdraw(params) {
|
|
561
588
|
const { userAddress, tokenId, amount } = params;
|
|
562
|
-
const { data: withdrawCalldataResponse } = await this.
|
|
563
|
-
userAddress,
|
|
589
|
+
const { data: withdrawCalldataResponse } = await this.openApiSdk.calldataBuilderUserSigned.calldataBuilderUserControllerBuildRequestWithdrawal({
|
|
590
|
+
root: userAddress,
|
|
564
591
|
tokenId,
|
|
565
592
|
amount: amount.toString(),
|
|
566
593
|
});
|
|
567
594
|
const hash = await this.walletClient.sendTransaction({
|
|
568
595
|
to: withdrawCalldataResponse.to,
|
|
569
|
-
data: withdrawCalldataResponse.
|
|
596
|
+
data: withdrawCalldataResponse.calldata,
|
|
570
597
|
account: this.walletClient.account,
|
|
571
598
|
chain: this.walletClient.chain,
|
|
572
599
|
type: 'eip1559',
|
|
573
600
|
});
|
|
574
|
-
const receipt = await publicClient_1.publicClient.waitForTransactionReceipt({
|
|
601
|
+
const receipt = await publicClient_1.publicClient.waitForTransactionReceipt({
|
|
602
|
+
hash,
|
|
603
|
+
confirmations: 1,
|
|
604
|
+
});
|
|
575
605
|
return receipt;
|
|
576
606
|
}
|
|
577
607
|
async cashTransfer(params) {
|
|
578
608
|
const { marketId, isDeposit, amount, nonces } = params;
|
|
579
|
-
const { data: cashTransferCalldataResponse } = await this.
|
|
609
|
+
const { data: cashTransferCalldataResponse } = await this.openApiSdk.calldataBuilderAgentExecutable.calldataBuilderAgentControllerBuildCashTransfer({
|
|
580
610
|
marketId,
|
|
581
|
-
isDeposit,
|
|
611
|
+
direction: isDeposit ? 'CROSS_TO_ISOLATED' : 'ISOLATED_TO_CROSS',
|
|
582
612
|
amount: amount.toString(),
|
|
583
613
|
});
|
|
584
|
-
const response = await this.bulkSignAndExecute(cashTransferCalldataResponse.
|
|
614
|
+
const response = await this.bulkSignAndExecute(cashTransferCalldataResponse.calls.map((call) => call.calldata), nonces);
|
|
585
615
|
return response;
|
|
586
616
|
}
|
|
587
617
|
async cashTransferBatch(transfers, options) {
|
|
588
618
|
if (transfers.length === 0)
|
|
589
619
|
return [];
|
|
590
|
-
const calldataResponses = await Promise.allSettled(transfers.map((t) => this.
|
|
620
|
+
const calldataResponses = await Promise.allSettled(transfers.map((t) => this.openApiSdk.calldataBuilderAgentExecutable.calldataBuilderAgentControllerBuildCashTransfer({
|
|
591
621
|
marketId: t.marketId,
|
|
592
|
-
|
|
622
|
+
direction: t.isDeposit ? 'CROSS_TO_ISOLATED' : 'ISOLATED_TO_CROSS',
|
|
593
623
|
amount: t.amount.toString(),
|
|
594
624
|
})));
|
|
595
625
|
const slots = [];
|
|
@@ -598,7 +628,7 @@ class Exchange {
|
|
|
598
628
|
const r = calldataResponses[i];
|
|
599
629
|
const transfer = transfers[i];
|
|
600
630
|
if (r.status === 'fulfilled') {
|
|
601
|
-
const cds = r.value.data.
|
|
631
|
+
const cds = r.value.data.calls.map((call) => call.calldata);
|
|
602
632
|
slots.push({ kind: 'fetched', transfer, calldataCount: cds.length });
|
|
603
633
|
allCalldatas.push(...cds);
|
|
604
634
|
}
|
|
@@ -625,29 +655,38 @@ class Exchange {
|
|
|
625
655
|
.map((r) => r.executeResponse?.txHash)
|
|
626
656
|
.find((h) => h !== undefined);
|
|
627
657
|
if (!txHash) {
|
|
628
|
-
return {
|
|
658
|
+
return {
|
|
659
|
+
...slot.transfer,
|
|
660
|
+
error: 'cashTransferBatch returned no txHash',
|
|
661
|
+
};
|
|
629
662
|
}
|
|
630
663
|
return { ...slot.transfer, txHash };
|
|
631
664
|
});
|
|
632
665
|
}
|
|
633
666
|
async closeActivePositions(params) {
|
|
634
667
|
const { marketAcc, marketId, side, size, limitTick, tif, slippage, nonces } = params;
|
|
635
|
-
const { data: closeActivePositionsCalldataResponse } = await this.
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
668
|
+
const { data: closeActivePositionsCalldataResponse } = await this.openApiSdk.calldataBuilderAgentExecutable.calldataBuilderAgentControllerBuildPlaceOrders({
|
|
669
|
+
orderRequests: [
|
|
670
|
+
{
|
|
671
|
+
singleOrder: {
|
|
672
|
+
marketAcc,
|
|
673
|
+
marketId,
|
|
674
|
+
side,
|
|
675
|
+
size: size.toString(),
|
|
676
|
+
limitTick,
|
|
677
|
+
tif,
|
|
678
|
+
slippage,
|
|
679
|
+
},
|
|
680
|
+
},
|
|
681
|
+
],
|
|
643
682
|
});
|
|
644
|
-
const response = await this.bulkSignAndExecute(closeActivePositionsCalldataResponse.
|
|
683
|
+
const response = await this.bulkSignAndExecute(closeActivePositionsCalldataResponse.calls.map((call) => call.calldata), nonces);
|
|
645
684
|
return response;
|
|
646
685
|
}
|
|
647
686
|
async getAmmCutOffTimestamp(marketId) {
|
|
648
687
|
const markets = await this.getMarkets();
|
|
649
688
|
const market = markets.results.find((m) => m.marketId === marketId);
|
|
650
|
-
const ammContract = this.contractsFactory.getAmmContract(market.
|
|
689
|
+
const ammContract = this.contractsFactory.getAmmContract(market.extConfig?.ammAddress);
|
|
651
690
|
const ammState = await ammContract?.readState();
|
|
652
691
|
return ammState ? Number(ammState.cutOffTimestamp) : undefined;
|
|
653
692
|
}
|
|
@@ -673,7 +712,7 @@ class Exchange {
|
|
|
673
712
|
const market = markets.results.find((m) => m.marketId === marketId);
|
|
674
713
|
const marketContract = this.contractsFactory.getMarketContract(market.address);
|
|
675
714
|
const explorerContract = this.contractsFactory.getExplorerContract((0, addresses_1.getExplorerContractAddress)());
|
|
676
|
-
const ammAddress = market.
|
|
715
|
+
const ammAddress = market.extConfig?.ammAddress;
|
|
677
716
|
const ammContract = ammAddress ? this.contractsFactory.getAmmContract(ammAddress) : undefined;
|
|
678
717
|
const [marketInfo, bestBidTickAndApr, bestAskTickAndApr, ammState, ammImpliedRateBigInt, impliedRateData, marketConfig,] = await Promise.all([
|
|
679
718
|
explorerContract.getMarketInfo(market.marketId),
|
|
@@ -722,13 +761,15 @@ class Exchange {
|
|
|
722
761
|
return cached.value;
|
|
723
762
|
}
|
|
724
763
|
}
|
|
725
|
-
const {
|
|
726
|
-
const { data: getMarketsCalldataResponse } = await this.
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
isWhitelisted,
|
|
764
|
+
const { limit, isWhitelisted } = params ?? {};
|
|
765
|
+
const { data: getMarketsCalldataResponse } = await this.openApiSdk.markets.marketsControllerListMarkets({
|
|
766
|
+
limit: limit ?? 200,
|
|
767
|
+
isUiWhitelisted: isWhitelisted,
|
|
730
768
|
});
|
|
731
|
-
Exchange._getMarketsCache[cacheKey] = {
|
|
769
|
+
Exchange._getMarketsCache[cacheKey] = {
|
|
770
|
+
value: getMarketsCalldataResponse,
|
|
771
|
+
timestamp: now,
|
|
772
|
+
};
|
|
732
773
|
return getMarketsCalldataResponse;
|
|
733
774
|
}
|
|
734
775
|
static _getMarketInfoCache = {};
|
|
@@ -743,7 +784,14 @@ class Exchange {
|
|
|
743
784
|
}
|
|
744
785
|
let data;
|
|
745
786
|
try {
|
|
746
|
-
|
|
787
|
+
const { data: byIds } = await this.openApiSdk.markets.marketsControllerGetMarketsByIds({
|
|
788
|
+
marketIds: String(marketId),
|
|
789
|
+
});
|
|
790
|
+
const market = byIds.results.find((m) => m.marketId === marketId) ?? byIds.results[0];
|
|
791
|
+
if (!market) {
|
|
792
|
+
throw new Error(`market ${marketId} not found (empty results)`);
|
|
793
|
+
}
|
|
794
|
+
data = market;
|
|
747
795
|
}
|
|
748
796
|
catch (e) {
|
|
749
797
|
const reason = e instanceof Error ? e.message : String(e);
|
|
@@ -800,14 +848,18 @@ class Exchange {
|
|
|
800
848
|
return marketIds.map((marketId) => ({ marketId, ...shared }));
|
|
801
849
|
}
|
|
802
850
|
if (responses.length === marketIds.length) {
|
|
803
|
-
return responses.map((response, index) => ({
|
|
851
|
+
return responses.map((response, index) => ({
|
|
852
|
+
marketId: marketIds[index],
|
|
853
|
+
...toResult(response),
|
|
854
|
+
}));
|
|
804
855
|
}
|
|
805
856
|
throw new Error(`exitMarketsTyped: cannot attribute ${responses.length} bulk response(s) to ${marketIds.length} marketId(s); ` +
|
|
806
857
|
`expected either 1 (batched) or ${marketIds.length} (per-input)`);
|
|
807
858
|
}
|
|
808
859
|
async getOrderBook(params) {
|
|
809
860
|
const { marketId, tickSize } = params;
|
|
810
|
-
const { data: getOrderBookCalldataResponse } = await this.
|
|
861
|
+
const { data: getOrderBookCalldataResponse } = await this.openApiSdk.markets.marketsControllerGetOrderBook({
|
|
862
|
+
marketId,
|
|
811
863
|
tickSize,
|
|
812
864
|
});
|
|
813
865
|
return getOrderBookCalldataResponse;
|
|
@@ -887,19 +939,17 @@ class Exchange {
|
|
|
887
939
|
};
|
|
888
940
|
}
|
|
889
941
|
async getPnlLimitOrders(params) {
|
|
890
|
-
const {
|
|
942
|
+
const { limit, isActive, marketId, userAddress, accountId, fromContract } = params ?? {};
|
|
891
943
|
if (fromContract) {
|
|
892
944
|
return this.getPnlLimitOrdersFromContract(params);
|
|
893
945
|
}
|
|
894
946
|
const [{ data: getPnlLimitOrdersCalldataResponse }, blockNumber] = await Promise.all([
|
|
895
|
-
this.
|
|
896
|
-
|
|
947
|
+
this.openApiSdk.accounts.accountsV2ControllerGetOrders({
|
|
948
|
+
root: userAddress ?? this.root,
|
|
897
949
|
accountId: accountId ?? this.accountId,
|
|
898
950
|
marketId,
|
|
899
|
-
skip,
|
|
900
951
|
limit,
|
|
901
952
|
isActive,
|
|
902
|
-
orderBy,
|
|
903
953
|
}),
|
|
904
954
|
this.publicClient.getBlockNumber(),
|
|
905
955
|
]);
|
|
@@ -909,23 +959,51 @@ class Exchange {
|
|
|
909
959
|
};
|
|
910
960
|
}
|
|
911
961
|
async getCollaterals({ userAddress, accountId, }) {
|
|
912
|
-
const { data: getCollateralsCalldataResponse } = await this.
|
|
913
|
-
|
|
914
|
-
accountId: accountId ?? this.accountId,
|
|
962
|
+
const { data: getCollateralsCalldataResponse } = await this.openApiSdk.accounts.accountsV2ControllerGetMarketAccInfosByRoot({
|
|
963
|
+
root: userAddress ?? this.root,
|
|
915
964
|
});
|
|
916
965
|
return getCollateralsCalldataResponse;
|
|
917
966
|
}
|
|
918
967
|
async getAssets() {
|
|
919
|
-
const { data:
|
|
920
|
-
return
|
|
968
|
+
const { data: getAssetsResponse } = await this.openApiSdk.assets.assetsControllerListAssets();
|
|
969
|
+
return { assets: getAssetsResponse.results };
|
|
921
970
|
}
|
|
922
971
|
async getAmmInfoByAmmId(ammId) {
|
|
923
|
-
const
|
|
924
|
-
|
|
972
|
+
const markets = await this.getMarkets();
|
|
973
|
+
const market = markets.results.find((m) => m.extConfig?.ammId === ammId);
|
|
974
|
+
if (!market) {
|
|
975
|
+
return undefined;
|
|
976
|
+
}
|
|
977
|
+
const { data: getAmmStatesResponse } = await this.openApiSdk.amm.ammv2ControllerGetAmmStates({
|
|
978
|
+
marketIds: String(market.marketId),
|
|
979
|
+
});
|
|
980
|
+
return getAmmStatesResponse.results.find((s) => s.ammId === ammId) ?? getAmmStatesResponse.results[0];
|
|
925
981
|
}
|
|
926
982
|
async getCumulativePnl(params) {
|
|
927
|
-
const {
|
|
928
|
-
|
|
983
|
+
const { root, accountId } = utils_1.MarketAccLib.unpack(params.marketAcc);
|
|
984
|
+
const { data: activePositions } = await this.openApiSdk.accounts.accountsV2ControllerGetActivePositions({
|
|
985
|
+
root,
|
|
986
|
+
accountId,
|
|
987
|
+
});
|
|
988
|
+
const position = activePositions.results.find((p) => p.marketId === params.marketId && p.marketAcc.toLowerCase() === params.marketAcc.toLowerCase());
|
|
989
|
+
if (position) {
|
|
990
|
+
return { cumulativeTradePnl: position.cumulativePnl };
|
|
991
|
+
}
|
|
992
|
+
let sum = 0n;
|
|
993
|
+
let resumeToken;
|
|
994
|
+
do {
|
|
995
|
+
const { data: events } = await this.openApiSdk.accounts.accountsV2ControllerGetPositionUpdateEvents({
|
|
996
|
+
marketAcc: params.marketAcc,
|
|
997
|
+
marketId: params.marketId,
|
|
998
|
+
limit: 2000,
|
|
999
|
+
resumeToken,
|
|
1000
|
+
});
|
|
1001
|
+
for (const event of events.results) {
|
|
1002
|
+
sum += BigInt(event.pnl);
|
|
1003
|
+
}
|
|
1004
|
+
resumeToken = events.resumeToken ?? undefined;
|
|
1005
|
+
} while (resumeToken);
|
|
1006
|
+
return { cumulativeTradePnl: sum.toString() };
|
|
929
1007
|
}
|
|
930
1008
|
async approveAgentData(params) {
|
|
931
1009
|
const calldata = types_1.functionEncoder.approveAgent(params);
|