@@ -9,19 +9,26 @@ import {
99} from './utils/getBalances'
1010import { useBlockNavigatingAway } from './utils/useBlockNavigatingAway'
1111import { transferToConsensus } from './withdraw/transferToConsensus'
12- import { useGenerateSapphireAccount } from './withdraw/useGenerateSapphireAccount'
12+ import { ConsensusAccount , SapphireAccount } from './withdraw/useGenerateSapphireAccount'
1313import { minimalWithdrawableAmount , withdrawToConsensus } from './withdraw/withdrawToConsensus'
1414import { trackEvent } from 'fathom-client'
1515import { consensusConfig , sapphireConfig } from './utils/oasisConfig.ts'
16+ import { UnmountedAbortError , useUnmountSignal } from './utils/useUnmountSignal'
1617
1718/**
1819 * sapphireAddress -> generatedSapphireAccount -> generatedConsensusAccount -> consensusAddress
1920 */
20- export function useWithdraw ( ) {
21+ export function useWithdraw ( {
22+ generatedSapphireAccount,
23+ generatedConsensusAccount,
24+ } : {
25+ generatedSapphireAccount : SapphireAccount
26+ generatedConsensusAccount : ConsensusAccount
27+ } ) {
28+ const unmountSignal = useUnmountSignal ( )
2129 const { isBlockingNavigatingAway, blockNavigatingAway, allowNavigatingAway } = useBlockNavigatingAway ( )
2230 const sapphireAddress = useAccount ( ) . address
23- const { generatedSapphireAccount, generatedConsensusAccount, generateSapphireAccount } =
24- useGenerateSapphireAccount ( )
31+
2532 const [ consensusAddress , setConsensusAddress ] = useState < `oasis1${string } `> ( )
2633 const [ progress , setProgress ] = useState ( { percentage : 0 as number | undefined , message : '' } )
2734 const [ isInputMode , setIsInputMode ] = useState ( true )
@@ -31,15 +38,6 @@ export function useWithdraw() {
3138 const { sendTransactionAsync } = useSendTransaction ( )
3239 const isPrevError = usePrevious ( progress . percentage === undefined )
3340
34- async function step2 ( ) {
35- if ( ! sapphireAddress ) return
36- await generateSapphireAccount ( sapphireAddress )
37-
38- if ( generatedConsensusAccount ?. isFresh ) {
39- trackEvent ( 'withdrawal account created' )
40- }
41- }
42-
4341 async function step3 ( value : bigint ) {
4442 if ( ! generatedSapphireAccount ) return
4543
@@ -53,7 +51,6 @@ export function useWithdraw() {
5351 } )
5452 }
5553
56- // Long running promise, doesn't get canceled if this component is destroyed
5754 async function step4 ( consensusAddress : `oasis1${string } `, retryingAfterError : number ) {
5855 // Note: outside state var consensusAddress is outdated. Use param.
5956 if ( ! sapphireAddress ) return
@@ -67,7 +64,8 @@ export function useWithdraw() {
6764 setProgress ( { percentage : 0.05 , message : 'Waiting to move your ROSE…' } )
6865 const availableAmountToWithdraw = await waitForSapphireBalance (
6966 generatedSapphireAccount . address ,
70- minimalWithdrawableAmount
67+ minimalWithdrawableAmount ,
68+ unmountSignal
7169 )
7270 setProgress ( { percentage : 0.25 , message : 'ROSE transfer initiated' } )
7371
@@ -84,21 +82,26 @@ export function useWithdraw() {
8482 }
8583
8684 // TODO: handle probable failure if balance doesn't change after ~10 seconds of withdraw
87- const amountToWithdraw2 = await waitForConsensusBalance ( generatedConsensusAccount . address , 0n )
85+ const amountToWithdraw2 = await waitForConsensusBalance (
86+ generatedConsensusAccount . address ,
87+ 0n ,
88+ unmountSignal
89+ )
8890
8991 trackEvent ( 'withdrawal flow started' , {
9092 _value : fromBaseUnitsToTrackEventCents ( amountToWithdraw2 . raw , consensusConfig . decimals ) ,
9193 } )
9294
9395 const preWithdrawConsensusBalance = await getConsensusBalance ( consensusAddress )
96+ if ( unmountSignal . aborted ) throw new UnmountedAbortError ( )
9497 await transferToConsensus ( {
9598 amount : amountToWithdraw2 . raw ,
9699 fromConsensusAccount : generatedConsensusAccount ,
97100 toConsensusAddress : consensusAddress ,
98101 } )
99102 setProgress ( { percentage : 0.75 , message : `Withdrawing ${ amountToWithdraw2 . formatted } ROSE` } )
100103 if ( window . mock && ! retryingAfterError ) throw 'mock error'
101- await waitForConsensusBalance ( consensusAddress , preWithdrawConsensusBalance . raw )
104+ await waitForConsensusBalance ( consensusAddress , preWithdrawConsensusBalance . raw , unmountSignal )
102105 setProgress ( {
103106 percentage : 1.0 ,
104107 message : 'Your ROSE transfer is complete!' ,
@@ -110,6 +113,7 @@ export function useWithdraw() {
110113
111114 allowNavigatingAway ( ) // Stop blocking unless new transfer comes in
112115 } catch ( err ) {
116+ if ( err instanceof UnmountedAbortError ) return // Ignore and stop looping
113117 console . error ( err )
114118 setProgress ( { percentage : undefined , message : `Error. Retrying…` } )
115119 await new Promise ( r => setTimeout ( r , 6000 ) )
@@ -118,11 +122,17 @@ export function useWithdraw() {
118122 return
119123 }
120124
121- await new Promise ( r => setTimeout ( r , 6000 ) )
122- // Stay on "Withdrawn" screen unless new transfer comes in
123- await waitForSapphireBalance ( generatedSapphireAccount . address , 0n )
124- // Don't loop, force user to input destination again
125- transferMore ( )
125+ try {
126+ await new Promise ( r => setTimeout ( r , 6000 ) )
127+ if ( unmountSignal . aborted ) throw new UnmountedAbortError ( )
128+ // Stay on "Withdrawn" screen unless new transfer comes in
129+ await waitForSapphireBalance ( generatedSapphireAccount . address , 0n , unmountSignal )
130+ if ( unmountSignal . aborted ) throw new UnmountedAbortError ( )
131+ // Don't loop, force user to input destination again
132+ transferMore ( )
133+ } catch ( err ) {
134+ return // Ignore
135+ }
126136 }
127137
128138 function transferMore ( ) {
@@ -131,11 +141,8 @@ export function useWithdraw() {
131141
132142 return {
133143 sapphireAddress,
134- generatedSapphireAccount,
135- generatedConsensusAccount,
136144 consensusAddress,
137145 setConsensusAddress,
138- step2,
139146 step3,
140147 step4,
141148 transferMore,
0 commit comments