JS: Reference Snippets
Copy-paste friendly snippets for common tasks when integrating with the DecentralizedOracle using ethers v6.
Note:
- Oracle (BSC Mainnet):
0xFA4595F636887CA28FCA3260486e44fdcc8c8A71 - Token (BSC Mainnet):
0x948d7a6f18511df422184360d1cd5d56f5be4444 - Import the oracle ABI from artifacts/contracts/FarmTruth.sol/DecentralizedOracle.json.
Instantiate (provider or signer)
import { JsonRpcProvider, BrowserProvider, Contract } from 'ethers'
import OracleArtifact from '../../../artifacts/contracts/FarmTruth.sol/DecentralizedOracle.json'
const ORACLE_ADDRESS = '0xFA4595F636887CA28FCA3260486e44fdcc8c8A71'
// Provider (read-only)
const provider = new JsonRpcProvider(process.env.RPC!)
const oracleRead = new Contract(ORACLE_ADDRESS, (OracleArtifact as any).abi, provider)
// Signer (write tx)
const browser = new BrowserProvider((globalThis as any).ethereum)
const signer = await browser.getSigner()
const oracle = new Contract(ORACLE_ADDRESS, (OracleArtifact as any).abi, signer)
Code presence check
const code = await provider.getCode(ORACLE_ADDRESS)
if (!code || code === '0x') throw new Error('No bytecode at ORACLE_ADDRESS')
Create query
const currentBlock = await signer.provider!.getBlockNumber()
const startBlock = BigInt(currentBlock + 20)
const question = 'Is block.number increasing?'
const metadata = JSON.stringify({ ref: 'snippet-create' })
const totalOptions = 2
await (await oracle.createQuery(startBlock, question, metadata, totalOptions)).wait()
Vote
const queryId = 0n
const selectedOptions = [1]
const collateral = 10n * 10n ** 18n
await (await oracle.voteOnQuery(queryId, selectedOptions, collateral)).wait()
Resolve (with try/catch at callsite)
try {
await (await oracle.resolveQuery(queryId)).wait()
} catch (e) {
// either too early or tie; if tie, voting period extended
}
Claim rewards
const me = await signer.getAddress()
if (await oracle.canClaimRewards(queryId, me)) {
await (await oracle.claimRewards(queryId)).wait()
}
Read details
const d = await oracleRead.getQueryDetails(queryId)
const [
creator, startBlock, originalEndBlock, currentEndBlock,
question, metadata, totalOptions, resolved, winningOptions,
totalCollateral, totalWinningCollateral, totalPot,
createdAt, lastVoteBlock, inTieExtension, canBeResolved
] = d
List queries (active)
const count = await oracleRead.getActiveQueriesCount()
const ids = await oracleRead.getActiveQueries(0, (count > 50n ? 50n : count))
Answers distribution page
const [hashes, decoded, weights] = await oracleRead.getQueryAnswers(queryId, 0, 100n)
decoded.forEach((opts, i) => {
console.log('opts:', opts.map(x => x.toString()), 'weight:', weights[i].toString())
})
Expected reward for address
const me = await signer.getAddress()
const expected = await oracleRead.getExpectedReward(queryId, me)
console.log(expected.toString())
Tips:
- Treat uint256 as BigInt in JS (use 10n ** 18n for decimals).
- Always check
block.number > currentEndBlockbefore callingresolveQuery. - On tie reverts, re-read details and retry after extension.