Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Operator mapping - code cleaning #223

Merged
merged 43 commits into from
Oct 24, 2022
Merged

Conversation

michalsmiarowski
Copy link
Contributor

@michalsmiarowski michalsmiarowski commented Oct 3, 2022

Closes #232 (64677b3)
Closes #255 (ae4231d)

Operator mapping code cleaning

Main changes:

  • rearrange the operator mapping related data in redux store
  • adds operator and staking provider addresses as props to the Operator Mapping related modals and OperatorAddressMappingCard
  • removes unnecessary shouldDisplayMapOperatorToStakingProviderModal effect
  • fix some of the review comments from Map operator to staking provider feature #199
  • fix a bug that caused the MapOperatorToStakingProvider modal to not be displayed when we changed accounts in MetaMask
  • update text in operator mapping modal (Closes: Operator Mapping Modal Copy needs an update #232)

TODO in the future:

Change details

rearrange the operator mapping related data in redux store

Previously we keep the data needed to operator mapping in the redux store in this format:

(1) rolesOf property in connected-account slice:

{
address: string,
  rolesOf: {
    owner: string,
    authorizer: string,
    beneficiary: string, 
}

This indicated if the currently logged account is used as a staking provider in other stakes. We fetched the data from the contract.

(2) mappedOperator property in staking-applications slice:

{
  tbtc: {
    parameter: (...),
    stakingProviders: (...),
    mappedOperator: {
      isInitialFetchDone: boolean,
      isFetching: boolean,
      error: string,
      data: string,
  },
  randomBeacon: {
    parameter: (...),
    stakingProviders: (...),
    mappedOperator: {
      isInitialFetchDone: boolean,
      isFetching: boolean,
      error: string,
      data: string,
  }
}

This stored the data related to currently logged staking provider and it's mapped operator for each application. This could wrongly indicate that the data is more related to the staking app itself (since it was in the staking-application slice) when in reality it was more related to currently connected account.

At first the idea was to not store this data in redux store at all, but unfrotunately this didn't went as smooth as I thogught and I think we must store them mainly to properly display OperatorAddressMappingCard in the staking page for all staking providers that don't have the operator mapped. I've encoutered problems with implementing it without using the store when the staking provider !=== owner.

This is why i decided to go back to keeping the data in the redux store (you can see all this in the commit history) but in better (and i think simpler) form.

Right now we will store this data in account slice and we ditched the whole rolesOf property which was object, to just isStakingProvider which is boolean value. After the changes the connected-account is renamed to account and looks like this:

{
  address: string,
  isStakingProvider: boolean,
  operatorMapping: {
    data: {
      tbtc: string,
      randomBeacon: string,
    },
    isFetching: boolean,
    isInitialFetchDone: boolean,
  },
}

adds operator and staking provider addresses as props to the Operator Mapping related modals and OperatorAddressMappingCard

For these components we are adding a props that contains operator address and staking provider address:

  • OperatorAddressMappingCard
  • MapOperatorToStakingProviderModal
  • MapOperatorToStakingProviderSuccessModal
  • MapOperatorToStakingProviderConfirmationModal
  • MapOperatorToStakingProviderForm

removes unnecessary shouldDisplayMapOperatorToStakingProviderModal effect

This effect was unnecessary. Instead of using predicate (from redux-toolkit) when calling displayMapOperatorToStakingProviderModalEffect we now use actionCreator (also from redux-toolkit) to call this effect on operatorMappingInitialFetchDone action call which in turn is called by setStakes action. This way we are sure we have all needed that in redux store and we can use them in displayMapOperatorToStakingProviderModalEffect

fix some of the review comments from #199

Most comment were either fixed by itself during the refactor process (the part of code that was commented does no longer exist) and other ones I've tried to fix. For some I've added comments there to start a discussion.

fix a bug that caused the MapOperatorToStakingProvider modal to not be displayed when we changed accounts in MetaMask

We are setting the isSuccessfullLoginModalClosed flag to false when resetting the store now, because the reset happend when we change the accounts and the login modal is not displayed in such case. We also set isMappingOperatorToStakingProviderModalClosed flag back to false in this case.

Additional note: The modal queue was also moved in the store to modal prop since it's related to modal. In the furue we are planning to create a real modal queue.

update text in operator mapping modal

Update text based on Figma designs in OperatorAddressMappingCard component and in MapOperatorToStakingProviderModal. For more details see #232

We decided that we don't have to store the data related to operator mapping in
the redux store. This commit adds that.

I've also commented out `OperatorAddressMappingCard`. This card will be
addressed in the future commits when working on subcribtion to
"OperatorRegistered" event
There was a bug where when you close the login modal too quick, before
`setStakes` action was dispatched, then the effect got blocked on `await
listenerApi.condition` and was waiting for another action to dispatch and there
were not any at that particular moment.

To fix that I've added a condition to check if login modal was closed, if not,
then we wait with the `condition()` function since the
`displayMapOperatorToStakingProviderModalEffect` effect is run on a `setStakes`
action, so we have all needed data fetcher already.
Fix displaying operator mapping modal when changins accouns. The display of this
modal is based on the condition that the user closed succesfull login modal. We
store this flag in `modalQueue` redux store.

When changing accounts this modal is not displayed thus the flag is not set. To
fix that we wil set this flag to true when resetting redux store action is
called.
Adds data about operator mapping to the connectedAccount slice:
{
	isUsedAsStakingProvider: boolean
	mappedOperator: {
		tbtc: string
		randomBeacon: string
	}
}

This data is neede to properly display the OperatorMappingCard that is on the
Staking page.
Gets the proper data from redux store and conditionally display
OperatorAddressMappingCard in staking page
Move operatorMapped event to connectedAddress slice and rename it to
operatorRegistered to indicate it's relation with `OperatorRegistered` event.
`stakingProvider` prop was not used in `MapOperatorToStakingProviderModal` so we
are removing it.
Use mappedOperators data from the store instead of calling the
`getMappedOperatorsForStakingProvider` method from threshold lib in
`useRegisterMultipleOperatorsTransaction` hook.
@github-actions
Copy link

github-actions bot commented Oct 3, 2022

Operator Address Mapping Card should be displayed when tbtc mapped operator is
AddressZero OR (not AND) random beacon mapped operator is AddressZero. This
commit fixes that.
Move the store related to modal queue inside `modal` property since it's related
to modal.
@r-czajkowski r-czajkowski added this to the v1.3.1 milestone Oct 5, 2022
- move provider address to the top
- change `Staking Provider` -> `Provider Address`
- change `Operator` -> `Operator Address`
- Display `View transaction 1 and transaction 2` in one row instead of two
separate rows foe each app.
The operator mapping modal and new apps to authorize modal were not displayed
properly when changin accounts in MetaMask. This commit fixes that.
Property `isFetching` was wrongly set to true and stays at this value when the
connected account was not a staking provider. This commit fixes that.
@github-actions
Copy link

github-actions bot commented Oct 5, 2022


const operatorMappedToStakingProviderHelpers =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why remove this helper hook? seems like it saved some duplicate code. What if we added the tbtc & random beacon addresses as parameters so we don't have to rewrite the logic?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be addressed in #278

@@ -69,14 +73,14 @@ const MapOperatorToStakingProviderForm = withFormik<
}
if (
isOperatorMappedOnlyInRandomBeacon &&
values.operator !== operatorMappedRandomBeacon
values.operator !== mappedOperatorRandomBeacon
) {
validationMsg =
"The operator address doesn't match the one used in tbtc app"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's use tBTC

Copy link
Contributor Author

@michalsmiarowski michalsmiarowski Oct 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Fixed in a1810ef

- use `isSameEthAddress` function in some conditions instead of `!==`
- move large part of the validation to a new `validateInputtedOperatorAddress`
function
Use `isAddressZero` instead of `isZeroAddress`
setAccountAddress -> walletConnected
@github-actions
Copy link

isUsedAsStakingProvider -> isStakingProvider

Also move it outside of mappedOperators object and keep it in the root of the
account slice.
We don't need mapped operator in `account` slice because we already have
`operatorMapping` property.
@github-actions
Copy link

Rename the `setFetchingOperatorMapping` action to `fetchingOperatorMapping` and
set the fetching to true automatically.
@github-actions
Copy link

1 similar comment
@github-actions
Copy link

Copy link
Collaborator

@r-czajkowski r-czajkowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! :shipit:

@pdyraga pdyraga dismissed georgeweiler’s stale review October 24, 2022 09:13

@georgeweiler is on vacation, all his requested changes were addressed and we have an approval from @r-czajkowski

@r-czajkowski r-czajkowski merged commit 2932ad4 into main Oct 24, 2022
@r-czajkowski r-czajkowski deleted the operator-mapping-code-cleaning branch October 24, 2022 09:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Etherscan link incorrect for operator registration tx Operator Mapping Modal Copy needs an update
3 participants