JS: Resolve a Query

Resolve a query after the voting period ends. The winning answer is the one with the highest total collateral. If there is a tie at the top, the contract extends currentEndBlock and reverts; resolve again after the extension.

Requirements:

  • block.number > currentEndBlock
  • The query must have a clear single highest-weight answer (no tie)

Readiness checks

Use getQueryDetails(queryId) to inspect timing and a helper flag.

import { Contract, JsonRpcProvider } 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)

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

console.log({
  currentEndBlock: currentEndBlock.toString(),
  inTieExtension,
  canBeResolved
})
  • canBeResolved provides a hint that a clear winner exists once the window is over.
  • If inTieExtension is true, wait until after the extended end block.

Resolve with a signer

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

const browser = new BrowserProvider((globalThis as any).ethereum)
const signer = await browser.getSigner()

const ORACLE_ADDRESS = '0xFA4595F636887CA28FCA3260486e44fdcc8c8A71'
const oracle = new Contract(ORACLE_ADDRESS, (OracleArtifact as any).abi, signer)

const queryId = 0n

try {
  const tx = await oracle.resolveQuery(queryId)
  await tx.wait()
  console.log('Query resolved')
} catch (e) {
  // Will revert if:
  // - block.number <= currentEndBlock
  // - top answers are tied (tie triggers extension and reverts)
  console.error('Resolve failed, likely tie or not ended yet', e)
}

Tie handling and extensions

  • On a tie, the contract sets currentEndBlock = block.number + tieExtensionPeriod() and reverts.
  • Re-check getQueryDetails(queryId) and watch for inTieExtension to flip back when the tie breaks (or after additional voting).
  • After the new currentEndBlock, call resolveQuery again.

Post-resolution data

const d = await oracle.getQueryDetails(queryId)
const resolved = d[7]
const winningOptions = d[8]
const totalPot = d[11]

console.log({ resolved, winningOptions, totalPot: totalPot.toString() })
  • winningOptions is the decoded uint8[] of the winning answer.
  • totalPot = initialDeposit + (totalCollateral - totalWinningCollateral)

Navigate next:

  • Claim rewards → ./claim
  • Ties, extensions, slashing → ./ties-slashing
  • Pagination and listing → ./pagination

Back to: