@sonarwatch/portfolio-plugins 0.12.37 → 0.12.39

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. package/CHANGELOG.md +1847 -1839
  2. package/README.md +3 -3
  3. package/package.json +1 -1
  4. package/src/plugins/balancer/helpers/pools.js +21 -21
  5. package/src/plugins/drift/constants.d.ts +1 -0
  6. package/src/plugins/drift/constants.js +2 -1
  7. package/src/plugins/drift/constants.js.map +1 -1
  8. package/src/plugins/drift/depositsFetcher.js +63 -1
  9. package/src/plugins/drift/depositsFetcher.js.map +1 -1
  10. package/src/plugins/drift/filters.d.ts +1 -0
  11. package/src/plugins/drift/filters.js +4 -1
  12. package/src/plugins/drift/filters.js.map +1 -1
  13. package/src/plugins/drift/index.js +2 -1
  14. package/src/plugins/drift/index.js.map +1 -1
  15. package/src/plugins/drift/perpHelpers/amm.d.ts +74 -0
  16. package/src/plugins/drift/perpHelpers/amm.js +567 -0
  17. package/src/plugins/drift/perpHelpers/amm.js.map +1 -0
  18. package/src/plugins/drift/perpHelpers/assert.d.ts +1 -0
  19. package/src/plugins/drift/perpHelpers/assert.js +10 -0
  20. package/src/plugins/drift/perpHelpers/assert.js.map +1 -0
  21. package/src/plugins/drift/perpHelpers/constants.d.ts +27 -0
  22. package/src/plugins/drift/perpHelpers/constants.js +35 -0
  23. package/src/plugins/drift/perpHelpers/constants.js.map +1 -0
  24. package/src/plugins/drift/perpHelpers/convert.d.ts +0 -0
  25. package/src/plugins/drift/perpHelpers/convert.js +2 -0
  26. package/src/plugins/drift/perpHelpers/convert.js.map +1 -0
  27. package/src/plugins/drift/perpHelpers/getOraclePrice.d.ts +4 -0
  28. package/src/plugins/drift/perpHelpers/getOraclePrice.js +61 -0
  29. package/src/plugins/drift/perpHelpers/getOraclePrice.js.map +1 -0
  30. package/src/plugins/drift/perpHelpers/getPerpMarket.d.ts +4 -0
  31. package/src/plugins/drift/perpHelpers/getPerpMarket.js +33 -0
  32. package/src/plugins/drift/perpHelpers/getPerpMarket.js.map +1 -0
  33. package/src/plugins/drift/perpHelpers/margin.d.ts +10 -0
  34. package/src/plugins/drift/perpHelpers/margin.js +82 -0
  35. package/src/plugins/drift/perpHelpers/margin.js.map +1 -0
  36. package/src/plugins/drift/perpHelpers/math.d.ts +4 -0
  37. package/src/plugins/drift/perpHelpers/math.js +29 -0
  38. package/src/plugins/drift/perpHelpers/math.js.map +1 -0
  39. package/src/plugins/drift/perpHelpers/oracle.d.ts +6 -0
  40. package/src/plugins/drift/perpHelpers/oracle.js +54 -0
  41. package/src/plugins/drift/perpHelpers/oracle.js.map +1 -0
  42. package/src/plugins/drift/perpHelpers/orders.d.ts +2 -0
  43. package/src/plugins/drift/perpHelpers/orders.js +9 -0
  44. package/src/plugins/drift/perpHelpers/orders.js.map +1 -0
  45. package/src/plugins/drift/perpHelpers/position.d.ts +44 -0
  46. package/src/plugins/drift/perpHelpers/position.js +184 -0
  47. package/src/plugins/drift/perpHelpers/position.js.map +1 -0
  48. package/src/plugins/drift/perpHelpers/prelaunchOracle.d.ts +3 -0
  49. package/src/plugins/drift/perpHelpers/prelaunchOracle.js +20 -0
  50. package/src/plugins/drift/perpHelpers/prelaunchOracle.js.map +1 -0
  51. package/src/plugins/drift/perpHelpers/pyth.d.ts +5 -0
  52. package/src/plugins/drift/perpHelpers/pyth.js +42 -0
  53. package/src/plugins/drift/perpHelpers/pyth.js.map +1 -0
  54. package/src/plugins/drift/perpHelpers/repeg.d.ts +14 -0
  55. package/src/plugins/drift/perpHelpers/repeg.js +162 -0
  56. package/src/plugins/drift/perpHelpers/repeg.js.map +1 -0
  57. package/src/plugins/drift/perpHelpers/switchboard.d.ts +3 -0
  58. package/src/plugins/drift/perpHelpers/switchboard.js +31 -0
  59. package/src/plugins/drift/perpHelpers/switchboard.js.map +1 -0
  60. package/src/plugins/drift/perpHelpers/types.d.ts +28 -0
  61. package/src/plugins/drift/perpHelpers/types.js +19 -0
  62. package/src/plugins/drift/perpHelpers/types.js.map +1 -0
  63. package/src/plugins/drift/prepMarketsJob.d.ts +3 -0
  64. package/src/plugins/drift/prepMarketsJob.js +38 -0
  65. package/src/plugins/drift/prepMarketsJob.js.map +1 -0
  66. package/src/plugins/drift/struct.d.ts +82 -76
  67. package/src/plugins/drift/struct.js +79 -74
  68. package/src/plugins/drift/struct.js.map +1 -1
  69. package/src/plugins/drift/types.d.ts +1 -0
  70. package/src/plugins/jupiter/exchange/perpetualFetcher.js +3 -6
  71. package/src/plugins/jupiter/exchange/perpetualFetcher.js.map +1 -1
  72. package/src/plugins/jupiter/exchange/valueAverageFetcher.js.map +1 -1
  73. package/src/plugins/jupiter/helpers.d.ts +2 -2
  74. package/src/plugins/jupiter/helpers.js.map +1 -1
  75. package/src/plugins/sushiswap/helpers.js +24 -24
  76. package/src/plugins/uniswap-v2/helpers.js +22 -22
  77. package/src/plugins/zeta/airdropFetcher.js +14 -14
  78. package/src/utils/sei/constants.js +8 -8
  79. package/src/utils/sei/getQueryBalanceByOwner.js +4 -4
