-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #324 from pendulum-chain/refactor/useMainProcess
Refactor useMainProcess
- Loading branch information
Showing
14 changed files
with
373 additions
and
287 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { useEffect } from 'preact/hooks'; | ||
import { useConfig } from 'wagmi'; | ||
|
||
import { advanceOfframpingState } from '../../services/offrampingFlow'; | ||
|
||
import { usePolkadotWalletState } from '../../contexts/polkadotWallet'; | ||
import { useAssetHubNode } from '../../contexts/polkadotNode'; | ||
import { usePendulumNode } from '../../contexts/polkadotNode'; | ||
import { useEventsContext } from '../../contexts/events'; | ||
|
||
import { useOfframpActions, useOfframpState } from '../../stores/offrampStore'; | ||
import { EventStatus } from '../../components/GenericEvent'; | ||
|
||
export const useOfframpAdvancement = () => { | ||
const { walletAccount } = usePolkadotWalletState(); | ||
const { trackEvent } = useEventsContext(); | ||
const wagmiConfig = useConfig(); | ||
|
||
const { apiComponents: pendulumNode } = usePendulumNode(); | ||
const { apiComponents: assetHubNode } = useAssetHubNode(); | ||
|
||
const offrampState = useOfframpState(); | ||
const { updateOfframpHookStateFromState, setOfframpSigningPhase } = useOfframpActions(); | ||
|
||
useEffect(() => { | ||
if (wagmiConfig.state.status !== 'connected') return; | ||
|
||
(async () => { | ||
if (!pendulumNode || !assetHubNode) { | ||
console.error('Polkadot nodes not initialized'); | ||
return; | ||
} | ||
|
||
const nextState = await advanceOfframpingState(offrampState, { | ||
wagmiConfig, | ||
setOfframpSigningPhase, | ||
trackEvent, | ||
pendulumNode, | ||
assetHubNode, | ||
walletAccount, | ||
renderEvent: (message: string, status: EventStatus) => { | ||
console.log('renderEvent: ', message, status); | ||
}, | ||
}); | ||
|
||
if (JSON.stringify(offrampState) !== JSON.stringify(nextState)) { | ||
updateOfframpHookStateFromState(nextState); | ||
} | ||
})(); | ||
|
||
// @todo: investigate and remove this | ||
// This effect has dependencies that are used inside the async function (assetHubNode, pendulumNode, walletAccount) | ||
// but we intentionally exclude them from the dependency array to prevent unnecessary re-renders. | ||
// These dependencies are stable and won't change during the lifecycle of this hook. | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [offrampState, trackEvent, updateOfframpHookStateFromState, wagmiConfig]); | ||
}; |
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,35 @@ | ||
import { useCallback } from 'preact/compat'; | ||
import { createTransactionEvent } from '../../contexts/events'; | ||
import { useEventsContext } from '../../contexts/events'; | ||
import { useNetwork } from '../../contexts/network'; | ||
|
||
import { getInputTokenDetailsOrDefault } from '../../constants/tokenConfig'; | ||
import { OfframpingState } from '../../services/offrampingFlow'; | ||
import { OFFRAMPING_PHASE_SECONDS } from '../../pages/progress'; | ||
|
||
export const useOfframpEvents = () => { | ||
const { trackEvent, resetUniqueEvents } = useEventsContext(); | ||
const { selectedNetwork } = useNetwork(); | ||
|
||
const trackOfframpingEvent = useCallback( | ||
(state: OfframpingState | undefined) => { | ||
if (!state) return; | ||
|
||
if (state.phase === 'success') { | ||
trackEvent(createTransactionEvent('transaction_success', state, selectedNetwork)); | ||
} else if (state.failure) { | ||
trackEvent({ | ||
...createTransactionEvent('transaction_failure', state, selectedNetwork), | ||
event: 'transaction_failure', | ||
phase_name: state.phase, | ||
phase_index: Object.keys(OFFRAMPING_PHASE_SECONDS).indexOf(state.phase), | ||
from_asset: getInputTokenDetailsOrDefault(selectedNetwork, state.inputTokenType).assetSymbol, | ||
error_message: state.failure.message, | ||
}); | ||
} | ||
}, | ||
[trackEvent, selectedNetwork], | ||
); | ||
|
||
return { trackOfframpingEvent, trackEvent, resetUniqueEvents }; | ||
}; |
Oops, something went wrong.