@@ -6,6 +6,7 @@ import { useTransactionMetadataRequest } from '../transactions/useTransactionMet
66import { useTransactionPayWithdraw } from './useTransactionPayWithdraw' ;
77import Engine from '../../../../../core/Engine' ;
88import { computeProxyAddress } from '../../../../UI/Predict/providers/polymarket/safe/utils' ;
9+ import { usePredictAccountState } from '../../../../UI/Predict/hooks/usePredictAccountState' ;
910
1011jest . mock ( '../transactions/useTransactionMetadataRequest' ) ;
1112jest . mock ( './useTransactionPayWithdraw' ) ;
@@ -14,14 +15,12 @@ jest.mock('../../../../../core/Engine', () => ({
1415 TransactionPayController : {
1516 setTransactionConfig : jest . fn ( ) ,
1617 } ,
17- PredictController : {
18- getAccountState : jest . fn ( ) ,
19- } ,
2018 } ,
2119} ) ) ;
2220jest . mock ( '../../../../UI/Predict/providers/polymarket/safe/utils' , ( ) => ( {
2321 computeProxyAddress : jest . fn ( ) ,
2422} ) ) ;
23+ jest . mock ( '../../../../UI/Predict/hooks/usePredictAccountState' ) ;
2524
2625const TRANSACTION_ID_MOCK = 'transaction-123' ;
2726const FROM_MOCK = '0x1234567890123456789012345678901234567890' as Hex ;
@@ -35,22 +34,24 @@ describe('useTransactionPayPostQuote', () => {
3534 const setTransactionConfigMock = jest . mocked (
3635 Engine . context . TransactionPayController . setTransactionConfig ,
3736 ) ;
38- const getAccountStateMock = jest . mocked (
39- Engine . context . PredictController . getAccountState ,
40- ) ;
37+ const usePredictAccountStateMock = jest . mocked ( usePredictAccountState ) ;
4138 const computeProxyAddressMock = jest . mocked ( computeProxyAddress ) ;
4239
43- const flushPromises = ( ) => new Promise ( setImmediate ) ;
40+ function mockAccountState ( walletType : 'safe' | 'deposit-wallet' ) : void {
41+ usePredictAccountStateMock . mockReturnValue ( {
42+ data : {
43+ address : '0xProxyAddress' ,
44+ isDeployed : true ,
45+ walletType,
46+ } ,
47+ isLoading : false ,
48+ } as never ) ;
49+ }
4450
4551 beforeEach ( ( ) => {
4652 jest . clearAllMocks ( ) ;
4753 computeProxyAddressMock . mockReturnValue ( PROXY_ADDRESS_MOCK ) ;
48- getAccountStateMock . mockResolvedValue ( {
49- address : '0xProxyAddress' ,
50- isDeployed : true ,
51- walletType : 'safe' ,
52- balance : 100 ,
53- } as never ) ;
54+ mockAccountState ( 'safe' ) ;
5455 useTransactionPayWithdrawMock . mockReturnValue ( {
5556 isWithdraw : false ,
5657 canSelectWithdrawToken : false ,
@@ -292,66 +293,68 @@ describe('useTransactionPayPostQuote', () => {
292293 } ) ;
293294 } ) ;
294295
295- it ( 'flags transaction and clears refundTo when walletType is deposit-wallet' , async ( ) => {
296- getAccountStateMock . mockResolvedValue ( {
297- address : '0xDepositWallet' ,
298- isDeployed : true ,
299- walletType : 'deposit-wallet' ,
300- balance : 50 ,
301- } as never ) ;
296+ it ( 'flags transaction and skips refundTo when walletType is deposit-wallet' , ( ) => {
297+ mockAccountState ( 'deposit-wallet' ) ;
302298
303299 renderHook ( ( ) => useTransactionPayPostQuote ( ) ) ;
304- await flushPromises ( ) ;
305300
306- expect ( setTransactionConfigMock ) . toHaveBeenCalledTimes ( 2 ) ;
301+ expect ( setTransactionConfigMock ) . toHaveBeenCalledTimes ( 1 ) ;
307302
308- const followUpCallback = setTransactionConfigMock . mock . calls [ 1 ] [ 1 ] ;
309- const config = {
310- refundTo : PROXY_ADDRESS_MOCK ,
311- } as {
303+ const callback = setTransactionConfigMock . mock . calls [ 0 ] [ 1 ] ;
304+ const config = { } as {
305+ isPostQuote ?: boolean ;
312306 isPolymarketDepositWallet ?: boolean ;
313307 refundTo ?: Hex ;
314308 } ;
315- followUpCallback ( config ) ;
309+ callback ( config ) ;
316310
311+ expect ( config . isPostQuote ) . toBe ( true ) ;
317312 expect ( config . isPolymarketDepositWallet ) . toBe ( true ) ;
318313 expect ( config . refundTo ) . toBeUndefined ( ) ;
314+ expect ( computeProxyAddressMock ) . not . toHaveBeenCalled ( ) ;
319315 } ) ;
320316
321- it ( 'does not set deposit-wallet flag when walletType is safe' , async ( ) => {
322- getAccountStateMock . mockResolvedValue ( {
323- address : '0xSafeProxy' ,
324- isDeployed : true ,
325- walletType : 'safe' ,
326- balance : 100 ,
327- } as never ) ;
317+ it ( 'does not set deposit-wallet flag when walletType is safe' , ( ) => {
318+ mockAccountState ( 'safe' ) ;
328319
329320 renderHook ( ( ) => useTransactionPayPostQuote ( ) ) ;
330- await flushPromises ( ) ;
331321
332322 expect ( setTransactionConfigMock ) . toHaveBeenCalledTimes ( 1 ) ;
323+
324+ const callback = setTransactionConfigMock . mock . calls [ 0 ] [ 1 ] ;
325+ const config = { } as {
326+ isPostQuote ?: boolean ;
327+ isPolymarketDepositWallet ?: boolean ;
328+ refundTo ?: Hex ;
329+ } ;
330+ callback ( config ) ;
331+
332+ expect ( config . isPolymarketDepositWallet ) . toBeUndefined ( ) ;
333333 } ) ;
334334
335- it ( 'does not resolve account state for non-predictWithdraw flows' , async ( ) => {
336- useTransactionMetadataRequestMock . mockReturnValue ( {
337- id : TRANSACTION_ID_MOCK ,
338- txParams : { from : FROM_MOCK } ,
339- type : TransactionType . perpsWithdraw ,
335+ it ( 'defers setTransactionConfig until predict account state resolves' , ( ) => {
336+ usePredictAccountStateMock . mockReturnValue ( {
337+ data : undefined ,
338+ isLoading : true ,
340339 } as never ) ;
341340
342341 renderHook ( ( ) => useTransactionPayPostQuote ( ) ) ;
343- await flushPromises ( ) ;
344342
345- expect ( getAccountStateMock ) . not . toHaveBeenCalled ( ) ;
346- expect ( setTransactionConfigMock ) . toHaveBeenCalledTimes ( 1 ) ;
343+ expect ( setTransactionConfigMock ) . not . toHaveBeenCalled ( ) ;
347344 } ) ;
348345
349- it ( 'swallows getAccountState errors without setting the deposit-wallet flag' , async ( ) => {
350- getAccountStateMock . mockRejectedValue ( new Error ( 'boom' ) ) ;
346+ it ( 'does not resolve account state for non-predictWithdraw flows' , ( ) => {
347+ useTransactionMetadataRequestMock . mockReturnValue ( {
348+ id : TRANSACTION_ID_MOCK ,
349+ txParams : { from : FROM_MOCK } ,
350+ type : TransactionType . perpsWithdraw ,
351+ } as never ) ;
351352
352353 renderHook ( ( ) => useTransactionPayPostQuote ( ) ) ;
353- await flushPromises ( ) ;
354354
355+ expect ( usePredictAccountStateMock ) . toHaveBeenCalledWith ( {
356+ enabled : false ,
357+ } ) ;
355358 expect ( setTransactionConfigMock ) . toHaveBeenCalledTimes ( 1 ) ;
356359 } ) ;
357360 } ) ;
0 commit comments