@drift-labs/common 1.0.59 → 1.0.61
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/_deprecated/common-math.d.ts +10 -0
- package/lib/_deprecated/common-math.js +9 -0
- package/lib/_deprecated/common-math.js.map +1 -0
- package/lib/_deprecated/common-ui-utils.d.ts +248 -0
- package/lib/_deprecated/common-ui-utils.js +59 -0
- package/lib/_deprecated/common-ui-utils.js.map +1 -0
- package/lib/_deprecated/equality-checks.d.ts +2 -0
- package/lib/_deprecated/equality-checks.js +7 -0
- package/lib/_deprecated/equality-checks.js.map +1 -0
- package/lib/{common-ui-utils/market.d.ts → _deprecated/market-utils.d.ts} +5 -7
- package/lib/_deprecated/market-utils.js +18 -0
- package/lib/_deprecated/market-utils.js.map +1 -0
- package/lib/_deprecated/order-utils.d.ts +12 -0
- package/lib/_deprecated/order-utils.js +18 -0
- package/lib/_deprecated/order-utils.js.map +1 -0
- package/lib/_deprecated/trading-utils.d.ts +52 -0
- package/lib/_deprecated/trading-utils.js +27 -0
- package/lib/_deprecated/trading-utils.js.map +1 -0
- package/lib/_deprecated/user-utils.d.ts +17 -0
- package/lib/_deprecated/user-utils.js +12 -0
- package/lib/_deprecated/user-utils.js.map +1 -0
- package/lib/_deprecated/utils.d.ts +40 -0
- package/lib/_deprecated/utils.js +47 -0
- package/lib/_deprecated/utils.js.map +1 -0
- package/lib/clients/tvFeed.js +2 -2
- package/lib/clients/tvFeed.js.map +1 -1
- package/lib/drift/Drift/clients/AuthorityDrift/DriftOperations/index.js +8 -8
- package/lib/drift/Drift/clients/AuthorityDrift/DriftOperations/index.js.map +1 -1
- package/lib/drift/Drift/clients/AuthorityDrift/index.js +9 -9
- package/lib/drift/Drift/clients/AuthorityDrift/index.js.map +1 -1
- package/lib/drift/Drift/clients/CentralServerDrift/index.js +5 -4
- package/lib/drift/Drift/clients/CentralServerDrift/index.js.map +1 -1
- package/lib/drift/base/actions/trade/editOrder.d.ts +1 -1
- package/lib/drift/base/actions/trade/editOrder.js.map +1 -1
- package/lib/drift/base/actions/trade/margin.js +4 -4
- package/lib/drift/base/actions/trade/margin.js.map +1 -1
- package/lib/drift/base/actions/trade/openPerpOrder/auction.d.ts +1 -1
- package/lib/drift/base/actions/trade/openPerpOrder/auction.js +4 -3
- package/lib/drift/base/actions/trade/openPerpOrder/auction.js.map +1 -1
- package/lib/drift/base/actions/trade/openPerpOrder/dlobServer/index.js +2 -2
- package/lib/drift/base/actions/trade/openPerpOrder/dlobServer/index.js.map +1 -1
- package/lib/drift/base/actions/trade/openPerpOrder/isolatedPositionDeposit.js +2 -2
- package/lib/drift/base/actions/trade/openPerpOrder/isolatedPositionDeposit.js.map +1 -1
- package/lib/drift/base/actions/trade/openPerpOrder/openPerpMarketOrder/index.d.ts +1 -1
- package/lib/drift/base/actions/trade/openPerpOrder/openPerpMarketOrder/index.js +4 -4
- package/lib/drift/base/actions/trade/openPerpOrder/openPerpMarketOrder/index.js.map +1 -1
- package/lib/drift/base/actions/trade/openPerpOrder/openPerpNonMarketOrder/index.d.ts +4 -4
- package/lib/drift/base/actions/trade/openPerpOrder/openPerpNonMarketOrder/index.js +60 -32
- package/lib/drift/base/actions/trade/openPerpOrder/openPerpNonMarketOrder/index.js.map +1 -1
- package/lib/drift/base/actions/trade/openPerpOrder/openSwiftOrder/index.js +4 -3
- package/lib/drift/base/actions/trade/openPerpOrder/openSwiftOrder/index.js.map +1 -1
- package/lib/drift/base/actions/trade/openPerpOrder/positionMaxLeverage.js +2 -2
- package/lib/drift/base/actions/trade/openPerpOrder/positionMaxLeverage.js.map +1 -1
- package/lib/drift/base/actions/trade/openPerpOrder/types.d.ts +5 -0
- package/lib/drift/base/actions/trade/openPerpOrder/types.js.map +1 -1
- package/lib/drift/base/actions/user/create.js +2 -2
- package/lib/drift/base/actions/user/create.js.map +1 -1
- package/lib/drift/base/details/user/balances.js +2 -2
- package/lib/drift/base/details/user/balances.js.map +1 -1
- package/lib/drift/base/details/user/positions.js +2 -2
- package/lib/drift/base/details/user/positions.js.map +1 -1
- package/lib/index.d.ts +28 -28
- package/lib/index.js +44 -29
- package/lib/index.js.map +1 -1
- package/lib/utils/accounts/index.d.ts +6 -0
- package/lib/utils/accounts/index.js +23 -0
- package/lib/utils/accounts/index.js.map +1 -0
- package/lib/utils/accounts/init.d.ts +22 -0
- package/lib/utils/accounts/init.js +90 -0
- package/lib/utils/accounts/init.js.map +1 -0
- package/lib/utils/accounts/keys.d.ts +22 -0
- package/lib/utils/accounts/keys.js +36 -0
- package/lib/utils/accounts/keys.js.map +1 -0
- package/lib/utils/accounts/multiple.d.ts +14 -0
- package/lib/utils/accounts/multiple.js +45 -0
- package/lib/utils/accounts/multiple.js.map +1 -0
- package/lib/utils/accounts/signature.d.ts +6 -0
- package/lib/utils/accounts/signature.js +53 -0
- package/lib/utils/accounts/signature.js.map +1 -0
- package/lib/utils/accounts/subaccounts.d.ts +8 -0
- package/lib/utils/accounts/subaccounts.js +31 -0
- package/lib/utils/accounts/subaccounts.js.map +1 -0
- package/lib/utils/{WalletConnectionState.d.ts → accounts/wallet.d.ts} +7 -1
- package/lib/utils/{WalletConnectionState.js → accounts/wallet.js} +32 -2
- package/lib/utils/accounts/wallet.js.map +1 -0
- package/lib/utils/core/arrays.d.ts +2 -0
- package/lib/utils/core/arrays.js +25 -0
- package/lib/utils/core/arrays.js.map +1 -0
- package/lib/utils/core/async.d.ts +5 -0
- package/lib/utils/core/async.js +17 -0
- package/lib/utils/core/async.js.map +1 -0
- package/lib/utils/core/cache.d.ts +1 -0
- package/lib/utils/core/cache.js +40 -0
- package/lib/utils/core/cache.js.map +1 -0
- package/lib/utils/core/data-structures.d.ts +30 -0
- package/lib/utils/core/data-structures.js +84 -0
- package/lib/utils/core/data-structures.js.map +1 -0
- package/lib/utils/{equalityChecks.d.ts → core/equality.d.ts} +1 -1
- package/lib/utils/{equalityChecks.js → core/equality.js} +3 -3
- package/lib/utils/core/equality.js.map +1 -0
- package/lib/utils/core/fetch.js.map +1 -0
- package/lib/utils/core/index.d.ts +7 -0
- package/lib/utils/core/index.js +24 -0
- package/lib/utils/core/index.js.map +1 -0
- package/lib/utils/core/serialization.d.ts +30 -0
- package/lib/utils/core/serialization.js +92 -0
- package/lib/utils/core/serialization.js.map +1 -0
- package/lib/utils/{enum.js → enum/index.js} +1 -1
- package/lib/utils/enum/index.js.map +1 -0
- package/lib/utils/index.d.ts +11 -176
- package/lib/utils/index.js +25 -594
- package/lib/utils/index.js.map +1 -1
- package/lib/utils/markets/balances.d.ts +6 -0
- package/lib/utils/markets/balances.js +29 -0
- package/lib/utils/markets/balances.js.map +1 -0
- package/lib/utils/markets/config.d.ts +5 -0
- package/lib/utils/markets/config.js +24 -0
- package/lib/utils/markets/config.js.map +1 -0
- package/lib/utils/markets/index.d.ts +6 -0
- package/lib/utils/markets/index.js +23 -0
- package/lib/utils/markets/index.js.map +1 -0
- package/lib/utils/markets/interest.d.ts +25 -0
- package/lib/utils/markets/interest.js +65 -0
- package/lib/utils/markets/interest.js.map +1 -0
- package/lib/utils/markets/leverage.d.ts +12 -0
- package/lib/utils/markets/leverage.js +60 -0
- package/lib/utils/markets/leverage.js.map +1 -0
- package/lib/utils/markets/operations.d.ts +21 -0
- package/lib/utils/markets/operations.js +59 -0
- package/lib/utils/markets/operations.js.map +1 -0
- package/lib/utils/math/bignum.d.ts +3 -0
- package/lib/utils/math/bignum.js +16 -0
- package/lib/utils/math/bignum.js.map +1 -0
- package/lib/utils/math/bn.d.ts +7 -0
- package/lib/utils/math/bn.js +58 -0
- package/lib/utils/math/bn.js.map +1 -0
- package/lib/utils/math/index.d.ts +7 -0
- package/lib/utils/math/index.js +24 -0
- package/lib/utils/math/index.js.map +1 -0
- package/lib/utils/math/numbers.d.ts +13 -0
- package/lib/utils/math/numbers.js +56 -0
- package/lib/utils/math/numbers.js.map +1 -0
- package/lib/utils/math/precision.d.ts +19 -0
- package/lib/utils/math/precision.js +73 -0
- package/lib/utils/math/precision.js.map +1 -0
- package/lib/utils/math/price.d.ts +12 -0
- package/lib/utils/math/price.js +45 -0
- package/lib/utils/math/price.js.map +1 -0
- package/lib/utils/math/sort.d.ts +13 -0
- package/lib/utils/math/sort.js +33 -0
- package/lib/utils/math/sort.js.map +1 -0
- package/lib/utils/math/spread.d.ts +8 -0
- package/lib/utils/math/spread.js +87 -0
- package/lib/utils/math/spread.js.map +1 -0
- package/lib/utils/orderbook/index.js +4 -4
- package/lib/utils/orderbook/index.js.map +1 -1
- package/lib/utils/orders/filters.d.ts +7 -0
- package/lib/utils/orders/filters.js +31 -0
- package/lib/utils/orders/filters.js.map +1 -0
- package/lib/utils/orders/flags.d.ts +12 -0
- package/lib/utils/orders/flags.js +44 -0
- package/lib/utils/orders/flags.js.map +1 -0
- package/lib/utils/orders/index.d.ts +6 -0
- package/lib/utils/orders/index.js +23 -0
- package/lib/utils/orders/index.js.map +1 -0
- package/lib/utils/orders/labels.d.ts +4 -0
- package/lib/utils/orders/labels.js +122 -0
- package/lib/utils/orders/labels.js.map +1 -0
- package/lib/utils/orders/misc.d.ts +11 -0
- package/lib/utils/orders/misc.js +27 -0
- package/lib/utils/orders/misc.js.map +1 -0
- package/lib/utils/orders/oracle.d.ts +5 -0
- package/lib/utils/orders/oracle.js +23 -0
- package/lib/utils/orders/oracle.js.map +1 -0
- package/lib/utils/orders/sort.d.ts +38 -0
- package/lib/utils/orders/sort.js +83 -0
- package/lib/utils/orders/sort.js.map +1 -0
- package/lib/utils/positions/index.d.ts +2 -0
- package/lib/{common-ui-utils → utils/positions}/index.js +1 -5
- package/lib/utils/positions/index.js.map +1 -0
- package/lib/utils/positions/open.d.ts +4 -0
- package/lib/{common-ui-utils/user.js → utils/positions/open.js} +10 -81
- package/lib/utils/positions/open.js.map +1 -0
- package/lib/utils/positions/user.d.ts +37 -0
- package/lib/utils/positions/user.js +74 -0
- package/lib/utils/positions/user.js.map +1 -0
- package/lib/utils/settings/settings.js.map +1 -0
- package/lib/utils/strings/convert.d.ts +11 -0
- package/lib/utils/{strings.js → strings/convert.js} +2 -51
- package/lib/utils/strings/convert.js.map +1 -0
- package/lib/utils/strings/format.d.ts +14 -0
- package/lib/utils/strings/format.js +61 -0
- package/lib/utils/strings/format.js.map +1 -0
- package/lib/utils/strings/index.d.ts +4 -0
- package/lib/utils/strings/index.js +21 -0
- package/lib/utils/strings/index.js.map +1 -0
- package/lib/utils/strings/parse.d.ts +4 -0
- package/lib/utils/strings/parse.js +25 -0
- package/lib/utils/strings/parse.js.map +1 -0
- package/lib/utils/strings/status.d.ts +15 -0
- package/lib/utils/strings/status.js +21 -0
- package/lib/utils/strings/status.js.map +1 -0
- package/lib/utils/token/account.d.ts +16 -0
- package/lib/utils/token/account.js +36 -0
- package/lib/utils/token/account.js.map +1 -0
- package/lib/utils/{token.d.ts → token/address.d.ts} +2 -7
- package/lib/utils/token/address.js +30 -0
- package/lib/utils/token/address.js.map +1 -0
- package/lib/utils/token/index.d.ts +3 -0
- package/lib/utils/token/index.js +20 -0
- package/lib/utils/token/index.js.map +1 -0
- package/lib/utils/token/instructions.d.ts +3 -0
- package/lib/utils/token/instructions.js +17 -0
- package/lib/utils/token/instructions.js.map +1 -0
- package/lib/utils/trading/auction.d.ts +82 -0
- package/lib/utils/trading/auction.js +208 -0
- package/lib/utils/trading/auction.js.map +1 -0
- package/lib/utils/trading/index.d.ts +7 -0
- package/lib/utils/trading/index.js +24 -0
- package/lib/utils/trading/index.js.map +1 -0
- package/lib/utils/trading/leverage.d.ts +18 -0
- package/lib/utils/trading/leverage.js +79 -0
- package/lib/utils/trading/leverage.js.map +1 -0
- package/lib/utils/trading/liquidation.d.ts +22 -0
- package/lib/utils/trading/liquidation.js +67 -0
- package/lib/utils/trading/liquidation.js.map +1 -0
- package/lib/utils/trading/lp.d.ts +4 -0
- package/lib/utils/trading/lp.js +20 -0
- package/lib/utils/trading/lp.js.map +1 -0
- package/lib/utils/trading/pnl.d.ts +34 -0
- package/lib/utils/trading/pnl.js +88 -0
- package/lib/utils/trading/pnl.js.map +1 -0
- package/lib/utils/trading/price.d.ts +12 -0
- package/lib/utils/trading/price.js +36 -0
- package/lib/utils/trading/price.js.map +1 -0
- package/lib/utils/trading/size.d.ts +27 -0
- package/lib/utils/trading/size.js +83 -0
- package/lib/utils/trading/size.js.map +1 -0
- package/lib/utils/{validation.d.ts → validation/address.d.ts} +1 -2
- package/lib/utils/{validation.js → validation/address.js} +4 -6
- package/lib/utils/validation/address.js.map +1 -0
- package/lib/utils/validation/index.d.ts +3 -0
- package/lib/utils/validation/index.js +20 -0
- package/lib/utils/validation/index.js.map +1 -0
- package/lib/utils/validation/input.d.ts +3 -0
- package/lib/utils/validation/input.js +33 -0
- package/lib/utils/validation/input.js.map +1 -0
- package/lib/utils/validation/notional.d.ts +2 -0
- package/lib/utils/validation/notional.js +8 -0
- package/lib/utils/validation/notional.js.map +1 -0
- package/package.json +90 -3
- package/lib/common-ui-utils/commonUiUtils.d.ts +0 -251
- package/lib/common-ui-utils/commonUiUtils.js +0 -647
- package/lib/common-ui-utils/commonUiUtils.js.map +0 -1
- package/lib/common-ui-utils/index.d.ts +0 -6
- package/lib/common-ui-utils/index.js.map +0 -1
- package/lib/common-ui-utils/market.js +0 -134
- package/lib/common-ui-utils/market.js.map +0 -1
- package/lib/common-ui-utils/order.d.ts +0 -25
- package/lib/common-ui-utils/order.js +0 -191
- package/lib/common-ui-utils/order.js.map +0 -1
- package/lib/common-ui-utils/settings/settings.js.map +0 -1
- package/lib/common-ui-utils/trading.d.ts +0 -79
- package/lib/common-ui-utils/trading.js +0 -313
- package/lib/common-ui-utils/trading.js.map +0 -1
- package/lib/common-ui-utils/user.d.ts +0 -18
- package/lib/common-ui-utils/user.js.map +0 -1
- package/lib/utils/WalletConnectionState.js.map +0 -1
- package/lib/utils/enum.js.map +0 -1
- package/lib/utils/equalityChecks.js.map +0 -1
- package/lib/utils/fetch.js.map +0 -1
- package/lib/utils/math.d.ts +0 -31
- package/lib/utils/math.js +0 -181
- package/lib/utils/math.js.map +0 -1
- package/lib/utils/strings.d.ts +0 -34
- package/lib/utils/strings.js.map +0 -1
- package/lib/utils/token.js +0 -45
- package/lib/utils/token.js.map +0 -1
- package/lib/utils/validation.js.map +0 -1
- /package/lib/utils/{fetch.d.ts → core/fetch.d.ts} +0 -0
- /package/lib/utils/{fetch.js → core/fetch.js} +0 -0
- /package/lib/utils/{enum.d.ts → enum/index.d.ts} +0 -0
- /package/lib/{common-ui-utils → utils}/settings/settings.d.ts +0 -0
- /package/lib/{common-ui-utils → utils}/settings/settings.js +0 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./numbers"), exports);
|
|
18
|
+
__exportStar(require("./bn"), exports);
|
|
19
|
+
__exportStar(require("./bignum"), exports);
|
|
20
|
+
__exportStar(require("./precision"), exports);
|
|
21
|
+
__exportStar(require("./spread"), exports);
|
|
22
|
+
__exportStar(require("./price"), exports);
|
|
23
|
+
__exportStar(require("./sort"), exports);
|
|
24
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/math/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4CAA0B;AAC1B,uCAAqB;AACrB,2CAAyB;AACzB,8CAA4B;AAC5B,2CAAyB;AACzB,0CAAwB;AACxB,yCAAuB","sourcesContent":["export * from './numbers';\nexport * from './bn';\nexport * from './bignum';\nexport * from './precision';\nexport * from './spread';\nexport * from './price';\nexport * from './sort';\n"]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare const aprFromApy: (apy: number, compoundsPerYear: number) => number;
|
|
2
|
+
export declare const calculateMean: (numbers: number[]) => number;
|
|
3
|
+
export declare const calculateMedian: (numbers: number[]) => number;
|
|
4
|
+
export declare const calculateStandardDeviation: (numbers: number[], mean: number) => number;
|
|
5
|
+
/**
|
|
6
|
+
* Returns the number of standard deviations between a target value and the history of values to compare it to.
|
|
7
|
+
* @param target
|
|
8
|
+
* @param previousValues
|
|
9
|
+
* @returns
|
|
10
|
+
*/
|
|
11
|
+
export declare const calculateZScore: (target: number, previousValues: number[]) => number;
|
|
12
|
+
export declare const getPctCompletion: (start: number, end: number, current: number) => number;
|
|
13
|
+
export declare function roundToDecimal(value: number, decimals: number | undefined | null): number;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.roundToDecimal = exports.getPctCompletion = exports.calculateZScore = exports.calculateStandardDeviation = exports.calculateMedian = exports.calculateMean = exports.aprFromApy = void 0;
|
|
4
|
+
const aprFromApy = (apy, compoundsPerYear) => {
|
|
5
|
+
const compoundedAmount = 1 + apy * 0.01;
|
|
6
|
+
const estimatedApr = (Math.pow(compoundedAmount, 1 / compoundsPerYear) - 1) * compoundsPerYear;
|
|
7
|
+
return estimatedApr * 100;
|
|
8
|
+
};
|
|
9
|
+
exports.aprFromApy = aprFromApy;
|
|
10
|
+
const calculateMean = (numbers) => {
|
|
11
|
+
const sum = numbers.reduce((total, num) => total + num, 0);
|
|
12
|
+
return sum / numbers.length;
|
|
13
|
+
};
|
|
14
|
+
exports.calculateMean = calculateMean;
|
|
15
|
+
const calculateMedian = (numbers) => {
|
|
16
|
+
const sortedNumbers = numbers.sort();
|
|
17
|
+
const middleIndex = Math.floor(sortedNumbers.length / 2);
|
|
18
|
+
if (sortedNumbers.length % 2 === 0) {
|
|
19
|
+
return (sortedNumbers[middleIndex - 1] + sortedNumbers[middleIndex]) / 2;
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
return sortedNumbers[middleIndex];
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
exports.calculateMedian = calculateMedian;
|
|
26
|
+
const calculateStandardDeviation = (numbers, mean) => {
|
|
27
|
+
const squaredDifferences = numbers.map((num) => Math.pow(num - mean, 2));
|
|
28
|
+
const sumSquaredDifferences = squaredDifferences.reduce((total, diff) => total + diff, 0);
|
|
29
|
+
const variance = sumSquaredDifferences / numbers.length;
|
|
30
|
+
return Math.sqrt(variance);
|
|
31
|
+
};
|
|
32
|
+
exports.calculateStandardDeviation = calculateStandardDeviation;
|
|
33
|
+
/**
|
|
34
|
+
* Returns the number of standard deviations between a target value and the history of values to compare it to.
|
|
35
|
+
* @param target
|
|
36
|
+
* @param previousValues
|
|
37
|
+
* @returns
|
|
38
|
+
*/
|
|
39
|
+
const calculateZScore = (target, previousValues) => {
|
|
40
|
+
const meanValue = (0, exports.calculateMean)(previousValues);
|
|
41
|
+
const standardDeviationValue = (0, exports.calculateStandardDeviation)(previousValues, meanValue);
|
|
42
|
+
const zScore = (target - meanValue) / standardDeviationValue;
|
|
43
|
+
return zScore;
|
|
44
|
+
};
|
|
45
|
+
exports.calculateZScore = calculateZScore;
|
|
46
|
+
const getPctCompletion = (start, end, current) => {
|
|
47
|
+
const totalProgressSize = end - start;
|
|
48
|
+
const currentProgressSize = current - start;
|
|
49
|
+
return (currentProgressSize / totalProgressSize) * 100;
|
|
50
|
+
};
|
|
51
|
+
exports.getPctCompletion = getPctCompletion;
|
|
52
|
+
function roundToDecimal(value, decimals) {
|
|
53
|
+
return decimals ? Math.round(value * 10 ** decimals) / 10 ** decimals : value;
|
|
54
|
+
}
|
|
55
|
+
exports.roundToDecimal = roundToDecimal;
|
|
56
|
+
//# sourceMappingURL=numbers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"numbers.js","sourceRoot":"","sources":["../../../src/utils/math/numbers.ts"],"names":[],"mappings":";;;AAAO,MAAM,UAAU,GAAG,CAAC,GAAW,EAAE,gBAAwB,EAAE,EAAE;IACnE,MAAM,gBAAgB,GAAG,CAAC,GAAG,GAAG,GAAG,IAAI,CAAC;IACxC,MAAM,YAAY,GACjB,CAAC,IAAI,CAAC,GAAG,CAAC,gBAAgB,EAAE,CAAC,GAAG,gBAAgB,CAAC,GAAG,CAAC,CAAC,GAAG,gBAAgB,CAAC;IAE3E,OAAO,YAAY,GAAG,GAAG,CAAC;AAC3B,CAAC,CAAC;AANW,QAAA,UAAU,cAMrB;AAEK,MAAM,aAAa,GAAG,CAAC,OAAiB,EAAU,EAAE;IAC1D,MAAM,GAAG,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE,CAAC,KAAK,GAAG,GAAG,EAAE,CAAC,CAAC,CAAC;IAC3D,OAAO,GAAG,GAAG,OAAO,CAAC,MAAM,CAAC;AAC7B,CAAC,CAAC;AAHW,QAAA,aAAa,iBAGxB;AAEK,MAAM,eAAe,GAAG,CAAC,OAAiB,EAAU,EAAE;IAC5D,MAAM,aAAa,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC;IACrC,MAAM,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,aAAa,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IACzD,IAAI,aAAa,CAAC,MAAM,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC;QACpC,OAAO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GAAG,OAAO,GAAG,KAAK,CAAC;IAE5C,OAAO,CAAC,mBAAmB,GAAG,iBAAiB,CAAC,GAAG,GAAG,CAAC;AACxD,CAAC,CAAC;AATW,QAAA,gBAAgB,oBAS3B;AAEF,SAAgB,cAAc,CAC7B,KAAa,EACb,QAAmC;IAEnC,OAAO,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,EAAE,IAAI,QAAQ,CAAC,GAAG,EAAE,IAAI,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC;AAC/E,CAAC;AALD,wCAKC","sourcesContent":["export const aprFromApy = (apy: number, compoundsPerYear: number) => {\n\tconst compoundedAmount = 1 + apy * 0.01;\n\tconst estimatedApr =\n\t\t(Math.pow(compoundedAmount, 1 / compoundsPerYear) - 1) * compoundsPerYear;\n\n\treturn estimatedApr * 100;\n};\n\nexport const calculateMean = (numbers: number[]): number => {\n\tconst sum = numbers.reduce((total, num) => total + num, 0);\n\treturn sum / numbers.length;\n};\n\nexport const calculateMedian = (numbers: number[]): number => {\n\tconst sortedNumbers = numbers.sort();\n\tconst middleIndex = Math.floor(sortedNumbers.length / 2);\n\tif (sortedNumbers.length % 2 === 0) {\n\t\treturn (sortedNumbers[middleIndex - 1] + sortedNumbers[middleIndex]) / 2;\n\t} else {\n\t\treturn sortedNumbers[middleIndex];\n\t}\n};\n\nexport const calculateStandardDeviation = (\n\tnumbers: number[],\n\tmean: number\n): number => {\n\tconst squaredDifferences = numbers.map((num) => Math.pow(num - mean, 2));\n\tconst sumSquaredDifferences = squaredDifferences.reduce(\n\t\t(total, diff) => total + diff,\n\t\t0\n\t);\n\tconst variance = sumSquaredDifferences / numbers.length;\n\treturn Math.sqrt(variance);\n};\n\n/**\n * Returns the number of standard deviations between a target value and the history of values to compare it to.\n * @param target\n * @param previousValues\n * @returns\n */\nexport const calculateZScore = (\n\ttarget: number,\n\tpreviousValues: number[]\n): number => {\n\tconst meanValue = calculateMean(previousValues);\n\tconst standardDeviationValue = calculateStandardDeviation(\n\t\tpreviousValues,\n\t\tmeanValue\n\t);\n\n\tconst zScore = (target - meanValue) / standardDeviationValue;\n\treturn zScore;\n};\n\nexport const getPctCompletion = (\n\tstart: number,\n\tend: number,\n\tcurrent: number\n) => {\n\tconst totalProgressSize = end - start;\n\tconst currentProgressSize = current - start;\n\n\treturn (currentProgressSize / totalProgressSize) * 100;\n};\n\nexport function roundToDecimal(\n\tvalue: number,\n\tdecimals: number | undefined | null\n) {\n\treturn decimals ? Math.round(value * 10 ** decimals) / 10 ** decimals : value;\n}\n"]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export declare const TRADE_PRECISION = 6;
|
|
2
|
+
export declare const truncateInputToPrecision: (input: string, marketPrecisionExp: BN) => string;
|
|
3
|
+
export declare const roundToStepSize: (value: string, stepSize?: number) => string;
|
|
4
|
+
export declare const roundToStepSizeIfLargeEnough: (value: string, stepSize?: number) => string;
|
|
5
|
+
export declare const valueIsBelowStepSize: (value: string, stepSize: number) => boolean;
|
|
6
|
+
/**
|
|
7
|
+
* NOTE: Do not use modulo alone to check if numbers fit evenly.
|
|
8
|
+
* Due to floating point precision issues this can return incorrect results.
|
|
9
|
+
* i.e. 5.1 % 0.1 = 0.09999999999999959 (should be 0)
|
|
10
|
+
* tells me 5.1 / 0.1 = 50.99999999999999
|
|
11
|
+
*/
|
|
12
|
+
export declare const numbersFitEvenly: (numberOne: number, numberTwo: number) => boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Check if numbers divide exactly, accounting for floating point division annoyingness
|
|
15
|
+
* @param numerator
|
|
16
|
+
* @param denominator
|
|
17
|
+
* @returns
|
|
18
|
+
*/
|
|
19
|
+
export declare const dividesExactly: (numerator: number, denominator: number) => boolean;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.dividesExactly = exports.numbersFitEvenly = exports.valueIsBelowStepSize = exports.roundToStepSizeIfLargeEnough = exports.roundToStepSize = exports.truncateInputToPrecision = exports.TRADE_PRECISION = void 0;
|
|
4
|
+
const sdk_1 = require("@drift-labs/sdk");
|
|
5
|
+
exports.TRADE_PRECISION = 6;
|
|
6
|
+
const truncateInputToPrecision = (input, marketPrecisionExp) => {
|
|
7
|
+
var _a, _b;
|
|
8
|
+
const decimalPlaces = (_b = (_a = input.split('.')[1]) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
|
|
9
|
+
const maxDecimals = marketPrecisionExp.toNumber();
|
|
10
|
+
if (decimalPlaces > maxDecimals) {
|
|
11
|
+
return input.slice(0, input.length - (decimalPlaces - maxDecimals));
|
|
12
|
+
}
|
|
13
|
+
return input;
|
|
14
|
+
};
|
|
15
|
+
exports.truncateInputToPrecision = truncateInputToPrecision;
|
|
16
|
+
const roundToStepSize = (value, stepSize) => {
|
|
17
|
+
var _a, _b;
|
|
18
|
+
const stepSizeExp = (_b = (_a = stepSize === null || stepSize === void 0 ? void 0 : stepSize.toString().split('.')[1]) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
|
|
19
|
+
const truncatedValue = (0, exports.truncateInputToPrecision)(value, new sdk_1.BN(stepSizeExp));
|
|
20
|
+
if (truncatedValue.charAt(truncatedValue.length - 1) === '.') {
|
|
21
|
+
return truncatedValue.slice(0, -1);
|
|
22
|
+
}
|
|
23
|
+
return truncatedValue;
|
|
24
|
+
};
|
|
25
|
+
exports.roundToStepSize = roundToStepSize;
|
|
26
|
+
const roundToStepSizeIfLargeEnough = (value, stepSize) => {
|
|
27
|
+
const parsedValue = parseFloat(value);
|
|
28
|
+
if (isNaN(parsedValue) || stepSize === 0 || !value || parsedValue === 0) {
|
|
29
|
+
return value;
|
|
30
|
+
}
|
|
31
|
+
return (0, exports.roundToStepSize)(value, stepSize);
|
|
32
|
+
};
|
|
33
|
+
exports.roundToStepSizeIfLargeEnough = roundToStepSizeIfLargeEnough;
|
|
34
|
+
const valueIsBelowStepSize = (value, stepSize) => {
|
|
35
|
+
const parsedValue = parseFloat(value);
|
|
36
|
+
if (isNaN(parsedValue))
|
|
37
|
+
return false;
|
|
38
|
+
return parsedValue < stepSize;
|
|
39
|
+
};
|
|
40
|
+
exports.valueIsBelowStepSize = valueIsBelowStepSize;
|
|
41
|
+
/**
|
|
42
|
+
* NOTE: Do not use modulo alone to check if numbers fit evenly.
|
|
43
|
+
* Due to floating point precision issues this can return incorrect results.
|
|
44
|
+
* i.e. 5.1 % 0.1 = 0.09999999999999959 (should be 0)
|
|
45
|
+
* tells me 5.1 / 0.1 = 50.99999999999999
|
|
46
|
+
*/
|
|
47
|
+
const numbersFitEvenly = (numberOne, numberTwo) => {
|
|
48
|
+
if (isNaN(numberOne) || isNaN(numberTwo))
|
|
49
|
+
return false;
|
|
50
|
+
if (numberOne === 0 || numberTwo === 0)
|
|
51
|
+
return true;
|
|
52
|
+
return (Number.isInteger(Number((numberOne / numberTwo).toFixed(9))) ||
|
|
53
|
+
numberOne % numberTwo === 0);
|
|
54
|
+
};
|
|
55
|
+
exports.numbersFitEvenly = numbersFitEvenly;
|
|
56
|
+
/**
|
|
57
|
+
* Check if numbers divide exactly, accounting for floating point division annoyingness
|
|
58
|
+
* @param numerator
|
|
59
|
+
* @param denominator
|
|
60
|
+
* @returns
|
|
61
|
+
*/
|
|
62
|
+
const dividesExactly = (numerator, denominator) => {
|
|
63
|
+
const division = numerator / denominator;
|
|
64
|
+
const remainder = division % 1;
|
|
65
|
+
if (remainder === 0)
|
|
66
|
+
return true;
|
|
67
|
+
// Because of floating point weirdness, we're just going to assume that if the remainder after dividing is less than 1/10^6 then the numbers do divide exactly
|
|
68
|
+
if (Math.abs(remainder - 1) < 1 / 10 ** 6)
|
|
69
|
+
return true;
|
|
70
|
+
return false;
|
|
71
|
+
};
|
|
72
|
+
exports.dividesExactly = dividesExactly;
|
|
73
|
+
//# sourceMappingURL=precision.js.map
|
|
@@ -0,0 +1 @@
|
|
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CAAC;IAE/B,IAAI,SAAS,KAAK,CAAC;QAAE,OAAO,IAAI,CAAC;IAEjC,8JAA8J;IAC9J,IAAI,IAAI,CAAC,GAAG,CAAC,SAAS,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC;QAAE,OAAO,IAAI,CAAC;IAEvD,OAAO,KAAK,CAAC;AACd,CAAC,CAAC;AAVW,QAAA,cAAc,kBAUzB","sourcesContent":["import { BN, SpotMarketConfig } from '@drift-labs/sdk';\n\nexport const TRADE_PRECISION = 6;\n\nexport const truncateInputToPrecision = (\n\tinput: string,\n\tmarketPrecisionExp: SpotMarketConfig['precisionExp']\n) => {\n\tconst decimalPlaces = input.split('.')[1]?.length ?? 0;\n\tconst maxDecimals = marketPrecisionExp.toNumber();\n\n\tif (decimalPlaces > maxDecimals) {\n\t\treturn input.slice(0, input.length - (decimalPlaces - maxDecimals));\n\t}\n\n\treturn input;\n};\n\nexport const roundToStepSize = (value: string, stepSize?: number) => {\n\tconst stepSizeExp = stepSize?.toString().split('.')[1]?.length ?? 0;\n\tconst truncatedValue = truncateInputToPrecision(value, new BN(stepSizeExp));\n\n\tif (truncatedValue.charAt(truncatedValue.length - 1) === '.') {\n\t\treturn truncatedValue.slice(0, -1);\n\t}\n\n\treturn truncatedValue;\n};\n\nexport const roundToStepSizeIfLargeEnough = (\n\tvalue: string,\n\tstepSize?: number\n) => {\n\tconst parsedValue = parseFloat(value);\n\tif (isNaN(parsedValue) || stepSize === 0 || !value || parsedValue === 0) {\n\t\treturn value;\n\t}\n\n\treturn roundToStepSize(value, stepSize);\n};\n\nexport const valueIsBelowStepSize = (value: string, stepSize: number) => {\n\tconst parsedValue = parseFloat(value);\n\n\tif (isNaN(parsedValue)) return false;\n\n\treturn parsedValue < stepSize;\n};\n\n/**\n * NOTE: Do not use modulo alone to check if numbers fit evenly.\n * Due to floating point precision issues this can return incorrect results.\n * i.e. 5.1 % 0.1 = 0.09999999999999959 (should be 0)\n * tells me 5.1 / 0.1 = 50.99999999999999\n */\nexport const numbersFitEvenly = (\n\tnumberOne: number,\n\tnumberTwo: number\n): boolean => {\n\tif (isNaN(numberOne) || isNaN(numberTwo)) return false;\n\tif (numberOne === 0 || numberTwo === 0) return true;\n\n\treturn (\n\t\tNumber.isInteger(Number((numberOne / numberTwo).toFixed(9))) ||\n\t\tnumberOne % numberTwo === 0\n\t);\n};\n\n/**\n * Check if numbers divide exactly, accounting for floating point division annoyingness\n * @param numerator\n * @param denominator\n * @returns\n */\nexport const dividesExactly = (numerator: number, denominator: number) => {\n\tconst division = numerator / denominator;\n\tconst remainder = division % 1;\n\n\tif (remainder === 0) return true;\n\n\t// Because of floating point weirdness, we're just going to assume that if the remainder after dividing is less than 1/10^6 then the numbers do divide exactly\n\tif (Math.abs(remainder - 1) < 1 / 10 ** 6) return true;\n\n\treturn false;\n};\n"]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { BigNum, OrderActionRecord } from '@drift-labs/sdk';
|
|
2
|
+
import { UISerializableOrderActionRecord } from '../../serializableTypes';
|
|
3
|
+
/**
|
|
4
|
+
* Returns the average price for a given base amount and quote amount.
|
|
5
|
+
* @param quoteAmount
|
|
6
|
+
* @param baseAmount
|
|
7
|
+
* @returns PRICE_PRECISION
|
|
8
|
+
*/
|
|
9
|
+
export declare const getPriceForBaseAndQuoteAmount: (quoteAmount: BN, baseAmount: BN) => any;
|
|
10
|
+
export declare const getPriceForOrderRecord: (orderRecord: Pick<OrderActionRecord, 'quoteAssetAmountFilled' | 'baseAssetAmountFilled'>) => any;
|
|
11
|
+
export declare const getPriceForUIOrderRecord: (orderRecord: Pick<UISerializableOrderActionRecord, 'quoteAssetAmountFilled' | 'baseAssetAmountFilled'>) => BigNum;
|
|
12
|
+
export declare const calculateAverageEntryPrice: (quoteAssetAmount: BigNum, baseAssetAmount: BigNum) => BigNum;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.calculateAverageEntryPrice = exports.getPriceForUIOrderRecord = exports.getPriceForOrderRecord = exports.getPriceForBaseAndQuoteAmount = void 0;
|
|
4
|
+
const sdk_1 = require("@drift-labs/sdk");
|
|
5
|
+
/**
|
|
6
|
+
* Returns the average price for a given base amount and quote amount.
|
|
7
|
+
* @param quoteAmount
|
|
8
|
+
* @param baseAmount
|
|
9
|
+
* @returns PRICE_PRECISION
|
|
10
|
+
*/
|
|
11
|
+
const getPriceForBaseAndQuoteAmount = (quoteAmount, baseAmount) => {
|
|
12
|
+
return quoteAmount
|
|
13
|
+
.mul(sdk_1.PRICE_PRECISION)
|
|
14
|
+
.mul(sdk_1.BASE_PRECISION)
|
|
15
|
+
.div(sdk_1.QUOTE_PRECISION)
|
|
16
|
+
.div(sdk_1.BigNum.from(baseAmount, sdk_1.BASE_PRECISION_EXP).val);
|
|
17
|
+
};
|
|
18
|
+
exports.getPriceForBaseAndQuoteAmount = getPriceForBaseAndQuoteAmount;
|
|
19
|
+
const getPriceForOrderRecord = (orderRecord) => {
|
|
20
|
+
return (0, exports.getPriceForBaseAndQuoteAmount)(
|
|
21
|
+
// @ts-ignore
|
|
22
|
+
orderRecord.quoteAssetAmountFilled,
|
|
23
|
+
// @ts-ignore
|
|
24
|
+
orderRecord.baseAssetAmountFilled);
|
|
25
|
+
};
|
|
26
|
+
exports.getPriceForOrderRecord = getPriceForOrderRecord;
|
|
27
|
+
const getPriceForUIOrderRecord = (orderRecord) => {
|
|
28
|
+
return orderRecord.quoteAssetAmountFilled
|
|
29
|
+
.shiftTo(sdk_1.AMM_RESERVE_PRECISION_EXP)
|
|
30
|
+
.shift(sdk_1.PRICE_PRECISION_EXP)
|
|
31
|
+
.div(orderRecord.baseAssetAmountFilled.shiftTo(sdk_1.BASE_PRECISION_EXP))
|
|
32
|
+
.shiftTo(sdk_1.PRICE_PRECISION_EXP);
|
|
33
|
+
};
|
|
34
|
+
exports.getPriceForUIOrderRecord = getPriceForUIOrderRecord;
|
|
35
|
+
const calculateAverageEntryPrice = (quoteAssetAmount, baseAssetAmount) => {
|
|
36
|
+
if (baseAssetAmount.eqZero())
|
|
37
|
+
return sdk_1.BigNum.zero();
|
|
38
|
+
return sdk_1.BigNum.from(quoteAssetAmount.val
|
|
39
|
+
.mul(sdk_1.PRICE_PRECISION)
|
|
40
|
+
.mul(sdk_1.AMM_TO_QUOTE_PRECISION_RATIO)
|
|
41
|
+
.div(baseAssetAmount.shiftTo(sdk_1.BASE_PRECISION_EXP).val)
|
|
42
|
+
.abs(), sdk_1.PRICE_PRECISION_EXP);
|
|
43
|
+
};
|
|
44
|
+
exports.calculateAverageEntryPrice = calculateAverageEntryPrice;
|
|
45
|
+
//# sourceMappingURL=price.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"price.js","sourceRoot":"","sources":["../../../src/utils/math/price.ts"],"names":[],"mappings":";;;AAAA,yCAWyB;AAGzB;;;;;GAKG;AACI,MAAM,6BAA6B,GAAG,CAC5C,WAAe,EACf,UAAc,EACb,EAAE;IACH,OAAO,WAAW;SAChB,GAAG,CAAC,qBAAe,CAAC;SACpB,GAAG,CAAC,oBAAc,CAAC;SACnB,GAAG,CAAC,qBAAe,CAAC;SACpB,GAAG,CAAC,YAAM,CAAC,IAAI,CAAC,UAAU,EAAE,wBAAkB,CAAC,CAAC,GAAG,CAAC,CAAC;AACxD,CAAC,CAAC;AATW,QAAA,6BAA6B,iCASxC;AAEK,MAAM,sBAAsB,GAAG,CACrC,WAGC,EACA,EAAE;IACH,OAAO,IAAA,qCAA6B;IACnC,aAAa;IACb,WAAW,CAAC,sBAAsB;IAClC,aAAa;IACb,WAAW,CAAC,qBAAqB,CACjC,CAAC;AACH,CAAC,CAAC;AAZW,QAAA,sBAAsB,0BAYjC;AAEK,MAAM,wBAAwB,GAAG,CACvC,WAGC,EACA,EAAE;IACH,OAAO,WAAW,CAAC,sBAAsB;SACvC,OAAO,CAAC,+BAAyB,CAAC;SAClC,KAAK,CAAC,yBAAmB,CAAC;SAC1B,GAAG,CAAC,WAAW,CAAC,qBAAqB,CAAC,OAAO,CAAC,wBAAkB,CAAC,CAAC;SAClE,OAAO,CAAC,yBAAmB,CAAC,CAAC;AAChC,CAAC,CAAC;AAXW,QAAA,wBAAwB,4BAWnC;AAEK,MAAM,0BAA0B,GAAG,CACzC,gBAAwB,EACxB,eAAuB,EACd,EAAE;IACX,IAAI,eAAe,CAAC,MAAM,EAAE;QAAE,OAAO,YAAM,CAAC,IAAI,EAAE,CAAC;IAEnD,OAAO,YAAM,CAAC,IAAI,CACjB,gBAAgB,CAAC,GAAG;SAClB,GAAG,CAAC,qBAAe,CAAC;SACpB,GAAG,CAAC,kCAA4B,CAAC;SACjC,GAAG,CAAC,eAAe,CAAC,OAAO,CAAC,wBAAkB,CAAC,CAAC,GAAG,CAAC;SACpD,GAAG,EAAE,EACP,yBAAmB,CACnB,CAAC;AACH,CAAC,CAAC;AAdW,QAAA,0BAA0B,8BAcrC","sourcesContent":["import {\n\tAMM_RESERVE_PRECISION_EXP,\n\tAMM_TO_QUOTE_PRECISION_RATIO,\n\tBASE_PRECISION,\n\tBASE_PRECISION_EXP,\n\tBigNum,\n\tBN,\n\tOrderActionRecord,\n\tPRICE_PRECISION,\n\tPRICE_PRECISION_EXP,\n\tQUOTE_PRECISION,\n} from '@drift-labs/sdk';\nimport { UISerializableOrderActionRecord } from '../../serializableTypes';\n\n/**\n * Returns the average price for a given base amount and quote amount.\n * @param quoteAmount\n * @param baseAmount\n * @returns PRICE_PRECISION\n */\nexport const getPriceForBaseAndQuoteAmount = (\n\tquoteAmount: BN,\n\tbaseAmount: BN\n) => {\n\treturn quoteAmount\n\t\t.mul(PRICE_PRECISION)\n\t\t.mul(BASE_PRECISION)\n\t\t.div(QUOTE_PRECISION)\n\t\t.div(BigNum.from(baseAmount, BASE_PRECISION_EXP).val);\n};\n\nexport const getPriceForOrderRecord = (\n\torderRecord: Pick<\n\t\tOrderActionRecord,\n\t\t'quoteAssetAmountFilled' | 'baseAssetAmountFilled'\n\t>\n) => {\n\treturn getPriceForBaseAndQuoteAmount(\n\t\t// @ts-ignore\n\t\torderRecord.quoteAssetAmountFilled,\n\t\t// @ts-ignore\n\t\torderRecord.baseAssetAmountFilled\n\t);\n};\n\nexport const getPriceForUIOrderRecord = (\n\torderRecord: Pick<\n\t\tUISerializableOrderActionRecord,\n\t\t'quoteAssetAmountFilled' | 'baseAssetAmountFilled'\n\t>\n) => {\n\treturn orderRecord.quoteAssetAmountFilled\n\t\t.shiftTo(AMM_RESERVE_PRECISION_EXP)\n\t\t.shift(PRICE_PRECISION_EXP)\n\t\t.div(orderRecord.baseAssetAmountFilled.shiftTo(BASE_PRECISION_EXP))\n\t\t.shiftTo(PRICE_PRECISION_EXP);\n};\n\nexport const calculateAverageEntryPrice = (\n\tquoteAssetAmount: BigNum,\n\tbaseAssetAmount: BigNum\n): BigNum => {\n\tif (baseAssetAmount.eqZero()) return BigNum.zero();\n\n\treturn BigNum.from(\n\t\tquoteAssetAmount.val\n\t\t\t.mul(PRICE_PRECISION)\n\t\t\t.mul(AMM_TO_QUOTE_PRECISION_RATIO)\n\t\t\t.div(baseAssetAmount.shiftTo(BASE_PRECISION_EXP).val)\n\t\t\t.abs(),\n\t\tPRICE_PRECISION_EXP\n\t);\n};\n"]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper utility to get a sort score for "tiered" parameters.
|
|
3
|
+
*
|
|
4
|
+
* Example: Want to sort students by Grade, but fall back to using Age if they are equal. This method will accept an array for each student of [grade, age] and return the appropriate sort score for each.
|
|
5
|
+
*
|
|
6
|
+
* @param aScores
|
|
7
|
+
* @param bScores
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
export declare const getTieredSortScore: (aScores: number[], bScores: number[]) => number;
|
|
11
|
+
export declare const sortRecordsByTs: <T extends {
|
|
12
|
+
ts: BN;
|
|
13
|
+
}[]>(records: T | undefined, direction?: 'asc' | 'desc') => T[number][];
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sortRecordsByTs = exports.getTieredSortScore = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Helper utility to get a sort score for "tiered" parameters.
|
|
6
|
+
*
|
|
7
|
+
* Example: Want to sort students by Grade, but fall back to using Age if they are equal. This method will accept an array for each student of [grade, age] and return the appropriate sort score for each.
|
|
8
|
+
*
|
|
9
|
+
* @param aScores
|
|
10
|
+
* @param bScores
|
|
11
|
+
* @returns
|
|
12
|
+
*/
|
|
13
|
+
const getTieredSortScore = (aScores, bScores) => {
|
|
14
|
+
var _a, _b;
|
|
15
|
+
const maxIndex = Math.max(aScores.length, bScores.length);
|
|
16
|
+
for (let i = 0; i < maxIndex; i++) {
|
|
17
|
+
const aScore = (_a = aScores[i]) !== null && _a !== void 0 ? _a : Number.MIN_SAFE_INTEGER;
|
|
18
|
+
const bScore = (_b = bScores[i]) !== null && _b !== void 0 ? _b : Number.MIN_SAFE_INTEGER;
|
|
19
|
+
if (aScore !== bScore)
|
|
20
|
+
return aScore - bScore;
|
|
21
|
+
}
|
|
22
|
+
return 0;
|
|
23
|
+
};
|
|
24
|
+
exports.getTieredSortScore = getTieredSortScore;
|
|
25
|
+
const sortRecordsByTs = (records, direction = 'desc') => {
|
|
26
|
+
if (!records || !(records === null || records === void 0 ? void 0 : records.length))
|
|
27
|
+
return [];
|
|
28
|
+
return direction === 'desc'
|
|
29
|
+
? [...records].sort((a, b) => b.ts.toNumber() - a.ts.toNumber())
|
|
30
|
+
: [...records].sort((a, b) => a.ts.toNumber() - b.ts.toNumber());
|
|
31
|
+
};
|
|
32
|
+
exports.sortRecordsByTs = sortRecordsByTs;
|
|
33
|
+
//# sourceMappingURL=sort.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sort.js","sourceRoot":"","sources":["../../../src/utils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C;AACnE,CAAC,CAAC;AATW,QAAA,eAAe,mBAS1B","sourcesContent":["import { BN } from '@drift-labs/sdk';\n\n/**\n * Helper utility to get a sort score for \"tiered\" parameters.\n *\n * Example: Want to sort students by Grade, but fall back to using Age if they are equal. This method will accept an array for each student of [grade, age] and return the appropriate sort score for each.\n *\n * @param aScores\n * @param bScores\n * @returns\n */\nexport const getTieredSortScore = (aScores: number[], bScores: number[]) => {\n\tconst maxIndex = Math.max(aScores.length, bScores.length);\n\n\tfor (let i = 0; i < maxIndex; i++) {\n\t\tconst aScore = aScores[i] ?? Number.MIN_SAFE_INTEGER;\n\t\tconst bScore = bScores[i] ?? Number.MIN_SAFE_INTEGER;\n\n\t\tif (aScore !== bScore) return aScore - bScore;\n\t}\n\n\treturn 0;\n};\n\nexport const sortRecordsByTs = <T extends { ts: BN }[]>(\n\trecords: T | undefined,\n\tdirection: 'asc' | 'desc' = 'desc'\n) => {\n\tif (!records || !records?.length) return [];\n\n\treturn direction === 'desc'\n\t\t? [...records].sort((a, b) => b.ts.toNumber() - a.ts.toNumber())\n\t\t: [...records].sort((a, b) => a.ts.toNumber() - b.ts.toNumber());\n};\n"]}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.calculateSpreadBidAskMark = void 0;
|
|
4
|
+
const sdk_1 = require("@drift-labs/sdk");
|
|
5
|
+
const calculateMarkPrice = (bestBidPrice, bestAskPrice, oraclePrice) => {
|
|
6
|
+
const bid = bestBidPrice;
|
|
7
|
+
const ask = bestAskPrice;
|
|
8
|
+
let mid;
|
|
9
|
+
// if bid/ask cross, force it to be the one closer to oracle, if oracle is in the middle, use oracle price
|
|
10
|
+
if (bid && ask && bid.gt(ask) && oraclePrice) {
|
|
11
|
+
if (bid.gt(oraclePrice) && ask.gt(oraclePrice)) {
|
|
12
|
+
mid = sdk_1.BN.min(bid, ask);
|
|
13
|
+
}
|
|
14
|
+
else if (bid.lt(oraclePrice) && ask.lt(oraclePrice)) {
|
|
15
|
+
mid = sdk_1.BN.max(bid, ask);
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
mid = oraclePrice;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
if (bid && ask) {
|
|
23
|
+
mid = bid.add(ask).divn(2);
|
|
24
|
+
}
|
|
25
|
+
else if (oraclePrice) {
|
|
26
|
+
mid = oraclePrice;
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
mid = undefined;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return mid;
|
|
33
|
+
};
|
|
34
|
+
const calculateBidAskAndmarkPrice = (l2, oraclePrice) => {
|
|
35
|
+
const bestBidPrice = l2.bids.reduce((previousMax, currentBid) => {
|
|
36
|
+
if (!previousMax)
|
|
37
|
+
return currentBid.price;
|
|
38
|
+
return sdk_1.BN.max(currentBid.price, previousMax);
|
|
39
|
+
}, undefined);
|
|
40
|
+
const bestAskPrice = l2.asks.reduce((previousMin, currentBid) => {
|
|
41
|
+
if (!previousMin)
|
|
42
|
+
return currentBid.price;
|
|
43
|
+
return sdk_1.BN.min(currentBid.price, previousMin);
|
|
44
|
+
}, undefined);
|
|
45
|
+
const markPrice = calculateMarkPrice(bestBidPrice, bestAskPrice, oraclePrice);
|
|
46
|
+
return {
|
|
47
|
+
bestBidPrice,
|
|
48
|
+
bestAskPrice,
|
|
49
|
+
markPrice,
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
const calculateSpreadQuote = (bestBidPrice, bestAskPrice) => {
|
|
53
|
+
return sdk_1.BN.max(bestAskPrice.sub(bestBidPrice), sdk_1.ZERO);
|
|
54
|
+
};
|
|
55
|
+
function calculateSpreadPct(markPricePrice, spreadQuote) {
|
|
56
|
+
return spreadQuote.muln(100).mul(sdk_1.PERCENTAGE_PRECISION).div(markPricePrice);
|
|
57
|
+
}
|
|
58
|
+
const calculateSpread = (bestBidPrice, bestAskPrice, markPrice) => {
|
|
59
|
+
const spreadQuote = calculateSpreadQuote(bestBidPrice, bestAskPrice);
|
|
60
|
+
const spreadPct = calculateSpreadPct(markPrice, spreadQuote);
|
|
61
|
+
return {
|
|
62
|
+
spreadPct,
|
|
63
|
+
spreadQuote,
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
const calculateSpreadBidAskMark = (l2, oraclePrice) => {
|
|
67
|
+
if (l2.asks.length === 0 || l2.bids.length === 0) {
|
|
68
|
+
return {
|
|
69
|
+
spreadQuote: undefined,
|
|
70
|
+
spreadPct: undefined,
|
|
71
|
+
markPrice: undefined,
|
|
72
|
+
bestBidPrice: undefined,
|
|
73
|
+
bestAskPrice: undefined,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
const { bestBidPrice, bestAskPrice, markPrice } = calculateBidAskAndmarkPrice(l2, oraclePrice);
|
|
77
|
+
const { spreadPct, spreadQuote } = calculateSpread(bestBidPrice, bestAskPrice, markPrice);
|
|
78
|
+
return {
|
|
79
|
+
bestBidPrice,
|
|
80
|
+
bestAskPrice,
|
|
81
|
+
markPrice,
|
|
82
|
+
spreadPct,
|
|
83
|
+
spreadQuote,
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
exports.calculateSpreadBidAskMark = calculateSpreadBidAskMark;
|
|
87
|
+
//# sourceMappingURL=spread.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"spread.js","sourceRoot":"","sources":["../../../src/utils/math/spread.ts"],"names":[],"mappings":";;;AAAA,yCAA8E;AAE9E,MAAM,kBAAkB,GAAG,CAC1B,YAAiB,EACjB,YAAiB,EACjB,WAAgB,EACf,EAAE;IACH,MAAM,GAAG,GAAG,YAAY,CAAC;IACzB,MAAM,GAAG,GAAG,YAAY,CAAC;IAEzB,IAAI,GAAO,CAAC;IAEZ,0GAA0G;IAC1G,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,CAAC,EAAE,CAAC,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AAS;YACpB,YAAY,EAAE,SAAS;YACvB,YAAY,EAAE,SAAS;SACvB,CAAC;IACH,CAAC;IAED,MAAM,EAAE,YAAY,EAAE,YAAY,EAAE,SAAS,EAAE,GAAG,2BAA2B,CAC5E,EAAE,EACF,WAAW,CACX,CAAC;IAEF,MAAM,EAAE,SAAS,EAAE,WAAW,EAAE,GAAG,eAAe,CACjD,YAAY,EACZ,YAAY,EACZ,SAAS,CACT,CAAC;IACF,OAAO;QACN,YAAY;QACZ,YAAY;QACZ,SAAS;QACT,SAAS;QACT,WAAW;KACX,CAAC;AACH,CAAC,CAAC;AA/BW,QAAA,yBAAyB,6BA+BpC","sourcesContent":["import { BN, L2OrderBook, PERCENTAGE_PRECISION, ZERO } from '@drift-labs/sdk';\n\nconst calculateMarkPrice = (\n\tbestBidPrice?: BN,\n\tbestAskPrice?: BN,\n\toraclePrice?: BN\n) => {\n\tconst bid = bestBidPrice;\n\tconst ask = bestAskPrice;\n\n\tlet mid: BN;\n\n\t// if bid/ask cross, force it to be the one closer to oracle, if oracle is in the middle, use oracle price\n\tif (bid && ask && bid.gt(ask) && oraclePrice) {\n\t\tif (bid.gt(oraclePrice) && ask.gt(oraclePrice)) {\n\t\t\tmid = BN.min(bid, ask);\n\t\t} else if (bid.lt(oraclePrice) && ask.lt(oraclePrice)) {\n\t\t\tmid = BN.max(bid, ask);\n\t\t} else {\n\t\t\tmid = oraclePrice;\n\t\t}\n\t} else {\n\t\tif (bid && ask) {\n\t\t\tmid = bid.add(ask).divn(2);\n\t\t} else if (oraclePrice) {\n\t\t\tmid = oraclePrice;\n\t\t} else {\n\t\t\tmid = undefined;\n\t\t}\n\t}\n\n\treturn mid;\n};\n\nconst calculateBidAskAndmarkPrice = (l2: L2OrderBook, oraclePrice?: BN) => {\n\tconst bestBidPrice = l2.bids.reduce((previousMax, currentBid) => {\n\t\tif (!previousMax) return currentBid.price;\n\t\treturn BN.max(currentBid.price, previousMax);\n\t}, undefined as BN);\n\n\tconst bestAskPrice = l2.asks.reduce((previousMin, currentBid) => {\n\t\tif (!previousMin) return currentBid.price;\n\t\treturn BN.min(currentBid.price, previousMin);\n\t}, undefined as BN);\n\n\tconst markPrice = calculateMarkPrice(bestBidPrice, bestAskPrice, oraclePrice);\n\n\treturn {\n\t\tbestBidPrice,\n\t\tbestAskPrice,\n\t\tmarkPrice,\n\t};\n};\n\nconst calculateSpreadQuote = (bestBidPrice: BN, bestAskPrice: BN) => {\n\treturn BN.max(bestAskPrice.sub(bestBidPrice), ZERO);\n};\n\nfunction calculateSpreadPct(markPricePrice: BN, spreadQuote: BN) {\n\treturn spreadQuote.muln(100).mul(PERCENTAGE_PRECISION).div(markPricePrice);\n}\n\nconst calculateSpread = (bestBidPrice: BN, bestAskPrice: BN, markPrice: BN) => {\n\tconst spreadQuote = calculateSpreadQuote(bestBidPrice, bestAskPrice);\n\tconst spreadPct = calculateSpreadPct(markPrice, spreadQuote);\n\n\treturn {\n\t\tspreadPct,\n\t\tspreadQuote,\n\t};\n};\n\nexport const calculateSpreadBidAskMark = (\n\tl2: Pick<L2OrderBook, 'bids' | 'asks'>,\n\toraclePrice?: BN\n) => {\n\tif (l2.asks.length === 0 || l2.bids.length === 0) {\n\t\treturn {\n\t\t\tspreadQuote: undefined,\n\t\t\tspreadPct: undefined,\n\t\t\tmarkPrice: undefined,\n\t\t\tbestBidPrice: undefined,\n\t\t\tbestAskPrice: undefined,\n\t\t};\n\t}\n\n\tconst { bestBidPrice, bestAskPrice, markPrice } = calculateBidAskAndmarkPrice(\n\t\tl2,\n\t\toraclePrice\n\t);\n\n\tconst { spreadPct, spreadQuote } = calculateSpread(\n\t\tbestBidPrice,\n\t\tbestAskPrice,\n\t\tmarkPrice\n\t);\n\treturn {\n\t\tbestBidPrice,\n\t\tbestAskPrice,\n\t\tmarkPrice,\n\t\tspreadPct,\n\t\tspreadQuote,\n\t};\n};\n"]}
|
|
@@ -18,7 +18,7 @@ exports.getEmptyFilledBidsAsks = exports.l2ToDisplayBidsAndAsks = exports.format
|
|
|
18
18
|
const sdk_1 = require("@drift-labs/sdk");
|
|
19
19
|
const math_1 = require("../math");
|
|
20
20
|
const types_1 = require("./types");
|
|
21
|
-
const
|
|
21
|
+
const math_2 = require("../math");
|
|
22
22
|
const constants_1 = require("./constants");
|
|
23
23
|
__exportStar(require("./types"), exports);
|
|
24
24
|
__exportStar(require("./constants"), exports);
|
|
@@ -125,7 +125,7 @@ const calculateDynamicSlippageFromL2 = ({ l2Data, marketId, startPrice, worstPri
|
|
|
125
125
|
? { ...DEFAULT_DYNAMIC_SLIPPAGE_CONFIG, ...dynamicSlippageConfig }
|
|
126
126
|
: DEFAULT_DYNAMIC_SLIPPAGE_CONFIG;
|
|
127
127
|
// Calculate spread information from L2 data using the oracle price
|
|
128
|
-
const spreadBidAskMark = math_1.
|
|
128
|
+
const spreadBidAskMark = (0, math_1.calculateSpreadBidAskMark)(l2Data, oraclePrice);
|
|
129
129
|
const bestAskNum = ((_b = (_a = spreadBidAskMark.bestAskPrice) === null || _a === void 0 ? void 0 : _a.toNumber) === null || _b === void 0 ? void 0 : _b.call(_a)) || 0;
|
|
130
130
|
const bestBidNum = ((_d = (_c = spreadBidAskMark.bestBidPrice) === null || _c === void 0 ? void 0 : _c.toNumber) === null || _d === void 0 ? void 0 : _d.call(_c)) || 0;
|
|
131
131
|
const spreadPctFromL2 = sdk_1.BigNum.from(spreadBidAskMark.spreadPct, sdk_1.PERCENTAGE_PRECISION_EXP).toNum();
|
|
@@ -191,7 +191,7 @@ const roundForOrderbook = (num) => Number(num.toFixed(6));
|
|
|
191
191
|
const getBucketFloorForPrice = (price, groupingSize) => {
|
|
192
192
|
const priceIsNegative = price < 0;
|
|
193
193
|
const _groupingSize = groupingSize;
|
|
194
|
-
if (
|
|
194
|
+
if ((0, math_2.dividesExactly)(price, _groupingSize)) {
|
|
195
195
|
return roundForOrderbook(price);
|
|
196
196
|
}
|
|
197
197
|
const amountToDeduct = priceIsNegative
|
|
@@ -221,7 +221,7 @@ const getBucketCeilingForPrice = (price, groupingSize) => {
|
|
|
221
221
|
};
|
|
222
222
|
const getBucketAnchorPrice = (type, price, groupingSize) => {
|
|
223
223
|
// If the grouping size matches exactly then the anchor price should be the same as the floor price regardless
|
|
224
|
-
if (
|
|
224
|
+
if ((0, math_2.dividesExactly)(price, groupingSize)) {
|
|
225
225
|
return (0, exports.getBucketFloorForPrice)(price, groupingSize);
|
|
226
226
|
}
|
|
227
227
|
if (type === 'bid') {
|