Skip to content

Commit 06009f9

Browse files
authored
Merge pull request #278 from threshold-network/simplify-mapped-operator-data-check
Simplify mapped operators data check The main goal is to reduce the repetition of the code for `isOperatorMappedOnlyInRandomBeacon`, `isOperatorMappedOnlyInTbtc` etc. For `useRegisterMultipleOperator` hook I've created the reducer that will get the operator addresses from the state and returns all the needed data.
2 parents 9371160 + 0f73828 commit 06009f9

File tree

9 files changed

+73
-76
lines changed

9 files changed

+73
-76
lines changed

src/components/Modal/MapOperatorToStakingProviderConfirmationModal/index.tsx

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import { useModal } from "../../../hooks/useModal"
2121
import StakeAddressInfo from "../../../pages/Staking/StakeCard/StakeAddressInfo"
2222
import { mapOperatorToStakingProviderModalClosed } from "../../../store/modal"
2323
import { BaseModalProps } from "../../../types"
24-
import { isAddressZero } from "../../../web3/utils"
2524
import InfoBox from "../../InfoBox"
2625
import withBaseModal from "../withBaseModal"
2726

@@ -54,28 +53,20 @@ const OperatorMappingConfirmation: FC<
5453
const MapOperatorToStakingProviderConfirmationModal: FC<
5554
BaseModalProps & {
5655
operator: string
57-
mappedOperatorTbtc: string
58-
mappedOperatorRandomBeacon: string
56+
isOperatorMappedOnlyInTbtc: boolean
57+
isOperatorMappedOnlyInRandomBeacon: boolean
5958
}
6059
> = ({
6160
operator,
62-
mappedOperatorTbtc,
63-
mappedOperatorRandomBeacon,
61+
isOperatorMappedOnlyInTbtc,
62+
isOperatorMappedOnlyInRandomBeacon,
6463
closeModal,
6564
}) => {
6665
const { account } = useWeb3React()
6766
const { registerMultipleOperators } =
6867
useRegisterMultipleOperatorsTransaction()
6968
const dispatch = useAppDispatch()
7069

71-
const isOperatorMappedOnlyInTbtc =
72-
!isAddressZero(mappedOperatorTbtc) &&
73-
isAddressZero(mappedOperatorRandomBeacon)
74-
75-
const isOperatorMappedOnlyInRandomBeacon =
76-
isAddressZero(mappedOperatorTbtc) &&
77-
!isAddressZero(mappedOperatorRandomBeacon)
78-
7970
const { openModal } = useModal()
8071
const onSuccess = useCallback(
8172
(tx: ContractTransaction) => {

src/components/Modal/MapOperatorToStakingProviderModal/MapOperatorToStakingProviderForm.tsx

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,27 @@ const validateInputtedOperatorAddress = async (
3131
checkIfOperatorIsMappedToAnotherStakingProvider: (
3232
operator: string
3333
) => Promise<boolean>,
34-
mappedOperatorRandomBeacon: string,
35-
mappedOperatorTbtc: string
34+
mappedOperatorTbtc: string,
35+
mappedOperatorRandomBeacon: string
3636
): Promise<string | undefined> => {
3737
let validationMsg: string | undefined = ""
3838

39-
const isOperatorMappedOnlyInTbtc =
40-
!isAddressZero(mappedOperatorTbtc) &&
41-
isAddressZero(mappedOperatorRandomBeacon)
42-
43-
const isOperatorMappedOnlyInRandomBeacon =
44-
isAddressZero(mappedOperatorTbtc) &&
45-
!isAddressZero(mappedOperatorRandomBeacon)
46-
4739
try {
4840
const isOperatorMappedToAnotherStakingProvider =
4941
await checkIfOperatorIsMappedToAnotherStakingProvider(operator)
5042
validationMsg = undefined
5143
if (isOperatorMappedToAnotherStakingProvider) {
5244
validationMsg = "Operator is already mapped to another staking provider."
5345
}
46+
47+
const isOperatorMappedOnlyInTbtc =
48+
!isAddressZero(mappedOperatorTbtc) &&
49+
isAddressZero(mappedOperatorRandomBeacon)
50+
51+
const isOperatorMappedOnlyInRandomBeacon =
52+
isAddressZero(mappedOperatorTbtc) &&
53+
!isAddressZero(mappedOperatorRandomBeacon)
54+
5455
if (
5556
isOperatorMappedOnlyInRandomBeacon &&
5657
!isSameETHAddress(operator, mappedOperatorRandomBeacon)
@@ -104,8 +105,8 @@ const MapOperatorToStakingProviderForm = withFormik<
104105
errors.operator = await validateInputtedOperatorAddress(
105106
values.operator,
106107
checkIfOperatorIsMappedToAnotherStakingProvider,
107-
mappedOperatorRandomBeacon,
108-
mappedOperatorTbtc
108+
mappedOperatorTbtc,
109+
mappedOperatorRandomBeacon
109110
)
110111
}
111112

src/components/Modal/MapOperatorToStakingProviderModal/index.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import {
3030
isSameETHAddress,
3131
AddressZero,
3232
} from "../../../web3/utils"
33+
import { selectMappedOperators } from "../../../store/account/selectors"
34+
import { useAppSelector } from "../../../hooks/store"
3335

3436
export interface MapOperatorToStakingProviderModalProps {
3537
mappedOperatorTbtc: string
@@ -38,29 +40,28 @@ export interface MapOperatorToStakingProviderModalProps {
3840

3941
const MapOperatorToStakingProviderModal: FC<
4042
BaseModalProps & MapOperatorToStakingProviderModalProps
41-
> = ({ mappedOperatorTbtc, mappedOperatorRandomBeacon }) => {
43+
> = () => {
4244
const { account } = useWeb3React()
4345
const formRef =
4446
useRef<FormikProps<MapOperatorToStakingProviderFormValues>>(null)
4547
const { closeModal, openModal } = useModal()
4648
const threshold = useThreshold()
4749

48-
const isOperatorMappedOnlyInTbtc =
49-
!isAddressZero(mappedOperatorTbtc) &&
50-
isAddressZero(mappedOperatorRandomBeacon)
51-
52-
const isOperatorMappedOnlyInRandomBeacon =
53-
isAddressZero(mappedOperatorTbtc) &&
54-
!isAddressZero(mappedOperatorRandomBeacon)
50+
const {
51+
mappedOperatorTbtc,
52+
mappedOperatorRandomBeacon,
53+
isOperatorMappedOnlyInRandomBeacon,
54+
isOperatorMappedOnlyInTbtc,
55+
} = useAppSelector(selectMappedOperators)
5556

5657
const onSubmit = async ({
5758
operator,
5859
}: MapOperatorToStakingProviderFormValues) => {
5960
if (account) {
6061
openModal(ModalType.MapOperatorToStakingProviderConfirmation, {
6162
operator,
62-
mappedOperatorTbtc,
63-
mappedOperatorRandomBeacon,
63+
isOperatorMappedOnlyInTbtc,
64+
isOperatorMappedOnlyInRandomBeacon,
6465
})
6566
}
6667
}

src/hooks/staking-applications/useRegisterMultipleOperatorsTransaction.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ import { useWeb3React } from "@web3-react/core"
77
import { OperatorMappedSuccessTx } from "../../components/Modal/MapOperatorToStakingProviderSuccessModal"
88
import { mapOperatorToStakingProviderModalClosed } from "../../store/modal"
99
import { useAppDispatch, useAppSelector } from "../store"
10+
import { selectMappedOperators } from "../../store/account"
1011

1112
export const useRegisterMultipleOperatorsTransaction = () => {
12-
const mappedOperatorTbtc = useAppSelector(
13-
(state) => state.account.operatorMapping.data.tbtc
14-
)
15-
const mappedOperatorRandomBeacon = useAppSelector(
16-
(state) => state.account.operatorMapping.data.randomBeacon
17-
)
13+
const {
14+
mappedOperatorTbtc,
15+
mappedOperatorRandomBeacon,
16+
isOperatorMappedInBothApps,
17+
isOperatorMappedOnlyInRandomBeacon,
18+
isOperatorMappedOnlyInTbtc,
19+
} = useAppSelector((state) => selectMappedOperators(state))
1820
const { account } = useWeb3React()
1921
const { openModal, closeModal } = useModal()
2022
const dispatch = useAppDispatch()
@@ -35,20 +37,9 @@ export const useRegisterMultipleOperatorsTransaction = () => {
3537
throw new Error("Connect to the staking provider account first!")
3638
}
3739

38-
if (
39-
!isAddressZero(mappedOperatorRandomBeacon) &&
40-
!isAddressZero(mappedOperatorTbtc)
41-
)
40+
if (isOperatorMappedInBothApps)
4241
throw new Error("Both apps already have mapped operator!")
4342

44-
const isOperatorMappedOnlyInTbtc =
45-
!isAddressZero(mappedOperatorTbtc) &&
46-
isAddressZero(mappedOperatorRandomBeacon)
47-
48-
const isOperatorMappedOnlyInRandomBeacon =
49-
isAddressZero(mappedOperatorTbtc) &&
50-
!isAddressZero(mappedOperatorRandomBeacon)
51-
5243
if (isOperatorMappedOnlyInRandomBeacon)
5344
throw new Error("Random beacon app already has mapped operator!")
5445

src/pages/Staking/OperatorAddressMappingCard.tsx

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,17 @@ import {
1010
} from "@threshold-network/components"
1111
import { FC } from "react"
1212
import { ModalType } from "../../enums"
13+
import { useAppSelector } from "../../hooks/store"
1314
import { useModal } from "../../hooks/useModal"
14-
import { isAddressZero } from "../../web3/utils"
15+
import { selectMappedOperators } from "../../store/account/selectors"
1516

16-
const OperatorAddressMappingCard: FC<{
17-
mappedOperatorTbtc: string
18-
mappedOperatorRandomBeacon: string
19-
}> = ({ mappedOperatorTbtc, mappedOperatorRandomBeacon }) => {
17+
const OperatorAddressMappingCard: FC = () => {
2018
const { openModal } = useModal()
21-
const isOperatorMappedOnlyInTbtc =
22-
!isAddressZero(mappedOperatorTbtc) &&
23-
isAddressZero(mappedOperatorRandomBeacon)
2419

25-
const isOperatorMappedOnlyInRandomBeacon =
26-
isAddressZero(mappedOperatorTbtc) &&
27-
!isAddressZero(mappedOperatorRandomBeacon)
28-
29-
const isOneOfTheAppsNotMapped =
30-
isOperatorMappedOnlyInRandomBeacon || isOperatorMappedOnlyInTbtc
20+
const { isOneOfTheAppsNotMapped } = useAppSelector(selectMappedOperators)
3121

3222
const onStartMappingClick = () => {
33-
openModal(ModalType.MapOperatorToStakingProvider, {
34-
mappedOperatorTbtc,
35-
mappedOperatorRandomBeacon,
36-
})
23+
openModal(ModalType.MapOperatorToStakingProvider)
3724
}
3825

3926
return (

src/pages/Staking/index.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,7 @@ const StakingPage: PageComponent = (props) => {
8282
isOperatorMappingInitialFetchDone &&
8383
(isAddressZero(mappedOperators.tbtc) ||
8484
isAddressZero(mappedOperators.randomBeacon)) && (
85-
<OperatorAddressMappingCard
86-
mappedOperatorTbtc={mappedOperators.tbtc}
87-
mappedOperatorRandomBeacon={mappedOperators.randomBeacon}
88-
/>
85+
<OperatorAddressMappingCard />
8986
)}
9087
{hasStakes ? (
9188
stakes.map((stake) => (

src/store/account/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from "./slice"
2+
export * from "./selectors"

src/store/account/selectors.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { createSelector } from "@reduxjs/toolkit"
2+
import { RootState } from ".."
3+
import { isAddressZero } from "../../web3/utils"
4+
import { AccountState } from "./slice"
5+
6+
export const selectAccountState = (state: RootState) => state.account
7+
8+
export const selectMappedOperators = createSelector(
9+
[selectAccountState],
10+
(accountState: AccountState) => {
11+
const { randomBeacon, tbtc } = accountState.operatorMapping.data
12+
const isOperatorMappedOnlyInTbtc =
13+
!isAddressZero(tbtc) && isAddressZero(randomBeacon)
14+
const isOperatorMappedOnlyInRandomBeacon =
15+
isAddressZero(tbtc) && !isAddressZero(randomBeacon)
16+
17+
return {
18+
mappedOperatorTbtc: tbtc,
19+
mappedOperatorRandomBeacon: randomBeacon,
20+
isOperatorMappedOnlyInTbtc,
21+
isOperatorMappedOnlyInRandomBeacon,
22+
isOneOfTheAppsNotMapped:
23+
isOperatorMappedOnlyInRandomBeacon || isOperatorMappedOnlyInTbtc,
24+
isOperatorMappedInBothApps:
25+
!isAddressZero(randomBeacon) && !isAddressZero(tbtc),
26+
}
27+
}
28+
)

src/store/account/slice.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
import { StakingAppName } from "../staking-applications"
1313
import { getStakingProviderOperatorInfo } from "./effects"
1414

15-
interface AccountState {
15+
export interface AccountState {
1616
address: string
1717
isStakingProvider: boolean
1818
operatorMapping: FetchingState<Record<StakingAppName, string>>

0 commit comments

Comments
 (0)