Skip to content

Commit

Permalink
Merge pull request #76 from oasisprotocol/csillag/simplify-eth-massing
Browse files Browse the repository at this point in the history
Simplify distributing ethereum access
  • Loading branch information
CedarMist authored Oct 4, 2024
2 parents 4ef1666 + f0ab968 commit 7679ba5
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 23 deletions.
6 changes: 4 additions & 2 deletions frontend/src/hooks/useContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import {
IPollACL__factory,
} from '@oasisprotocol/blockvote-contracts'
import type { PollManager, GaslessVoting, IPollACL, IPollManagerACL } from '../types'
import { EthereumContext } from '../providers/EthereumContext'
import { useEffect, useState } from 'react'
import {
VITE_CONTRACT_GASLESSVOTING,
VITE_CONTRACT_POLLMANAGER,
VITE_CONTRACT_POLLMANAGER_ACL,
} from '../constants/config'
import { useEthereum } from './useEthereum'

export const useContracts = (eth: EthereumContext, aclAddress?: string | undefined) => {
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>()
Expand Down Expand Up @@ -69,6 +70,7 @@ export const useContracts = (eth: EthereumContext, aclAddress?: string | undefin
}, [aclAddress, eth.state.provider])

return {
eth,
pollManager,
pollManagerAddress,
pollManagerWithSigner,
Expand Down
4 changes: 1 addition & 3 deletions frontend/src/hooks/useExtendedPoll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { dashboard, demoSettings } from '../constants/config'
import { getDemoPoll } from '../constants/demoPolls'
import { usePollGaslessStatus } from './usePollGaslessStatus'
import { usePollPermissions } from './usePollPermissions'
import { useEthereum } from './useEthereum'
import { useContracts } from './useContracts'
import { decodePollMetadata } from '../utils/poll.utils'
import { getVerdict } from '../components/InputFields'
Expand All @@ -29,8 +28,7 @@ export const useExtendedPoll = (
onDashboard: boolean
},
) => {
const eth = useEthereum()
const { pollManager } = useContracts(eth)
const { pollManager } = useContracts()

const proposalId = proposal?.id
const isDemo = proposalId === '0xdemo'
Expand Down
4 changes: 1 addition & 3 deletions frontend/src/hooks/usePollGaslessStatus.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { useEffect, useState } from 'react'
import { useContracts } from './useContracts'
import { useEthereum } from './useEthereum'
import { dashboard } from '../constants/config'

export const usePollGaslessStatus = (proposalId: string | undefined, onDashboard: boolean) => {
const eth = useEthereum()
const { pollManagerAddress: daoAddress, gaslessVoting } = useContracts(eth)
const { pollManagerAddress: daoAddress, gaslessVoting } = useContracts()

const [version, setVersion] = useState(0)
const [gvAddresses, setGvAddresses] = useState<string[]>([])
Expand Down
17 changes: 10 additions & 7 deletions frontend/src/hooks/usePollPermissions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useEffect, useState } from 'react'
import { useContracts } from './useContracts'
import { useEthereum } from './useEthereum'
import { denyWithReason } from '../components/InputFields'
import { ExtendedPoll } from '../types'
import { CheckPermissionContext, CheckPermissionInputs, PollPermissions } from '../utils/poll.utils'
Expand All @@ -21,17 +20,18 @@ export const usePollPermissions = (poll: ExtendedPoll | undefined, onDashboard:
const aclAddress = poll?.proposal.params.acl
const creator = poll?.proposal.owner

const eth = useEthereum()
const { pollManagerAddress: daoAddress, pollACL } = useContracts(eth, aclAddress)

const { userAddress } = eth
const { eth, pollManagerAddress: daoAddress, pollACL } = useContracts(aclAddress)

const [isPending, setIsPending] = useState(false)
const [isMine, setIsMine] = useState<boolean | undefined>()
const [permissions, setPermissions] = useState<PollPermissions>({ ...blackPermissions })

const checkPermissions = async (force = false) => {
const isDemo = proposalId === '0xdemo'
const {
userAddress,
state: { provider },
} = eth

setIsMine(
isDemo
Expand Down Expand Up @@ -75,7 +75,7 @@ export const usePollPermissions = (poll: ExtendedPoll | undefined, onDashboard:

const context: CheckPermissionContext = {
daoAddress,
provider: eth.state.provider,
provider,
}

setIsPending(true)
Expand All @@ -84,7 +84,10 @@ export const usePollPermissions = (poll: ExtendedPoll | undefined, onDashboard:
if (newStatus) setPermissions(newStatus)
}

useEffect(() => void checkPermissions(), [proposalId, pollACL, daoAddress, poll?.ipfsParams, userAddress])
useEffect(
() => void checkPermissions(),
[proposalId, pollACL, daoAddress, poll?.ipfsParams, eth.userAddress],
)

return {
isMine,
Expand Down
4 changes: 1 addition & 3 deletions frontend/src/hooks/useProposalFromChain.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { useEffect, useState } from 'react'
import { Proposal } from '../types'
import { useEthereum } from './useEthereum'
import { useContracts } from './useContracts'
import { ZeroAddress } from 'ethers'

export const useProposalFromChain = (proposalId: string) => {
const eth = useEthereum()
const { pollManager } = useContracts(eth)
const { pollManager } = useContracts()

const [isLoading, setIsLoading] = useState(false)
const [proposal, setProposal] = useState<Proposal>()
Expand Down
5 changes: 2 additions & 3 deletions frontend/src/pages/CreatePollPage/useCreatePollForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
useTextField,
} from '../../components/InputFields'
import { createPoll as doCreatePoll, parseEther, CreatePollProps } from '../../utils/poll.utils'
import { useEthereum } from '../../hooks/useEthereum'
import { useContracts } from '../../hooks/useContracts'
import classes from './index.module.css'
import { DateUtils } from '../../utils/date.utils'
Expand Down Expand Up @@ -43,8 +42,8 @@ const expectedRanges = {
} as const

export const useCreatePollForm = () => {
const eth = useEthereum()
const { pollManagerWithSigner: daoSigner } = useContracts(eth)
// const eth = useEthereum()
const { eth, pollManagerWithSigner: daoSigner } = useContracts()

const [isCreating, setIsCreating] = useState<boolean>(false)
const [step, setStep] = useState<CreationStep>('basics')
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/DashboardPage/useDashboardData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ DashboardData.export()

export const useDashboardData = () => {
const eth = useEthereum()
const { pollManager, pollManagerAddress: daoAddress, pollManagerACL } = useContracts(eth)
const { pollManager, pollManagerAddress: daoAddress, pollManagerACL } = useContracts()

const { userAddress } = eth

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/PollPage/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const usePollData = (pollId: string) => {
} = useExtendedPoll(proposal, { onDashboard: false })

const { now } = useTime()
const { pollManagerWithSigner: signerDao, gaslessVoting } = useContracts(eth, poll?.proposal.params?.acl)
const { pollManagerWithSigner: signerDao, gaslessVoting } = useContracts(poll?.proposal.params?.acl)

const remainingTime = useMemo(
() => (deadline ? DateUtils.calculateRemainingTimeFrom(deadline, now) : undefined),
Expand Down

0 comments on commit 7679ba5

Please sign in to comment.