generated from scaffold-eth/scaffold-eth
-
Notifications
You must be signed in to change notification settings - Fork 693
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into cooler-loans-api-bigquery
- Loading branch information
Showing
13 changed files
with
341 additions
and
116 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 |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { | ||
Box, | ||
Paper, | ||
Tab, | ||
Table, | ||
TableBody, | ||
TableCell, | ||
TableContainer, | ||
TableHead, | ||
TableRow, | ||
Tabs, | ||
Typography, | ||
} from "@mui/material"; | ||
import React, { useState } from "react"; | ||
import { useParams } from "react-router-dom"; | ||
import { useGetVotes } from "src/views/Governance/hooks/useGetVotes"; | ||
import { VoteRow } from "src/views/Governance/Proposals/VoteRow"; | ||
|
||
// Data for the tables, including a 'reason' field for each row | ||
const tablesData = [ | ||
{ | ||
id: "For", | ||
contractValue: 1, | ||
}, | ||
{ | ||
id: "Against", | ||
contractValue: 0, | ||
}, | ||
{ | ||
id: "Abstain", | ||
contractValue: 2, | ||
}, | ||
]; | ||
|
||
const TabPanel = (props: { children: React.ReactNode; value: number; index: number }) => { | ||
const { children, value, index, ...other } = props; | ||
return ( | ||
<div role="tabpanel" hidden={value !== index} id={`tabpanel-${index}`} aria-labelledby={`tab-${index}`} {...other}> | ||
{value === index && <Box p={3}>{children}</Box>} | ||
</div> | ||
); | ||
}; | ||
|
||
export default function GovernanceTable() { | ||
const { id } = useParams(); | ||
const [tabIndex, setTabIndex] = useState(0); | ||
const [supportValue, setSupportValue] = useState(1); | ||
const { data: voteData } = useGetVotes({ proposalId: id, support: supportValue }); | ||
|
||
const handleTabChange = (event: React.SyntheticEvent, newIndex: number) => { | ||
setSupportValue(tablesData[newIndex].contractValue); | ||
setTabIndex(newIndex); | ||
}; | ||
|
||
return ( | ||
<Box sx={{ width: "100%" }}> | ||
<Tabs | ||
textColor="primary" | ||
aria-label="proposal tabs" | ||
indicatorColor="primary" | ||
value={tabIndex} | ||
onChange={handleTabChange} | ||
//hides the tab underline sliding animation in while <Zoom> is loading | ||
TabIndicatorProps={{ style: { display: "none" } }} | ||
centered | ||
> | ||
{" "} | ||
{tablesData.map((table, index) => ( | ||
<Tab label={table.id} key={index} /> | ||
))} | ||
</Tabs> | ||
|
||
{tablesData.map((table, index) => ( | ||
<TabPanel value={tabIndex} index={index} key={index}> | ||
<TableContainer component={Paper}> | ||
<Table> | ||
<TableHead> | ||
<TableRow> | ||
<TableCell>{tablesData[tabIndex].id}</TableCell> | ||
<TableCell align="right">Votes</TableCell> | ||
</TableRow> | ||
</TableHead> | ||
<TableBody> | ||
{voteData?.length ? ( | ||
voteData.map((row, i) => ( | ||
<TableRow key={i}> | ||
<VoteRow voter={row.voter} reason={row.reason} votes={row.votes} tx={row.transactionHash} /> | ||
</TableRow> | ||
)) | ||
) : ( | ||
<TableRow> | ||
<TableCell colSpan={2} align="center"> | ||
<Typography>No votes yet</Typography> | ||
</TableCell> | ||
</TableRow> | ||
)} | ||
</TableBody> | ||
</Table> | ||
</TableContainer> | ||
</TabPanel> | ||
))} | ||
</Box> | ||
); | ||
} |
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,37 @@ | ||
import { Box, Link, TableCell, Tooltip, Typography } from "@mui/material"; | ||
import { formatEther } from "ethers/lib/utils.js"; | ||
import { abbreviatedNumber } from "src/helpers"; | ||
import { truncateEthereumAddress } from "src/helpers/truncateAddress"; | ||
import { useEnsName } from "wagmi"; | ||
|
||
export const VoteRow = ({ | ||
voter, | ||
reason, | ||
votes, | ||
tx, | ||
}: { | ||
voter: string; | ||
reason?: string; | ||
votes: string; | ||
tx: string; | ||
}) => { | ||
const { data: ensName } = useEnsName({ address: voter as `0x${string}` }); | ||
return ( | ||
<> | ||
<TableCell> | ||
<Link href={`https://etherscan.io/tx/${tx}`} target="_blank" rel="noopener noreferrer"> | ||
<Tooltip title={voter}> | ||
<Box>{ensName || truncateEthereumAddress(voter)}</Box> | ||
</Tooltip> | ||
</Link> | ||
{/* Render the reason if provided, and style it as a comment */} | ||
{reason && ( | ||
<Typography variant="body2" sx={{ color: "gray", fontStyle: "italic", mt: 1 }}> | ||
"{reason}" | ||
</Typography> | ||
)} | ||
</TableCell> | ||
<TableCell align="right">{abbreviatedNumber.format(Number(formatEther(votes) || 0))} gOHM</TableCell> | ||
</> | ||
); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Proposal } from "src/views/Governance/hooks/useGetProposalFromSubgraph"; | ||
|
||
// Normalizes the proposal data to match the onchain format | ||
export const normalizeProposal = (proposal: Proposal) => { | ||
return { | ||
createdAtBlock: new Date(Number(proposal.blockTimestamp) * 1000), | ||
details: { | ||
id: proposal.proposalId, | ||
proposer: proposal.proposer, | ||
targets: proposal.targets, | ||
values: proposal.values, | ||
signatures: proposal.signatures, | ||
calldatas: proposal.calldatas, | ||
startBlock: proposal.startBlock, | ||
description: proposal.description, | ||
}, | ||
title: proposal.description.split(/#+\s|\n/g)[1] || `${proposal.description.slice(0, 20)}...`, | ||
txHash: proposal.transactionHash, | ||
}; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { useQuery } from "@tanstack/react-query"; | ||
import request, { gql } from "graphql-request"; | ||
import { Environment } from "src/helpers/environment/Environment/Environment"; | ||
import { normalizeProposal } from "src/views/Governance/helpers/normalizeProposal"; | ||
|
||
export type Proposal = { | ||
proposalId: string; | ||
proposer: string; | ||
targets: string[]; | ||
signatures: string[]; | ||
calldatas: string[]; | ||
transactionHash: string; | ||
description: string; | ||
blockTimestamp: string; | ||
blockNumber: string; | ||
startBlock: string; | ||
values: string[]; | ||
}; | ||
|
||
type ProposalResponse = { | ||
proposalCreated: Proposal; | ||
}; | ||
|
||
export const useGetProposalFromSubgraph = ({ proposalId }: { proposalId?: string }) => { | ||
const query = gql` | ||
query { | ||
proposalCreated(id: ${proposalId}) { | ||
proposalId | ||
proposer | ||
targets | ||
signatures | ||
calldatas | ||
transactionHash | ||
description | ||
blockTimestamp | ||
blockNumber | ||
startBlock | ||
values | ||
} | ||
} | ||
`; | ||
|
||
return useQuery( | ||
["getProposal", proposalId], | ||
async () => { | ||
try { | ||
const subgraphUrl = Environment.getGovernanceSubgraphUrl(); | ||
const response = await request<ProposalResponse>(subgraphUrl, query); | ||
if (!response.proposalCreated) { | ||
return null; | ||
} | ||
return normalizeProposal(response.proposalCreated); | ||
} catch (error) { | ||
console.error("useGetProposalFromSubgraph", error); | ||
return null; | ||
} | ||
}, | ||
{ enabled: !!proposalId }, | ||
); | ||
}; |
Oops, something went wrong.