-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The SDK currently doesn't set the redeem TX in all cases, so we use this hook as a stop-gap until it does.
- Loading branch information
1 parent
dc52896
commit 8e6b28e
Showing
3 changed files
with
75 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { useContext, useEffect } from 'react'; | ||
import { useDispatch } from 'react-redux'; | ||
import { setRedeemTx } from 'store/redeem'; | ||
import { RouteContext } from 'contexts/RouteContext'; | ||
import { sleep } from 'utils'; | ||
import { | ||
api, | ||
toChain, | ||
TransferState, | ||
UniversalAddress, | ||
} from '@wormhole-foundation/sdk'; | ||
import config from 'config'; | ||
|
||
// TODO: this hook is a stop-gap until the SDK reliably | ||
// sets the redeem tx hash on the receipt | ||
const useFetchRedeemTx = () => { | ||
const dispatch = useDispatch(); | ||
const { receipt } = useContext(RouteContext); | ||
|
||
useEffect(() => { | ||
if ( | ||
!receipt || | ||
receipt.state < TransferState.Attested || | ||
'originTxs' in receipt === false || | ||
receipt.originTxs.length === 0 | ||
) { | ||
return; | ||
} | ||
|
||
let isActive = true; | ||
|
||
const fetchRedeemTx = async () => { | ||
const wormholeApi = config.wormholeApi.replace(/\/$/, ''); | ||
const { txid } = receipt.originTxs[receipt.originTxs.length - 1]; | ||
while (isActive) { | ||
try { | ||
const vaa = await api.getVaaByTxHash(wormholeApi, txid); | ||
if (vaa) { | ||
const status = await api.getTransactionStatus(wormholeApi, { | ||
chain: toChain(vaa.emitterChain), | ||
emitter: new UniversalAddress(vaa.emitterAddr), | ||
sequence: BigInt(vaa.sequence), | ||
}); | ||
const redeemTx = status?.globalTx?.destinationTx?.txHash; | ||
if (redeemTx) { | ||
if (isActive) { | ||
dispatch(setRedeemTx(redeemTx)); | ||
} | ||
break; | ||
} | ||
} | ||
} catch (e) { | ||
console.warn(e); | ||
} | ||
await sleep(10_000); | ||
} | ||
}; | ||
|
||
fetchRedeemTx(); | ||
|
||
return () => { | ||
isActive = false; | ||
}; | ||
}, [receipt]); | ||
}; | ||
|
||
export default useFetchRedeemTx; |
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