// SPDX-License-Identifier: MIT pragma solidity 0.8.24; import "./ITrinityOracleV2.sol"; /// Scheduled raffle example: freeze entrants BEFORE requesting; poll/read, not a VRF callback. /// requestHash identifies fixed source terms; entrants/rules remain committed in this contract's state. /// No prize custody. Production applications must define challenge and refund recovery policies. contract ExampleRaffleConsumer { bytes32 public constant SOURCE_TERMS_HASH = 0x3d823e84110f9acee2dc0c0108d24732c8817cff92d838b32ac7e61bcd02f083; ITrinityOracleV2 public immutable oracle; address public immutable owner = msg.sender; bytes32 public requestId; uint256 public sequence; address[] public entrants; address public winner; /// 60 s draw + 15 min delivery window + buffer. A shorter deadline can leave the draw refunded with no result. uint64 public constant DELIVERY_WINDOW = 17 minutes; constructor(address oracle_) { oracle = ITrinityOracleV2(oracle_); } modifier onlyOwner() { require(msg.sender == owner, "owner only"); _; } function fundCredit() external payable onlyOwner { oracle.depositEth{value: msg.value}(); } function startRaffle(address[] calldata frozenEntrants) external onlyOwner { require(requestId == 0 && frozenEntrants.length > 0, "already started/empty"); entrants = frozenEntrants; requestId = oracle.requestFromCredit(0, keccak256(abi.encode(address(this), ++sequence)), SOURCE_TERMS_HASH, uint64(block.timestamp) + DELIVERY_WINDOW); } function finishRaffle() external { require(requestId != 0 && winner == address(0), "not pending"); (,,, bytes32 resultHash,,,,, uint64 challengeUntil,, ITrinityOracleV2.State state) = oracle.requests(requestId); require((state == ITrinityOracleV2.State.Delivered || state == ITrinityOracleV2.State.RevenuePaid) && block.timestamp > challengeUntil, "result not final"); winner = entrants[uint256(resultHash) % entrants.length]; } /// Refunds belong to this contract (the payer), not the owner EOA. function recoverExpiredRequest() external onlyOwner { oracle.claimRefund(requestId); } function recoverCredit(uint8 asset, uint256 amount) external onlyOwner { oracle.withdrawCredit(asset, amount); } function withdrawEth() external onlyOwner { (bool ok,) = payable(owner).call{value: address(this).balance}(""); require(ok, "transfer"); } receive() external payable {} }