@drift-labs/sdk 0.2.0-master.0 → 0.2.0-master.3
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 -0
- package/lib/admin.js +9 -0
- package/lib/clearingHouse.d.ts +5 -9
- package/lib/clearingHouse.js +53 -70
- 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 +106 -39
- package/lib/math/amm.d.ts +6 -1
- package/lib/math/amm.js +127 -16
- package/lib/math/orders.js +2 -12
- package/lib/math/repeg.js +1 -1
- package/lib/orders.d.ts +1 -2
- package/lib/orders.js +6 -85
- package/lib/types.d.ts +5 -0
- package/lib/types.js +1 -0
- package/package.json +2 -2
- package/src/admin.ts +13 -0
- package/src/clearingHouse.ts +75 -126
- 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 +106 -39
- package/src/math/amm.ts +202 -17
- package/src/math/orders.ts +3 -13
- package/src/math/repeg.ts +2 -1
- package/src/orders.ts +7 -131
- package/src/types.ts +4 -1
package/lib/admin.d.ts
CHANGED
|
@@ -20,6 +20,7 @@ export declare class Admin extends ClearingHouse {
|
|
|
20
20
|
updateCurveUpdateIntensity(marketIndex: BN, curveUpdateIntensity: number): Promise<TransactionSignature>;
|
|
21
21
|
updateMarginRatio(marketIndex: BN, marginRatioInitial: number, marginRatioPartial: number, marginRatioMaintenance: number): Promise<TransactionSignature>;
|
|
22
22
|
updateMarketBaseSpread(marketIndex: BN, baseSpread: number): Promise<TransactionSignature>;
|
|
23
|
+
updateMarketMaxSpread(marketIndex: BN, maxSpread: number): Promise<TransactionSignature>;
|
|
23
24
|
updatePartialLiquidationClosePercentage(numerator: BN, denominator: BN): Promise<TransactionSignature>;
|
|
24
25
|
updatePartialLiquidationPenaltyPercentage(numerator: BN, denominator: BN): Promise<TransactionSignature>;
|
|
25
26
|
updateFullLiquidationPenaltyPercentage(numerator: BN, denominator: BN): Promise<TransactionSignature>;
|
package/lib/admin.js
CHANGED
|
@@ -255,6 +255,15 @@ class Admin extends clearingHouse_1.ClearingHouse {
|
|
|
255
255
|
},
|
|
256
256
|
});
|
|
257
257
|
}
|
|
258
|
+
async updateMarketMaxSpread(marketIndex, maxSpread) {
|
|
259
|
+
return await this.program.rpc.updateMarketMaxSpread(maxSpread, {
|
|
260
|
+
accounts: {
|
|
261
|
+
admin: this.wallet.publicKey,
|
|
262
|
+
state: await this.getStatePublicKey(),
|
|
263
|
+
market: await (0, pda_1.getMarketPublicKey)(this.program.programId, marketIndex),
|
|
264
|
+
},
|
|
265
|
+
});
|
|
266
|
+
}
|
|
258
267
|
async updatePartialLiquidationClosePercentage(numerator, denominator) {
|
|
259
268
|
return await this.program.rpc.updatePartialLiquidationClosePercentage(numerator, denominator, {
|
|
260
269
|
accounts: {
|
package/lib/clearingHouse.d.ts
CHANGED
|
@@ -97,20 +97,16 @@ export declare class ClearingHouse {
|
|
|
97
97
|
openPosition(direction: PositionDirection, amount: BN, marketIndex: BN, limitPrice?: BN): Promise<TransactionSignature>;
|
|
98
98
|
placeOrder(orderParams: OrderParams): Promise<TransactionSignature>;
|
|
99
99
|
getPlaceOrderIx(orderParams: OrderParams): Promise<TransactionInstruction>;
|
|
100
|
-
expireOrders(userAccountPublicKey: PublicKey): Promise<TransactionSignature>;
|
|
101
|
-
getExpireOrdersIx(userAccountPublicKey: PublicKey): Promise<TransactionInstruction>;
|
|
102
100
|
updateAMMs(marketIndexes: BN[]): Promise<TransactionSignature>;
|
|
103
101
|
getUpdateAMMsIx(marketIndexes: BN[]): Promise<TransactionInstruction>;
|
|
104
|
-
cancelOrder(orderId
|
|
105
|
-
getCancelOrderIx(orderId
|
|
102
|
+
cancelOrder(orderId?: BN): Promise<TransactionSignature>;
|
|
103
|
+
getCancelOrderIx(orderId?: BN): Promise<TransactionInstruction>;
|
|
106
104
|
cancelOrderByUserId(userOrderId: number): Promise<TransactionSignature>;
|
|
107
105
|
getCancelOrderByUserIdIx(userOrderId: number): Promise<TransactionInstruction>;
|
|
108
|
-
|
|
109
|
-
getCancelAllOrdersIx(bestEffort?: boolean): Promise<TransactionInstruction>;
|
|
110
|
-
cancelOrdersByMarketAndSide(bestEffort?: boolean, marketIndexOnly?: BN, directionOnly?: PositionDirection): Promise<TransactionSignature>;
|
|
111
|
-
getCancelOrdersByMarketAndSideIx(bestEffort?: boolean, marketIndexOnly?: BN, directionOnly?: PositionDirection): Promise<TransactionInstruction>;
|
|
112
|
-
fillOrder(userAccountPublicKey: PublicKey, user: UserAccount, order: Order, makerInfo?: MakerInfo): Promise<TransactionSignature>;
|
|
106
|
+
fillOrder(userAccountPublicKey: PublicKey, user: UserAccount, order?: Order, makerInfo?: MakerInfo): Promise<TransactionSignature>;
|
|
113
107
|
getFillOrderIx(userAccountPublicKey: PublicKey, userAccount: UserAccount, order: Order, makerInfo?: MakerInfo): Promise<TransactionInstruction>;
|
|
108
|
+
triggerOrder(userAccountPublicKey: PublicKey, user: UserAccount, order: Order): Promise<TransactionSignature>;
|
|
109
|
+
getTriggerOrderIx(userAccountPublicKey: PublicKey, userAccount: UserAccount, order: Order): Promise<TransactionInstruction>;
|
|
114
110
|
placeAndTake(orderParams: OrderParams, makerInfo?: MakerInfo): Promise<TransactionSignature>;
|
|
115
111
|
getPlaceAndTakeIx(orderParams: OrderParams, makerInfo?: MakerInfo): Promise<TransactionInstruction>;
|
|
116
112
|
/**
|
package/lib/clearingHouse.js
CHANGED
|
@@ -491,8 +491,6 @@ class ClearingHouse {
|
|
|
491
491
|
}
|
|
492
492
|
async getPlaceOrderIx(orderParams) {
|
|
493
493
|
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
494
|
-
const priceOracle = this.getMarketAccount(orderParams.marketIndex).amm
|
|
495
|
-
.oracle;
|
|
496
494
|
const remainingAccounts = this.getRemainingAccounts({
|
|
497
495
|
writableMarketIndex: orderParams.marketIndex,
|
|
498
496
|
});
|
|
@@ -501,26 +499,10 @@ class ClearingHouse {
|
|
|
501
499
|
state: await this.getStatePublicKey(),
|
|
502
500
|
user: userAccountPublicKey,
|
|
503
501
|
authority: this.wallet.publicKey,
|
|
504
|
-
oracle: priceOracle,
|
|
505
502
|
},
|
|
506
503
|
remainingAccounts,
|
|
507
504
|
});
|
|
508
505
|
}
|
|
509
|
-
async expireOrders(userAccountPublicKey) {
|
|
510
|
-
const { txSig } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getExpireOrdersIx(userAccountPublicKey)), [], this.opts);
|
|
511
|
-
return txSig;
|
|
512
|
-
}
|
|
513
|
-
async getExpireOrdersIx(userAccountPublicKey) {
|
|
514
|
-
const fillerPublicKey = await this.getUserAccountPublicKey();
|
|
515
|
-
return await this.program.instruction.expireOrders({
|
|
516
|
-
accounts: {
|
|
517
|
-
state: await this.getStatePublicKey(),
|
|
518
|
-
filler: fillerPublicKey,
|
|
519
|
-
user: userAccountPublicKey,
|
|
520
|
-
authority: this.wallet.publicKey,
|
|
521
|
-
},
|
|
522
|
-
});
|
|
523
|
-
}
|
|
524
506
|
async updateAMMs(marketIndexes) {
|
|
525
507
|
const { txSig } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getUpdateAMMsIx(marketIndexes)), [], this.opts);
|
|
526
508
|
return txSig;
|
|
@@ -561,15 +543,12 @@ class ClearingHouse {
|
|
|
561
543
|
}
|
|
562
544
|
async getCancelOrderIx(orderId) {
|
|
563
545
|
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
564
|
-
const order = this.getOrder(orderId);
|
|
565
|
-
const oracle = this.getMarketAccount(order.marketIndex).amm.oracle;
|
|
566
546
|
const remainingAccounts = this.getRemainingAccounts({});
|
|
567
|
-
return await this.program.instruction.cancelOrder(orderId, {
|
|
547
|
+
return await this.program.instruction.cancelOrder(orderId !== null && orderId !== void 0 ? orderId : null, {
|
|
568
548
|
accounts: {
|
|
569
549
|
state: await this.getStatePublicKey(),
|
|
570
550
|
user: userAccountPublicKey,
|
|
571
551
|
authority: this.wallet.publicKey,
|
|
572
|
-
oracle,
|
|
573
552
|
},
|
|
574
553
|
remainingAccounts,
|
|
575
554
|
});
|
|
@@ -593,63 +572,79 @@ class ClearingHouse {
|
|
|
593
572
|
remainingAccounts,
|
|
594
573
|
});
|
|
595
574
|
}
|
|
596
|
-
async
|
|
597
|
-
const { txSig } = await this.txSender.send((0, utils_1.wrapInTx)(await this.
|
|
575
|
+
async fillOrder(userAccountPublicKey, user, order, makerInfo) {
|
|
576
|
+
const { txSig } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getFillOrderIx(userAccountPublicKey, user, order, makerInfo)), [], this.opts);
|
|
598
577
|
return txSig;
|
|
599
578
|
}
|
|
600
|
-
async
|
|
601
|
-
const
|
|
602
|
-
const
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
pubkey:
|
|
579
|
+
async getFillOrderIx(userAccountPublicKey, userAccount, order, makerInfo) {
|
|
580
|
+
const fillerPublicKey = await this.getUserAccountPublicKey();
|
|
581
|
+
const marketIndex = order.marketIndex;
|
|
582
|
+
const marketAccount = this.getMarketAccount(marketIndex);
|
|
583
|
+
const bankAccountInfos = [
|
|
584
|
+
{
|
|
585
|
+
pubkey: this.getQuoteAssetBankAccount().pubkey,
|
|
586
|
+
isSigner: false,
|
|
587
|
+
isWritable: true,
|
|
588
|
+
},
|
|
589
|
+
];
|
|
590
|
+
const marketAccountInfos = [
|
|
591
|
+
{
|
|
592
|
+
pubkey: marketAccount.pubkey,
|
|
593
|
+
isWritable: true,
|
|
594
|
+
isSigner: false,
|
|
595
|
+
},
|
|
596
|
+
];
|
|
597
|
+
const oracleAccountInfos = [
|
|
598
|
+
{
|
|
599
|
+
pubkey: marketAccount.amm.oracle,
|
|
607
600
|
isWritable: false,
|
|
608
601
|
isSigner: false,
|
|
609
|
-
});
|
|
610
|
-
}
|
|
611
|
-
return await this.program.instruction.cancelAllOrders(bestEffort, {
|
|
612
|
-
accounts: {
|
|
613
|
-
state: await this.getStatePublicKey(),
|
|
614
|
-
user: userAccountPublicKey,
|
|
615
|
-
authority: this.wallet.publicKey,
|
|
616
602
|
},
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
603
|
+
];
|
|
604
|
+
for (const position of userAccount.positions) {
|
|
605
|
+
if (!(0, position_1.positionIsAvailable)(position) &&
|
|
606
|
+
!position.marketIndex.eq(order.marketIndex)) {
|
|
607
|
+
const market = this.getMarketAccount(position.marketIndex);
|
|
608
|
+
marketAccountInfos.push({
|
|
609
|
+
pubkey: market.pubkey,
|
|
610
|
+
isWritable: false,
|
|
611
|
+
isSigner: false,
|
|
612
|
+
});
|
|
613
|
+
oracleAccountInfos.push({
|
|
614
|
+
pubkey: market.amm.oracle,
|
|
615
|
+
isWritable: false,
|
|
616
|
+
isSigner: false,
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
const remainingAccounts = oracleAccountInfos.concat(bankAccountInfos.concat(marketAccountInfos));
|
|
621
|
+
if (makerInfo) {
|
|
629
622
|
remainingAccounts.push({
|
|
630
|
-
pubkey:
|
|
631
|
-
isWritable:
|
|
623
|
+
pubkey: makerInfo.maker,
|
|
624
|
+
isWritable: true,
|
|
632
625
|
isSigner: false,
|
|
633
626
|
});
|
|
634
627
|
}
|
|
635
|
-
|
|
628
|
+
const orderId = order.orderId;
|
|
629
|
+
const makerOrderId = makerInfo ? makerInfo.order.orderId : null;
|
|
630
|
+
return await this.program.instruction.fillOrder(orderId, makerOrderId, {
|
|
636
631
|
accounts: {
|
|
637
632
|
state: await this.getStatePublicKey(),
|
|
633
|
+
filler: fillerPublicKey,
|
|
638
634
|
user: userAccountPublicKey,
|
|
639
635
|
authority: this.wallet.publicKey,
|
|
640
636
|
},
|
|
641
637
|
remainingAccounts,
|
|
642
638
|
});
|
|
643
639
|
}
|
|
644
|
-
async
|
|
645
|
-
const { txSig } = await this.txSender.send((0, utils_1.wrapInTx)(await this.
|
|
640
|
+
async triggerOrder(userAccountPublicKey, user, order) {
|
|
641
|
+
const { txSig } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getTriggerOrderIx(userAccountPublicKey, user, order)), [], this.opts);
|
|
646
642
|
return txSig;
|
|
647
643
|
}
|
|
648
|
-
async
|
|
644
|
+
async getTriggerOrderIx(userAccountPublicKey, userAccount, order) {
|
|
649
645
|
const fillerPublicKey = await this.getUserAccountPublicKey();
|
|
650
646
|
const marketIndex = order.marketIndex;
|
|
651
647
|
const marketAccount = this.getMarketAccount(marketIndex);
|
|
652
|
-
const oracle = marketAccount.amm.oracle;
|
|
653
648
|
const bankAccountInfos = [
|
|
654
649
|
{
|
|
655
650
|
pubkey: this.getQuoteAssetBankAccount().pubkey,
|
|
@@ -688,22 +683,13 @@ class ClearingHouse {
|
|
|
688
683
|
}
|
|
689
684
|
}
|
|
690
685
|
const remainingAccounts = oracleAccountInfos.concat(bankAccountInfos.concat(marketAccountInfos));
|
|
691
|
-
if (makerInfo) {
|
|
692
|
-
remainingAccounts.push({
|
|
693
|
-
pubkey: makerInfo.maker,
|
|
694
|
-
isWritable: true,
|
|
695
|
-
isSigner: false,
|
|
696
|
-
});
|
|
697
|
-
}
|
|
698
686
|
const orderId = order.orderId;
|
|
699
|
-
|
|
700
|
-
return await this.program.instruction.fillOrder(orderId, makerOrderId, {
|
|
687
|
+
return await this.program.instruction.triggerOrder(orderId, {
|
|
701
688
|
accounts: {
|
|
702
689
|
state: await this.getStatePublicKey(),
|
|
703
690
|
filler: fillerPublicKey,
|
|
704
691
|
user: userAccountPublicKey,
|
|
705
692
|
authority: this.wallet.publicKey,
|
|
706
|
-
oracle: oracle,
|
|
707
693
|
},
|
|
708
694
|
remainingAccounts,
|
|
709
695
|
});
|
|
@@ -715,8 +701,6 @@ class ClearingHouse {
|
|
|
715
701
|
}
|
|
716
702
|
async getPlaceAndTakeIx(orderParams, makerInfo) {
|
|
717
703
|
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
718
|
-
const priceOracle = this.getMarketAccount(orderParams.marketIndex).amm
|
|
719
|
-
.oracle;
|
|
720
704
|
const remainingAccounts = this.getRemainingAccounts({
|
|
721
705
|
writableMarketIndex: orderParams.marketIndex,
|
|
722
706
|
writableBankIndex: numericConstants_1.QUOTE_ASSET_BANK_INDEX,
|
|
@@ -735,7 +719,6 @@ class ClearingHouse {
|
|
|
735
719
|
state: await this.getStatePublicKey(),
|
|
736
720
|
user: userAccountPublicKey,
|
|
737
721
|
authority: this.wallet.publicKey,
|
|
738
|
-
oracle: priceOracle,
|
|
739
722
|
},
|
|
740
723
|
remainingAccounts,
|
|
741
724
|
});
|
package/lib/config.js
CHANGED
|
@@ -7,7 +7,7 @@ exports.configs = {
|
|
|
7
7
|
devnet: {
|
|
8
8
|
ENV: 'devnet',
|
|
9
9
|
PYTH_ORACLE_MAPPING_ADDRESS: 'BmA9Z6FjioHJPpjT39QazZyhDRUdZy2ezwx4GiDdE2u2',
|
|
10
|
-
CLEARING_HOUSE_PROGRAM_ID: '
|
|
10
|
+
CLEARING_HOUSE_PROGRAM_ID: '9jwr5nC2f9yAraXrg4UzHXmCX3vi9FQkjD6p9e8bRqNa',
|
|
11
11
|
USDC_MINT_ADDRESS: '8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2',
|
|
12
12
|
MARKETS: markets_1.DevnetMarkets,
|
|
13
13
|
BANKS: banks_1.DevnetBanks,
|
|
@@ -29,6 +29,7 @@ export declare const AMM_RESERVE_PRECISION: BN;
|
|
|
29
29
|
export declare const BASE_PRECISION: BN;
|
|
30
30
|
export declare const BASE_PRECISION_EXP: BN;
|
|
31
31
|
export declare const AMM_TO_QUOTE_PRECISION_RATIO: BN;
|
|
32
|
+
export declare const PRICE_DIV_PEG: BN;
|
|
32
33
|
export declare const PRICE_TO_QUOTE_PRECISION: BN;
|
|
33
34
|
export declare const AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO: BN;
|
|
34
35
|
export declare const MARGIN_PRECISION: BN;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.QUOTE_ASSET_BANK_INDEX = exports.ONE_YEAR = exports.BID_ASK_SPREAD_PRECISION = exports.MARGIN_PRECISION = exports.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO = exports.PRICE_TO_QUOTE_PRECISION = exports.AMM_TO_QUOTE_PRECISION_RATIO = exports.BASE_PRECISION_EXP = exports.BASE_PRECISION = exports.AMM_RESERVE_PRECISION = exports.PEG_PRECISION = exports.FUNDING_PAYMENT_PRECISION = exports.MARK_PRICE_PRECISION = exports.QUOTE_PRECISION = exports.BANK_BALANCE_PRECISION = exports.BANK_BALANCE_PRECISION_EXP = exports.BANK_WEIGHT_PRECISION = exports.BANK_RATE_PRECISION = exports.BANK_UTILIZATION_PRECISION = exports.BANK_CUMULATIVE_INTEREST_PRECISION = exports.BANK_INTEREST_PRECISION = exports.AMM_RESERVE_PRECISION_EXP = exports.PEG_PRECISION_EXP = exports.FUNDING_RATE_PRECISION_EXP = exports.MARK_PRICE_PRECISION_EXP = exports.FUNDING_PAYMENT_PRECISION_EXP = exports.QUOTE_PRECISION_EXP = exports.MAX_LEVERAGE = exports.TEN_MILLION = exports.BN_MAX = exports.TEN_THOUSAND = exports.TEN = exports.TWO = exports.ONE = exports.ZERO = void 0;
|
|
3
|
+
exports.QUOTE_ASSET_BANK_INDEX = exports.ONE_YEAR = exports.BID_ASK_SPREAD_PRECISION = exports.MARGIN_PRECISION = exports.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO = exports.PRICE_TO_QUOTE_PRECISION = exports.PRICE_DIV_PEG = exports.AMM_TO_QUOTE_PRECISION_RATIO = exports.BASE_PRECISION_EXP = exports.BASE_PRECISION = exports.AMM_RESERVE_PRECISION = exports.PEG_PRECISION = exports.FUNDING_PAYMENT_PRECISION = exports.MARK_PRICE_PRECISION = exports.QUOTE_PRECISION = exports.BANK_BALANCE_PRECISION = exports.BANK_BALANCE_PRECISION_EXP = exports.BANK_WEIGHT_PRECISION = exports.BANK_RATE_PRECISION = exports.BANK_UTILIZATION_PRECISION = exports.BANK_CUMULATIVE_INTEREST_PRECISION = exports.BANK_INTEREST_PRECISION = exports.AMM_RESERVE_PRECISION_EXP = exports.PEG_PRECISION_EXP = exports.FUNDING_RATE_PRECISION_EXP = exports.MARK_PRICE_PRECISION_EXP = exports.FUNDING_PAYMENT_PRECISION_EXP = exports.QUOTE_PRECISION_EXP = exports.MAX_LEVERAGE = exports.TEN_MILLION = exports.BN_MAX = exports.TEN_THOUSAND = exports.TEN = exports.TWO = exports.ONE = exports.ZERO = void 0;
|
|
4
4
|
const __1 = require("../");
|
|
5
5
|
exports.ZERO = new __1.BN(0);
|
|
6
6
|
exports.ONE = new __1.BN(1);
|
|
@@ -31,6 +31,7 @@ exports.AMM_RESERVE_PRECISION = new __1.BN(10).pow(exports.AMM_RESERVE_PRECISION
|
|
|
31
31
|
exports.BASE_PRECISION = exports.AMM_RESERVE_PRECISION;
|
|
32
32
|
exports.BASE_PRECISION_EXP = exports.AMM_RESERVE_PRECISION_EXP;
|
|
33
33
|
exports.AMM_TO_QUOTE_PRECISION_RATIO = exports.AMM_RESERVE_PRECISION.div(exports.QUOTE_PRECISION); // 10^7
|
|
34
|
+
exports.PRICE_DIV_PEG = exports.MARK_PRICE_PRECISION.div(exports.PEG_PRECISION); //10^7
|
|
34
35
|
exports.PRICE_TO_QUOTE_PRECISION = exports.MARK_PRICE_PRECISION.div(exports.QUOTE_PRECISION);
|
|
35
36
|
exports.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO = exports.AMM_RESERVE_PRECISION.mul(exports.PEG_PRECISION).div(exports.QUOTE_PRECISION); // 10^10
|
|
36
37
|
exports.MARGIN_PRECISION = exports.TEN_THOUSAND;
|
package/lib/factory/bigNum.d.ts
CHANGED
|
@@ -55,7 +55,7 @@ export declare class BigNum {
|
|
|
55
55
|
* @returns
|
|
56
56
|
*/
|
|
57
57
|
print(): string;
|
|
58
|
-
prettyPrint(): string;
|
|
58
|
+
prettyPrint(useTradePrecision?: boolean, precisionOverride?: number): string;
|
|
59
59
|
/**
|
|
60
60
|
* Print and remove unnecessary trailing zeroes
|
|
61
61
|
* @returns
|
|
@@ -75,7 +75,13 @@ export declare class BigNum {
|
|
|
75
75
|
*/
|
|
76
76
|
toPrecision(fixedPrecision: number, trailingZeroes?: boolean): string;
|
|
77
77
|
toTradePrecision(): string;
|
|
78
|
-
|
|
78
|
+
/**
|
|
79
|
+
* Print dollar formatted value. Defaults to fixed decimals two unless a given precision is given.
|
|
80
|
+
* @param useTradePrecision
|
|
81
|
+
* @param precisionOverride
|
|
82
|
+
* @returns
|
|
83
|
+
*/
|
|
84
|
+
toNotional(useTradePrecision?: boolean, precisionOverride?: number): string;
|
|
79
85
|
toMillified(precision?: number): string;
|
|
80
86
|
toJSON(): {
|
|
81
87
|
val: string;
|
package/lib/factory/bigNum.js
CHANGED
|
@@ -156,8 +156,8 @@ class BigNum {
|
|
|
156
156
|
printString = printString.slice(0, printString.length - 1);
|
|
157
157
|
return printString;
|
|
158
158
|
}
|
|
159
|
-
prettyPrint() {
|
|
160
|
-
const [leftSide, rightSide] = this.printShort().split(BigNum.delim);
|
|
159
|
+
prettyPrint(useTradePrecision, precisionOverride) {
|
|
160
|
+
const [leftSide, rightSide] = this.printShort(useTradePrecision, precisionOverride).split(BigNum.delim);
|
|
161
161
|
let formattedLeftSide = leftSide;
|
|
162
162
|
const isNeg = formattedLeftSide.includes('-');
|
|
163
163
|
if (isNeg) {
|
|
@@ -254,10 +254,18 @@ class BigNum {
|
|
|
254
254
|
toTradePrecision() {
|
|
255
255
|
return this.toPrecision(6, true);
|
|
256
256
|
}
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
257
|
+
/**
|
|
258
|
+
* Print dollar formatted value. Defaults to fixed decimals two unless a given precision is given.
|
|
259
|
+
* @param useTradePrecision
|
|
260
|
+
* @param precisionOverride
|
|
261
|
+
* @returns
|
|
262
|
+
*/
|
|
263
|
+
toNotional(useTradePrecision, precisionOverride) {
|
|
264
|
+
const prefix = `${this.lt(BigNum.zero()) ? `-` : ``}$`;
|
|
265
|
+
const val = useTradePrecision || precisionOverride
|
|
266
|
+
? this.prettyPrint(useTradePrecision, precisionOverride)
|
|
267
|
+
: BigNum.fromPrint(this.toFixed(2), new anchor_1.BN(2)).prettyPrint();
|
|
268
|
+
return `${prefix}${val.replace('-', '')}`;
|
|
261
269
|
}
|
|
262
270
|
toMillified(precision = 3) {
|
|
263
271
|
const stringVal = this.print();
|
|
@@ -381,11 +381,6 @@
|
|
|
381
381
|
"name": "authority",
|
|
382
382
|
"isMut": false,
|
|
383
383
|
"isSigner": true
|
|
384
|
-
},
|
|
385
|
-
{
|
|
386
|
-
"name": "oracle",
|
|
387
|
-
"isMut": false,
|
|
388
|
-
"isSigner": false
|
|
389
384
|
}
|
|
390
385
|
],
|
|
391
386
|
"args": [
|
|
@@ -414,17 +409,14 @@
|
|
|
414
409
|
"name": "authority",
|
|
415
410
|
"isMut": false,
|
|
416
411
|
"isSigner": true
|
|
417
|
-
},
|
|
418
|
-
{
|
|
419
|
-
"name": "oracle",
|
|
420
|
-
"isMut": false,
|
|
421
|
-
"isSigner": false
|
|
422
412
|
}
|
|
423
413
|
],
|
|
424
414
|
"args": [
|
|
425
415
|
{
|
|
426
416
|
"name": "orderId",
|
|
427
|
-
"type":
|
|
417
|
+
"type": {
|
|
418
|
+
"option": "u64"
|
|
419
|
+
}
|
|
428
420
|
}
|
|
429
421
|
]
|
|
430
422
|
},
|
|
@@ -445,11 +437,6 @@
|
|
|
445
437
|
"name": "authority",
|
|
446
438
|
"isMut": false,
|
|
447
439
|
"isSigner": true
|
|
448
|
-
},
|
|
449
|
-
{
|
|
450
|
-
"name": "oracle",
|
|
451
|
-
"isMut": false,
|
|
452
|
-
"isSigner": false
|
|
453
440
|
}
|
|
454
441
|
],
|
|
455
442
|
"args": [
|
|
@@ -481,17 +468,14 @@
|
|
|
481
468
|
"name": "user",
|
|
482
469
|
"isMut": true,
|
|
483
470
|
"isSigner": false
|
|
484
|
-
},
|
|
485
|
-
{
|
|
486
|
-
"name": "oracle",
|
|
487
|
-
"isMut": false,
|
|
488
|
-
"isSigner": false
|
|
489
471
|
}
|
|
490
472
|
],
|
|
491
473
|
"args": [
|
|
492
474
|
{
|
|
493
475
|
"name": "orderId",
|
|
494
|
-
"type":
|
|
476
|
+
"type": {
|
|
477
|
+
"option": "u64"
|
|
478
|
+
}
|
|
495
479
|
},
|
|
496
480
|
{
|
|
497
481
|
"name": "makerOrderId",
|
|
@@ -518,11 +502,6 @@
|
|
|
518
502
|
"name": "authority",
|
|
519
503
|
"isMut": false,
|
|
520
504
|
"isSigner": true
|
|
521
|
-
},
|
|
522
|
-
{
|
|
523
|
-
"name": "oracle",
|
|
524
|
-
"isMut": false,
|
|
525
|
-
"isSigner": false
|
|
526
505
|
}
|
|
527
506
|
],
|
|
528
507
|
"args": [
|
|
@@ -562,11 +541,6 @@
|
|
|
562
541
|
"name": "authority",
|
|
563
542
|
"isMut": false,
|
|
564
543
|
"isSigner": true
|
|
565
|
-
},
|
|
566
|
-
{
|
|
567
|
-
"name": "oracle",
|
|
568
|
-
"isMut": false,
|
|
569
|
-
"isSigner": false
|
|
570
544
|
}
|
|
571
545
|
],
|
|
572
546
|
"args": [
|
|
@@ -582,6 +556,37 @@
|
|
|
582
556
|
}
|
|
583
557
|
]
|
|
584
558
|
},
|
|
559
|
+
{
|
|
560
|
+
"name": "triggerOrder",
|
|
561
|
+
"accounts": [
|
|
562
|
+
{
|
|
563
|
+
"name": "state",
|
|
564
|
+
"isMut": false,
|
|
565
|
+
"isSigner": false
|
|
566
|
+
},
|
|
567
|
+
{
|
|
568
|
+
"name": "authority",
|
|
569
|
+
"isMut": false,
|
|
570
|
+
"isSigner": true
|
|
571
|
+
},
|
|
572
|
+
{
|
|
573
|
+
"name": "filler",
|
|
574
|
+
"isMut": true,
|
|
575
|
+
"isSigner": false
|
|
576
|
+
},
|
|
577
|
+
{
|
|
578
|
+
"name": "user",
|
|
579
|
+
"isMut": true,
|
|
580
|
+
"isSigner": false
|
|
581
|
+
}
|
|
582
|
+
],
|
|
583
|
+
"args": [
|
|
584
|
+
{
|
|
585
|
+
"name": "orderId",
|
|
586
|
+
"type": "u64"
|
|
587
|
+
}
|
|
588
|
+
]
|
|
589
|
+
},
|
|
585
590
|
{
|
|
586
591
|
"name": "updateAmms",
|
|
587
592
|
"accounts": [
|
|
@@ -993,11 +998,6 @@
|
|
|
993
998
|
"name": "user",
|
|
994
999
|
"isMut": true,
|
|
995
1000
|
"isSigner": false
|
|
996
|
-
},
|
|
997
|
-
{
|
|
998
|
-
"name": "market",
|
|
999
|
-
"isMut": false,
|
|
1000
|
-
"isSigner": false
|
|
1001
1001
|
}
|
|
1002
1002
|
],
|
|
1003
1003
|
"args": []
|
|
@@ -1389,6 +1389,32 @@
|
|
|
1389
1389
|
}
|
|
1390
1390
|
]
|
|
1391
1391
|
},
|
|
1392
|
+
{
|
|
1393
|
+
"name": "updateMarketMaxSpread",
|
|
1394
|
+
"accounts": [
|
|
1395
|
+
{
|
|
1396
|
+
"name": "admin",
|
|
1397
|
+
"isMut": false,
|
|
1398
|
+
"isSigner": true
|
|
1399
|
+
},
|
|
1400
|
+
{
|
|
1401
|
+
"name": "state",
|
|
1402
|
+
"isMut": false,
|
|
1403
|
+
"isSigner": false
|
|
1404
|
+
},
|
|
1405
|
+
{
|
|
1406
|
+
"name": "market",
|
|
1407
|
+
"isMut": true,
|
|
1408
|
+
"isSigner": false
|
|
1409
|
+
}
|
|
1410
|
+
],
|
|
1411
|
+
"args": [
|
|
1412
|
+
{
|
|
1413
|
+
"name": "maxSpread",
|
|
1414
|
+
"type": "u32"
|
|
1415
|
+
}
|
|
1416
|
+
]
|
|
1417
|
+
},
|
|
1392
1418
|
{
|
|
1393
1419
|
"name": "updateMarketBaseAssetAmountStepSize",
|
|
1394
1420
|
"accounts": [
|
|
@@ -2194,6 +2220,10 @@
|
|
|
2194
2220
|
"name": "shortSpread",
|
|
2195
2221
|
"type": "u128"
|
|
2196
2222
|
},
|
|
2223
|
+
{
|
|
2224
|
+
"name": "maxSpread",
|
|
2225
|
+
"type": "u32"
|
|
2226
|
+
},
|
|
2197
2227
|
{
|
|
2198
2228
|
"name": "askBaseAssetReserve",
|
|
2199
2229
|
"type": "u128"
|
|
@@ -2252,7 +2282,7 @@
|
|
|
2252
2282
|
},
|
|
2253
2283
|
{
|
|
2254
2284
|
"name": "totalFeeMinusDistributions",
|
|
2255
|
-
"type": "
|
|
2285
|
+
"type": "i128"
|
|
2256
2286
|
},
|
|
2257
2287
|
{
|
|
2258
2288
|
"name": "totalFeeWithdrawn",
|
|
@@ -2712,6 +2742,10 @@
|
|
|
2712
2742
|
"defined": "OrderTriggerCondition"
|
|
2713
2743
|
}
|
|
2714
2744
|
},
|
|
2745
|
+
{
|
|
2746
|
+
"name": "triggered",
|
|
2747
|
+
"type": "bool"
|
|
2748
|
+
},
|
|
2715
2749
|
{
|
|
2716
2750
|
"name": "referrer",
|
|
2717
2751
|
"type": "publicKey"
|
|
@@ -2848,6 +2882,9 @@
|
|
|
2848
2882
|
{
|
|
2849
2883
|
"name": "Fill"
|
|
2850
2884
|
},
|
|
2885
|
+
{
|
|
2886
|
+
"name": "Trigger"
|
|
2887
|
+
},
|
|
2851
2888
|
{
|
|
2852
2889
|
"name": "Expire"
|
|
2853
2890
|
}
|
|
@@ -3016,6 +3053,11 @@
|
|
|
3016
3053
|
"type": "u64",
|
|
3017
3054
|
"index": false
|
|
3018
3055
|
},
|
|
3056
|
+
{
|
|
3057
|
+
"name": "oraclePrice",
|
|
3058
|
+
"type": "i128",
|
|
3059
|
+
"index": false
|
|
3060
|
+
},
|
|
3019
3061
|
{
|
|
3020
3062
|
"name": "from",
|
|
3021
3063
|
"type": {
|
|
@@ -3217,7 +3259,7 @@
|
|
|
3217
3259
|
},
|
|
3218
3260
|
{
|
|
3219
3261
|
"name": "totalFeeMinusDistributions",
|
|
3220
|
-
"type": "
|
|
3262
|
+
"type": "i128",
|
|
3221
3263
|
"index": false
|
|
3222
3264
|
},
|
|
3223
3265
|
{
|
|
@@ -3349,6 +3391,16 @@
|
|
|
3349
3391
|
},
|
|
3350
3392
|
"index": false
|
|
3351
3393
|
},
|
|
3394
|
+
{
|
|
3395
|
+
"name": "makerUnsettledPnl",
|
|
3396
|
+
"type": "i128",
|
|
3397
|
+
"index": false
|
|
3398
|
+
},
|
|
3399
|
+
{
|
|
3400
|
+
"name": "takerUnsettledPnl",
|
|
3401
|
+
"type": "i128",
|
|
3402
|
+
"index": false
|
|
3403
|
+
},
|
|
3352
3404
|
{
|
|
3353
3405
|
"name": "action",
|
|
3354
3406
|
"type": {
|
|
@@ -3854,6 +3906,21 @@
|
|
|
3854
3906
|
},
|
|
3855
3907
|
{
|
|
3856
3908
|
"code": 6087,
|
|
3909
|
+
"name": "OrderMustBeTriggeredFirst",
|
|
3910
|
+
"msg": "OrderMustBeTriggeredFirst"
|
|
3911
|
+
},
|
|
3912
|
+
{
|
|
3913
|
+
"code": 6088,
|
|
3914
|
+
"name": "OrderNotTriggerable",
|
|
3915
|
+
"msg": "OrderNotTriggerable"
|
|
3916
|
+
},
|
|
3917
|
+
{
|
|
3918
|
+
"code": 6089,
|
|
3919
|
+
"name": "OrderDidNotSatisfyTriggerCondition",
|
|
3920
|
+
"msg": "OrderDidNotSatisfyTriggerCondition"
|
|
3921
|
+
},
|
|
3922
|
+
{
|
|
3923
|
+
"code": 6090,
|
|
3857
3924
|
"name": "DefaultError",
|
|
3858
3925
|
"msg": "DefaultError"
|
|
3859
3926
|
}
|
package/lib/math/amm.d.ts
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
import { BN } from '@project-serum/anchor';
|
|
3
3
|
import { AMM, PositionDirection, SwapDirection, MarketAccount } from '../types';
|
|
4
4
|
import { OraclePriceData } from '../oracles/types';
|
|
5
|
+
export declare function calculatePegFromTargetPrice(targetPrice: BN, baseAssetReserve: BN, quoteAssetReserve: BN): BN;
|
|
6
|
+
export declare function calculateOptimalPegAndBudget(amm: AMM, oraclePriceData: OraclePriceData): [BN, BN, BN, boolean];
|
|
5
7
|
export declare function calculateNewAmm(amm: AMM, oraclePriceData: OraclePriceData): [BN, BN, BN, BN];
|
|
6
8
|
export declare function calculateUpdatedAMM(amm: AMM, oraclePriceData: OraclePriceData): AMM;
|
|
7
9
|
export declare function calculateUpdatedAMMSpreadReserves(amm: AMM, direction: PositionDirection, oraclePriceData: OraclePriceData): {
|
|
@@ -10,7 +12,7 @@ export declare function calculateUpdatedAMMSpreadReserves(amm: AMM, direction: P
|
|
|
10
12
|
sqrtK: BN;
|
|
11
13
|
newPeg: BN;
|
|
12
14
|
};
|
|
13
|
-
export declare function calculateBidAskPrice(amm: AMM, oraclePriceData: OraclePriceData): [BN, BN];
|
|
15
|
+
export declare function calculateBidAskPrice(amm: AMM, oraclePriceData: OraclePriceData, withUpdate?: boolean): [BN, BN];
|
|
14
16
|
/**
|
|
15
17
|
* Calculates a price given an arbitrary base and quote amount (they must have the same precision)
|
|
16
18
|
*
|
|
@@ -31,6 +33,9 @@ export declare type AssetType = 'quote' | 'base';
|
|
|
31
33
|
* @returns quoteAssetReserve and baseAssetReserve after swap. : Precision AMM_RESERVE_PRECISION
|
|
32
34
|
*/
|
|
33
35
|
export declare function calculateAmmReservesAfterSwap(amm: Pick<AMM, 'pegMultiplier' | 'quoteAssetReserve' | 'sqrtK' | 'baseAssetReserve'>, inputAssetType: AssetType, swapAmount: BN, swapDirection: SwapDirection): [BN, BN];
|
|
36
|
+
export declare function calculateEffectiveLeverage(baseSpread: number, quoteAssetReserve: BN, terminalQuoteAssetReserve: BN, pegMultiplier: BN, netBaseAssetAmount: BN, markPrice: BN, totalFeeMinusDistributions: BN): number;
|
|
37
|
+
export declare function calculateMaxSpread(marginRatioInitial: number): number;
|
|
38
|
+
export declare function calculateSpreadBN(baseSpread: number, lastOracleMarkSpreadPct: BN, lastOracleConfPct: BN, maxSpread: number, quoteAssetReserve: BN, terminalQuoteAssetReserve: BN, pegMultiplier: BN, netBaseAssetAmount: BN, markPrice: BN, totalFeeMinusDistributions: BN): [number, number];
|
|
34
39
|
export declare function calculateSpread(amm: AMM, direction: PositionDirection, oraclePriceData: OraclePriceData): number;
|
|
35
40
|
export declare function calculateSpreadReserves(amm: AMM, direction: PositionDirection, oraclePriceData: OraclePriceData): {
|
|
36
41
|
baseAssetReserve: BN;
|