-
Notifications
You must be signed in to change notification settings - Fork 28
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
Changes from all commits
4a1a30f
71b182a
938440a
b9c7e62
711eed8
d4936b2
a3ab33a
e779671
7c816ee
748894c
a6da181
051023e
e66dd9d
8bd4f10
33b13e3
7562242
206b6dc
6d0c5d1
c144a63
28eee62
a916901
d7e5a84
367be12
5b96c75
ec89e73
dff12d7
22c103d
15ccbbf
5ac843f
5b2af30
1141bb7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need a cleanup function here if the user disconnects? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}> | ||
|
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" |
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 | ||
) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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 | ||
} |
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 | ||
} |
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, | ||
}) | ||
) | ||
} | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./useAppDispatch" | ||
export * from "./useAppSelector" |
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 |
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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