Skip to content

Commit

Permalink
fix: Network switching loader (#670)
Browse files Browse the repository at this point in the history
  • Loading branch information
LautaroPetaccio authored Feb 6, 2025
1 parent 40f0c02 commit def71a2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
10 changes: 9 additions & 1 deletion src/modules/wallet/sagas.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ describe('Wallet sagas', () => {
})
describe('when getting the connected provider succeeds', () => {
describe('when wallet_switchEthereumChain succeeds', () => {
it('should dispatch an action to signal that the request succeded', () => {
it('should dispatch an action to signal that the request succeeded', () => {
return expectSaga(walletSaga)
.provide([
[
Expand All @@ -136,6 +136,14 @@ describe('Wallet sagas', () => {
timeout: delay(SWITCH_NETWORK_TIMEOUT)
}),
{ switched: true }
],
[put(fetchWalletRequest()), undefined],
[
race({
success: take(FETCH_WALLET_SUCCESS),
failure: take(FETCH_WALLET_FAILURE)
}),
{ success: true }
]
])
.put(switchNetworkSuccess(ChainId.ETHEREUM_MAINNET))
Expand Down
14 changes: 6 additions & 8 deletions src/modules/wallet/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ import {
SwitchNetworkRequestAction,
switchNetworkSuccess,
switchNetworkFailure,
SWITCH_NETWORK_SUCCESS,
SwitchNetworkSuccessAction,
setAppChainId,
disconnectWalletSuccess,
disconnectWalletFailure
Expand Down Expand Up @@ -108,8 +106,7 @@ export function* walletSaga() {
takeEvery(FETCH_WALLET_REQUEST, handleFetchWalletRequest),
takeEvery(DISCONNECT_WALLET_REQUEST, handleDisconnectWalletRequest),
takeEvery(CONNECT_WALLET_SUCCESS, handleConnectWalletSuccess),
takeEvery(SWITCH_NETWORK_REQUEST, handleSwitchNetworkRequest),
takeEvery(SWITCH_NETWORK_SUCCESS, handleSwitchNetworkSuccess)
takeEvery(SWITCH_NETWORK_REQUEST, handleSwitchNetworkRequest)
])
}

Expand Down Expand Up @@ -222,6 +219,11 @@ function* handleSwitchNetworkRequest(action: SwitchNetworkRequestAction) {
yield put(showToast(getSwitchChainErrorToast(chainId)))
throw new Error('Error switching network: Operation timed out')
} else {
yield put(fetchWalletRequest())
yield race({
success: take(FETCH_WALLET_SUCCESS),
failure: take(FETCH_WALLET_FAILURE)
})
yield put(switchNetworkSuccess(chainId))
}
} catch (switchError) {
Expand All @@ -230,10 +232,6 @@ function* handleSwitchNetworkRequest(action: SwitchNetworkRequestAction) {
}
}

function* handleSwitchNetworkSuccess(_action: SwitchNetworkSuccessAction) {
yield put(fetchWalletRequest())
}

export function createWalletSaga(options: CreateWalletOptions) {
if (isValidChainId(options.CHAIN_ID)) {
_setAppChainId(Number(options.CHAIN_ID))
Expand Down

0 comments on commit def71a2

Please sign in to comment.