@drift-labs/sdk 0.2.0-master.3 → 0.2.0-master.6
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/lib/admin.d.ts +5 -3
- package/lib/admin.js +30 -7
- package/lib/clearingHouse.d.ts +8 -5
- package/lib/clearingHouse.js +98 -34
- package/lib/config.js +1 -1
- package/lib/idl/clearing_house.json +171 -16
- package/lib/index.d.ts +2 -1
- package/lib/index.js +6 -1
- package/lib/math/amm.js +7 -35
- package/lib/math/auction.js +4 -1
- package/lib/math/orders.d.ts +2 -2
- package/lib/math/orders.js +19 -2
- package/lib/math/position.js +3 -1
- package/lib/math/trade.d.ts +1 -1
- package/lib/math/trade.js +7 -10
- package/lib/orderParams.d.ts +14 -5
- package/lib/orderParams.js +8 -96
- package/lib/slot/SlotSubscriber.d.ts +7 -0
- package/lib/slot/SlotSubscriber.js +3 -0
- package/lib/tx/utils.js +1 -1
- package/lib/types.d.ts +70 -1
- package/lib/types.js +41 -1
- package/package.json +3 -3
- package/src/admin.ts +47 -8
- package/src/clearingHouse.ts +133 -58
- package/src/config.ts +1 -1
- package/src/idl/clearing_house.json +171 -16
- package/src/index.ts +2 -1
- package/src/math/amm.ts +19 -49
- package/src/math/auction.ts +5 -1
- package/src/math/orders.ts +17 -3
- package/src/math/position.ts +5 -1
- package/src/math/trade.ts +23 -25
- package/src/orderParams.ts +20 -141
- package/src/slot/SlotSubscriber.ts +11 -1
- package/src/tx/utils.ts +1 -1
- package/src/types.ts +65 -2
|
@@ -141,6 +141,10 @@
|
|
|
141
141
|
{
|
|
142
142
|
"name": "maintenanceLiabilityWeight",
|
|
143
143
|
"type": "u128"
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
"name": "imfFactor",
|
|
147
|
+
"type": "u128"
|
|
144
148
|
}
|
|
145
149
|
]
|
|
146
150
|
},
|
|
@@ -534,7 +538,7 @@
|
|
|
534
538
|
},
|
|
535
539
|
{
|
|
536
540
|
"name": "taker",
|
|
537
|
-
"isMut":
|
|
541
|
+
"isMut": true,
|
|
538
542
|
"isSigner": false
|
|
539
543
|
},
|
|
540
544
|
{
|
|
@@ -716,7 +720,7 @@
|
|
|
716
720
|
]
|
|
717
721
|
},
|
|
718
722
|
{
|
|
719
|
-
"name": "
|
|
723
|
+
"name": "withdrawFromMarketToInsuranceVault",
|
|
720
724
|
"accounts": [
|
|
721
725
|
{
|
|
722
726
|
"name": "state",
|
|
@@ -730,7 +734,7 @@
|
|
|
730
734
|
},
|
|
731
735
|
{
|
|
732
736
|
"name": "bank",
|
|
733
|
-
"isMut":
|
|
737
|
+
"isMut": true,
|
|
734
738
|
"isSigner": false
|
|
735
739
|
},
|
|
736
740
|
{
|
|
@@ -835,6 +839,11 @@
|
|
|
835
839
|
"isMut": false,
|
|
836
840
|
"isSigner": false
|
|
837
841
|
},
|
|
842
|
+
{
|
|
843
|
+
"name": "bank",
|
|
844
|
+
"isMut": true,
|
|
845
|
+
"isSigner": false
|
|
846
|
+
},
|
|
838
847
|
{
|
|
839
848
|
"name": "bankVault",
|
|
840
849
|
"isMut": true,
|
|
@@ -1093,6 +1102,62 @@
|
|
|
1093
1102
|
}
|
|
1094
1103
|
]
|
|
1095
1104
|
},
|
|
1105
|
+
{
|
|
1106
|
+
"name": "updateMarketImfFactor",
|
|
1107
|
+
"accounts": [
|
|
1108
|
+
{
|
|
1109
|
+
"name": "admin",
|
|
1110
|
+
"isMut": false,
|
|
1111
|
+
"isSigner": true
|
|
1112
|
+
},
|
|
1113
|
+
{
|
|
1114
|
+
"name": "state",
|
|
1115
|
+
"isMut": false,
|
|
1116
|
+
"isSigner": false
|
|
1117
|
+
},
|
|
1118
|
+
{
|
|
1119
|
+
"name": "market",
|
|
1120
|
+
"isMut": true,
|
|
1121
|
+
"isSigner": false
|
|
1122
|
+
}
|
|
1123
|
+
],
|
|
1124
|
+
"args": [
|
|
1125
|
+
{
|
|
1126
|
+
"name": "imfFactor",
|
|
1127
|
+
"type": "u128"
|
|
1128
|
+
}
|
|
1129
|
+
]
|
|
1130
|
+
},
|
|
1131
|
+
{
|
|
1132
|
+
"name": "updateMarketUnsettledAssetWeight",
|
|
1133
|
+
"accounts": [
|
|
1134
|
+
{
|
|
1135
|
+
"name": "admin",
|
|
1136
|
+
"isMut": false,
|
|
1137
|
+
"isSigner": true
|
|
1138
|
+
},
|
|
1139
|
+
{
|
|
1140
|
+
"name": "state",
|
|
1141
|
+
"isMut": false,
|
|
1142
|
+
"isSigner": false
|
|
1143
|
+
},
|
|
1144
|
+
{
|
|
1145
|
+
"name": "market",
|
|
1146
|
+
"isMut": true,
|
|
1147
|
+
"isSigner": false
|
|
1148
|
+
}
|
|
1149
|
+
],
|
|
1150
|
+
"args": [
|
|
1151
|
+
{
|
|
1152
|
+
"name": "unsettledInitialAssetWeight",
|
|
1153
|
+
"type": "u8"
|
|
1154
|
+
},
|
|
1155
|
+
{
|
|
1156
|
+
"name": "unsettledMaintenanceAssetWeight",
|
|
1157
|
+
"type": "u8"
|
|
1158
|
+
}
|
|
1159
|
+
]
|
|
1160
|
+
},
|
|
1096
1161
|
{
|
|
1097
1162
|
"name": "updateCurveUpdateIntensity",
|
|
1098
1163
|
"accounts": [
|
|
@@ -1441,6 +1506,58 @@
|
|
|
1441
1506
|
}
|
|
1442
1507
|
]
|
|
1443
1508
|
},
|
|
1509
|
+
{
|
|
1510
|
+
"name": "updateMarketMaxSlippageRatio",
|
|
1511
|
+
"accounts": [
|
|
1512
|
+
{
|
|
1513
|
+
"name": "admin",
|
|
1514
|
+
"isMut": false,
|
|
1515
|
+
"isSigner": true
|
|
1516
|
+
},
|
|
1517
|
+
{
|
|
1518
|
+
"name": "state",
|
|
1519
|
+
"isMut": false,
|
|
1520
|
+
"isSigner": false
|
|
1521
|
+
},
|
|
1522
|
+
{
|
|
1523
|
+
"name": "market",
|
|
1524
|
+
"isMut": true,
|
|
1525
|
+
"isSigner": false
|
|
1526
|
+
}
|
|
1527
|
+
],
|
|
1528
|
+
"args": [
|
|
1529
|
+
{
|
|
1530
|
+
"name": "maxSlippageRatio",
|
|
1531
|
+
"type": "u16"
|
|
1532
|
+
}
|
|
1533
|
+
]
|
|
1534
|
+
},
|
|
1535
|
+
{
|
|
1536
|
+
"name": "updateMaxBaseAssetAmountRatio",
|
|
1537
|
+
"accounts": [
|
|
1538
|
+
{
|
|
1539
|
+
"name": "admin",
|
|
1540
|
+
"isMut": false,
|
|
1541
|
+
"isSigner": true
|
|
1542
|
+
},
|
|
1543
|
+
{
|
|
1544
|
+
"name": "state",
|
|
1545
|
+
"isMut": false,
|
|
1546
|
+
"isSigner": false
|
|
1547
|
+
},
|
|
1548
|
+
{
|
|
1549
|
+
"name": "market",
|
|
1550
|
+
"isMut": true,
|
|
1551
|
+
"isSigner": false
|
|
1552
|
+
}
|
|
1553
|
+
],
|
|
1554
|
+
"args": [
|
|
1555
|
+
{
|
|
1556
|
+
"name": "maxBaseAssetAmountRatio",
|
|
1557
|
+
"type": "u16"
|
|
1558
|
+
}
|
|
1559
|
+
]
|
|
1560
|
+
},
|
|
1444
1561
|
{
|
|
1445
1562
|
"name": "updateAdmin",
|
|
1446
1563
|
"accounts": [
|
|
@@ -1563,7 +1680,7 @@
|
|
|
1563
1680
|
]
|
|
1564
1681
|
},
|
|
1565
1682
|
{
|
|
1566
|
-
"name": "
|
|
1683
|
+
"name": "updateAuctionDuration",
|
|
1567
1684
|
"accounts": [
|
|
1568
1685
|
{
|
|
1569
1686
|
"name": "admin",
|
|
@@ -1578,7 +1695,11 @@
|
|
|
1578
1695
|
],
|
|
1579
1696
|
"args": [
|
|
1580
1697
|
{
|
|
1581
|
-
"name": "
|
|
1698
|
+
"name": "minAuctionDuration",
|
|
1699
|
+
"type": "u8"
|
|
1700
|
+
},
|
|
1701
|
+
{
|
|
1702
|
+
"name": "maxAuctionDuration",
|
|
1582
1703
|
"type": "u8"
|
|
1583
1704
|
}
|
|
1584
1705
|
]
|
|
@@ -1675,6 +1796,10 @@
|
|
|
1675
1796
|
{
|
|
1676
1797
|
"name": "maintenanceLiabilityWeight",
|
|
1677
1798
|
"type": "u128"
|
|
1799
|
+
},
|
|
1800
|
+
{
|
|
1801
|
+
"name": "imfFactor",
|
|
1802
|
+
"type": "u128"
|
|
1678
1803
|
}
|
|
1679
1804
|
]
|
|
1680
1805
|
}
|
|
@@ -1752,6 +1877,22 @@
|
|
|
1752
1877
|
"name": "unsettledLoss",
|
|
1753
1878
|
"type": "u128"
|
|
1754
1879
|
},
|
|
1880
|
+
{
|
|
1881
|
+
"name": "imfFactor",
|
|
1882
|
+
"type": "u128"
|
|
1883
|
+
},
|
|
1884
|
+
{
|
|
1885
|
+
"name": "unsettledInitialAssetWeight",
|
|
1886
|
+
"type": "u8"
|
|
1887
|
+
},
|
|
1888
|
+
{
|
|
1889
|
+
"name": "unsettledMaintenanceAssetWeight",
|
|
1890
|
+
"type": "u8"
|
|
1891
|
+
},
|
|
1892
|
+
{
|
|
1893
|
+
"name": "unsettledImfFactor",
|
|
1894
|
+
"type": "u128"
|
|
1895
|
+
},
|
|
1755
1896
|
{
|
|
1756
1897
|
"name": "padding0",
|
|
1757
1898
|
"type": "u32"
|
|
@@ -1885,7 +2026,11 @@
|
|
|
1885
2026
|
"type": "u128"
|
|
1886
2027
|
},
|
|
1887
2028
|
{
|
|
1888
|
-
"name": "
|
|
2029
|
+
"name": "minAuctionDuration",
|
|
2030
|
+
"type": "u8"
|
|
2031
|
+
},
|
|
2032
|
+
{
|
|
2033
|
+
"name": "maxAuctionDuration",
|
|
1889
2034
|
"type": "u8"
|
|
1890
2035
|
},
|
|
1891
2036
|
{
|
|
@@ -1990,10 +2135,6 @@
|
|
|
1990
2135
|
"name": "userOrderId",
|
|
1991
2136
|
"type": "u8"
|
|
1992
2137
|
},
|
|
1993
|
-
{
|
|
1994
|
-
"name": "quoteAssetAmount",
|
|
1995
|
-
"type": "u128"
|
|
1996
|
-
},
|
|
1997
2138
|
{
|
|
1998
2139
|
"name": "baseAssetAmount",
|
|
1999
2140
|
"type": "u128"
|
|
@@ -2042,6 +2183,10 @@
|
|
|
2042
2183
|
"name": "oraclePriceOffset",
|
|
2043
2184
|
"type": "i128"
|
|
2044
2185
|
},
|
|
2186
|
+
{
|
|
2187
|
+
"name": "auctionDuration",
|
|
2188
|
+
"type": "u8"
|
|
2189
|
+
},
|
|
2045
2190
|
{
|
|
2046
2191
|
"name": "padding0",
|
|
2047
2192
|
"type": "bool"
|
|
@@ -2204,6 +2349,14 @@
|
|
|
2204
2349
|
"name": "minimumQuoteAssetTradeSize",
|
|
2205
2350
|
"type": "u128"
|
|
2206
2351
|
},
|
|
2352
|
+
{
|
|
2353
|
+
"name": "maxBaseAssetAmountRatio",
|
|
2354
|
+
"type": "u16"
|
|
2355
|
+
},
|
|
2356
|
+
{
|
|
2357
|
+
"name": "maxSlippageRatio",
|
|
2358
|
+
"type": "u16"
|
|
2359
|
+
},
|
|
2207
2360
|
{
|
|
2208
2361
|
"name": "baseAssetAmountStepSize",
|
|
2209
2362
|
"type": "u128"
|
|
@@ -2688,10 +2841,6 @@
|
|
|
2688
2841
|
"defined": "PositionDirection"
|
|
2689
2842
|
}
|
|
2690
2843
|
},
|
|
2691
|
-
{
|
|
2692
|
-
"name": "quoteAssetAmount",
|
|
2693
|
-
"type": "u128"
|
|
2694
|
-
},
|
|
2695
2844
|
{
|
|
2696
2845
|
"name": "baseAssetAmount",
|
|
2697
2846
|
"type": "u128"
|
|
@@ -2904,6 +3053,12 @@
|
|
|
2904
3053
|
},
|
|
2905
3054
|
{
|
|
2906
3055
|
"name": "OraclePriceBreachedLimitPrice"
|
|
3056
|
+
},
|
|
3057
|
+
{
|
|
3058
|
+
"name": "MarketOrderFilledToLimitPrice"
|
|
3059
|
+
},
|
|
3060
|
+
{
|
|
3061
|
+
"name": "MarketOrderAuctionExpired"
|
|
2907
3062
|
}
|
|
2908
3063
|
]
|
|
2909
3064
|
}
|
|
@@ -3691,8 +3846,8 @@
|
|
|
3691
3846
|
},
|
|
3692
3847
|
{
|
|
3693
3848
|
"code": 6044,
|
|
3694
|
-
"name": "
|
|
3695
|
-
"msg": "
|
|
3849
|
+
"name": "FillOrderDidNotUpdateState",
|
|
3850
|
+
"msg": "FillOrderDidNotUpdateState"
|
|
3696
3851
|
},
|
|
3697
3852
|
{
|
|
3698
3853
|
"code": 6045,
|
package/lib/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { BN } from '@project-serum/anchor';
|
|
2
2
|
import { PublicKey } from '@solana/web3.js';
|
|
3
|
+
import pyth from '@pythnetwork/client';
|
|
3
4
|
export * from './mockUSDCFaucet';
|
|
4
5
|
export * from './oracles/types';
|
|
5
6
|
export * from './oracles/pythClient';
|
|
@@ -47,4 +48,4 @@ export * from './util/tps';
|
|
|
47
48
|
export * from './math/bankBalance';
|
|
48
49
|
export * from './constants/banks';
|
|
49
50
|
export * from './clearingHouseConfig';
|
|
50
|
-
export { BN, PublicKey };
|
|
51
|
+
export { BN, PublicKey, pyth };
|
package/lib/index.js
CHANGED
|
@@ -13,12 +13,17 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
13
13
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
+
};
|
|
16
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.PublicKey = exports.BN = void 0;
|
|
20
|
+
exports.pyth = exports.PublicKey = exports.BN = void 0;
|
|
18
21
|
const anchor_1 = require("@project-serum/anchor");
|
|
19
22
|
Object.defineProperty(exports, "BN", { enumerable: true, get: function () { return anchor_1.BN; } });
|
|
20
23
|
const web3_js_1 = require("@solana/web3.js");
|
|
21
24
|
Object.defineProperty(exports, "PublicKey", { enumerable: true, get: function () { return web3_js_1.PublicKey; } });
|
|
25
|
+
const client_1 = __importDefault(require("@pythnetwork/client"));
|
|
26
|
+
exports.pyth = client_1.default;
|
|
22
27
|
__exportStar(require("./mockUSDCFaucet"), exports);
|
|
23
28
|
__exportStar(require("./oracles/types"), exports);
|
|
24
29
|
__exportStar(require("./oracles/pythClient"), exports);
|
package/lib/math/amm.js
CHANGED
|
@@ -227,9 +227,8 @@ function calculateSpreadBN(baseSpread, lastOracleMarkSpreadPct, lastOracleConfPc
|
|
|
227
227
|
}
|
|
228
228
|
exports.calculateSpreadBN = calculateSpreadBN;
|
|
229
229
|
function calculateSpread(amm, direction, oraclePriceData) {
|
|
230
|
-
let spread = amm.baseSpread / 2;
|
|
231
230
|
if (amm.baseSpread == 0 || amm.curveUpdateIntensity == 0) {
|
|
232
|
-
return
|
|
231
|
+
return amm.baseSpread / 2;
|
|
233
232
|
}
|
|
234
233
|
const markPrice = calculatePrice(amm.baseAssetReserve, amm.quoteAssetReserve, amm.pegMultiplier);
|
|
235
234
|
const targetPrice = (oraclePriceData === null || oraclePriceData === void 0 ? void 0 : oraclePriceData.price) || markPrice;
|
|
@@ -241,40 +240,13 @@ function calculateSpread(amm, direction, oraclePriceData) {
|
|
|
241
240
|
const confIntervalPct = confInterval
|
|
242
241
|
.mul(numericConstants_1.BID_ASK_SPREAD_PRECISION)
|
|
243
242
|
.div(markPrice);
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
spread =
|
|
243
|
+
const [longSpread, shortSpread] = calculateSpreadBN(amm.baseSpread, targetMarkSpreadPct, confIntervalPct, amm.maxSpread, amm.quoteAssetReserve, amm.terminalQuoteAssetReserve, amm.pegMultiplier, amm.netBaseAssetAmount, markPrice, amm.totalFeeMinusDistributions);
|
|
244
|
+
let spread;
|
|
245
|
+
if ((0, types_1.isVariant)(direction, 'long')) {
|
|
246
|
+
spread = longSpread;
|
|
248
247
|
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
if ((amm.netBaseAssetAmount.gt(numericConstants_1.ZERO) && (0, types_1.isVariant)(direction, 'long')) ||
|
|
252
|
-
(amm.netBaseAssetAmount.lt(numericConstants_1.ZERO) && (0, types_1.isVariant)(direction, 'short')) ||
|
|
253
|
-
amm.totalFeeMinusDistributions.eq(numericConstants_1.ZERO)) {
|
|
254
|
-
const netCostBasis = amm.quoteAssetAmountLong.sub(amm.quoteAssetAmountShort);
|
|
255
|
-
const netBaseAssetValue = amm.quoteAssetReserve
|
|
256
|
-
.sub(amm.terminalQuoteAssetReserve)
|
|
257
|
-
.mul(amm.pegMultiplier)
|
|
258
|
-
.div(numericConstants_1.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO);
|
|
259
|
-
const localBaseAssetValue = amm.netBaseAssetAmount
|
|
260
|
-
.mul(markPrice)
|
|
261
|
-
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO.mul(numericConstants_1.MARK_PRICE_PRECISION));
|
|
262
|
-
const netPnl = netBaseAssetValue.sub(netCostBasis);
|
|
263
|
-
const localPnl = localBaseAssetValue.sub(netCostBasis);
|
|
264
|
-
let effectiveLeverage = MAX_INVENTORY_SKEW;
|
|
265
|
-
if (amm.totalFeeMinusDistributions.gt(numericConstants_1.ZERO)) {
|
|
266
|
-
effectiveLeverage =
|
|
267
|
-
localPnl.sub(netPnl).toNumber() /
|
|
268
|
-
(amm.totalFeeMinusDistributions.toNumber() + 1);
|
|
269
|
-
}
|
|
270
|
-
let spreadScale = Math.min(MAX_INVENTORY_SKEW, 1 + effectiveLeverage);
|
|
271
|
-
const maxTargetSpread = numericConstants_1.BID_ASK_SPREAD_PRECISION.toNumber() / 50; // 2%
|
|
272
|
-
// cap the scale to attempt to only scale up to maxTargetSpread
|
|
273
|
-
// always let the oracle retreat methods go through 100%
|
|
274
|
-
if (spreadScale * spread > maxTargetSpread) {
|
|
275
|
-
spreadScale = Math.max(1.05, maxTargetSpread / spread);
|
|
276
|
-
}
|
|
277
|
-
spread *= spreadScale;
|
|
248
|
+
else {
|
|
249
|
+
spread = shortSpread;
|
|
278
250
|
}
|
|
279
251
|
return spread;
|
|
280
252
|
}
|
package/lib/math/auction.js
CHANGED
|
@@ -4,7 +4,10 @@ exports.getAuctionPrice = exports.isAuctionComplete = void 0;
|
|
|
4
4
|
const types_1 = require("../types");
|
|
5
5
|
const _1 = require("../.");
|
|
6
6
|
function isAuctionComplete(order, slot) {
|
|
7
|
-
|
|
7
|
+
if (order.auctionDuration === 0) {
|
|
8
|
+
return true;
|
|
9
|
+
}
|
|
10
|
+
return new _1.BN(slot).sub(order.slot).gt(new _1.BN(order.auctionDuration));
|
|
8
11
|
}
|
|
9
12
|
exports.isAuctionComplete = isAuctionComplete;
|
|
10
13
|
function getAuctionPrice(order, slot) {
|
package/lib/math/orders.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
import { ClearingHouseUser } from '../clearingHouseUser';
|
|
3
|
-
import { Order } from '../types';
|
|
3
|
+
import { MarketAccount, Order } from '../types';
|
|
4
4
|
import { BN } from '@project-serum/anchor';
|
|
5
5
|
import { OraclePriceData } from '../oracles/types';
|
|
6
6
|
export declare function isOrderRiskIncreasing(user: ClearingHouseUser, order: Order): boolean;
|
|
7
7
|
export declare function isOrderRiskIncreasingInSameDirection(user: ClearingHouseUser, order: Order): boolean;
|
|
8
8
|
export declare function isOrderReduceOnly(user: ClearingHouseUser, order: Order): boolean;
|
|
9
9
|
export declare function standardizeBaseAssetAmount(baseAssetAmount: BN, stepSize: BN): BN;
|
|
10
|
-
export declare function getLimitPrice(order: Order, oraclePriceData: OraclePriceData, slot: number): BN;
|
|
10
|
+
export declare function getLimitPrice(order: Order, market: MarketAccount, oraclePriceData: OraclePriceData, slot: number): BN;
|
package/lib/math/orders.js
CHANGED
|
@@ -3,7 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.getLimitPrice = exports.standardizeBaseAssetAmount = exports.isOrderReduceOnly = exports.isOrderRiskIncreasingInSameDirection = exports.isOrderRiskIncreasing = void 0;
|
|
4
4
|
const types_1 = require("../types");
|
|
5
5
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
|
+
const anchor_1 = require("@project-serum/anchor");
|
|
6
7
|
const auction_1 = require("./auction");
|
|
8
|
+
const market_1 = require("./market");
|
|
7
9
|
function isOrderRiskIncreasing(user, order) {
|
|
8
10
|
if ((0, types_1.isVariant)(order.status, 'init')) {
|
|
9
11
|
return false;
|
|
@@ -77,13 +79,28 @@ function standardizeBaseAssetAmount(baseAssetAmount, stepSize) {
|
|
|
77
79
|
return baseAssetAmount.sub(remainder);
|
|
78
80
|
}
|
|
79
81
|
exports.standardizeBaseAssetAmount = standardizeBaseAssetAmount;
|
|
80
|
-
function getLimitPrice(order, oraclePriceData, slot) {
|
|
82
|
+
function getLimitPrice(order, market, oraclePriceData, slot) {
|
|
81
83
|
let limitPrice;
|
|
82
84
|
if (!order.oraclePriceOffset.eq(numericConstants_1.ZERO)) {
|
|
83
85
|
limitPrice = oraclePriceData.price.add(order.oraclePriceOffset);
|
|
84
86
|
}
|
|
85
87
|
else if ((0, types_1.isOneOfVariant)(order.orderType, ['market', 'triggerMarket'])) {
|
|
86
|
-
|
|
88
|
+
if ((0, auction_1.isAuctionComplete)(order, slot)) {
|
|
89
|
+
limitPrice = (0, auction_1.getAuctionPrice)(order, slot);
|
|
90
|
+
}
|
|
91
|
+
else if (!order.price.eq(numericConstants_1.ZERO)) {
|
|
92
|
+
limitPrice = order.price;
|
|
93
|
+
}
|
|
94
|
+
else if ((0, types_1.isVariant)(order.direction, 'long')) {
|
|
95
|
+
const askPrice = (0, market_1.calculateAskPrice)(market, oraclePriceData);
|
|
96
|
+
const delta = askPrice.div(new anchor_1.BN(market.amm.maxSlippageRatio));
|
|
97
|
+
limitPrice = askPrice.add(delta);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
const bidPrice = (0, market_1.calculateBidPrice)(market, oraclePriceData);
|
|
101
|
+
const delta = bidPrice.div(new anchor_1.BN(market.amm.maxSlippageRatio));
|
|
102
|
+
limitPrice = bidPrice.sub(delta);
|
|
103
|
+
}
|
|
87
104
|
}
|
|
88
105
|
else {
|
|
89
106
|
limitPrice = order.price;
|
package/lib/math/position.js
CHANGED
|
@@ -101,7 +101,9 @@ function calculatePositionFundingPNL(market, marketPosition) {
|
|
|
101
101
|
}
|
|
102
102
|
exports.calculatePositionFundingPNL = calculatePositionFundingPNL;
|
|
103
103
|
function positionIsAvailable(position) {
|
|
104
|
-
return position.baseAssetAmount.eq(numericConstants_1.ZERO) &&
|
|
104
|
+
return (position.baseAssetAmount.eq(numericConstants_1.ZERO) &&
|
|
105
|
+
position.openOrders.eq(numericConstants_1.ZERO) &&
|
|
106
|
+
position.unsettledPnl.eq(numericConstants_1.ZERO));
|
|
105
107
|
}
|
|
106
108
|
exports.positionIsAvailable = positionIsAvailable;
|
|
107
109
|
/**
|
package/lib/math/trade.d.ts
CHANGED
|
@@ -33,7 +33,7 @@ export declare function calculateTradeSlippage(direction: PositionDirection, amo
|
|
|
33
33
|
* | 'acquiredBase' => positive/negative change in user's base : BN AMM_RESERVE_PRECISION
|
|
34
34
|
* | 'acquiredQuote' => positive/negative change in user's quote : BN TODO-PRECISION
|
|
35
35
|
*/
|
|
36
|
-
export declare function calculateTradeAcquiredAmounts(direction: PositionDirection, amount: BN, market: MarketAccount, inputAssetType: AssetType, oraclePriceData: OraclePriceData, useSpread?: boolean): [BN, BN];
|
|
36
|
+
export declare function calculateTradeAcquiredAmounts(direction: PositionDirection, amount: BN, market: MarketAccount, inputAssetType: AssetType, oraclePriceData: OraclePriceData, useSpread?: boolean): [BN, BN, BN];
|
|
37
37
|
/**
|
|
38
38
|
* calculateTargetPriceTrade
|
|
39
39
|
* simple function for finding arbitraging trades
|
package/lib/math/trade.js
CHANGED
|
@@ -43,15 +43,11 @@ function calculateTradeSlippage(direction, amount, market, inputAssetType = 'quo
|
|
|
43
43
|
if (amount.eq(numericConstants_1.ZERO)) {
|
|
44
44
|
return [numericConstants_1.ZERO, numericConstants_1.ZERO, oldPrice, oldPrice];
|
|
45
45
|
}
|
|
46
|
-
const [
|
|
47
|
-
const
|
|
48
|
-
? types_1.SwapDirection.REMOVE
|
|
49
|
-
: types_1.SwapDirection.ADD;
|
|
50
|
-
const quoteAssetAmountAcquired = (0, amm_1.calculateQuoteAssetAmountSwapped)(acquiredQuote.abs(), market.amm.pegMultiplier, swapDirection);
|
|
51
|
-
const entryPrice = quoteAssetAmountAcquired
|
|
46
|
+
const [acquiredBaseReserve, acquiredQuoteReserve, acquiredQuoteAssetAmount] = calculateTradeAcquiredAmounts(direction, amount, market, inputAssetType, oraclePriceData, useSpread);
|
|
47
|
+
const entryPrice = acquiredQuoteAssetAmount
|
|
52
48
|
.mul(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO)
|
|
53
49
|
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
54
|
-
.div(
|
|
50
|
+
.div(acquiredBaseReserve.abs());
|
|
55
51
|
let amm;
|
|
56
52
|
if (useSpread && market.amm.baseSpread > 0) {
|
|
57
53
|
const { baseAssetReserve, quoteAssetReserve, sqrtK, newPeg } = (0, amm_1.calculateUpdatedAMMSpreadReserves)(market.amm, direction, oraclePriceData);
|
|
@@ -65,7 +61,7 @@ function calculateTradeSlippage(direction, amount, market, inputAssetType = 'quo
|
|
|
65
61
|
else {
|
|
66
62
|
amm = market.amm;
|
|
67
63
|
}
|
|
68
|
-
const newPrice = (0, amm_1.calculatePrice)(amm.baseAssetReserve.sub(
|
|
64
|
+
const newPrice = (0, amm_1.calculatePrice)(amm.baseAssetReserve.sub(acquiredBaseReserve), amm.quoteAssetReserve.sub(acquiredQuoteReserve), amm.pegMultiplier);
|
|
69
65
|
if (direction == types_1.PositionDirection.SHORT) {
|
|
70
66
|
(0, assert_1.assert)(newPrice.lte(oldPrice));
|
|
71
67
|
}
|
|
@@ -98,7 +94,7 @@ exports.calculateTradeSlippage = calculateTradeSlippage;
|
|
|
98
94
|
*/
|
|
99
95
|
function calculateTradeAcquiredAmounts(direction, amount, market, inputAssetType = 'quote', oraclePriceData, useSpread = true) {
|
|
100
96
|
if (amount.eq(numericConstants_1.ZERO)) {
|
|
101
|
-
return [numericConstants_1.ZERO, numericConstants_1.ZERO];
|
|
97
|
+
return [numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO];
|
|
102
98
|
}
|
|
103
99
|
const swapDirection = (0, amm_1.getSwapDirection)(inputAssetType, direction);
|
|
104
100
|
let amm;
|
|
@@ -117,7 +113,8 @@ function calculateTradeAcquiredAmounts(direction, amount, market, inputAssetType
|
|
|
117
113
|
const [newQuoteAssetReserve, newBaseAssetReserve] = (0, amm_1.calculateAmmReservesAfterSwap)(amm, inputAssetType, amount, swapDirection);
|
|
118
114
|
const acquiredBase = amm.baseAssetReserve.sub(newBaseAssetReserve);
|
|
119
115
|
const acquiredQuote = amm.quoteAssetReserve.sub(newQuoteAssetReserve);
|
|
120
|
-
|
|
116
|
+
const acquiredQuoteAssetamount = (0, amm_1.calculateQuoteAssetAmountSwapped)(acquiredQuote.abs(), amm.pegMultiplier, swapDirection);
|
|
117
|
+
return [acquiredBase, acquiredQuote, acquiredQuoteAssetamount];
|
|
121
118
|
}
|
|
122
119
|
exports.calculateTradeAcquiredAmounts = calculateTradeAcquiredAmounts;
|
|
123
120
|
/**
|
package/lib/orderParams.d.ts
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
|
-
import {
|
|
2
|
+
import { OptionalOrderParams, OrderTriggerCondition } from './types';
|
|
3
3
|
import { BN } from '@project-serum/anchor';
|
|
4
|
-
export declare function getLimitOrderParams(
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export declare function
|
|
4
|
+
export declare function getLimitOrderParams(params: Omit<OptionalOrderParams, 'orderType'> & {
|
|
5
|
+
price: BN;
|
|
6
|
+
}): OptionalOrderParams;
|
|
7
|
+
export declare function getTriggerMarketOrderParams(params: Omit<OptionalOrderParams, 'orderType'> & {
|
|
8
|
+
triggerCondition: OrderTriggerCondition;
|
|
9
|
+
triggerPrice: BN;
|
|
10
|
+
}): OptionalOrderParams;
|
|
11
|
+
export declare function getTriggerLimitOrderParams(params: Omit<OptionalOrderParams, 'orderType'> & {
|
|
12
|
+
triggerCondition: OrderTriggerCondition;
|
|
13
|
+
triggerPrice: BN;
|
|
14
|
+
price: BN;
|
|
15
|
+
}): OptionalOrderParams;
|
|
16
|
+
export declare function getMarketOrderParams(params: Omit<OptionalOrderParams, 'orderType'>): OptionalOrderParams;
|