JS: Claim Rewards
After a query is resolved, addresses that voted for the winning answer can claim their proportional rewards from the pot.
Pot formula:
totalPot = initialDeposit + (totalCollateral - totalWinningCollateral)
Payout for a winner:
userShare = (userCollateral * totalPot) / totalWinningCollateral
Requirements:
- Query must be resolved
- Caller must have a winning vote that is not yet claimed
Check eligibility
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
const eligible = await oracle.canClaimRewards(queryId, await signer.getAddress())
console.log('Can claim?', eligible)
Optional: estimate expected reward:
const expected = await oracle.getExpectedReward(queryId, await signer.getAddress())
console.log('Expected reward:', expected.toString())
Claim with a signer
if (await oracle.canClaimRewards(queryId, await signer.getAddress())) {
const tx = await oracle.claimRewards(queryId)
await tx.wait()
console.log('Rewards claimed')
}
Reading post-resolution details
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()
})
Common pitfalls:
- Calling before resolution: claims will revert
- Non-winner attempting to claim: reverts
- Double claim from the same address: reverts
Navigate:
- Ties, extensions, slashing → ./ties-slashing
- Pagination and listing → ./pagination
Back to: