Solidity: Minimal Oracle Interface
Use this minimal interface to integrate your contracts with the DecentralizedOracle. This page focuses on on-chain integration only.
Contract Addresses (BSC Mainnet)
- DecentralizedOracle:
0xFA4595F636887CA28FCA3260486e44fdcc8c8A71 - FarmTruthToken:
0x948d7a6f18511df422184360d1cd5d56f5be4444
See Contract Addresses for complete details and verification links.
Interface
Note: For the complete interface with all functions, events, and detailed documentation, see IDecentralizedOracle Interface Reference.
The minimal interface below includes only the essential functions for basic integration:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
/// @notice Minimal interface for interacting with the oracle on-chain.
interface IOracle {
// Mutations
function createQuery(
uint256 startBlock,
string calldata question,
string calldata metadata,
uint8 totalOptions
) external returns (uint256);
function voteOnQuery(
uint256 queryId,
uint8[] calldata selectedOptions,
uint256 collateral
) external;
function resolveQuery(uint256 queryId) external;
function claimRewards(uint256 queryId) external;
function slashInactiveQuery(uint256 queryId) external;
// Reads
function getQueryDetails(uint256 queryId) external view returns (
address creator,
uint256 startBlock,
uint256 originalEndBlock,
uint256 currentEndBlock,
string memory question,
string memory metadata,
uint8 totalOptions,
bool resolved,
uint8[] memory winningOptions,
uint256 totalCollateral,
uint256 totalWinningCollateral,
uint256 totalPot,
uint256 createdAt,
uint256 lastVoteBlock,
bool inTieExtension,
bool canBeResolved
);
function getQueryAnswers(
uint256 queryId,
uint256 offset,
uint256 limit
) external view returns (
bytes32[] memory answerHashes,
uint8[][] memory decodedAnswers,
uint256[] memory weights
);
function getQueryAnswersCount(uint256 queryId) external view returns (uint256);
function getActiveQueries(uint256 offset, uint256 limit) external view returns (uint256[] memory);
function getActiveQueriesCount() external view returns (uint256);
function getPendingQueries(uint256 offset, uint256 limit) external view returns (uint256[] memory);
function getPendingQueriesCount() external view returns (uint256);
function getPastQueries(uint256 offset, uint256 limit) external view returns (uint256[] memory);
function getPastQueriesCount() external view returns (uint256);
/// @dev 0 = ALL, 1 = PENDING, 2 = ACTIVE, 3 = PAST
function getExtendedQueries(uint256 offset, uint256 limit, uint8 stateFilter) external view returns (uint256[] memory);
function getExtendedQueriesCount(uint8 stateFilter) external view returns (uint256);
function canClaimRewards(uint256 queryId, address user) external view returns (bool);
function getUserVote(uint256 queryId, address user) external view returns (
uint8[] memory selectedOptions,
uint256 collateral,
bool claimed
);
}
Notes:
- The oracle pulls ERC‑20 tokens via
transferFromoncreateQuery(deposit) andvoteOnQuery(collateral). Your contract must hold tokens and approve the oracle before calling these functions. - Time is block-based. Resolving too early reverts. Ties extend the period and revert; retry after the extension.
- Answers are encoded
uint8[](unique option indexes). The winning answer is the highest total collateral.
Typical Usage Patterns
- Approve once: Your contract approves the oracle to pull tokens, then calls
createQuery/voteOnQueryas needed. - Resolve after end: Check
getQueryDetails(queryId); only callresolveQueryifblock.number > currentEndBlock. Expect reverts on ties (extension). - Claim when winning: If your contract voted on the winning answer, call
claimRewards(queryId).
Example Snippets
Approve the oracle to pull tokens from your contract:
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract OracleApprovals {
IOracle public immutable oracle;
IERC20 public immutable token;
constructor(address oracle_, address token_) {
oracle = IOracle(oracle_);
token = IERC20(token_);
}
function approveOracle(uint256 amount) external {
// This contract must already hold the tokens
require(token.approve(address(oracle), amount), "approve failed");
}
}
Create a query:
contract OracleCreate {
IOracle public immutable oracle;
constructor(address oracle_) { oracle = IOracle(oracle_); }
function createSimple(
uint256 startOffset,
string calldata question,
string calldata metadata,
uint8 totalOptions
) external returns (uint256 queryId) {
queryId = oracle.createQuery(block.number + startOffset, question, metadata, totalOptions);
}
}
Vote:
contract OracleVote {
IOracle public immutable oracle;
constructor(address oracle_) { oracle = IOracle(oracle_); }
function voteSingle(uint256 queryId, uint8 option, uint256 collateral) external {
uint8[] memory opts = new uint8[](1);
opts[0] = option;
oracle.voteOnQuery(queryId, opts, collateral);
}
}
Resolve (with try/catch pattern via helper):
contract OracleResolve {
IOracle public immutable oracle;
constructor(address oracle_) { oracle = IOracle(oracle_); }
function tryResolve(uint256 queryId) external returns (bool) {
// May revert if too early or tie (extension)
try this._resolve(queryId) {
return true;
} catch {
return false;
}
}
function _resolve(uint256 queryId) external {
oracle.resolveQuery(queryId);
}
}
Claim:
contract OracleClaim {
IOracle public immutable oracle;
constructor(address oracle_) { oracle = IOracle(oracle_); }
function claimIfWinner(uint256 queryId) external {
if (oracle.canClaimRewards(queryId, address(this))) {
oracle.claimRewards(queryId);
}
}
}
Read distributions and pagination:
// View helper (for off-chain calls)
function readDistribution(IOracle oracle, uint256 queryId) external view returns (bytes32[] memory, uint8[][] memory, uint256[] memory) {
return oracle.getQueryAnswers(queryId, 0, 100);
}
Reference Documentation
- Complete IDecentralizedOracle Interface - Full interface with all functions and events
- IGovernance Interface - Governance contract interface (for reading parameters)
- All Interfaces Overview - Complete interface documentation
Next Pages
Back to: