@glamsystems/glam-sdk 0.1.20 → 0.1.21

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/index.esm.js CHANGED
@@ -5,7 +5,6 @@ import { getExtensionData, ExtensionType, TOKEN_PROGRAM_ID, getAssociatedTokenAd
5
5
  import { unpack } from '@solana/spl-token-metadata';
6
6
  import DLMM, { binIdToBinArrayIndex, deriveBinArray, Strategy } from '@meteora-ag/dlmm';
7
7
  import { bs58 } from '@coral-xyz/anchor/dist/cjs/utils/bytes';
8
- import { getUserAccountPublicKeySync, getUserStatsAccountPublicKey, decodeUser, MarketType, getDriftStateAccountPublicKey } from '@drift-labs/sdk';
9
8
  import { Marinade } from '@marinade.finance/marinade-ts-sdk';
10
9
  import { getStakePoolAccount } from '@solana/spl-stake-pool';
11
10
  import * as borsh from '@coral-xyz/borsh';
@@ -9195,16 +9194,21 @@ var errors = [
9195
9194
  },
9196
9195
  {
9197
9196
  code: 50004,
9197
+ name: "InvalidPlatformFeeForSwap",
9198
+ msg: "Invalid platform fee"
9199
+ },
9200
+ {
9201
+ code: 50005,
9198
9202
  name: "InvalidTokenAccount",
9199
9203
  msg: "Invalid token account"
9200
9204
  },
9201
9205
  {
9202
- code: 50005,
9206
+ code: 50006,
9203
9207
  name: "InvalidVoteSide",
9204
9208
  msg: "Invalid vote side"
9205
9209
  },
9206
9210
  {
9207
- code: 50006,
9211
+ code: 50007,
9208
9212
  name: "MultipleStakeAccountsDisallowed",
9209
9213
  msg: "Multiple stake accounts disallowed"
9210
9214
  },
@@ -12908,6 +12912,13 @@ const getErrorFromRPCResponse = (rpcResponse)=>{
12908
12912
  throw Error(error.toString());
12909
12913
  }
12910
12914
  };
12915
+ const setsAreEqual = (a, b)=>{
12916
+ if (a.size !== b.size) return false;
12917
+ for (let item of a){
12918
+ if (!b.has(item)) return false;
12919
+ }
12920
+ return true;
12921
+ };
12911
12922
 
