@drift-labs/sdk 2.31.1-beta.9 → 2.32.0
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/VERSION +1 -1
- package/lib/constants/perpMarkets.js +20 -0
- package/lib/dlob/orderBookLevels.js +2 -2
- package/lib/driftClient.d.ts +51 -4
- package/lib/driftClient.js +195 -194
- package/lib/idl/drift.json +31 -1
- package/lib/index.d.ts +3 -0
- package/lib/index.js +3 -0
- package/lib/marinade/index.d.ts +11 -0
- package/lib/marinade/index.js +36 -0
- package/lib/marinade/types.d.ts +1963 -0
- package/lib/marinade/types.js +1965 -0
- package/lib/math/spotBalance.d.ts +9 -2
- package/lib/math/spotBalance.js +54 -6
- package/lib/math/superStake.d.ts +22 -0
- package/lib/math/superStake.js +108 -0
- package/lib/math/utils.d.ts +1 -0
- package/lib/math/utils.js +5 -1
- package/lib/orderParams.d.ts +18 -5
- package/lib/orderParams.js +17 -1
- package/lib/user.d.ts +45 -1
- package/lib/user.js +227 -9
- package/package.json +1 -1
- package/src/assert/assert.js +9 -0
- package/src/constants/perpMarkets.ts +20 -0
- package/src/dlob/orderBookLevels.ts +3 -2
- package/src/driftClient.ts +373 -223
- package/src/idl/drift.json +31 -1
- package/src/index.ts +3 -0
- package/src/marinade/idl/idl.json +1962 -0
- package/src/marinade/index.ts +64 -0
- package/src/marinade/types.ts +3925 -0
- package/src/math/spotBalance.ts +83 -5
- package/src/math/superStake.ts +148 -0
- package/src/math/utils.ts +4 -0
- package/src/orderParams.ts +35 -5
- package/src/token/index.js +38 -0
- package/src/user.ts +453 -15
- package/src/util/computeUnits.js +27 -0
- package/src/util/promiseTimeout.js +14 -0
- package/src/util/tps.js +27 -0
- package/tests/spot/test.ts +156 -0
|
@@ -49,8 +49,15 @@ export declare function getStrictTokenValue(tokenAmount: BN, spotDecimals: numbe
|
|
|
49
49
|
export declare function getTokenValue(tokenAmount: BN, spotDecimals: number, oraclePriceData: OraclePriceData): BN;
|
|
50
50
|
export declare function calculateAssetWeight(balanceAmount: BN, spotMarket: SpotMarketAccount, marginCategory: MarginCategory): BN;
|
|
51
51
|
export declare function calculateLiabilityWeight(size: BN, spotMarket: SpotMarketAccount, marginCategory: MarginCategory): BN;
|
|
52
|
-
export declare function calculateUtilization(bank: SpotMarketAccount): BN;
|
|
53
|
-
|
|
52
|
+
export declare function calculateUtilization(bank: SpotMarketAccount, delta?: any): BN;
|
|
53
|
+
/**
|
|
54
|
+
* calculates max borrow amount where rate would stay below targetBorrowRate
|
|
55
|
+
* @param spotMarketAccount
|
|
56
|
+
* @param targetBorrowRate
|
|
57
|
+
* @returns : Precision: TOKEN DECIMALS
|
|
58
|
+
*/
|
|
59
|
+
export declare function calculateSpotMarketBorrowCapacity(spotMarketAccount: SpotMarketAccount, targetBorrowRate: BN): BN;
|
|
60
|
+
export declare function calculateInterestRate(bank: SpotMarketAccount, delta?: any): BN;
|
|
54
61
|
export declare function calculateDepositRate(bank: SpotMarketAccount): BN;
|
|
55
62
|
export declare function calculateBorrowRate(bank: SpotMarketAccount): BN;
|
|
56
63
|
export declare function calculateInterestAccumulated(bank: SpotMarketAccount, now: BN): {
|
package/lib/math/spotBalance.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateWithdrawLimit = exports.calculateInterestAccumulated = exports.calculateBorrowRate = exports.calculateDepositRate = exports.calculateInterestRate = exports.calculateUtilization = exports.calculateLiabilityWeight = exports.calculateAssetWeight = exports.getTokenValue = exports.getStrictTokenValue = exports.getSignedTokenAmount = exports.getTokenAmount = exports.getBalance = void 0;
|
|
3
|
+
exports.calculateWithdrawLimit = exports.calculateInterestAccumulated = exports.calculateBorrowRate = exports.calculateDepositRate = exports.calculateInterestRate = exports.calculateSpotMarketBorrowCapacity = exports.calculateUtilization = exports.calculateLiabilityWeight = exports.calculateAssetWeight = exports.getTokenValue = exports.getStrictTokenValue = exports.getSignedTokenAmount = exports.getTokenAmount = exports.getBalance = void 0;
|
|
4
4
|
const types_1 = require("../types");
|
|
5
5
|
const anchor_1 = require("@coral-xyz/anchor");
|
|
6
6
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
@@ -156,9 +156,15 @@ function calculateLiabilityWeight(size, spotMarket, marginCategory) {
|
|
|
156
156
|
return liabilityWeight;
|
|
157
157
|
}
|
|
158
158
|
exports.calculateLiabilityWeight = calculateLiabilityWeight;
|
|
159
|
-
function calculateUtilization(bank) {
|
|
160
|
-
|
|
161
|
-
|
|
159
|
+
function calculateUtilization(bank, delta = numericConstants_1.ZERO) {
|
|
160
|
+
let tokenDepositAmount = getTokenAmount(bank.depositBalance, bank, types_1.SpotBalanceType.DEPOSIT);
|
|
161
|
+
let tokenBorrowAmount = getTokenAmount(bank.borrowBalance, bank, types_1.SpotBalanceType.BORROW);
|
|
162
|
+
if (delta.gt(numericConstants_1.ZERO)) {
|
|
163
|
+
tokenDepositAmount = tokenDepositAmount.add(delta);
|
|
164
|
+
}
|
|
165
|
+
else if (delta.lt(numericConstants_1.ZERO)) {
|
|
166
|
+
tokenBorrowAmount = tokenBorrowAmount.add(delta.abs());
|
|
167
|
+
}
|
|
162
168
|
let utilization;
|
|
163
169
|
if (tokenBorrowAmount.eq(numericConstants_1.ZERO) && tokenDepositAmount.eq(numericConstants_1.ZERO)) {
|
|
164
170
|
utilization = numericConstants_1.ZERO;
|
|
@@ -174,8 +180,50 @@ function calculateUtilization(bank) {
|
|
|
174
180
|
return utilization;
|
|
175
181
|
}
|
|
176
182
|
exports.calculateUtilization = calculateUtilization;
|
|
177
|
-
|
|
178
|
-
|
|
183
|
+
/**
|
|
184
|
+
* calculates max borrow amount where rate would stay below targetBorrowRate
|
|
185
|
+
* @param spotMarketAccount
|
|
186
|
+
* @param targetBorrowRate
|
|
187
|
+
* @returns : Precision: TOKEN DECIMALS
|
|
188
|
+
*/
|
|
189
|
+
function calculateSpotMarketBorrowCapacity(spotMarketAccount, targetBorrowRate) {
|
|
190
|
+
const currentBorrowRate = calculateBorrowRate(spotMarketAccount);
|
|
191
|
+
if (currentBorrowRate.gte(targetBorrowRate)) {
|
|
192
|
+
return numericConstants_1.ZERO;
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
const tokenDepositAmount = getTokenAmount(spotMarketAccount.depositBalance, spotMarketAccount, types_1.SpotBalanceType.DEPOSIT);
|
|
196
|
+
const tokenBorrowAmount = getTokenAmount(spotMarketAccount.borrowBalance, spotMarketAccount, types_1.SpotBalanceType.BORROW);
|
|
197
|
+
let targetUtilization;
|
|
198
|
+
// target utilization past mid point
|
|
199
|
+
if (targetBorrowRate.gte(new anchor_1.BN(spotMarketAccount.optimalBorrowRate))) {
|
|
200
|
+
const borrowRateSlope = new anchor_1.BN(spotMarketAccount.maxBorrowRate - spotMarketAccount.optimalBorrowRate)
|
|
201
|
+
.mul(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION)
|
|
202
|
+
.div(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION.sub(new anchor_1.BN(spotMarketAccount.optimalUtilization)));
|
|
203
|
+
const surplusTargetUtilization = targetBorrowRate
|
|
204
|
+
.sub(new anchor_1.BN(spotMarketAccount.optimalBorrowRate))
|
|
205
|
+
.mul(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION)
|
|
206
|
+
.div(borrowRateSlope);
|
|
207
|
+
targetUtilization = surplusTargetUtilization.add(new anchor_1.BN(spotMarketAccount.optimalUtilization));
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
const borrowRateSlope = new anchor_1.BN(spotMarketAccount.optimalBorrowRate)
|
|
211
|
+
.mul(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION)
|
|
212
|
+
.div(new anchor_1.BN(spotMarketAccount.optimalUtilization));
|
|
213
|
+
targetUtilization = targetBorrowRate
|
|
214
|
+
.mul(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION)
|
|
215
|
+
.div(borrowRateSlope);
|
|
216
|
+
}
|
|
217
|
+
const targetBorrowAmount = tokenDepositAmount
|
|
218
|
+
.mul(targetUtilization)
|
|
219
|
+
.div(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION);
|
|
220
|
+
const capacity = anchor_1.BN.max(numericConstants_1.ZERO, targetBorrowAmount.sub(tokenBorrowAmount));
|
|
221
|
+
return capacity;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
exports.calculateSpotMarketBorrowCapacity = calculateSpotMarketBorrowCapacity;
|
|
225
|
+
function calculateInterestRate(bank, delta = numericConstants_1.ZERO) {
|
|
226
|
+
const utilization = calculateUtilization(bank, delta);
|
|
179
227
|
let interestRate;
|
|
180
228
|
if (utilization.gt(new anchor_1.BN(bank.optimalUtilization))) {
|
|
181
229
|
const surplusUtilization = utilization.sub(new anchor_1.BN(bank.optimalUtilization));
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { AddressLookupTableAccount, PublicKey, TransactionInstruction } from '@solana/web3.js';
|
|
2
|
+
import { JupiterClient } from '../jupiter/jupiterClient';
|
|
3
|
+
import { DriftClient } from '../driftClient';
|
|
4
|
+
import { BN } from '@coral-xyz/anchor';
|
|
5
|
+
import { User } from '../user';
|
|
6
|
+
import { DepositRecord } from '../types';
|
|
7
|
+
export declare function findBestSuperStakeIxs({ amount, jupiterClient, driftClient, userAccountPublicKey, }: {
|
|
8
|
+
amount: BN;
|
|
9
|
+
jupiterClient: JupiterClient;
|
|
10
|
+
driftClient: DriftClient;
|
|
11
|
+
userAccountPublicKey?: PublicKey;
|
|
12
|
+
}): Promise<{
|
|
13
|
+
ixs: TransactionInstruction[];
|
|
14
|
+
lookupTables: AddressLookupTableAccount[];
|
|
15
|
+
method: 'jupiter' | 'marinade';
|
|
16
|
+
price: number;
|
|
17
|
+
}>;
|
|
18
|
+
export declare function calculateSolEarned({ user, depositRecords, }: {
|
|
19
|
+
user: User;
|
|
20
|
+
depositRecords: DepositRecord[];
|
|
21
|
+
}): Promise<BN>;
|
|
22
|
+
export declare function calculateEstimatedSuperStakeLiquidationPrice(msolDepositAmount: number, msolMaintenanceAssetWeight: number, solBorrowAmount: number, solMaintenanceLiabilityWeight: number, msolPriceRatio: number): number;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.calculateEstimatedSuperStakeLiquidationPrice = exports.calculateSolEarned = exports.findBestSuperStakeIxs = void 0;
|
|
7
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
8
|
+
const marinade_1 = require("../marinade");
|
|
9
|
+
const anchor_1 = require("@coral-xyz/anchor");
|
|
10
|
+
const types_1 = require("../types");
|
|
11
|
+
const numericConstants_1 = require("../constants/numericConstants");
|
|
12
|
+
const node_fetch_1 = __importDefault(require("node-fetch"));
|
|
13
|
+
async function findBestSuperStakeIxs({ amount, jupiterClient, driftClient, userAccountPublicKey, }) {
|
|
14
|
+
const marinadeProgram = (0, marinade_1.getMarinadeFinanceProgram)(driftClient.provider);
|
|
15
|
+
const marinadePrice = await (0, marinade_1.getMarinadeMSolPrice)(marinadeProgram);
|
|
16
|
+
const solMint = driftClient.getSpotMarketAccount(1).mint;
|
|
17
|
+
const mSOLMint = driftClient.getSpotMarketAccount(2).mint;
|
|
18
|
+
const jupiterRoutes = await jupiterClient.getRoutes({
|
|
19
|
+
inputMint: solMint,
|
|
20
|
+
outputMint: mSOLMint,
|
|
21
|
+
amount,
|
|
22
|
+
});
|
|
23
|
+
const bestRoute = jupiterRoutes[0];
|
|
24
|
+
const jupiterPrice = bestRoute.inAmount / bestRoute.outAmount;
|
|
25
|
+
if (marinadePrice <= jupiterPrice) {
|
|
26
|
+
const ixs = await driftClient.getStakeForMSOLIx({ amount });
|
|
27
|
+
return {
|
|
28
|
+
method: 'marinade',
|
|
29
|
+
ixs,
|
|
30
|
+
lookupTables: [],
|
|
31
|
+
price: marinadePrice,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
const { ixs, lookupTables } = await driftClient.getJupiterSwapIx({
|
|
36
|
+
inMarketIndex: 1,
|
|
37
|
+
outMarketIndex: 2,
|
|
38
|
+
route: bestRoute,
|
|
39
|
+
jupiterClient,
|
|
40
|
+
amount,
|
|
41
|
+
userAccountPublicKey,
|
|
42
|
+
});
|
|
43
|
+
return {
|
|
44
|
+
method: 'jupiter',
|
|
45
|
+
ixs,
|
|
46
|
+
lookupTables,
|
|
47
|
+
price: jupiterPrice,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exports.findBestSuperStakeIxs = findBestSuperStakeIxs;
|
|
52
|
+
async function calculateSolEarned({ user, depositRecords, }) {
|
|
53
|
+
const now = Date.now() / 1000;
|
|
54
|
+
const timestamps = [
|
|
55
|
+
now,
|
|
56
|
+
...depositRecords.map((r) => r.ts.toNumber()),
|
|
57
|
+
];
|
|
58
|
+
const msolRatios = new Map();
|
|
59
|
+
const getPrice = async (timestamp) => {
|
|
60
|
+
const date = new Date(timestamp * 1000); // Convert Unix timestamp to milliseconds
|
|
61
|
+
const swaggerApiDateTime = date.toISOString(); // Format date as swagger API date-time
|
|
62
|
+
const url = `https://api.marinade.finance/msol/price_sol?time=${swaggerApiDateTime}`;
|
|
63
|
+
const response = await (0, node_fetch_1.default)(url);
|
|
64
|
+
if (response.status === 200) {
|
|
65
|
+
const data = await response.json();
|
|
66
|
+
msolRatios.set(timestamp, data);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
await Promise.all(timestamps.map(getPrice));
|
|
70
|
+
let solEarned = numericConstants_1.ZERO;
|
|
71
|
+
for (const record of depositRecords) {
|
|
72
|
+
if (record.marketIndex === 1) {
|
|
73
|
+
if ((0, types_1.isVariant)(record.direction, 'deposit')) {
|
|
74
|
+
solEarned = solEarned.sub(record.amount);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
solEarned = solEarned.add(record.amount);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
else if (record.marketIndex === 2) {
|
|
81
|
+
const msolRatio = msolRatios.get(record.ts.toNumber());
|
|
82
|
+
const msolRatioBN = new anchor_1.BN(msolRatio * web3_js_1.LAMPORTS_PER_SOL);
|
|
83
|
+
const solAmount = record.amount.mul(msolRatioBN).div(numericConstants_1.LAMPORTS_PRECISION);
|
|
84
|
+
if ((0, types_1.isVariant)(record.direction, 'deposit')) {
|
|
85
|
+
solEarned = solEarned.sub(solAmount);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
solEarned = solEarned.add(solAmount);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const currentMSOLTokenAmount = await user.getTokenAmount(2);
|
|
93
|
+
const currentSOLTokenAmount = await user.getTokenAmount(1);
|
|
94
|
+
const currentMSOLRatio = msolRatios.get(now);
|
|
95
|
+
const currentMSOLRatioBN = new anchor_1.BN(currentMSOLRatio * web3_js_1.LAMPORTS_PER_SOL);
|
|
96
|
+
solEarned = solEarned.add(currentMSOLTokenAmount.mul(currentMSOLRatioBN).div(numericConstants_1.LAMPORTS_PRECISION));
|
|
97
|
+
solEarned = solEarned.add(currentSOLTokenAmount);
|
|
98
|
+
return solEarned;
|
|
99
|
+
}
|
|
100
|
+
exports.calculateSolEarned = calculateSolEarned;
|
|
101
|
+
// calculate estimated liquidation price (in mSOL/SOL) based on target amounts
|
|
102
|
+
function calculateEstimatedSuperStakeLiquidationPrice(msolDepositAmount, msolMaintenanceAssetWeight, solBorrowAmount, solMaintenanceLiabilityWeight, msolPriceRatio) {
|
|
103
|
+
const liquidationDivergence = (solMaintenanceLiabilityWeight * solBorrowAmount) /
|
|
104
|
+
(msolMaintenanceAssetWeight * msolDepositAmount * msolPriceRatio);
|
|
105
|
+
const liquidationPrice = msolPriceRatio * liquidationDivergence;
|
|
106
|
+
return liquidationPrice;
|
|
107
|
+
}
|
|
108
|
+
exports.calculateEstimatedSuperStakeLiquidationPrice = calculateEstimatedSuperStakeLiquidationPrice;
|
package/lib/math/utils.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { BN } from '../';
|
|
|
2
2
|
export declare function clampBN(x: BN, min: BN, max: BN): BN;
|
|
3
3
|
export declare const squareRootBN: (n: BN) => BN;
|
|
4
4
|
export declare const divCeil: (a: BN, b: BN) => BN;
|
|
5
|
+
export declare const sigNum: (x: BN) => BN;
|
|
5
6
|
/**
|
|
6
7
|
* calculates the time remaining until the next update based on a rounded, "on-the-hour" update schedule
|
|
7
8
|
* this schedule is used for Perpetual Funding Rate and Revenue -> Insurance Updates
|
package/lib/math/utils.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.timeRemainingUntilUpdate = exports.divCeil = exports.squareRootBN = exports.clampBN = void 0;
|
|
3
|
+
exports.timeRemainingUntilUpdate = exports.sigNum = exports.divCeil = exports.squareRootBN = exports.clampBN = void 0;
|
|
4
4
|
const __1 = require("../");
|
|
5
5
|
function clampBN(x, min, max) {
|
|
6
6
|
return __1.BN.max(min, __1.BN.min(x, max));
|
|
@@ -34,6 +34,10 @@ const divCeil = (a, b) => {
|
|
|
34
34
|
}
|
|
35
35
|
};
|
|
36
36
|
exports.divCeil = divCeil;
|
|
37
|
+
const sigNum = (x) => {
|
|
38
|
+
return x.isNeg() ? new __1.BN(-1) : new __1.BN(1);
|
|
39
|
+
};
|
|
40
|
+
exports.sigNum = sigNum;
|
|
37
41
|
/**
|
|
38
42
|
* calculates the time remaining until the next update based on a rounded, "on-the-hour" update schedule
|
|
39
43
|
* this schedule is used for Perpetual Funding Rate and Revenue -> Insurance Updates
|
package/lib/orderParams.d.ts
CHANGED
|
@@ -1,15 +1,28 @@
|
|
|
1
|
-
import { OptionalOrderParams, OrderTriggerCondition } from './types';
|
|
1
|
+
import { OptionalOrderParams, OrderParams, OrderTriggerCondition } from './types';
|
|
2
2
|
import { BN } from '@coral-xyz/anchor';
|
|
3
|
-
export declare function getLimitOrderParams(params: Omit<OptionalOrderParams, 'orderType'
|
|
3
|
+
export declare function getLimitOrderParams(params: Omit<OptionalOrderParams, 'orderType'> & {
|
|
4
4
|
price: BN;
|
|
5
5
|
}): OptionalOrderParams;
|
|
6
|
-
export declare function getTriggerMarketOrderParams(params: Omit<OptionalOrderParams, 'orderType'
|
|
6
|
+
export declare function getTriggerMarketOrderParams(params: Omit<OptionalOrderParams, 'orderType'> & {
|
|
7
7
|
triggerCondition: OrderTriggerCondition;
|
|
8
8
|
triggerPrice: BN;
|
|
9
9
|
}): OptionalOrderParams;
|
|
10
|
-
export declare function getTriggerLimitOrderParams(params: Omit<OptionalOrderParams, 'orderType'
|
|
10
|
+
export declare function getTriggerLimitOrderParams(params: Omit<OptionalOrderParams, 'orderType'> & {
|
|
11
11
|
triggerCondition: OrderTriggerCondition;
|
|
12
12
|
triggerPrice: BN;
|
|
13
13
|
price: BN;
|
|
14
14
|
}): OptionalOrderParams;
|
|
15
|
-
export declare function getMarketOrderParams(params: Omit<OptionalOrderParams, 'orderType'
|
|
15
|
+
export declare function getMarketOrderParams(params: Omit<OptionalOrderParams, 'orderType'>): OptionalOrderParams;
|
|
16
|
+
/**
|
|
17
|
+
* Creates an OrderParams object with the given OptionalOrderParams and any params to override.
|
|
18
|
+
*
|
|
19
|
+
* example:
|
|
20
|
+
* ```
|
|
21
|
+
* const orderParams = getOrderParams(optionalOrderParams, { marketType: MarketType.PERP });
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @param optionalOrderParams
|
|
25
|
+
* @param overridingParams
|
|
26
|
+
* @returns
|
|
27
|
+
*/
|
|
28
|
+
export declare function getOrderParams(optionalOrderParams: OptionalOrderParams, overridingParams?: Record<string, any>): OrderParams;
|
package/lib/orderParams.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getMarketOrderParams = exports.getTriggerLimitOrderParams = exports.getTriggerMarketOrderParams = exports.getLimitOrderParams = void 0;
|
|
3
|
+
exports.getOrderParams = exports.getMarketOrderParams = exports.getTriggerLimitOrderParams = exports.getTriggerMarketOrderParams = exports.getLimitOrderParams = void 0;
|
|
4
4
|
const types_1 = require("./types");
|
|
5
5
|
function getLimitOrderParams(params) {
|
|
6
6
|
return Object.assign({}, params, {
|
|
@@ -26,3 +26,19 @@ function getMarketOrderParams(params) {
|
|
|
26
26
|
});
|
|
27
27
|
}
|
|
28
28
|
exports.getMarketOrderParams = getMarketOrderParams;
|
|
29
|
+
/**
|
|
30
|
+
* Creates an OrderParams object with the given OptionalOrderParams and any params to override.
|
|
31
|
+
*
|
|
32
|
+
* example:
|
|
33
|
+
* ```
|
|
34
|
+
* const orderParams = getOrderParams(optionalOrderParams, { marketType: MarketType.PERP });
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @param optionalOrderParams
|
|
38
|
+
* @param overridingParams
|
|
39
|
+
* @returns
|
|
40
|
+
*/
|
|
41
|
+
function getOrderParams(optionalOrderParams, overridingParams = {}) {
|
|
42
|
+
return Object.assign({}, types_1.DefaultOrderParams, optionalOrderParams, overridingParams);
|
|
43
|
+
}
|
|
44
|
+
exports.getOrderParams = getOrderParams;
|
package/lib/user.d.ts
CHANGED
|
@@ -42,6 +42,7 @@ export declare class User {
|
|
|
42
42
|
* @returns userSpotPosition
|
|
43
43
|
*/
|
|
44
44
|
getSpotPosition(marketIndex: number): SpotPosition | undefined;
|
|
45
|
+
getEmptySpotPosition(marketIndex: number): SpotPosition;
|
|
45
46
|
/**
|
|
46
47
|
* Returns the token amount for a given market. The spot market precision is based on the token mint decimals.
|
|
47
48
|
* Positive if it is a deposit, negative if it is a borrow.
|
|
@@ -126,7 +127,6 @@ export declare class User {
|
|
|
126
127
|
getSpotLiabilityValue(tokenAmount: BN, oraclePriceData: OraclePriceData, spotMarketAccount: SpotMarketAccount, marginCategory?: MarginCategory, liquidationBuffer?: BN, strict?: boolean, now?: BN): BN;
|
|
127
128
|
getSpotMarketAssetValue(marketIndex?: number, marginCategory?: MarginCategory, includeOpenOrders?: boolean, strict?: boolean, now?: BN): BN;
|
|
128
129
|
getSpotAssetValue(tokenAmount: BN, oraclePriceData: OraclePriceData, spotMarketAccount: SpotMarketAccount, marginCategory?: MarginCategory, strict?: boolean, now?: BN): BN;
|
|
129
|
-
getSpotTokenAmount(marketIndex: number): BN;
|
|
130
130
|
getSpotPositionValue(marketIndex: number, marginCategory?: MarginCategory, includeOpenOrders?: boolean, strict?: boolean, now?: BN): BN;
|
|
131
131
|
getNetSpotMarketValue(withWeightMarginCategory?: MarginCategory): BN;
|
|
132
132
|
/**
|
|
@@ -160,6 +160,12 @@ export declare class User {
|
|
|
160
160
|
* @returns : Precision TEN_THOUSAND
|
|
161
161
|
*/
|
|
162
162
|
getLeverage(): BN;
|
|
163
|
+
calculateLeverageFromComponents({ perpLiabilityValue, perpPnl, spotAssetValue, spotLiabilityValue, }: {
|
|
164
|
+
perpLiabilityValue: BN;
|
|
165
|
+
perpPnl: BN;
|
|
166
|
+
spotAssetValue: BN;
|
|
167
|
+
spotLiabilityValue: BN;
|
|
168
|
+
}): BN;
|
|
163
169
|
getLeverageComponents(): {
|
|
164
170
|
perpLiabilityValue: BN;
|
|
165
171
|
perpPnl: BN;
|
|
@@ -252,6 +258,44 @@ export declare class User {
|
|
|
252
258
|
* @returns tradeSizeAllowed : Precision QUOTE_PRECISION
|
|
253
259
|
*/
|
|
254
260
|
getMaxTradeSizeUSDCForSpot(targetMarketIndex: number, direction: PositionDirection, currentQuoteAssetValue?: BN, currentSpotMarketNetValue?: BN): BN;
|
|
261
|
+
/**
|
|
262
|
+
* Calculates the max amount of token that can be swapped from inMarket to outMarket
|
|
263
|
+
* Assumes swap happens at oracle price
|
|
264
|
+
*
|
|
265
|
+
* @param inMarketIndex
|
|
266
|
+
* @param outMarketIndex
|
|
267
|
+
* @param calculateSwap function to similate in to out swa
|
|
268
|
+
* @param iterationLimit how long to run appromixation before erroring out
|
|
269
|
+
*/
|
|
270
|
+
getMaxSwapAmount({ inMarketIndex, outMarketIndex, calculateSwap, iterationLimit, }: {
|
|
271
|
+
inMarketIndex: number;
|
|
272
|
+
outMarketIndex: number;
|
|
273
|
+
calculateSwap?: (inAmount: BN) => BN;
|
|
274
|
+
iterationLimit?: number;
|
|
275
|
+
}): {
|
|
276
|
+
inAmount: BN;
|
|
277
|
+
outAmount: BN;
|
|
278
|
+
leverage: BN;
|
|
279
|
+
};
|
|
280
|
+
cloneAndUpdateSpotPosition(position: SpotPosition, tokenAmount: BN, market: SpotMarketAccount): SpotPosition;
|
|
281
|
+
calculateSpotPositionFreeCollateralContribution(spotPosition: SpotPosition): BN;
|
|
282
|
+
calculateSpotPositionLeverageContribution(spotPosition: SpotPosition): {
|
|
283
|
+
totalAssetValue: BN;
|
|
284
|
+
totalLiabilityValue: BN;
|
|
285
|
+
};
|
|
286
|
+
/**
|
|
287
|
+
* Estimates what the user leverage will be after swap
|
|
288
|
+
* @param inMarketIndex
|
|
289
|
+
* @param outMarketIndex
|
|
290
|
+
* @param inAmount
|
|
291
|
+
* @param outAmount
|
|
292
|
+
*/
|
|
293
|
+
accountLeverageAfterSwap({ inMarketIndex, outMarketIndex, inAmount, outAmount, }: {
|
|
294
|
+
inMarketIndex: number;
|
|
295
|
+
outMarketIndex: number;
|
|
296
|
+
inAmount: BN;
|
|
297
|
+
outAmount: BN;
|
|
298
|
+
}): BN;
|
|
255
299
|
/**
|
|
256
300
|
* Returns the leverage ratio for the account after adding (or subtracting) the given quote size to the given position
|
|
257
301
|
* @param targetMarketIndex
|