@@ -43,7 +43,7 @@ import {
4343import type { RootStore } from '@/state/_store' ;
4444import { store as reduxStore } from '@/state/_store' ;
4545import { getSubaccountId , getUserWalletAddress } from '@/state/accountInfoSelectors' ;
46- import { getSelectedNetwork } from '@/state/appSelectors' ;
46+ import { getSelectedDydxChainId , getSelectedNetwork } from '@/state/appSelectors' ;
4747import { createAppSelector } from '@/state/appTypes' ;
4848import {
4949 cancelAllSubmitted ,
@@ -54,7 +54,7 @@ import {
5454 placeOrderSubmitted ,
5555 placeOrderTimeout ,
5656} from '@/state/localOrders' ;
57- import { getLocalWalletNonce } from '@/state/walletSelectors' ;
57+ import { getLocalWalletNonce , selectIsKeplrConnected } from '@/state/walletSelectors' ;
5858
5959import { track } from '@/lib/analytics/analytics' ;
6060import { calc } from '@/lib/do' ;
@@ -79,13 +79,15 @@ import { StateConditionNotifier, Tracker } from './StateConditionNotifier';
7979import { getSimpleOrderStatus } from './calculators/orders' ;
8080import { TradeFormPayload } from './forms/trade/types' ;
8181import { PlaceOrderMarketInfo , PlaceOrderPayload } from './forms/triggers/types' ;
82+ import { getLazyLocalWallet } from './lib/lazyDynamicLibs' ;
8283import { CompositeClientManager } from './rest/lib/compositeClientManager' ;
8384import { estimateLiveValidatorHeight } from './selectors/apiStatus' ;
8485
8586interface TransactionSupervisorShared {
8687 store : RootStore ;
8788 compositeClientManager : typeof CompositeClientManager ;
8889 stateNotifier : StateConditionNotifier ;
90+ maybeDydxLocalWallet ?: LocalWallet | null ;
8991}
9092
9193const selectOrdersAndFills = createAppSelector (
@@ -111,10 +113,13 @@ export const SHORT_TERM_ORDER_DURATION_SAFETY_MARGIN = 5;
111113export class AccountTransactionSupervisor {
112114 private store : RootStore ;
113115
116+ private cachedDydxLocalWallet : LocalWallet | null ;
117+
114118 private shared : TransactionSupervisorShared ;
115119
116120 constructor ( store : RootStore , compositeClientManager : typeof CompositeClientManager ) {
117121 this . store = store ;
122+ this . cachedDydxLocalWallet = null ;
118123
119124 this . shared = {
120125 compositeClientManager,
@@ -130,9 +135,11 @@ export class AccountTransactionSupervisor {
130135 tracking ?: Tracker < P , Q >
131136 ) {
132137 return async ( ) => {
138+ const maybeDydxLocalWallet = await this . getCosmosLocalWallet ( ) ;
139+
133140 const result = await taskBuilder ( { payload : basePayload } )
134141 . with < AddSharedContextMiddlewareProps > (
135- addSharedContextMiddleware ( nameForLogging , this . shared )
142+ addSharedContextMiddleware ( nameForLogging , { ... this . shared , maybeDydxLocalWallet } )
136143 )
137144 . with < ValidateLocalWalletMiddlewareProps > ( validateLocalWalletMiddleware ( ) )
138145 . with < StateTrackingProps < Q > > ( stateTrackingMiddleware ( tracking ) )
@@ -153,6 +160,28 @@ export class AccountTransactionSupervisor {
153160 } ;
154161 }
155162
163+ private async getCosmosLocalWallet ( ) {
164+ const state = this . store . getState ( ) ;
165+ const isKeplrConnected = selectIsKeplrConnected ( state ) ;
166+
167+ if ( isKeplrConnected && window . keplr ) {
168+ if ( this . cachedDydxLocalWallet ) {
169+ return this . cachedDydxLocalWallet ;
170+ }
171+
172+ const chainId = getSelectedDydxChainId ( state ) ;
173+ const dydxOfflineSigner = await window . keplr . getOfflineSigner ( chainId ) ;
174+ const dydxLocalWallet = await (
175+ await getLazyLocalWallet ( )
176+ ) . fromOfflineSigner ( dydxOfflineSigner ) ;
177+
178+ this . cachedDydxLocalWallet = dydxLocalWallet ;
179+ return dydxLocalWallet ;
180+ }
181+
182+ return undefined ;
183+ }
184+
156185 private createCancelOrderPayload ( orderId : string ) : CancelOrderPayload | undefined {
157186 const state = this . store . getState ( ) ;
158187 const orders = BonsaiCore . account . allOrders . data ( state ) ;
@@ -570,10 +599,15 @@ export class AccountTransactionSupervisor {
570599 payloadArg : PlaceOrderPayload ,
571600 source : TradeMetadataSource
572601 ) : Promise < OperationResult < any > > {
602+ const maybeDydxLocalWallet = await this . getCosmosLocalWallet ( ) ;
603+
573604 return (
574605 taskBuilder ( { payload : payloadArg } )
575606 . with < AddSharedContextMiddlewareProps > (
576- addSharedContextMiddleware ( 'AccountTransactionSupervisor/placeOrderWrapper' , this . shared )
607+ addSharedContextMiddleware ( 'AccountTransactionSupervisor/placeOrderWrapper' , {
608+ ...this . shared ,
609+ maybeDydxLocalWallet,
610+ } )
577611 )
578612 . with < ValidateLocalWalletMiddlewareProps > ( validateLocalWalletMiddleware ( ) )
579613 // fully prepare/augment the trade payload
@@ -780,10 +814,14 @@ export class AccountTransactionSupervisor {
780814
781815 public async closeAllPositions ( ) {
782816 track ( AnalyticsEvents . TradeCloseAllPositionsClick ( { } ) ) ;
817+ const maybeDydxLocalWallet = await this . getCosmosLocalWallet ( ) ;
783818
784819 return taskBuilder ( { payload : { } } )
785820 . with < AddSharedContextMiddlewareProps > (
786- addSharedContextMiddleware ( 'AccountTransactionSupervisor/closeAllPositions' , this . shared )
821+ addSharedContextMiddleware ( 'AccountTransactionSupervisor/closeAllPositions' , {
822+ ...this . shared ,
823+ maybeDydxLocalWallet,
824+ } )
787825 )
788826 . with < ValidateLocalWalletMiddlewareProps > ( validateLocalWalletMiddleware ( ) )
789827 . with < { closePayloads : PlaceOrderPayload [ ] } > ( async ( context , next ) => {
@@ -846,10 +884,15 @@ export class AccountTransactionSupervisor {
846884 orderId : string ;
847885 withNotification ?: boolean ;
848886 } ) {
887+ const maybeDydxLocalWallet = await this . getCosmosLocalWallet ( ) ;
888+
849889 return (
850890 taskBuilder ( { payload : { orderId, withNotification } } )
851891 . with < AddSharedContextMiddlewareProps > (
852- addSharedContextMiddleware ( 'AccountTransactionSupervisor/cancelOrder' , this . shared )
892+ addSharedContextMiddleware ( 'AccountTransactionSupervisor/cancelOrder' , {
893+ ...this . shared ,
894+ maybeDydxLocalWallet,
895+ } )
853896 )
854897 . with < ValidateLocalWalletMiddlewareProps > ( validateLocalWalletMiddleware ( ) )
855898 // populate order details
@@ -944,10 +987,14 @@ export class AccountTransactionSupervisor {
944987
945988 public async cancelAllOrders ( { marketId } : { marketId ?: string } ) {
946989 track ( AnalyticsEvents . TradeCancelAllOrdersClick ( { marketId } ) ) ;
990+ const maybeDydxLocalWallet = await this . getCosmosLocalWallet ( ) ;
947991
948992 return taskBuilder ( { payload : { marketId } } )
949993 . with < AddSharedContextMiddlewareProps > (
950- addSharedContextMiddleware ( 'AccountTransactionSupervisor/cancelAllOrders' , this . shared )
994+ addSharedContextMiddleware ( 'AccountTransactionSupervisor/cancelAllOrders' , {
995+ ...this . shared ,
996+ maybeDydxLocalWallet,
997+ } )
951998 )
952999 . with < ValidateLocalWalletMiddlewareProps > ( validateLocalWalletMiddleware ( ) )
9531000 . with < { ordersWithUuids : Array < { order : SubaccountOrder ; uuid : string } > } > (
@@ -1038,6 +1085,8 @@ export class AccountTransactionSupervisor {
10381085 const hasStatefulOperations = isMainOrderStateful || ( order . triggersPayloads ?. length ?? 0 ) > 0 ;
10391086
10401087 if ( hasStatefulOperations ) {
1088+ const maybeDydxLocalWallet = await this . getCosmosLocalWallet ( ) ;
1089+
10411090 return (
10421091 taskBuilder ( {
10431092 payload : {
@@ -1047,10 +1096,10 @@ export class AccountTransactionSupervisor {
10471096 } ,
10481097 } )
10491098 . with < AddSharedContextMiddlewareProps > (
1050- addSharedContextMiddleware (
1051- 'AccountTransactionSupervisor/executeBulkStatefulOrders' ,
1052- this . shared
1053- )
1099+ addSharedContextMiddleware ( 'AccountTransactionSupervisor/executeBulkStatefulOrders' , {
1100+ ... this . shared ,
1101+ maybeDydxLocalWallet ,
1102+ } )
10541103 )
10551104 . with < ValidateLocalWalletMiddlewareProps > ( validateLocalWalletMiddleware ( ) )
10561105 // Prepare payloads with current height and collect cancel/place operations
@@ -1396,6 +1445,10 @@ function validateLocalWalletMiddleware() {
13961445 const state = context . shared . store . getState ( ) ;
13971446 const localWalletNonce = getLocalWalletNonce ( state ) ;
13981447
1448+ if ( context . shared . maybeDydxLocalWallet ) {
1449+ return next ( context ) ;
1450+ }
1451+
13991452 if ( localWalletNonce == null ) {
14001453 const errorMsg = 'No valid local wallet available' ;
14011454 const errSource = context . fnName ;
@@ -1420,14 +1473,16 @@ function addClientAndWalletMiddleware(store: RootStore) {
14201473
14211474 return createMiddleware < AddClientAndWalletMiddlewareProps , AddSharedContextMiddlewareProps > (
14221475 async ( context , next ) => {
1423- const network = getSelectedNetwork ( context . shared . store . getState ( ) ) ;
1424- const localWalletNonce = getLocalWalletNonce ( context . shared . store . getState ( ) ) ;
1476+ const state = context . shared . store . getState ( ) ;
1477+ const network = getSelectedNetwork ( state ) ;
1478+ const localWalletNonce = getLocalWalletNonce ( state ) ;
14251479
14261480 const clientConfig = {
14271481 network,
14281482 dispatch : context . shared . store . dispatch ,
14291483 } ;
14301484 const clientWrapper = context . shared . compositeClientManager . use ( clientConfig ) ;
1485+ const maybeDydxLocalWallet = context . shared . maybeDydxLocalWallet ;
14311486
14321487 try {
14331488 if ( network !== networkBefore ) {
@@ -1436,11 +1491,18 @@ function addClientAndWalletMiddleware(store: RootStore) {
14361491 if ( localWalletNonce !== nonceBefore ) {
14371492 throw new Error ( 'Local wallet changed before operation execution' ) ;
14381493 }
1439- if ( localWalletNonce == null ) {
1440- throw new Error ( 'No valid local wallet nonce found' ) ;
1441- }
14421494
1443- const localWallet = localWalletManager . getLocalWallet ( localWalletNonce ) ;
1495+ const localWallet = calc ( ( ) => {
1496+ if ( maybeDydxLocalWallet ) {
1497+ return maybeDydxLocalWallet ;
1498+ }
1499+
1500+ if ( localWalletNonce == null ) {
1501+ throw new Error ( 'No valid local wallet nonce found' ) ;
1502+ }
1503+
1504+ return localWalletManager . getLocalWallet ( localWalletNonce ) ;
1505+ } ) ;
14441506
14451507 if ( localWallet == null ) {
14461508 throw new Error ( 'Local wallet not initialized or nonce was incorrect.' ) ;
0 commit comments