Skip to content

Commit

Permalink
Add extra card for Operator mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
theref committed Dec 6, 2023
1 parent 4ccdefd commit ac202f7
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ const validateInputtedOperatorAddress = async (
operator: string
) => Promise<boolean>,
mappedOperatorTbtc: string,
mappedOperatorRandomBeacon: string,
mappedOperatorTaco: string
mappedOperatorRandomBeacon: string
): Promise<string | undefined> => {
let validationMsg: string | undefined = ""

Expand All @@ -47,18 +46,11 @@ const validateInputtedOperatorAddress = async (

const isOperatorMappedOnlyInTbtc =
!isAddressZero(mappedOperatorTbtc) &&
isAddressZero(mappedOperatorRandomBeacon) &&
isAddressZero(mappedOperatorTaco)
isAddressZero(mappedOperatorRandomBeacon)

const isOperatorMappedOnlyInRandomBeacon =
isAddressZero(mappedOperatorTbtc) &&
!isAddressZero(mappedOperatorRandomBeacon) &&
isAddressZero(mappedOperatorTaco)

const isOperatorMappedOnlyInTaco =
isAddressZero(mappedOperatorTbtc) &&
isAddressZero(mappedOperatorRandomBeacon) &&
!isAddressZero(mappedOperatorTaco)
!isAddressZero(mappedOperatorRandomBeacon)

if (
isOperatorMappedOnlyInRandomBeacon &&
Expand All @@ -74,13 +66,6 @@ const validateInputtedOperatorAddress = async (
validationMsg =
"The operator address doesn't match the one used in tbtc app"
}
if (
isOperatorMappedOnlyInTaco &&
!isSameETHAddress(operator, mappedOperatorTaco)
) {
validationMsg =
"The operator address doesn't match the one used in TACo app"
}
} catch (error) {
console.error("`MapOperatorToStakingProviderForm` validation error.", error)
validationMsg = (error as Error)?.message
Expand All @@ -91,9 +76,9 @@ const validateInputtedOperatorAddress = async (

type MapOperatorToStakingProviderFormProps = {
initialAddress: string
mappedOperatorTbtc: string
mappedOperatorRandomBeacon: string
mappedOperatorTaco: string
mappedOperatorTbtc?: string
mappedOperatorRandomBeacon?: string
mappedOperatorTaco?: string
innerRef: Ref<FormikProps<MapOperatorToStakingProviderFormValues>>
checkIfOperatorIsMappedToAnotherStakingProvider: (
operator: string
Expand All @@ -117,16 +102,42 @@ const MapOperatorToStakingProviderForm = withFormik<
} = props
const errors: FormikErrors<MapOperatorToStakingProviderFormValues> = {}

console.log("tbtc", mappedOperatorTbtc)
console.log("random beacon", mappedOperatorRandomBeacon)
console.log("taco", mappedOperatorTaco)

errors.operator = validateETHAddress(values.operator)
if (!errors.operator) {
if (
!errors.operator &&
mappedOperatorTbtc !== undefined &&
mappedOperatorRandomBeacon !== undefined
) {
errors.operator = await validateInputtedOperatorAddress(
values.operator,
checkIfOperatorIsMappedToAnotherStakingProvider,
mappedOperatorTbtc,
mappedOperatorRandomBeacon,
mappedOperatorTaco
mappedOperatorRandomBeacon
)
}
if (!errors.operator && mappedOperatorTaco !== undefined) {
let validationMsg: string | undefined = ""
try {
const isOperatorMappedToAnotherStakingProvider =
await checkIfOperatorIsMappedToAnotherStakingProvider(values.operator)
validationMsg = undefined
if (isOperatorMappedToAnotherStakingProvider) {
validationMsg =
"Operator is already mapped to another staking provider."
}
} catch (error) {
console.error(
"`MapOperatorToStakingProviderForm` validation error.",
error
)
validationMsg = (error as Error)?.message
}
errors.operator = validationMsg
}

return getErrorsObj(errors)
},
Expand Down
46 changes: 38 additions & 8 deletions src/components/Modal/MapOperatorToStakingProviderModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ const MapOperatorToStakingProviderModal: FC<
BaseModalProps & MapOperatorToStakingProviderModalProps
> = () => {
const { account } = useWeb3React()
const formRef =
const formRefTbtc =
useRef<FormikProps<MapOperatorToStakingProviderFormValues>>(null)
const formRefTaco =
useRef<FormikProps<MapOperatorToStakingProviderFormValues>>(null)
const { closeModal, openModal } = useModal()
const threshold = useThreshold()
Expand Down Expand Up @@ -88,6 +90,16 @@ const MapOperatorToStakingProviderModal: FC<
)
}

const handleSubmit = async () => {
if (formRefTbtc.current) {
await formRefTbtc.current.handleSubmit()
}

if (formRefTaco.current) {
await formRefTaco.current.handleSubmit()
}
}

return (
<>
<ModalHeader>Operator Address Mapping</ModalHeader>
Expand Down Expand Up @@ -128,19 +140,17 @@ const MapOperatorToStakingProviderModal: FC<
) : isOperatorMappedOnlyInTbtc ? (
<LabelSm>random beacon app</LabelSm>
) : (
<LabelSm>tBTC + Random Beacon + TACo (requires 3txs)</LabelSm>
<LabelSm>tBTC + Random Beacon (requires 2txs)</LabelSm>
)}
<StakeAddressInfo stakingProvider={account ? account : AddressZero} />
<MapOperatorToStakingProviderForm
innerRef={formRef}
formId="map-operator-to-staking-provider-form"
innerRef={formRefTbtc}
formId="map-operator-to-staking-provider-form-tbtc"
initialAddress={
isOperatorMappedOnlyInRandomBeacon
? mappedOperatorRandomBeacon
: isOperatorMappedOnlyInTbtc
? mappedOperatorTbtc
: isOperatorMappedOnlyInTaco
? mappedOperatorTaco
: ""
}
onSubmitForm={onSubmit}
Expand All @@ -149,7 +159,6 @@ const MapOperatorToStakingProviderModal: FC<
}
mappedOperatorTbtc={mappedOperatorTbtc}
mappedOperatorRandomBeacon={mappedOperatorRandomBeacon}
mappedOperatorTaco={mappedOperatorTaco}
/>
</Box>
<AlertBox
Expand All @@ -164,12 +173,33 @@ const MapOperatorToStakingProviderModal: FC<
transactions, one transaction per application.
</BodyXs>
</AlertBox>
<Box
p={"24px"}
border={"1px solid"}
borderColor={"gray.100"}
borderRadius={"12px"}
mt={"5"}
mb={"5"}
>
<LabelSm>Taco (requires 1tx)</LabelSm>
<StakeAddressInfo stakingProvider={account ? account : AddressZero} />
<MapOperatorToStakingProviderForm
innerRef={formRefTaco}
formId="map-operator-to-staking-provider-form-taco"
initialAddress={""}
onSubmitForm={onSubmit}
checkIfOperatorIsMappedToAnotherStakingProvider={
checkIfOperatorIsMappedToAnotherStakingProvider
}
mappedOperatorTaco={mappedOperatorTaco}
/>
</Box>
</ModalBody>
<ModalFooter>
<Button onClick={closeModal} variant="outline" mr={2}>
Dismiss
</Button>
<Button type="submit" form="map-operator-to-staking-provider-form">
<Button type="submit" onClick={handleSubmit}>
Map Address
</Button>
</ModalFooter>
Expand Down

0 comments on commit ac202f7

Please sign in to comment.