@drift-labs/sdk 0.2.0-master.30 → 0.2.0-master.32
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 +11 -7
- package/lib/admin.js +50 -12
- package/lib/clearingHouse.d.ts +26 -23
- package/lib/clearingHouse.js +290 -652
- package/lib/clearingHouseUser.d.ts +1 -1
- package/lib/clearingHouseUser.js +9 -12
- package/lib/config.js +1 -1
- package/lib/constants/numericConstants.d.ts +7 -0
- package/lib/constants/numericConstants.js +8 -1
- package/lib/constants/spotMarkets.js +4 -4
- package/lib/dlob/DLOB.d.ts +7 -5
- package/lib/dlob/DLOB.js +159 -88
- package/lib/dlob/DLOBNode.d.ts +2 -0
- package/lib/dlob/DLOBNode.js +3 -0
- package/lib/dlob/NodeList.d.ts +2 -1
- package/lib/dlob/NodeList.js +10 -4
- package/lib/events/types.d.ts +2 -1
- package/lib/events/types.js +1 -0
- package/lib/factory/bigNum.d.ts +1 -0
- package/lib/factory/bigNum.js +14 -0
- package/lib/idl/clearing_house.json +1091 -611
- package/lib/math/amm.d.ts +2 -2
- package/lib/math/amm.js +31 -28
- package/lib/math/market.d.ts +1 -1
- package/lib/math/market.js +6 -6
- package/lib/math/spotBalance.js +7 -8
- package/lib/math/trade.js +11 -11
- package/lib/types.d.ts +118 -41
- package/lib/types.js +34 -4
- package/package.json +4 -2
- package/src/admin.ts +129 -29
- package/src/clearingHouse.ts +380 -787
- package/src/clearingHouseUser.ts +11 -14
- package/src/config.ts +1 -1
- package/src/constants/numericConstants.ts +7 -0
- package/src/constants/spotMarkets.ts +5 -4
- package/src/dlob/DLOB.ts +221 -103
- package/src/dlob/DLOBNode.ts +5 -0
- package/src/dlob/NodeList.ts +15 -5
- package/src/events/types.ts +3 -0
- package/src/factory/bigNum.ts +23 -0
- package/src/idl/clearing_house.json +1091 -611
- package/src/math/amm.ts +42 -29
- package/src/math/market.ts +9 -4
- package/src/math/spotBalance.ts +11 -8
- package/src/math/trade.ts +11 -11
- package/src/types.ts +82 -41
- package/tests/bn/test.ts +13 -8
- package/tests/dlob/helpers.ts +56 -33
- package/tests/dlob/test.ts +1397 -495
- package/src/assert/assert.js +0 -9
- package/src/events/eventList.js +0 -77
- package/src/examples/makeTradeExample.js +0 -157
- package/src/token/index.js +0 -38
- package/src/tx/types.js +0 -2
- package/src/tx/utils.js +0 -17
- package/src/util/computeUnits.js +0 -27
- package/src/util/getTokenAddress.js +0 -9
- package/src/util/promiseTimeout.js +0 -14
- package/src/util/tps.js +0 -27
package/lib/types.js
CHANGED
|
@@ -1,13 +1,28 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DefaultOrderParams = exports.LiquidationType = exports.LPAction = exports.TradeSide = exports.getVariant = exports.isOneOfVariant = exports.isVariant = exports.SpotFulfillmentStatus = exports.SpotFulfillmentType = exports.OrderTriggerCondition = exports.OrderActionExplanation = exports.OrderAction = exports.OrderDiscountTier = exports.OrderStatus = exports.MarketType = exports.OrderType = exports.OracleSource = exports.DepositDirection = exports.PositionDirection = exports.SpotBalanceType = exports.SwapDirection = exports.ContractType = exports.MarketStatus = void 0;
|
|
3
|
+
exports.DefaultOrderParams = exports.LiquidationType = exports.LPAction = exports.TradeSide = exports.getVariant = exports.isOneOfVariant = exports.isVariant = exports.SpotFulfillmentStatus = exports.SpotFulfillmentType = exports.OrderTriggerCondition = exports.OrderActionExplanation = exports.OrderAction = exports.OrderDiscountTier = exports.OrderStatus = exports.MarketType = exports.OrderType = exports.OracleSource = exports.DepositDirection = exports.PositionDirection = exports.SpotBalanceType = exports.SwapDirection = exports.AssetTier = exports.ContractTier = exports.ContractType = exports.MarketStatus = exports.ExchangeStatus = void 0;
|
|
4
4
|
const _1 = require(".");
|
|
5
5
|
// # Utility Types / Enums / Constants
|
|
6
|
+
class ExchangeStatus {
|
|
7
|
+
}
|
|
8
|
+
exports.ExchangeStatus = ExchangeStatus;
|
|
9
|
+
ExchangeStatus.ACTIVE = { active: {} };
|
|
10
|
+
ExchangeStatus.FUNDINGPAUSED = { fundingPaused: {} };
|
|
11
|
+
ExchangeStatus.AMMPAUSED = { ammPaused: {} };
|
|
12
|
+
ExchangeStatus.FILLPAUSED = { fillPaused: {} };
|
|
13
|
+
ExchangeStatus.LIQPAUSED = { liqPaused: {} };
|
|
14
|
+
ExchangeStatus.WITHDRAWPAUSED = { withdrawPaused: {} };
|
|
15
|
+
ExchangeStatus.PAUSED = { paused: {} };
|
|
6
16
|
class MarketStatus {
|
|
7
17
|
}
|
|
8
18
|
exports.MarketStatus = MarketStatus;
|
|
9
19
|
MarketStatus.INITIALIZED = { initialized: {} };
|
|
10
|
-
MarketStatus.
|
|
20
|
+
MarketStatus.ACTIVE = { active: {} };
|
|
21
|
+
MarketStatus.FUNDINGPAUSED = { fundingPaused: {} };
|
|
22
|
+
MarketStatus.AMMPAUSED = { ammPaused: {} };
|
|
23
|
+
MarketStatus.FILLPAUSED = { fillPaused: {} };
|
|
24
|
+
MarketStatus.WITHDRAWPAUSED = { withdrawPaused: {} };
|
|
25
|
+
MarketStatus.REDUCEONLY = { reduceOnly: {} };
|
|
11
26
|
MarketStatus.SETTLEMENT = { settlement: {} };
|
|
12
27
|
MarketStatus.DELISTED = { delisted: {} };
|
|
13
28
|
class ContractType {
|
|
@@ -15,6 +30,21 @@ class ContractType {
|
|
|
15
30
|
exports.ContractType = ContractType;
|
|
16
31
|
ContractType.PERPETUAL = { perpetual: {} };
|
|
17
32
|
ContractType.FUTURE = { future: {} };
|
|
33
|
+
class ContractTier {
|
|
34
|
+
}
|
|
35
|
+
exports.ContractTier = ContractTier;
|
|
36
|
+
ContractTier.A = { a: {} };
|
|
37
|
+
ContractTier.B = { b: {} };
|
|
38
|
+
ContractTier.C = { c: {} };
|
|
39
|
+
ContractTier.Speculative = { speculative: {} };
|
|
40
|
+
class AssetTier {
|
|
41
|
+
}
|
|
42
|
+
exports.AssetTier = AssetTier;
|
|
43
|
+
AssetTier.COLLATERAL = { collateral: {} };
|
|
44
|
+
AssetTier.PROTECTED = { protected: {} };
|
|
45
|
+
AssetTier.CROSS = { cross: {} };
|
|
46
|
+
AssetTier.ISOLATED = { isolated: {} };
|
|
47
|
+
AssetTier.UNLISTED = { unlisted: {} };
|
|
18
48
|
class SwapDirection {
|
|
19
49
|
}
|
|
20
50
|
exports.SwapDirection = SwapDirection;
|
|
@@ -158,10 +188,10 @@ exports.DefaultOrderParams = {
|
|
|
158
188
|
reduceOnly: false,
|
|
159
189
|
postOnly: false,
|
|
160
190
|
immediateOrCancel: false,
|
|
161
|
-
triggerPrice:
|
|
191
|
+
triggerPrice: null,
|
|
162
192
|
triggerCondition: OrderTriggerCondition.ABOVE,
|
|
163
193
|
positionLimit: _1.ZERO,
|
|
164
|
-
oraclePriceOffset:
|
|
194
|
+
oraclePriceOffset: null,
|
|
165
195
|
auctionDuration: null,
|
|
166
196
|
timeInForce: null,
|
|
167
197
|
auctionStartPrice: null,
|
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.32",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"types": "lib/index.d.ts",
|
|
6
6
|
"author": "crispheaney",
|
|
@@ -14,6 +14,8 @@
|
|
|
14
14
|
"build": "yarn clean && tsc",
|
|
15
15
|
"clean": "rm -rf lib",
|
|
16
16
|
"test": "mocha -r ts-node/register tests/**/*.ts",
|
|
17
|
+
"test:inspect": "mocha --inspect-brk -r ts-node/register tests/**/*.ts",
|
|
18
|
+
"test:bignum": "mocha -r ts-node/register tests/bn/**/*.ts",
|
|
17
19
|
"patch-and-pub": "npm version patch --force && npm publish",
|
|
18
20
|
"prettify": "prettier --check './src/***/*.ts'",
|
|
19
21
|
"prettify:fix": "prettier --write './{src,tests}/***/*.ts'"
|
|
@@ -32,7 +34,7 @@
|
|
|
32
34
|
},
|
|
33
35
|
"dependencies": {
|
|
34
36
|
"@project-serum/serum": "^0.13.38",
|
|
35
|
-
"@project-serum/anchor": "0.
|
|
37
|
+
"@project-serum/anchor": "0.25.0-beta.1",
|
|
36
38
|
"@pythnetwork/client": "2.5.3",
|
|
37
39
|
"@solana/spl-token": "^0.1.6",
|
|
38
40
|
"@solana/web3.js": "1.41.0",
|
package/src/admin.ts
CHANGED
|
@@ -3,7 +3,15 @@ import {
|
|
|
3
3
|
SYSVAR_RENT_PUBKEY,
|
|
4
4
|
TransactionSignature,
|
|
5
5
|
} from '@solana/web3.js';
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
FeeStructure,
|
|
8
|
+
OracleGuardRails,
|
|
9
|
+
OracleSource,
|
|
10
|
+
ExchangeStatus,
|
|
11
|
+
MarketStatus,
|
|
12
|
+
ContractTier,
|
|
13
|
+
AssetTier,
|
|
14
|
+
} from './types';
|
|
7
15
|
import { BN } from '@project-serum/anchor';
|
|
8
16
|
import * as anchor from '@project-serum/anchor';
|
|
9
17
|
import {
|
|
@@ -25,7 +33,7 @@ import { calculateAmmReservesAfterSwap, getSwapDirection } from './math/amm';
|
|
|
25
33
|
export class Admin extends ClearingHouse {
|
|
26
34
|
public async initialize(
|
|
27
35
|
usdcMint: PublicKey,
|
|
28
|
-
|
|
36
|
+
_adminControlsPrices: boolean
|
|
29
37
|
): Promise<[TransactionSignature]> {
|
|
30
38
|
const stateAccountRPCResponse = await this.connection.getParsedAccountInfo(
|
|
31
39
|
await this.getStatePublicKey()
|
|
@@ -39,20 +47,17 @@ export class Admin extends ClearingHouse {
|
|
|
39
47
|
this.program.programId
|
|
40
48
|
);
|
|
41
49
|
|
|
42
|
-
const initializeTx = await this.program.transaction.initialize(
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
},
|
|
54
|
-
}
|
|
55
|
-
);
|
|
50
|
+
const initializeTx = await this.program.transaction.initialize({
|
|
51
|
+
accounts: {
|
|
52
|
+
admin: this.wallet.publicKey,
|
|
53
|
+
state: clearingHouseStatePublicKey,
|
|
54
|
+
quoteAssetMint: usdcMint,
|
|
55
|
+
rent: SYSVAR_RENT_PUBKEY,
|
|
56
|
+
clearingHouseSigner: this.getSignerPublicKey(),
|
|
57
|
+
systemProgram: anchor.web3.SystemProgram.programId,
|
|
58
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
59
|
+
},
|
|
60
|
+
});
|
|
56
61
|
|
|
57
62
|
const { txSig: initializeTxSig } = await this.txSender.send(
|
|
58
63
|
initializeTx,
|
|
@@ -65,9 +70,9 @@ export class Admin extends ClearingHouse {
|
|
|
65
70
|
|
|
66
71
|
public async initializeSpotMarket(
|
|
67
72
|
mint: PublicKey,
|
|
68
|
-
optimalUtilization:
|
|
69
|
-
optimalRate:
|
|
70
|
-
maxRate:
|
|
73
|
+
optimalUtilization: number,
|
|
74
|
+
optimalRate: number,
|
|
75
|
+
maxRate: number,
|
|
71
76
|
oracle: PublicKey,
|
|
72
77
|
oracleSource: OracleSource,
|
|
73
78
|
initialAssetWeight: BN,
|
|
@@ -75,7 +80,8 @@ export class Admin extends ClearingHouse {
|
|
|
75
80
|
initialLiabilityWeight: BN,
|
|
76
81
|
maintenanceLiabilityWeight: BN,
|
|
77
82
|
imfFactor = new BN(0),
|
|
78
|
-
liquidationFee = ZERO
|
|
83
|
+
liquidationFee = ZERO,
|
|
84
|
+
activeStatus = true
|
|
79
85
|
): Promise<TransactionSignature> {
|
|
80
86
|
const spotMarketIndex = this.getStateAccount().numberOfSpotMarkets;
|
|
81
87
|
const spotMarket = await getSpotMarketPublicKey(
|
|
@@ -104,6 +110,7 @@ export class Admin extends ClearingHouse {
|
|
|
104
110
|
maintenanceLiabilityWeight,
|
|
105
111
|
imfFactor,
|
|
106
112
|
liquidationFee,
|
|
113
|
+
activeStatus,
|
|
107
114
|
{
|
|
108
115
|
accounts: {
|
|
109
116
|
admin: this.wallet.publicKey,
|
|
@@ -176,7 +183,8 @@ export class Admin extends ClearingHouse {
|
|
|
176
183
|
oracleSource: OracleSource = OracleSource.PYTH,
|
|
177
184
|
marginRatioInitial = 2000,
|
|
178
185
|
marginRatioMaintenance = 500,
|
|
179
|
-
liquidationFee = ZERO
|
|
186
|
+
liquidationFee = ZERO,
|
|
187
|
+
activeStatus = true
|
|
180
188
|
): Promise<TransactionSignature> {
|
|
181
189
|
const marketPublicKey = await getMarketPublicKey(
|
|
182
190
|
this.program.programId,
|
|
@@ -192,6 +200,7 @@ export class Admin extends ClearingHouse {
|
|
|
192
200
|
marginRatioInitial,
|
|
193
201
|
marginRatioMaintenance,
|
|
194
202
|
liquidationFee,
|
|
203
|
+
activeStatus,
|
|
195
204
|
{
|
|
196
205
|
accounts: {
|
|
197
206
|
state: await this.getStatePublicKey(),
|
|
@@ -650,6 +659,25 @@ export class Admin extends ClearingHouse {
|
|
|
650
659
|
);
|
|
651
660
|
}
|
|
652
661
|
|
|
662
|
+
public async updateSpotMarketMaxTokenDeposits(
|
|
663
|
+
marketIndex: number,
|
|
664
|
+
maxTokenDeposits: BN
|
|
665
|
+
): Promise<TransactionSignature> {
|
|
666
|
+
return await this.program.rpc.updateSpotMarketMaxTokenDeposits(
|
|
667
|
+
maxTokenDeposits,
|
|
668
|
+
{
|
|
669
|
+
accounts: {
|
|
670
|
+
admin: this.wallet.publicKey,
|
|
671
|
+
state: await this.getStatePublicKey(),
|
|
672
|
+
spotMarket: await getSpotMarketPublicKey(
|
|
673
|
+
this.program.programId,
|
|
674
|
+
marketIndex
|
|
675
|
+
),
|
|
676
|
+
},
|
|
677
|
+
}
|
|
678
|
+
);
|
|
679
|
+
}
|
|
680
|
+
|
|
653
681
|
public async updateInsuranceWithdrawEscrowPeriod(
|
|
654
682
|
marketIndex: number,
|
|
655
683
|
insuranceWithdrawEscrowPeriod: BN
|
|
@@ -775,29 +803,101 @@ export class Admin extends ClearingHouse {
|
|
|
775
803
|
});
|
|
776
804
|
}
|
|
777
805
|
|
|
778
|
-
public async
|
|
779
|
-
|
|
806
|
+
public async updateSpotMarketMarginWeights(
|
|
807
|
+
spotMarketIndex: number,
|
|
808
|
+
initialAssetWeight: BN,
|
|
809
|
+
maintenanceAssetWeight: BN,
|
|
810
|
+
initialLiabilityWeight: BN,
|
|
811
|
+
maintenanceLiabilityWeight: BN,
|
|
812
|
+
imfFactor = new BN(0)
|
|
813
|
+
): Promise<TransactionSignature> {
|
|
814
|
+
return await this.program.rpc.updateSpotMarketMarginWeights(
|
|
815
|
+
initialAssetWeight,
|
|
816
|
+
maintenanceAssetWeight,
|
|
817
|
+
initialLiabilityWeight,
|
|
818
|
+
maintenanceLiabilityWeight,
|
|
819
|
+
imfFactor,
|
|
820
|
+
{
|
|
821
|
+
accounts: {
|
|
822
|
+
admin: this.wallet.publicKey,
|
|
823
|
+
state: await this.getStatePublicKey(),
|
|
824
|
+
spotMarket: await getSpotMarketPublicKey(
|
|
825
|
+
this.program.programId,
|
|
826
|
+
spotMarketIndex
|
|
827
|
+
),
|
|
828
|
+
},
|
|
829
|
+
}
|
|
830
|
+
);
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
public async updateSpotMarketAssetTier(
|
|
834
|
+
spotMarketIndex: number,
|
|
835
|
+
assetTier: AssetTier
|
|
836
|
+
): Promise<TransactionSignature> {
|
|
837
|
+
return await this.program.rpc.updateSpotMarketAssetTier(assetTier, {
|
|
838
|
+
accounts: {
|
|
839
|
+
admin: this.wallet.publicKey,
|
|
840
|
+
state: await this.getStatePublicKey(),
|
|
841
|
+
spotMarket: await getSpotMarketPublicKey(
|
|
842
|
+
this.program.programId,
|
|
843
|
+
spotMarketIndex
|
|
844
|
+
),
|
|
845
|
+
},
|
|
846
|
+
});
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
public async updateSpotMarketStatus(
|
|
850
|
+
spotMarketIndex: number,
|
|
851
|
+
marketStatus: MarketStatus
|
|
852
|
+
): Promise<TransactionSignature> {
|
|
853
|
+
return await this.program.rpc.updateSpotMarketStatus(marketStatus, {
|
|
854
|
+
accounts: {
|
|
855
|
+
admin: this.wallet.publicKey,
|
|
856
|
+
state: await this.getStatePublicKey(),
|
|
857
|
+
spotMarket: await getSpotMarketPublicKey(
|
|
858
|
+
this.program.programId,
|
|
859
|
+
spotMarketIndex
|
|
860
|
+
),
|
|
861
|
+
},
|
|
862
|
+
});
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
public async updatePerpMarketStatus(
|
|
866
|
+
perpMarketIndex: number,
|
|
867
|
+
marketStatus: MarketStatus
|
|
780
868
|
): Promise<TransactionSignature> {
|
|
781
|
-
return await this.program.rpc.
|
|
869
|
+
return await this.program.rpc.updatePerpMarketStatus(marketStatus, {
|
|
782
870
|
accounts: {
|
|
783
871
|
admin: this.wallet.publicKey,
|
|
784
872
|
state: await this.getStatePublicKey(),
|
|
873
|
+
market: await getMarketPublicKey(
|
|
874
|
+
this.program.programId,
|
|
875
|
+
perpMarketIndex
|
|
876
|
+
),
|
|
785
877
|
},
|
|
786
878
|
});
|
|
787
879
|
}
|
|
788
|
-
|
|
789
|
-
|
|
880
|
+
|
|
881
|
+
public async updatePerpMarketContractTier(
|
|
882
|
+
perpMarketIndex: number,
|
|
883
|
+
contractTier: ContractTier
|
|
790
884
|
): Promise<TransactionSignature> {
|
|
791
|
-
return await this.program.rpc.
|
|
885
|
+
return await this.program.rpc.updatePerpMarketContractTier(contractTier, {
|
|
792
886
|
accounts: {
|
|
793
887
|
admin: this.wallet.publicKey,
|
|
794
888
|
state: await this.getStatePublicKey(),
|
|
889
|
+
market: await getMarketPublicKey(
|
|
890
|
+
this.program.programId,
|
|
891
|
+
perpMarketIndex
|
|
892
|
+
),
|
|
795
893
|
},
|
|
796
894
|
});
|
|
797
895
|
}
|
|
798
896
|
|
|
799
|
-
public async
|
|
800
|
-
|
|
897
|
+
public async updateExchangeStatus(
|
|
898
|
+
exchangeStatus: ExchangeStatus
|
|
899
|
+
): Promise<TransactionSignature> {
|
|
900
|
+
return await this.program.rpc.updateExchangeStatus(exchangeStatus, {
|
|
801
901
|
accounts: {
|
|
802
902
|
admin: this.wallet.publicKey,
|
|
803
903
|
state: await this.getStatePublicKey(),
|