@d8x/perpetuals-sdk 2.1.28-alpha2 → 2.1.30-alpha2
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/cjs/constants.d.ts +0 -1
- package/dist/cjs/constants.js +4 -3
- package/dist/cjs/constants.js.map +1 -1
- package/dist/cjs/d8XMath.d.ts +0 -18
- package/dist/cjs/d8XMath.js +1 -70
- package/dist/cjs/d8XMath.js.map +1 -1
- package/dist/cjs/marketData.d.ts +15 -6
- package/dist/cjs/marketData.js +38 -67
- package/dist/cjs/marketData.js.map +1 -1
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/constants.d.ts +0 -1
- package/dist/esm/constants.js +3 -2
- package/dist/esm/constants.js.map +1 -1
- package/dist/esm/d8XMath.d.ts +0 -18
- package/dist/esm/d8XMath.js +0 -68
- package/dist/esm/d8XMath.js.map +1 -1
- package/dist/esm/marketData.d.ts +15 -6
- package/dist/esm/marketData.js +39 -68
- package/dist/esm/marketData.js.map +1 -1
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/doc/d8x-perpetuals-sdk.md +67 -58
- package/doc/marketData.md +25 -8
- package/package.json +1 -1
- package/src/constants.ts +3 -2
- package/src/d8XMath.ts +0 -77
- package/src/marketData.ts +38 -112
- package/src/version.ts +1 -1
|
@@ -156,8 +156,7 @@ on-chain</p></dd>
|
|
|
156
156
|
* [~excessMargin(tradeAmt, currentCashCC, currentPos, currentLockedInQC, limitPrice, Sm, S3)](#module_d8xMath..excessMargin) ⇒
|
|
157
157
|
* [~pmGetDepositAmtForLvgTrade(tradeAmt, targetLvg, price, S3, S2Mark)](#module_d8xMath..pmGetDepositAmtForLvgTrade) ⇒
|
|
158
158
|
* [~pmExcessCashAtLvg(tradeAmt, lvg, walletBalCC, currentCashCC, currentPosition, currentLockedInValue, slippage, S2, Sm, S3, totLong, totShort)](#module_d8xMath..pmExcessCashAtLvg) ⇒
|
|
159
|
-
* [~pmFindMaxPersonalTradeSizeAtLeverage(dir, lvg, walletBalCC, slippage, currentPosition, currentCashCC, currentLockedInValue, S2, Sm, S3,
|
|
160
|
-
* [~pmFindMaxTradeSize(dir, currentPosition, currentCashCC, currentLockedInValue, limitPrice, Sm, S3, totLong, totShort, maxShort, maxLong)](#module_d8xMath..pmFindMaxTradeSize) ⇒
|
|
159
|
+
* [~pmFindMaxPersonalTradeSizeAtLeverage(dir, lvg, walletBalCC, slippage, currentPosition, currentCashCC, currentLockedInValue, S2, Sm, S3, glblMaxTrade)](#module_d8xMath..pmFindMaxPersonalTradeSizeAtLeverage) ⇒
|
|
161
160
|
|
|
162
161
|
<a name="module_d8xMath..ABDK29ToFloat"></a>
|
|
163
162
|
|
|
@@ -594,53 +593,28 @@ after a trade of size tradeAmt in prediction markets</p>
|
|
|
594
593
|
|
|
595
594
|
<a name="module_d8xMath..pmFindMaxPersonalTradeSizeAtLeverage"></a>
|
|
596
595
|
|
|
597
|
-
### d8xMath~pmFindMaxPersonalTradeSizeAtLeverage(dir, lvg, walletBalCC, slippage, currentPosition, currentCashCC, currentLockedInValue, S2, Sm, S3,
|
|
598
|
-
<p>Find maximal trade size (short dir=-1 or long dir=1) for prediction
|
|
596
|
+
### d8xMath~pmFindMaxPersonalTradeSizeAtLeverage(dir, lvg, walletBalCC, slippage, currentPosition, currentCashCC, currentLockedInValue, S2, Sm, S3, glblMaxTrade) ⇒
|
|
597
|
+
<p>Find maximal <em>affordable</em> trade size (short dir=-1 or long dir=1) for prediction
|
|
599
598
|
markets at provided leverage and incorporating the current position
|
|
600
599
|
and wallet balance.
|
|
601
600
|
Factors in lot size and global max short/long</p>
|
|
602
601
|
|
|
603
602
|
**Kind**: inner method of [<code>d8xMath</code>](#module_d8xMath)
|
|
604
|
-
**Returns**: <p>max trade size</p>
|
|
603
|
+
**Returns**: <p>max <em>signed</em> trade size</p>
|
|
605
604
|
|
|
606
605
|
| Param | Description |
|
|
607
606
|
| --- | --- |
|
|
608
|
-
| dir |
|
|
609
|
-
| lvg |
|
|
610
|
-
| walletBalCC |
|
|
611
|
-
| slippage | <p>slippage percent</p> |
|
|
612
|
-
| currentPosition |
|
|
613
|
-
| currentCashCC |
|
|
614
|
-
| currentLockedInValue |
|
|
615
|
-
| S2 |
|
|
616
|
-
| Sm |
|
|
617
|
-
| S3 |
|
|
618
|
-
|
|
|
619
|
-
| maxLong | <p>global max long order size (positive)</p> |
|
|
620
|
-
|
|
621
|
-
<a name="module_d8xMath..pmFindMaxTradeSize"></a>
|
|
622
|
-
|
|
623
|
-
### d8xMath~pmFindMaxTradeSize(dir, currentPosition, currentCashCC, currentLockedInValue, limitPrice, Sm, S3, totLong, totShort, maxShort, maxLong) ⇒
|
|
624
|
-
<p>Find maximal trade size (short dir=-1 or long dir=1) for prediction
|
|
625
|
-
markets at maximal leverage and incorporating the current position.
|
|
626
|
-
Agnostic about wallet balance.</p>
|
|
627
|
-
|
|
628
|
-
**Kind**: inner method of [<code>d8xMath</code>](#module_d8xMath)
|
|
629
|
-
**Returns**: <p>signed max trade size</p>
|
|
630
|
-
|
|
631
|
-
| Param |
|
|
632
|
-
| --- |
|
|
633
|
-
| dir |
|
|
634
|
-
| currentPosition |
|
|
635
|
-
| currentCashCC |
|
|
636
|
-
| currentLockedInValue |
|
|
637
|
-
| limitPrice |
|
|
638
|
-
| Sm |
|
|
639
|
-
| S3 |
|
|
640
|
-
| totLong |
|
|
641
|
-
| totShort |
|
|
642
|
-
| maxShort |
|
|
643
|
-
| maxLong |
|
|
607
|
+
| dir | <p>direction of trade (-1 sell, 1 buy)</p> |
|
|
608
|
+
| lvg | <p>leverage of the trade</p> |
|
|
609
|
+
| walletBalCC | <p>wallet balance of the trader (collateral currency)</p> |
|
|
610
|
+
| slippage | <p>slippage percent used to estimate a traded price</p> |
|
|
611
|
+
| currentPosition | <p>position in base currency of the trader</p> |
|
|
612
|
+
| currentCashCC | <p>this is the cash available net of unpaid funding (often called available cash)</p> |
|
|
613
|
+
| currentLockedInValue | <p>average entry price * signed position size in base currency, in margin account</p> |
|
|
614
|
+
| S2 | <p>current index price of the form 1+p (regardless whether short or long)</p> |
|
|
615
|
+
| Sm | <p>current mark price (not just the mark price index but including the ema-premium from the contract)</p> |
|
|
616
|
+
| S3 | <p>current collateral to quote index price</p> |
|
|
617
|
+
| glblMaxTrade | <p>global max short or long order size that we retreive, e.g., from position risk (sign irrelevant) based on long: (<em>ℓ+n) * (1-p) - m (1-p) s = F → n = (F+m</em>(1-p)<em>s)/(1-p)-ℓ</em> short: (s+n)<em>p - m p <em>ℓ</em> = F →n = (F+m</em>p**ℓ*)/p-s</p> |
|
|
644
618
|
|
|
645
619
|
<a name="module_utils"></a>
|
|
646
620
|
|
|
@@ -3741,7 +3715,8 @@ No gas required for the queries here.</p>
|
|
|
3741
3715
|
* [.getShareTokenPrice(symbolOrId)](#MarketData+getShareTokenPrice) ⇒ <code>number</code>
|
|
3742
3716
|
* [.getParticipationValue(address, symbolOrId)](#MarketData+getParticipationValue) ⇒
|
|
3743
3717
|
* [.maxOrderSizeForTrader(traderAddr, symbol)](#MarketData+maxOrderSizeForTrader) ⇒
|
|
3744
|
-
* [.
|
|
3718
|
+
* [.pmMaxOrderSizeForTrader(traderAddr, symbol, overrides)](#MarketData+pmMaxOrderSizeForTrader) ⇒
|
|
3719
|
+
* [.getMaxShortLongTrade(perpId, currentTraderPos, overrides)](#MarketData+getMaxShortLongTrade) ⇒
|
|
3745
3720
|
* [.maxSignedPosition(side, symbol)](#MarketData+maxSignedPosition) ⇒ <code>number</code>
|
|
3746
3721
|
* [.getOraclePrice(base, quote)](#MarketData+getOraclePrice) ⇒ <code>number</code>
|
|
3747
3722
|
* [.getOrderStatus(symbol, orderId, overrides)](#MarketData+getOrderStatus) ⇒
|
|
@@ -4145,7 +4120,7 @@ considering the existing position, state of the perpetual
|
|
|
4145
4120
|
Accounts for user's wallet balance.</p>
|
|
4146
4121
|
|
|
4147
4122
|
**Kind**: instance method of [<code>MarketData</code>](#MarketData)
|
|
4148
|
-
**Returns**: <p>Maximal trade sizes</p>
|
|
4123
|
+
**Returns**: <p>Maximal buy and sell trade sizes (positive)</p>
|
|
4149
4124
|
|
|
4150
4125
|
| Param | Type | Description |
|
|
4151
4126
|
| --- | --- | --- |
|
|
@@ -4167,19 +4142,35 @@ async function main() {
|
|
|
4167
4142
|
}
|
|
4168
4143
|
main();
|
|
4169
4144
|
```
|
|
4170
|
-
<a name="MarketData+
|
|
4145
|
+
<a name="MarketData+pmMaxOrderSizeForTrader"></a>
|
|
4171
4146
|
|
|
4172
|
-
### marketData.
|
|
4173
|
-
<p>
|
|
4174
|
-
|
|
4147
|
+
### marketData.pmMaxOrderSizeForTrader(traderAddr, symbol, overrides) ⇒
|
|
4148
|
+
<p>pmMaxOrderSizeForTrader returns the max order size for the
|
|
4149
|
+
trader that is possible from AMM perspective (agnostic about wallet
|
|
4150
|
+
balance and leverage)</p>
|
|
4175
4151
|
|
|
4176
4152
|
**Kind**: instance method of [<code>MarketData</code>](#MarketData)
|
|
4177
|
-
**Returns**: <p>
|
|
4153
|
+
**Returns**: <p>buy: number; sell: number absolute</p>
|
|
4154
|
+
|
|
4155
|
+
| Param | Description |
|
|
4156
|
+
| --- | --- |
|
|
4157
|
+
| traderAddr | <p>address of trader</p> |
|
|
4158
|
+
| symbol | <p>perp symbol</p> |
|
|
4159
|
+
| overrides | <p>optional</p> |
|
|
4160
|
+
|
|
4161
|
+
<a name="MarketData+getMaxShortLongTrade"></a>
|
|
4162
|
+
|
|
4163
|
+
### marketData.getMaxShortLongTrade(perpId, currentTraderPos, overrides) ⇒
|
|
4164
|
+
<p>Returns the maximal allowed short trade and long trade (signed) for a trader
|
|
4165
|
+
that has a given notional (in ABDK format) in the perpetual, ignoring the traders wallet balance</p>
|
|
4166
|
+
|
|
4167
|
+
**Kind**: instance method of [<code>MarketData</code>](#MarketData)
|
|
4168
|
+
**Returns**: <p>[maxShortPos, maxLongPos] signed maximal trade sizes</p>
|
|
4178
4169
|
|
|
4179
4170
|
| Param | Description |
|
|
4180
4171
|
| --- | --- |
|
|
4181
4172
|
| perpId | |
|
|
4182
|
-
| currentTraderPos | <p>ABDK64x64 notional position of trader</p> |
|
|
4173
|
+
| currentTraderPos | <p>ABDK64x64 signed notional position of trader</p> |
|
|
4183
4174
|
| overrides | |
|
|
4184
4175
|
|
|
4185
4176
|
<a name="MarketData+maxSignedPosition"></a>
|
|
@@ -7191,7 +7182,8 @@ so that signatures can be handled in frontend via wallet</p>
|
|
|
7191
7182
|
* [.getShareTokenPrice(symbolOrId)](#MarketData+getShareTokenPrice) ⇒ <code>number</code>
|
|
7192
7183
|
* [.getParticipationValue(address, symbolOrId)](#MarketData+getParticipationValue) ⇒
|
|
7193
7184
|
* [.maxOrderSizeForTrader(traderAddr, symbol)](#MarketData+maxOrderSizeForTrader) ⇒
|
|
7194
|
-
* [.
|
|
7185
|
+
* [.pmMaxOrderSizeForTrader(traderAddr, symbol, overrides)](#MarketData+pmMaxOrderSizeForTrader) ⇒
|
|
7186
|
+
* [.getMaxShortLongTrade(perpId, currentTraderPos, overrides)](#MarketData+getMaxShortLongTrade) ⇒
|
|
7195
7187
|
* [.maxSignedPosition(side, symbol)](#MarketData+maxSignedPosition) ⇒ <code>number</code>
|
|
7196
7188
|
* [.getOraclePrice(base, quote)](#MarketData+getOraclePrice) ⇒ <code>number</code>
|
|
7197
7189
|
* [.getOrderStatus(symbol, orderId, overrides)](#MarketData+getOrderStatus) ⇒
|
|
@@ -7848,7 +7840,7 @@ Accounts for user's wallet balance.</p>
|
|
|
7848
7840
|
|
|
7849
7841
|
**Kind**: instance method of [<code>TraderInterface</code>](#TraderInterface)
|
|
7850
7842
|
**Overrides**: [<code>maxOrderSizeForTrader</code>](#MarketData+maxOrderSizeForTrader)
|
|
7851
|
-
**Returns**: <p>Maximal trade sizes</p>
|
|
7843
|
+
**Returns**: <p>Maximal buy and sell trade sizes (positive)</p>
|
|
7852
7844
|
|
|
7853
7845
|
| Param | Type | Description |
|
|
7854
7846
|
| --- | --- | --- |
|
|
@@ -7870,20 +7862,37 @@ async function main() {
|
|
|
7870
7862
|
}
|
|
7871
7863
|
main();
|
|
7872
7864
|
```
|
|
7873
|
-
<a name="MarketData+
|
|
7865
|
+
<a name="MarketData+pmMaxOrderSizeForTrader"></a>
|
|
7866
|
+
|
|
7867
|
+
### traderInterface.pmMaxOrderSizeForTrader(traderAddr, symbol, overrides) ⇒
|
|
7868
|
+
<p>pmMaxOrderSizeForTrader returns the max order size for the
|
|
7869
|
+
trader that is possible from AMM perspective (agnostic about wallet
|
|
7870
|
+
balance and leverage)</p>
|
|
7871
|
+
|
|
7872
|
+
**Kind**: instance method of [<code>TraderInterface</code>](#TraderInterface)
|
|
7873
|
+
**Overrides**: [<code>pmMaxOrderSizeForTrader</code>](#MarketData+pmMaxOrderSizeForTrader)
|
|
7874
|
+
**Returns**: <p>buy: number; sell: number absolute</p>
|
|
7875
|
+
|
|
7876
|
+
| Param | Description |
|
|
7877
|
+
| --- | --- |
|
|
7878
|
+
| traderAddr | <p>address of trader</p> |
|
|
7879
|
+
| symbol | <p>perp symbol</p> |
|
|
7880
|
+
| overrides | <p>optional</p> |
|
|
7881
|
+
|
|
7882
|
+
<a name="MarketData+getMaxShortLongTrade"></a>
|
|
7874
7883
|
|
|
7875
|
-
### traderInterface.
|
|
7876
|
-
<p>Returns the maximal allowed short
|
|
7877
|
-
|
|
7884
|
+
### traderInterface.getMaxShortLongTrade(perpId, currentTraderPos, overrides) ⇒
|
|
7885
|
+
<p>Returns the maximal allowed short trade and long trade (signed) for a trader
|
|
7886
|
+
that has a given notional (in ABDK format) in the perpetual, ignoring the traders wallet balance</p>
|
|
7878
7887
|
|
|
7879
7888
|
**Kind**: instance method of [<code>TraderInterface</code>](#TraderInterface)
|
|
7880
|
-
**Overrides**: [<code>
|
|
7881
|
-
**Returns**: <p>[maxShortPos, maxLongPos] signed maximal
|
|
7889
|
+
**Overrides**: [<code>getMaxShortLongTrade</code>](#MarketData+getMaxShortLongTrade)
|
|
7890
|
+
**Returns**: <p>[maxShortPos, maxLongPos] signed maximal trade sizes</p>
|
|
7882
7891
|
|
|
7883
7892
|
| Param | Description |
|
|
7884
7893
|
| --- | --- |
|
|
7885
7894
|
| perpId | |
|
|
7886
|
-
| currentTraderPos | <p>ABDK64x64 notional position of trader</p> |
|
|
7895
|
+
| currentTraderPos | <p>ABDK64x64 signed notional position of trader</p> |
|
|
7887
7896
|
| overrides | |
|
|
7888
7897
|
|
|
7889
7898
|
<a name="MarketData+maxSignedPosition"></a>
|
package/doc/marketData.md
CHANGED
|
@@ -26,7 +26,8 @@ No gas required for the queries here.</p>
|
|
|
26
26
|
* [.getShareTokenPrice(symbolOrId)](#MarketData+getShareTokenPrice) ⇒ <code>number</code>
|
|
27
27
|
* [.getParticipationValue(address, symbolOrId)](#MarketData+getParticipationValue) ⇒
|
|
28
28
|
* [.maxOrderSizeForTrader(traderAddr, symbol)](#MarketData+maxOrderSizeForTrader) ⇒
|
|
29
|
-
* [.
|
|
29
|
+
* [.pmMaxOrderSizeForTrader(traderAddr, symbol, overrides)](#MarketData+pmMaxOrderSizeForTrader) ⇒
|
|
30
|
+
* [.getMaxShortLongTrade(perpId, currentTraderPos, overrides)](#MarketData+getMaxShortLongTrade) ⇒
|
|
30
31
|
* [.maxSignedPosition(side, symbol)](#MarketData+maxSignedPosition) ⇒ <code>number</code>
|
|
31
32
|
* [.getOraclePrice(base, quote)](#MarketData+getOraclePrice) ⇒ <code>number</code>
|
|
32
33
|
* [.getOrderStatus(symbol, orderId, overrides)](#MarketData+getOrderStatus) ⇒
|
|
@@ -402,7 +403,7 @@ considering the existing position, state of the perpetual
|
|
|
402
403
|
Accounts for user's wallet balance.</p>
|
|
403
404
|
|
|
404
405
|
**Kind**: instance method of [<code>MarketData</code>](#MarketData)
|
|
405
|
-
**Returns**: <p>Maximal trade sizes</p>
|
|
406
|
+
**Returns**: <p>Maximal buy and sell trade sizes (positive)</p>
|
|
406
407
|
|
|
407
408
|
| Param | Type | Description |
|
|
408
409
|
| --- | --- | --- |
|
|
@@ -424,19 +425,35 @@ async function main() {
|
|
|
424
425
|
}
|
|
425
426
|
main();
|
|
426
427
|
```
|
|
427
|
-
<a name="MarketData+
|
|
428
|
+
<a name="MarketData+pmMaxOrderSizeForTrader"></a>
|
|
428
429
|
|
|
429
|
-
### marketData.
|
|
430
|
-
<p>
|
|
431
|
-
|
|
430
|
+
### marketData.pmMaxOrderSizeForTrader(traderAddr, symbol, overrides) ⇒
|
|
431
|
+
<p>pmMaxOrderSizeForTrader returns the max order size for the
|
|
432
|
+
trader that is possible from AMM perspective (agnostic about wallet
|
|
433
|
+
balance and leverage)</p>
|
|
432
434
|
|
|
433
435
|
**Kind**: instance method of [<code>MarketData</code>](#MarketData)
|
|
434
|
-
**Returns**: <p>
|
|
436
|
+
**Returns**: <p>buy: number; sell: number absolute</p>
|
|
437
|
+
|
|
438
|
+
| Param | Description |
|
|
439
|
+
| --- | --- |
|
|
440
|
+
| traderAddr | <p>address of trader</p> |
|
|
441
|
+
| symbol | <p>perp symbol</p> |
|
|
442
|
+
| overrides | <p>optional</p> |
|
|
443
|
+
|
|
444
|
+
<a name="MarketData+getMaxShortLongTrade"></a>
|
|
445
|
+
|
|
446
|
+
### marketData.getMaxShortLongTrade(perpId, currentTraderPos, overrides) ⇒
|
|
447
|
+
<p>Returns the maximal allowed short trade and long trade (signed) for a trader
|
|
448
|
+
that has a given notional (in ABDK format) in the perpetual, ignoring the traders wallet balance</p>
|
|
449
|
+
|
|
450
|
+
**Kind**: instance method of [<code>MarketData</code>](#MarketData)
|
|
451
|
+
**Returns**: <p>[maxShortPos, maxLongPos] signed maximal trade sizes</p>
|
|
435
452
|
|
|
436
453
|
| Param | Description |
|
|
437
454
|
| --- | --- |
|
|
438
455
|
| perpId | |
|
|
439
|
-
| currentTraderPos | <p>ABDK64x64 notional position of trader</p> |
|
|
456
|
+
| currentTraderPos | <p>ABDK64x64 signed notional position of trader</p> |
|
|
440
457
|
| overrides | |
|
|
441
458
|
|
|
442
459
|
<a name="MarketData+maxSignedPosition"></a>
|
package/package.json
CHANGED
package/src/constants.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { NodeSDKConfig } from "./nodeSDKTypes";
|
|
|
4
4
|
export const ERC20_ABI = require("./abi/ERC20.json");
|
|
5
5
|
export const MOCK_TOKEN_SWAP_ABI = require("./abi/MockTokenSwap.json");
|
|
6
6
|
export const PROXY_ABI = require("./abi/IPerpetualManager.json");
|
|
7
|
-
export const PROXY_ZKEVM_ABI = require("./abi-zkevm/IPerpetualManager.json");
|
|
7
|
+
//export const PROXY_ZKEVM_ABI = require("./abi-zkevm/IPerpetualManager.json");
|
|
8
8
|
export const LOB_FACTORY_ABI = require("./abi/LimitOrderBookFactory.json");
|
|
9
9
|
export const LOB_ABI = require("./abi/LimitOrderBook.json");
|
|
10
10
|
export const SHARE_TOKEN_ABI = require("./abi/ShareToken.json");
|
|
@@ -58,7 +58,8 @@ export const DEFAULT_CONFIG_TESTNET_NAME = "testnet";
|
|
|
58
58
|
|
|
59
59
|
let defaultConfigs = require("./config/defaultConfig.json") as NodeSDKConfig[];
|
|
60
60
|
defaultConfigs.map((config) => {
|
|
61
|
-
config.proxyABI = config.proxyABILocation.includes("abi-zkevm") ? PROXY_ZKEVM_ABI : PROXY_ABI;
|
|
61
|
+
//config.proxyABI = config.proxyABILocation.includes("abi-zkevm") ? PROXY_ZKEVM_ABI : PROXY_ABI;
|
|
62
|
+
config.proxyABI = PROXY_ABI;
|
|
62
63
|
config.lobABI = LOB_ABI;
|
|
63
64
|
config.lobFactoryABI = LOB_FACTORY_ABI;
|
|
64
65
|
config.shareTokenABI = SHARE_TOKEN_ABI;
|
package/src/d8XMath.ts
CHANGED
|
@@ -896,80 +896,3 @@ export function pmFindMaxPersonalTradeSizeAtLeverage(
|
|
|
896
896
|
sNew = Math.sign(sNew) * Math.floor(Math.abs(sNew) / lot) * lot;
|
|
897
897
|
return sNew;
|
|
898
898
|
}
|
|
899
|
-
|
|
900
|
-
/**
|
|
901
|
-
* Find maximal trade size (short dir=-1 or long dir=1) for prediction
|
|
902
|
-
* markets at maximal leverage and incorporating the current position.
|
|
903
|
-
* Agnostic about wallet balance.
|
|
904
|
-
* @param dir
|
|
905
|
-
* @param currentPosition
|
|
906
|
-
* @param currentCashCC
|
|
907
|
-
* @param currentLockedInValue
|
|
908
|
-
* @param limitPrice
|
|
909
|
-
* @param Sm
|
|
910
|
-
* @param S3
|
|
911
|
-
* @param totLong
|
|
912
|
-
* @param totShort
|
|
913
|
-
* @param maxShort
|
|
914
|
-
* @param maxLong
|
|
915
|
-
* @returns signed max trade size
|
|
916
|
-
*/
|
|
917
|
-
export function pmFindMaxTradeSize(
|
|
918
|
-
dir: number,
|
|
919
|
-
currentPosition: number,
|
|
920
|
-
currentCashCC: number,
|
|
921
|
-
currentLockedInValue: number,
|
|
922
|
-
limitPrice: number,
|
|
923
|
-
Sm: number,
|
|
924
|
-
S3: number,
|
|
925
|
-
maxShort: number,
|
|
926
|
-
maxLong: number
|
|
927
|
-
): number {
|
|
928
|
-
if (dir < 0) {
|
|
929
|
-
dir = -1;
|
|
930
|
-
} else {
|
|
931
|
-
dir = 1;
|
|
932
|
-
}
|
|
933
|
-
const lot = 10;
|
|
934
|
-
const deltaS = 1; //for derivative
|
|
935
|
-
const f0 = excessMargin(dir * deltaS, currentCashCC, currentPosition, currentLockedInValue, limitPrice, Sm, S3);
|
|
936
|
-
if (f0 < lot) {
|
|
937
|
-
// no trade possible
|
|
938
|
-
return 0;
|
|
939
|
-
}
|
|
940
|
-
// numerically find maximal trade size
|
|
941
|
-
let sNew = dir * lot * 10;
|
|
942
|
-
let s = 2 * sNew;
|
|
943
|
-
while (true) {
|
|
944
|
-
let count = 0;
|
|
945
|
-
while (Math.abs(sNew - s) > 1 && count < 100) {
|
|
946
|
-
s = sNew;
|
|
947
|
-
const f = excessMargin(s, currentCashCC, currentPosition, currentLockedInValue, limitPrice, Sm, S3) ** 2;
|
|
948
|
-
const f2 =
|
|
949
|
-
excessMargin(s + deltaS, currentCashCC, currentPosition, currentLockedInValue, limitPrice, Sm, S3) ** 2;
|
|
950
|
-
let ds = (f2 - f) / deltaS;
|
|
951
|
-
sNew = s - f / ds;
|
|
952
|
-
count += 1;
|
|
953
|
-
}
|
|
954
|
-
if (count < 100) {
|
|
955
|
-
break;
|
|
956
|
-
}
|
|
957
|
-
// Newton algorithm failed,
|
|
958
|
-
// choose new starting value
|
|
959
|
-
if (dir > 0) {
|
|
960
|
-
sNew = Math.random() * (maxLong - currentPosition);
|
|
961
|
-
} else {
|
|
962
|
-
sNew = -Math.random() * (Math.abs(maxShort) + currentPosition);
|
|
963
|
-
}
|
|
964
|
-
}
|
|
965
|
-
// ensure trade maximal trade sNew does not exceed
|
|
966
|
-
// the contract limits
|
|
967
|
-
if (currentPosition + sNew < maxShort) {
|
|
968
|
-
sNew = maxShort - currentPosition;
|
|
969
|
-
} else if (currentPosition + sNew > maxLong) {
|
|
970
|
-
sNew = maxLong - currentPosition;
|
|
971
|
-
}
|
|
972
|
-
// round trade size down to lot
|
|
973
|
-
sNew = Math.sign(sNew) * Math.floor(Math.abs(sNew) / lot) * lot;
|
|
974
|
-
return sNew;
|
|
975
|
-
}
|
package/src/marketData.ts
CHANGED
|
@@ -38,7 +38,6 @@ import {
|
|
|
38
38
|
entropy,
|
|
39
39
|
expectedLoss,
|
|
40
40
|
pmExchangeFee,
|
|
41
|
-
pmFindMaxTradeSize,
|
|
42
41
|
} from "./d8XMath";
|
|
43
42
|
import {
|
|
44
43
|
type ExchangeInfo,
|
|
@@ -1160,7 +1159,7 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
1160
1159
|
* Accounts for user's wallet balance.
|
|
1161
1160
|
* @param {string} traderAddr Address of trader
|
|
1162
1161
|
* @param {symbol} symbol Symbol of the form ETH-USD-MATIC
|
|
1163
|
-
* @returns Maximal trade sizes
|
|
1162
|
+
* @returns Maximal buy and sell trade sizes (positive)
|
|
1164
1163
|
* @example
|
|
1165
1164
|
* import { MarketData, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
1166
1165
|
* async function main() {
|
|
@@ -1191,6 +1190,15 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
1191
1190
|
return this.rmMaxOrderSizeForTrader(traderAddr, symbol, overrides);
|
|
1192
1191
|
}
|
|
1193
1192
|
|
|
1193
|
+
/**
|
|
1194
|
+
* pmMaxOrderSizeForTrader returns the max order size for the
|
|
1195
|
+
* trader that is possible from AMM perspective (agnostic about wallet
|
|
1196
|
+
* balance and leverage)
|
|
1197
|
+
* @param traderAddr address of trader
|
|
1198
|
+
* @param symbol perp symbol
|
|
1199
|
+
* @param overrides optional
|
|
1200
|
+
* @returns buy: number; sell: number absolute
|
|
1201
|
+
*/
|
|
1194
1202
|
private async pmMaxOrderSizeForTrader(
|
|
1195
1203
|
traderAddr: string,
|
|
1196
1204
|
symbol: string,
|
|
@@ -1199,110 +1207,25 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
1199
1207
|
if (!this.proxyContract || !this.multicall) {
|
|
1200
1208
|
throw new Error("proxy contract not initialized");
|
|
1201
1209
|
}
|
|
1202
|
-
const IERC20 = new Interface(ERC20_ABI) as ERC20Interface;
|
|
1203
1210
|
const perpId = PerpetualDataHandler.symbolToPerpetualId(symbol, this.symbolToPerpStaticInfo);
|
|
1204
|
-
const poolInfo = this.poolStaticInfos[this.getPoolStaticInfoIndexFromSymbol(symbol)];
|
|
1205
1211
|
const indexPriceInfo = await this.priceFeedGetter.fetchPricesForPerpetual(symbol);
|
|
1206
|
-
const [
|
|
1207
|
-
floatToABK64x64(x)
|
|
1208
|
-
) as [bigint, bigint, bigint];
|
|
1209
|
-
const proxyCalls: Multicall3.Call3Struct[] = [
|
|
1210
|
-
// 0: traderState
|
|
1211
|
-
{
|
|
1212
|
-
target: this.proxyContract.target,
|
|
1213
|
-
allowFailure: false,
|
|
1214
|
-
callData: this.proxyContract.interface.encodeFunctionData("getTraderState", [perpId, traderAddr, [fEma, fS3]]),
|
|
1215
|
-
},
|
|
1216
|
-
|
|
1217
|
-
// 1: wallet balance
|
|
1218
|
-
{
|
|
1219
|
-
target: poolInfo.poolSettleTokenAddr,
|
|
1220
|
-
allowFailure: false,
|
|
1221
|
-
callData: IERC20.encodeFunctionData("balanceOf", [traderAddr]),
|
|
1222
|
-
},
|
|
1223
|
-
// 2: amm state
|
|
1224
|
-
{
|
|
1225
|
-
target: this.proxyContract.target,
|
|
1226
|
-
allowFailure: false,
|
|
1227
|
-
callData: this.proxyContract.interface.encodeFunctionData("getAMMState", [perpId, [fS2, fS3]]),
|
|
1228
|
-
},
|
|
1229
|
-
];
|
|
1212
|
+
const [fS3, fEma] = [indexPriceInfo.s3 ?? 0, indexPriceInfo.ema].map((x) => floatToABK64x64(x)) as [bigint, bigint];
|
|
1230
1213
|
|
|
1231
|
-
|
|
1232
|
-
const encodedResults = await this.multicall.aggregate3.staticCall(proxyCalls, overrides || {});
|
|
1233
|
-
const traderState = this.proxyContract.interface.decodeFunctionResult(
|
|
1234
|
-
"getTraderState",
|
|
1235
|
-
encodedResults[0].returnData
|
|
1236
|
-
)[0];
|
|
1237
|
-
const walletBalance = decNToFloat(
|
|
1238
|
-
IERC20.decodeFunctionResult("balanceOf", encodedResults[1].returnData)[0],
|
|
1239
|
-
poolInfo.poolSettleTokenDecimals
|
|
1240
|
-
);
|
|
1241
|
-
const ammState = this.proxyContract.interface.decodeFunctionResult("getAMMState", encodedResults[2].returnData)[0];
|
|
1242
|
-
|
|
1243
|
-
const account = MarketData.buildMarginAccountFromState(
|
|
1244
|
-
symbol,
|
|
1245
|
-
traderState,
|
|
1246
|
-
this.symbolToPerpStaticInfo,
|
|
1247
|
-
indexPriceInfo,
|
|
1248
|
-
true //isPredMkt
|
|
1249
|
-
);
|
|
1250
|
-
const openInterestBC = ABK64x64ToFloat(ammState[11]);
|
|
1251
|
-
const net = -ABK64x64ToFloat(ammState[1]);
|
|
1252
|
-
let totLong, totShort;
|
|
1253
|
-
if (net < 0) {
|
|
1254
|
-
totLong = openInterestBC;
|
|
1255
|
-
totShort = openInterestBC - Math.abs(net);
|
|
1256
|
-
} else {
|
|
1257
|
-
totLong = openInterestBC - net;
|
|
1258
|
-
totShort = openInterestBC;
|
|
1259
|
-
}
|
|
1260
|
-
|
|
1261
|
-
let currentPositionBC = (account.side == BUY_SIDE ? 1 : -1) * account.positionNotionalBaseCCY;
|
|
1262
|
-
const Sm = ABK64x64ToFloat(traderState[8]);
|
|
1263
|
-
// settlement token must be equal to collateral token for walletBalance to be correct
|
|
1264
|
-
const availCashCC = account.collateralCC + walletBalance + account.unrealizedFundingCollateralCCY;
|
|
1214
|
+
const traderState = await this.proxyContract.getTraderState(perpId, traderAddr, [fEma, fS3], overrides || {});
|
|
1265
1215
|
const idxNotional = 4;
|
|
1266
|
-
const [
|
|
1267
|
-
|
|
1268
|
-
traderState[idxNotional],
|
|
1269
|
-
overrides
|
|
1270
|
-
);
|
|
1271
|
-
|
|
1272
|
-
const maxShort = pmFindMaxTradeSize(
|
|
1273
|
-
-1,
|
|
1274
|
-
currentPositionBC,
|
|
1275
|
-
availCashCC,
|
|
1276
|
-
account.entryPrice * currentPositionBC,
|
|
1277
|
-
Sm,
|
|
1278
|
-
Sm,
|
|
1279
|
-
indexPriceInfo.s3 ?? 0,
|
|
1280
|
-
maxShortPosPerp,
|
|
1281
|
-
maxLongPosPerp
|
|
1282
|
-
);
|
|
1283
|
-
const maxLong = pmFindMaxTradeSize(
|
|
1284
|
-
1,
|
|
1285
|
-
currentPositionBC,
|
|
1286
|
-
availCashCC,
|
|
1287
|
-
account.entryPrice * currentPositionBC,
|
|
1288
|
-
Sm,
|
|
1289
|
-
Sm,
|
|
1290
|
-
indexPriceInfo.s3 ?? 0,
|
|
1291
|
-
maxShortPosPerp,
|
|
1292
|
-
maxLongPosPerp
|
|
1293
|
-
);
|
|
1294
|
-
return { buy: maxLong, sell: maxShort };
|
|
1216
|
+
const [maxShortTrade, maxLongTrade] = await this.getMaxShortLongTrade(perpId, traderState[idxNotional], overrides);
|
|
1217
|
+
return { buy: maxLongTrade, sell: Math.abs(maxShortTrade) };
|
|
1295
1218
|
}
|
|
1296
1219
|
|
|
1297
1220
|
/**
|
|
1298
|
-
* Returns the maximal allowed short
|
|
1299
|
-
*
|
|
1221
|
+
* Returns the maximal allowed short trade and long trade (signed) for a trader
|
|
1222
|
+
* that has a given notional (in ABDK format) in the perpetual, ignoring the traders wallet balance
|
|
1300
1223
|
* @param perpId
|
|
1301
|
-
* @param currentTraderPos ABDK64x64 notional position of trader
|
|
1224
|
+
* @param currentTraderPos ABDK64x64 signed notional position of trader
|
|
1302
1225
|
* @param overrides
|
|
1303
|
-
* @returns [maxShortPos, maxLongPos] signed maximal
|
|
1226
|
+
* @returns [maxShortPos, maxLongPos] signed maximal trade sizes
|
|
1304
1227
|
*/
|
|
1305
|
-
public async
|
|
1228
|
+
public async getMaxShortLongTrade(
|
|
1306
1229
|
perpId: number,
|
|
1307
1230
|
currentTraderPos: bigint,
|
|
1308
1231
|
overrides?: Overrides
|
|
@@ -1338,22 +1261,29 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
1338
1261
|
|
|
1339
1262
|
// Max based on perp:
|
|
1340
1263
|
// max buy
|
|
1341
|
-
|
|
1264
|
+
let maxLongOrderPerp = ABK64x64ToFloat(
|
|
1342
1265
|
this.proxyContract.interface.decodeFunctionResult(
|
|
1343
1266
|
"getMaxSignedOpenTradeSizeForPos",
|
|
1344
1267
|
encodedResults2[0].returnData
|
|
1345
1268
|
)[0] as bigint
|
|
1346
1269
|
);
|
|
1347
|
-
|
|
1270
|
+
|
|
1348
1271
|
// max short
|
|
1349
|
-
|
|
1272
|
+
let maxShortOrderPerp = ABK64x64ToFloat(
|
|
1350
1273
|
this.proxyContract.interface.decodeFunctionResult(
|
|
1351
1274
|
"getMaxSignedOpenTradeSizeForPos",
|
|
1352
1275
|
encodedResults2[1].returnData
|
|
1353
1276
|
)[0] as bigint
|
|
1354
1277
|
);
|
|
1355
|
-
|
|
1356
|
-
|
|
1278
|
+
// now we ensure that closing direction can at least close position (always possible in smart contract)
|
|
1279
|
+
if (currentTraderPos > 0n) {
|
|
1280
|
+
// pos > 0, so short is closing direction
|
|
1281
|
+
maxShortOrderPerp = -Math.max(Math.abs(maxShortOrderPerp), ABK64x64ToFloat(currentTraderPos));
|
|
1282
|
+
} else {
|
|
1283
|
+
// pos <=0, so long is closing direction
|
|
1284
|
+
maxLongOrderPerp = Math.max(maxLongOrderPerp, Math.abs(ABK64x64ToFloat(currentTraderPos)));
|
|
1285
|
+
}
|
|
1286
|
+
return [maxShortOrderPerp, maxLongOrderPerp];
|
|
1357
1287
|
}
|
|
1358
1288
|
|
|
1359
1289
|
private async rmMaxOrderSizeForTrader(
|
|
@@ -1432,11 +1362,7 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
1432
1362
|
poolInfo.poolSettleTokenDecimals
|
|
1433
1363
|
);
|
|
1434
1364
|
|
|
1435
|
-
const [
|
|
1436
|
-
perpId,
|
|
1437
|
-
traderState[idxNotional],
|
|
1438
|
-
overrides
|
|
1439
|
-
);
|
|
1365
|
+
const [maxShortTrade, maxLongTrade] = await this.getMaxShortLongTrade(perpId, traderState[idxNotional], overrides);
|
|
1440
1366
|
const curPos = (account.side == BUY_SIDE ? 1 : -1) * account.positionNotionalBaseCCY;
|
|
1441
1367
|
|
|
1442
1368
|
const px: number = await coll2SettlePromise;
|
|
@@ -1466,22 +1392,22 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
1466
1392
|
account.collToQuoteConversion
|
|
1467
1393
|
);
|
|
1468
1394
|
|
|
1469
|
-
// max long/short all accounted for
|
|
1470
|
-
const
|
|
1471
|
-
const
|
|
1395
|
+
// max long/short position all accounted for
|
|
1396
|
+
const maxShort = Math.min(Math.abs(curPos + maxShortTrade), Math.abs(maxShortPosAccount));
|
|
1397
|
+
const maxLong = Math.min(Math.abs(curPos + maxLongTrade), Math.abs(maxLongPosAccount));
|
|
1472
1398
|
|
|
1473
1399
|
// max long order
|
|
1474
|
-
const
|
|
1400
|
+
const maxAffordableLongTrade =
|
|
1475
1401
|
account.side == BUY_SIDE
|
|
1476
1402
|
? Math.max(0, maxLong - account.positionNotionalBaseCCY)
|
|
1477
1403
|
: maxLong + account.positionNotionalBaseCCY;
|
|
1478
1404
|
// max short order
|
|
1479
|
-
const
|
|
1405
|
+
const maxAffordableShortTrade =
|
|
1480
1406
|
account.side == SELL_SIDE
|
|
1481
1407
|
? Math.max(0, maxShort - account.positionNotionalBaseCCY)
|
|
1482
1408
|
: maxShort + account.positionNotionalBaseCCY;
|
|
1483
1409
|
|
|
1484
|
-
return { buy:
|
|
1410
|
+
return { buy: maxAffordableLongTrade, sell: maxAffordableShortTrade };
|
|
1485
1411
|
}
|
|
1486
1412
|
|
|
1487
1413
|
/**
|
package/src/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const D8X_SDK_VERSION = "2.1.
|
|
1
|
+
export const D8X_SDK_VERSION = "2.1.30-alpha2";
|