Skip to content
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

Fix indexing #80

Merged
merged 5 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
218 changes: 100 additions & 118 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,18 @@ import { ethers } from 'ethers'
import { HeadingComponent } from '../components/layout/HeadingComponent'
import { ArrowForwardIcon, WarningIcon } from '@chakra-ui/icons'
import Image from 'next/image'
import { firstIteration } from '../utils/config'
import axios from 'axios'
import { startBlock, firstIteration, listOfBlocks } from '../utils/config'

export default function Home() {
const { address, chainId, isConnected } = useWeb3ModalAccount()
const { walletProvider } = useWeb3ModalProvider()
const customProvider = new ethers.JsonRpcProvider(process.env.NEXT_PUBLIC_RPC_ENDPOINT_URL)
const toast = useToast()
const queryURL: string = 'https://api.studio.thegraph.com/query/52496/gov-subgraph/version/latest'

const [initialized, setInitialized] = useState<boolean>(false)
const [isLoading, setIsLoading] = useState<boolean>(false)
const [provider, setProvider] = useState<any>(undefined)
const [name, setName] = useState<string>('Berlin04 Demo DAO')
const [name, setName] = useState<string>('Test DAO')
const [proposal, setProposal] = useState<{ id: string; link: string; title: string; state: number }[]>([])
const stateText = ['Pending', 'Active', 'Canceled', 'Defeated', 'Succeeded', 'Queued', 'Expired', 'Executed']
const stateColor = ['orange', 'green', 'blue', 'red', 'purple', 'blue', 'blue', 'blue']
Expand All @@ -43,18 +41,6 @@ export default function Home() {
}

const makeProposalObject = async () => {
const uniswapGraphQuery: string = `query proposalsCreated {
proposalCreateds(orderDirection: desc, orderBy: voteEnd) {
proposalId
blockNumber
calldatas
description
voteEnd
voteStart
transactionHash
}
}`

try {
console.log('fetching proposals...')
if (initialized) {
Expand All @@ -64,43 +50,114 @@ export default function Home() {
}
setIsLoading(true)
setProposal([])
const response = await axios.post(
queryURL,
{
query: uniswapGraphQuery,
},
{
headers: { 'Content-Type': 'application/json' },
}
)

const proposals = response.data.data.proposalCreateds
console.log('proposals', proposals)
let proposalRaw = proposal
const currentBlock = await customProvider.getBlockNumber()
const gov = new ethers.Contract(govContract.address, govContract.abi, customProvider)

console.log('listOfBlocks:', listOfBlocks)

let proposals: any
let i: number = 0
let proposalsRaw: any = proposal

// TODO: add a static list of blocks https://github.com/w3hc/gov-ui/issues/79
if (listOfBlocks.length > 0) {
console.log('listOfBlocks.length > 0')

let staticProposalsRaw = proposal

const firstBlock = listOfBlocks[0]
console.log('firstBlock', firstBlock)

console.log('proposals[0].proposalId', proposals[0].proposalId)
console.log('proposals.length', proposals.length)
if (proposals.length > 0) {
for (let i = 0; i < proposals.length; i++) {
proposalRaw.push(
const lastBlock = listOfBlocks[listOfBlocks.length - 1]
console.log(' lastBlock', lastBlock)

const staticProposals = await gov.queryFilter('ProposalCreated' as any, firstBlock, lastBlock)
console.log('staticProposals', staticProposals)

console.log('Number(listOfBlocks.length)', Number(listOfBlocks.length))

for (i = 0; i < Number(listOfBlocks.length); i++) {
console.log('iterations', i)

const staticProposal: any = await gov.queryFilter('ProposalCreated' as any, firstBlock, firstBlock)
console.log('staticProposal', staticProposal)

console.log('staticProposal proposalId', staticProposal[0].args?.proposalId)
console.log('staticProposal description', staticProposal[0].args?.description)
console.log('staticProposal title', staticProposal[0].args?.description)

staticProposalsRaw.push(
...[
{
id: String(proposals[i].proposalId),
link: baseUrl + String(proposals[i].proposalId),
title: proposals[i].description.substring(proposals[i].description == '#' ? 2 : 2, proposals[i].description.indexOf('\n')),
state: Number(await getState(proposals[i].proposalId)),
id: String(staticProposal[0].args?.proposalId),
link: baseUrl + String(staticProposal[0].args?.proposalId),
// title: 'hey',
title: staticProposal[0].args?.description.substring(
staticProposal[0].args?.description == '#' ? 2 : 0,
staticProposal[0].args?.description.indexOf('\n')
),
state: await getState(staticProposal[0].args?.proposalId),
},
]
)
}

const uniqueProposals = staticProposalsRaw.filter((item, index, self) => index === self.findIndex((t) => t.id === item.id))
setProposal(uniqueProposals)
console.log('all proposals fetched ✅')
setInitialized(true)
setIsLoading(false)

// proposals = await gov.queryFilter('ProposalCreated' as any, lastBlock, currentBlock)
// console.log('proposals:', proposals)

// let proposalsRaw = proposal // to remove

// if (proposals[0].args != undefined) {
// for (i = 0; i < Number(proposals.length); i++) {
// proposalsRaw.push(
// ...[
// {
// id: String(proposals[i].args?.proposalId),
// link: baseUrl + String(proposals[i].args?.proposalId),
// title: proposals[i].args[8].substring(proposals[i].args[8][0] == '#' ? 2 : 0, proposals[i].args[8].indexOf('\n')),
// state: await getState(proposals[i].args?.proposalId),
// },
// ]
// )
// }
// const uniqueProposals = proposal.filter((item, index, self) => index === self.findIndex((t) => t.id === item.id))
// setProposal(uniqueProposals)
// console.log('all proposals fetched ✅')
// setInitialized(true)
// setIsLoading(false)
// }
// }
} else {
console.log('\nNo proposals found')
}
proposals = await gov.queryFilter('ProposalCreated' as any, startBlock, currentBlock)
console.log('proposals:', proposals)

const uniqueProposals = proposal.filter((item, index, self) => index === self.findIndex((t) => t.id === item.id))
setProposal(uniqueProposals)
console.log('all proposals fetched ✅')
setInitialized(true)
if (proposals[0].args != undefined) {
for (i = firstIteration; i < Number(proposals.length); i++) {
proposalsRaw.push(
...[
{
id: String(proposals[i].args?.proposalId),
link: baseUrl + String(proposals[i].args?.proposalId),
title: proposals[i].args[8].substring(proposals[i].args[8][0] == '#' ? 2 : 0, proposals[i].args[8].indexOf('\n')),
state: await getState(proposals[i].args?.proposalId),
},
]
)
}
const uniqueProposals = proposal.filter((item, index, self) => index === self.findIndex((t) => t.id === item.id))
setProposal(uniqueProposals)
console.log('all proposals fetched ✅')
setInitialized(true)
setIsLoading(false)
}
}
setIsLoading(false)
} catch (error: any) {
setIsLoading(false)
Expand All @@ -118,81 +175,6 @@ export default function Home() {
})
}
}

// try {
// console.log('fetching proposals...')
// if (initialized) {
// console.log('already initialized')
// setIsLoading(false)
// return
// }
// setIsLoading(true)
// const gov = new ethers.Contract(govContract.address, govContract.abi, customProvider)
// setProposal([])

// if (typeof gov.getProposalCreatedBlocks === 'function') {
// const proposalCreatedBlocks = await gov.getProposalCreatedBlocks()
// let proposalRaw = proposal
// for (let i = firstIteration; i < proposalCreatedBlocks.length; i++) {
// console.log('iteration:', i)
// /////////////////*******//////////////

// const proposals = (await gov.queryFilter('ProposalCreated', proposalCreatedBlocks[i])) as any

// if (proposals.length > 0) {
// proposalRaw.push(
// ...[
// {
// id: String(proposals[0].args[0]),
// link: baseUrl + String(proposals[0].args[0]),
// title: proposals[0].args[8].substring(proposals[0].args[8] == '#' ? 2 : 2, proposals[0].args[8].indexOf('\n')),
// state: Number(await getState(proposals[0].args[0])),
// },
// ]
// )
// } else {
// console.log('\nNo proposals found at block #' + Number(proposalCreatedBlocks[i]))
// }
// }

// // TODO: fix executed twice...
// // Remove duplicates based on the `id` property
// const uniqueProposals = proposal.filter((item, index, self) => index === self.findIndex((t) => t.id === item.id))
// setProposal(uniqueProposals)

// console.log('all proposals fetched ✅')
// setInitialized(true)
// setIsLoading(false)
// } else {
// console.error('getProposalCreatedBlocks method not available on this DAO contract')
// setInitialized(true)
// setIsLoading(false)
// toast({
// title: 'Oh no!',
// description: 'The getProposalCreatedBlocks method is NOT available on this contract',
// status: 'error',
// position: 'bottom',
// variant: 'subtle',
// duration: 9000,
// isClosable: true,
// })
// }
// } catch (error: any) {
// setIsLoading(false)
// setInitialized(true)
// console.error('error:', error)
// if (!error.message.includes('could not decode result data')) {
// toast({
// title: 'Woops',
// description: 'Something went wrong...',
// status: 'error',
// position: 'bottom',
// variant: 'subtle',
// duration: 9000,
// isClosable: true,
// })
// }
// }
}

useEffect(() => {
Expand Down
64 changes: 30 additions & 34 deletions src/pages/proposal/[proposalId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,50 +84,46 @@ export default function Proposal() {

const getProposalData = async () => {
setIsLoading(true)
try {
if (initialized) {
console.log('already initialized')
setIsLoading(false)
return
}
const block = await customProvider.getBlockNumber()
const gov = new ethers.Contract(govContract.address, govContract.abi, customProvider)
const proposals: any = await gov.queryFilter('ProposalCreated' as any, 6031421, block)

const gov = new ethers.Contract(govContract.address, govContract.abi, customProvider)

const proposalCreatedBlocks = await gov.getProposalCreatedBlocks()

let block
for (let i = firstIteration; i < proposalCreatedBlocks.length; i++) {
console.log('iteration:', i)

const proposals: any = await gov.queryFilter('ProposalCreated', block)

if (String(proposals[i].args[0]) === proposalId) {
setTitle(proposals[i].args[8].substring(proposals[i].args[8] == '#' ? 2 : 2, proposals[i].args[8].indexOf('\n')))
setDescription(proposals[i].args[8].substring(proposals[i].args[8].indexOf('\n')))
setCalldatas(proposals[i].args[5])
await getState(proposalId)
await getCurrentVotes(proposalId)
setTargets(proposals[i].args[2][0])
setValues(proposals[i].args[3][0])
setCalldatas(proposals[i].args[5][0])
setRawDescription(proposals[i].args[8])
const proposalExecuted: any = await gov.queryFilter('ProposalExecuted', block)
for (let i = 18; i < proposalExecuted.length; i++) {
if (String(proposalExecuted[i].args[0]) === proposalId) {
const executeTxHash: any = await gov.queryFilter('ProposalExecuted', proposalExecuted[i].blockNumber)
console.log('executeTxHash:', executeTxHash[0].transactionHash)
setExecuteTxLink('https://sepolia.etherscan.io/tx/' + executeTxHash[0].transactionHash)
try {
let i: number = 0

if (proposals[0].args != undefined) {
for (i = firstIteration; i < Number(proposals.length); i++) {
const id = String(proposals[i].args?.proposalId)

if (id == proposalId) {
setTitle(proposals[i].args[8].substring(proposals[i].args[8][0] == '#' ? 2 : 0, proposals[i].args[8].indexOf('\n')))
setDescription(proposals[i].args[8].substring(proposals[i].args[8].indexOf('\n')))
setCalldatas(proposals[0].args[5])

await getState(proposalId)
await getCurrentVotes(proposalId)

setTargets(proposals[i].args[2][0])
setValues(proposals[i].args[3][0])

setCalldatas(proposals[i].args[5][0])
setRawDescription(proposals[i].args[8])
const proposalExecuted: any = await gov.queryFilter('ProposalExecuted', block)
for (let i = 18; i < proposalExecuted.length; i++) {
if (String(proposalExecuted[i].args[0]) === proposalId) {
const executeTxHash: any = await gov.queryFilter('ProposalExecuted', proposalExecuted[i].blockNumber)
console.log('executeTxHash:', executeTxHash[0].transactionHash)
setExecuteTxLink('https://sepolia.etherscan.io/tx/' + executeTxHash[0].transactionHash)
}
}
}
}
}

setInitialized(true)
setIsLoading(false)
} catch (error) {
console.error('error:', error)
setInitialized(true)
setIsLoading(false)
}
}

Expand Down
32 changes: 32 additions & 0 deletions src/pages/request-eur/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,38 @@ export default function RequestEur() {
return
}

// TODO: Check if user is MEMBER
// if (!isConnected) {
// toast({
// title: 'Disconnected',
// position: 'bottom',
// description: 'Please connect your wallet first.',
// status: 'info',
// variant: 'subtle',
// duration: 2000,
// isClosable: true,
// })
// console.log('user disconnected')
// setIsLoading(false)
// return
// }

// TODO: Check if user is DELEGATED
// if (!isConnected) {
// toast({
// title: 'Disconnected',
// position: 'bottom',
// description: 'Please connect your wallet first.',
// status: 'info',
// variant: 'subtle',
// duration: 2000,
// isClosable: true,
// })
// console.log('user disconnected')
// setIsLoading(false)
// return
// }

await handleBalance()

// Load contracts
Expand Down
Loading