Solidity: Create a Query
Create a new oracle query by scheduling a start block, providing the question and metadata, and selecting the number of options. This page covers on-chain usage only (no deployment, governance, or meta info).
Requirements:
- Your contract holds the governance token and has approved the oracle to pull the creation deposit (see ../solidity/approvals).
startBlock >= block.numbertotalOptionsin[2..255]
Minimal interfaces
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
interface IOracleCreate {
function createQuery(
uint256 startBlock,
string calldata question,
string calldata metadata,
uint8 totalOptions
) external returns (uint256);
}
Single-purpose creator
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
interface IOracleCreate {
function createQuery(uint256, string calldata, string calldata, uint8) external returns (uint256);
}
contract OracleCreate {
IOracleCreate public immutable oracle;
constructor(address oracle_) {
oracle = IOracleCreate(oracle_);
}
/// @notice Open a query scheduled to start after `startOffsetBlocks` from now.
/// @dev Assumes this contract already approved the oracle to pull the deposit.
function openQuery(
uint256 startOffsetBlocks,
string calldata question,
string calldata metadata,
uint8 totalOptions
) external returns (uint256 queryId) {
uint256 startBlock = block.number + startOffsetBlocks;
queryId = oracle.createQuery(startBlock, question, metadata, totalOptions);
}
}
Notes:
- The oracle pulls the fixed creation deposit from
msg.senderusingtransferFrom. Ensure you approved the oracle and the contract holds enough tokens. - The query remains “pending” until the first vote arrives, then moves to “active”.
Metadata tips
Use a small JSON string in metadata to help indexers/UIs:
// Example metadata string (JSON)
// {"topic":"markets","tag":"demo-1","link":"https://example.com/context"}
Keep it short to avoid excessive gas.
Common pitfalls
- Missing allowance: call token
approve(oracle, amount)first. startBlockin the past: computeblock.number + offset.- Invalid
totalOptions: must be within[2..255]. - Overly long
question/metadata: impacts gas and may exceed internal gas limits for complex transactions.
Related pages
Back to: