Skip to content

MAS UI <-> data access layer integration #178

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

Merged
merged 31 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
4a1a30f
Move fn to common file
r-czajkowski Sep 5, 2022
71b182a
Add FetchingState type
r-czajkowski Sep 5, 2022
938440a
Add listener middleware
r-czajkowski Sep 5, 2022
b9c7e62
Make applications types generic
r-czajkowski Sep 5, 2022
711eed8
Make applications method as arrow fn
r-czajkowski Sep 5, 2022
d4936b2
Add application slices and effects
r-czajkowski Sep 5, 2022
a3ab33a
Dispatch an action to fetch the supporrted apps
r-czajkowski Sep 5, 2022
e779671
Define typed hooks for store
r-czajkowski Sep 6, 2022
7c816ee
Create selectors for applications state
r-czajkowski Sep 6, 2022
748894c
Connect store data with App cards
r-czajkowski Sep 6, 2022
a6da181
Remove unnecessary console log
r-czajkowski Sep 6, 2022
051023e
Update `getContract` function
r-czajkowski Sep 6, 2022
e66dd9d
Fix failing unit test in threhold-ts lib
r-czajkowski Sep 6, 2022
8bd4f10
Udpdate selector
r-czajkowski Sep 7, 2022
33b13e3
Display auth data in stake card from redux store
r-czajkowski Sep 7, 2022
7562242
Update threshold ts lib
r-czajkowski Sep 7, 2022
206b6dc
Update `AuthorizeApplicationsCardCheckbox` props
r-czajkowski Sep 7, 2022
6d0c5d1
Remove unnecessary import
r-czajkowski Sep 7, 2022
c144a63
Update validation for app auth form
r-czajkowski Sep 7, 2022
28eee62
Update applications slice
r-czajkowski Sep 7, 2022
a916901
Subscribe to the `AuthorizationIncreased` event
r-czajkowski Sep 7, 2022
d7e5a84
Test `increaseAuthorization` transaction
r-czajkowski Sep 7, 2022
367be12
Add `staking` prefix to the application term
r-czajkowski Sep 8, 2022
5b96c75
Keep the rule one hook in one file
r-czajkowski Sep 8, 2022
ec89e73
Remove unnecessary file
r-czajkowski Sep 8, 2022
dff12d7
Update `FetchingState` type
r-czajkowski Sep 8, 2022
22c103d
Format percentage in `AuthorizeApplicationRow`
r-czajkowski Sep 8, 2022
15ccbbf
Merge branch 'main' into mas-integration
r-czajkowski Sep 12, 2022
5ac843f
Update threshold lib provider
r-czajkowski Sep 15, 2022
5b2af30
Merge branch 'main' into mas-integration
r-czajkowski Sep 15, 2022
1141bb7
Fix erors after merge
r-czajkowski Sep 15, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { useCheckBonusEligibility } from "./hooks/useCheckBonusEligibility"
import { useFetchStakingRewards } from "./hooks/useFetchStakingRewards"
import { isSameETHAddress } from "./web3/utils"
import { ThresholdProvider } from "./contexts/ThresholdContext"
import { useSubscribeToAuthorizationIncreasedEvent } from "./hooks/staking-applications"

const Web3EventHandlerComponent = () => {
useSubscribeToVendingMachineContractEvents()
Expand All @@ -48,6 +49,8 @@ const Web3EventHandlerComponent = () => {
useSubscribeToStakedEvent()
useSubscribeToUnstakedEvent()
useSubscribeToToppedUpEvent()
useSubscribeToAuthorizationIncreasedEvent("tbtc")
Copy link
Contributor

Choose a reason for hiding this comment

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

do you think we should use enums for these? And in other places in the d'app?

Copy link
Contributor

Choose a reason for hiding this comment

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

nevermind I see the type StakingAppName

useSubscribeToAuthorizationIncreasedEvent("randomBeacon")

return <></>
}
Expand Down
53 changes: 31 additions & 22 deletions src/contexts/ThresholdContext.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,43 @@
import { createContext, FC, useContext, useMemo } from "react"
import { createContext, FC, useContext, useEffect, useRef } from "react"
import { useWeb3React } from "@web3-react/core"
import { JsonRpcProvider, Provider } from "@ethersproject/providers"
import { Signer } from "ethers"
import { Threshold } from "../threshold-ts"
import { EnvVariable } from "../enums"
import { getEnvVariable, supportedChainId } from "../utils/getEnvVariable"
import {
getDefaultThresholdLibProvider,
threshold,
} from "../utils/getThresholdLib"
import { supportedChainId } from "../utils/getEnvVariable"

const getThresholdLib = (providerOrSigner?: Provider | Signer) => {
return new Threshold({
ethereum: {
chainId: supportedChainId,
providerOrSigner:
providerOrSigner ||
new JsonRpcProvider(getEnvVariable(EnvVariable.ETH_HOSTNAME_HTTP)),
},
})
}

const ThresholdContext = createContext(getThresholdLib())
const ThresholdContext = createContext(threshold)

export const useThreshold = () => {
return useContext(ThresholdContext)
}

export const ThresholdProvider: FC = ({ children }) => {
const { library, active } = useWeb3React()
const { library, active, account } = useWeb3React()
const hasThresholdLibConfigBeenUpdated = useRef(false)

useEffect(() => {
if (active && library && account) {
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need a cleanup function here if the user disconnects?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

threshold.updateConfig({
ethereum: {
chainId: supportedChainId,
providerOrSigner: library,
account,
},
})
hasThresholdLibConfigBeenUpdated.current = true
}

const threshold = useMemo(() => {
return getThresholdLib(active ? library : undefined)
}, [library, active])
if (!active && !account && hasThresholdLibConfigBeenUpdated.current) {
threshold.updateConfig({
ethereum: {
chainId: supportedChainId,
providerOrSigner: getDefaultThresholdLibProvider(),
},
})
hasThresholdLibConfigBeenUpdated.current = false
}
}, [library, active, account])

return (
<ThresholdContext.Provider value={threshold}>
Expand Down
6 changes: 6 additions & 0 deletions src/hooks/staking-applications/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * from "./useStakingAppContract"
export * from "./useStakingAppDataByStakingProvider"
export * from "./useStakingApplicationState"
export * from "./useStakingAppMinAuthorizationAmount"
export * from "./useStakingAppParameters"
export * from "./useSubscribeToAuthorizationIncreasedEvent"
11 changes: 11 additions & 0 deletions src/hooks/staking-applications/useStakingAppContract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { StakingAppName } from "../../store/staking-applications"
import { useThreshold } from "../../contexts/ThresholdContext"

const appNameToAppService: Record<StakingAppName, "ecdsa" | "randomBeacon"> = {
tbtc: "ecdsa",
randomBeacon: "randomBeacon",
}

export const useStakingAppContract = (appName: StakingAppName) => {
return useThreshold().multiAppStaking[appNameToAppService[appName]].contract
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {
selectStakingAppByStakingProvider,
StakingAppName,
} from "../../store/staking-applications"
import { useAppSelector } from "../store"

export const useStakingAppDataByStakingProvider = (
appName: StakingAppName,
stakingProvider: string
) => {
Copy link
Contributor

@georgeweiler georgeweiler Sep 14, 2022

Choose a reason for hiding this comment

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

Do we have an interface defined somewhere that we could use for the return value of this func? and the other similar selector funcs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you think we need this? When you use this hook or selector with useAppSelector you will receive types automatically.

return useAppSelector((state) =>
selectStakingAppByStakingProvider(state, appName, stakingProvider)
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { StakingAppName } from "../../store/staking-applications"
import { useStakingAppParameters } from "./useStakingAppParameters"

export const useStakingAppMinAuthorizationAmount = (
appName: StakingAppName
) => {
return useStakingAppParameters(appName).data.minimumAuthorization
}
6 changes: 6 additions & 0 deletions src/hooks/staking-applications/useStakingAppParameters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { StakingAppName } from "../../store/staking-applications"
import { useStakingApplicationState } from "./useStakingApplicationState"

export const useStakingAppParameters = (appName: StakingAppName) => {
return useStakingApplicationState(appName).parameters
}
11 changes: 11 additions & 0 deletions src/hooks/staking-applications/useStakingApplicationState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {
StakingAppName,
selectStakingAppStateByAppName,
} from "../../store/staking-applications"
import { useAppSelector } from "../store"

export const useStakingApplicationState = (appName: StakingAppName) => {
return useAppSelector((state) =>
selectStakingAppStateByAppName(state, appName)
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {
stakingApplicationsSlice,
StakingAppName,
} from "../../store/staking-applications"
import { useSubscribeToContractEvent } from "../../web3/hooks"
import { useAppDispatch } from "../store"
import { useStakingAppContract } from "./useStakingAppContract"

export const useSubscribeToAuthorizationIncreasedEvent = (
appName: StakingAppName
) => {
const contract = useStakingAppContract(appName)
const dispatch = useAppDispatch()

useSubscribeToContractEvent(
contract,
"AuthorizationIncreased",
// @ts-ignore
async (stakingProvider, operator, fromAmount, toAmount) => {
dispatch(
stakingApplicationsSlice.actions.authorizationIncreased({
stakingProvider,
toAmount: toAmount.toString(),
appName,
})
)
}
)
}
2 changes: 2 additions & 0 deletions src/hooks/store/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./useAppDispatch"
export * from "./useAppSelector"
4 changes: 4 additions & 0 deletions src/hooks/store/useAppDispatch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { useDispatch } from "react-redux"
import { AppDispatch } from "../../store"

export const useAppDispatch: () => AppDispatch = useDispatch
4 changes: 4 additions & 0 deletions src/hooks/store/useAppSelector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { useSelector, TypedUseSelectorHook } from "react-redux"
import { RootState } from "../../store"

export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { FC } from "react"
import { TokenAmountForm } from "../../../../components/Forms"
import { AppAuthorizationInfo } from "./AppAuthorizationInfo"
import { formatTokenAmount } from "../../../../utils/formatAmount"
import { useThreshold } from "../../../../contexts/ThresholdContext"
import { WeiPerEther } from "@ethersproject/constants"

export interface AppAuthDataProps {
label: string
Expand All @@ -20,6 +22,7 @@ export interface AppAuthDataProps {

export interface AuthorizeApplicationsCardCheckboxProps extends BoxProps {
appAuthData: AppAuthDataProps
stakingProvider: string
onCheckboxClick: (app: AppAuthDataProps, isChecked: boolean) => void
isSelected: boolean
maxAuthAmount: string
Expand All @@ -34,9 +37,21 @@ export const AuthorizeApplicationsCardCheckbox: FC<
isSelected,
maxAuthAmount,
minAuthAmount,
stakingProvider,
...restProps
}) => {
const collapsed = !appAuthData.isAuthRequired
const threshold = useThreshold()

const onAuthorizeApp = async (tokenAmount: string) => {
// TODO: Pass the staking provider address as a prop.
// TODO: Use `useSendtTransacion` hook to open confirmation modal/pending modals/success modal.
// Just test the transacion. The real flow is diffrent- we should opean confirmation modal then trigger transacion.
await threshold.multiAppStaking.randomBeacon.increaseAuthorization(
stakingProvider,
tokenAmount
)
}

if (collapsed) {
return (
Expand Down Expand Up @@ -93,7 +108,7 @@ export const AuthorizeApplicationsCardCheckbox: FC<
<AppAuthorizationInfo
gridArea="app-info"
label={appAuthData.label}
percentageAuthorized={100}
percentageAuthorized={appAuthData.percentage}
aprPercentage={10}
slashingPercentage={1}
isAuthorizationRequired={true}
Expand All @@ -111,14 +126,16 @@ export const AuthorizeApplicationsCardCheckbox: FC<
/>
<GridItem gridArea="token-amount-form" mt={5}>
<TokenAmountForm
onSubmitForm={() => {
console.log("form submitted")
}}
onSubmitForm={onAuthorizeApp}
label="Amount"
submitButtonText={`Authorize ${appAuthData.label}`}
maxTokenAmount={maxAuthAmount}
placeholder={"Enter amount"}
minTokenAmount={minAuthAmount}
minTokenAmount={
appAuthData.percentage === 0
? minAuthAmount
: WeiPerEther.toString()
}
helperText={`Minimum ${formatTokenAmount(minAuthAmount)} T for ${
appAuthData.label
}`}
Expand Down
39 changes: 27 additions & 12 deletions src/pages/Staking/AuthorizeStakingApps/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { useSelector } from "react-redux"
import { useNavigate, useParams } from "react-router-dom"
import { RootState } from "../../../store"
import { PageComponent, StakeData } from "../../../types"
import { isSameETHAddress } from "../../../web3/utils"
import { AddressZero, isSameETHAddress, isAddress } from "../../../web3/utils"
import { StakeCardHeaderTitle } from "../StakeCard/Header/HeaderTitle"
import AuthorizeApplicationsCardCheckbox, {
AppAuthDataProps,
Expand All @@ -23,7 +23,10 @@ import { useEffect, useState } from "react"
import { featureFlags } from "../../../constants"
import { selectStakeByStakingProvider } from "../../../store/staking"
import { useWeb3React } from "@web3-react/core"
import { isAddress } from "web3-utils"
import {
useStakingAppDataByStakingProvider,
useStakingAppMinAuthorizationAmount,
} from "../../../hooks/staking-applications"

const AuthorizeStakingAppsPage: PageComponent = (props) => {
const { stakingProviderAddress } = useParams()
Expand All @@ -34,8 +37,18 @@ const AuthorizeStakingAppsPage: PageComponent = (props) => {
if (!isAddress(stakingProviderAddress!)) navigate(`/staking`)
}, [stakingProviderAddress, navigate])

//TODO: This should be fetched from contract for each app
const minAuthAmount = "1000000000000000000000"
const tbtcMinAuthAmount = useStakingAppMinAuthorizationAmount("tbtc")
const randomBeaconMinAuthAmount =
useStakingAppMinAuthorizationAmount("randomBeacon")

const tbtcApp = useStakingAppDataByStakingProvider(
"tbtc",
stakingProviderAddress || AddressZero
)
const randomBeaconApp = useStakingAppDataByStakingProvider(
"randomBeacon",
stakingProviderAddress || AddressZero
)

const stake = useSelector((state: RootState) =>
selectStakeByStakingProvider(state, stakingProviderAddress!)
Expand All @@ -48,18 +61,17 @@ const AuthorizeStakingAppsPage: PageComponent = (props) => {
? BigNumber.from(stake?.totalInTStake).isZero()
: false

// TODO: This will probably be fetched from contracts
const appsAuthData = {
tbtc: {
label: "tBTC",
isAuthorized: true,
percentage: 40,
isAuthorized: tbtcApp.isAuthorized,
percentage: tbtcApp.percentage,
isAuthRequired: true,
},
randomBeacon: {
label: "Random Beacon",
isAuthorized: false,
percentage: 0,
isAuthorized: randomBeaconApp.isAuthorized,
percentage: randomBeaconApp.percentage,
isAuthRequired: true,
},
pre: {
Expand Down Expand Up @@ -124,7 +136,8 @@ const AuthorizeStakingAppsPage: PageComponent = (props) => {
onCheckboxClick={onCheckboxClick}
isSelected={selectedApps.map((app) => app.label).includes("tBTC")}
maxAuthAmount={stake.totalInTStake}
minAuthAmount={minAuthAmount}
minAuthAmount={tbtcMinAuthAmount}
stakingProvider={stakingProviderAddress!}
/>
<AuthorizeApplicationsCardCheckbox
mt={5}
Expand All @@ -134,15 +147,17 @@ const AuthorizeStakingAppsPage: PageComponent = (props) => {
.map((app) => app.label)
.includes("Random Beacon")}
maxAuthAmount={stake.totalInTStake}
minAuthAmount={minAuthAmount}
minAuthAmount={randomBeaconMinAuthAmount}
stakingProvider={stakingProviderAddress!}
/>
<AuthorizeApplicationsCardCheckbox
mt={5}
appAuthData={appsAuthData.pre}
onCheckboxClick={onCheckboxClick}
isSelected={selectedApps.map((app) => app.label).includes("PRE")}
maxAuthAmount={stake.totalInTStake}
minAuthAmount={minAuthAmount}
minAuthAmount={"0"}
stakingProvider={stakingProviderAddress!}
/>
<Button
disabled={selectedApps.length === 0}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { CheckCircleIcon } from "@chakra-ui/icons"
import { AppAuthDataProps } from "../../AuthorizeStakingApps/AuthorizeApplicationsCardCheckbox"
import { Link as RouterLink } from "react-router-dom"
import { formatPercentage } from "../../../../utils/percentage"

export interface AuthorizeApplicationRowProps extends StackProps {
appAuthData: AppAuthDataProps
Expand Down Expand Up @@ -47,7 +48,7 @@ const AuthorizeApplicationRow: FC<AuthorizeApplicationRowProps> = ({
colorScheme="brand"
borderRadius={50}
/>
<BodySm>{percentage}%</BodySm>
<BodySm>{formatPercentage(percentage)}</BodySm>
</HStack>
) : (
<Button
Expand Down
Loading