Skip to content

Commit

Permalink
DAO-707 LastBlock in the frontend implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Freshenext committed Sep 27, 2024
1 parent b43627a commit 4b93cad
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
14 changes: 10 additions & 4 deletions src/app/proposals/api/route.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import { fetchProposalCreated } from '@/app/user/Balances/actions'
import { NextRequest } from 'next/server'

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

function fetchProposals() {
cachedProposals.isFetching = true
console.log(11, 'Fetching proposals...')
fetchProposalCreated()
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 []
cachedProposals.error = ''
cachedProposals.lastFromBlock = Number(data[data.length - 1].blockNumber) + 1 // Update lastFromBlock based on last proposal
} else {
cachedProposals.error = JSON.stringify(data)
}
Expand All @@ -29,8 +32,11 @@ function fetchProposals() {

const SECONDS_INTERVAL = 10

export async function GET(request: Request) {
const disableCache = request.url
export async function GET(request: NextRequest) {
const shouldRestartFromBlock = request.nextUrl.searchParams.get('restartBlock')
if (shouldRestartFromBlock === '1') {
cachedProposals.lastFromBlock = 0
}
const currentTime = Date.now()
const timeElapsed = (currentTime - cachedProposals.lastUpdated) / 1000 // Time elapsed in seconds

Expand Down
8 changes: 6 additions & 2 deletions src/app/user/Balances/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,12 @@ export const fetchNftsOwnedByAddressAndNFTAddress = (address: string, nftAddress
.then(({ data }) => data)
.catch(error => console.log(error))

export const fetchProposalCreated = () =>
axiosInstance.get(fetchProposalsCreatedByGovernorAddress.replace('{{address}}', GovernorAddress))
export const fetchProposalCreated = (fromBlock = 0) =>
axiosInstance.get(
fetchProposalsCreatedByGovernorAddress
.replace('{{address}}', GovernorAddress)
.replace('{{fromBlock}}', fromBlock.toString()),
)

export const fetchProposalsCreatedCached = () => axiosInstance.get('/proposals/api', { baseURL: '/' })

Expand Down
2 changes: 1 addition & 1 deletion src/lib/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export const fetchPricesEndpoint = `/price?addresses={{addresses}}&convert={{con

export const fetchNFTsOwnedByAddressAndNftAddress = `/address/{{address}}/nfts/{{nftAddress}}?chainId=${CHAIN_ID}`

export const fetchProposalsCreatedByGovernorAddress = `/address/{{address}}/eventsByTopic0?topic0=0x7d84a6263ae0d98d3329bd7b46bb4e8d6f98cd35a7adb45c274c8b7fd5ebd5e0&chainId=${CHAIN_ID}`
export const fetchProposalsCreatedByGovernorAddress = `/address/{{address}}/eventsByTopic0?topic0=0x7d84a6263ae0d98d3329bd7b46bb4e8d6f98cd35a7adb45c274c8b7fd5ebd5e0&chainId=${CHAIN_ID}&fromBlock={{fromBlock}}`

export const getNftInfo = `/nfts/{{nftAddress}}?chainId=${CHAIN_ID}`

0 comments on commit 4b93cad

Please sign in to comment.