@drift-labs/sdk 0.2.0-master.4 → 0.2.0-temp.0
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 +1 -3
- package/lib/admin.js +2 -20
- package/lib/clearingHouse.d.ts +5 -6
- package/lib/clearingHouse.js +3 -20
- package/lib/idl/clearing_house.json +13 -83
- package/lib/index.d.ts +1 -2
- package/lib/index.js +1 -6
- package/lib/math/amm.js +35 -7
- package/lib/math/auction.js +1 -4
- package/lib/math/orders.d.ts +2 -2
- package/lib/math/orders.js +2 -19
- package/lib/math/trade.d.ts +1 -1
- package/lib/math/trade.js +10 -7
- package/lib/orderParams.d.ts +5 -14
- package/lib/orderParams.js +96 -8
- package/lib/types.d.ts +1 -57
- package/lib/types.js +1 -36
- package/package.json +3 -3
- package/src/admin.ts +4 -35
- package/src/clearingHouse.ts +25 -27
- package/src/idl/clearing_house.json +13 -83
- package/src/index.ts +1 -2
- package/src/math/amm.ts +49 -19
- package/src/math/auction.ts +1 -5
- package/src/math/orders.ts +3 -17
- package/src/math/trade.ts +25 -23
- package/src/orderParams.ts +141 -20
- package/src/types.ts +2 -54
package/lib/admin.d.ts
CHANGED
|
@@ -38,7 +38,5 @@ export declare class Admin extends ClearingHouse {
|
|
|
38
38
|
updateFundingPaused(fundingPaused: boolean): Promise<TransactionSignature>;
|
|
39
39
|
updateExchangePaused(exchangePaused: boolean): Promise<TransactionSignature>;
|
|
40
40
|
disableAdminControlsPrices(): Promise<TransactionSignature>;
|
|
41
|
-
|
|
42
|
-
updateMaxBaseAssetAmountRatio(marketIndex: BN, maxBaseAssetAmountRatio: number): Promise<TransactionSignature>;
|
|
43
|
-
updateMaxSlippageRatio(marketIndex: BN, maxSlippageRatio: number): Promise<TransactionSignature>;
|
|
41
|
+
updateOrderAuctionTime(time: BN | number): Promise<TransactionSignature>;
|
|
44
42
|
}
|
package/lib/admin.js
CHANGED
|
@@ -403,31 +403,13 @@ class Admin extends clearingHouse_1.ClearingHouse {
|
|
|
403
403
|
},
|
|
404
404
|
});
|
|
405
405
|
}
|
|
406
|
-
async
|
|
407
|
-
return await this.program.rpc.
|
|
406
|
+
async updateOrderAuctionTime(time) {
|
|
407
|
+
return await this.program.rpc.updateOrderAuctionTime(typeof time === 'number' ? time : time.toNumber, {
|
|
408
408
|
accounts: {
|
|
409
409
|
admin: this.wallet.publicKey,
|
|
410
410
|
state: await this.getStatePublicKey(),
|
|
411
411
|
},
|
|
412
412
|
});
|
|
413
413
|
}
|
|
414
|
-
async updateMaxBaseAssetAmountRatio(marketIndex, maxBaseAssetAmountRatio) {
|
|
415
|
-
return await this.program.rpc.updateMaxBaseAssetAmountRatio(maxBaseAssetAmountRatio, {
|
|
416
|
-
accounts: {
|
|
417
|
-
admin: this.wallet.publicKey,
|
|
418
|
-
state: await this.getStatePublicKey(),
|
|
419
|
-
market: this.getMarketAccount(marketIndex).pubkey,
|
|
420
|
-
},
|
|
421
|
-
});
|
|
422
|
-
}
|
|
423
|
-
async updateMaxSlippageRatio(marketIndex, maxSlippageRatio) {
|
|
424
|
-
return await this.program.rpc.updateMaxSlippageRatio(maxSlippageRatio, {
|
|
425
|
-
accounts: {
|
|
426
|
-
admin: this.wallet.publicKey,
|
|
427
|
-
state: await this.getStatePublicKey(),
|
|
428
|
-
market: this.getMarketAccount(marketIndex).pubkey,
|
|
429
|
-
},
|
|
430
|
-
});
|
|
431
|
-
}
|
|
432
414
|
}
|
|
433
415
|
exports.Admin = Admin;
|
package/lib/clearingHouse.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="bn.js" />
|
|
3
3
|
import { AnchorProvider, BN, Program } from '@project-serum/anchor';
|
|
4
|
-
import { StateAccount, IWallet, PositionDirection, UserAccount, MarketAccount, OrderParams, Order, BankAccount, UserBankBalance, MakerInfo
|
|
4
|
+
import { StateAccount, IWallet, PositionDirection, UserAccount, MarketAccount, OrderParams, Order, BankAccount, UserBankBalance, MakerInfo } from './types';
|
|
5
5
|
import { Connection, PublicKey, TransactionSignature, ConfirmOptions, TransactionInstruction, AccountMeta } from '@solana/web3.js';
|
|
6
6
|
import { MockUSDCFaucet } from './mockUSDCFaucet';
|
|
7
7
|
import { EventEmitter } from 'events';
|
|
@@ -95,9 +95,8 @@ export declare class ClearingHouse {
|
|
|
95
95
|
updateBankCumulativeInterest(bankIndex: BN): Promise<TransactionSignature>;
|
|
96
96
|
updateBankCumulativeInterestIx(bankIndex: BN): Promise<TransactionInstruction>;
|
|
97
97
|
openPosition(direction: PositionDirection, amount: BN, marketIndex: BN, limitPrice?: BN): Promise<TransactionSignature>;
|
|
98
|
-
placeOrder(orderParams:
|
|
99
|
-
|
|
100
|
-
getPlaceOrderIx(orderParams: OptionalOrderParams): Promise<TransactionInstruction>;
|
|
98
|
+
placeOrder(orderParams: OrderParams): Promise<TransactionSignature>;
|
|
99
|
+
getPlaceOrderIx(orderParams: OrderParams): Promise<TransactionInstruction>;
|
|
101
100
|
updateAMMs(marketIndexes: BN[]): Promise<TransactionSignature>;
|
|
102
101
|
getUpdateAMMsIx(marketIndexes: BN[]): Promise<TransactionInstruction>;
|
|
103
102
|
cancelOrder(orderId?: BN): Promise<TransactionSignature>;
|
|
@@ -108,8 +107,8 @@ export declare class ClearingHouse {
|
|
|
108
107
|
getFillOrderIx(userAccountPublicKey: PublicKey, userAccount: UserAccount, order: Order, makerInfo?: MakerInfo): Promise<TransactionInstruction>;
|
|
109
108
|
triggerOrder(userAccountPublicKey: PublicKey, user: UserAccount, order: Order): Promise<TransactionSignature>;
|
|
110
109
|
getTriggerOrderIx(userAccountPublicKey: PublicKey, userAccount: UserAccount, order: Order): Promise<TransactionInstruction>;
|
|
111
|
-
placeAndTake(orderParams:
|
|
112
|
-
getPlaceAndTakeIx(orderParams:
|
|
110
|
+
placeAndTake(orderParams: OrderParams, makerInfo?: MakerInfo): Promise<TransactionSignature>;
|
|
111
|
+
getPlaceAndTakeIx(orderParams: OrderParams, makerInfo?: MakerInfo): Promise<TransactionInstruction>;
|
|
113
112
|
/**
|
|
114
113
|
* Close an entire position. If you want to reduce a position, use the {@link openPosition} method in the opposite direction of the current position.
|
|
115
114
|
* @param marketIndex
|
package/lib/clearingHouse.js
CHANGED
|
@@ -29,7 +29,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
29
29
|
exports.ClearingHouse = void 0;
|
|
30
30
|
const anchor_1 = require("@project-serum/anchor");
|
|
31
31
|
const spl_token_1 = require("@solana/spl-token");
|
|
32
|
-
const types_1 = require("./types");
|
|
33
32
|
const anchor = __importStar(require("@project-serum/anchor"));
|
|
34
33
|
const clearing_house_json_1 = __importDefault(require("./idl/clearing_house.json"));
|
|
35
34
|
const web3_js_1 = require("@solana/web3.js");
|
|
@@ -43,6 +42,7 @@ const pollingClearingHouseAccountSubscriber_1 = require("./accounts/pollingClear
|
|
|
43
42
|
const webSocketClearingHouseAccountSubscriber_1 = require("./accounts/webSocketClearingHouseAccountSubscriber");
|
|
44
43
|
const retryTxSender_1 = require("./tx/retryTxSender");
|
|
45
44
|
const clearingHouseUser_1 = require("./clearingHouseUser");
|
|
45
|
+
const orderParams_1 = require("./orderParams");
|
|
46
46
|
const config_1 = require("./config");
|
|
47
47
|
/**
|
|
48
48
|
* # ClearingHouse
|
|
@@ -482,24 +482,14 @@ class ClearingHouse {
|
|
|
482
482
|
});
|
|
483
483
|
}
|
|
484
484
|
async openPosition(direction, amount, marketIndex, limitPrice) {
|
|
485
|
-
return await this.placeAndTake(
|
|
486
|
-
orderType: types_1.OrderType.MARKET,
|
|
487
|
-
marketIndex,
|
|
488
|
-
direction,
|
|
489
|
-
baseAssetAmount: amount,
|
|
490
|
-
price: limitPrice,
|
|
491
|
-
});
|
|
485
|
+
return await this.placeAndTake((0, orderParams_1.getMarketOrderParams)(marketIndex, direction, numericConstants_1.ZERO, amount, false, limitPrice));
|
|
492
486
|
}
|
|
493
487
|
async placeOrder(orderParams) {
|
|
494
488
|
const { txSig, slot } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getPlaceOrderIx(orderParams)), [], this.opts);
|
|
495
489
|
this.marketLastSlotCache.set(orderParams.marketIndex.toNumber(), slot);
|
|
496
490
|
return txSig;
|
|
497
491
|
}
|
|
498
|
-
getOrderParams(optionalOrderParams) {
|
|
499
|
-
return Object.assign({}, types_1.DefaultOrderParams, optionalOrderParams);
|
|
500
|
-
}
|
|
501
492
|
async getPlaceOrderIx(orderParams) {
|
|
502
|
-
orderParams = this.getOrderParams(orderParams);
|
|
503
493
|
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
504
494
|
const remainingAccounts = this.getRemainingAccounts({
|
|
505
495
|
writableMarketIndex: orderParams.marketIndex,
|
|
@@ -710,7 +700,6 @@ class ClearingHouse {
|
|
|
710
700
|
return txSig;
|
|
711
701
|
}
|
|
712
702
|
async getPlaceAndTakeIx(orderParams, makerInfo) {
|
|
713
|
-
orderParams = this.getOrderParams(orderParams);
|
|
714
703
|
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
715
704
|
const remainingAccounts = this.getRemainingAccounts({
|
|
716
705
|
writableMarketIndex: orderParams.marketIndex,
|
|
@@ -744,13 +733,7 @@ class ClearingHouse {
|
|
|
744
733
|
if (!userPosition) {
|
|
745
734
|
throw Error(`No position in market ${marketIndex.toString()}`);
|
|
746
735
|
}
|
|
747
|
-
return await this.placeAndTake(
|
|
748
|
-
orderType: types_1.OrderType.MARKET,
|
|
749
|
-
marketIndex,
|
|
750
|
-
direction: (0, position_1.findDirectionToClose)(userPosition),
|
|
751
|
-
baseAssetAmount: userPosition.baseAssetAmount,
|
|
752
|
-
reduceOnly: true,
|
|
753
|
-
});
|
|
736
|
+
return await this.placeAndTake((0, orderParams_1.getMarketOrderParams)(marketIndex, (0, position_1.findDirectionToClose)(userPosition), numericConstants_1.ZERO, userPosition.baseAssetAmount, true, undefined));
|
|
754
737
|
}
|
|
755
738
|
async settlePNLs(users, marketIndex) {
|
|
756
739
|
const ixs = [];
|
|
@@ -1441,58 +1441,6 @@
|
|
|
1441
1441
|
}
|
|
1442
1442
|
]
|
|
1443
1443
|
},
|
|
1444
|
-
{
|
|
1445
|
-
"name": "updateMarketMaxSlippageRatio",
|
|
1446
|
-
"accounts": [
|
|
1447
|
-
{
|
|
1448
|
-
"name": "admin",
|
|
1449
|
-
"isMut": false,
|
|
1450
|
-
"isSigner": true
|
|
1451
|
-
},
|
|
1452
|
-
{
|
|
1453
|
-
"name": "state",
|
|
1454
|
-
"isMut": false,
|
|
1455
|
-
"isSigner": false
|
|
1456
|
-
},
|
|
1457
|
-
{
|
|
1458
|
-
"name": "market",
|
|
1459
|
-
"isMut": true,
|
|
1460
|
-
"isSigner": false
|
|
1461
|
-
}
|
|
1462
|
-
],
|
|
1463
|
-
"args": [
|
|
1464
|
-
{
|
|
1465
|
-
"name": "maxSlippageRatio",
|
|
1466
|
-
"type": "u16"
|
|
1467
|
-
}
|
|
1468
|
-
]
|
|
1469
|
-
},
|
|
1470
|
-
{
|
|
1471
|
-
"name": "updateMaxBaseAssetAmountRatio",
|
|
1472
|
-
"accounts": [
|
|
1473
|
-
{
|
|
1474
|
-
"name": "admin",
|
|
1475
|
-
"isMut": false,
|
|
1476
|
-
"isSigner": true
|
|
1477
|
-
},
|
|
1478
|
-
{
|
|
1479
|
-
"name": "state",
|
|
1480
|
-
"isMut": false,
|
|
1481
|
-
"isSigner": false
|
|
1482
|
-
},
|
|
1483
|
-
{
|
|
1484
|
-
"name": "market",
|
|
1485
|
-
"isMut": true,
|
|
1486
|
-
"isSigner": false
|
|
1487
|
-
}
|
|
1488
|
-
],
|
|
1489
|
-
"args": [
|
|
1490
|
-
{
|
|
1491
|
-
"name": "maxBaseAssetAmountRatio",
|
|
1492
|
-
"type": "u16"
|
|
1493
|
-
}
|
|
1494
|
-
]
|
|
1495
|
-
},
|
|
1496
1444
|
{
|
|
1497
1445
|
"name": "updateAdmin",
|
|
1498
1446
|
"accounts": [
|
|
@@ -1615,7 +1563,7 @@
|
|
|
1615
1563
|
]
|
|
1616
1564
|
},
|
|
1617
1565
|
{
|
|
1618
|
-
"name": "
|
|
1566
|
+
"name": "updateOrderAuctionTime",
|
|
1619
1567
|
"accounts": [
|
|
1620
1568
|
{
|
|
1621
1569
|
"name": "admin",
|
|
@@ -1630,11 +1578,7 @@
|
|
|
1630
1578
|
],
|
|
1631
1579
|
"args": [
|
|
1632
1580
|
{
|
|
1633
|
-
"name": "
|
|
1634
|
-
"type": "u8"
|
|
1635
|
-
},
|
|
1636
|
-
{
|
|
1637
|
-
"name": "maxAuctionDuration",
|
|
1581
|
+
"name": "orderAuctionTime",
|
|
1638
1582
|
"type": "u8"
|
|
1639
1583
|
}
|
|
1640
1584
|
]
|
|
@@ -1941,11 +1885,7 @@
|
|
|
1941
1885
|
"type": "u128"
|
|
1942
1886
|
},
|
|
1943
1887
|
{
|
|
1944
|
-
"name": "
|
|
1945
|
-
"type": "u8"
|
|
1946
|
-
},
|
|
1947
|
-
{
|
|
1948
|
-
"name": "maxAuctionDuration",
|
|
1888
|
+
"name": "orderAuctionDuration",
|
|
1949
1889
|
"type": "u8"
|
|
1950
1890
|
},
|
|
1951
1891
|
{
|
|
@@ -2050,6 +1990,10 @@
|
|
|
2050
1990
|
"name": "userOrderId",
|
|
2051
1991
|
"type": "u8"
|
|
2052
1992
|
},
|
|
1993
|
+
{
|
|
1994
|
+
"name": "quoteAssetAmount",
|
|
1995
|
+
"type": "u128"
|
|
1996
|
+
},
|
|
2053
1997
|
{
|
|
2054
1998
|
"name": "baseAssetAmount",
|
|
2055
1999
|
"type": "u128"
|
|
@@ -2098,10 +2042,6 @@
|
|
|
2098
2042
|
"name": "oraclePriceOffset",
|
|
2099
2043
|
"type": "i128"
|
|
2100
2044
|
},
|
|
2101
|
-
{
|
|
2102
|
-
"name": "auctionDuration",
|
|
2103
|
-
"type": "u8"
|
|
2104
|
-
},
|
|
2105
2045
|
{
|
|
2106
2046
|
"name": "padding0",
|
|
2107
2047
|
"type": "bool"
|
|
@@ -2264,14 +2204,6 @@
|
|
|
2264
2204
|
"name": "minimumQuoteAssetTradeSize",
|
|
2265
2205
|
"type": "u128"
|
|
2266
2206
|
},
|
|
2267
|
-
{
|
|
2268
|
-
"name": "maxBaseAssetAmountRatio",
|
|
2269
|
-
"type": "u16"
|
|
2270
|
-
},
|
|
2271
|
-
{
|
|
2272
|
-
"name": "maxSlippageRatio",
|
|
2273
|
-
"type": "u16"
|
|
2274
|
-
},
|
|
2275
2207
|
{
|
|
2276
2208
|
"name": "baseAssetAmountStepSize",
|
|
2277
2209
|
"type": "u128"
|
|
@@ -2756,6 +2688,10 @@
|
|
|
2756
2688
|
"defined": "PositionDirection"
|
|
2757
2689
|
}
|
|
2758
2690
|
},
|
|
2691
|
+
{
|
|
2692
|
+
"name": "quoteAssetAmount",
|
|
2693
|
+
"type": "u128"
|
|
2694
|
+
},
|
|
2759
2695
|
{
|
|
2760
2696
|
"name": "baseAssetAmount",
|
|
2761
2697
|
"type": "u128"
|
|
@@ -2968,12 +2904,6 @@
|
|
|
2968
2904
|
},
|
|
2969
2905
|
{
|
|
2970
2906
|
"name": "OraclePriceBreachedLimitPrice"
|
|
2971
|
-
},
|
|
2972
|
-
{
|
|
2973
|
-
"name": "MarketOrderFilledToLimitPrice"
|
|
2974
|
-
},
|
|
2975
|
-
{
|
|
2976
|
-
"name": "MarketOrderAuctionExpired"
|
|
2977
2907
|
}
|
|
2978
2908
|
]
|
|
2979
2909
|
}
|
|
@@ -3761,8 +3691,8 @@
|
|
|
3761
3691
|
},
|
|
3762
3692
|
{
|
|
3763
3693
|
"code": 6044,
|
|
3764
|
-
"name": "
|
|
3765
|
-
"msg": "
|
|
3694
|
+
"name": "CouldNotFillOrder",
|
|
3695
|
+
"msg": "CouldNotFillOrder"
|
|
3766
3696
|
},
|
|
3767
3697
|
{
|
|
3768
3698
|
"code": 6045,
|
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { BN } from '@project-serum/anchor';
|
|
2
2
|
import { PublicKey } from '@solana/web3.js';
|
|
3
|
-
import pyth from '@pythnetwork/client';
|
|
4
3
|
export * from './mockUSDCFaucet';
|
|
5
4
|
export * from './oracles/types';
|
|
6
5
|
export * from './oracles/pythClient';
|
|
@@ -48,4 +47,4 @@ export * from './util/tps';
|
|
|
48
47
|
export * from './math/bankBalance';
|
|
49
48
|
export * from './constants/banks';
|
|
50
49
|
export * from './clearingHouseConfig';
|
|
51
|
-
export { BN, PublicKey
|
|
50
|
+
export { BN, PublicKey };
|
package/lib/index.js
CHANGED
|
@@ -13,17 +13,12 @@ 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
|
-
};
|
|
19
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
-
exports.
|
|
17
|
+
exports.PublicKey = exports.BN = void 0;
|
|
21
18
|
const anchor_1 = require("@project-serum/anchor");
|
|
22
19
|
Object.defineProperty(exports, "BN", { enumerable: true, get: function () { return anchor_1.BN; } });
|
|
23
20
|
const web3_js_1 = require("@solana/web3.js");
|
|
24
21
|
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;
|
|
27
22
|
__exportStar(require("./mockUSDCFaucet"), exports);
|
|
28
23
|
__exportStar(require("./oracles/types"), exports);
|
|
29
24
|
__exportStar(require("./oracles/pythClient"), exports);
|
package/lib/math/amm.js
CHANGED
|
@@ -227,8 +227,9 @@ 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;
|
|
230
231
|
if (amm.baseSpread == 0 || amm.curveUpdateIntensity == 0) {
|
|
231
|
-
return
|
|
232
|
+
return spread;
|
|
232
233
|
}
|
|
233
234
|
const markPrice = calculatePrice(amm.baseAssetReserve, amm.quoteAssetReserve, amm.pegMultiplier);
|
|
234
235
|
const targetPrice = (oraclePriceData === null || oraclePriceData === void 0 ? void 0 : oraclePriceData.price) || markPrice;
|
|
@@ -240,13 +241,40 @@ function calculateSpread(amm, direction, oraclePriceData) {
|
|
|
240
241
|
const confIntervalPct = confInterval
|
|
241
242
|
.mul(numericConstants_1.BID_ASK_SPREAD_PRECISION)
|
|
242
243
|
.div(markPrice);
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
spread =
|
|
244
|
+
// oracle retreat
|
|
245
|
+
if (((0, types_1.isVariant)(direction, 'long') && targetMarkSpreadPct.lt(numericConstants_1.ZERO)) ||
|
|
246
|
+
((0, types_1.isVariant)(direction, 'short') && targetMarkSpreadPct.gt(numericConstants_1.ZERO))) {
|
|
247
|
+
spread = Math.max(spread, targetMarkSpreadPct.abs().toNumber() + confIntervalPct.abs().toNumber());
|
|
247
248
|
}
|
|
248
|
-
|
|
249
|
-
|
|
249
|
+
// inventory skew
|
|
250
|
+
const MAX_INVENTORY_SKEW = 5;
|
|
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;
|
|
250
278
|
}
|
|
251
279
|
return spread;
|
|
252
280
|
}
|
package/lib/math/auction.js
CHANGED
|
@@ -4,10 +4,7 @@ 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
|
-
|
|
8
|
-
return true;
|
|
9
|
-
}
|
|
10
|
-
return new _1.BN(slot).sub(order.slot).gt(new _1.BN(order.auctionDuration));
|
|
7
|
+
return new _1.BN(slot).sub(order.slot).gte(new _1.BN(order.auctionDuration));
|
|
11
8
|
}
|
|
12
9
|
exports.isAuctionComplete = isAuctionComplete;
|
|
13
10
|
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 {
|
|
3
|
+
import { 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,
|
|
10
|
+
export declare function getLimitPrice(order: Order, oraclePriceData: OraclePriceData, slot: number): BN;
|
package/lib/math/orders.js
CHANGED
|
@@ -3,9 +3,7 @@ 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");
|
|
7
6
|
const auction_1 = require("./auction");
|
|
8
|
-
const market_1 = require("./market");
|
|
9
7
|
function isOrderRiskIncreasing(user, order) {
|
|
10
8
|
if ((0, types_1.isVariant)(order.status, 'init')) {
|
|
11
9
|
return false;
|
|
@@ -79,28 +77,13 @@ function standardizeBaseAssetAmount(baseAssetAmount, stepSize) {
|
|
|
79
77
|
return baseAssetAmount.sub(remainder);
|
|
80
78
|
}
|
|
81
79
|
exports.standardizeBaseAssetAmount = standardizeBaseAssetAmount;
|
|
82
|
-
function getLimitPrice(order,
|
|
80
|
+
function getLimitPrice(order, oraclePriceData, slot) {
|
|
83
81
|
let limitPrice;
|
|
84
82
|
if (!order.oraclePriceOffset.eq(numericConstants_1.ZERO)) {
|
|
85
83
|
limitPrice = oraclePriceData.price.add(order.oraclePriceOffset);
|
|
86
84
|
}
|
|
87
85
|
else if ((0, types_1.isOneOfVariant)(order.orderType, ['market', 'triggerMarket'])) {
|
|
88
|
-
|
|
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
|
-
}
|
|
86
|
+
limitPrice = (0, auction_1.getAuctionPrice)(order, slot);
|
|
104
87
|
}
|
|
105
88
|
else {
|
|
106
89
|
limitPrice = order.price;
|
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];
|
|
37
37
|
/**
|
|
38
38
|
* calculateTargetPriceTrade
|
|
39
39
|
* simple function for finding arbitraging trades
|
package/lib/math/trade.js
CHANGED
|
@@ -43,11 +43,15 @@ 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
|
|
46
|
+
const [acquiredBase, acquiredQuote] = calculateTradeAcquiredAmounts(direction, amount, market, inputAssetType, oraclePriceData, useSpread);
|
|
47
|
+
const swapDirection = (0, types_2.isVariant)(direction, 'long')
|
|
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
|
|
48
52
|
.mul(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO)
|
|
49
53
|
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
50
|
-
.div(
|
|
54
|
+
.div(acquiredBase.abs());
|
|
51
55
|
let amm;
|
|
52
56
|
if (useSpread && market.amm.baseSpread > 0) {
|
|
53
57
|
const { baseAssetReserve, quoteAssetReserve, sqrtK, newPeg } = (0, amm_1.calculateUpdatedAMMSpreadReserves)(market.amm, direction, oraclePriceData);
|
|
@@ -61,7 +65,7 @@ function calculateTradeSlippage(direction, amount, market, inputAssetType = 'quo
|
|
|
61
65
|
else {
|
|
62
66
|
amm = market.amm;
|
|
63
67
|
}
|
|
64
|
-
const newPrice = (0, amm_1.calculatePrice)(amm.baseAssetReserve.sub(
|
|
68
|
+
const newPrice = (0, amm_1.calculatePrice)(amm.baseAssetReserve.sub(acquiredBase), amm.quoteAssetReserve.sub(acquiredQuote), amm.pegMultiplier);
|
|
65
69
|
if (direction == types_1.PositionDirection.SHORT) {
|
|
66
70
|
(0, assert_1.assert)(newPrice.lte(oldPrice));
|
|
67
71
|
}
|
|
@@ -94,7 +98,7 @@ exports.calculateTradeSlippage = calculateTradeSlippage;
|
|
|
94
98
|
*/
|
|
95
99
|
function calculateTradeAcquiredAmounts(direction, amount, market, inputAssetType = 'quote', oraclePriceData, useSpread = true) {
|
|
96
100
|
if (amount.eq(numericConstants_1.ZERO)) {
|
|
97
|
-
return [numericConstants_1.ZERO, numericConstants_1.ZERO
|
|
101
|
+
return [numericConstants_1.ZERO, numericConstants_1.ZERO];
|
|
98
102
|
}
|
|
99
103
|
const swapDirection = (0, amm_1.getSwapDirection)(inputAssetType, direction);
|
|
100
104
|
let amm;
|
|
@@ -113,8 +117,7 @@ function calculateTradeAcquiredAmounts(direction, amount, market, inputAssetType
|
|
|
113
117
|
const [newQuoteAssetReserve, newBaseAssetReserve] = (0, amm_1.calculateAmmReservesAfterSwap)(amm, inputAssetType, amount, swapDirection);
|
|
114
118
|
const acquiredBase = amm.baseAssetReserve.sub(newBaseAssetReserve);
|
|
115
119
|
const acquiredQuote = amm.quoteAssetReserve.sub(newQuoteAssetReserve);
|
|
116
|
-
|
|
117
|
-
return [acquiredBase, acquiredQuote, acquiredQuoteAssetamount];
|
|
120
|
+
return [acquiredBase, acquiredQuote];
|
|
118
121
|
}
|
|
119
122
|
exports.calculateTradeAcquiredAmounts = calculateTradeAcquiredAmounts;
|
|
120
123
|
/**
|
package/lib/orderParams.d.ts
CHANGED
|
@@ -1,16 +1,7 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
|
-
import {
|
|
2
|
+
import { OrderParams, OrderTriggerCondition, PositionDirection } from './types';
|
|
3
3
|
import { BN } from '@project-serum/anchor';
|
|
4
|
-
export declare function getLimitOrderParams(
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export declare function
|
|
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;
|
|
4
|
+
export declare function getLimitOrderParams(marketIndex: BN, direction: PositionDirection, baseAssetAmount: BN, price: BN, reduceOnly: boolean, discountToken?: boolean, referrer?: boolean, userOrderId?: number, postOnly?: boolean, oraclePriceOffset?: BN, immediateOrCancel?: boolean): OrderParams;
|
|
5
|
+
export declare function getTriggerMarketOrderParams(marketIndex: BN, direction: PositionDirection, baseAssetAmount: BN, triggerPrice: BN, triggerCondition: OrderTriggerCondition, reduceOnly: boolean, discountToken?: boolean, referrer?: boolean, userOrderId?: number): OrderParams;
|
|
6
|
+
export declare function getTriggerLimitOrderParams(marketIndex: BN, direction: PositionDirection, baseAssetAmount: BN, price: BN, triggerPrice: BN, triggerCondition: OrderTriggerCondition, reduceOnly: boolean, discountToken?: boolean, referrer?: boolean, userOrderId?: number): OrderParams;
|
|
7
|
+
export declare function getMarketOrderParams(marketIndex: BN, direction: PositionDirection, quoteAssetAmount: BN, baseAssetAmount: BN, reduceOnly: boolean, price?: BN, discountToken?: boolean, referrer?: boolean): OrderParams;
|