Skip to content

Commit

Permalink
DAO-XXX-Route fix: cached proposals additional process logic for prod…
Browse files Browse the repository at this point in the history
… build (#254)
  • Loading branch information
Freshenext authored Oct 7, 2024
1 parent 2569f11 commit e891a56
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/app/proposals/api/route.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
import { fetchProposalCreated } from '@/app/user/Balances/actions'
import { BackendEventByTopic0ResponseValue, fetchProposalCreated } from '@/app/user/Balances/actions'
import { NextRequest } from 'next/server'

let cachedProposals = {
lastUpdated: Date.now(),
data: [],
data: [] as BackendEventByTopic0ResponseValue[],
isFetching: false,
error: '',
lastFromBlock: 0,
}

const shouldAddRowToDataArray = (newTransaction: { blockNumber: string }) => {
const indexFound = cachedProposals.data.findIndex(i => i.blockNumber === newTransaction.blockNumber)
return indexFound === -1
}

function fetchProposals() {
cachedProposals.isFetching = true
console.log(`13: Fetching proposals with lastFromBlock: ${cachedProposals.lastFromBlock}`)
fetchProposalCreated(cachedProposals.lastFromBlock)
.then(({ data }) => {
console.log(14, 'Finished fetching proposals...')
if (Array.isArray(data) && data.length > 0) {
cachedProposals.data = data as []
const dataToBeAdded = data.filter(shouldAddRowToDataArray)
if (dataToBeAdded.length > 0) {
cachedProposals.data.push(...dataToBeAdded)
}
cachedProposals.error = ''
cachedProposals.lastFromBlock = Number(data[data.length - 1].blockNumber) + 1 // Update lastFromBlock based on last proposal
} else {
Expand Down
15 changes: 14 additions & 1 deletion src/app/user/Balances/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,21 @@ export const fetchNftsOwnedByAddressAndNFTAddress = (address: string, nftAddress
.then(({ data }) => data)
.catch(error => console.log(error))

export interface BackendEventByTopic0ResponseValue {
address: string
blockNumber: string
data: string
gasPrice: string
gasUsed: string
logIndex: string
timeStamp: string
topics: Array<null | string>
transactionHash: string
transactionIndex: string
}

export const fetchProposalCreated = (fromBlock = 0) =>
axiosInstance.get(
axiosInstance.get<BackendEventByTopic0ResponseValue[]>(
fetchProposalsCreatedByGovernorAddress
.replace('{{address}}', GovernorAddress)
.replace('{{fromBlock}}', fromBlock.toString()),
Expand Down

0 comments on commit e891a56

Please sign in to comment.