JS: Create a Query
Create a new oracle query by scheduling a start block, setting the question and metadata, and choosing the number of options. Requires prior ERC-20 approval to cover the creation deposit.
Requirements
- ERC-20 allowance to the oracle for at least the creation deposit
- totalOptions in 2..255
- startBlock >= current block
Example (browser 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 currentBlock = await signer.provider!.getBlockNumber()
const startBlock = BigInt(currentBlock + 20) // schedule in the near future
const question = 'Is block.number increasing?'
const metadata = JSON.stringify({ ref: 'docs-create-1' })
const totalOptions = 2
const tx = await oracle.createQuery(startBlock, question, metadata, totalOptions)
await tx.wait()
Notes:
- A query is pending until first vote; then it moves to active.
- Use human-readable metadata JSON to help off-chain UIs (e.g., tags, category, links).
Example (server signer)
import { Wallet, JsonRpcProvider, Contract } from 'ethers'
import OracleArtifact from '../../../artifacts/contracts/FarmTruth.sol/DecentralizedOracle.json'
const provider = new JsonRpcProvider(process.env.RPC!)
const wallet = new Wallet(process.env.PRIVATE_KEY!, provider)
const ORACLE_ADDRESS = '0xFA4595F636887CA28FCA3260486e44fdcc8c8A71'
const oracle = new Contract(ORACLE_ADDRESS, (OracleArtifact as any).abi, wallet)
const currentBlock = await provider.getBlockNumber()
const startBlock = BigInt(currentBlock + 40)
const question = 'Will the oracle resolve without a tie on first attempt?'
const metadata = JSON.stringify({ severity: 'low', app: 'my-backend' })
const totalOptions = 2
const tx = await oracle.createQuery(startBlock, question, metadata, totalOptions)
await tx.wait()
Common pitfalls
- Insufficient allowance: approve the oracle to pull the deposit before calling createQuery.
- startBlock in the past: always read the current block and add an offset.
- totalOptions out of range: must be 2–255 inclusive.
Navigate next:
- Vote on a query → ./vote
- Resolve a query → ./resolve
- Claim rewards → ./claim
Back to:
- JS Setup → ./setup
- Documentation Home → ../../README.md