@drift-labs/sdk 0.2.0-master.3 → 0.2.0-master.4
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 +3 -1
- package/lib/admin.js +20 -2
- package/lib/clearingHouse.d.ts +6 -5
- package/lib/clearingHouse.js +21 -4
- package/lib/idl/clearing_house.json +83 -13
- 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/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/types.d.ts +57 -1
- package/lib/types.js +36 -1
- package/package.json +3 -3
- package/src/admin.ts +35 -4
- package/src/clearingHouse.ts +28 -26
- package/src/idl/clearing_house.json +83 -13
- 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/trade.ts +23 -25
- package/src/orderParams.ts +20 -141
- package/src/types.ts +54 -2
package/lib/admin.d.ts
CHANGED
|
@@ -38,5 +38,7 @@ 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
|
-
|
|
41
|
+
updateAuctionDuration(minDuration: BN | number, maxDuration: BN | number): Promise<TransactionSignature>;
|
|
42
|
+
updateMaxBaseAssetAmountRatio(marketIndex: BN, maxBaseAssetAmountRatio: number): Promise<TransactionSignature>;
|
|
43
|
+
updateMaxSlippageRatio(marketIndex: BN, maxSlippageRatio: number): Promise<TransactionSignature>;
|
|
42
44
|
}
|
package/lib/admin.js
CHANGED
|
@@ -403,13 +403,31 @@ class Admin extends clearingHouse_1.ClearingHouse {
|
|
|
403
403
|
},
|
|
404
404
|
});
|
|
405
405
|
}
|
|
406
|
-
async
|
|
407
|
-
return await this.program.rpc.
|
|
406
|
+
async updateAuctionDuration(minDuration, maxDuration) {
|
|
407
|
+
return await this.program.rpc.updateAuctionDuration(typeof minDuration === 'number' ? minDuration : minDuration.toNumber(), typeof maxDuration === 'number' ? maxDuration : maxDuration.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
|
+
}
|
|
414
432
|
}
|
|
415
433
|
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 } from './types';
|
|
4
|
+
import { StateAccount, IWallet, PositionDirection, UserAccount, MarketAccount, OrderParams, Order, BankAccount, UserBankBalance, MakerInfo, OptionalOrderParams } 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,8 +95,9 @@ 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
|
-
|
|
98
|
+
placeOrder(orderParams: OptionalOrderParams): Promise<TransactionSignature>;
|
|
99
|
+
getOrderParams(optionalOrderParams: OptionalOrderParams): OrderParams;
|
|
100
|
+
getPlaceOrderIx(orderParams: OptionalOrderParams): Promise<TransactionInstruction>;
|
|
100
101
|
updateAMMs(marketIndexes: BN[]): Promise<TransactionSignature>;
|
|
101
102
|
getUpdateAMMsIx(marketIndexes: BN[]): Promise<TransactionInstruction>;
|
|
102
103
|
cancelOrder(orderId?: BN): Promise<TransactionSignature>;
|
|
@@ -107,8 +108,8 @@ export declare class ClearingHouse {
|
|
|
107
108
|
getFillOrderIx(userAccountPublicKey: PublicKey, userAccount: UserAccount, order: Order, makerInfo?: MakerInfo): Promise<TransactionInstruction>;
|
|
108
109
|
triggerOrder(userAccountPublicKey: PublicKey, user: UserAccount, order: Order): Promise<TransactionSignature>;
|
|
109
110
|
getTriggerOrderIx(userAccountPublicKey: PublicKey, userAccount: UserAccount, order: Order): Promise<TransactionInstruction>;
|
|
110
|
-
placeAndTake(orderParams:
|
|
111
|
-
getPlaceAndTakeIx(orderParams:
|
|
111
|
+
placeAndTake(orderParams: OptionalOrderParams, makerInfo?: MakerInfo): Promise<TransactionSignature>;
|
|
112
|
+
getPlaceAndTakeIx(orderParams: OptionalOrderParams, makerInfo?: MakerInfo): Promise<TransactionInstruction>;
|
|
112
113
|
/**
|
|
113
114
|
* Close an entire position. If you want to reduce a position, use the {@link openPosition} method in the opposite direction of the current position.
|
|
114
115
|
* @param marketIndex
|
package/lib/clearingHouse.js
CHANGED
|
@@ -29,6 +29,7 @@ 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");
|
|
32
33
|
const anchor = __importStar(require("@project-serum/anchor"));
|
|
33
34
|
const clearing_house_json_1 = __importDefault(require("./idl/clearing_house.json"));
|
|
34
35
|
const web3_js_1 = require("@solana/web3.js");
|
|
@@ -42,7 +43,6 @@ const pollingClearingHouseAccountSubscriber_1 = require("./accounts/pollingClear
|
|
|
42
43
|
const webSocketClearingHouseAccountSubscriber_1 = require("./accounts/webSocketClearingHouseAccountSubscriber");
|
|
43
44
|
const retryTxSender_1 = require("./tx/retryTxSender");
|
|
44
45
|
const clearingHouseUser_1 = require("./clearingHouseUser");
|
|
45
|
-
const orderParams_1 = require("./orderParams");
|
|
46
46
|
const config_1 = require("./config");
|
|
47
47
|
/**
|
|
48
48
|
* # ClearingHouse
|
|
@@ -482,14 +482,24 @@ class ClearingHouse {
|
|
|
482
482
|
});
|
|
483
483
|
}
|
|
484
484
|
async openPosition(direction, amount, marketIndex, limitPrice) {
|
|
485
|
-
return await this.placeAndTake(
|
|
485
|
+
return await this.placeAndTake({
|
|
486
|
+
orderType: types_1.OrderType.MARKET,
|
|
487
|
+
marketIndex,
|
|
488
|
+
direction,
|
|
489
|
+
baseAssetAmount: amount,
|
|
490
|
+
price: limitPrice,
|
|
491
|
+
});
|
|
486
492
|
}
|
|
487
493
|
async placeOrder(orderParams) {
|
|
488
494
|
const { txSig, slot } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getPlaceOrderIx(orderParams)), [], this.opts);
|
|
489
495
|
this.marketLastSlotCache.set(orderParams.marketIndex.toNumber(), slot);
|
|
490
496
|
return txSig;
|
|
491
497
|
}
|
|
498
|
+
getOrderParams(optionalOrderParams) {
|
|
499
|
+
return Object.assign({}, types_1.DefaultOrderParams, optionalOrderParams);
|
|
500
|
+
}
|
|
492
501
|
async getPlaceOrderIx(orderParams) {
|
|
502
|
+
orderParams = this.getOrderParams(orderParams);
|
|
493
503
|
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
494
504
|
const remainingAccounts = this.getRemainingAccounts({
|
|
495
505
|
writableMarketIndex: orderParams.marketIndex,
|
|
@@ -607,7 +617,7 @@ class ClearingHouse {
|
|
|
607
617
|
const market = this.getMarketAccount(position.marketIndex);
|
|
608
618
|
marketAccountInfos.push({
|
|
609
619
|
pubkey: market.pubkey,
|
|
610
|
-
isWritable:
|
|
620
|
+
isWritable: true,
|
|
611
621
|
isSigner: false,
|
|
612
622
|
});
|
|
613
623
|
oracleAccountInfos.push({
|
|
@@ -700,6 +710,7 @@ class ClearingHouse {
|
|
|
700
710
|
return txSig;
|
|
701
711
|
}
|
|
702
712
|
async getPlaceAndTakeIx(orderParams, makerInfo) {
|
|
713
|
+
orderParams = this.getOrderParams(orderParams);
|
|
703
714
|
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
704
715
|
const remainingAccounts = this.getRemainingAccounts({
|
|
705
716
|
writableMarketIndex: orderParams.marketIndex,
|
|
@@ -733,7 +744,13 @@ class ClearingHouse {
|
|
|
733
744
|
if (!userPosition) {
|
|
734
745
|
throw Error(`No position in market ${marketIndex.toString()}`);
|
|
735
746
|
}
|
|
736
|
-
return await this.placeAndTake(
|
|
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
|
+
});
|
|
737
754
|
}
|
|
738
755
|
async settlePNLs(users, marketIndex) {
|
|
739
756
|
const ixs = [];
|
|
@@ -1441,6 +1441,58 @@
|
|
|
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
|
+
},
|
|
1444
1496
|
{
|
|
1445
1497
|
"name": "updateAdmin",
|
|
1446
1498
|
"accounts": [
|
|
@@ -1563,7 +1615,7 @@
|
|
|
1563
1615
|
]
|
|
1564
1616
|
},
|
|
1565
1617
|
{
|
|
1566
|
-
"name": "
|
|
1618
|
+
"name": "updateAuctionDuration",
|
|
1567
1619
|
"accounts": [
|
|
1568
1620
|
{
|
|
1569
1621
|
"name": "admin",
|
|
@@ -1578,7 +1630,11 @@
|
|
|
1578
1630
|
],
|
|
1579
1631
|
"args": [
|
|
1580
1632
|
{
|
|
1581
|
-
"name": "
|
|
1633
|
+
"name": "minAuctionDuration",
|
|
1634
|
+
"type": "u8"
|
|
1635
|
+
},
|
|
1636
|
+
{
|
|
1637
|
+
"name": "maxAuctionDuration",
|
|
1582
1638
|
"type": "u8"
|
|
1583
1639
|
}
|
|
1584
1640
|
]
|
|
@@ -1885,7 +1941,11 @@
|
|
|
1885
1941
|
"type": "u128"
|
|
1886
1942
|
},
|
|
1887
1943
|
{
|
|
1888
|
-
"name": "
|
|
1944
|
+
"name": "minAuctionDuration",
|
|
1945
|
+
"type": "u8"
|
|
1946
|
+
},
|
|
1947
|
+
{
|
|
1948
|
+
"name": "maxAuctionDuration",
|
|
1889
1949
|
"type": "u8"
|
|
1890
1950
|
},
|
|
1891
1951
|
{
|
|
@@ -1990,10 +2050,6 @@
|
|
|
1990
2050
|
"name": "userOrderId",
|
|
1991
2051
|
"type": "u8"
|
|
1992
2052
|
},
|
|
1993
|
-
{
|
|
1994
|
-
"name": "quoteAssetAmount",
|
|
1995
|
-
"type": "u128"
|
|
1996
|
-
},
|
|
1997
2053
|
{
|
|
1998
2054
|
"name": "baseAssetAmount",
|
|
1999
2055
|
"type": "u128"
|
|
@@ -2042,6 +2098,10 @@
|
|
|
2042
2098
|
"name": "oraclePriceOffset",
|
|
2043
2099
|
"type": "i128"
|
|
2044
2100
|
},
|
|
2101
|
+
{
|
|
2102
|
+
"name": "auctionDuration",
|
|
2103
|
+
"type": "u8"
|
|
2104
|
+
},
|
|
2045
2105
|
{
|
|
2046
2106
|
"name": "padding0",
|
|
2047
2107
|
"type": "bool"
|
|
@@ -2204,6 +2264,14 @@
|
|
|
2204
2264
|
"name": "minimumQuoteAssetTradeSize",
|
|
2205
2265
|
"type": "u128"
|
|
2206
2266
|
},
|
|
2267
|
+
{
|
|
2268
|
+
"name": "maxBaseAssetAmountRatio",
|
|
2269
|
+
"type": "u16"
|
|
2270
|
+
},
|
|
2271
|
+
{
|
|
2272
|
+
"name": "maxSlippageRatio",
|
|
2273
|
+
"type": "u16"
|
|
2274
|
+
},
|
|
2207
2275
|
{
|
|
2208
2276
|
"name": "baseAssetAmountStepSize",
|
|
2209
2277
|
"type": "u128"
|
|
@@ -2688,10 +2756,6 @@
|
|
|
2688
2756
|
"defined": "PositionDirection"
|
|
2689
2757
|
}
|
|
2690
2758
|
},
|
|
2691
|
-
{
|
|
2692
|
-
"name": "quoteAssetAmount",
|
|
2693
|
-
"type": "u128"
|
|
2694
|
-
},
|
|
2695
2759
|
{
|
|
2696
2760
|
"name": "baseAssetAmount",
|
|
2697
2761
|
"type": "u128"
|
|
@@ -2904,6 +2968,12 @@
|
|
|
2904
2968
|
},
|
|
2905
2969
|
{
|
|
2906
2970
|
"name": "OraclePriceBreachedLimitPrice"
|
|
2971
|
+
},
|
|
2972
|
+
{
|
|
2973
|
+
"name": "MarketOrderFilledToLimitPrice"
|
|
2974
|
+
},
|
|
2975
|
+
{
|
|
2976
|
+
"name": "MarketOrderAuctionExpired"
|
|
2907
2977
|
}
|
|
2908
2978
|
]
|
|
2909
2979
|
}
|
|
@@ -3691,8 +3761,8 @@
|
|
|
3691
3761
|
},
|
|
3692
3762
|
{
|
|
3693
3763
|
"code": 6044,
|
|
3694
|
-
"name": "
|
|
3695
|
-
"msg": "
|
|
3764
|
+
"name": "FillOrderDidNotUpdateState",
|
|
3765
|
+
"msg": "FillOrderDidNotUpdateState"
|
|
3696
3766
|
},
|
|
3697
3767
|
{
|
|
3698
3768
|
"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/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;
|