@drift-labs/sdk 0.2.0-master.1 → 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/accounts/types.d.ts +1 -0
- package/lib/admin.d.ts +4 -1
- package/lib/admin.js +29 -2
- package/lib/clearingHouse.d.ts +11 -14
- package/lib/clearingHouse.js +73 -73
- package/lib/config.js +1 -1
- package/lib/constants/numericConstants.d.ts +1 -0
- package/lib/constants/numericConstants.js +2 -1
- package/lib/factory/bigNum.d.ts +8 -2
- package/lib/factory/bigNum.js +14 -6
- package/lib/idl/clearing_house.json +189 -52
- package/lib/index.d.ts +2 -1
- package/lib/index.js +6 -1
- package/lib/math/amm.d.ts +6 -1
- package/lib/math/amm.js +124 -41
- 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/repeg.js +1 -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.d.ts +1 -2
- package/lib/orders.js +6 -85
- package/lib/types.d.ts +62 -1
- package/lib/types.js +37 -1
- package/package.json +3 -3
- package/src/admin.ts +48 -4
- package/src/clearingHouse.ts +102 -151
- package/src/config.ts +1 -1
- package/src/constants/numericConstants.ts +1 -0
- package/src/factory/bigNum.ts +26 -9
- package/src/idl/clearing_house.json +189 -52
- package/src/index.ts +2 -1
- package/src/math/amm.ts +207 -52
- package/src/math/auction.ts +5 -1
- package/src/math/orders.ts +17 -13
- package/src/math/repeg.ts +2 -1
- package/src/math/trade.ts +23 -25
- package/src/orderParams.ts +20 -141
- package/src/orders.ts +7 -131
- package/src/types.ts +58 -3
package/lib/types.d.ts
CHANGED
|
@@ -88,6 +88,23 @@ export declare class OrderAction {
|
|
|
88
88
|
static readonly FILL: {
|
|
89
89
|
fill: {};
|
|
90
90
|
};
|
|
91
|
+
static readonly TRIGGER: {
|
|
92
|
+
trigger: {};
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
export declare class OrderActionExplanation {
|
|
96
|
+
static readonly NONE: {
|
|
97
|
+
none: {};
|
|
98
|
+
};
|
|
99
|
+
static readonly BREACHED_MARGIN_REQUIREMENT: {
|
|
100
|
+
breachedMarginRequirement: {};
|
|
101
|
+
};
|
|
102
|
+
static readonly ORACLE_PRICE_BREACHED_LIMIT_PRICE: {
|
|
103
|
+
oraclePriceBreachedLimitPrice: {};
|
|
104
|
+
};
|
|
105
|
+
static readonly MARKET_ORDER_FILLED_TO_LIMIT_PRICE: {
|
|
106
|
+
marketOrderFilledToLimitPrice: {};
|
|
107
|
+
};
|
|
91
108
|
}
|
|
92
109
|
export declare class OrderTriggerCondition {
|
|
93
110
|
static readonly ABOVE: {
|
|
@@ -182,7 +199,10 @@ export declare type OrderRecord = {
|
|
|
182
199
|
maker: PublicKey;
|
|
183
200
|
takerOrder: Order;
|
|
184
201
|
makerOrder: Order;
|
|
202
|
+
takerUnsettledPnl: BN;
|
|
203
|
+
makerUnsettledPnl: BN;
|
|
185
204
|
action: OrderAction;
|
|
205
|
+
actionExplanation: OrderActionExplanation;
|
|
186
206
|
filler: PublicKey;
|
|
187
207
|
fillRecordId: BN;
|
|
188
208
|
marketIndex: BN;
|
|
@@ -274,6 +294,8 @@ export declare type AMM = {
|
|
|
274
294
|
lastMarkPriceTwapTs: BN;
|
|
275
295
|
lastOraclePriceTwap: BN;
|
|
276
296
|
lastOraclePriceTwapTs: BN;
|
|
297
|
+
lastOracleMarkSpreadPct: BN;
|
|
298
|
+
lastOracleConfPct: BN;
|
|
277
299
|
oracle: PublicKey;
|
|
278
300
|
oracleSource: OracleSource;
|
|
279
301
|
fundingPeriod: BN;
|
|
@@ -288,6 +310,8 @@ export declare type AMM = {
|
|
|
288
310
|
totalFee: BN;
|
|
289
311
|
minimumQuoteAssetTradeSize: BN;
|
|
290
312
|
baseAssetAmountStepSize: BN;
|
|
313
|
+
maxBaseAssetAmountRatio: number;
|
|
314
|
+
maxSlippageRatio: number;
|
|
291
315
|
lastOraclePrice: BN;
|
|
292
316
|
baseSpread: number;
|
|
293
317
|
curveUpdateIntensity: number;
|
|
@@ -304,6 +328,7 @@ export declare type AMM = {
|
|
|
304
328
|
lastAskPriceTwap: BN;
|
|
305
329
|
longSpread: BN;
|
|
306
330
|
shortSpread: BN;
|
|
331
|
+
maxSpread: number;
|
|
307
332
|
};
|
|
308
333
|
export declare type UserPosition = {
|
|
309
334
|
baseAssetAmount: BN;
|
|
@@ -356,6 +381,7 @@ export declare type Order = {
|
|
|
356
381
|
reduceOnly: boolean;
|
|
357
382
|
triggerPrice: BN;
|
|
358
383
|
triggerCondition: OrderTriggerCondition;
|
|
384
|
+
triggered: boolean;
|
|
359
385
|
discountTier: OrderDiscountTier;
|
|
360
386
|
existingPositionDirection: PositionDirection;
|
|
361
387
|
referrer: PublicKey;
|
|
@@ -370,7 +396,6 @@ export declare type OrderParams = {
|
|
|
370
396
|
orderType: OrderType;
|
|
371
397
|
userOrderId: number;
|
|
372
398
|
direction: PositionDirection;
|
|
373
|
-
quoteAssetAmount: BN;
|
|
374
399
|
baseAssetAmount: BN;
|
|
375
400
|
price: BN;
|
|
376
401
|
marketIndex: BN;
|
|
@@ -388,6 +413,42 @@ export declare type OrderParams = {
|
|
|
388
413
|
referrer: boolean;
|
|
389
414
|
};
|
|
390
415
|
};
|
|
416
|
+
export declare type NecessaryOrderParams = {
|
|
417
|
+
orderType: OrderType;
|
|
418
|
+
marketIndex: BN;
|
|
419
|
+
baseAssetAmount: BN;
|
|
420
|
+
direction: PositionDirection;
|
|
421
|
+
};
|
|
422
|
+
export declare type OptionalOrderParams = {
|
|
423
|
+
[Property in keyof OrderParams]?: OrderParams[Property];
|
|
424
|
+
} & NecessaryOrderParams;
|
|
425
|
+
export declare const DefaultOrderParams: {
|
|
426
|
+
orderType: {
|
|
427
|
+
market: {};
|
|
428
|
+
};
|
|
429
|
+
userOrderId: number;
|
|
430
|
+
direction: {
|
|
431
|
+
long: {};
|
|
432
|
+
};
|
|
433
|
+
baseAssetAmount: BN;
|
|
434
|
+
price: BN;
|
|
435
|
+
marketIndex: BN;
|
|
436
|
+
reduceOnly: boolean;
|
|
437
|
+
postOnly: boolean;
|
|
438
|
+
immediateOrCancel: boolean;
|
|
439
|
+
triggerPrice: BN;
|
|
440
|
+
triggerCondition: {
|
|
441
|
+
above: {};
|
|
442
|
+
};
|
|
443
|
+
positionLimit: BN;
|
|
444
|
+
oraclePriceOffset: BN;
|
|
445
|
+
padding0: BN;
|
|
446
|
+
padding1: BN;
|
|
447
|
+
optionalAccounts: {
|
|
448
|
+
discountToken: boolean;
|
|
449
|
+
referrer: boolean;
|
|
450
|
+
};
|
|
451
|
+
};
|
|
391
452
|
export declare type MakerInfo = {
|
|
392
453
|
maker: PublicKey;
|
|
393
454
|
order: Order;
|
package/lib/types.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TradeSide = exports.isOneOfVariant = exports.isVariant = exports.OrderTriggerCondition = exports.OrderAction = exports.OrderDiscountTier = exports.OrderStatus = exports.OrderType = exports.OracleSource = exports.PositionDirection = exports.BankBalanceType = exports.SwapDirection = void 0;
|
|
3
|
+
exports.DefaultOrderParams = exports.TradeSide = exports.isOneOfVariant = exports.isVariant = exports.OrderTriggerCondition = exports.OrderActionExplanation = exports.OrderAction = exports.OrderDiscountTier = exports.OrderStatus = exports.OrderType = exports.OracleSource = exports.PositionDirection = exports.BankBalanceType = exports.SwapDirection = void 0;
|
|
4
|
+
const _1 = require(".");
|
|
4
5
|
// # Utility Types / Enums / Constants
|
|
5
6
|
class SwapDirection {
|
|
6
7
|
}
|
|
@@ -50,6 +51,20 @@ OrderAction.PLACE = { place: {} };
|
|
|
50
51
|
OrderAction.CANCEL = { cancel: {} };
|
|
51
52
|
OrderAction.EXPIRE = { expire: {} };
|
|
52
53
|
OrderAction.FILL = { fill: {} };
|
|
54
|
+
OrderAction.TRIGGER = { trigger: {} };
|
|
55
|
+
class OrderActionExplanation {
|
|
56
|
+
}
|
|
57
|
+
exports.OrderActionExplanation = OrderActionExplanation;
|
|
58
|
+
OrderActionExplanation.NONE = { none: {} };
|
|
59
|
+
OrderActionExplanation.BREACHED_MARGIN_REQUIREMENT = {
|
|
60
|
+
breachedMarginRequirement: {},
|
|
61
|
+
};
|
|
62
|
+
OrderActionExplanation.ORACLE_PRICE_BREACHED_LIMIT_PRICE = {
|
|
63
|
+
oraclePriceBreachedLimitPrice: {},
|
|
64
|
+
};
|
|
65
|
+
OrderActionExplanation.MARKET_ORDER_FILLED_TO_LIMIT_PRICE = {
|
|
66
|
+
marketOrderFilledToLimitPrice: {},
|
|
67
|
+
};
|
|
53
68
|
class OrderTriggerCondition {
|
|
54
69
|
}
|
|
55
70
|
exports.OrderTriggerCondition = OrderTriggerCondition;
|
|
@@ -71,3 +86,24 @@ var TradeSide;
|
|
|
71
86
|
TradeSide[TradeSide["Buy"] = 1] = "Buy";
|
|
72
87
|
TradeSide[TradeSide["Sell"] = 2] = "Sell";
|
|
73
88
|
})(TradeSide = exports.TradeSide || (exports.TradeSide = {}));
|
|
89
|
+
exports.DefaultOrderParams = {
|
|
90
|
+
orderType: OrderType.MARKET,
|
|
91
|
+
userOrderId: 0,
|
|
92
|
+
direction: PositionDirection.LONG,
|
|
93
|
+
baseAssetAmount: _1.ZERO,
|
|
94
|
+
price: _1.ZERO,
|
|
95
|
+
marketIndex: _1.ZERO,
|
|
96
|
+
reduceOnly: false,
|
|
97
|
+
postOnly: false,
|
|
98
|
+
immediateOrCancel: false,
|
|
99
|
+
triggerPrice: _1.ZERO,
|
|
100
|
+
triggerCondition: OrderTriggerCondition.ABOVE,
|
|
101
|
+
positionLimit: _1.ZERO,
|
|
102
|
+
oraclePriceOffset: _1.ZERO,
|
|
103
|
+
padding0: _1.ZERO,
|
|
104
|
+
padding1: _1.ZERO,
|
|
105
|
+
optionalAccounts: {
|
|
106
|
+
discountToken: false,
|
|
107
|
+
referrer: false,
|
|
108
|
+
},
|
|
109
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@drift-labs/sdk",
|
|
3
|
-
"version": "0.2.0-master.
|
|
3
|
+
"version": "0.2.0-master.4",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"types": "lib/index.d.ts",
|
|
6
6
|
"author": "crispheaney",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"@project-serum/anchor": "0.24.2",
|
|
33
|
-
"@pythnetwork/client": "2.5.
|
|
33
|
+
"@pythnetwork/client": "2.5.3",
|
|
34
34
|
"@solana/spl-token": "^0.1.6",
|
|
35
35
|
"@solana/web3.js": "1.41.0",
|
|
36
36
|
"@switchboard-xyz/switchboard-v2": "^0.0.67",
|
|
@@ -39,10 +39,10 @@
|
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@types/chai": "^4.3.1",
|
|
42
|
+
"@types/jest": "^28.1.3",
|
|
42
43
|
"@types/mocha": "^9.1.1",
|
|
43
44
|
"@typescript-eslint/eslint-plugin": "^4.28.0",
|
|
44
45
|
"@typescript-eslint/parser": "^4.28.0",
|
|
45
|
-
"@types/jest": "^28.1.3",
|
|
46
46
|
"chai": "^4.3.6",
|
|
47
47
|
"eslint": "^7.29.0",
|
|
48
48
|
"eslint-config-prettier": "^8.3.0",
|
package/src/admin.ts
CHANGED
|
@@ -444,6 +444,19 @@ export class Admin extends ClearingHouse {
|
|
|
444
444
|
});
|
|
445
445
|
}
|
|
446
446
|
|
|
447
|
+
public async updateMarketMaxSpread(
|
|
448
|
+
marketIndex: BN,
|
|
449
|
+
maxSpread: number
|
|
450
|
+
): Promise<TransactionSignature> {
|
|
451
|
+
return await this.program.rpc.updateMarketMaxSpread(maxSpread, {
|
|
452
|
+
accounts: {
|
|
453
|
+
admin: this.wallet.publicKey,
|
|
454
|
+
state: await this.getStatePublicKey(),
|
|
455
|
+
market: await getMarketPublicKey(this.program.programId, marketIndex),
|
|
456
|
+
},
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
|
|
447
460
|
public async updatePartialLiquidationClosePercentage(
|
|
448
461
|
numerator: BN,
|
|
449
462
|
denominator: BN
|
|
@@ -662,17 +675,48 @@ export class Admin extends ClearingHouse {
|
|
|
662
675
|
});
|
|
663
676
|
}
|
|
664
677
|
|
|
665
|
-
public async
|
|
666
|
-
|
|
678
|
+
public async updateAuctionDuration(
|
|
679
|
+
minDuration: BN | number,
|
|
680
|
+
maxDuration: BN | number
|
|
681
|
+
): Promise<TransactionSignature> {
|
|
682
|
+
return await this.program.rpc.updateAuctionDuration(
|
|
683
|
+
typeof minDuration === 'number' ? minDuration : minDuration.toNumber(),
|
|
684
|
+
typeof maxDuration === 'number' ? maxDuration : maxDuration.toNumber(),
|
|
685
|
+
{
|
|
686
|
+
accounts: {
|
|
687
|
+
admin: this.wallet.publicKey,
|
|
688
|
+
state: await this.getStatePublicKey(),
|
|
689
|
+
},
|
|
690
|
+
}
|
|
691
|
+
);
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
public async updateMaxBaseAssetAmountRatio(
|
|
695
|
+
marketIndex: BN,
|
|
696
|
+
maxBaseAssetAmountRatio: number
|
|
667
697
|
): Promise<TransactionSignature> {
|
|
668
|
-
return await this.program.rpc.
|
|
669
|
-
|
|
698
|
+
return await this.program.rpc.updateMaxBaseAssetAmountRatio(
|
|
699
|
+
maxBaseAssetAmountRatio,
|
|
670
700
|
{
|
|
671
701
|
accounts: {
|
|
672
702
|
admin: this.wallet.publicKey,
|
|
673
703
|
state: await this.getStatePublicKey(),
|
|
704
|
+
market: this.getMarketAccount(marketIndex).pubkey,
|
|
674
705
|
},
|
|
675
706
|
}
|
|
676
707
|
);
|
|
677
708
|
}
|
|
709
|
+
|
|
710
|
+
public async updateMaxSlippageRatio(
|
|
711
|
+
marketIndex: BN,
|
|
712
|
+
maxSlippageRatio: number
|
|
713
|
+
): Promise<TransactionSignature> {
|
|
714
|
+
return await this.program.rpc.updateMaxSlippageRatio(maxSlippageRatio, {
|
|
715
|
+
accounts: {
|
|
716
|
+
admin: this.wallet.publicKey,
|
|
717
|
+
state: await this.getStatePublicKey(),
|
|
718
|
+
market: this.getMarketAccount(marketIndex).pubkey,
|
|
719
|
+
},
|
|
720
|
+
});
|
|
721
|
+
}
|
|
678
722
|
}
|
package/src/clearingHouse.ts
CHANGED
|
@@ -11,6 +11,9 @@ import {
|
|
|
11
11
|
BankAccount,
|
|
12
12
|
UserBankBalance,
|
|
13
13
|
MakerInfo,
|
|
14
|
+
OptionalOrderParams,
|
|
15
|
+
DefaultOrderParams,
|
|
16
|
+
OrderType,
|
|
14
17
|
} from './types';
|
|
15
18
|
import * as anchor from '@project-serum/anchor';
|
|
16
19
|
import clearingHouseIDL from './idl/clearing_house.json';
|
|
@@ -52,7 +55,6 @@ import { WebSocketClearingHouseAccountSubscriber } from './accounts/webSocketCle
|
|
|
52
55
|
import { RetryTxSender } from './tx/retryTxSender';
|
|
53
56
|
import { ClearingHouseUser } from './clearingHouseUser';
|
|
54
57
|
import { ClearingHouseUserAccountSubscriptionConfig } from './clearingHouseUserConfig';
|
|
55
|
-
import { getMarketOrderParams } from './orderParams';
|
|
56
58
|
import { getMarketsBanksAndOraclesForSubscription } from './config';
|
|
57
59
|
|
|
58
60
|
/**
|
|
@@ -798,20 +800,17 @@ export class ClearingHouse {
|
|
|
798
800
|
marketIndex: BN,
|
|
799
801
|
limitPrice?: BN
|
|
800
802
|
): Promise<TransactionSignature> {
|
|
801
|
-
return await this.placeAndTake(
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
limitPrice
|
|
809
|
-
)
|
|
810
|
-
);
|
|
803
|
+
return await this.placeAndTake({
|
|
804
|
+
orderType: OrderType.MARKET,
|
|
805
|
+
marketIndex,
|
|
806
|
+
direction,
|
|
807
|
+
baseAssetAmount: amount,
|
|
808
|
+
price: limitPrice,
|
|
809
|
+
});
|
|
811
810
|
}
|
|
812
811
|
|
|
813
812
|
public async placeOrder(
|
|
814
|
-
orderParams:
|
|
813
|
+
orderParams: OptionalOrderParams
|
|
815
814
|
): Promise<TransactionSignature> {
|
|
816
815
|
const { txSig, slot } = await this.txSender.send(
|
|
817
816
|
wrapInTx(await this.getPlaceOrderIx(orderParams)),
|
|
@@ -822,14 +821,16 @@ export class ClearingHouse {
|
|
|
822
821
|
return txSig;
|
|
823
822
|
}
|
|
824
823
|
|
|
824
|
+
getOrderParams(optionalOrderParams: OptionalOrderParams): OrderParams {
|
|
825
|
+
return Object.assign({}, DefaultOrderParams, optionalOrderParams);
|
|
826
|
+
}
|
|
827
|
+
|
|
825
828
|
public async getPlaceOrderIx(
|
|
826
|
-
orderParams:
|
|
829
|
+
orderParams: OptionalOrderParams
|
|
827
830
|
): Promise<TransactionInstruction> {
|
|
831
|
+
orderParams = this.getOrderParams(orderParams);
|
|
828
832
|
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
829
833
|
|
|
830
|
-
const priceOracle = this.getMarketAccount(orderParams.marketIndex).amm
|
|
831
|
-
.oracle;
|
|
832
|
-
|
|
833
834
|
const remainingAccounts = this.getRemainingAccounts({
|
|
834
835
|
writableMarketIndex: orderParams.marketIndex,
|
|
835
836
|
});
|
|
@@ -839,38 +840,11 @@ export class ClearingHouse {
|
|
|
839
840
|
state: await this.getStatePublicKey(),
|
|
840
841
|
user: userAccountPublicKey,
|
|
841
842
|
authority: this.wallet.publicKey,
|
|
842
|
-
oracle: priceOracle,
|
|
843
843
|
},
|
|
844
844
|
remainingAccounts,
|
|
845
845
|
});
|
|
846
846
|
}
|
|
847
847
|
|
|
848
|
-
public async expireOrders(
|
|
849
|
-
userAccountPublicKey: PublicKey
|
|
850
|
-
): Promise<TransactionSignature> {
|
|
851
|
-
const { txSig } = await this.txSender.send(
|
|
852
|
-
wrapInTx(await this.getExpireOrdersIx(userAccountPublicKey)),
|
|
853
|
-
[],
|
|
854
|
-
this.opts
|
|
855
|
-
);
|
|
856
|
-
return txSig;
|
|
857
|
-
}
|
|
858
|
-
|
|
859
|
-
public async getExpireOrdersIx(
|
|
860
|
-
userAccountPublicKey: PublicKey
|
|
861
|
-
): Promise<TransactionInstruction> {
|
|
862
|
-
const fillerPublicKey = await this.getUserAccountPublicKey();
|
|
863
|
-
|
|
864
|
-
return await this.program.instruction.expireOrders({
|
|
865
|
-
accounts: {
|
|
866
|
-
state: await this.getStatePublicKey(),
|
|
867
|
-
filler: fillerPublicKey,
|
|
868
|
-
user: userAccountPublicKey,
|
|
869
|
-
authority: this.wallet.publicKey,
|
|
870
|
-
},
|
|
871
|
-
});
|
|
872
|
-
}
|
|
873
|
-
|
|
874
848
|
public async updateAMMs(marketIndexes: BN[]): Promise<TransactionSignature> {
|
|
875
849
|
const { txSig } = await this.txSender.send(
|
|
876
850
|
wrapInTx(await this.getUpdateAMMsIx(marketIndexes)),
|
|
@@ -914,7 +888,7 @@ export class ClearingHouse {
|
|
|
914
888
|
});
|
|
915
889
|
}
|
|
916
890
|
|
|
917
|
-
public async cancelOrder(orderId
|
|
891
|
+
public async cancelOrder(orderId?: BN): Promise<TransactionSignature> {
|
|
918
892
|
const { txSig } = await this.txSender.send(
|
|
919
893
|
wrapInTx(await this.getCancelOrderIx(orderId)),
|
|
920
894
|
[],
|
|
@@ -923,20 +897,16 @@ export class ClearingHouse {
|
|
|
923
897
|
return txSig;
|
|
924
898
|
}
|
|
925
899
|
|
|
926
|
-
public async getCancelOrderIx(orderId
|
|
900
|
+
public async getCancelOrderIx(orderId?: BN): Promise<TransactionInstruction> {
|
|
927
901
|
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
928
902
|
|
|
929
|
-
const order = this.getOrder(orderId);
|
|
930
|
-
const oracle = this.getMarketAccount(order.marketIndex).amm.oracle;
|
|
931
|
-
|
|
932
903
|
const remainingAccounts = this.getRemainingAccounts({});
|
|
933
904
|
|
|
934
|
-
return await this.program.instruction.cancelOrder(orderId, {
|
|
905
|
+
return await this.program.instruction.cancelOrder(orderId ?? null, {
|
|
935
906
|
accounts: {
|
|
936
907
|
state: await this.getStatePublicKey(),
|
|
937
908
|
user: userAccountPublicKey,
|
|
938
909
|
authority: this.wallet.publicKey,
|
|
939
|
-
oracle,
|
|
940
910
|
},
|
|
941
911
|
remainingAccounts,
|
|
942
912
|
});
|
|
@@ -974,36 +944,90 @@ export class ClearingHouse {
|
|
|
974
944
|
});
|
|
975
945
|
}
|
|
976
946
|
|
|
977
|
-
public async
|
|
978
|
-
|
|
947
|
+
public async fillOrder(
|
|
948
|
+
userAccountPublicKey: PublicKey,
|
|
949
|
+
user: UserAccount,
|
|
950
|
+
order?: Order,
|
|
951
|
+
makerInfo?: MakerInfo
|
|
979
952
|
): Promise<TransactionSignature> {
|
|
980
953
|
const { txSig } = await this.txSender.send(
|
|
981
|
-
wrapInTx(
|
|
954
|
+
wrapInTx(
|
|
955
|
+
await this.getFillOrderIx(userAccountPublicKey, user, order, makerInfo)
|
|
956
|
+
),
|
|
982
957
|
[],
|
|
983
958
|
this.opts
|
|
984
959
|
);
|
|
985
960
|
return txSig;
|
|
986
961
|
}
|
|
987
962
|
|
|
988
|
-
public async
|
|
989
|
-
|
|
963
|
+
public async getFillOrderIx(
|
|
964
|
+
userAccountPublicKey: PublicKey,
|
|
965
|
+
userAccount: UserAccount,
|
|
966
|
+
order: Order,
|
|
967
|
+
makerInfo?: MakerInfo
|
|
990
968
|
): Promise<TransactionInstruction> {
|
|
991
|
-
const
|
|
969
|
+
const fillerPublicKey = await this.getUserAccountPublicKey();
|
|
992
970
|
|
|
993
|
-
const
|
|
971
|
+
const marketIndex = order.marketIndex;
|
|
972
|
+
const marketAccount = this.getMarketAccount(marketIndex);
|
|
994
973
|
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
974
|
+
const bankAccountInfos = [
|
|
975
|
+
{
|
|
976
|
+
pubkey: this.getQuoteAssetBankAccount().pubkey,
|
|
977
|
+
isSigner: false,
|
|
978
|
+
isWritable: true,
|
|
979
|
+
},
|
|
980
|
+
];
|
|
981
|
+
const marketAccountInfos = [
|
|
982
|
+
{
|
|
983
|
+
pubkey: marketAccount.pubkey,
|
|
984
|
+
isWritable: true,
|
|
985
|
+
isSigner: false,
|
|
986
|
+
},
|
|
987
|
+
];
|
|
988
|
+
const oracleAccountInfos = [
|
|
989
|
+
{
|
|
990
|
+
pubkey: marketAccount.amm.oracle,
|
|
999
991
|
isWritable: false,
|
|
1000
992
|
isSigner: false,
|
|
993
|
+
},
|
|
994
|
+
];
|
|
995
|
+
for (const position of userAccount.positions) {
|
|
996
|
+
if (
|
|
997
|
+
!positionIsAvailable(position) &&
|
|
998
|
+
!position.marketIndex.eq(order.marketIndex)
|
|
999
|
+
) {
|
|
1000
|
+
const market = this.getMarketAccount(position.marketIndex);
|
|
1001
|
+
marketAccountInfos.push({
|
|
1002
|
+
pubkey: market.pubkey,
|
|
1003
|
+
isWritable: true,
|
|
1004
|
+
isSigner: false,
|
|
1005
|
+
});
|
|
1006
|
+
oracleAccountInfos.push({
|
|
1007
|
+
pubkey: market.amm.oracle,
|
|
1008
|
+
isWritable: false,
|
|
1009
|
+
isSigner: false,
|
|
1010
|
+
});
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
const remainingAccounts = oracleAccountInfos.concat(
|
|
1014
|
+
bankAccountInfos.concat(marketAccountInfos)
|
|
1015
|
+
);
|
|
1016
|
+
|
|
1017
|
+
if (makerInfo) {
|
|
1018
|
+
remainingAccounts.push({
|
|
1019
|
+
pubkey: makerInfo.maker,
|
|
1020
|
+
isWritable: true,
|
|
1021
|
+
isSigner: false,
|
|
1001
1022
|
});
|
|
1002
1023
|
}
|
|
1003
1024
|
|
|
1004
|
-
|
|
1025
|
+
const orderId = order.orderId;
|
|
1026
|
+
const makerOrderId = makerInfo ? makerInfo.order.orderId : null;
|
|
1027
|
+
return await this.program.instruction.fillOrder(orderId, makerOrderId, {
|
|
1005
1028
|
accounts: {
|
|
1006
1029
|
state: await this.getStatePublicKey(),
|
|
1030
|
+
filler: fillerPublicKey,
|
|
1007
1031
|
user: userAccountPublicKey,
|
|
1008
1032
|
authority: this.wallet.publicKey,
|
|
1009
1033
|
},
|
|
@@ -1011,85 +1035,28 @@ export class ClearingHouse {
|
|
|
1011
1035
|
});
|
|
1012
1036
|
}
|
|
1013
1037
|
|
|
1014
|
-
public async
|
|
1015
|
-
bestEffort?: boolean,
|
|
1016
|
-
marketIndexOnly?: BN,
|
|
1017
|
-
directionOnly?: PositionDirection
|
|
1018
|
-
): Promise<TransactionSignature> {
|
|
1019
|
-
const { txSig } = await this.txSender.send(
|
|
1020
|
-
wrapInTx(
|
|
1021
|
-
await this.getCancelOrdersByMarketAndSideIx(
|
|
1022
|
-
bestEffort,
|
|
1023
|
-
marketIndexOnly,
|
|
1024
|
-
directionOnly
|
|
1025
|
-
)
|
|
1026
|
-
),
|
|
1027
|
-
[],
|
|
1028
|
-
this.opts
|
|
1029
|
-
);
|
|
1030
|
-
return txSig;
|
|
1031
|
-
}
|
|
1032
|
-
|
|
1033
|
-
public async getCancelOrdersByMarketAndSideIx(
|
|
1034
|
-
bestEffort?: boolean,
|
|
1035
|
-
marketIndexOnly?: BN,
|
|
1036
|
-
directionOnly?: PositionDirection
|
|
1037
|
-
): Promise<TransactionInstruction> {
|
|
1038
|
-
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
1039
|
-
|
|
1040
|
-
const remainingAccounts = this.getRemainingAccounts({});
|
|
1041
|
-
|
|
1042
|
-
for (const order of this.getUserAccount().orders) {
|
|
1043
|
-
const oracle = this.getMarketAccount(order.marketIndex).amm.oracle;
|
|
1044
|
-
remainingAccounts.push({
|
|
1045
|
-
pubkey: oracle,
|
|
1046
|
-
isWritable: false,
|
|
1047
|
-
isSigner: false,
|
|
1048
|
-
});
|
|
1049
|
-
}
|
|
1050
|
-
|
|
1051
|
-
return await this.program.instruction.cancelOrdersByMarketAndSide(
|
|
1052
|
-
bestEffort,
|
|
1053
|
-
marketIndexOnly,
|
|
1054
|
-
directionOnly,
|
|
1055
|
-
{
|
|
1056
|
-
accounts: {
|
|
1057
|
-
state: await this.getStatePublicKey(),
|
|
1058
|
-
user: userAccountPublicKey,
|
|
1059
|
-
authority: this.wallet.publicKey,
|
|
1060
|
-
},
|
|
1061
|
-
remainingAccounts,
|
|
1062
|
-
}
|
|
1063
|
-
);
|
|
1064
|
-
}
|
|
1065
|
-
|
|
1066
|
-
public async fillOrder(
|
|
1038
|
+
public async triggerOrder(
|
|
1067
1039
|
userAccountPublicKey: PublicKey,
|
|
1068
1040
|
user: UserAccount,
|
|
1069
|
-
order: Order
|
|
1070
|
-
makerInfo?: MakerInfo
|
|
1041
|
+
order: Order
|
|
1071
1042
|
): Promise<TransactionSignature> {
|
|
1072
1043
|
const { txSig } = await this.txSender.send(
|
|
1073
|
-
wrapInTx(
|
|
1074
|
-
await this.getFillOrderIx(userAccountPublicKey, user, order, makerInfo)
|
|
1075
|
-
),
|
|
1044
|
+
wrapInTx(await this.getTriggerOrderIx(userAccountPublicKey, user, order)),
|
|
1076
1045
|
[],
|
|
1077
1046
|
this.opts
|
|
1078
1047
|
);
|
|
1079
1048
|
return txSig;
|
|
1080
1049
|
}
|
|
1081
1050
|
|
|
1082
|
-
public async
|
|
1051
|
+
public async getTriggerOrderIx(
|
|
1083
1052
|
userAccountPublicKey: PublicKey,
|
|
1084
1053
|
userAccount: UserAccount,
|
|
1085
|
-
order: Order
|
|
1086
|
-
makerInfo?: MakerInfo
|
|
1054
|
+
order: Order
|
|
1087
1055
|
): Promise<TransactionInstruction> {
|
|
1088
1056
|
const fillerPublicKey = await this.getUserAccountPublicKey();
|
|
1089
1057
|
|
|
1090
1058
|
const marketIndex = order.marketIndex;
|
|
1091
1059
|
const marketAccount = this.getMarketAccount(marketIndex);
|
|
1092
|
-
const oracle = marketAccount.amm.oracle;
|
|
1093
1060
|
|
|
1094
1061
|
const bankAccountInfos = [
|
|
1095
1062
|
{
|
|
@@ -1134,30 +1101,20 @@ export class ClearingHouse {
|
|
|
1134
1101
|
bankAccountInfos.concat(marketAccountInfos)
|
|
1135
1102
|
);
|
|
1136
1103
|
|
|
1137
|
-
if (makerInfo) {
|
|
1138
|
-
remainingAccounts.push({
|
|
1139
|
-
pubkey: makerInfo.maker,
|
|
1140
|
-
isWritable: true,
|
|
1141
|
-
isSigner: false,
|
|
1142
|
-
});
|
|
1143
|
-
}
|
|
1144
|
-
|
|
1145
1104
|
const orderId = order.orderId;
|
|
1146
|
-
|
|
1147
|
-
return await this.program.instruction.fillOrder(orderId, makerOrderId, {
|
|
1105
|
+
return await this.program.instruction.triggerOrder(orderId, {
|
|
1148
1106
|
accounts: {
|
|
1149
1107
|
state: await this.getStatePublicKey(),
|
|
1150
1108
|
filler: fillerPublicKey,
|
|
1151
1109
|
user: userAccountPublicKey,
|
|
1152
1110
|
authority: this.wallet.publicKey,
|
|
1153
|
-
oracle: oracle,
|
|
1154
1111
|
},
|
|
1155
1112
|
remainingAccounts,
|
|
1156
1113
|
});
|
|
1157
1114
|
}
|
|
1158
1115
|
|
|
1159
1116
|
public async placeAndTake(
|
|
1160
|
-
orderParams:
|
|
1117
|
+
orderParams: OptionalOrderParams,
|
|
1161
1118
|
makerInfo?: MakerInfo
|
|
1162
1119
|
): Promise<TransactionSignature> {
|
|
1163
1120
|
const { txSig, slot } = await this.txSender.send(
|
|
@@ -1170,14 +1127,12 @@ export class ClearingHouse {
|
|
|
1170
1127
|
}
|
|
1171
1128
|
|
|
1172
1129
|
public async getPlaceAndTakeIx(
|
|
1173
|
-
orderParams:
|
|
1130
|
+
orderParams: OptionalOrderParams,
|
|
1174
1131
|
makerInfo?: MakerInfo
|
|
1175
1132
|
): Promise<TransactionInstruction> {
|
|
1133
|
+
orderParams = this.getOrderParams(orderParams);
|
|
1176
1134
|
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
1177
1135
|
|
|
1178
|
-
const priceOracle = this.getMarketAccount(orderParams.marketIndex).amm
|
|
1179
|
-
.oracle;
|
|
1180
|
-
|
|
1181
1136
|
const remainingAccounts = this.getRemainingAccounts({
|
|
1182
1137
|
writableMarketIndex: orderParams.marketIndex,
|
|
1183
1138
|
writableBankIndex: QUOTE_ASSET_BANK_INDEX,
|
|
@@ -1201,7 +1156,6 @@ export class ClearingHouse {
|
|
|
1201
1156
|
state: await this.getStatePublicKey(),
|
|
1202
1157
|
user: userAccountPublicKey,
|
|
1203
1158
|
authority: this.wallet.publicKey,
|
|
1204
|
-
oracle: priceOracle,
|
|
1205
1159
|
},
|
|
1206
1160
|
remainingAccounts,
|
|
1207
1161
|
}
|
|
@@ -1219,16 +1173,13 @@ export class ClearingHouse {
|
|
|
1219
1173
|
throw Error(`No position in market ${marketIndex.toString()}`);
|
|
1220
1174
|
}
|
|
1221
1175
|
|
|
1222
|
-
return await this.placeAndTake(
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
undefined
|
|
1230
|
-
)
|
|
1231
|
-
);
|
|
1176
|
+
return await this.placeAndTake({
|
|
1177
|
+
orderType: OrderType.MARKET,
|
|
1178
|
+
marketIndex,
|
|
1179
|
+
direction: findDirectionToClose(userPosition),
|
|
1180
|
+
baseAssetAmount: userPosition.baseAssetAmount,
|
|
1181
|
+
reduceOnly: true,
|
|
1182
|
+
});
|
|
1232
1183
|
}
|
|
1233
1184
|
|
|
1234
1185
|
public async settlePNLs(
|
package/src/config.ts
CHANGED
|
@@ -28,7 +28,7 @@ export const configs: { [key in DriftEnv]: DriftConfig } = {
|
|
|
28
28
|
devnet: {
|
|
29
29
|
ENV: 'devnet',
|
|
30
30
|
PYTH_ORACLE_MAPPING_ADDRESS: 'BmA9Z6FjioHJPpjT39QazZyhDRUdZy2ezwx4GiDdE2u2',
|
|
31
|
-
CLEARING_HOUSE_PROGRAM_ID: '
|
|
31
|
+
CLEARING_HOUSE_PROGRAM_ID: '9jwr5nC2f9yAraXrg4UzHXmCX3vi9FQkjD6p9e8bRqNa',
|
|
32
32
|
USDC_MINT_ADDRESS: '8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2',
|
|
33
33
|
MARKETS: DevnetMarkets,
|
|
34
34
|
BANKS: DevnetBanks,
|
|
@@ -43,6 +43,7 @@ export const BASE_PRECISION_EXP = AMM_RESERVE_PRECISION_EXP;
|
|
|
43
43
|
|
|
44
44
|
export const AMM_TO_QUOTE_PRECISION_RATIO =
|
|
45
45
|
AMM_RESERVE_PRECISION.div(QUOTE_PRECISION); // 10^7
|
|
46
|
+
export const PRICE_DIV_PEG = MARK_PRICE_PRECISION.div(PEG_PRECISION); //10^7
|
|
46
47
|
export const PRICE_TO_QUOTE_PRECISION =
|
|
47
48
|
MARK_PRICE_PRECISION.div(QUOTE_PRECISION);
|
|
48
49
|
export const AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO =
|