-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Redesign] Replace explorer API with SDK route.track for in-progress …
…widget (#2835) * [Redesign] Using SDK traker for in-progress widget Signed-off-by: Emre Bogazliyanlioglu <[email protected]> * [Redesign] Fix React warnings Signed-off-by: Emre Bogazliyanlioglu <[email protected]> * Addressing the first round of feedback Signed-off-by: Emre Bogazliyanlioglu <[email protected]> --------- Signed-off-by: Emre Bogazliyanlioglu <[email protected]>
- Loading branch information
Showing
10 changed files
with
175 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
wormhole-connect/src/hooks/useTrackTransferInProgress.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { isCompleted, routes } from '@wormhole-foundation/sdk'; | ||
|
||
import config, { getWormholeContextV2 } from 'config'; | ||
import { sleep } from 'utils'; | ||
|
||
import type { AttestationReceipt } from '@wormhole-foundation/sdk'; | ||
|
||
const TRACK_INTERVAL = 5000; | ||
const TRACK_INTERVAL_FAST = 1000; | ||
const TRACK_TIMEOUT = 120 * 1000; | ||
|
||
type Props = { | ||
route: string; | ||
receipt: routes.Receipt<AttestationReceipt>; | ||
eta?: number; | ||
}; | ||
|
||
type ReturnProps = { | ||
isCompleted: boolean; | ||
}; | ||
|
||
const useTrackTransferV2 = (props: Props): ReturnProps => { | ||
const [completed, setCompleted] = useState(false); | ||
const [receipt, setReceipt] = useState<routes.Receipt<AttestationReceipt>>(); | ||
|
||
const { eta, route: routeName } = props; | ||
|
||
// Set initial receipt from the caller | ||
useEffect(() => { | ||
if (props.receipt) { | ||
setReceipt(props.receipt); | ||
} | ||
}, [props.receipt]); | ||
|
||
useEffect(() => { | ||
let isActive = true; | ||
|
||
let sleepTime = TRACK_INTERVAL; | ||
|
||
if (eta && eta < 30_000) { | ||
// Poll aggressively for very fast transfers | ||
sleepTime = TRACK_INTERVAL_FAST; | ||
} | ||
|
||
const track = async () => { | ||
if (!routeName || !receipt) { | ||
return; | ||
} | ||
|
||
const route = config.routes.get(routeName); | ||
|
||
if (!route) { | ||
return; | ||
} | ||
|
||
const wh = await getWormholeContextV2(); | ||
const sdkRoute = new route.rc(wh); | ||
|
||
let stateChanged = false; | ||
|
||
while (isActive && !isCompleted(receipt) && !stateChanged) { | ||
try { | ||
// We need to consume all of the values the track generator yields in case any of them | ||
// update the receipt state. | ||
for await (const currentReceipt of sdkRoute.track( | ||
receipt, | ||
TRACK_TIMEOUT, | ||
)) { | ||
if (!isActive) { | ||
break; | ||
} | ||
|
||
// When the receipt state is updated, we set the new receipt in the local state | ||
// and break out of the loop. | ||
// The hook will then be re-run and the new receipt will be used to continue tracking | ||
// until the transfer is completed. | ||
if (currentReceipt.state !== receipt.state) { | ||
setReceipt(currentReceipt); | ||
|
||
if (isCompleted(currentReceipt)) { | ||
setCompleted(true); | ||
stateChanged = true; | ||
break; | ||
} | ||
} | ||
} | ||
} catch (e) { | ||
console.error('Error tracking transfer:', e); | ||
sleepTime = TRACK_INTERVAL; // Back off if we were polling aggressively | ||
} | ||
|
||
// Retry | ||
// TODO: exponential backoff depending on the current state? | ||
await sleep(sleepTime); | ||
} | ||
}; | ||
|
||
track(); | ||
|
||
return () => { | ||
isActive = false; | ||
}; | ||
}, [eta, receipt, routeName]); | ||
|
||
return { | ||
isCompleted: completed, | ||
}; | ||
}; | ||
|
||
export default useTrackTransferV2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.