@drift-labs/sdk 0.1.18-orders.1 → 0.1.18-orders.5
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/README.md +2 -1
- package/lib/accounts/bulkAccountLoader.d.ts +31 -0
- package/lib/accounts/bulkAccountLoader.js +175 -0
- package/lib/accounts/bulkUserSubscription.d.ts +7 -0
- package/lib/accounts/bulkUserSubscription.js +28 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +49 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +228 -0
- package/lib/accounts/pollingTokenAccountSubscriber.d.ts +25 -0
- package/lib/accounts/pollingTokenAccountSubscriber.js +79 -0
- package/lib/accounts/pollingUserAccountSubscriber.d.ts +32 -0
- package/lib/accounts/pollingUserAccountSubscriber.js +136 -0
- package/lib/accounts/types.d.ts +34 -25
- package/lib/accounts/utils.d.ts +1 -0
- package/lib/accounts/utils.js +7 -0
- package/lib/accounts/webSocketAccountSubscriber.d.ts +6 -2
- package/lib/accounts/webSocketAccountSubscriber.js +43 -12
- package/lib/accounts/{defaultClearingHouseAccountSubscriber.d.ts → webSocketClearingHouseAccountSubscriber.d.ts} +6 -4
- package/lib/accounts/{defaultClearingHouseAccountSubscriber.js → webSocketClearingHouseAccountSubscriber.js} +5 -4
- package/lib/accounts/{defaultUserAccountSubscriber.d.ts → webSocketUserAccountSubscriber.d.ts} +3 -1
- package/lib/accounts/{defaultUserAccountSubscriber.js → webSocketUserAccountSubscriber.js} +10 -7
- package/lib/addresses.d.ts +2 -2
- package/lib/addresses.js +4 -4
- package/lib/admin.d.ts +3 -0
- package/lib/admin.js +61 -13
- package/lib/clearingHouse.d.ts +20 -4
- package/lib/clearingHouse.js +90 -10
- package/lib/clearingHouseUser.d.ts +12 -4
- package/lib/clearingHouseUser.js +35 -25
- package/lib/config.js +1 -1
- package/lib/constants/markets.d.ts +2 -1
- package/lib/constants/markets.js +35 -14
- package/lib/constants/numericConstants.d.ts +3 -1
- package/lib/constants/numericConstants.js +15 -17
- package/lib/examples/makeTradeExample.js +1 -1
- package/lib/factory/clearingHouse.d.ts +25 -0
- package/lib/factory/clearingHouse.js +64 -0
- package/lib/factory/clearingHouseUser.d.ts +19 -0
- package/lib/factory/clearingHouseUser.js +34 -0
- package/lib/idl/clearing_house.json +366 -17
- package/lib/index.d.ts +8 -3
- package/lib/index.js +9 -3
- package/lib/math/conversion.d.ts +1 -1
- package/lib/math/funding.js +4 -1
- package/lib/math/insuranceFund.d.ts +2 -1
- package/lib/math/insuranceFund.js +3 -6
- package/lib/math/position.d.ts +2 -1
- package/lib/math/position.js +2 -5
- package/lib/math/utils.d.ts +2 -1
- package/lib/math/utils.js +3 -3
- package/lib/mockUSDCFaucet.d.ts +2 -1
- package/lib/orderParams.d.ts +3 -3
- package/lib/orderParams.js +28 -8
- package/lib/orders.d.ts +3 -2
- package/lib/orders.js +8 -8
- package/lib/token/index.d.ts +3 -0
- package/lib/token/index.js +38 -0
- package/lib/types.d.ts +20 -8
- package/lib/types.js +2 -2
- package/package.json +11 -3
- package/src/accounts/bulkAccountLoader.ts +215 -0
- package/src/accounts/bulkUserSubscription.ts +28 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +326 -0
- package/src/accounts/pollingTokenAccountSubscriber.ts +99 -0
- package/src/accounts/pollingUserAccountSubscriber.ts +194 -0
- package/src/accounts/types.ts +40 -30
- package/src/accounts/utils.ts +3 -0
- package/src/accounts/webSocketAccountSubscriber.ts +67 -17
- package/src/accounts/{defaultClearingHouseAccountSubscriber.ts → webSocketClearingHouseAccountSubscriber.ts} +9 -6
- package/src/accounts/{defaultUserAccountSubscriber.ts → webSocketUserAccountSubscriber.ts} +10 -5
- package/src/addresses.ts +6 -4
- package/src/admin.ts +73 -20
- package/src/clearingHouse.ts +125 -25
- package/src/clearingHouseUser.ts +28 -8
- package/src/config.ts +1 -1
- package/src/constants/markets.ts +25 -1
- package/src/constants/numericConstants.ts +2 -1
- package/src/examples/makeTradeExample.ts +4 -1
- package/src/factory/clearingHouse.ts +125 -0
- package/src/factory/clearingHouseUser.ts +73 -0
- package/src/idl/clearing_house.json +366 -17
- package/src/index.ts +8 -3
- package/src/math/conversion.ts +1 -1
- package/src/math/funding.ts +5 -1
- package/src/math/insuranceFund.ts +1 -1
- package/src/math/position.ts +1 -1
- package/src/math/utils.ts +1 -1
- package/src/mockUSDCFaucet.ts +1 -1
- package/src/orderParams.ts +30 -7
- package/src/orders.ts +13 -7
- package/src/token/index.ts +37 -0
- package/src/types.ts +17 -6
- package/lib/accounts/defaultHistoryAccountSubscriber.d.ts +0 -28
- package/lib/accounts/defaultHistoryAccountSubscriber.js +0 -110
- package/src/accounts/defaultHistoryAccountSubscriber.ts +0 -176
package/lib/index.d.ts
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
1
1
|
import { BN } from '@project-serum/anchor';
|
|
2
|
+
import { PublicKey } from '@solana/web3.js';
|
|
2
3
|
export * from './mockUSDCFaucet';
|
|
3
4
|
export * from './pythClient';
|
|
4
5
|
export * from './types';
|
|
5
6
|
export * from './constants/markets';
|
|
6
|
-
export * from './accounts/
|
|
7
|
-
export * from './accounts/
|
|
7
|
+
export * from './accounts/webSocketClearingHouseAccountSubscriber';
|
|
8
|
+
export * from './accounts/bulkAccountLoader';
|
|
9
|
+
export * from './accounts/pollingClearingHouseAccountSubscriber';
|
|
10
|
+
export * from './accounts/pollingTokenAccountSubscriber';
|
|
8
11
|
export * from './accounts/types';
|
|
9
12
|
export * from './addresses';
|
|
10
13
|
export * from './admin';
|
|
11
14
|
export * from './clearingHouseUser';
|
|
12
15
|
export * from './clearingHouse';
|
|
16
|
+
export * from './factory/clearingHouse';
|
|
17
|
+
export * from './factory/clearingHouseUser';
|
|
13
18
|
export * from './math/conversion';
|
|
14
19
|
export * from './math/funding';
|
|
15
20
|
export * from './math/insuranceFund';
|
|
@@ -27,4 +32,4 @@ export * from './config';
|
|
|
27
32
|
export * from './constants/numericConstants';
|
|
28
33
|
export * from './util/computeUnits';
|
|
29
34
|
export * from './util/tps';
|
|
30
|
-
export { BN };
|
|
35
|
+
export { BN, PublicKey };
|
package/lib/index.js
CHANGED
|
@@ -10,20 +10,26 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
10
10
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
11
|
};
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
-
exports.BN = void 0;
|
|
13
|
+
exports.PublicKey = exports.BN = void 0;
|
|
14
14
|
const anchor_1 = require("@project-serum/anchor");
|
|
15
15
|
Object.defineProperty(exports, "BN", { enumerable: true, get: function () { return anchor_1.BN; } });
|
|
16
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
17
|
+
Object.defineProperty(exports, "PublicKey", { enumerable: true, get: function () { return web3_js_1.PublicKey; } });
|
|
16
18
|
__exportStar(require("./mockUSDCFaucet"), exports);
|
|
17
19
|
__exportStar(require("./pythClient"), exports);
|
|
18
20
|
__exportStar(require("./types"), exports);
|
|
19
21
|
__exportStar(require("./constants/markets"), exports);
|
|
20
|
-
__exportStar(require("./accounts/
|
|
21
|
-
__exportStar(require("./accounts/
|
|
22
|
+
__exportStar(require("./accounts/webSocketClearingHouseAccountSubscriber"), exports);
|
|
23
|
+
__exportStar(require("./accounts/bulkAccountLoader"), exports);
|
|
24
|
+
__exportStar(require("./accounts/pollingClearingHouseAccountSubscriber"), exports);
|
|
25
|
+
__exportStar(require("./accounts/pollingTokenAccountSubscriber"), exports);
|
|
22
26
|
__exportStar(require("./accounts/types"), exports);
|
|
23
27
|
__exportStar(require("./addresses"), exports);
|
|
24
28
|
__exportStar(require("./admin"), exports);
|
|
25
29
|
__exportStar(require("./clearingHouseUser"), exports);
|
|
26
30
|
__exportStar(require("./clearingHouse"), exports);
|
|
31
|
+
__exportStar(require("./factory/clearingHouse"), exports);
|
|
32
|
+
__exportStar(require("./factory/clearingHouseUser"), exports);
|
|
27
33
|
__exportStar(require("./math/conversion"), exports);
|
|
28
34
|
__exportStar(require("./math/funding"), exports);
|
|
29
35
|
__exportStar(require("./math/insuranceFund"), exports);
|
package/lib/math/conversion.d.ts
CHANGED
package/lib/math/funding.js
CHANGED
|
@@ -242,7 +242,10 @@ exports.calculateLongShortFundingRateAndLiveTwaps = calculateLongShortFundingRat
|
|
|
242
242
|
function calculateFundingPool(market) {
|
|
243
243
|
// todo
|
|
244
244
|
const totalFeeLB = market.amm.totalFee.div(new anchor_1.BN(2));
|
|
245
|
-
const feePool = anchor_1.BN.max(numericConstants_1.ZERO, market.amm.totalFeeMinusDistributions
|
|
245
|
+
const feePool = anchor_1.BN.max(numericConstants_1.ZERO, market.amm.totalFeeMinusDistributions
|
|
246
|
+
.sub(totalFeeLB)
|
|
247
|
+
.mul(new anchor_1.BN(2))
|
|
248
|
+
.div(new anchor_1.BN(3)));
|
|
246
249
|
return feePool;
|
|
247
250
|
}
|
|
248
251
|
exports.calculateFundingPool = calculateFundingPool;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
/// <reference types="bn.js" />
|
|
1
2
|
import { MarketsAccount, StateAccount } from '../types';
|
|
2
|
-
import BN from '
|
|
3
|
+
import { BN } from '../';
|
|
3
4
|
import { Connection } from '@solana/web3.js';
|
|
4
5
|
/**
|
|
5
6
|
* In the case of a levered loss, the exchange first pays out undistributed fees and then the insurance fund.
|
|
@@ -8,12 +8,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
12
|
exports.calculateInsuranceFundSize = void 0;
|
|
16
|
-
const
|
|
13
|
+
const __1 = require("../");
|
|
17
14
|
/**
|
|
18
15
|
* In the case of a levered loss, the exchange first pays out undistributed fees and then the insurance fund.
|
|
19
16
|
* Thus the de facto size of the insurance fund is the amount in the insurance vault plus the sum of each markets
|
|
@@ -27,9 +24,9 @@ const bn_js_1 = __importDefault(require("bn.js"));
|
|
|
27
24
|
function calculateInsuranceFundSize(connection, state, marketsAccount) {
|
|
28
25
|
return __awaiter(this, void 0, void 0, function* () {
|
|
29
26
|
const insuranceVaultPublicKey = state.insuranceVault;
|
|
30
|
-
const insuranceVaultAmount = new
|
|
27
|
+
const insuranceVaultAmount = new __1.BN((yield connection.getTokenAccountBalance(insuranceVaultPublicKey)).value.amount);
|
|
31
28
|
return marketsAccount.markets.reduce((insuranceVaultAmount, market) => {
|
|
32
|
-
return insuranceVaultAmount.add(market.amm.totalFee.div(new
|
|
29
|
+
return insuranceVaultAmount.add(market.amm.totalFee.div(new __1.BN(2)));
|
|
33
30
|
}, insuranceVaultAmount);
|
|
34
31
|
});
|
|
35
32
|
}
|
package/lib/math/position.d.ts
CHANGED
package/lib/math/position.js
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.isEmptyPosition = exports.positionCurrentDirection = exports.findDirectionToClose = exports.calculateEntryPrice = exports.calculatePositionFundingPNL = exports.calculatePositionPNL = exports.calculateBaseAssetValue = void 0;
|
|
7
|
-
const
|
|
4
|
+
const __1 = require("../");
|
|
8
5
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
9
6
|
const types_1 = require("../types");
|
|
10
7
|
const amm_1 = require("./amm");
|
|
@@ -84,7 +81,7 @@ function calculatePositionFundingPNL(market, marketPosition) {
|
|
|
84
81
|
.mul(marketPosition.baseAssetAmount)
|
|
85
82
|
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
86
83
|
.div(numericConstants_1.FUNDING_PAYMENT_PRECISION)
|
|
87
|
-
.mul(new
|
|
84
|
+
.mul(new __1.BN(-1));
|
|
88
85
|
return perPositionFundingRate;
|
|
89
86
|
}
|
|
90
87
|
exports.calculatePositionFundingPNL = calculatePositionFundingPNL;
|
package/lib/math/utils.d.ts
CHANGED
package/lib/math/utils.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.squareRootBN = void 0;
|
|
4
|
-
const
|
|
5
|
-
const squareRootBN = (n, closeness = new
|
|
4
|
+
const __1 = require("../");
|
|
5
|
+
const squareRootBN = (n, closeness = new __1.BN(1)) => {
|
|
6
6
|
// Assuming the sqrt of n as n only
|
|
7
7
|
let x = n;
|
|
8
8
|
// The closed guess will be stored in the root
|
|
9
9
|
let root;
|
|
10
10
|
// To count the number of iterations
|
|
11
11
|
let count = 0;
|
|
12
|
-
const TWO = new
|
|
12
|
+
const TWO = new __1.BN(2);
|
|
13
13
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
14
14
|
while (count < Number.MAX_SAFE_INTEGER) {
|
|
15
15
|
count++;
|
package/lib/mockUSDCFaucet.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
/// <reference types="bn.js" />
|
|
1
2
|
import * as anchor from '@project-serum/anchor';
|
|
2
3
|
import { Program, Provider } from '@project-serum/anchor';
|
|
3
4
|
import { AccountInfo } from '@solana/spl-token';
|
|
4
5
|
import { ConfirmOptions, Connection, PublicKey, TransactionInstruction, TransactionSignature } from '@solana/web3.js';
|
|
5
|
-
import BN from '
|
|
6
|
+
import { BN } from '.';
|
|
6
7
|
import { IWallet } from './types';
|
|
7
8
|
export declare class MockUSDCFaucet {
|
|
8
9
|
connection: Connection;
|
package/lib/orderParams.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
import { OrderParams, OrderTriggerCondition, PositionDirection } from './types';
|
|
3
3
|
import { BN } from '@project-serum/anchor';
|
|
4
|
-
export declare function getLimitOrderParams(marketIndex: BN, direction: PositionDirection, baseAssetAmount: BN, price: BN, reduceOnly: boolean, discountToken?: boolean, referrer?: boolean): OrderParams;
|
|
5
|
-
export declare function
|
|
6
|
-
export declare function
|
|
4
|
+
export declare function getLimitOrderParams(marketIndex: BN, direction: PositionDirection, baseAssetAmount: BN, price: BN, reduceOnly: boolean, discountToken?: boolean, referrer?: boolean, userOrderId?: number): OrderParams;
|
|
5
|
+
export declare function getTriggerMarketOrderParams(marketIndex: BN, direction: PositionDirection, baseAssetAmount: BN, triggerPrice: BN, triggerCondition: OrderTriggerCondition, reduceOnly: boolean, discountToken?: boolean, referrer?: boolean, userOrderId?: number): OrderParams;
|
|
6
|
+
export declare function getTriggerLimitOrderParams(marketIndex: BN, direction: PositionDirection, baseAssetAmount: BN, price: BN, triggerPrice: BN, triggerCondition: OrderTriggerCondition, reduceOnly: boolean, discountToken?: boolean, referrer?: boolean, userOrderId?: number): OrderParams;
|
|
7
7
|
export declare function getMarketOrderParams(marketIndex: BN, direction: PositionDirection, quoteAssetAmount: BN, baseAssetAmount: BN, reduceOnly: boolean, price?: BN, discountToken?: boolean, referrer?: boolean): OrderParams;
|
package/lib/orderParams.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getMarketOrderParams = exports.
|
|
3
|
+
exports.getMarketOrderParams = exports.getTriggerLimitOrderParams = exports.getTriggerMarketOrderParams = exports.getLimitOrderParams = void 0;
|
|
4
4
|
const types_1 = require("./types");
|
|
5
5
|
const numericConstants_1 = require("./constants/numericConstants");
|
|
6
|
-
function getLimitOrderParams(marketIndex, direction, baseAssetAmount, price, reduceOnly, discountToken = false, referrer = false) {
|
|
6
|
+
function getLimitOrderParams(marketIndex, direction, baseAssetAmount, price, reduceOnly, discountToken = false, referrer = false, userOrderId = 0) {
|
|
7
7
|
return {
|
|
8
8
|
orderType: types_1.OrderType.LIMIT,
|
|
9
|
+
userOrderId,
|
|
9
10
|
marketIndex,
|
|
10
11
|
direction,
|
|
11
12
|
quoteAssetAmount: numericConstants_1.ZERO,
|
|
@@ -14,18 +15,23 @@ function getLimitOrderParams(marketIndex, direction, baseAssetAmount, price, red
|
|
|
14
15
|
reduceOnly,
|
|
15
16
|
postOnly: false,
|
|
16
17
|
immediateOrCancel: false,
|
|
18
|
+
positionLimit: numericConstants_1.ZERO,
|
|
19
|
+
padding0: true,
|
|
20
|
+
padding1: numericConstants_1.ZERO,
|
|
17
21
|
optionalAccounts: {
|
|
18
22
|
discountToken,
|
|
19
23
|
referrer,
|
|
20
24
|
},
|
|
21
25
|
triggerCondition: types_1.OrderTriggerCondition.ABOVE,
|
|
22
26
|
triggerPrice: numericConstants_1.ZERO,
|
|
27
|
+
oraclePriceOffset: numericConstants_1.ZERO,
|
|
23
28
|
};
|
|
24
29
|
}
|
|
25
30
|
exports.getLimitOrderParams = getLimitOrderParams;
|
|
26
|
-
function
|
|
31
|
+
function getTriggerMarketOrderParams(marketIndex, direction, baseAssetAmount, triggerPrice, triggerCondition, reduceOnly, discountToken = false, referrer = false, userOrderId = 0) {
|
|
27
32
|
return {
|
|
28
|
-
orderType: types_1.OrderType.
|
|
33
|
+
orderType: types_1.OrderType.TRIGGER_MARKET,
|
|
34
|
+
userOrderId,
|
|
29
35
|
marketIndex,
|
|
30
36
|
direction,
|
|
31
37
|
quoteAssetAmount: numericConstants_1.ZERO,
|
|
@@ -34,18 +40,23 @@ function getStopOrderParams(marketIndex, direction, baseAssetAmount, triggerPric
|
|
|
34
40
|
reduceOnly,
|
|
35
41
|
postOnly: false,
|
|
36
42
|
immediateOrCancel: false,
|
|
43
|
+
positionLimit: numericConstants_1.ZERO,
|
|
44
|
+
padding0: true,
|
|
45
|
+
padding1: numericConstants_1.ZERO,
|
|
37
46
|
optionalAccounts: {
|
|
38
47
|
discountToken,
|
|
39
48
|
referrer,
|
|
40
49
|
},
|
|
41
50
|
triggerCondition,
|
|
42
51
|
triggerPrice,
|
|
52
|
+
oraclePriceOffset: numericConstants_1.ZERO,
|
|
43
53
|
};
|
|
44
54
|
}
|
|
45
|
-
exports.
|
|
46
|
-
function
|
|
55
|
+
exports.getTriggerMarketOrderParams = getTriggerMarketOrderParams;
|
|
56
|
+
function getTriggerLimitOrderParams(marketIndex, direction, baseAssetAmount, price, triggerPrice, triggerCondition, reduceOnly, discountToken = false, referrer = false, userOrderId = 0) {
|
|
47
57
|
return {
|
|
48
|
-
orderType: types_1.OrderType.
|
|
58
|
+
orderType: types_1.OrderType.TRIGGER_LIMIT,
|
|
59
|
+
userOrderId,
|
|
49
60
|
marketIndex,
|
|
50
61
|
direction,
|
|
51
62
|
quoteAssetAmount: numericConstants_1.ZERO,
|
|
@@ -54,21 +65,26 @@ function getStopLimitOrderParams(marketIndex, direction, baseAssetAmount, price,
|
|
|
54
65
|
reduceOnly,
|
|
55
66
|
postOnly: false,
|
|
56
67
|
immediateOrCancel: false,
|
|
68
|
+
positionLimit: numericConstants_1.ZERO,
|
|
69
|
+
padding0: true,
|
|
70
|
+
padding1: numericConstants_1.ZERO,
|
|
57
71
|
optionalAccounts: {
|
|
58
72
|
discountToken,
|
|
59
73
|
referrer,
|
|
60
74
|
},
|
|
61
75
|
triggerCondition,
|
|
62
76
|
triggerPrice,
|
|
77
|
+
oraclePriceOffset: numericConstants_1.ZERO,
|
|
63
78
|
};
|
|
64
79
|
}
|
|
65
|
-
exports.
|
|
80
|
+
exports.getTriggerLimitOrderParams = getTriggerLimitOrderParams;
|
|
66
81
|
function getMarketOrderParams(marketIndex, direction, quoteAssetAmount, baseAssetAmount, reduceOnly, price = numericConstants_1.ZERO, discountToken = false, referrer = false) {
|
|
67
82
|
if (baseAssetAmount.eq(numericConstants_1.ZERO) && quoteAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
68
83
|
throw Error('baseAssetAmount or quoteAssetAmount must be zero');
|
|
69
84
|
}
|
|
70
85
|
return {
|
|
71
86
|
orderType: types_1.OrderType.MARKET,
|
|
87
|
+
userOrderId: 0,
|
|
72
88
|
marketIndex,
|
|
73
89
|
direction,
|
|
74
90
|
quoteAssetAmount,
|
|
@@ -77,12 +93,16 @@ function getMarketOrderParams(marketIndex, direction, quoteAssetAmount, baseAsse
|
|
|
77
93
|
reduceOnly,
|
|
78
94
|
postOnly: false,
|
|
79
95
|
immediateOrCancel: false,
|
|
96
|
+
positionLimit: numericConstants_1.ZERO,
|
|
97
|
+
padding0: true,
|
|
98
|
+
padding1: numericConstants_1.ZERO,
|
|
80
99
|
optionalAccounts: {
|
|
81
100
|
discountToken,
|
|
82
101
|
referrer,
|
|
83
102
|
},
|
|
84
103
|
triggerCondition: types_1.OrderTriggerCondition.ABOVE,
|
|
85
104
|
triggerPrice: numericConstants_1.ZERO,
|
|
105
|
+
oraclePriceOffset: numericConstants_1.ZERO,
|
|
86
106
|
};
|
|
87
107
|
}
|
|
88
108
|
exports.getMarketOrderParams = getMarketOrderParams;
|
package/lib/orders.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
/// <reference types="bn.js" />
|
|
1
2
|
import { Market, Order, UserAccount, UserPosition } from './types';
|
|
2
|
-
import BN from '
|
|
3
|
+
import { BN } from '.';
|
|
3
4
|
export declare function calculateNewStateAfterOrder(userAccount: UserAccount, userPosition: UserPosition, market: Market, order: Order): [UserAccount, UserPosition, Market] | null;
|
|
4
5
|
export declare function calculateAmountToTradeForLimit(market: Market, order: Order): BN;
|
|
5
|
-
export declare function
|
|
6
|
+
export declare function calculateAmountToTradeForTriggerLimit(market: Market, order: Order): BN;
|
package/lib/orders.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.calculateAmountToTradeForTriggerLimit = exports.calculateAmountToTradeForLimit = exports.calculateNewStateAfterOrder = void 0;
|
|
4
4
|
const types_1 = require("./types");
|
|
5
5
|
const market_1 = require("./math/market");
|
|
6
6
|
const numericConstants_1 = require("./constants/numericConstants");
|
|
@@ -83,15 +83,15 @@ function calculateAmountToTrade(market, order) {
|
|
|
83
83
|
if (types_1.isVariant(order.orderType, 'limit')) {
|
|
84
84
|
return calculateAmountToTradeForLimit(market, order);
|
|
85
85
|
}
|
|
86
|
-
else if (types_1.isVariant(order.orderType, '
|
|
87
|
-
return
|
|
86
|
+
else if (types_1.isVariant(order.orderType, 'triggerLimit')) {
|
|
87
|
+
return calculateAmountToTradeForTriggerLimit(market, order);
|
|
88
88
|
}
|
|
89
89
|
else if (types_1.isVariant(order.orderType, 'market')) {
|
|
90
90
|
// should never be a market order queued
|
|
91
91
|
return numericConstants_1.ZERO;
|
|
92
92
|
}
|
|
93
93
|
else {
|
|
94
|
-
return
|
|
94
|
+
return calculateAmountToTradeForTriggerMarket(market, order);
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
97
|
function calculateAmountToTradeForLimit(market, order) {
|
|
@@ -106,21 +106,21 @@ function calculateAmountToTradeForLimit(market, order) {
|
|
|
106
106
|
: maxAmountToTrade;
|
|
107
107
|
}
|
|
108
108
|
exports.calculateAmountToTradeForLimit = calculateAmountToTradeForLimit;
|
|
109
|
-
function
|
|
109
|
+
function calculateAmountToTradeForTriggerLimit(market, order) {
|
|
110
110
|
if (order.baseAssetAmountFilled.eq(numericConstants_1.ZERO)) {
|
|
111
|
-
const baseAssetAmount =
|
|
111
|
+
const baseAssetAmount = calculateAmountToTradeForTriggerMarket(market, order);
|
|
112
112
|
if (baseAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
113
113
|
return numericConstants_1.ZERO;
|
|
114
114
|
}
|
|
115
115
|
}
|
|
116
116
|
return calculateAmountToTradeForLimit(market, order);
|
|
117
117
|
}
|
|
118
|
-
exports.
|
|
118
|
+
exports.calculateAmountToTradeForTriggerLimit = calculateAmountToTradeForTriggerLimit;
|
|
119
119
|
function isSameDirection(firstDirection, secondDirection) {
|
|
120
120
|
return ((types_1.isVariant(firstDirection, 'long') && types_1.isVariant(secondDirection, 'long')) ||
|
|
121
121
|
(types_1.isVariant(firstDirection, 'short') && types_1.isVariant(secondDirection, 'short')));
|
|
122
122
|
}
|
|
123
|
-
function
|
|
123
|
+
function calculateAmountToTradeForTriggerMarket(market, order) {
|
|
124
124
|
return isTriggerConditionSatisfied(market, order)
|
|
125
125
|
? order.baseAssetAmount
|
|
126
126
|
: numericConstants_1.ZERO;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseTokenAccount = void 0;
|
|
4
|
+
const spl_token_1 = require("@solana/spl-token");
|
|
5
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
6
|
+
function parseTokenAccount(data) {
|
|
7
|
+
const accountInfo = spl_token_1.AccountLayout.decode(data);
|
|
8
|
+
accountInfo.mint = new web3_js_1.PublicKey(accountInfo.mint);
|
|
9
|
+
accountInfo.owner = new web3_js_1.PublicKey(accountInfo.owner);
|
|
10
|
+
accountInfo.amount = spl_token_1.u64.fromBuffer(accountInfo.amount);
|
|
11
|
+
if (accountInfo.delegateOption === 0) {
|
|
12
|
+
accountInfo.delegate = null;
|
|
13
|
+
// eslint-disable-next-line new-cap
|
|
14
|
+
accountInfo.delegatedAmount = new spl_token_1.u64(0);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
accountInfo.delegate = new web3_js_1.PublicKey(accountInfo.delegate);
|
|
18
|
+
accountInfo.delegatedAmount = spl_token_1.u64.fromBuffer(accountInfo.delegatedAmount);
|
|
19
|
+
}
|
|
20
|
+
accountInfo.isInitialized = accountInfo.state !== 0;
|
|
21
|
+
accountInfo.isFrozen = accountInfo.state === 2;
|
|
22
|
+
if (accountInfo.isNativeOption === 1) {
|
|
23
|
+
accountInfo.rentExemptReserve = spl_token_1.u64.fromBuffer(accountInfo.isNative);
|
|
24
|
+
accountInfo.isNative = true;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
accountInfo.rentExemptReserve = null;
|
|
28
|
+
accountInfo.isNative = false;
|
|
29
|
+
}
|
|
30
|
+
if (accountInfo.closeAuthorityOption === 0) {
|
|
31
|
+
accountInfo.closeAuthority = null;
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
accountInfo.closeAuthority = new web3_js_1.PublicKey(accountInfo.closeAuthority);
|
|
35
|
+
}
|
|
36
|
+
return accountInfo;
|
|
37
|
+
}
|
|
38
|
+
exports.parseTokenAccount = parseTokenAccount;
|
package/lib/types.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
/// <reference types="bn.js" />
|
|
1
2
|
import { PublicKey, Transaction } from '@solana/web3.js';
|
|
2
|
-
import BN from '
|
|
3
|
+
import { BN } from '.';
|
|
3
4
|
export declare class SwapDirection {
|
|
4
5
|
static readonly ADD: {
|
|
5
6
|
add: {};
|
|
@@ -28,11 +29,11 @@ export declare class OrderType {
|
|
|
28
29
|
static readonly LIMIT: {
|
|
29
30
|
limit: {};
|
|
30
31
|
};
|
|
31
|
-
static readonly
|
|
32
|
-
|
|
32
|
+
static readonly TRIGGER_MARKET: {
|
|
33
|
+
triggerMarket: {};
|
|
33
34
|
};
|
|
34
|
-
static readonly
|
|
35
|
-
|
|
35
|
+
static readonly TRIGGER_LIMIT: {
|
|
36
|
+
triggerLimit: {};
|
|
36
37
|
};
|
|
37
38
|
static readonly MARKET: {
|
|
38
39
|
market: {};
|
|
@@ -97,9 +98,9 @@ export declare type DepositHistoryAccount = {
|
|
|
97
98
|
head: BN;
|
|
98
99
|
depositRecords: DepositRecord[];
|
|
99
100
|
};
|
|
100
|
-
export declare type
|
|
101
|
+
export declare type ExtendedCurveHistoryAccount = {
|
|
101
102
|
head: BN;
|
|
102
|
-
curveRecords:
|
|
103
|
+
curveRecords: ExtendedCurveRecord[];
|
|
103
104
|
};
|
|
104
105
|
export declare type FundingRateHistoryAccount = {
|
|
105
106
|
head: BN;
|
|
@@ -131,7 +132,7 @@ export declare type DepositRecord = {
|
|
|
131
132
|
cumulativeDepositsBefore: BN;
|
|
132
133
|
amount: BN;
|
|
133
134
|
};
|
|
134
|
-
export declare type
|
|
135
|
+
export declare type ExtendedCurveRecord = {
|
|
135
136
|
ts: BN;
|
|
136
137
|
recordId: BN;
|
|
137
138
|
marketIndex: BN;
|
|
@@ -147,6 +148,8 @@ export declare type CurveRecord = {
|
|
|
147
148
|
baseAssetAmountShort: BN;
|
|
148
149
|
baseAssetAmount: BN;
|
|
149
150
|
openInterest: BN;
|
|
151
|
+
oraclePrice: BN;
|
|
152
|
+
tradeId: BN;
|
|
150
153
|
};
|
|
151
154
|
export declare type TradeRecord = {
|
|
152
155
|
ts: BN;
|
|
@@ -261,6 +264,7 @@ export declare type StateAccount = {
|
|
|
261
264
|
oracleGuardRails: OracleGuardRails;
|
|
262
265
|
maxDeposit: BN;
|
|
263
266
|
orderState: PublicKey;
|
|
267
|
+
extendedCurveHistory: PublicKey;
|
|
264
268
|
};
|
|
265
269
|
export declare type OrderStateAccount = {
|
|
266
270
|
orderHistory: PublicKey;
|
|
@@ -303,6 +307,7 @@ export declare type AMM = {
|
|
|
303
307
|
totalFee: BN;
|
|
304
308
|
minimumQuoteAssetTradeSize: BN;
|
|
305
309
|
minimumBaseAssetTradeSize: BN;
|
|
310
|
+
lastOraclePrice: BN;
|
|
306
311
|
};
|
|
307
312
|
export declare type UserPosition = {
|
|
308
313
|
baseAssetAmount: BN;
|
|
@@ -334,8 +339,10 @@ export declare type Order = {
|
|
|
334
339
|
orderType: OrderType;
|
|
335
340
|
ts: BN;
|
|
336
341
|
orderId: BN;
|
|
342
|
+
userOrderId: number;
|
|
337
343
|
marketIndex: BN;
|
|
338
344
|
price: BN;
|
|
345
|
+
userBaseAssetAmount: BN;
|
|
339
346
|
baseAssetAmount: BN;
|
|
340
347
|
baseAssetAmountFilled: BN;
|
|
341
348
|
quoteAssetAmount: BN;
|
|
@@ -352,6 +359,7 @@ export declare type Order = {
|
|
|
352
359
|
};
|
|
353
360
|
export declare type OrderParams = {
|
|
354
361
|
orderType: OrderType;
|
|
362
|
+
userOrderId: number;
|
|
355
363
|
direction: PositionDirection;
|
|
356
364
|
quoteAssetAmount: BN;
|
|
357
365
|
baseAssetAmount: BN;
|
|
@@ -362,6 +370,10 @@ export declare type OrderParams = {
|
|
|
362
370
|
immediateOrCancel: boolean;
|
|
363
371
|
triggerPrice: BN;
|
|
364
372
|
triggerCondition: OrderTriggerCondition;
|
|
373
|
+
positionLimit: BN;
|
|
374
|
+
oraclePriceOffset: BN;
|
|
375
|
+
padding0: boolean;
|
|
376
|
+
padding1: BN;
|
|
365
377
|
optionalAccounts: {
|
|
366
378
|
discountToken: boolean;
|
|
367
379
|
referrer: boolean;
|
package/lib/types.js
CHANGED
|
@@ -21,8 +21,8 @@ class OrderType {
|
|
|
21
21
|
}
|
|
22
22
|
exports.OrderType = OrderType;
|
|
23
23
|
OrderType.LIMIT = { limit: {} };
|
|
24
|
-
OrderType.
|
|
25
|
-
OrderType.
|
|
24
|
+
OrderType.TRIGGER_MARKET = { triggerMarket: {} };
|
|
25
|
+
OrderType.TRIGGER_LIMIT = { triggerLimit: {} };
|
|
26
26
|
OrderType.MARKET = { market: {} };
|
|
27
27
|
class OrderStatus {
|
|
28
28
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@drift-labs/sdk",
|
|
3
|
-
"version": "0.1.18-orders.
|
|
3
|
+
"version": "0.1.18-orders.5",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"types": "lib/index.d.ts",
|
|
6
6
|
"author": "crispheaney",
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
"url": "git@github.com:drift-labs/protocol-v1.git"
|
|
11
11
|
},
|
|
12
12
|
"scripts": {
|
|
13
|
+
"lint": "eslint './**/*.{ts,tsx}' --quiet",
|
|
13
14
|
"build": "yarn clean && tsc",
|
|
14
15
|
"clean": "rm -rf lib",
|
|
15
16
|
"patch-and-pub": "npm version patch --force && npm publish"
|
|
@@ -31,8 +32,15 @@
|
|
|
31
32
|
"@pythnetwork/client": "^2.5.1",
|
|
32
33
|
"@solana/spl-token": "^0.1.6",
|
|
33
34
|
"@solana/web3.js": "^1.22.0",
|
|
34
|
-
"
|
|
35
|
-
"
|
|
35
|
+
"strict-event-emitter-types": "^2.0.0",
|
|
36
|
+
"uuid": "^8.3.2"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@typescript-eslint/eslint-plugin": "^4.28.0",
|
|
40
|
+
"@typescript-eslint/parser": "^4.28.0",
|
|
41
|
+
"eslint": "^7.29.0",
|
|
42
|
+
"eslint-config-prettier": "^8.3.0",
|
|
43
|
+
"eslint-plugin-prettier": "^3.4.0"
|
|
36
44
|
},
|
|
37
45
|
"description": "SDK for Drift Protocol v1",
|
|
38
46
|
"engines": {
|