@drift-labs/sdk 0.2.0-master.11 → 0.2.0-master.14

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.
Files changed (114) hide show
  1. package/lib/accounts/fetch.d.ts +2 -1
  2. package/lib/accounts/fetch.js +9 -1
  3. package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
  4. package/lib/accounts/pollingUserStatsAccountSubscriber.js +113 -0
  5. package/lib/accounts/types.d.ts +14 -1
  6. package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
  7. package/lib/accounts/webSocketUserStatsAccountSubsriber.js +47 -0
  8. package/lib/addresses/pda.d.ts +1 -0
  9. package/lib/addresses/pda.js +8 -1
  10. package/lib/admin.d.ts +2 -0
  11. package/lib/admin.js +18 -0
  12. package/lib/clearingHouse.d.ts +24 -3
  13. package/lib/clearingHouse.js +352 -51
  14. package/lib/clearingHouseConfig.d.ts +1 -0
  15. package/lib/clearingHouseUser.d.ts +15 -15
  16. package/lib/clearingHouseUser.js +185 -73
  17. package/lib/clearingHouseUserStats.d.ts +17 -0
  18. package/lib/clearingHouseUserStats.js +36 -0
  19. package/lib/clearingHouseUserStatsConfig.d.ts +14 -0
  20. package/lib/clearingHouseUserStatsConfig.js +2 -0
  21. package/lib/config.js +1 -1
  22. package/lib/constants/banks.d.ts +2 -2
  23. package/lib/constants/banks.js +4 -3
  24. package/lib/constants/numericConstants.d.ts +2 -0
  25. package/lib/constants/numericConstants.js +3 -1
  26. package/lib/events/eventList.js +3 -0
  27. package/lib/events/types.d.ts +2 -1
  28. package/lib/factory/bigNum.d.ts +1 -0
  29. package/lib/factory/bigNum.js +37 -11
  30. package/lib/idl/clearing_house.json +692 -58
  31. package/lib/index.d.ts +1 -0
  32. package/lib/index.js +1 -0
  33. package/lib/math/amm.js +2 -2
  34. package/lib/math/bankBalance.d.ts +7 -1
  35. package/lib/math/bankBalance.js +76 -1
  36. package/lib/math/margin.d.ts +11 -0
  37. package/lib/math/margin.js +72 -0
  38. package/lib/math/market.d.ts +4 -1
  39. package/lib/math/market.js +35 -1
  40. package/lib/math/oracles.d.ts +3 -0
  41. package/lib/math/oracles.js +25 -5
  42. package/lib/math/position.d.ts +8 -0
  43. package/lib/math/position.js +43 -12
  44. package/lib/math/trade.js +2 -2
  45. package/lib/orders.d.ts +1 -2
  46. package/lib/orders.js +2 -77
  47. package/lib/tokenFaucet.d.ts +1 -0
  48. package/lib/tokenFaucet.js +23 -12
  49. package/lib/tx/retryTxSender.js +9 -2
  50. package/lib/types.d.ts +78 -10
  51. package/lib/types.js +12 -0
  52. package/lib/util/getTokenAddress.d.ts +2 -0
  53. package/lib/util/getTokenAddress.js +9 -0
  54. package/package.json +1 -1
  55. package/src/accounts/fetch.ts +27 -2
  56. package/src/accounts/pollingUserStatsAccountSubscriber.ts +172 -0
  57. package/src/accounts/types.ts +18 -0
  58. package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
  59. package/src/addresses/pda.ts +13 -0
  60. package/src/admin.ts +29 -1
  61. package/src/clearingHouse.ts +619 -62
  62. package/src/clearingHouseConfig.ts +1 -0
  63. package/src/clearingHouseUser.ts +317 -105
  64. package/src/clearingHouseUserStats.ts +53 -0
  65. package/src/clearingHouseUserStatsConfig.ts +18 -0
  66. package/src/config.ts +1 -1
  67. package/src/constants/banks.js +42 -0
  68. package/src/constants/banks.ts +6 -3
  69. package/src/constants/markets.js +42 -0
  70. package/src/constants/numericConstants.js +41 -0
  71. package/src/constants/numericConstants.ts +3 -0
  72. package/src/events/eventList.ts +3 -0
  73. package/src/events/types.ts +2 -0
  74. package/src/factory/bigNum.js +37 -11
  75. package/src/factory/bigNum.ts +43 -13
  76. package/src/idl/clearing_house.json +692 -58
  77. package/src/index.ts +1 -0
  78. package/src/math/amm.ts +8 -5
  79. package/src/math/bankBalance.ts +147 -1
  80. package/src/math/margin.ts +124 -0
  81. package/src/math/market.ts +66 -1
  82. package/src/math/oracles.ts +42 -5
  83. package/src/math/position.ts +60 -9
  84. package/src/math/trade.ts +2 -2
  85. package/src/orders.ts +4 -157
  86. package/src/tokenFaucet.js +189 -0
  87. package/src/tokenFaucet.ts +38 -15
  88. package/src/tx/retryTxSender.ts +11 -3
  89. package/src/types.js +12 -1
  90. package/src/types.ts +83 -10
  91. package/src/{accounts/fetch.js → util/computeUnits.js} +11 -13
  92. package/src/util/getTokenAddress.js +9 -0
  93. package/src/util/getTokenAddress.ts +18 -0
  94. package/tests/bn/test.ts +2 -0
  95. package/src/accounts/bulkAccountLoader.js +0 -197
  96. package/src/accounts/bulkUserSubscription.js +0 -33
  97. package/src/accounts/pollingClearingHouseAccountSubscriber.js +0 -311
  98. package/src/accounts/pollingOracleSubscriber.js +0 -93
  99. package/src/accounts/pollingTokenAccountSubscriber.js +0 -90
  100. package/src/accounts/pollingUserAccountSubscriber.js +0 -132
  101. package/src/accounts/types.js +0 -10
  102. package/src/accounts/utils.js +0 -7
  103. package/src/accounts/webSocketAccountSubscriber.js +0 -93
  104. package/src/accounts/webSocketClearingHouseAccountSubscriber.js +0 -233
  105. package/src/accounts/webSocketUserAccountSubscriber.js +0 -62
  106. package/src/addresses/pda.js +0 -104
  107. package/src/index.js +0 -100
  108. package/src/math/bankBalance.js +0 -75
  109. package/src/math/market.js +0 -57
  110. package/src/math/orders.js +0 -110
  111. package/src/math/position.js +0 -140
  112. package/src/mockUSDCFaucet.js +0 -280
  113. package/src/orders.js +0 -134
  114. package/src/tx/retryTxSender.js +0 -188
@@ -0,0 +1,53 @@
1
+ import { ClearingHouse } from './clearingHouse';
2
+ import { PublicKey } from '@solana/web3.js';
3
+ import { DataAndSlot, UserStatsAccountSubscriber } from './accounts/types';
4
+ import { ClearingHouseUserStatsConfig } from './clearingHouseUserStatsConfig';
5
+ import { PollingUserStatsAccountSubscriber } from './accounts/pollingUserStatsAccountSubscriber';
6
+ import { WebSocketUserStatsAccountSubscriber } from './accounts/webSocketUserStatsAccountSubsriber';
7
+ import { UserStatsAccount } from './types';
8
+
9
+ export class ClearingHouseUserStats {
10
+ clearingHouse: ClearingHouse;
11
+ userStatsAccountPublicKey: PublicKey;
12
+ accountSubscriber: UserStatsAccountSubscriber;
13
+ isSubscribed: boolean;
14
+
15
+ public constructor(config: ClearingHouseUserStatsConfig) {
16
+ this.clearingHouse = config.clearingHouse;
17
+ this.userStatsAccountPublicKey = config.userStatsAccountPublicKey;
18
+ if (config.accountSubscription?.type === 'polling') {
19
+ this.accountSubscriber = new PollingUserStatsAccountSubscriber(
20
+ config.clearingHouse.program,
21
+ config.userStatsAccountPublicKey,
22
+ config.accountSubscription.accountLoader
23
+ );
24
+ } else {
25
+ this.accountSubscriber = new WebSocketUserStatsAccountSubscriber(
26
+ config.clearingHouse.program,
27
+ config.userStatsAccountPublicKey
28
+ );
29
+ }
30
+ }
31
+
32
+ public async subscribe(): Promise<boolean> {
33
+ this.isSubscribed = await this.accountSubscriber.subscribe();
34
+ return this.isSubscribed;
35
+ }
36
+
37
+ public async fetchAccounts(): Promise<void> {
38
+ await this.accountSubscriber.fetch();
39
+ }
40
+
41
+ public async unsubscribe(): Promise<void> {
42
+ await this.accountSubscriber.unsubscribe();
43
+ this.isSubscribed = false;
44
+ }
45
+
46
+ public getAccountAndSlot(): DataAndSlot<UserStatsAccount> {
47
+ return this.accountSubscriber.getUserStatsAccountAndSlot();
48
+ }
49
+
50
+ public getAccount(): UserStatsAccount {
51
+ return this.accountSubscriber.getUserStatsAccountAndSlot().data;
52
+ }
53
+ }
@@ -0,0 +1,18 @@
1
+ import { ClearingHouse } from './clearingHouse';
2
+ import { PublicKey } from '@solana/web3.js';
3
+ import { BulkAccountLoader } from './accounts/bulkAccountLoader';
4
+
5
+ export type ClearingHouseUserStatsConfig = {
6
+ accountSubscription?: ClearingHouseUserStatsAccountSubscriptionConfig;
7
+ clearingHouse: ClearingHouse;
8
+ userStatsAccountPublicKey: PublicKey;
9
+ };
10
+
11
+ export type ClearingHouseUserStatsAccountSubscriptionConfig =
12
+ | {
13
+ type: 'websocket';
14
+ }
15
+ | {
16
+ type: 'polling';
17
+ accountLoader: BulkAccountLoader;
18
+ };
package/src/config.ts CHANGED
@@ -28,7 +28,7 @@ export const configs: { [key in DriftEnv]: DriftConfig } = {
28
28
  devnet: {
29
29
  ENV: 'devnet',
30
30
  PYTH_ORACLE_MAPPING_ADDRESS: 'BmA9Z6FjioHJPpjT39QazZyhDRUdZy2ezwx4GiDdE2u2',
31
- CLEARING_HOUSE_PROGRAM_ID: '4oyTJnAQ9FqJj1y9mPytbWsLeeHmBzGYfuFqypwyQvuh',
31
+ CLEARING_HOUSE_PROGRAM_ID: 'D9bW92Maa1yDigJqvabRgr5S5VybPNDB5xxSpQD6mkkV',
32
32
  USDC_MINT_ADDRESS: '8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2',
33
33
  MARKETS: DevnetMarkets,
34
34
  BANKS: DevnetBanks,
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Banks = exports.MainnetBanks = exports.DevnetBanks = exports.WRAPPED_SOL_MINT = void 0;
4
+ const web3_js_1 = require("@solana/web3.js");
5
+ const __1 = require("../");
6
+ exports.WRAPPED_SOL_MINT = new web3_js_1.PublicKey('So11111111111111111111111111111111111111112');
7
+ exports.DevnetBanks = [
8
+ {
9
+ symbol: 'USDC',
10
+ bankIndex: new __1.BN(0),
11
+ oracle: web3_js_1.PublicKey.default,
12
+ oracleSource: __1.OracleSource.QUOTE_ASSET,
13
+ mint: new web3_js_1.PublicKey('8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2'),
14
+ },
15
+ {
16
+ symbol: 'SOL',
17
+ bankIndex: new __1.BN(1),
18
+ oracle: new web3_js_1.PublicKey('J83w4HKfqxwcq3BEMMkPFSppX3gqekLyLJBexebFVkix'),
19
+ oracleSource: __1.OracleSource.PYTH,
20
+ mint: new web3_js_1.PublicKey(exports.WRAPPED_SOL_MINT),
21
+ },
22
+ {
23
+ symbol: 'BTC',
24
+ bankIndex: new __1.BN(2),
25
+ oracle: new web3_js_1.PublicKey('HovQMDrbAgAYPCmHVSrezcSmkMtXSSUsLDFANExrZh2J'),
26
+ oracleSource: __1.OracleSource.PYTH,
27
+ mint: new web3_js_1.PublicKey('3BZPwbcqB5kKScF3TEXxwNfx5ipV13kbRVDvfVp5c6fv'),
28
+ },
29
+ ];
30
+ exports.MainnetBanks = [
31
+ {
32
+ symbol: 'USDC',
33
+ bankIndex: new __1.BN(0),
34
+ oracle: web3_js_1.PublicKey.default,
35
+ oracleSource: __1.OracleSource.QUOTE_ASSET,
36
+ mint: new web3_js_1.PublicKey('EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v'),
37
+ },
38
+ ];
39
+ exports.Banks = {
40
+ devnet: exports.DevnetBanks,
41
+ 'mainnet-beta': exports.MainnetBanks,
42
+ };
@@ -1,6 +1,5 @@
1
- import { BN, OracleSource } from '../';
2
- import { DriftEnv } from '../';
3
1
  import { PublicKey } from '@solana/web3.js';
2
+ import { BN, DriftEnv, OracleSource } from '../';
4
3
 
5
4
  export type BankConfig = {
6
5
  symbol: string;
@@ -10,6 +9,10 @@ export type BankConfig = {
10
9
  oracleSource: OracleSource;
11
10
  };
12
11
 
12
+ export const WRAPPED_SOL_MINT = new PublicKey(
13
+ 'So11111111111111111111111111111111111111112'
14
+ );
15
+
13
16
  export const DevnetBanks: BankConfig[] = [
14
17
  {
15
18
  symbol: 'USDC',
@@ -23,7 +26,7 @@ export const DevnetBanks: BankConfig[] = [
23
26
  bankIndex: new BN(1),
24
27
  oracle: new PublicKey('J83w4HKfqxwcq3BEMMkPFSppX3gqekLyLJBexebFVkix'),
25
28
  oracleSource: OracleSource.PYTH,
26
- mint: new PublicKey('So11111111111111111111111111111111111111112'),
29
+ mint: new PublicKey(WRAPPED_SOL_MINT),
27
30
  },
28
31
  {
29
32
  symbol: 'BTC',
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Markets = exports.MainnetMarkets = exports.DevnetMarkets = void 0;
4
+ const __1 = require("../");
5
+ const web3_js_1 = require("@solana/web3.js");
6
+ exports.DevnetMarkets = [
7
+ {
8
+ fullName: 'Solana',
9
+ category: ['L1', 'Infra'],
10
+ symbol: 'SOL-PERP',
11
+ baseAssetSymbol: 'SOL',
12
+ marketIndex: new __1.BN(0),
13
+ oracle: new web3_js_1.PublicKey('J83w4HKfqxwcq3BEMMkPFSppX3gqekLyLJBexebFVkix'),
14
+ launchTs: 1655751353000,
15
+ oracleSource: __1.OracleSource.PYTH,
16
+ },
17
+ {
18
+ fullName: 'Bitcoin',
19
+ category: ['L1', 'Payment'],
20
+ symbol: 'BTC-PERP',
21
+ baseAssetSymbol: 'BTC',
22
+ marketIndex: new __1.BN(1),
23
+ oracle: new web3_js_1.PublicKey('HovQMDrbAgAYPCmHVSrezcSmkMtXSSUsLDFANExrZh2J'),
24
+ launchTs: 1655751353000,
25
+ oracleSource: __1.OracleSource.PYTH,
26
+ },
27
+ {
28
+ fullName: 'Ethereum',
29
+ category: ['L1', 'Infra'],
30
+ symbol: 'ETH-PERP',
31
+ baseAssetSymbol: 'ETH',
32
+ marketIndex: new __1.BN(2),
33
+ oracle: new web3_js_1.PublicKey('EdVCmQ9FSPcVe5YySXDPCRmc8aDQLKJ9xvYBMZPie1Vw'),
34
+ launchTs: 1637691133472,
35
+ oracleSource: __1.OracleSource.PYTH,
36
+ },
37
+ ];
38
+ exports.MainnetMarkets = [];
39
+ exports.Markets = {
40
+ devnet: exports.DevnetMarkets,
41
+ 'mainnet-beta': [],
42
+ };
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.QUOTE_ASSET_BANK_INDEX = exports.ONE_YEAR = exports.BID_ASK_SPREAD_PRECISION = exports.MARGIN_PRECISION = exports.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO = exports.PRICE_TO_QUOTE_PRECISION = exports.PRICE_DIV_PEG = exports.AMM_TO_QUOTE_PRECISION_RATIO = exports.BASE_PRECISION_EXP = exports.BASE_PRECISION = exports.AMM_RESERVE_PRECISION = exports.PEG_PRECISION = exports.FUNDING_PAYMENT_PRECISION = exports.MARK_PRICE_PRECISION = exports.QUOTE_PRECISION = exports.LIQUIDATION_FEE_PRECISION = exports.BANK_BALANCE_PRECISION = exports.BANK_BALANCE_PRECISION_EXP = exports.BANK_WEIGHT_PRECISION = exports.BANK_RATE_PRECISION = exports.BANK_UTILIZATION_PRECISION = exports.BANK_CUMULATIVE_INTEREST_PRECISION = exports.BANK_INTEREST_PRECISION = exports.AMM_RESERVE_PRECISION_EXP = exports.PEG_PRECISION_EXP = exports.FUNDING_RATE_PRECISION_EXP = exports.MARK_PRICE_PRECISION_EXP = exports.FUNDING_PAYMENT_PRECISION_EXP = exports.QUOTE_PRECISION_EXP = exports.MAX_LEVERAGE = exports.TEN_MILLION = exports.BN_MAX = exports.TEN_THOUSAND = exports.TEN = exports.TWO = exports.ONE = exports.ZERO = void 0;
4
+ const __1 = require("../");
5
+ exports.ZERO = new __1.BN(0);
6
+ exports.ONE = new __1.BN(1);
7
+ exports.TWO = new __1.BN(2);
8
+ exports.TEN = new __1.BN(10);
9
+ exports.TEN_THOUSAND = new __1.BN(10000);
10
+ exports.BN_MAX = new __1.BN(Number.MAX_SAFE_INTEGER);
11
+ exports.TEN_MILLION = exports.TEN_THOUSAND.mul(exports.TEN_THOUSAND);
12
+ exports.MAX_LEVERAGE = new __1.BN(5);
13
+ exports.QUOTE_PRECISION_EXP = new __1.BN(6);
14
+ exports.FUNDING_PAYMENT_PRECISION_EXP = new __1.BN(4);
15
+ exports.MARK_PRICE_PRECISION_EXP = new __1.BN(10);
16
+ exports.FUNDING_RATE_PRECISION_EXP = exports.MARK_PRICE_PRECISION_EXP.mul(exports.FUNDING_PAYMENT_PRECISION_EXP);
17
+ exports.PEG_PRECISION_EXP = new __1.BN(3);
18
+ exports.AMM_RESERVE_PRECISION_EXP = new __1.BN(13);
19
+ exports.BANK_INTEREST_PRECISION = new __1.BN(1000000);
20
+ exports.BANK_CUMULATIVE_INTEREST_PRECISION = new __1.BN(10000000000);
21
+ exports.BANK_UTILIZATION_PRECISION = new __1.BN(1000000);
22
+ exports.BANK_RATE_PRECISION = new __1.BN(1000000);
23
+ exports.BANK_WEIGHT_PRECISION = new __1.BN(100);
24
+ exports.BANK_BALANCE_PRECISION_EXP = new __1.BN(6);
25
+ exports.BANK_BALANCE_PRECISION = new __1.BN(10).pow(exports.BANK_BALANCE_PRECISION_EXP);
26
+ exports.LIQUIDATION_FEE_PRECISION = new __1.BN(1000000);
27
+ exports.QUOTE_PRECISION = new __1.BN(10).pow(exports.QUOTE_PRECISION_EXP);
28
+ exports.MARK_PRICE_PRECISION = new __1.BN(10).pow(exports.MARK_PRICE_PRECISION_EXP);
29
+ exports.FUNDING_PAYMENT_PRECISION = new __1.BN(10).pow(exports.FUNDING_PAYMENT_PRECISION_EXP);
30
+ exports.PEG_PRECISION = new __1.BN(10).pow(exports.PEG_PRECISION_EXP);
31
+ exports.AMM_RESERVE_PRECISION = new __1.BN(10).pow(exports.AMM_RESERVE_PRECISION_EXP);
32
+ exports.BASE_PRECISION = exports.AMM_RESERVE_PRECISION;
33
+ exports.BASE_PRECISION_EXP = exports.AMM_RESERVE_PRECISION_EXP;
34
+ exports.AMM_TO_QUOTE_PRECISION_RATIO = exports.AMM_RESERVE_PRECISION.div(exports.QUOTE_PRECISION); // 10^7
35
+ exports.PRICE_DIV_PEG = exports.MARK_PRICE_PRECISION.div(exports.PEG_PRECISION); //10^7
36
+ exports.PRICE_TO_QUOTE_PRECISION = exports.MARK_PRICE_PRECISION.div(exports.QUOTE_PRECISION);
37
+ exports.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO = exports.AMM_RESERVE_PRECISION.mul(exports.PEG_PRECISION).div(exports.QUOTE_PRECISION); // 10^10
38
+ exports.MARGIN_PRECISION = exports.TEN_THOUSAND;
39
+ exports.BID_ASK_SPREAD_PRECISION = new __1.BN(1000000);
40
+ exports.ONE_YEAR = new __1.BN(31536000);
41
+ exports.QUOTE_ASSET_BANK_INDEX = new __1.BN(0);
@@ -28,6 +28,9 @@ export const BANK_BALANCE_PRECISION_EXP = new BN(6);
28
28
  export const BANK_BALANCE_PRECISION = new BN(10).pow(
29
29
  BANK_BALANCE_PRECISION_EXP
30
30
  );
31
+ export const BANK_IMF_PRECISION_EXP = new BN(6);
32
+
33
+ export const BANK_IMF_PRECISION = new BN(10).pow(BANK_IMF_PRECISION_EXP);
31
34
  export const LIQUIDATION_FEE_PRECISION = new BN(1000000);
32
35
 
33
36
  export const QUOTE_PRECISION = new BN(10).pow(QUOTE_PRECISION_EXP);
@@ -53,7 +53,10 @@ export class EventList<Type extends EventType> {
53
53
  newNode.next = currentNode.next;
54
54
  if (currentNode.next !== undefined) {
55
55
  newNode.next.prev = newNode;
56
+ } else {
57
+ this.tail = newNode;
56
58
  }
59
+
57
60
  currentNode.next = newNode;
58
61
  newNode.prev = currentNode;
59
62
  }
@@ -5,6 +5,7 @@ import {
5
5
  FundingRateRecord,
6
6
  LiquidationRecord,
7
7
  OrderRecord,
8
+ SettlePnlRecord,
8
9
  } from '../index';
9
10
 
10
11
  export type EventSubscriptionOptions = {
@@ -59,6 +60,7 @@ export type EventMap = {
59
60
  LiquidationRecord: Event<LiquidationRecord>;
60
61
  FundingRateRecord: Event<FundingRateRecord>;
61
62
  OrderRecord: Event<OrderRecord>;
63
+ SettlePnlRecord: Event<SettlePnlRecord>;
62
64
  };
63
65
 
64
66
  export type EventType = keyof EventMap;
@@ -10,6 +10,9 @@ class BigNum {
10
10
  this.val = new anchor_1.BN(val);
11
11
  this.precision = new anchor_1.BN(precisionVal);
12
12
  }
13
+ bigNumFromParam(bn) {
14
+ return anchor_1.BN.isBN(bn) ? BigNum.from(bn) : bn;
15
+ }
13
16
  add(bn) {
14
17
  assert_1.assert(bn.precision.eq(this.precision), 'Adding unequal precisions');
15
18
  return BigNum.from(this.val.add(bn.val), this.precision);
@@ -19,7 +22,7 @@ class BigNum {
19
22
  return BigNum.from(this.val.sub(bn.val), this.precision);
20
23
  }
21
24
  mul(bn) {
22
- const mulVal = bn instanceof BigNum ? bn : BigNum.from(bn);
25
+ const mulVal = this.bigNumFromParam(bn);
23
26
  return BigNum.from(this.val.mul(mulVal.val), this.precision.add(mulVal.precision));
24
27
  }
25
28
  /**
@@ -28,12 +31,12 @@ class BigNum {
28
31
  * @returns
29
32
  */
30
33
  scalarMul(bn) {
31
- if (bn instanceof anchor_1.BN)
34
+ if (anchor_1.BN.isBN(bn))
32
35
  return BigNum.from(this.val.mul(bn), this.precision);
33
36
  return BigNum.from(this.val.mul(bn.val), this.precision.add(bn.precision)).shift(bn.precision.neg());
34
37
  }
35
38
  div(bn) {
36
- if (bn instanceof anchor_1.BN)
39
+ if (anchor_1.BN.isBN(bn))
37
40
  return BigNum.from(this.val.div(bn), this.precision);
38
41
  return BigNum.from(this.val.div(bn.val), this.precision.sub(bn.precision));
39
42
  }
@@ -73,35 +76,35 @@ class BigNum {
73
76
  .toPrecision(precision);
74
77
  }
75
78
  gt(bn, ignorePrecision) {
76
- const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
79
+ const comparisonVal = this.bigNumFromParam(bn);
77
80
  if (!ignorePrecision && !comparisonVal.eq(numericConstants_1.ZERO)) {
78
81
  assert_1.assert(comparisonVal.precision.eq(this.precision), 'Trying to compare numbers with different precision. Yo can opt to ignore precision using the ignorePrecision parameter');
79
82
  }
80
83
  return this.val.gt(comparisonVal.val);
81
84
  }
82
85
  lt(bn, ignorePrecision) {
83
- const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
86
+ const comparisonVal = this.bigNumFromParam(bn);
84
87
  if (!ignorePrecision && !comparisonVal.val.eq(numericConstants_1.ZERO)) {
85
88
  assert_1.assert(comparisonVal.precision.eq(this.precision), 'Trying to compare numbers with different precision. Yo can opt to ignore precision using the ignorePrecision parameter');
86
89
  }
87
90
  return this.val.lt(comparisonVal.val);
88
91
  }
89
92
  gte(bn, ignorePrecision) {
90
- const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
93
+ const comparisonVal = this.bigNumFromParam(bn);
91
94
  if (!ignorePrecision && !comparisonVal.val.eq(numericConstants_1.ZERO)) {
92
95
  assert_1.assert(comparisonVal.precision.eq(this.precision), 'Trying to compare numbers with different precision. Yo can opt to ignore precision using the ignorePrecision parameter');
93
96
  }
94
97
  return this.val.gte(comparisonVal.val);
95
98
  }
96
99
  lte(bn, ignorePrecision) {
97
- const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
100
+ const comparisonVal = this.bigNumFromParam(bn);
98
101
  if (!ignorePrecision && !comparisonVal.val.eq(numericConstants_1.ZERO)) {
99
102
  assert_1.assert(comparisonVal.precision.eq(this.precision), 'Trying to compare numbers with different precision. Yo can opt to ignore precision using the ignorePrecision parameter');
100
103
  }
101
104
  return this.val.lte(comparisonVal.val);
102
105
  }
103
106
  eq(bn, ignorePrecision) {
104
- const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
107
+ const comparisonVal = this.bigNumFromParam(bn);
105
108
  if (!ignorePrecision && !comparisonVal.val.eq(numericConstants_1.ZERO)) {
106
109
  assert_1.assert(comparisonVal.precision.eq(this.precision), 'Trying to compare numbers with different precision. Yo can opt to ignore precision using the ignorePrecision parameter');
107
110
  }
@@ -149,8 +152,14 @@ class BigNum {
149
152
  // remove leading zeroes
150
153
  printString = printString.replace(/^0+/, '');
151
154
  // add zero if leading delim
152
- if (printString[0] === BigNum.delim)
153
- printString = `0${printString}`;
155
+ if (this.isNeg()) {
156
+ if (printString[1] === BigNum.delim)
157
+ printString = printString.replace('-.', '-0.');
158
+ }
159
+ else {
160
+ if (printString[0] === BigNum.delim)
161
+ printString = `0${printString}`;
162
+ }
154
163
  // remove trailing delim
155
164
  if (printString[printString.length - 1] === BigNum.delim)
156
165
  printString = printString.slice(0, printString.length - 1);
@@ -261,10 +270,27 @@ class BigNum {
261
270
  * @returns
262
271
  */
263
272
  toNotional(useTradePrecision, precisionOverride) {
273
+ var _a;
264
274
  const prefix = `${this.lt(BigNum.zero()) ? `-` : ``}$`;
265
- const val = useTradePrecision || precisionOverride
275
+ const usingCustomPrecision = true && (useTradePrecision || precisionOverride);
276
+ let val = usingCustomPrecision
266
277
  ? this.prettyPrint(useTradePrecision, precisionOverride)
267
278
  : BigNum.fromPrint(this.toFixed(2), new anchor_1.BN(2)).prettyPrint();
279
+ // Append two trailing zeroes if not using custom precision
280
+ if (!usingCustomPrecision) {
281
+ const [_, rightSide] = val.split(BigNum.delim);
282
+ const trailingLength = (_a = rightSide === null || rightSide === void 0 ? void 0 : rightSide.length) !== null && _a !== void 0 ? _a : 0;
283
+ if (trailingLength < 2) {
284
+ const numHasDecimals = this.print().includes(BigNum.delim);
285
+ // Handle case where pretty print won't include the decimal point
286
+ if (trailingLength === 0 && numHasDecimals) {
287
+ val = `${val}.00`;
288
+ }
289
+ else {
290
+ val = `${val}${new Array(2 - trailingLength).fill('0').join('')}`;
291
+ }
292
+ }
293
+ }
268
294
  return `${prefix}${val.replace('-', '')}`;
269
295
  }
270
296
  toMillified(precision = 3) {
@@ -17,6 +17,10 @@ export class BigNum {
17
17
  this.precision = new BN(precisionVal);
18
18
  }
19
19
 
20
+ private bigNumFromParam(bn: BigNum | BN) {
21
+ return BN.isBN(bn) ? BigNum.from(bn) : bn;
22
+ }
23
+
20
24
  public add(bn: BigNum): BigNum {
21
25
  assert(bn.precision.eq(this.precision), 'Adding unequal precisions');
22
26
 
@@ -30,7 +34,7 @@ export class BigNum {
30
34
  }
31
35
 
32
36
  public mul(bn: BigNum | BN): BigNum {
33
- const mulVal = bn instanceof BigNum ? bn : BigNum.from(bn);
37
+ const mulVal = this.bigNumFromParam(bn);
34
38
 
35
39
  return BigNum.from(
36
40
  this.val.mul(mulVal.val),
@@ -44,7 +48,7 @@ export class BigNum {
44
48
  * @returns
45
49
  */
46
50
  public scalarMul(bn: BigNum | BN): BigNum {
47
- if (bn instanceof BN) return BigNum.from(this.val.mul(bn), this.precision);
51
+ if (BN.isBN(bn)) return BigNum.from(this.val.mul(bn), this.precision);
48
52
 
49
53
  return BigNum.from(
50
54
  this.val.mul(bn.val),
@@ -53,7 +57,8 @@ export class BigNum {
53
57
  }
54
58
 
55
59
  public div(bn: BigNum | BN): BigNum {
56
- if (bn instanceof BN) return BigNum.from(this.val.div(bn), this.precision);
60
+ if (BN.isBN(bn)) return BigNum.from(this.val.div(bn), this.precision);
61
+
57
62
  return BigNum.from(this.val.div(bn.val), this.precision.sub(bn.precision));
58
63
  }
59
64
 
@@ -101,7 +106,7 @@ export class BigNum {
101
106
  }
102
107
 
103
108
  public gt(bn: BigNum | BN, ignorePrecision?: boolean): boolean {
104
- const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
109
+ const comparisonVal = this.bigNumFromParam(bn);
105
110
 
106
111
  if (!ignorePrecision && !comparisonVal.eq(ZERO)) {
107
112
  assert(
@@ -114,7 +119,7 @@ export class BigNum {
114
119
  }
115
120
 
116
121
  public lt(bn: BigNum | BN, ignorePrecision?: boolean): boolean {
117
- const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
122
+ const comparisonVal = this.bigNumFromParam(bn);
118
123
 
119
124
  if (!ignorePrecision && !comparisonVal.val.eq(ZERO)) {
120
125
  assert(
@@ -127,7 +132,7 @@ export class BigNum {
127
132
  }
128
133
 
129
134
  public gte(bn: BigNum | BN, ignorePrecision?: boolean): boolean {
130
- const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
135
+ const comparisonVal = this.bigNumFromParam(bn);
131
136
 
132
137
  if (!ignorePrecision && !comparisonVal.val.eq(ZERO)) {
133
138
  assert(
@@ -140,7 +145,7 @@ export class BigNum {
140
145
  }
141
146
 
142
147
  public lte(bn: BigNum | BN, ignorePrecision?: boolean): boolean {
143
- const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
148
+ const comparisonVal = this.bigNumFromParam(bn);
144
149
 
145
150
  if (!ignorePrecision && !comparisonVal.val.eq(ZERO)) {
146
151
  assert(
@@ -153,7 +158,7 @@ export class BigNum {
153
158
  }
154
159
 
155
160
  public eq(bn: BigNum | BN, ignorePrecision?: boolean): boolean {
156
- const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
161
+ const comparisonVal = this.bigNumFromParam(bn);
157
162
 
158
163
  if (!ignorePrecision && !comparisonVal.val.eq(ZERO)) {
159
164
  assert(
@@ -225,7 +230,12 @@ export class BigNum {
225
230
  printString = printString.replace(/^0+/, '');
226
231
 
227
232
  // add zero if leading delim
228
- if (printString[0] === BigNum.delim) printString = `0${printString}`;
233
+ if (this.isNeg()) {
234
+ if (printString[1] === BigNum.delim)
235
+ printString = printString.replace('-.', '-0.');
236
+ } else {
237
+ if (printString[0] === BigNum.delim) printString = `0${printString}`;
238
+ }
229
239
 
230
240
  // remove trailing delim
231
241
  if (printString[printString.length - 1] === BigNum.delim)
@@ -392,10 +402,30 @@ export class BigNum {
392
402
  ): string {
393
403
  const prefix = `${this.lt(BigNum.zero()) ? `-` : ``}$`;
394
404
 
395
- const val =
396
- useTradePrecision || precisionOverride
397
- ? this.prettyPrint(useTradePrecision, precisionOverride)
398
- : BigNum.fromPrint(this.toFixed(2), new BN(2)).prettyPrint();
405
+ const usingCustomPrecision =
406
+ true && (useTradePrecision || precisionOverride);
407
+
408
+ let val = usingCustomPrecision
409
+ ? this.prettyPrint(useTradePrecision, precisionOverride)
410
+ : BigNum.fromPrint(this.toFixed(2), new BN(2)).prettyPrint();
411
+
412
+ // Append two trailing zeroes if not using custom precision
413
+ if (!usingCustomPrecision) {
414
+ const [_, rightSide] = val.split(BigNum.delim);
415
+
416
+ const trailingLength = rightSide?.length ?? 0;
417
+
418
+ if (trailingLength < 2) {
419
+ const numHasDecimals = this.print().includes(BigNum.delim);
420
+
421
+ // Handle case where pretty print won't include the decimal point
422
+ if (trailingLength === 0 && numHasDecimals) {
423
+ val = `${val}.00`;
424
+ } else {
425
+ val = `${val}${new Array(2 - trailingLength).fill('0').join('')}`;
426
+ }
427
+ }
428
+ }
399
429
 
400
430
  return `${prefix}${val.replace('-', '')}`;
401
431
  }