-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
899: Consolidated all balance and allowance dispatches in balancesHooks
- Loading branch information
piersss
committed
Apr 18, 2024
1 parent
b56da69
commit c900dc8
Showing
9 changed files
with
82 additions
and
295 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
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 { useEffect, useState } from "react"; | ||
|
||
import { Web3Provider } from "@ethersproject/providers"; | ||
import { useWeb3React } from "@web3-react/core"; | ||
|
||
import { useAppDispatch, useAppSelector } from "../../app/hooks"; | ||
import { TransactionTypes } from "../../types/transactionTypes"; | ||
import { fetchUnkownTokens } from "../metadata/metadataActions"; | ||
import { selectActiveTokens } from "../metadata/metadataSlice"; | ||
import useLatestSucceededTransaction from "../transactions/hooks/useLatestSucceededTransaction"; | ||
import { | ||
requestActiveTokenAllowancesSwap, | ||
requestActiveTokenAllowancesWrapper, | ||
requestActiveTokenBalances, | ||
} from "./balancesSlice"; | ||
|
||
export const useBalances = () => { | ||
const { library, account, chainId } = useWeb3React<Web3Provider>(); | ||
const dispatch = useAppDispatch(); | ||
|
||
const activeTokens = useAppSelector(selectActiveTokens); | ||
const latestSuccessfulTransaction = useLatestSucceededTransaction(); | ||
|
||
const [activeAccount, setActiveAccount] = useState<string>(); | ||
const [activeChainId, setActiveChainId] = useState<number>(); | ||
|
||
useEffect(() => { | ||
if (!library || !activeTokens.length || !account) { | ||
return; | ||
} | ||
|
||
if (activeAccount === account && activeChainId === chainId) { | ||
return; | ||
} | ||
|
||
setActiveAccount(account); | ||
setActiveChainId(chainId); | ||
|
||
dispatch(requestActiveTokenBalances({ provider: library })); | ||
dispatch(requestActiveTokenAllowancesSwap({ provider: library })); | ||
dispatch(requestActiveTokenAllowancesWrapper({ provider: library })); | ||
dispatch(fetchUnkownTokens({ provider: library })); | ||
}, [account, chainId, library, activeTokens]); | ||
|
||
useEffect(() => { | ||
if (!latestSuccessfulTransaction || !library) { | ||
return; | ||
} | ||
|
||
const { type } = latestSuccessfulTransaction; | ||
|
||
if ( | ||
type === TransactionTypes.order || | ||
type === TransactionTypes.withdraw || | ||
type === TransactionTypes.deposit | ||
) { | ||
dispatch(requestActiveTokenBalances({ provider: library })); | ||
} | ||
|
||
if (type === TransactionTypes.approval) { | ||
dispatch(requestActiveTokenAllowancesSwap({ provider: library })); | ||
dispatch(requestActiveTokenAllowancesWrapper({ provider: library })); | ||
} | ||
}, [latestSuccessfulTransaction]); | ||
}; | ||
|
||
export default useBalances; |
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.