JS: Pagination and Listing

Use the oracle’s pagination helpers to list queries and inspect answers without loading unbounded arrays on-chain.

Queries listing

Available sets:

  • Pending: getPendingQueries(offset, limit)
  • Active: getActiveQueries(offset, limit)
  • Past: getPastQueries(offset, limit)
  • Extended (tie extensions): getExtendedQueries(offset, limit, stateFilter)
    • stateFilter: 0 = ALL, 1 = PENDING, 2 = ACTIVE, 3 = PAST

Counts:

  • getPendingQueriesCount()
  • getActiveQueriesCount()
  • getPastQueriesCount()
  • getExtendedQueriesCount(stateFilter)

Example:

import { JsonRpcProvider, Contract } from 'ethers'
import OracleArtifact from '../../../artifacts/contracts/FarmTruth.sol/DecentralizedOracle.json'

const provider = new JsonRpcProvider(process.env.RPC!)
const ORACLE_ADDRESS = '0xFA4595F636887CA28FCA3260486e44fdcc8c8A71'
const oracle = new Contract(ORACLE_ADDRESS, (OracleArtifact as any).abi, provider)

// Counts (BigInt)
const activeCount = await oracle.getActiveQueriesCount()
console.log('Active:', activeCount.toString())

// Page 1 (0..49)
const page1 = await oracle.getActiveQueries(0, 50n)
console.log('Active IDs page 1:', page1.map(v => v.toString()))

// Page 2 (50..99)
const page2 = await oracle.getActiveQueries(50n, 50n)

Filter extended queries:

// 0=ALL, 1=PENDING, 2=ACTIVE, 3=PAST
const EXT_ACTIVE = 2n
const extendedActive = await oracle.getExtendedQueries(0, 50n, EXT_ACTIVE)

Answer distribution pagination

Use getQueryAnswers(queryId, offset, limit) to page through answers.

const queryId = 0n
const [hashes, decodedAnswers, weights] = await oracle.getQueryAnswers(queryId, 0, 100n)

decodedAnswers.forEach((opts, i) => {
  console.log('Answer', i, 'options:', opts.map(o => o.toString()), 'weight:', weights[i].toString())
})

Count answers up-front:

const totalAnswers = await oracle.getQueryAnswersCount(queryId)
console.log('Total answer variants:', totalAnswers.toString())

Details drill-down

For each ID, read detailed metadata/timing:

const id = 0n
const d = await oracle.getQueryDetails(id)
const [
  creator, startBlock, originalEndBlock, currentEndBlock,
  question, metadata, totalOptions, resolved, winningOptions,
  totalCollateral, totalWinningCollateral, totalPot,
  createdAt, lastVoteBlock, inTieExtension, canBeResolved
] = d

console.log({ id: id.toString(), question, totalOptions: Number(totalOptions), resolved })

Tips:

  • Convert BigInt to string before logging or serializing.
  • Use smaller page sizes (25–100) for responsive UIs.
  • Combine counts + pages in your app’s state for SEO-friendly routes.

Navigate:

  • Reference snippets → ./snippets

Back to: