@drift-labs/sdk 0.2.0-master.2 → 0.2.0-master.5
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/accounts/types.d.ts +0 -1
- package/lib/admin.d.ts +5 -3
- package/lib/admin.js +30 -7
- package/lib/clearingHouse.d.ts +8 -11
- package/lib/clearingHouse.js +49 -75
- package/lib/config.js +1 -1
- package/lib/idl/clearing_house.json +171 -36
- 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 +18 -11
- 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/orders.js +1 -9
- 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 +79 -151
- package/src/config.ts +1 -1
- package/src/idl/clearing_house.json +171 -36
- 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 -13
- package/src/math/position.ts +5 -1
- package/src/math/trade.ts +23 -25
- package/src/orderParams.ts +20 -141
- package/src/orders.ts +1 -8
- 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
|
},
|
|
@@ -381,11 +385,6 @@
|
|
|
381
385
|
"name": "authority",
|
|
382
386
|
"isMut": false,
|
|
383
387
|
"isSigner": true
|
|
384
|
-
},
|
|
385
|
-
{
|
|
386
|
-
"name": "oracle",
|
|
387
|
-
"isMut": false,
|
|
388
|
-
"isSigner": false
|
|
389
388
|
}
|
|
390
389
|
],
|
|
391
390
|
"args": [
|
|
@@ -473,11 +472,6 @@
|
|
|
473
472
|
"name": "user",
|
|
474
473
|
"isMut": true,
|
|
475
474
|
"isSigner": false
|
|
476
|
-
},
|
|
477
|
-
{
|
|
478
|
-
"name": "oracle",
|
|
479
|
-
"isMut": false,
|
|
480
|
-
"isSigner": false
|
|
481
475
|
}
|
|
482
476
|
],
|
|
483
477
|
"args": [
|
|
@@ -512,11 +506,6 @@
|
|
|
512
506
|
"name": "authority",
|
|
513
507
|
"isMut": false,
|
|
514
508
|
"isSigner": true
|
|
515
|
-
},
|
|
516
|
-
{
|
|
517
|
-
"name": "oracle",
|
|
518
|
-
"isMut": false,
|
|
519
|
-
"isSigner": false
|
|
520
509
|
}
|
|
521
510
|
],
|
|
522
511
|
"args": [
|
|
@@ -549,18 +538,13 @@
|
|
|
549
538
|
},
|
|
550
539
|
{
|
|
551
540
|
"name": "taker",
|
|
552
|
-
"isMut":
|
|
541
|
+
"isMut": true,
|
|
553
542
|
"isSigner": false
|
|
554
543
|
},
|
|
555
544
|
{
|
|
556
545
|
"name": "authority",
|
|
557
546
|
"isMut": false,
|
|
558
547
|
"isSigner": true
|
|
559
|
-
},
|
|
560
|
-
{
|
|
561
|
-
"name": "oracle",
|
|
562
|
-
"isMut": false,
|
|
563
|
-
"isSigner": false
|
|
564
548
|
}
|
|
565
549
|
],
|
|
566
550
|
"args": [
|
|
@@ -736,7 +720,7 @@
|
|
|
736
720
|
]
|
|
737
721
|
},
|
|
738
722
|
{
|
|
739
|
-
"name": "
|
|
723
|
+
"name": "withdrawFromMarketToInsuranceVault",
|
|
740
724
|
"accounts": [
|
|
741
725
|
{
|
|
742
726
|
"name": "state",
|
|
@@ -750,7 +734,7 @@
|
|
|
750
734
|
},
|
|
751
735
|
{
|
|
752
736
|
"name": "bank",
|
|
753
|
-
"isMut":
|
|
737
|
+
"isMut": true,
|
|
754
738
|
"isSigner": false
|
|
755
739
|
},
|
|
756
740
|
{
|
|
@@ -855,6 +839,11 @@
|
|
|
855
839
|
"isMut": false,
|
|
856
840
|
"isSigner": false
|
|
857
841
|
},
|
|
842
|
+
{
|
|
843
|
+
"name": "bank",
|
|
844
|
+
"isMut": true,
|
|
845
|
+
"isSigner": false
|
|
846
|
+
},
|
|
858
847
|
{
|
|
859
848
|
"name": "bankVault",
|
|
860
849
|
"isMut": true,
|
|
@@ -1113,6 +1102,62 @@
|
|
|
1113
1102
|
}
|
|
1114
1103
|
]
|
|
1115
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
|
+
},
|
|
1116
1161
|
{
|
|
1117
1162
|
"name": "updateCurveUpdateIntensity",
|
|
1118
1163
|
"accounts": [
|
|
@@ -1461,6 +1506,58 @@
|
|
|
1461
1506
|
}
|
|
1462
1507
|
]
|
|
1463
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
|
+
},
|
|
1464
1561
|
{
|
|
1465
1562
|
"name": "updateAdmin",
|
|
1466
1563
|
"accounts": [
|
|
@@ -1583,7 +1680,7 @@
|
|
|
1583
1680
|
]
|
|
1584
1681
|
},
|
|
1585
1682
|
{
|
|
1586
|
-
"name": "
|
|
1683
|
+
"name": "updateAuctionDuration",
|
|
1587
1684
|
"accounts": [
|
|
1588
1685
|
{
|
|
1589
1686
|
"name": "admin",
|
|
@@ -1598,7 +1695,11 @@
|
|
|
1598
1695
|
],
|
|
1599
1696
|
"args": [
|
|
1600
1697
|
{
|
|
1601
|
-
"name": "
|
|
1698
|
+
"name": "minAuctionDuration",
|
|
1699
|
+
"type": "u8"
|
|
1700
|
+
},
|
|
1701
|
+
{
|
|
1702
|
+
"name": "maxAuctionDuration",
|
|
1602
1703
|
"type": "u8"
|
|
1603
1704
|
}
|
|
1604
1705
|
]
|
|
@@ -1695,6 +1796,10 @@
|
|
|
1695
1796
|
{
|
|
1696
1797
|
"name": "maintenanceLiabilityWeight",
|
|
1697
1798
|
"type": "u128"
|
|
1799
|
+
},
|
|
1800
|
+
{
|
|
1801
|
+
"name": "imfFactor",
|
|
1802
|
+
"type": "u128"
|
|
1698
1803
|
}
|
|
1699
1804
|
]
|
|
1700
1805
|
}
|
|
@@ -1772,6 +1877,22 @@
|
|
|
1772
1877
|
"name": "unsettledLoss",
|
|
1773
1878
|
"type": "u128"
|
|
1774
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
|
+
},
|
|
1775
1896
|
{
|
|
1776
1897
|
"name": "padding0",
|
|
1777
1898
|
"type": "u32"
|
|
@@ -1905,7 +2026,11 @@
|
|
|
1905
2026
|
"type": "u128"
|
|
1906
2027
|
},
|
|
1907
2028
|
{
|
|
1908
|
-
"name": "
|
|
2029
|
+
"name": "minAuctionDuration",
|
|
2030
|
+
"type": "u8"
|
|
2031
|
+
},
|
|
2032
|
+
{
|
|
2033
|
+
"name": "maxAuctionDuration",
|
|
1909
2034
|
"type": "u8"
|
|
1910
2035
|
},
|
|
1911
2036
|
{
|
|
@@ -2010,10 +2135,6 @@
|
|
|
2010
2135
|
"name": "userOrderId",
|
|
2011
2136
|
"type": "u8"
|
|
2012
2137
|
},
|
|
2013
|
-
{
|
|
2014
|
-
"name": "quoteAssetAmount",
|
|
2015
|
-
"type": "u128"
|
|
2016
|
-
},
|
|
2017
2138
|
{
|
|
2018
2139
|
"name": "baseAssetAmount",
|
|
2019
2140
|
"type": "u128"
|
|
@@ -2062,6 +2183,10 @@
|
|
|
2062
2183
|
"name": "oraclePriceOffset",
|
|
2063
2184
|
"type": "i128"
|
|
2064
2185
|
},
|
|
2186
|
+
{
|
|
2187
|
+
"name": "auctionDuration",
|
|
2188
|
+
"type": "u8"
|
|
2189
|
+
},
|
|
2065
2190
|
{
|
|
2066
2191
|
"name": "padding0",
|
|
2067
2192
|
"type": "bool"
|
|
@@ -2224,6 +2349,14 @@
|
|
|
2224
2349
|
"name": "minimumQuoteAssetTradeSize",
|
|
2225
2350
|
"type": "u128"
|
|
2226
2351
|
},
|
|
2352
|
+
{
|
|
2353
|
+
"name": "maxBaseAssetAmountRatio",
|
|
2354
|
+
"type": "u16"
|
|
2355
|
+
},
|
|
2356
|
+
{
|
|
2357
|
+
"name": "maxSlippageRatio",
|
|
2358
|
+
"type": "u16"
|
|
2359
|
+
},
|
|
2227
2360
|
{
|
|
2228
2361
|
"name": "baseAssetAmountStepSize",
|
|
2229
2362
|
"type": "u128"
|
|
@@ -2708,10 +2841,6 @@
|
|
|
2708
2841
|
"defined": "PositionDirection"
|
|
2709
2842
|
}
|
|
2710
2843
|
},
|
|
2711
|
-
{
|
|
2712
|
-
"name": "quoteAssetAmount",
|
|
2713
|
-
"type": "u128"
|
|
2714
|
-
},
|
|
2715
2844
|
{
|
|
2716
2845
|
"name": "baseAssetAmount",
|
|
2717
2846
|
"type": "u128"
|
|
@@ -2924,6 +3053,12 @@
|
|
|
2924
3053
|
},
|
|
2925
3054
|
{
|
|
2926
3055
|
"name": "OraclePriceBreachedLimitPrice"
|
|
3056
|
+
},
|
|
3057
|
+
{
|
|
3058
|
+
"name": "MarketOrderFilledToLimitPrice"
|
|
3059
|
+
},
|
|
3060
|
+
{
|
|
3061
|
+
"name": "MarketOrderAuctionExpired"
|
|
2927
3062
|
}
|
|
2928
3063
|
]
|
|
2929
3064
|
}
|
|
@@ -3711,8 +3846,8 @@
|
|
|
3711
3846
|
},
|
|
3712
3847
|
{
|
|
3713
3848
|
"code": 6044,
|
|
3714
|
-
"name": "
|
|
3715
|
-
"msg": "
|
|
3849
|
+
"name": "FillOrderDidNotUpdateState",
|
|
3850
|
+
"msg": "FillOrderDidNotUpdateState"
|
|
3716
3851
|
},
|
|
3717
3852
|
{
|
|
3718
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
|
@@ -5,6 +5,7 @@ const types_1 = require("../types");
|
|
|
5
5
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
6
|
const anchor_1 = require("@project-serum/anchor");
|
|
7
7
|
const auction_1 = require("./auction");
|
|
8
|
+
const market_1 = require("./market");
|
|
8
9
|
function isOrderRiskIncreasing(user, order) {
|
|
9
10
|
if ((0, types_1.isVariant)(order.status, 'init')) {
|
|
10
11
|
return false;
|
|
@@ -78,23 +79,29 @@ function standardizeBaseAssetAmount(baseAssetAmount, stepSize) {
|
|
|
78
79
|
return baseAssetAmount.sub(remainder);
|
|
79
80
|
}
|
|
80
81
|
exports.standardizeBaseAssetAmount = standardizeBaseAssetAmount;
|
|
81
|
-
function getLimitPrice(order, oraclePriceData, slot) {
|
|
82
|
+
function getLimitPrice(order, market, oraclePriceData, slot) {
|
|
82
83
|
let limitPrice;
|
|
83
84
|
if (!order.oraclePriceOffset.eq(numericConstants_1.ZERO)) {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
85
|
+
limitPrice = oraclePriceData.price.add(order.oraclePriceOffset);
|
|
86
|
+
}
|
|
87
|
+
else if ((0, types_1.isOneOfVariant)(order.orderType, ['market', 'triggerMarket'])) {
|
|
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);
|
|
89
98
|
}
|
|
90
99
|
else {
|
|
91
|
-
|
|
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);
|
|
92
103
|
}
|
|
93
104
|
}
|
|
94
|
-
else if ((0, types_1.isVariant)(order.orderType, 'market') ||
|
|
95
|
-
(0, types_1.isVariant)(order.orderType, 'triggerMarket')) {
|
|
96
|
-
limitPrice = (0, auction_1.getAuctionPrice)(order, slot);
|
|
97
|
-
}
|
|
98
105
|
else {
|
|
99
106
|
limitPrice = order.price;
|
|
100
107
|
}
|
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;
|