12912
12923
  const ASSETS_MAINNET = new Map([
12913
12924
  [
@@ -13577,7 +13588,901 @@ class BaseClient {
13577
13588
  }
13578
13589
  }
13579
13590
 
13580
- const DRIFT_VAULT = new PublicKey("JCNCMFXo5M5qwUPg2Utu1u6YWp3MbygxqBsBeXXJfrw");
13591
+ const ZERO = new BN(0);
13592
+ // # Utility Types / Enums / Constants
13593
+ var ExchangeStatus = /*#__PURE__*/ function(ExchangeStatus) {
13594
+ ExchangeStatus[ExchangeStatus["ACTIVE"] = 0] = "ACTIVE";
13595
+ ExchangeStatus[ExchangeStatus["DEPOSIT_PAUSED"] = 1] = "DEPOSIT_PAUSED";
13596
+ ExchangeStatus[ExchangeStatus["WITHDRAW_PAUSED"] = 2] = "WITHDRAW_PAUSED";
13597
+ ExchangeStatus[ExchangeStatus["AMM_PAUSED"] = 4] = "AMM_PAUSED";
13598
+ ExchangeStatus[ExchangeStatus["FILL_PAUSED"] = 8] = "FILL_PAUSED";
13599
+ ExchangeStatus[ExchangeStatus["LIQ_PAUSED"] = 16] = "LIQ_PAUSED";
13600
+ ExchangeStatus[ExchangeStatus["FUNDING_PAUSED"] = 32] = "FUNDING_PAUSED";
13601
+ ExchangeStatus[ExchangeStatus["SETTLE_PNL_PAUSED"] = 64] = "SETTLE_PNL_PAUSED";
13602
+ ExchangeStatus[ExchangeStatus["AMM_IMMEDIATE_FILL_PAUSED"] = 128] = "AMM_IMMEDIATE_FILL_PAUSED";
13603
+ ExchangeStatus[ExchangeStatus["PAUSED"] = 255] = "PAUSED";
13604
+ return ExchangeStatus;
13605
+ }({});
13606
+ class MarketStatus {
13607
+ }
13608
+ MarketStatus.INITIALIZED = {
13609
+ initialized: {}
13610
+ };
13611
+ MarketStatus.ACTIVE = {
13612
+ active: {}
13613
+ };
13614
+ MarketStatus.FUNDING_PAUSED = {
13615
+ fundingPaused: {}
13616
+ };
13617
+ MarketStatus.AMM_PAUSED = {
13618
+ ammPaused: {}
13619
+ };
13620
+ MarketStatus.FILL_PAUSED = {
13621
+ fillPaused: {}
13622
+ };
13623
+ MarketStatus.WITHDRAW_PAUSED = {
13624
+ withdrawPaused: {}
13625
+ };
13626
+ MarketStatus.REDUCE_ONLY = {
13627
+ reduceOnly: {}
13628
+ };
13629
+ MarketStatus.SETTLEMENT = {
13630
+ settlement: {}
13631
+ };
13632
+ MarketStatus.DELISTED = {
13633
+ delisted: {}
13634
+ };
13635
+ var PerpOperation = /*#__PURE__*/ function(PerpOperation) {
13636
+ PerpOperation[PerpOperation["UPDATE_FUNDING"] = 1] = "UPDATE_FUNDING";
13637
+ PerpOperation[PerpOperation["AMM_FILL"] = 2] = "AMM_FILL";
13638
+ PerpOperation[PerpOperation["FILL"] = 4] = "FILL";
13639
+ PerpOperation[PerpOperation["SETTLE_PNL"] = 8] = "SETTLE_PNL";
13640
+ PerpOperation[PerpOperation["SETTLE_PNL_WITH_POSITION"] = 16] = "SETTLE_PNL_WITH_POSITION";
13641
+ PerpOperation[PerpOperation["LIQUIDATION"] = 32] = "LIQUIDATION";
13642
+ return PerpOperation;
13643
+ }({});
13644
+ var SpotOperation = /*#__PURE__*/ function(SpotOperation) {
13645
+ SpotOperation[SpotOperation["UPDATE_CUMULATIVE_INTEREST"] = 1] = "UPDATE_CUMULATIVE_INTEREST";
13646
+ SpotOperation[SpotOperation["FILL"] = 2] = "FILL";
13647
+ SpotOperation[SpotOperation["DEPOSIT"] = 4] = "DEPOSIT";
13648
+ SpotOperation[SpotOperation["WITHDRAW"] = 8] = "WITHDRAW";
13649
+ SpotOperation[SpotOperation["LIQUIDATION"] = 16] = "LIQUIDATION";
13650
+ return SpotOperation;
13651
+ }({});
13652
+ var InsuranceFundOperation = /*#__PURE__*/ function(InsuranceFundOperation) {
13653
+ InsuranceFundOperation[InsuranceFundOperation["INIT"] = 1] = "INIT";
13654
+ InsuranceFundOperation[InsuranceFundOperation["ADD"] = 2] = "ADD";
13655
+ InsuranceFundOperation[InsuranceFundOperation["REQUEST_REMOVE"] = 4] = "REQUEST_REMOVE";
13656
+ InsuranceFundOperation[InsuranceFundOperation["REMOVE"] = 8] = "REMOVE";
13657
+ return InsuranceFundOperation;
13658
+ }({});
13659
+ var UserStatus = /*#__PURE__*/ function(UserStatus) {
13660
+ UserStatus[UserStatus["BEING_LIQUIDATED"] = 1] = "BEING_LIQUIDATED";
13661
+ UserStatus[UserStatus["BANKRUPT"] = 2] = "BANKRUPT";
13662
+ UserStatus[UserStatus["REDUCE_ONLY"] = 4] = "REDUCE_ONLY";
13663
+ UserStatus[UserStatus["ADVANCED_LP"] = 8] = "ADVANCED_LP";
13664
+ UserStatus[UserStatus["PROTECTED_MAKER"] = 16] = "PROTECTED_MAKER";
13665
+ return UserStatus;
13666
+ }({});
13667
+ class MarginMode {
13668
+ }
13669
+ MarginMode.DEFAULT = {
13670
+ default: {}
13671
+ };
13672
+ MarginMode.HIGH_LEVERAGE = {
13673
+ highLeverage: {}
13674
+ };
13675
+ class ContractType {
13676
+ }
13677
+ ContractType.PERPETUAL = {
13678
+ perpetual: {}
13679
+ };
13680
+ ContractType.FUTURE = {
13681
+ future: {}
13682
+ };
13683
+ ContractType.PREDICTION = {
13684
+ prediction: {}
13685
+ };
13686
+ class ContractTier {
13687
+ }
13688
+ ContractTier.A = {
13689
+ a: {}
13690
+ };
13691
+ ContractTier.B = {
13692
+ b: {}
13693
+ };
13694
+ ContractTier.C = {
13695
+ c: {}
13696
+ };
13697
+ ContractTier.SPECULATIVE = {
13698
+ speculative: {}
13699
+ };
13700
+ ContractTier.HIGHLY_SPECULATIVE = {
13701
+ highlySpeculative: {}
13702
+ };
13703
+ ContractTier.ISOLATED = {
13704
+ isolated: {}
13705
+ };
13706
+ class AssetTier {
13707
+ }
13708
+ AssetTier.COLLATERAL = {
13709
+ collateral: {}
13710
+ };
13711
+ AssetTier.PROTECTED = {
13712
+ protected: {}
13713
+ };
13714
+ AssetTier.CROSS = {
13715
+ cross: {}
13716
+ };
13717
+ AssetTier.ISOLATED = {
13718
+ isolated: {}
13719
+ };
13720
+ AssetTier.UNLISTED = {
13721
+ unlisted: {}
13722
+ };
13723
+ class SwapDirection {
13724
+ }
13725
+ SwapDirection.ADD = {
13726
+ add: {}
13727
+ };
13728
+ SwapDirection.REMOVE = {
13729
+ remove: {}
13730
+ };
13731
+ class SpotBalanceType {
13732
+ }
13733
+ SpotBalanceType.DEPOSIT = {
13734
+ deposit: {}
13735
+ };
13736
+ SpotBalanceType.BORROW = {
13737
+ borrow: {}
13738
+ };
13739
+ class PositionDirection {
13740
+ }
13741
+ PositionDirection.LONG = {
13742
+ long: {}
13743
+ };
13744
+ PositionDirection.SHORT = {
13745
+ short: {}
13746
+ };
13747
+ class DepositDirection {
13748
+ }
13749
+ DepositDirection.DEPOSIT = {
13750
+ deposit: {}
13751
+ };
13752
+ DepositDirection.WITHDRAW = {
13753
+ withdraw: {}
13754
+ };
13755
+ class OracleSource {
13756
+ static get(n) {
13757
+ const name = Object.entries(OracleSourceNum).find(([, v])=>v === n)?.[0];
13758
+ const source = Object.entries(OracleSource).find(([key, _])=>key.toLocaleLowerCase() === name?.toLocaleLowerCase())?.[1];
13759
+ if (!source) {
13760
+ throw new Error(`Invalid oracle source enum value: ${n}`);
13761
+ }
13762
+ return source;
13763
+ }
13764
+ }
13765
+ OracleSource.PYTH = {
13766
+ pyth: {}
13767
+ };
13768
+ OracleSource.PYTH_1K = {
13769
+ pyth1K: {}
13770
+ };
13771
+ OracleSource.PYTH_1M = {
13772
+ pyth1M: {}
13773
+ };
13774
+ OracleSource.PYTH_PULL = {
13775
+ pythPull: {}
13776
+ };
13777
+ OracleSource.PYTH_1K_PULL = {
13778
+ pyth1KPull: {}
13779
+ };
13780
+ OracleSource.PYTH_1M_PULL = {
13781
+ pyth1MPull: {}
13782
+ };
13783
+ OracleSource.SWITCHBOARD = {
13784
+ switchboard: {}
13785
+ };
13786
+ OracleSource.QUOTE_ASSET = {
13787
+ quoteAsset: {}
13788
+ };
13789
+ OracleSource.PYTH_STABLE_COIN = {
13790
+ pythStableCoin: {}
13791
+ };
13792
+ OracleSource.PYTH_STABLE_COIN_PULL = {
13793
+ pythStableCoinPull: {}
13794
+ };
13795
+ OracleSource.Prelaunch = {
13796
+ prelaunch: {}
13797
+ };
13798
+ OracleSource.SWITCHBOARD_ON_DEMAND = {
13799
+ switchboardOnDemand: {}
13800
+ };
13801
+ OracleSource.PYTH_LAZER = {
13802
+ pythLazer: {}
13803
+ };
13804
+ OracleSource.PYTH_LAZER_1K = {
13805
+ pythLazer1K: {}
13806
+ };
13807
+ OracleSource.PYTH_LAZER_1M = {
13808
+ pythLazer1M: {}
13809
+ };
13810
+ OracleSource.PYTH_LAZER_STABLE_COIN = {
13811
+ pythLazerStableCoin: {}
13812
+ };
13813
+ class OracleSourceNum {
13814
+ }
13815
+ OracleSourceNum.PYTH = 0;
13816
+ OracleSourceNum.PYTH_1K = 1;
13817
+ OracleSourceNum.PYTH_1M = 2;
13818
+ OracleSourceNum.PYTH_PULL = 3;
13819
+ OracleSourceNum.PYTH_1K_PULL = 4;
13820
+ OracleSourceNum.PYTH_1M_PULL = 5;
13821
+ OracleSourceNum.SWITCHBOARD = 6;
13822
+ OracleSourceNum.QUOTE_ASSET = 7;
13823
+ OracleSourceNum.PYTH_STABLE_COIN = 8;
13824
+ OracleSourceNum.PYTH_STABLE_COIN_PULL = 9;
13825
+ OracleSourceNum.PRELAUNCH = 10;
13826
+ OracleSourceNum.SWITCHBOARD_ON_DEMAND = 11;
13827
+ OracleSourceNum.PYTH_LAZER = 12;
13828
+ OracleSourceNum.PYTH_LAZER_1K = 13;
13829
+ OracleSourceNum.PYTH_LAZER_1M = 14;
13830
+ OracleSourceNum.PYTH_LAZER_STABLE_COIN = 15;
13831
+ class OrderType {
13832
+ }
13833
+ OrderType.LIMIT = {
13834
+ limit: {}
13835
+ };
13836
+ OrderType.TRIGGER_MARKET = {
13837
+ triggerMarket: {}
13838
+ };
13839
+ OrderType.TRIGGER_LIMIT = {
13840
+ triggerLimit: {}
13841
+ };
13842
+ OrderType.MARKET = {
13843
+ market: {}
13844
+ };
13845
+ OrderType.ORACLE = {
13846
+ oracle: {}
13847
+ };
13848
+ class MarketType {
13849
+ }
13850
+ MarketType.SPOT = {
13851
+ spot: {}
13852
+ };
13853
+ MarketType.PERP = {
13854
+ perp: {}
13855
+ };
13856
+ class OrderStatus {
13857
+ }
13858
+ OrderStatus.INIT = {
13859
+ init: {}
13860
+ };
13861
+ OrderStatus.OPEN = {
13862
+ open: {}
13863
+ };
13864
+ OrderStatus.FILLED = {
13865
+ filled: {}
13866
+ };
13867
+ OrderStatus.CANCELED = {
13868
+ canceled: {}
13869
+ };
13870
+ class OrderAction {
13871
+ }
13872
+ OrderAction.PLACE = {
13873
+ place: {}
13874
+ };
13875
+ OrderAction.CANCEL = {
13876
+ cancel: {}
13877
+ };
13878
+ OrderAction.EXPIRE = {
13879
+ expire: {}
13880
+ };
13881
+ OrderAction.FILL = {
13882
+ fill: {}
13883
+ };
13884
+ OrderAction.TRIGGER = {
13885
+ trigger: {}
13886
+ };
13887
+ class OrderActionExplanation {
13888
+ }
13889
+ OrderActionExplanation.NONE = {
13890
+ none: {}
13891
+ };
13892
+ OrderActionExplanation.INSUFFICIENT_FREE_COLLATERAL = {
13893
+ insufficientFreeCollateral: {}
13894
+ };
13895
+ OrderActionExplanation.ORACLE_PRICE_BREACHED_LIMIT_PRICE = {
13896
+ oraclePriceBreachedLimitPrice: {}
13897
+ };
13898
+ OrderActionExplanation.MARKET_ORDER_FILLED_TO_LIMIT_PRICE = {
13899
+ marketOrderFilledToLimitPrice: {}
13900
+ };
13901
+ OrderActionExplanation.ORDER_EXPIRED = {
13902
+ orderExpired: {}
13903
+ };
13904
+ OrderActionExplanation.LIQUIDATION = {
13905
+ liquidation: {}
13906
+ };
13907
+ OrderActionExplanation.ORDER_FILLED_WITH_AMM = {
13908
+ orderFilledWithAmm: {}
13909
+ };
13910
+ OrderActionExplanation.ORDER_FILLED_WITH_AMM_JIT = {
13911
+ orderFilledWithAmmJit: {}
13912
+ };
13913
+ OrderActionExplanation.ORDER_FILLED_WITH_AMM_JIT_LP_SPLIT = {
13914
+ orderFilledWithAmmJitLpSplit: {}
13915
+ };
13916
+ OrderActionExplanation.ORDER_FILLED_WITH_LP_JIT = {
13917
+ orderFilledWithLpJit: {}
13918
+ };
13919
+ OrderActionExplanation.ORDER_FILLED_WITH_MATCH = {
13920
+ orderFilledWithMatch: {}
13921
+ };
13922
+ OrderActionExplanation.ORDER_FILLED_WITH_MATCH_JIT = {
13923
+ orderFilledWithMatchJit: {}
13924
+ };
13925
+ OrderActionExplanation.MARKET_EXPIRED = {
13926
+ marketExpired: {}
13927
+ };
13928
+ OrderActionExplanation.RISK_INCREASING_ORDER = {
13929
+ riskingIncreasingOrder: {}
13930
+ };
13931
+ OrderActionExplanation.ORDER_FILLED_WITH_SERUM = {
13932
+ orderFillWithSerum: {}
13933
+ };
13934
+ OrderActionExplanation.ORDER_FILLED_WITH_OPENBOOK_V2 = {
13935
+ orderFilledWithOpenbookV2: {}
13936
+ };
13937
+ OrderActionExplanation.ORDER_FILLED_WITH_PHOENIX = {
13938
+ orderFillWithPhoenix: {}
13939
+ };
13940
+ OrderActionExplanation.REDUCE_ONLY_ORDER_INCREASED_POSITION = {
13941
+ reduceOnlyOrderIncreasedPosition: {}
13942
+ };
13943
+ OrderActionExplanation.DERISK_LP = {
13944
+ deriskLp: {}
13945
+ };
13946
+ OrderActionExplanation.TRANSFER_PERP_POSITION = {
13947
+ transferPerpPosition: {}
13948
+ };
13949
+ class OrderTriggerCondition {
13950
+ }
13951
+ OrderTriggerCondition.ABOVE = {
13952
+ above: {}
13953
+ };
13954
+ OrderTriggerCondition.BELOW = {
13955
+ below: {}
13956
+ };
13957
+ OrderTriggerCondition.TRIGGERED_ABOVE = {
13958
+ triggeredAbove: {}
13959
+ } // above condition has been triggered
13960
+ ;
13961
+ OrderTriggerCondition.TRIGGERED_BELOW = {
13962
+ triggeredBelow: {}
13963
+ } // below condition has been triggered
13964
+ ;
13965
+ class SpotFulfillmentType {
13966
+ }
13967
+ SpotFulfillmentType.EXTERNAL = {
13968
+ external: {}
13969
+ };
13970
+ SpotFulfillmentType.MATCH = {
13971
+ match: {}
13972
+ };
13973
+ class SpotFulfillmentStatus {
13974
+ }
13975
+ SpotFulfillmentStatus.ENABLED = {
13976
+ enabled: {}
13977
+ };
13978
+ SpotFulfillmentStatus.DISABLED = {
13979
+ disabled: {}
13980
+ };
13981
+ class DepositExplanation {
13982
+ }
13983
+ DepositExplanation.NONE = {
13984
+ none: {}
13985
+ };
13986
+ DepositExplanation.TRANSFER = {
13987
+ transfer: {}
13988
+ };
13989
+ DepositExplanation.BORROW = {
13990
+ borrow: {}
13991
+ };
13992
+ DepositExplanation.REPAY_BORROW = {
13993
+ repayBorrow: {}
13994
+ };
13995
+ class SettlePnlExplanation {
13996
+ }
13997
+ SettlePnlExplanation.NONE = {
13998
+ none: {}
13999
+ };
14000
+ SettlePnlExplanation.EXPIRED_POSITION = {
14001
+ expiredPosition: {}
14002
+ };
14003
+ class SpotFulfillmentConfigStatus {
14004
+ }
14005
+ SpotFulfillmentConfigStatus.ENABLED = {
14006
+ enabled: {}
14007
+ };
14008
+ SpotFulfillmentConfigStatus.DISABLED = {
14009
+ disabled: {}
14010
+ };
14011
+ class StakeAction {
14012
+ }
14013
+ StakeAction.STAKE = {
14014
+ stake: {}
14015
+ };
14016
+ StakeAction.UNSTAKE_REQUEST = {
14017
+ unstakeRequest: {}
14018
+ };
14019
+ StakeAction.UNSTAKE_CANCEL_REQUEST = {
14020
+ unstakeCancelRequest: {}
14021
+ };
14022
+ StakeAction.UNSTAKE = {
14023
+ unstake: {}
14024
+ };
14025
+ StakeAction.UNSTAKE_TRANSFER = {
14026
+ unstakeTransfer: {}
14027
+ };
14028
+ StakeAction.STAKE_TRANSFER = {
14029
+ stakeTransfer: {}
14030
+ };
14031
+ class SettlePnlMode {
14032
+ }
14033
+ SettlePnlMode.TRY_SETTLE = {
14034
+ trySettle: {}
14035
+ };
14036
+ SettlePnlMode.MUST_SETTLE = {
14037
+ mustSettle: {}
14038
+ };
14039
+ function isVariant(object, type) {
14040
+ return object.hasOwnProperty(type);
14041
+ }
14042
+ function isOneOfVariant(object, types) {
14043
+ return types.reduce((result, type)=>{
14044
+ return result || object.hasOwnProperty(type);
14045
+ }, false);
14046
+ }
14047
+ function getVariant(object) {
14048
+ return Object.keys(object)[0];
14049
+ }
14050
+ var TradeSide = /*#__PURE__*/ function(TradeSide) {
14051
+ TradeSide[TradeSide["None"] = 0] = "None";
14052
+ TradeSide[TradeSide["Buy"] = 1] = "Buy";
14053
+ TradeSide[TradeSide["Sell"] = 2] = "Sell";
14054
+ return TradeSide;
14055
+ }({});
14056
+ class LPAction {
14057
+ }
14058
+ LPAction.ADD_LIQUIDITY = {
14059
+ addLiquidity: {}
14060
+ };
14061
+ LPAction.REMOVE_LIQUIDITY = {
14062
+ removeLiquidity: {}
14063
+ };
14064
+ LPAction.SETTLE_LIQUIDITY = {
14065
+ settleLiquidity: {}
14066
+ };
14067
+ LPAction.REMOVE_LIQUIDITY_DERISK = {
14068
+ removeLiquidityDerisk: {}
14069
+ };
14070
+ class LiquidationType {
14071
+ }
14072
+ LiquidationType.LIQUIDATE_PERP = {
14073
+ liquidatePerp: {}
14074
+ };
14075
+ LiquidationType.LIQUIDATE_BORROW_FOR_PERP_PNL = {
14076
+ liquidateBorrowForPerpPnl: {}
14077
+ };
14078
+ LiquidationType.LIQUIDATE_PERP_PNL_FOR_DEPOSIT = {
14079
+ liquidatePerpPnlForDeposit: {}
14080
+ };
14081
+ LiquidationType.PERP_BANKRUPTCY = {
14082
+ perpBankruptcy: {}
14083
+ };
14084
+ LiquidationType.SPOT_BANKRUPTCY = {
14085
+ spotBankruptcy: {}
14086
+ };
14087
+ LiquidationType.LIQUIDATE_SPOT = {
14088
+ liquidateSpot: {}
14089
+ };
14090
+ class PostOnlyParams {
14091
+ }
14092
+ PostOnlyParams.NONE = {
14093
+ none: {}
14094
+ };
14095
+ PostOnlyParams.MUST_POST_ONLY = {
14096
+ mustPostOnly: {}
14097
+ } // Tx fails if order can't be post only
14098
+ ;
14099
+ PostOnlyParams.TRY_POST_ONLY = {
14100
+ tryPostOnly: {}
14101
+ } // Tx succeeds and order not placed if can't be post only
14102
+ ;
14103
+ PostOnlyParams.SLIDE = {
14104
+ slide: {}
14105
+ } // Modify price to be post only if can't be post only
14106
+ ;
14107
+ var ModifyOrderPolicy = /*#__PURE__*/ function(ModifyOrderPolicy) {
14108
+ ModifyOrderPolicy[ModifyOrderPolicy["MustModify"] = 1] = "MustModify";
14109
+ ModifyOrderPolicy[ModifyOrderPolicy["ExcludePreviousFill"] = 2] = "ExcludePreviousFill";
14110
+ return ModifyOrderPolicy;
14111
+ }({});
14112
+ const DefaultOrderParams = {
14113
+ orderType: OrderType.MARKET,
14114
+ marketType: MarketType.PERP,
14115
+ userOrderId: 0,
14116
+ direction: PositionDirection.LONG,
14117
+ baseAssetAmount: ZERO,
14118
+ price: ZERO,
14119
+ marketIndex: 0,
14120
+ reduceOnly: false,
14121
+ postOnly: PostOnlyParams.NONE,
14122
+ immediateOrCancel: false,
14123
+ triggerPrice: null,
14124
+ triggerCondition: OrderTriggerCondition.ABOVE,
14125
+ oraclePriceOffset: null,
14126
+ auctionDuration: null,
14127
+ maxTs: null,
14128
+ auctionStartPrice: null,
14129
+ auctionEndPrice: null
14130
+ };
14131
+ var ReferrerStatus = /*#__PURE__*/ function(ReferrerStatus) {
14132
+ ReferrerStatus[ReferrerStatus["IsReferrer"] = 1] = "IsReferrer";
14133
+ ReferrerStatus[ReferrerStatus["IsReferred"] = 2] = "IsReferred";
14134
+ return ReferrerStatus;
14135
+ }({});
14136
+ var FuelOverflowStatus = /*#__PURE__*/ function(FuelOverflowStatus) {
14137
+ FuelOverflowStatus[FuelOverflowStatus["Exists"] = 1] = "Exists";
14138
+ return FuelOverflowStatus;
14139
+ }({});
14140
+ var PlaceAndTakeOrderSuccessCondition = /*#__PURE__*/ function(PlaceAndTakeOrderSuccessCondition) {
14141
+ PlaceAndTakeOrderSuccessCondition[PlaceAndTakeOrderSuccessCondition["PartialFill"] = 1] = "PartialFill";
14142
+ PlaceAndTakeOrderSuccessCondition[PlaceAndTakeOrderSuccessCondition["FullFill"] = 2] = "FullFill";
14143
+ return PlaceAndTakeOrderSuccessCondition;
14144
+ }({});
14145
+ class SwapReduceOnly {
14146
+ }
14147
+ SwapReduceOnly.In = {
14148
+ in: {}
14149
+ };
14150
+ SwapReduceOnly.Out = {
14151
+ out: {}
14152
+ };
14153
+
14154
+ function readUnsignedBigInt64LE(buffer, offset) {
14155
+ return new BN(buffer.subarray(offset, offset + 8), 10, "le");
14156
+ }
14157
+ function readSignedBigInt64LE(buffer, offset) {
14158
+ const unsignedValue = new BN(buffer.subarray(offset, offset + 8), 10, "le");
14159
+ if (unsignedValue.testn(63)) {
14160
+ const inverted = unsignedValue.notn(64).addn(1);
14161
+ return inverted.neg();
14162
+ } else {
14163
+ return unsignedValue;
14164
+ }
14165
+ }
14166
+ function decodeUser(buffer) {
14167
+ let offset = 8;
14168
+ const authority = new PublicKey(buffer.slice(offset, offset + 32));
14169
+ offset += 32;
14170
+ const delegate = new PublicKey(buffer.slice(offset, offset + 32));
14171
+ offset += 32;
14172
+ const name = [];
14173
+ for(let i = 0; i < 32; i++){
14174
+ name.push(buffer.readUint8(offset + i));
14175
+ }
14176
+ offset += 32;
14177
+ const spotPositions = [];
14178
+ for(let i = 0; i < 8; i++){
14179
+ const scaledBalance = readUnsignedBigInt64LE(buffer, offset);
14180
+ const openOrders = buffer.readUInt8(offset + 35);
14181
+ if (scaledBalance.eq(ZERO) && openOrders === 0) {
14182
+ offset += 40;
14183
+ continue;
14184
+ }
14185
+ offset += 8;
14186
+ const openBids = readSignedBigInt64LE(buffer, offset);
14187
+ offset += 8;
14188
+ const openAsks = readSignedBigInt64LE(buffer, offset);
14189
+ offset += 8;
14190
+ const cumulativeDeposits = readSignedBigInt64LE(buffer, offset);
14191
+ offset += 8;
14192
+ const marketIndex = buffer.readUInt16LE(offset);
14193
+ offset += 2;
14194
+ const balanceTypeNum = buffer.readUInt8(offset);
14195
+ let balanceType;
14196
+ if (balanceTypeNum === 0) {
14197
+ balanceType = SpotBalanceType.DEPOSIT;
14198
+ } else {
14199
+ balanceType = SpotBalanceType.BORROW;
14200
+ }
14201
+ offset += 6;
14202
+ spotPositions.push({
14203
+ scaledBalance,
14204
+ openBids,
14205
+ openAsks,
14206
+ cumulativeDeposits,
14207
+ marketIndex,
14208
+ balanceType,
14209
+ openOrders
14210
+ });
14211
+ }
14212
+ const perpPositions = [];
14213
+ for(let i = 0; i < 8; i++){
14214
+ const baseAssetAmount = readSignedBigInt64LE(buffer, offset + 8);
14215
+ const quoteAssetAmount = readSignedBigInt64LE(buffer, offset + 16);
14216
+ const lpShares = readUnsignedBigInt64LE(buffer, offset + 64);
14217
+ const openOrders = buffer.readUInt8(offset + 94);
14218
+ if (baseAssetAmount.eq(ZERO) && openOrders === 0 && quoteAssetAmount.eq(ZERO) && lpShares.eq(ZERO)) {
14219
+ offset += 96;
14220
+ continue;
14221
+ }
14222
+ const lastCumulativeFundingRate = readSignedBigInt64LE(buffer, offset);
14223
+ offset += 24;
14224
+ const quoteBreakEvenAmount = readSignedBigInt64LE(buffer, offset);
14225
+ offset += 8;
14226
+ const quoteEntryAmount = readSignedBigInt64LE(buffer, offset);
14227
+ offset += 8;
14228
+ const openBids = readSignedBigInt64LE(buffer, offset);
14229
+ offset += 8;
14230
+ const openAsks = readSignedBigInt64LE(buffer, offset);
14231
+ offset += 8;
14232
+ const settledPnl = readSignedBigInt64LE(buffer, offset);
14233
+ offset += 16;
14234
+ const lastBaseAssetAmountPerLp = readSignedBigInt64LE(buffer, offset);
14235
+ offset += 8;
14236
+ const lastQuoteAssetAmountPerLp = readSignedBigInt64LE(buffer, offset);
14237
+ offset += 8;
14238
+ const remainderBaseAssetAmount = buffer.readInt32LE(offset);
14239
+ offset += 4;
14240
+ const marketIndex = buffer.readUInt16LE(offset);
14241
+ offset += 3;
14242
+ const perLpBase = buffer.readUInt8(offset);
14243
+ offset += 1;
14244
+ perpPositions.push({
14245
+ lastCumulativeFundingRate,
14246
+ baseAssetAmount,
14247
+ quoteAssetAmount,
14248
+ quoteBreakEvenAmount,
14249
+ quoteEntryAmount,
14250
+ openBids,
14251
+ openAsks,
14252
+ settledPnl,
14253
+ lpShares,
14254
+ lastBaseAssetAmountPerLp,
14255
+ lastQuoteAssetAmountPerLp,
14256
+ remainderBaseAssetAmount,
14257
+ marketIndex,
14258
+ openOrders,
14259
+ perLpBase
14260
+ });
14261
+ }
14262
+ const orders = [];
14263
+ for(let i = 0; i < 32; i++){
14264
+ // skip order if it's not open
14265
+ if (buffer.readUint8(offset + 82) !== 1) {
14266
+ offset += 96;
14267
+ continue;
14268
+ }
14269
+ const slot = readUnsignedBigInt64LE(buffer, offset);
14270
+ offset += 8;
14271
+ const price = readUnsignedBigInt64LE(buffer, offset);
14272
+ offset += 8;
14273
+ const baseAssetAmount = readUnsignedBigInt64LE(buffer, offset);
14274
+ offset += 8;
14275
+ const baseAssetAmountFilled = readUnsignedBigInt64LE(buffer, offset);
14276
+ offset += 8;
14277
+ const quoteAssetAmountFilled = readUnsignedBigInt64LE(buffer, offset);
14278
+ offset += 8;
14279
+ const triggerPrice = readUnsignedBigInt64LE(buffer, offset);
14280
+ offset += 8;
14281
+ const auctionStartPrice = readSignedBigInt64LE(buffer, offset);
14282
+ offset += 8;
14283
+ const auctionEndPrice = readSignedBigInt64LE(buffer, offset);
14284
+ offset += 8;
14285
+ const maxTs = readSignedBigInt64LE(buffer, offset);
14286
+ offset += 8;
14287
+ const oraclePriceOffset = buffer.readInt32LE(offset);
14288
+ offset += 4;
14289
+ const orderId = buffer.readUInt32LE(offset);
14290
+ offset += 4;
14291
+ const marketIndex = buffer.readUInt16LE(offset);
14292
+ offset += 2;
14293
+ const orderStatusNum = buffer.readUInt8(offset);
14294
+ let status = OrderStatus.INIT;
14295
+ if (orderStatusNum === 0) {
14296
+ status = OrderStatus.INIT;
14297
+ } else if (orderStatusNum === 1) {
14298
+ status = OrderStatus.OPEN;
14299
+ } else if (orderStatusNum === 2) {
14300
+ status = OrderStatus.FILLED;
14301
+ } else if (orderStatusNum === 3) {
14302
+ status = OrderStatus.CANCELED;
14303
+ }
14304
+ offset += 1;
14305
+ const orderTypeNum = buffer.readUInt8(offset);
14306
+ let orderType = OrderType.MARKET;
14307
+ if (orderTypeNum === 0) {
14308
+ orderType = OrderType.MARKET;
14309
+ } else if (orderTypeNum === 1) {
14310
+ orderType = OrderType.LIMIT;
14311
+ } else if (orderTypeNum === 2) {
14312
+ orderType = OrderType.TRIGGER_MARKET;
14313
+ } else if (orderTypeNum === 3) {
14314
+ orderType = OrderType.TRIGGER_LIMIT;
14315
+ } else if (orderTypeNum === 4) {
14316
+ orderType = OrderType.ORACLE;
14317
+ }
14318
+ offset += 1;
14319
+ const marketTypeNum = buffer.readUInt8(offset);
14320
+ let marketType;
14321
+ if (marketTypeNum === 0) {
14322
+ marketType = MarketType.SPOT;
14323
+ } else {
14324
+ marketType = MarketType.PERP;
14325
+ }
14326
+ offset += 1;
14327
+ const userOrderId = buffer.readUint8(offset);
14328
+ offset += 1;
14329
+ const existingPositionDirectionNum = buffer.readUInt8(offset);
14330
+ let existingPositionDirection;
14331
+ if (existingPositionDirectionNum === 0) {
14332
+ existingPositionDirection = PositionDirection.LONG;
14333
+ } else {
14334
+ existingPositionDirection = PositionDirection.SHORT;
14335
+ }
14336
+ offset += 1;
14337
+ const positionDirectionNum = buffer.readUInt8(offset);
14338
+ let direction;
14339
+ if (positionDirectionNum === 0) {
14340
+ direction = PositionDirection.LONG;
14341
+ } else {
14342
+ direction = PositionDirection.SHORT;
14343
+ }
14344
+ offset += 1;
14345
+ const reduceOnly = buffer.readUInt8(offset) === 1;
14346
+ offset += 1;
14347
+ const postOnly = buffer.readUInt8(offset) === 1;
14348
+ offset += 1;
14349
+ const immediateOrCancel = buffer.readUInt8(offset) === 1;
14350
+ offset += 1;
14351
+ const triggerConditionNum = buffer.readUInt8(offset);
14352
+ let triggerCondition = OrderTriggerCondition.ABOVE;
14353
+ if (triggerConditionNum === 0) {
14354
+ triggerCondition = OrderTriggerCondition.ABOVE;
14355
+ } else if (triggerConditionNum === 1) {
14356
+ triggerCondition = OrderTriggerCondition.BELOW;
14357
+ } else if (triggerConditionNum === 2) {
14358
+ triggerCondition = OrderTriggerCondition.TRIGGERED_ABOVE;
14359
+ } else if (triggerConditionNum === 3) {
14360
+ triggerCondition = OrderTriggerCondition.TRIGGERED_BELOW;
14361
+ }
14362
+ offset += 1;
14363
+ const auctionDuration = buffer.readUInt8(offset);
14364
+ offset += 1;
14365
+ const postedSlotTail = buffer.readUint8(offset);
14366
+ offset += 1;
14367
+ const bitFlags = buffer.readUint8(offset);
14368
+ offset += 1;
14369
+ offset += 1; // padding
14370
+ orders.push({
14371
+ slot,
14372
+ price,
14373
+ baseAssetAmount,
14374
+ quoteAssetAmount: undefined,
14375
+ baseAssetAmountFilled,
14376
+ quoteAssetAmountFilled,
14377
+ triggerPrice,
14378
+ auctionStartPrice,
14379
+ auctionEndPrice,
14380
+ maxTs,
14381
+ oraclePriceOffset,
14382
+ orderId,
14383
+ marketIndex,
14384
+ status,
14385
+ orderType,
14386
+ marketType,
14387
+ userOrderId,
14388
+ existingPositionDirection,
14389
+ direction,
14390
+ reduceOnly,
14391
+ postOnly,
14392
+ immediateOrCancel,
14393
+ triggerCondition,
14394
+ auctionDuration,
14395
+ bitFlags,
14396
+ postedSlotTail
14397
+ });
14398
+ }
14399
+ const lastAddPerpLpSharesTs = readSignedBigInt64LE(buffer, offset);
14400
+ offset += 8;
14401
+ const totalDeposits = readUnsignedBigInt64LE(buffer, offset);
14402
+ offset += 8;
14403
+ const totalWithdraws = readUnsignedBigInt64LE(buffer, offset);
14404
+ offset += 8;
14405
+ const totalSocialLoss = readUnsignedBigInt64LE(buffer, offset);
14406
+ offset += 8;
14407
+ const settledPerpPnl = readSignedBigInt64LE(buffer, offset);
14408
+ offset += 8;
14409
+ const cumulativeSpotFees = readSignedBigInt64LE(buffer, offset);
14410
+ offset += 8;
14411
+ const cumulativePerpFunding = readSignedBigInt64LE(buffer, offset);
14412
+ offset += 8;
14413
+ const liquidationMarginFreed = readUnsignedBigInt64LE(buffer, offset);
14414
+ offset += 8;
14415
+ const lastActiveSlot = readUnsignedBigInt64LE(buffer, offset);
14416
+ offset += 8;
14417
+ const nextOrderId = buffer.readUInt32LE(offset);
14418
+ offset += 4;
14419
+ const maxMarginRatio = buffer.readUInt32LE(offset);
14420
+ offset += 4;
14421
+ const nextLiquidationId = buffer.readUInt16LE(offset);
14422
+ offset += 2;
14423
+ const subAccountId = buffer.readUInt16LE(offset);
14424
+ offset += 2;
14425
+ const status = buffer.readUInt8(offset);
14426
+ offset += 1;
14427
+ const isMarginTradingEnabled = buffer.readUInt8(offset) === 1;
14428
+ offset += 1;
14429
+ const idle = buffer.readUInt8(offset) === 1;
14430
+ offset += 1;
14431
+ const openOrders = buffer.readUInt8(offset);
14432
+ offset += 1;
14433
+ const hasOpenOrder = buffer.readUInt8(offset) === 1;
14434
+ offset += 1;
14435
+ const openAuctions = buffer.readUInt8(offset);
14436
+ offset += 1;
14437
+ const hasOpenAuction = buffer.readUInt8(offset) === 1;
14438
+ offset += 1;
14439
+ let marginMode;
14440
+ const marginModeNum = buffer.readUInt8(offset);
14441
+ if (marginModeNum === 0) {
14442
+ marginMode = MarginMode.DEFAULT;
14443
+ } else {
14444
+ marginMode = MarginMode.HIGH_LEVERAGE;
14445
+ }
14446
+ offset += 1;
14447
+ const poolId = buffer.readUint8(offset);
14448
+ offset += 1;
14449
+ offset += 3; // padding
14450
+ const lastFuelBonusUpdateTs = buffer.readUint32LE(offset);
14451
+ offset += 4;
14452
+ return {
14453
+ authority,
14454
+ delegate,
14455
+ name,
14456
+ spotPositions,
14457
+ perpPositions,
14458
+ orders,
14459
+ lastAddPerpLpSharesTs,
14460
+ totalDeposits,
14461
+ totalWithdraws,
14462
+ totalSocialLoss,
14463
+ settledPerpPnl,
14464
+ cumulativeSpotFees,
14465
+ cumulativePerpFunding,
14466
+ liquidationMarginFreed,
14467
+ lastActiveSlot,
14468
+ nextOrderId,
14469
+ maxMarginRatio,
14470
+ nextLiquidationId,
14471
+ subAccountId,
14472
+ status,
14473
+ isMarginTradingEnabled,
14474
+ idle,
14475
+ openOrders,
14476
+ hasOpenOrder,
14477
+ openAuctions,
14478
+ hasOpenAuction,
14479
+ marginMode,
14480
+ poolId,
14481
+ lastFuelBonusUpdateTs
14482
+ };
14483
+ }
14484
+
14485
+ const DRIFT_SIGNER = new PublicKey("JCNCMFXo5M5qwUPg2Utu1u6YWp3MbygxqBsBeXXJfrw");
13581
14486
  const DRIFT_MARGIN_PRECISION = 10000;
13582
14487
  class DriftClient {
13583
14488
  /*
@@ -13602,89 +14507,268 @@ class DriftClient {
13602
14507
  const tx = await this.deleteUserTx(new PublicKey(statePda), subAccountId, txOptions);
13603
14508
  return await this.base.sendAndConfirm(tx);
13604
14509
  }
13605
- async deposit(statePda, amount, marketIndex = 1, subAccountId = 0, marketConfigs, txOptions = {}) {
13606
- const tx = await this.depositTx(new PublicKey(statePda), amount, marketIndex, subAccountId, marketConfigs, txOptions);
14510
+ async deposit(statePda, amount, marketIndex = 1, subAccountId = 0, txOptions = {}) {
14511
+ const tx = await this.depositTx(new PublicKey(statePda), amount, marketIndex, subAccountId, txOptions);
13607
14512
  return await this.base.sendAndConfirm(tx);
13608
14513
  }
13609
- async withdraw(statePda, amount, marketIndex = 1, subAccountId = 0, marketConfigs, txOptions = {}) {
13610
- const tx = await this.withdrawTx(new PublicKey(statePda), amount, marketIndex, subAccountId, marketConfigs, txOptions);
14514
+ async withdraw(statePda, amount, marketIndex = 1, subAccountId = 0, txOptions = {}) {
14515
+ const tx = await this.withdrawTx(new PublicKey(statePda), amount, marketIndex, subAccountId, txOptions);
13611
14516
  return await this.base.sendAndConfirm(tx);
13612
14517
  }
13613
- async placeOrder(statePda, orderParams, subAccountId = 0, marketConfigs, txOptions = {}) {
13614
- const tx = await this.placeOrderTx(new PublicKey(statePda), orderParams, subAccountId, marketConfigs, txOptions);
14518
+ async placeOrder(statePda, orderParams, subAccountId = 0, txOptions = {}) {
14519
+ const tx = await this.placeOrderTx(new PublicKey(statePda), orderParams, subAccountId, txOptions);
13615
14520
  return await this.base.sendAndConfirm(tx);
13616
14521
  }
13617
- async modifyOrder(statePda, modifyOrderParams, subAccountId = 0, marketConfigs, txOptions = {}) {
13618
- const tx = await this.modifyOrderTx(new PublicKey(statePda), modifyOrderParams, subAccountId, marketConfigs, txOptions);
14522
+ async modifyOrder(statePda, modifyOrderParams, subAccountId = 0, txOptions = {}) {
14523
+ const tx = await this.modifyOrderTx(new PublicKey(statePda), modifyOrderParams, subAccountId, txOptions);
13619
14524
  return await this.base.sendAndConfirm(tx);
13620
14525
  }
13621
- async cancelOrders(statePda, marketType, marketIndex, direction, subAccountId = 0, marketConfigs, txOptions = {}) {
13622
- const tx = await this.cancelOrdersTx(new PublicKey(statePda), marketType, marketIndex, direction, subAccountId, marketConfigs, txOptions);
14526
+ async cancelOrders(statePda, marketType, marketIndex, direction, subAccountId = 0, txOptions = {}) {
14527
+ const tx = await this.cancelOrdersTx(new PublicKey(statePda), marketType, marketIndex, direction, subAccountId, txOptions);
13623
14528
  return await this.base.sendAndConfirm(tx);
13624
14529
  }
13625
- async cancelOrdersByIds(statePda, orderIds, subAccountId = 0, marketConfigs, txOptions = {}) {
13626
- const tx = await this.cancelOrdersByIdsTx(new PublicKey(statePda), orderIds, subAccountId, marketConfigs, txOptions);
14530
+ async cancelOrdersByIds(statePda, orderIds, subAccountId = 0, txOptions = {}) {
14531
+ const tx = await this.cancelOrdersByIdsTx(new PublicKey(statePda), orderIds, subAccountId, txOptions);
13627
14532
  return await this.base.sendAndConfirm(tx);
13628
14533
  }
13629
- async settlePnl(statePda, marketIndex, subAccountId = 0, marketConfigs, txOptions = {}) {
13630
- const tx = await this.settlePnlTx(new PublicKey(statePda), marketIndex, subAccountId, marketConfigs, txOptions);
14534
+ async settlePnl(statePda, marketIndex, subAccountId = 0, txOptions = {}) {
14535
+ const tx = await this.settlePnlTx(new PublicKey(statePda), marketIndex, subAccountId, txOptions);
13631
14536
  return await this.base.sendAndConfirm(tx);
13632
14537
  }
13633
- async priceDrift(statePda, marketConfigs, priceDenom, txOptions = {}) {
13634
- const tx = await this.priceDriftTx(new PublicKey(statePda), marketConfigs, priceDenom, txOptions);
14538
+ async priceDrift(statePda, priceDenom, txOptions = {}) {
14539
+ const tx = await this.priceDriftTx(new PublicKey(statePda), priceDenom, txOptions);
13635
14540
  return await this.base.sendAndConfirm(tx);
13636
14541
  }
13637
- /*
13638
- * Utils
13639
- */ getUser(statePda, subAccountId = 0) {
14542
+ getGlamReferrerPdas() {
14543
+ return {
14544
+ user: this.getUserPda(GLAM_REFERRER, 0),
14545
+ userStats: this.getUserStatsPda(GLAM_REFERRER)
14546
+ };
14547
+ }
14548
+ parsePerpMarket(data) {
14549
+ const marketPda = new PublicKey(data.subarray(8, 40));
14550
+ const oracle = new PublicKey(data.subarray(40, 72));
14551
+ const name = this.charsToName(data.subarray(1000, 1032));
14552
+ const oralceEnum = data.subarray(926, 927).readUint8();
14553
+ const oracleSource = OracleSource.get(oralceEnum);
14554
+ const marketIndex = data.subarray(1160, 1162).readUint16LE();
14555
+ return {
14556
+ name,
14557
+ marketPda,
14558
+ marketIndex,
14559
+ oracle,
14560
+ oracleSource
14561
+ };
14562
+ }
14563
+ parseSpotMarket(data) {
14564
+ const marketPda = new PublicKey(data.subarray(8, 40));
14565
+ const oracle = new PublicKey(data.subarray(40, 72));
14566
+ const mint = new PublicKey(data.subarray(72, 104));
14567
+ const vault = new PublicKey(data.subarray(104, 136));
14568
+ const name = this.charsToName(data.subarray(136, 168));
14569
+ const cumulativeDepositInterest = new BN(data.subarray(464, 480), "le");
14570
+ const cumulativeBorrowInterest = new BN(data.subarray(480, 496), "le");
14571
+ const decimals = data.subarray(680, 684).readUint32LE();
14572
+ const marketIndex = data.subarray(684, 686).readUint16LE();
14573
+ const oralceEnum = data.subarray(687, 688).readUint8();
14574
+ const oracleSource = OracleSource.get(oralceEnum);
14575
+ const tokenProgram = data.subarray(734, 735).readUint8() == 0 ? TOKEN_PROGRAM_ID : TOKEN_2022_PROGRAM_ID;
14576
+ return {
14577
+ name,
14578
+ marketIndex,
14579
+ marketPda,
14580
+ oracle,
14581
+ oracleSource,
14582
+ vault,
14583
+ mint,
14584
+ decimals,
14585
+ tokenProgram,
14586
+ cumulativeDepositInterest,
14587
+ cumulativeBorrowInterest
14588
+ };
14589
+ }
14590
+ async calcSpotBalance(marketIndex, scaledBalance, scaledBalanceType) {
14591
+ const { decimals, cumulativeDepositInterest, cumulativeBorrowInterest } = await this.fetchAndParseSpotMarket(marketIndex);
14592
+ const precisionAdjustment = new BN(10 ** (19 - decimals));
14593
+ let interest = cumulativeDepositInterest;
14594
+ if (scaledBalanceType === SpotBalanceType.BORROW) {
14595
+ interest = cumulativeBorrowInterest;
14596
+ }
14597
+ const balance = scaledBalance.mul(interest).div(precisionAdjustment);
14598
+ const amount = scaledBalanceType === SpotBalanceType.BORROW ? balance.neg().toNumber() : balance.toNumber();
14599
+ const uiAmount = amount / 10 ** decimals;
14600
+ return {
14601
+ amount,
14602
+ uiAmount
14603
+ };
14604
+ }
14605
+ getDriftUserPdas(statePda, subAccountId = 0) {
13640
14606
  const vault = this.base.getVaultPda(new PublicKey(statePda));
13641
- return [
13642
- getUserAccountPublicKeySync(DRIFT_PROGRAM_ID, vault, subAccountId),
13643
- getUserStatsAccountPublicKey(DRIFT_PROGRAM_ID, vault)
13644
- ];
14607
+ return {
14608
+ user: this.getUserPda(vault, subAccountId),
14609
+ userStats: this.getUserStatsPda(vault)
14610
+ };
13645
14611
  }
13646
- getGlamReferrer() {
13647
- return [
13648
- getUserAccountPublicKeySync(DRIFT_PROGRAM_ID, GLAM_REFERRER, 0),
13649
- getUserStatsAccountPublicKey(DRIFT_PROGRAM_ID, GLAM_REFERRER)
13650
- ];
14612
+ get driftStatePda() {
14613
+ return PublicKey.findProgramAddressSync([
14614
+ Buffer.from("drift_state")
14615
+ ], DRIFT_PROGRAM_ID)[0];
13651
14616
  }
13652
- async fetchMarketConfigs() {
13653
- const response = await fetch("https://api.glam.systems/v0/drift/market_configs/");
13654
- if (!response.ok) {
13655
- throw new Error(`Failed to fetch market configs: ${response.status}`);
14617
+ async fetchAndParseSpotMarket(marketIndex) {
14618
+ const markets = await this.fetchAndParseSpotMarkets([
14619
+ marketIndex
14620
+ ]);
14621
+ if (!markets || markets.length === 0) {
14622
+ throw new Error(`Spot market not found at index ${marketIndex}`);
13656
14623
  }
13657
- const data = await response.json();
13658
- return data;
14624
+ return markets[0];
14625
+ }
14626
+ async fetchAndParseSpotMarkets(marketIndexes) {
14627
+ const indexesToFetch = marketIndexes.filter((marketIndex)=>!this.spotMarkets.has(marketIndex));
14628
+ if (indexesToFetch.length > 0) {
14629
+ console.log("Fetching spot markets:", indexesToFetch);
14630
+ const marketPdas = indexesToFetch.map((marketIndex)=>this.getMarketPda(MarketType.SPOT, marketIndex));
14631
+ const accounts = await this.base.provider.connection.getMultipleAccountsInfo(marketPdas);
14632
+ accounts.forEach((account)=>{
14633
+ if (account) {
14634
+ const spotMarket = this.parseSpotMarket(account.data);
14635
+ this.spotMarkets.set(spotMarket.marketIndex, spotMarket);
14636
+ }
14637
+ });
14638
+ }
14639
+ // At this point this.spotMarkets has all the requested markets
14640
+ return marketIndexes.map((marketIndex)=>this.spotMarkets.get(marketIndex)).filter((m)=>m);
13659
14641
  }
13660
- async fetchGlamDriftUser(glamState, subAccountId = 0) {
13661
- const vault = this.base.getVaultPda(new PublicKey(glamState));
13662
- const response = await fetch(`https://api.glam.systems/v0/drift/user?authority=${vault.toBase58()}&accountId=${subAccountId}`);
13663
- const data = await response.json();
13664
- if (!data) {
13665
- throw new Error("Failed to fetch drift user.");
14642
+ async fetchAndParsePerpMarket(marketIndex) {
14643
+ const markets = await this.fetchAndParsePerpMarkets([
14644
+ marketIndex
14645
+ ]);
14646
+ if (!markets || markets.length === 0) {
14647
+ throw new Error(`Perp market not found at index ${marketIndex}`);
13666
14648
  }
13667
- return data;
14649
+ return markets[0];
14650
+ }
14651
+ async fetchAndParsePerpMarkets(marketIndexes) {
14652
+ const indexesToFetch = marketIndexes.filter((marketIndex)=>!this.perpMarkets.has(marketIndex));
14653
+ if (indexesToFetch.length > 0) {
14654
+ console.log("Fetching perp markets:", indexesToFetch);
14655
+ const marketPdas = marketIndexes.map((marketIndex)=>this.getMarketPda(MarketType.PERP, marketIndex));
14656
+ const accounts = await this.base.provider.connection.getMultipleAccountsInfo(marketPdas);
14657
+ accounts.forEach((account)=>{
14658
+ if (account) {
14659
+ const perpMarket = this.parsePerpMarket(account.data);
14660
+ this.perpMarkets.set(perpMarket.marketIndex, perpMarket);
14661
+ }
14662
+ });
14663
+ } else {
14664
+ console.log("Requested perp markets already cached:", marketIndexes);
14665
+ }
14666
+ return marketIndexes.map((marketIndex)=>this.perpMarkets.get(marketIndex)).filter((m)=>m);
13668
14667
  }
13669
- async getPositions(statePda, subAccountId = 0) {
13670
- const { spotPositions, perpPositions } = await this.fetchGlamDriftUser(new PublicKey(statePda), subAccountId);
14668
+ async fetchMarketConfigs() {
14669
+ // const response = await fetch(
14670
+ // "https://api.glam.systems/v0/drift/market_configs/",
14671
+ // );
14672
+ // if (!response.ok) {
14673
+ // throw new Error(`Failed to fetch market configs: ${response.status}`);
14674
+ // }
14675
+ // const data = await response.json();
14676
+ // const { orderConstants, perp, spot } = data;
14677
+ // // Transform perp market from API to `PerpMarket` type
14678
+ // const perpMarkets = perp.map((m: any) => ({
14679
+ // marketIndex: m.marketIndex,
14680
+ // marketPda: m.marketPDA,
14681
+ // oracle: new PublicKey(m.oracle),
14682
+ // }));
14683
+ // // Transform spot market from API to `SpotMarket` type
14684
+ // const spotMarkets = spot.map((m: any) => ({
14685
+ // marketIndex: m.marketIndex,
14686
+ // marketPda: m.marketPDA,
14687
+ // oracle: new PublicKey(m.oracle),
14688
+ // mint: new PublicKey(m.mint),
14689
+ // vault: new PublicKey(m.vaultPDA),
14690
+ // decimals: m.decimals,
14691
+ // }));
14692
+ if (!this.marketConfigs) {
14693
+ const perpMarkets = await this.fetchAndParsePerpMarkets(Array.from(Array(100).keys()));
14694
+ const spotMarkets = await this.fetchAndParseSpotMarkets(Array.from(Array(100).keys()));
14695
+ this.marketConfigs = {
14696
+ orderConstants: {
14697
+ perpBaseScale: 9,
14698
+ quoteScale: 6
14699
+ },
14700
+ perpMarkets,
14701
+ spotMarkets
14702
+ };
14703
+ }
14704
+ return this.marketConfigs;
14705
+ }
14706
+ // public async fetchGlamDriftUser(
14707
+ // glamState: PublicKey | string,
14708
+ // subAccountId: number = 0,
14709
+ // ): Promise<GlamDriftUser> {
14710
+ // const vault = this.base.getVaultPda(new PublicKey(glamState));
14711
+ // const response = await fetch(
14712
+ // `https://api.glam.systems/v0/drift/user?authority=${vault.toBase58()}&accountId=${subAccountId}`,
14713
+ // );
14714
+ // const data = await response.json();
14715
+ // if (!data) {
14716
+ // throw new Error("Failed to fetch drift user.");
14717
+ // }
14718
+ // return data as GlamDriftUser;
14719
+ // }
14720
+ charsToName(chars) {
14721
+ return String.fromCharCode(...chars).replace(/\0/g, "").trim();
14722
+ }
14723
+ async fetchDriftUser(statePda, subAccountId = 0) {
14724
+ const { user } = this.getDriftUserPdas(new PublicKey(statePda), subAccountId);
14725
+ const accountInfo = await this.base.provider.connection.getAccountInfo(user);
14726
+ if (!accountInfo) {
14727
+ return null;
14728
+ }
14729
+ const { delegate, name, spotPositions, marginMode, perpPositions, isMarginTradingEnabled, maxMarginRatio, orders } = decodeUser(accountInfo.data);
14730
+ // Prefetch market configs
14731
+ const marketConfigs = await this.fetchMarketConfigs();
14732
+ const spotPositionsExt = await Promise.all(spotPositions.map(async (p)=>{
14733
+ const { amount, uiAmount } = await this.calcSpotBalance(p.marketIndex, p.scaledBalance, p.balanceType);
14734
+ const spotMarket = marketConfigs.spotMarkets.find((m)=>m.marketIndex === p.marketIndex);
14735
+ return {
14736
+ ...p,
14737
+ amount,
14738
+ uiAmount,
14739
+ mint: spotMarket.mint,
14740
+ decimals: spotMarket.decimals,
14741
+ marketName: spotMarket.name
14742
+ };
14743
+ }));
13671
14744
  return {
13672
- spotPositions,
13673
- perpPositions
14745
+ delegate,
14746
+ name: this.charsToName(name),
14747
+ spotPositions: spotPositionsExt,
14748
+ perpPositions,
14749
+ orders,
14750
+ marginMode,
14751
+ subAccountId,
14752
+ isMarginTradingEnabled,
14753
+ maxMarginRatio
13674
14754
  };
13675
14755
  }
14756
+ // async getPositions(statePda: PublicKey | string, subAccountId: number = 0) {
14757
+ // const driftUser = await this.fetchDriftUser(
14758
+ // new PublicKey(statePda),
14759
+ // subAccountId,
14760
+ // );
14761
+ // if (!driftUser) {
14762
+ // return { spotPositions: [], perpPositions: [] };
14763
+ // }
14764
+ // const marketConfigs = await this.fetchMarketConfigs();
14765
+ // const { spotPositions, perpPositions } = driftUser;
14766
+ // return { spotPositions, perpPositions };
14767
+ // }
13676
14768
  async fetchPolicyConfig(glamState) {
13677
- let driftUserAccount;
13678
- if (glamState && glamState.id) {
13679
- const [driftUserAddress] = this.getUser(glamState.id);
13680
- const connection = this.base.provider.connection;
13681
- const info = await connection.getAccountInfo(driftUserAddress, connection.commitment);
13682
- if (info) {
13683
- driftUserAccount = decodeUser(info.data);
13684
- }
13685
- }
14769
+ const driftUserAccount = glamState && glamState.id && await this.fetchDriftUser(glamState.id);
13686
14770
  let delegate = driftUserAccount?.delegate;
13687
- if (delegate && delegate.equals(new PublicKey(0))) {
14771
+ if (delegate && delegate.equals(PublicKey.default)) {
13688
14772
  delegate = undefined;
13689
14773
  }
13690
14774
  return {
@@ -13697,8 +14781,12 @@ class DriftClient {
13697
14781
  driftMarketIndexesSpot: glamState?.driftMarketIndexesSpot || []
13698
14782
  };
13699
14783
  }
13700
- async composeRemainingAccounts(glamState, subAccountId, marketConfigs, marketType, marketIndex) {
13701
- const { spotPositions, perpPositions } = await this.getPositions(glamState, subAccountId);
14784
+ async composeRemainingAccounts(glamState, subAccountId, marketType, marketIndex) {
14785
+ const driftUser = await this.fetchDriftUser(glamState, subAccountId);
14786
+ if (!driftUser) {
14787
+ throw new Error("Drift user not found");
14788
+ }
14789
+ const { spotPositions, perpPositions } = driftUser;
13702
14790
  const spotMarketIndexes = spotPositions.map((p)=>p.marketIndex);
13703
14791
  const perpMarketIndexes = perpPositions.map((p)=>p.marketIndex);
13704
14792
  // Note that marketIndex is could be 0, need to explicitly check undefined
@@ -13713,10 +14801,13 @@ class DriftClient {
13713
14801
  spotMarketIndexes.push(0);
13714
14802
  }
13715
14803
  }
13716
- const oracles = spotMarketIndexes.map((i)=>marketConfigs.spot[i].oracle).concat(perpMarketIndexes.map((i)=>marketConfigs.perp[i].oracle));
13717
- const markets = spotMarketIndexes.map((i)=>marketConfigs.spot[i].marketPDA).concat(perpMarketIndexes.map((i)=>marketConfigs.perp[i].marketPDA));
13718
- console.log("[composeRemainingAccounts] markets:", markets);
13719
- console.log("[composeRemainingAccounts] oracles:", oracles);
14804
+ const spotMarkets = await this.fetchAndParseSpotMarkets(spotMarketIndexes);
14805
+ const perpMarkets = await this.fetchAndParsePerpMarkets(perpMarketIndexes);
14806
+ console.log("[composeRemainingAccounts] perpMarkets:", perpMarkets);
14807
+ const oracles = spotMarkets.map((m)=>m.oracle).concat(perpMarkets.map((m)=>m.oracle));
14808
+ const markets = spotMarkets.map((m)=>m.marketPda).concat(perpMarkets.map((m)=>m.marketPda));
14809
+ console.log("[composeRemainingAccounts] markets:", markets.map((m)=>m.toBase58()));
14810
+ console.log("[composeRemainingAccounts] oracles:", oracles.map((o)=>o.toBase58()));
13720
14811
  return oracles.map((o)=>({
13721
14812
  pubkey: new PublicKey(o),
13722
14813
  isWritable: false,
@@ -13728,30 +14819,36 @@ class DriftClient {
13728
14819
  })));
13729
14820
  }
13730
14821
  async initializeUserStatsIx(glamState, glamSigner) {
13731
- const [_, userStats] = this.getUser(glamState);
13732
- const state = await getDriftStateAccountPublicKey(DRIFT_PROGRAM_ID);
14822
+ const { userStats } = this.getDriftUserPdas(glamState);
13733
14823
  // @ts-ignore
13734
14824
  return await this.base.program.methods.driftInitializeUserStats().accounts({
13735
14825
  glamState,
13736
14826
  glamSigner,
13737
- state,
14827
+ state: this.driftStatePda,
13738
14828
  userStats
13739
14829
  }).instruction();
13740
14830
  }
13741
14831
  async initializeUserIx(glamState, glamSigner, subAccountId) {
13742
14832
  const name = `GLAM *.+ ${subAccountId}`.split("").map((char)=>char.charCodeAt(0)).concat(Array(24).fill(0));
13743
- const [user, userStats] = this.getUser(glamState, subAccountId);
13744
- const state = await getDriftStateAccountPublicKey(DRIFT_PROGRAM_ID);
13745
- const remainingAccounts = this.getGlamReferrer().map((p)=>({
13746
- pubkey: p,
14833
+ const { user, userStats } = this.getDriftUserPdas(glamState, subAccountId);
14834
+ const { user: referrer, userStats: referrerStats } = this.getGlamReferrerPdas();
14835
+ const remainingAccounts = [
14836
+ {
14837
+ pubkey: referrer,
13747
14838
  isWritable: true,
13748
14839
  isSigner: false
13749
- }));
14840
+ },
14841
+ {
14842
+ pubkey: referrerStats,
14843
+ isWritable: true,
14844
+ isSigner: false
14845
+ }
14846
+ ];
13750
14847
  return await this.base.program.methods.driftInitializeUser(subAccountId, name).accounts({
13751
14848
  glamState,
13752
14849
  user,
13753
14850
  userStats,
13754
- state,
14851
+ state: this.driftStatePda,
13755
14852
  glamSigner
13756
14853
  }).remainingAccounts(remainingAccounts).instruction();
13757
14854
  }
@@ -13759,7 +14856,7 @@ class DriftClient {
13759
14856
  const glamSigner = txOptions.signer || this.base.getSigner();
13760
14857
  const tx = new Transaction();
13761
14858
  // Create userStats account if it doesn't exist
13762
- const [_, userStats] = this.getUser(glamState);
14859
+ const { userStats } = this.getDriftUserPdas(glamState);
13763
14860
  const userStatsInfo = await this.base.provider.connection.getAccountInfo(userStats);
13764
14861
  if (!userStatsInfo) {
13765
14862
  tx.add(await this.initializeUserStatsIx(glamState, glamSigner));
@@ -13770,7 +14867,7 @@ class DriftClient {
13770
14867
  }
13771
14868
  async updateUserCustomMarginRatioIx(glamState, maxLeverage, subAccountId = 0, txOptions = {}) {
13772
14869
  const glamSigner = txOptions.signer || this.base.getSigner();
13773
- const [user] = this.getUser(glamState, subAccountId);
14870
+ const { user } = this.getDriftUserPdas(glamState, subAccountId);
13774
14871
  // https://github.com/drift-labs/protocol-v2/blob/babed162b08b1fe34e49a81c5aa3e4ec0a88ecdf/programs/drift/src/math/constants.rs#L183-L184
13775
14872
  const marginRatio = DRIFT_MARGIN_PRECISION / maxLeverage;
13776
14873
  return await this.base.program.methods.driftUpdateUserCustomMarginRatio(subAccountId, marginRatio).accounts({
@@ -13785,7 +14882,7 @@ class DriftClient {
13785
14882
  }
13786
14883
  async updateUserMarginTradingEnabledIx(glamState, marginTradingEnabled, subAccountId = 0, txOptions = {}) {
13787
14884
  const glamSigner = txOptions.signer || this.base.getSigner();
13788
- const [user] = this.getUser(glamState, subAccountId);
14885
+ const { user } = this.getDriftUserPdas(glamState, subAccountId);
13789
14886
  return await this.base.program.methods.driftUpdateUserMarginTradingEnabled(subAccountId, marginTradingEnabled).accounts({
13790
14887
  glamState,
13791
14888
  glamSigner,
@@ -13798,7 +14895,7 @@ class DriftClient {
13798
14895
  }
13799
14896
  async updateUserDelegateIx(glamState, delegate, subAccountId = 0, txOptions = {}) {
13800
14897
  const glamSigner = txOptions.signer || this.base.getSigner();
13801
- const [user] = this.getUser(glamState, subAccountId);
14898
+ const { user } = this.getDriftUserPdas(glamState, subAccountId);
13802
14899
  return await this.base.program.methods.driftUpdateUserDelegate(subAccountId, new PublicKey(delegate)).accounts({
13803
14900
  glamState,
13804
14901
  glamSigner,
@@ -13811,32 +14908,28 @@ class DriftClient {
13811
14908
  }
13812
14909
  async deleteUserTx(glamState, subAccountId = 0, txOptions = {}) {
13813
14910
  const glamSigner = txOptions.signer || this.base.getSigner();
13814
- const [user, userStats] = this.getUser(glamState, subAccountId);
13815
- const state = await getDriftStateAccountPublicKey(DRIFT_PROGRAM_ID);
14911
+ const { user, userStats } = this.getDriftUserPdas(glamState, subAccountId);
13816
14912
  const tx = await this.base.program.methods.driftDeleteUser().accounts({
13817
14913
  glamState,
13818
- state,
14914
+ state: this.driftStatePda,
13819
14915
  user,
13820
14916
  userStats,
13821
14917
  glamSigner
13822
14918
  }).transaction();
13823
14919
  return await this.base.intoVersionedTransaction(tx, txOptions);
13824
14920
  }
13825
- async depositTx(glamState, amount, marketIndex = 1, subAccountId = 0, marketConfigs, txOptions = {}) {
14921
+ async depositTx(glamState, amount, marketIndex = 1, subAccountId = 0, txOptions = {}) {
13826
14922
  const glamSigner = txOptions.signer || this.base.getSigner();
13827
- const [user, userStats] = this.getUser(glamState, subAccountId);
13828
- const state = await getDriftStateAccountPublicKey(DRIFT_PROGRAM_ID);
13829
- const { mint, oracle, marketPDA, vaultPDA } = marketConfigs.spot[marketIndex];
13830
- console.log("mint, oracle, marketPDA, vaultPDA", mint, oracle, marketPDA, vaultPDA);
14923
+ const { user, userStats } = this.getDriftUserPdas(glamState, subAccountId);
14924
+ const { mint, oracle, tokenProgram, marketPda, vault: driftVault } = await this.fetchAndParseSpotMarket(marketIndex);
14925
+ console.log(`Spot market ${marketIndex} mint ${mint}, oracle: ${oracle}, marketPda: ${marketPda}, vault: ${driftVault}`);
13831
14926
  const preInstructions = [];
13832
14927
  const postInstructions = [];
13833
14928
  // If drift user doesn't exist, prepend initializeUserStats and initializeUser instructions
13834
- try {
13835
- await this.fetchGlamDriftUser(glamState, subAccountId);
13836
- } catch (_) {
14929
+ if (!await this.fetchDriftUser(glamState, subAccountId)) {
13837
14930
  preInstructions.push(await this.initializeUserStatsIx(glamState, glamSigner), await this.initializeUserIx(glamState, glamSigner, subAccountId));
13838
14931
  }
13839
- if (mint === WSOL.toBase58()) {
14932
+ if (mint.equals(WSOL)) {
13840
14933
  const wrapSolIxs = await this.base.maybeWrapSol(glamState, amount, glamSigner);
13841
14934
  preInstructions.push(...wrapSolIxs);
13842
14935
  // If we need to wrap SOL, it means the wSOL balance will be drained,
@@ -13850,79 +14943,97 @@ class DriftClient {
13850
14943
  }).instruction();
13851
14944
  postInstructions.push(closeTokenAccountIx);
13852
14945
  }
13853
- const tx = await this.base.program.methods.driftDeposit(marketIndex, amount, false).accounts({
13854
- glamState,
13855
- state,
13856
- user,
13857
- userStats,
13858
- spotMarketVault: new PublicKey(vaultPDA),
13859
- userTokenAccount: this.base.getVaultAta(glamState, new PublicKey(mint)),
13860
- glamSigner,
13861
- tokenProgram: TOKEN_PROGRAM_ID
13862
- }).remainingAccounts([
14946
+ const remainingAccounts = [
13863
14947
  {
13864
14948
  pubkey: new PublicKey(oracle),
13865
14949
  isSigner: false,
13866
14950
  isWritable: false
13867
14951
  },
13868
14952
  {
13869
- pubkey: new PublicKey(marketPDA),
14953
+ pubkey: new PublicKey(marketPda),
13870
14954
  isSigner: false,
13871
14955
  isWritable: true
13872
14956
  }
13873
- ]).preInstructions(preInstructions).postInstructions(postInstructions).transaction();
14957
+ ];
14958
+ if (tokenProgram.equals(TOKEN_2022_PROGRAM_ID)) {
14959
+ remainingAccounts.push({
14960
+ pubkey: mint,
14961
+ isSigner: false,
14962
+ isWritable: false
14963
+ });
14964
+ }
14965
+ const tx = await this.base.program.methods.driftDeposit(marketIndex, amount, false).accounts({
14966
+ glamState,
14967
+ state: this.driftStatePda,
14968
+ user,
14969
+ userStats,
14970
+ spotMarketVault: driftVault,
14971
+ userTokenAccount: this.base.getVaultAta(glamState, mint, tokenProgram),
14972
+ glamSigner,
14973
+ tokenProgram
14974
+ }).remainingAccounts(remainingAccounts).preInstructions(preInstructions).postInstructions(postInstructions).transaction();
13874
14975
  return await this.base.intoVersionedTransaction(tx, txOptions);
13875
14976
  }
13876
- async withdrawTx(statePda, amount, marketIndex = 1, subAccountId = 0, marketConfigs, txOptions = {}) {
14977
+ async withdrawTx(statePda, amount, marketIndex = 1, subAccountId = 0, txOptions = {}) {
13877
14978
  const glamSigner = txOptions.signer || this.base.getSigner();
13878
- const [user, userStats] = this.getUser(statePda, subAccountId);
13879
- const state = await getDriftStateAccountPublicKey(DRIFT_PROGRAM_ID);
13880
- const { mint: m, vaultPDA: d } = marketConfigs.spot[marketIndex];
13881
- const mint = new PublicKey(m);
13882
- const driftAta = new PublicKey(d); // drift vault ata
13883
- const vault = this.base.getVaultPda(statePda);
13884
- const vaultAta = this.base.getVaultAta(statePda, mint); // glam vault ata
13885
- const remainingAccounts = await this.composeRemainingAccounts(statePda, subAccountId, marketConfigs, MarketType.SPOT, marketIndex);
13886
- const { tokenProgram } = await this.base.fetchMintWithOwner(mint);
14979
+ const { user, userStats } = this.getDriftUserPdas(statePda, subAccountId);
14980
+ const { mint, tokenProgram, vault: driftVault } = await this.fetchAndParseSpotMarket(marketIndex);
14981
+ const glamVault = this.base.getVaultPda(statePda);
14982
+ const glamVaultAta = this.base.getVaultAta(statePda, mint, tokenProgram);
14983
+ const remainingAccounts = await this.composeRemainingAccounts(statePda, subAccountId, MarketType.SPOT, marketIndex);
14984
+ if (tokenProgram.equals(TOKEN_2022_PROGRAM_ID)) {
14985
+ remainingAccounts.push({
14986
+ pubkey: mint,
14987
+ isSigner: false,
14988
+ isWritable: false
14989
+ });
14990
+ }
13887
14991
  // Create vault ata in case it doesn't exist
13888
14992
  const preInstructions = [
13889
- createAssociatedTokenAccountIdempotentInstruction(glamSigner, vaultAta, vault, mint, tokenProgram)
14993
+ createAssociatedTokenAccountIdempotentInstruction(glamSigner, glamVaultAta, glamVault, mint, tokenProgram)
13890
14994
  ];
13891
14995
  const tx = await this.base.program.methods.driftWithdraw(marketIndex, amount, false).accounts({
13892
14996
  glamState: statePda,
13893
- state,
14997
+ state: this.driftStatePda,
13894
14998
  user,
13895
14999
  userStats,
13896
15000
  glamSigner,
13897
- spotMarketVault: driftAta,
13898
- userTokenAccount: vaultAta,
13899
- driftSigner: DRIFT_VAULT,
13900
- tokenProgram: TOKEN_PROGRAM_ID
15001
+ spotMarketVault: driftVault,
15002
+ userTokenAccount: glamVaultAta,
15003
+ driftSigner: DRIFT_SIGNER,
15004
+ tokenProgram
13901
15005
  }).remainingAccounts(remainingAccounts).preInstructions(preInstructions).transaction();
13902
15006
  return await this.base.intoVersionedTransaction(tx, txOptions);
13903
15007
  }
13904
- async placeOrderTx(glamState, orderParams, subAccountId = 0, marketConfigs, txOptions = {}) {
15008
+ async placeOrderTx(glamState, orderParams, subAccountId = 0, txOptions = {}) {
13905
15009
  const { marketIndex, marketType } = orderParams;
13906
- const remainingAccounts = (await this.composeRemainingAccounts(glamState, subAccountId, marketConfigs, marketType, marketIndex)).concat(this.getGlamReferrer().map((p)=>({
13907
- pubkey: p,
15010
+ const { user: referrer, userStats: referrerStats } = this.getGlamReferrerPdas();
15011
+ const remainingAccounts = (await this.composeRemainingAccounts(glamState, subAccountId, marketType, marketIndex)).concat([
15012
+ {
15013
+ pubkey: referrer,
13908
15014
  isWritable: true,
13909
15015
  isSigner: false
13910
- })));
15016
+ },
15017
+ {
15018
+ pubkey: referrerStats,
15019
+ isWritable: true,
15020
+ isSigner: false
15021
+ }
15022
+ ]);
13911
15023
  const glamSigner = txOptions.signer || this.base.getSigner();
13912
- const [user] = this.getUser(glamState, subAccountId);
13913
- const state = await getDriftStateAccountPublicKey(DRIFT_PROGRAM_ID);
13914
- const tx = await this.base.program.methods// @ts-ignore
13915
- .driftPlaceOrders([
15024
+ const { user } = this.getDriftUserPdas(glamState, subAccountId);
15025
+ // @ts-ignore
15026
+ const tx = await this.base.program.methods.driftPlaceOrders([
13916
15027
  orderParams
13917
15028
  ]).accounts({
13918
15029
  glamState,
13919
15030
  user,
13920
- state,
15031
+ state: this.driftStatePda,
13921
15032
  glamSigner
13922
15033
  }).remainingAccounts(remainingAccounts).transaction();
13923
15034
  return await this.base.intoVersionedTransaction(tx, txOptions);
13924
15035
  }
13925
- async modifyOrderTx(statePda, modifyOrderParams, subAccountId = 0, marketConfigs, txOptions = {}) {
15036
+ async modifyOrderTx(statePda, modifyOrderParams, subAccountId = 0, txOptions = {}) {
13926
15037
  // const { marketIndex, marketType } = orderParams;
13927
15038
  // const remainingAccounts = await this.composeRemainingAccounts(
13928
15039
  // statePda,
@@ -13932,65 +15043,62 @@ class DriftClient {
13932
15043
  // marketIndex,
13933
15044
  // );
13934
15045
  const signer = txOptions.signer || this.base.getSigner();
13935
- const [user] = this.getUser(statePda, subAccountId);
13936
- const driftState = await getDriftStateAccountPublicKey(DRIFT_PROGRAM_ID);
15046
+ const { user } = this.getDriftUserPdas(statePda, subAccountId);
13937
15047
  const tx = await this.base.program.methods// @ts-ignore
13938
15048
  .driftModifyOrder(1, modifyOrderParams).accounts({
13939
15049
  glamState: statePda,
13940
15050
  glamSigner: signer,
13941
15051
  user,
13942
- state: driftState
15052
+ state: this.driftStatePda
13943
15053
  })// .remainingAccounts(remainingAccounts)
13944
15054
  .transaction();
13945
15055
  return await this.base.intoVersionedTransaction(tx, txOptions);
13946
15056
  }
13947
- async cancelOrdersTx(glamState, marketType, marketIndex, direction, subAccountId = 0, marketConfigs, txOptions = {}) {
15057
+ async cancelOrdersTx(glamState, marketType, marketIndex, direction, subAccountId = 0, txOptions = {}) {
13948
15058
  const glamSigner = txOptions.signer || this.base.getSigner();
13949
- const [user] = this.getUser(glamState, subAccountId);
13950
- const driftState = await getDriftStateAccountPublicKey(DRIFT_PROGRAM_ID);
13951
- const remainingAccounts = await this.composeRemainingAccounts(glamState, subAccountId, marketConfigs, marketType, marketIndex);
15059
+ const { user } = this.getDriftUserPdas(glamState, subAccountId);
15060
+ const remainingAccounts = await this.composeRemainingAccounts(glamState, subAccountId, marketType, marketIndex);
13952
15061
  // @ts-ignore
13953
15062
  const tx = await this.base.program.methods.driftCancelOrders(marketType, marketIndex, direction).accounts({
13954
15063
  glamState,
13955
15064
  glamSigner,
13956
15065
  user,
13957
- state: driftState
15066
+ state: this.driftStatePda
13958
15067
  }).remainingAccounts(remainingAccounts).transaction();
13959
15068
  return await this.base.intoVersionedTransaction(tx, txOptions);
13960
15069
  }
13961
- async cancelOrdersByIdsTx(glamState, orderIds, subAccountId = 0, marketConfigs, txOptions = {}) {
15070
+ async cancelOrdersByIdsTx(glamState, orderIds, subAccountId = 0, txOptions = {}) {
13962
15071
  const glamSigner = txOptions.signer || this.base.getSigner();
13963
- const [user] = this.getUser(glamState, subAccountId);
13964
- const driftState = await getDriftStateAccountPublicKey(DRIFT_PROGRAM_ID);
13965
- const remainingAccounts = await this.composeRemainingAccounts(glamState, subAccountId, marketConfigs);
15072
+ const { user } = this.getDriftUserPdas(glamState, subAccountId);
15073
+ const remainingAccounts = await this.composeRemainingAccounts(glamState, subAccountId);
15074
+ // @ts-ignore
13966
15075
  const tx = await this.base.program.methods.driftCancelOrdersByIds(orderIds).accounts({
13967
15076
  glamState,
13968
15077
  glamSigner,
13969
15078
  user,
13970
- state: driftState
15079
+ state: this.driftStatePda
13971
15080
  }).remainingAccounts(remainingAccounts).transaction();
13972
15081
  return await this.base.intoVersionedTransaction(tx, txOptions);
13973
15082
  }
13974
- async settlePnlTx(glamState, marketIndex, subAccountId = 0, marketConfigs, txOptions = {}) {
15083
+ async settlePnlTx(glamState, marketIndex, subAccountId = 0, txOptions = {}) {
13975
15084
  const glamSigner = txOptions.signer || this.base.getSigner();
13976
- const [user] = this.getUser(glamState, subAccountId);
13977
- const driftState = await getDriftStateAccountPublicKey(DRIFT_PROGRAM_ID);
13978
- const { vaultPDA } = marketConfigs.spot[marketIndex];
15085
+ const { user } = this.getDriftUserPdas(glamState, subAccountId);
15086
+ const { vault: driftVault } = await this.fetchAndParseSpotMarket(marketIndex);
15087
+ // @ts-ignore
13979
15088
  const tx = await this.base.program.methods.driftSettlePnl(marketIndex).accounts({
13980
15089
  glamState,
13981
15090
  glamSigner,
13982
15091
  user,
13983
- state: driftState,
13984
- spotMarketVault: new PublicKey(vaultPDA)
15092
+ state: this.driftStatePda,
15093
+ spotMarketVault: driftVault
13985
15094
  }).transaction();
13986
15095
  return await this.base.intoVersionedTransaction(tx, txOptions);
13987
15096
  }
13988
- async priceDriftTx(glamState, marketConfigs, priceDenom, txOptions = {}) {
15097
+ async priceDriftTx(glamState, priceDenom, txOptions = {}) {
13989
15098
  const signer = txOptions.signer || this.base.getSigner();
13990
15099
  const glamVault = this.base.getVaultPda(glamState);
13991
- const [user, userStats] = this.getUser(glamState);
13992
- const driftState = await getDriftStateAccountPublicKey(DRIFT_PROGRAM_ID);
13993
- const remainingAccounts = await this.composeRemainingAccounts(glamState, 0, marketConfigs);
15100
+ const { user, userStats } = this.getDriftUserPdas(glamState);
15101
+ const remainingAccounts = await this.composeRemainingAccounts(glamState, 0);
13994
15102
  const tx = await this.base.program.methods.priceDrift(priceDenom).accounts({
13995
15103
  glamState,
13996
15104
  signer,
@@ -13998,12 +15106,30 @@ class DriftClient {
13998
15106
  user,
13999
15107
  userStats,
14000
15108
  solOracle: SOL_ORACLE,
14001
- state: driftState
15109
+ state: this.driftStatePda
14002
15110
  }).remainingAccounts(remainingAccounts).transaction();
14003
15111
  return await this.base.intoVersionedTransaction(tx, txOptions);
14004
15112
  }
14005
15113
  constructor(base){
14006
15114
  this.base = base;
15115
+ this.spotMarkets = new Map();
15116
+ this.perpMarkets = new Map();
15117
+ this.marketConfigs = null;
15118
+ this./*
15119
+ * Utils
15120
+ */ getMarketPda = (marketType, marketIndex)=>PublicKey.findProgramAddressSync([
15121
+ Buffer.from(`${marketType === MarketType.PERP ? "perp" : "spot"}_market`),
15122
+ new anchor.BN(marketIndex).toArrayLike(Buffer, "le", 2)
15123
+ ], DRIFT_PROGRAM_ID)[0];
15124
+ this.getUserPda = (authority, subAccountId = 0)=>PublicKey.findProgramAddressSync([
15125
+ Buffer.from("user"),
15126
+ authority.toBuffer(),
15127
+ new anchor.BN(subAccountId).toArrayLike(Buffer, "le", 2)
15128
+ ], DRIFT_PROGRAM_ID)[0];
15129
+ this.getUserStatsPda = (authority)=>PublicKey.findProgramAddressSync([
15130
+ Buffer.from("user_stats"),
15131
+ authority.toBuffer()
15132
+ ], DRIFT_PROGRAM_ID)[0];
14007
15133
  this.marketTypeEquals = (a, b)=>a && Object.keys(a)[0] === Object.keys(b)[0];
14008
15134
  }
14009
15135
  }
@@ -17366,4 +18492,39 @@ const getPriorityFeeEstimate = async (heliusApiKey, tx, accountKeys, priorityLev
17366
18492
  return data.result.priorityFeeEstimate;
17367
18493
  };
17368
18494
 
17369
- export { ASSETS_MAINNET, ASSETS_TESTS, BaseClient, ClusterNetwork, CompanyModel, CreatedModel, DRIFT_PROGRAM_ID, DelegateAcl, DriftClient, FundOpenfundsModel, GLAM_REFERRER, GOVERNANCE_PROGRAM_ID, GlamClient, GlamError, GlamIdl, GlamIntegrations, GlamPermissions, GlamProtocolIdlJson, JITOSOL, JITO_STAKE_POOL, JITO_TIP_DEFAULT, JUP, JUPITER_API_DEFAULT, JUPITER_PROGRAM_ID, JUPSOL_STAKE_POOL, JUP_VOTE_PROGRAM, JupiterSwapClient, JupiterVoteClient, KAMINO_FARM_PROGRAM, KAMINO_LENDING_PROGRAM, KAMINO_OBTRIGATION_SIZE, MARINADE_PROGRAM_ID, MARINADE_TICKET_SIZE, MEMO_PROGRAM, MERKLE_DISTRIBUTOR_PROGRAM, METEORA_DLMM_PROGRAM, METEORA_POSITION_SIZE, MSOL, ManagerModel, Metadata, MintIdlModel, MintModel, MintOpenfundsModel, PriceDenom, SANCTUM_STAKE_POOL_PROGRAM_ID, SEED_ESCROW, SEED_METADATA, SEED_MINT, SEED_STATE, SEED_VAULT, SOL_ORACLE, STAKE_ACCOUNT_SIZE, StateIdlModel, StateModel, TRANSFER_HOOK_PROGRAM, USDC, WBTC, WETH, WSOL, fetchKaminoObligations, fetchMarinadeTicketAccounts, fetchMeteoraPositions, fetchStakeAccounts, getGlamProgram, getGlamProgramId, getPriorityFeeEstimate, getSimulationComputeUnits, isBrowser, parseMeteoraPosition };
18495
+ function getLimitOrderParams(params) {
18496
+ return getOrderParams(Object.assign({}, params, {
18497
+ orderType: OrderType.LIMIT
18498
+ }));
18499
+ }
18500
+ function getTriggerMarketOrderParams(params) {
18501
+ return getOrderParams(Object.assign({}, params, {
18502
+ orderType: OrderType.TRIGGER_MARKET
18503
+ }));
18504
+ }
18505
+ function getTriggerLimitOrderParams(params) {
18506
+ return getOrderParams(Object.assign({}, params, {
18507
+ orderType: OrderType.TRIGGER_LIMIT
18508
+ }));
18509
+ }
18510
+ function getMarketOrderParams(params) {
18511
+ return getOrderParams(Object.assign({}, params, {
18512
+ orderType: OrderType.MARKET
18513
+ }));
18514
+ }
18515
+ /**
18516
+ * Creates an OrderParams object with the given OptionalOrderParams and any params to override.
18517
+ *
18518
+ * example:
18519
+ * ```
18520
+ * const orderParams = getOrderParams(optionalOrderParams, { marketType: MarketType.PERP });
18521
+ * ```
18522
+ *
18523
+ * @param optionalOrderParams
18524
+ * @param overridingParams
18525
+ * @returns
18526
+ */ function getOrderParams(optionalOrderParams, overridingParams = {}) {
18527
+ return Object.assign({}, DefaultOrderParams, optionalOrderParams, overridingParams);
18528
+ }
18529
+
18530
+ export { ASSETS_MAINNET, ASSETS_TESTS, AssetTier, BaseClient, ClusterNetwork, CompanyModel, ContractTier, ContractType, CreatedModel, DRIFT_PROGRAM_ID, DefaultOrderParams, DelegateAcl, DepositDirection, DepositExplanation, DriftClient, ExchangeStatus, FuelOverflowStatus, FundOpenfundsModel, GLAM_REFERRER, GOVERNANCE_PROGRAM_ID, GlamClient, GlamError, GlamIdl, GlamIntegrations, GlamPermissions, GlamProtocolIdlJson, InsuranceFundOperation, JITOSOL, JITO_STAKE_POOL, JITO_TIP_DEFAULT, JUP, JUPITER_API_DEFAULT, JUPITER_PROGRAM_ID, JUPSOL_STAKE_POOL, JUP_VOTE_PROGRAM, JupiterSwapClient, JupiterVoteClient, KAMINO_FARM_PROGRAM, KAMINO_LENDING_PROGRAM, KAMINO_OBTRIGATION_SIZE, LPAction, LiquidationType, MARINADE_PROGRAM_ID, MARINADE_TICKET_SIZE, MEMO_PROGRAM, MERKLE_DISTRIBUTOR_PROGRAM, METEORA_DLMM_PROGRAM, METEORA_POSITION_SIZE, MSOL, ManagerModel, MarginMode, MarketStatus, MarketType, Metadata, MintIdlModel, MintModel, MintOpenfundsModel, ModifyOrderPolicy, OracleSource, OracleSourceNum, OrderAction, OrderActionExplanation, OrderStatus, OrderTriggerCondition, OrderType, PerpOperation, PlaceAndTakeOrderSuccessCondition, PositionDirection, PostOnlyParams, PriceDenom, ReferrerStatus, SANCTUM_STAKE_POOL_PROGRAM_ID, SEED_ESCROW, SEED_METADATA, SEED_MINT, SEED_STATE, SEED_VAULT, SOL_ORACLE, STAKE_ACCOUNT_SIZE, SettlePnlExplanation, SettlePnlMode, SpotBalanceType, SpotFulfillmentConfigStatus, SpotFulfillmentStatus, SpotFulfillmentType, SpotOperation, StakeAction, StateIdlModel, StateModel, SwapDirection, SwapReduceOnly, TRANSFER_HOOK_PROGRAM, TradeSide, USDC, UserStatus, WBTC, WETH, WSOL, ZERO, decodeUser, fetchKaminoObligations, fetchMarinadeTicketAccounts, fetchMeteoraPositions, fetchStakeAccounts, getGlamProgram, getGlamProgramId, getLimitOrderParams, getMarketOrderParams, getOrderParams, getPriorityFeeEstimate, getSimulationComputeUnits, getTriggerLimitOrderParams, getTriggerMarketOrderParams, getVariant, isBrowser, isOneOfVariant, isVariant, parseMeteoraPosition, setsAreEqual };