-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from oasisprotocol/csillag/refactor-simplify
Refactor and simplify frontend code
- Loading branch information
Showing
19 changed files
with
189 additions
and
548 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,11 @@ | ||
import { | ||
PollManager__factory, | ||
GaslessVoting__factory, | ||
IPollManagerACL__factory, | ||
IPollACL__factory, | ||
} from '@oasisprotocol/blockvote-contracts' | ||
import type { PollManager, GaslessVoting, IPollACL, IPollManagerACL } from '../types' | ||
import { useEffect, useState } from 'react' | ||
import { | ||
VITE_CONTRACT_GASLESSVOTING, | ||
VITE_CONTRACT_POLLMANAGER, | ||
VITE_CONTRACT_POLLMANAGER_ACL, | ||
} from '../constants/config' | ||
import { useEthereum } from './useEthereum' | ||
import { useContext } from 'react' | ||
import { ContractContext } from '../providers/ContractContext' | ||
|
||
export const useContracts = (aclAddress?: string | undefined) => { | ||
const eth = useEthereum() | ||
const [pollManager, setPollManager] = useState<PollManager | undefined>() | ||
const [pollManagerAddress, setPollManagerAddress] = useState<string | undefined>() | ||
const [pollManagerWithSigner, setPollManagerWithSigner] = useState<PollManager | undefined>() | ||
const [pollACL, setPollACL] = useState<IPollACL | undefined>() | ||
const [pollManagerACL, setPollManagerACL] = useState<IPollManagerACL | undefined>() | ||
const [gaslessVoting, setGaslessVoting] = useState<GaslessVoting>() | ||
|
||
useEffect(() => { | ||
if (!eth.state.provider) { | ||
setPollManager(undefined) | ||
setPollManagerACL(undefined) | ||
setGaslessVoting(undefined) | ||
return | ||
} | ||
const pollManagerAddr = VITE_CONTRACT_POLLMANAGER | ||
// console.log('PollManager at', pollManagerAddr); | ||
setPollManager(PollManager__factory.connect(pollManagerAddr, eth.state.provider)) | ||
|
||
const pollManagerAclAddr = VITE_CONTRACT_POLLMANAGER_ACL | ||
// console.log('IPollManagerACL at', pollManagerAclAddr); | ||
setPollManagerACL(IPollManagerACL__factory.connect(pollManagerAclAddr, eth.state.provider)) | ||
|
||
const gaslessVotingAddr = VITE_CONTRACT_GASLESSVOTING | ||
// console.log('GaslessVoting at', gaslessVotingAddr); | ||
setGaslessVoting(GaslessVoting__factory.connect(gaslessVotingAddr, eth.state.provider)) | ||
}, [eth.state.provider]) | ||
|
||
useEffect(() => { | ||
if (pollManager) { | ||
pollManager.getAddress().then(setPollManagerAddress) | ||
} else { | ||
setPollManagerAddress(undefined) | ||
} | ||
}, [pollManager]) | ||
|
||
useEffect(() => { | ||
if (!eth.state.signer) { | ||
setPollManagerWithSigner(undefined) | ||
return | ||
} | ||
const pollManagerAddr = VITE_CONTRACT_POLLMANAGER | ||
// console.log('PollManager at', pollManagerAddr); | ||
setPollManagerWithSigner(PollManager__factory.connect(pollManagerAddr, eth.state.signer)) | ||
}, [eth.state.signer]) | ||
|
||
useEffect(() => { | ||
if (!eth.state.provider || !aclAddress) { | ||
setPollACL(undefined) | ||
return | ||
} | ||
// console.log('IPollACL at', aclAddress); | ||
setPollACL(IPollACL__factory.connect(aclAddress, eth.state.provider)) | ||
}, [aclAddress, eth.state.provider]) | ||
|
||
return { | ||
eth, | ||
pollManager, | ||
pollManagerAddress, | ||
pollManagerWithSigner, | ||
pollACL, | ||
pollManagerACL, | ||
gaslessVoting, | ||
export const useContracts = () => { | ||
const value = useContext(ContractContext) | ||
if (Object.keys(value).length === 0) { | ||
throw new Error('[useContracts] Component not wrapped within a Provider') | ||
} | ||
|
||
return value | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import { FC, PropsWithChildren } from 'react' | ||
import { useWeb3 } from '../hooks/useWeb3' | ||
import { useEthereum } from '../hooks/useEthereum' | ||
import { LandingPage } from './LandingPage' | ||
|
||
export const RestrictedContent: FC<PropsWithChildren> = props => | ||
useWeb3().state.isConnected ? props.children : <LandingPage /> | ||
export const RestrictedContent: FC<PropsWithChildren> = props => { | ||
const { isConnected, isHomeChain } = useEthereum() | ||
return isHomeChain && isConnected ? props.children : <LandingPage /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { EthereumContext } from './EthereumContext' | ||
import { PollManager, IPollACL, GaslessVoting, IPollManagerACL } from '@oasisprotocol/blockvote-contracts' | ||
import { createContext } from 'react' | ||
|
||
export interface ContractContextData { | ||
readonly eth: EthereumContext | ||
readonly pollManager: PollManager | undefined | ||
readonly pollManagerAddress: string | undefined | ||
readonly pollManagerWithSigner: PollManager | undefined | ||
getPollACL: (address: string | undefined) => IPollACL | undefined | ||
readonly pollManagerACL: IPollManagerACL | undefined | ||
readonly gaslessVoting: GaslessVoting | undefined | ||
} | ||
|
||
export const ContractContext = createContext<ContractContextData>({} as ContractContextData) |
Oops, something went wrong.