@@ -0,0 +1,27 @@
1
+ import BigNumber from 'bignumber.js';
2
+ export declare const PRICE_PRECISION_EXP: import("bn.js");
3
+ export declare const PRICE_PRECISION: import("bn.js");
4
+ export declare const PRICE_PRECISION_BIG_NUMBER: BigNumber;
5
+ export declare const PEG_PRECISION_EXP: import("bn.js");
6
+ export declare const PEG_PRECISION: import("bn.js");
7
+ export declare const ZERO: import("bn.js");
8
+ export declare const ONE: import("bn.js");
9
+ export declare const TWO: import("bn.js");
10
+ export declare const TEN: import("bn.js");
11
+ export declare const AMM_RESERVE_PRECISION_EXP: import("bn.js");
12
+ export declare const QUOTE_PRECISION_EXP: import("bn.js");
13
+ export declare const QUOTE_PRECISION: import("bn.js");
14
+ export declare const AMM_RESERVE_PRECISION: import("bn.js");
15
+ export declare const AMM_TO_QUOTE_PRECISION_RATIO: import("bn.js");
16
+ export declare const PRICE_DIV_PEG: import("bn.js");
17
+ export declare const BID_ASK_SPREAD_PRECISION: import("bn.js");
18
+ export declare const PERCENTAGE_PRECISION_EXP: import("bn.js");
19
+ export declare const PERCENTAGE_PRECISION: import("bn.js");
20
+ export declare const DEFAULT_REVENUE_SINCE_LAST_FUNDING_SPREAD_RETREAT: import("bn.js");
21
+ export declare const AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO: import("bn.js");
22
+ export declare const FIVE_MINUTE: import("bn.js");
23
+ export declare const FUNDING_RATE_BUFFER_PRECISION_EXP: import("bn.js");
24
+ export declare const FUNDING_RATE_BUFFER_PRECISION: import("bn.js");
25
+ export declare const SPOT_MARKET_IMF_PRECISION_EXP: import("bn.js");
26
+ export declare const SPOT_MARKET_IMF_PRECISION: import("bn.js");
27
+ export declare const SPOT_MARKET_WEIGHT_PRECISION: import("bn.js");
@@ -0,0 +1,35 @@
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.SPOT_MARKET_WEIGHT_PRECISION = exports.SPOT_MARKET_IMF_PRECISION = exports.SPOT_MARKET_IMF_PRECISION_EXP = exports.FUNDING_RATE_BUFFER_PRECISION = exports.FUNDING_RATE_BUFFER_PRECISION_EXP = exports.FIVE_MINUTE = exports.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO = exports.DEFAULT_REVENUE_SINCE_LAST_FUNDING_SPREAD_RETREAT = exports.PERCENTAGE_PRECISION = exports.PERCENTAGE_PRECISION_EXP = exports.BID_ASK_SPREAD_PRECISION = exports.PRICE_DIV_PEG = exports.AMM_TO_QUOTE_PRECISION_RATIO = exports.AMM_RESERVE_PRECISION = exports.QUOTE_PRECISION = exports.QUOTE_PRECISION_EXP = exports.AMM_RESERVE_PRECISION_EXP = exports.TEN = exports.TWO = exports.ONE = exports.ZERO = exports.PEG_PRECISION = exports.PEG_PRECISION_EXP = exports.PRICE_PRECISION_BIG_NUMBER = exports.PRICE_PRECISION = exports.PRICE_PRECISION_EXP = void 0;
7
+ const bignumber_js_1 = __importDefault(require("bignumber.js"));
8
+ const bn_js_1 = require("bn.js");
9
+ exports.PRICE_PRECISION_EXP = new bn_js_1.BN(6);
10
+ exports.PRICE_PRECISION = new bn_js_1.BN(10).pow(exports.PRICE_PRECISION_EXP);
11
+ exports.PRICE_PRECISION_BIG_NUMBER = new bignumber_js_1.default(exports.PRICE_PRECISION.toString());
12
+ exports.PEG_PRECISION_EXP = new bn_js_1.BN(6);
13
+ exports.PEG_PRECISION = new bn_js_1.BN(10).pow(exports.PEG_PRECISION_EXP);
14
+ exports.ZERO = new bn_js_1.BN(0);
15
+ exports.ONE = new bn_js_1.BN(1);
16
+ exports.TWO = new bn_js_1.BN(2);
17
+ exports.TEN = new bn_js_1.BN(10);
18
+ exports.AMM_RESERVE_PRECISION_EXP = new bn_js_1.BN(9);
19
+ exports.QUOTE_PRECISION_EXP = new bn_js_1.BN(6);
20
+ exports.QUOTE_PRECISION = new bn_js_1.BN(10).pow(exports.QUOTE_PRECISION_EXP);
21
+ exports.AMM_RESERVE_PRECISION = new bn_js_1.BN(10).pow(exports.AMM_RESERVE_PRECISION_EXP);
22
+ exports.AMM_TO_QUOTE_PRECISION_RATIO = exports.AMM_RESERVE_PRECISION.div(exports.QUOTE_PRECISION);
23
+ exports.PRICE_DIV_PEG = exports.PRICE_PRECISION.div(exports.PEG_PRECISION);
24
+ exports.BID_ASK_SPREAD_PRECISION = new bn_js_1.BN(1000000);
25
+ exports.PERCENTAGE_PRECISION_EXP = new bn_js_1.BN(6);
26
+ exports.PERCENTAGE_PRECISION = new bn_js_1.BN(10).pow(exports.PERCENTAGE_PRECISION_EXP);
27
+ exports.DEFAULT_REVENUE_SINCE_LAST_FUNDING_SPREAD_RETREAT = new bn_js_1.BN(-25).mul(exports.QUOTE_PRECISION);
28
+ exports.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO = exports.AMM_RESERVE_PRECISION.mul(exports.PEG_PRECISION).div(exports.QUOTE_PRECISION);
29
+ exports.FIVE_MINUTE = new bn_js_1.BN(60 * 5);
30
+ exports.FUNDING_RATE_BUFFER_PRECISION_EXP = new bn_js_1.BN(3);
31
+ exports.FUNDING_RATE_BUFFER_PRECISION = new bn_js_1.BN(10).pow(exports.FUNDING_RATE_BUFFER_PRECISION_EXP);
32
+ exports.SPOT_MARKET_IMF_PRECISION_EXP = new bn_js_1.BN(6);
33
+ exports.SPOT_MARKET_IMF_PRECISION = new bn_js_1.BN(10).pow(exports.SPOT_MARKET_IMF_PRECISION_EXP);
34
+ exports.SPOT_MARKET_WEIGHT_PRECISION = new bn_js_1.BN(10000);
35
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../../../../../packages/plugins/src/plugins/drift/perpHelpers/constants.ts"],"names":[],"mappings":";;;;;;AAAA,gEAAqC;AACrC,iCAA2B;AAEd,QAAA,mBAAmB,GAAG,IAAI,UAAE,CAAC,CAAC,CAAC,CAAC;AAChC,QAAA,eAAe,GAAG,IAAI,UAAE,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,2BAAmB,CAAC,CAAC;AACtD,QAAA,0BAA0B,GAAG,IAAI,sBAAS,CACrD,uBAAe,CAAC,QAAQ,EAAE,CAC3B,CAAC;AACW,QAAA,iBAAiB,GAAG,IAAI,UAAE,CAAC,CAAC,CAAC,CAAC;AAC9B,QAAA,aAAa,GAAG,IAAI,UAAE,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,yBAAiB,CAAC,CAAC;AAClD,QAAA,IAAI,GAAG,IAAI,UAAE,CAAC,CAAC,CAAC,CAAC;AACjB,QAAA,GAAG,GAAG,IAAI,UAAE,CAAC,CAAC,CAAC,CAAC;AAChB,QAAA,GAAG,GAAG,IAAI,UAAE,CAAC,CAAC,CAAC,CAAC;AAChB,QAAA,GAAG,GAAG,IAAI,UAAE,CAAC,EAAE,CAAC,CAAC;AACjB,QAAA,yBAAyB,GAAG,IAAI,UAAE,CAAC,CAAC,CAAC,CAAC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IAAI,UAAE,CAAC,KAAK,CAAC,CAAC"}
File without changes
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ //# sourceMappingURL=convert.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"convert.js","sourceRoot":"","sources":["../../../../../../../packages/plugins/src/plugins/drift/perpHelpers/convert.ts"],"names":[],"mappings":""}
@@ -0,0 +1,4 @@
1
+ import { SolanaClient } from '../../../utils/clients/types';
2
+ import { OracleSource } from '../struct';
3
+ import { OraclePriceData } from './types';
4
+ export declare function getOraclePrice(oracle: string, oracleSource: OracleSource, connection: SolanaClient): Promise<OraclePriceData>;
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.getOraclePrice = void 0;
13
+ const web3_js_1 = require("@solana/web3.js");
14
+ const bn_js_1 = require("bn.js");
15
+ const struct_1 = require("../struct");
16
+ const pyth_1 = require("./pyth");
17
+ const switchboard_1 = require("./switchboard");
18
+ const prelaunchOracle_1 = require("./prelaunchOracle");
19
+ const oraclePrices = new Map();
20
+ const lastUpdates = new Map();
21
+ const oraclePriceTtl = 90000;
22
+ function getOraclePrice(oracle, oracleSource, connection) {
23
+ return __awaiter(this, void 0, void 0, function* () {
24
+ const lastUpdate = lastUpdates.get(oracle);
25
+ const oPriceData = oraclePrices.get(oracle);
26
+ if (!oPriceData || !lastUpdate || lastUpdate < Date.now() - oraclePriceTtl) {
27
+ const acc = yield connection.getAccountInfo(new web3_js_1.PublicKey(oracle));
28
+ if (!acc)
29
+ throw new Error('Unable to fetch oracle data');
30
+ let coPriceData;
31
+ switch (oracleSource) {
32
+ case struct_1.OracleSource.Pyth:
33
+ coPriceData = (0, pyth_1.getPythOraclePriceDataFromBuffer)(acc.data, new bn_js_1.BN(1), false);
34
+ break;
35
+ case struct_1.OracleSource.Pyth1K:
36
+ coPriceData = (0, pyth_1.getPythOraclePriceDataFromBuffer)(acc.data, new bn_js_1.BN(1000), false);
37
+ break;
38
+ case struct_1.OracleSource.Pyth1M:
39
+ coPriceData = (0, pyth_1.getPythOraclePriceDataFromBuffer)(acc.data, new bn_js_1.BN(1000000), false);
40
+ break;
41
+ case struct_1.OracleSource.PythStableCoin:
42
+ coPriceData = (0, pyth_1.getPythOraclePriceDataFromBuffer)(acc.data, new bn_js_1.BN(1), true);
43
+ break;
44
+ case struct_1.OracleSource.Switchboard:
45
+ coPriceData = (0, switchboard_1.getSwitchboardOraclePriceDataFromBuffer)(acc.data);
46
+ break;
47
+ case struct_1.OracleSource.Prelaunch:
48
+ coPriceData = (0, prelaunchOracle_1.getPreLaunchOraclePriceDataFromBuffer)(acc.data);
49
+ break;
50
+ default:
51
+ throw new Error('Unsupported OracleSource');
52
+ }
53
+ oraclePrices.set(oracle, coPriceData);
54
+ lastUpdates.set(oracle, Date.now());
55
+ return coPriceData;
56
+ }
57
+ return oPriceData;
58
+ });
59
+ }
60
+ exports.getOraclePrice = getOraclePrice;
61
+ //# sourceMappingURL=getOraclePrice.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getOraclePrice.js","sourceRoot":"","sources":["../../../../../../../packages/plugins/src/plugins/drift/perpHelpers/getOraclePrice.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,6CAA4C;AAC5C,iCAA2B;AAE3B,sCAAyC;AAEzC,iCAA0D;AAC1D,+CAAwE;AACxE,uDAA0E;AAE1E,MAAM,YAAY,GAAiC,IAAI,GAAG,EAAE,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EACT,IAAI,CACL,CAAC;oBACF,MAAM;gBACR,KAAK,qBAAY,CAAC,WAAW;oBAC3B,WAAW,GAAG,IAAA,qDAAuC,EAAC,GAAG,CAAC,IAAI,CAAC,CAAC;oBAChE,MAAM;gBACR,KAAK,qBAAY,CAAC,SAAS;oBACzB,WAAW,GAAG,IAAA,uDAAqC,EAAC,GAAG,CAAC,IAAI,CAAC,CAAC;oBAC9D,MAAM;gBACR;oBACE,MAAM,IAAI,KAAK,CAAC,0BAA0B,CAAC,CAAC;YAChD,CAAC;YAED,YAAY,CAAC,GAAG,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;YACtC,WAAW,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;YACpC,OAAO,WAAW,CAAC;QACrB,CAAC;QACD,OAAO,UAAU,CAAC;IACpB,CAAC;CAAA;AAvDD,wCAuDC"}
@@ -0,0 +1,4 @@
1
+ import { SolanaClient } from '../../../utils/clients/types';
2
+ import { PerpMarket } from '../struct';
3
+ import { ParsedAccount } from '../../../utils/solana';
4
+ export declare function getPerpMarket(perpMarketAdd: string, connection: SolanaClient): Promise<ParsedAccount<PerpMarket> | null>;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.getPerpMarket = void 0;
13
+ const web3_js_1 = require("@solana/web3.js");
14
+ const struct_1 = require("../struct");
15
+ const getParsedAccountInfo_1 = require("../../../utils/solana/getParsedAccountInfo");
16
+ const perpMarkets = new Map();
17
+ const lastUpdates = new Map();
18
+ const perpMarketTtl = 3600000; // 1 hour
19
+ function getPerpMarket(perpMarketAdd, connection) {
20
+ return __awaiter(this, void 0, void 0, function* () {
21
+ const lastUpdate = lastUpdates.get(perpMarketAdd);
22
+ const perpMarket = perpMarkets.get(perpMarketAdd);
23
+ if (!perpMarket || !lastUpdate || lastUpdate < Date.now() - perpMarketTtl) {
24
+ const cPerpMarket = yield (0, getParsedAccountInfo_1.getParsedAccountInfo)(connection, struct_1.perpMarketStruct, new web3_js_1.PublicKey(perpMarketAdd));
25
+ perpMarkets.set(perpMarketAdd, cPerpMarket);
26
+ lastUpdates.set(perpMarketAdd, Date.now());
27
+ return cPerpMarket;
28
+ }
29
+ return perpMarket;
30
+ });
31
+ }
32
+ exports.getPerpMarket = getPerpMarket;
33
+ //# sourceMappingURL=getPerpMarket.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getPerpMarket.js","sourceRoot":"","sources":["../../../../../../../packages/plugins/src/plugins/drift/perpHelpers/getPerpMarket.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,6CAA4C;AAE5C,sCAAyD;AACzD,qFAAkF;AAGlF,MAAM,WAAW,GAAkD,IAAI,GAAG,EAAE,CAAC;AAC7E,MAAM,WAAW,GAAwB,IAAI,GAAG,EAAE,CAAC;AACnD,MAAM,aAAa,GAAG,OAAO,CAAC,CAAC,SAAS;AAExC,SAAsB,aAAa,CACjC,aAAqB,EACrB,UAAwB;;QAExB,MAAM,UAAU,GAAG,WAAW,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;QAClD,MAAM,UAAU,GAAG,WAAW,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;QAElD,IAAI,CAAC,UAAU,IAAI,CAAC,UAAU,IAAI,UAAU,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,aAAa,EAAE,CAAC;YAC1E,MAAM,WAAW,GAAG,MAAM,IAAA,2CAAoB,EAC5C,UAAU,EACV,yBAAgB,EAChB,IAAI,mBAAS,CAAC,aAAa,CAAC,CAC7B,CAAC;YAEF,WAAW,CAAC,GAAG,CAAC,aAAa,EAAE,WAAW,CAAC,CAAC;YAC5C,WAAW,CAAC,GAAG,CAAC,aAAa,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;YAC3C,OAAO,WAAW,CAAC;QACrB,CAAC;QACD,OAAO,UAAU,CAAC;IACpB,CAAC;CAAA;AAnBD,sCAmBC"}
@@ -0,0 +1,10 @@
1
+ import BN from 'bn.js';
2
+ import { PerpMarket, PerpPosition } from '../struct';
3
+ import { OraclePriceData } from './types';
4
+ export declare function calculateSizePremiumLiabilityWeight(size: BN, // AMM_RESERVE_PRECISION
5
+ imfFactor: BN, liabilityWeight: BN, precision: BN): BN;
6
+ export declare function calculateSizeDiscountAssetWeight(size: BN, // AMM_RESERVE_PRECISION
7
+ imfFactor: BN, assetWeight: BN): BN;
8
+ export declare function calculateOraclePriceForPerpMargin(perpPosition: PerpPosition, market: PerpMarket, oraclePriceData: OraclePriceData): BN;
9
+ export declare function calculateBaseAssetValueWithOracle(market: PerpMarket, perpPosition: PerpPosition, oraclePriceData: OraclePriceData, includeOpenOrders?: boolean): BN;
10
+ export declare function calculateWorstCaseBaseAssetAmount(perpPosition: PerpPosition): BN;
@@ -0,0 +1,82 @@
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.calculateWorstCaseBaseAssetAmount = exports.calculateBaseAssetValueWithOracle = exports.calculateOraclePriceForPerpMargin = exports.calculateSizeDiscountAssetWeight = exports.calculateSizePremiumLiabilityWeight = void 0;
7
+ const bn_js_1 = __importDefault(require("bn.js"));
8
+ const struct_1 = require("../struct");
9
+ const constants_1 = require("./constants");
10
+ const math_1 = require("./math");
11
+ const assert_1 = require("./assert");
12
+ function calculateSizePremiumLiabilityWeight(size, // AMM_RESERVE_PRECISION
13
+ imfFactor, liabilityWeight, precision) {
14
+ if (imfFactor.eq(constants_1.ZERO)) {
15
+ return liabilityWeight;
16
+ }
17
+ const sizeSqrt = (0, math_1.squareRootBN)(size.abs().mul(new bn_js_1.default(10)).add(new bn_js_1.default(1))); // 1e9 -> 1e10 -> 1e5
18
+ const liabilityWeightNumerator = liabilityWeight.sub(liabilityWeight.div(new bn_js_1.default(5)));
19
+ const denom = new bn_js_1.default(100000).mul(constants_1.SPOT_MARKET_IMF_PRECISION).div(precision);
20
+ (0, assert_1.assert)(denom.gt(constants_1.ZERO));
21
+ const sizePremiumLiabilityWeight = liabilityWeightNumerator.add(sizeSqrt // 1e5
22
+ .mul(imfFactor)
23
+ .div(denom) // 1e5
24
+ );
25
+ const maxLiabilityWeight = bn_js_1.default.max(liabilityWeight, sizePremiumLiabilityWeight);
26
+ return maxLiabilityWeight;
27
+ }
28
+ exports.calculateSizePremiumLiabilityWeight = calculateSizePremiumLiabilityWeight;
29
+ function calculateSizeDiscountAssetWeight(size, // AMM_RESERVE_PRECISION
30
+ imfFactor, assetWeight) {
31
+ if (imfFactor.eq(constants_1.ZERO)) {
32
+ return assetWeight;
33
+ }
34
+ const sizeSqrt = (0, math_1.squareRootBN)(size.abs().mul(new bn_js_1.default(10)).add(new bn_js_1.default(1))); // 1e9 -> 1e10 -> 1e5
35
+ const imfNumerator = constants_1.SPOT_MARKET_IMF_PRECISION.add(constants_1.SPOT_MARKET_IMF_PRECISION.div(new bn_js_1.default(10)));
36
+ const sizeDiscountAssetWeight = imfNumerator
37
+ .mul(constants_1.SPOT_MARKET_WEIGHT_PRECISION)
38
+ .div(constants_1.SPOT_MARKET_IMF_PRECISION.add(sizeSqrt // 1e5
39
+ .mul(imfFactor)
40
+ .div(new bn_js_1.default(100000)) // 1e5
41
+ ));
42
+ const minAssetWeight = bn_js_1.default.min(assetWeight, sizeDiscountAssetWeight);
43
+ return minAssetWeight;
44
+ }
45
+ exports.calculateSizeDiscountAssetWeight = calculateSizeDiscountAssetWeight;
46
+ function calculateOraclePriceForPerpMargin(perpPosition, market, oraclePriceData) {
47
+ const oraclePriceOffset = bn_js_1.default.min(new bn_js_1.default(market.amm.maxSpread)
48
+ .mul(oraclePriceData.price)
49
+ .div(constants_1.BID_ASK_SPREAD_PRECISION), oraclePriceData.confidence.add(new bn_js_1.default(market.amm.baseSpread)
50
+ .mul(oraclePriceData.price)
51
+ .div(constants_1.BID_ASK_SPREAD_PRECISION)));
52
+ let marginPrice;
53
+ if (perpPosition.baseAssetAmount.gt(constants_1.ZERO)) {
54
+ marginPrice = oraclePriceData.price.sub(oraclePriceOffset);
55
+ }
56
+ else {
57
+ marginPrice = oraclePriceData.price.add(oraclePriceOffset);
58
+ }
59
+ return marginPrice;
60
+ }
61
+ exports.calculateOraclePriceForPerpMargin = calculateOraclePriceForPerpMargin;
62
+ function calculateBaseAssetValueWithOracle(market, perpPosition, oraclePriceData, includeOpenOrders = false) {
63
+ let { price } = oraclePriceData;
64
+ if (market.status === struct_1.MarketStatus.Settlement) {
65
+ price = new bn_js_1.default(market.expiryPrice.toString(10), 10);
66
+ }
67
+ const baseAssetAmount = includeOpenOrders
68
+ ? calculateWorstCaseBaseAssetAmount(perpPosition)
69
+ : perpPosition.baseAssetAmount;
70
+ return baseAssetAmount.abs().mul(price).div(constants_1.AMM_RESERVE_PRECISION);
71
+ }
72
+ exports.calculateBaseAssetValueWithOracle = calculateBaseAssetValueWithOracle;
73
+ function calculateWorstCaseBaseAssetAmount(perpPosition) {
74
+ const allBids = perpPosition.baseAssetAmount.add(perpPosition.openBids);
75
+ const allAsks = perpPosition.baseAssetAmount.add(perpPosition.openAsks);
76
+ if (allBids.abs().gt(allAsks.abs())) {
77
+ return allBids;
78
+ }
79
+ return allAsks;
80
+ }
81
+ exports.calculateWorstCaseBaseAssetAmount = calculateWorstCaseBaseAssetAmount;
82
+ //# sourceMappingURL=margin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"margin.js","sourceRoot":"","sources":["../../../../../../../packages/plugins/src/plugins/drift/perpHelpers/margin.ts"],"names":[],"mappings":";;;;;;AAAA,kDAAuB;AACvB,sCAAmE;AAEnE,2CAMqB;AACrB,iCAAsC;AACtC,qCAAkC;AAElC,SAAgB,mCAAmC,CACjD,IAAQ,EAAE,wBAAwB;AAClC,SAAa,EACb,eAAmB,EACnB,SAAa;IAEb,IAAI,SAAS,CAAC,EAAE,CAAC,gBAAI,CAAC,EAAE,CAAC;QACvB,OAAO,eAAe,CAAC;IACzB,CAAC;IAED,MAAM,QAAQ,GAAG,IAAA,mBAAY,EAAC,IAAI,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,IAAI,eAAE,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,eAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,qBAAqB;IAE/F,MAAM,wBAAwB,GAAG,eAAe,CAAC,GAAG,CAClD,eAAe,CAAC,GAAG,CAAC,IAAI,eAAE,CAAC,CAAC,CAAC,CAAC,CAC/B,CAAC;IAEF,MAAM,KAAK,GAAG,IAAI,eAAE,CAAC,MAAO,CAAC,CAAC,GAAG,CAAC,qCAAyB,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;IAC5E,IAAA,eAAM,EAAC,KAAK,CAAC,EAAE,CAAC,gBAAI,CAAC,CAAC,CAAC;IAEvB,MAAM,0BAA0B,GAAG,wBAAwB,CAAC,GAAG,CAC7D,QAAQ,CAAC,MAAM;SACZ,GAAG,CAAC,SAAS,CAAC;SACd,GAAG,CAAC,KAAK,CAAC,CAAC,MAAM;KACrB,CAAC;IAEF,MAAM,kBAAkB,GAAG,eAAE,CAAC,GAAG,CAC/B,eAAe,EACf,0BAA0B,CAC3B,CAAC;IACF,OAAO,kBAAkB,CAAC;AAC5B,CAAC;AA9BD,kFA8BC;AAED,SAAgB,gCAAgC,CAC9C,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AE,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC;QACpC,OAAO,OAAO,CAAC;IACjB,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC;AAVD,8EAUC"}
@@ -0,0 +1,4 @@
1
+ import BN from 'bn.js';
2
+ export declare function clampBN(x: BN, min: BN, max: BN): BN;
3
+ export declare const squareRootBN: (n: BN) => BN;
4
+ export declare const sigNum: (x: BN) => BN;
@@ -0,0 +1,29 @@
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.sigNum = exports.squareRootBN = exports.clampBN = void 0;
7
+ const bn_js_1 = __importDefault(require("bn.js"));
8
+ function clampBN(x, min, max) {
9
+ return bn_js_1.default.max(min, bn_js_1.default.min(x, max));
10
+ }
11
+ exports.clampBN = clampBN;
12
+ const squareRootBN = (n) => {
13
+ if (n.lt(new bn_js_1.default(0))) {
14
+ throw new Error('Sqrt only works on non-negtiave inputs');
15
+ }
16
+ if (n.lt(new bn_js_1.default(2))) {
17
+ return n;
18
+ }
19
+ const smallCand = (0, exports.squareRootBN)(n.shrn(2)).shln(1);
20
+ const largeCand = smallCand.add(new bn_js_1.default(1));
21
+ if (largeCand.mul(largeCand).gt(n)) {
22
+ return smallCand;
23
+ }
24
+ return largeCand;
25
+ };
26
+ exports.squareRootBN = squareRootBN;
27
+ const sigNum = (x) => (x.isNeg() ? new bn_js_1.default(-1) : new bn_js_1.default(1));
28
+ exports.sigNum = sigNum;
29
+ //# sourceMappingURL=math.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"math.js","sourceRoot":"","sources":["../../../../../../../packages/plugins/src/plugins/drift/perpHelpers/math.ts"],"names":[],"mappings":";;;;;;AAAA,kDAAuB;AAEvB,SAAgB,OAAO,CAAC,CAAK,EAAE,GAAO,EAAE,GAAO;IAC7C,OAAO,eAAE,CAAC,GAAG,CAAC,GAAG,EAAE,eAAE,CAAC,GAAG,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC;AACrC,CAAC;AAFD,0BAEC;AAEM,MAAM,YAAY,GAAG,CAAC,CAAK,EAAM,EAAE;IACxC,IAAI,CAAC,CAAC,EAAE,CAAC,IAAI,eAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QACpB,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;IAC5D,CAAC;IACD,IAAI,CAAC,CAAC,EAAE,CAAC,IAAI,eAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QACpB,OAAO,CAAC,CAAC;IACX,CAAC;IAED,MAAM,SAAS,GAAG,IAAA,oBAAY,EAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClD,MAAM,SAAS,GAAG,SAAS,CAAC,GAAG,CAAC,IAAI,eAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IAE3C,IAAI,SAAS,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACnC,OAAO,SAAS,CAAC;IACnB,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC;AAfW,QAAA,YAAY,gBAevB;AAEK,MAAM,MAAM,GAAG,CAAC,CAAK,EAAM,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,IAAI,eAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,eAAE,CAAC,CAAC,CAAC,CAAC,CAAC;AAA7D,QAAA,MAAM,UAAuD"}
@@ -0,0 +1,6 @@
1
+ import BN from 'bn.js';
2
+ import { AMM, HistoricalOracleData } from '../struct';
3
+ import { OraclePriceData } from './types';
4
+ export declare function calculateLiveOracleTwap(histOracleData: HistoricalOracleData, oraclePriceData: OraclePriceData, now: BN, period: BN): BN;
5
+ export declare function calculateLiveOracleStd(amm: AMM, oraclePriceData: OraclePriceData, now: BN): BN;
6
+ export declare function getNewOracleConfPct(amm: AMM, oraclePriceData: OraclePriceData, reservePrice: BN, now: BN): BN;
@@ -0,0 +1,54 @@
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.getNewOracleConfPct = exports.calculateLiveOracleStd = exports.calculateLiveOracleTwap = void 0;
7
+ const bn_js_1 = __importDefault(require("bn.js"));
8
+ const constants_1 = require("./constants");
9
+ function calculateLiveOracleTwap(histOracleData, oraclePriceData, now, period) {
10
+ let oracleTwap;
11
+ if (period.eq(constants_1.FIVE_MINUTE)) {
12
+ oracleTwap = new bn_js_1.default(histOracleData.lastOraclePriceTwap5Min.toString(10), 10);
13
+ }
14
+ else {
15
+ // todo: assumes its fundingPeriod (1hr)
16
+ // period = amm.fundingPeriod;
17
+ oracleTwap = new bn_js_1.default(histOracleData.lastOraclePriceTwap.toString(10), 10);
18
+ }
19
+ const sinceLastUpdate = bn_js_1.default.max(constants_1.ONE, now.sub(new bn_js_1.default(histOracleData.lastOraclePriceTwapTs.toString(10), 10)));
20
+ const sinceStart = bn_js_1.default.max(constants_1.ZERO, period.sub(sinceLastUpdate));
21
+ const clampRange = oracleTwap.div(new bn_js_1.default(3));
22
+ const clampedOraclePrice = bn_js_1.default.min(oracleTwap.add(clampRange), bn_js_1.default.max(oraclePriceData.price, oracleTwap.sub(clampRange)));
23
+ const newOracleTwap = oracleTwap
24
+ .mul(sinceStart)
25
+ .add(clampedOraclePrice.mul(sinceLastUpdate))
26
+ .div(sinceStart.add(sinceLastUpdate));
27
+ return newOracleTwap;
28
+ }
29
+ exports.calculateLiveOracleTwap = calculateLiveOracleTwap;
30
+ function calculateLiveOracleStd(amm, oraclePriceData, now) {
31
+ const sinceLastUpdate = bn_js_1.default.max(constants_1.ONE, now.sub(new bn_js_1.default(amm.historicalOracleData.lastOraclePriceTwapTs.toString(10), 10)));
32
+ const sinceStart = bn_js_1.default.max(constants_1.ZERO, amm.fundingPeriod.sub(sinceLastUpdate));
33
+ const liveOracleTwap = calculateLiveOracleTwap(amm.historicalOracleData, oraclePriceData, now, amm.fundingPeriod);
34
+ const priceDeltaVsTwap = oraclePriceData.price.sub(liveOracleTwap).abs();
35
+ const oracleStd = priceDeltaVsTwap.add(amm.oracleStd.mul(sinceStart).div(sinceStart.add(sinceLastUpdate)));
36
+ return oracleStd;
37
+ }
38
+ exports.calculateLiveOracleStd = calculateLiveOracleStd;
39
+ function getNewOracleConfPct(amm, oraclePriceData, reservePrice, now) {
40
+ const confInterval = oraclePriceData.confidence || constants_1.ZERO;
41
+ const sinceLastUpdate = bn_js_1.default.max(constants_1.ZERO, now.sub(new bn_js_1.default(amm.historicalOracleData.lastOraclePriceTwapTs.toString(10), 10)));
42
+ let lowerBoundConfPct = amm.lastOracleConfPct;
43
+ if (sinceLastUpdate.gt(constants_1.ZERO)) {
44
+ const lowerBoundConfDivisor = bn_js_1.default.max(new bn_js_1.default(21).sub(sinceLastUpdate), new bn_js_1.default(5));
45
+ lowerBoundConfPct = amm.lastOracleConfPct.sub(amm.lastOracleConfPct.div(lowerBoundConfDivisor));
46
+ }
47
+ const confIntervalPct = confInterval
48
+ .mul(constants_1.BID_ASK_SPREAD_PRECISION)
49
+ .div(reservePrice);
50
+ const confIntervalPctResult = bn_js_1.default.max(confIntervalPct, lowerBoundConfPct);
51
+ return confIntervalPctResult;
52
+ }
53
+ exports.getNewOracleConfPct = getNewOracleConfPct;
54
+ //# sourceMappingURL=oracle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"oracle.js","sourceRoot":"","sources":["../../../../../../../packages/plugins/src/plugins/drift/perpHelpers/oracle.ts"],"names":[],"mappings":";;;;;;AAAA,kDAAuB;AAEvB,2CAA+E;AAG/E,SAAgB,uBAAuB,CACrC,cAAoC,EACpC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AAC,EAAE,CAAC,gBAAI,CAAC,EAAE,CAAC;QAC7B,MAAM,qBAAqB,GAAG,eAAE,CAAC,GAAG,CAClC,IAAI,eAAE,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,eAAe,CAAC,EAC/B,IAAI,eAAE,CAAC,CAAC,CAAC,CACV,CAAC;QACF,iBAAiB,GAAG,GAAG,CAAC,iBAAiB,CAAC,GAAG,CAC3C,GAAG,CAAC,iBAAiB,CAAC,GAAG,CAAC,qBAAqB,CAAC,CACjD,CAAC;IACJ,CAAC;IACD,MAAM,eAAe,GAAG,YAAY;SACjC,GAAG,CAAC,oCAAwB,CAAC;SAC7B,GAAG,CAAC,YAAY,CAAC,CAAC;IAErB,MAAM,qBAAqB,GAAG,eAAE,CAAC,GAAG,CAAC,eAAe,EAAE,iBAAiB,CAAC,CAAC;IAEzE,OAAO,qBAAqB,CAAC;AAC/B,CAAC;AA/BD,kDA+BC"}
@@ -0,0 +1,2 @@
1
+ import BN from 'bn.js';
2
+ export declare function standardizeBaseAssetAmount(baseAssetAmount: BN, stepSize: BN): BN;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.standardizeBaseAssetAmount = void 0;
4
+ function standardizeBaseAssetAmount(baseAssetAmount, stepSize) {
5
+ const remainder = baseAssetAmount.mod(stepSize);
6
+ return baseAssetAmount.sub(remainder);
7
+ }
8
+ exports.standardizeBaseAssetAmount = standardizeBaseAssetAmount;
9
+ //# sourceMappingURL=orders.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"orders.js","sourceRoot":"","sources":["../../../../../../../packages/plugins/src/plugins/drift/perpHelpers/orders.ts"],"names":[],"mappings":";;;AAEA,SAAgB,0BAA0B,CACxC,eAAmB,EACnB,QAAY;IAEZ,MAAM,SAAS,GAAG,eAAe,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAChD,OAAO,eAAe,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;AACxC,CAAC;AAND,gEAMC"}
@@ -0,0 +1,44 @@
1
+ import BN from 'bn.js';
2
+ import { PerpMarket, PerpPosition } from '../struct';
3
+ import { OraclePriceData, PositionDirection } from './types';
4
+ export declare function calculateBaseAssetValue(market: PerpMarket, userPosition: PerpPosition, oraclePriceData: OraclePriceData, useSpread?: boolean, skipUpdate?: boolean): BN;
5
+ /**
6
+ * calculatePositionPNL
7
+ * = BaseAssetAmount * (Avg Exit Price - Avg Entry Price)
8
+ * @param market
9
+ * @param PerpPosition
10
+ * @param withFunding (adds unrealized funding payment pnl to result)
11
+ * @param oraclePriceData
12
+ * @returns BaseAssetAmount : Precision QUOTE_PRECISION
13
+ */
14
+ export declare function calculatePositionPNL(market: PerpMarket, perpPosition: PerpPosition, oraclePriceData: OraclePriceData, withFunding?: boolean): BN;
15
+ /**
16
+ *
17
+ * @param market
18
+ * @param PerpPosition
19
+ * @returns // QUOTE_PRECISION
20
+ */
21
+ export declare function calculatePositionFundingPNL(market: PerpMarket, perpPosition: PerpPosition): BN;
22
+ export declare function positionIsAvailable(position: PerpPosition): boolean;
23
+ /**
24
+ *
25
+ * @param userPosition
26
+ * @returns Precision: PRICE_PRECISION (10^6)
27
+ */
28
+ export declare function calculateBreakEvenPrice(userPosition: PerpPosition): BN;
29
+ /**
30
+ *
31
+ * @param userPosition
32
+ * @returns Precision: PRICE_PRECISION (10^6)
33
+ */
34
+ export declare function calculateEntryPrice(userPosition: PerpPosition): BN;
35
+ /**
36
+ *
37
+ * @param userPosition
38
+ * @returns Precision: PRICE_PRECISION (10^10)
39
+ */
40
+ export declare function calculateCostBasis(userPosition: PerpPosition, includeSettledPnl?: boolean): BN;
41
+ export declare function findDirectionToClose(userPosition: PerpPosition): PositionDirection;
42
+ export declare function positionCurrentDirection(userPosition: PerpPosition): PositionDirection;
43
+ export declare function isEmptyPosition(userPosition: PerpPosition): boolean;
44
+ export declare function hasOpenOrders(position: PerpPosition): boolean;