feat: finish up challenges
Some checks failed
Lint / ci (lts/*, ubuntu-latest) (push) Has been cancelled

This commit is contained in:
han
2026-01-26 18:22:19 +07:00
parent b330aba2b4
commit c18c66cca1
22 changed files with 8397 additions and 58 deletions

View File

@@ -141,7 +141,35 @@ contract OptimisticOracle {
string memory description,
uint256 startTime,
uint256 endTime
) external payable returns (uint256) {}
) external payable returns (uint256) {
uint256 assertionId = nextAssertionId;
nextAssertionId++;
if (msg.value == 0) revert InvalidValue();
if (startTime == 0) startTime = block.timestamp;
if (endTime == 0) endTime = block.timestamp + MINIMUM_ASSERTION_WINDOW;
if (startTime < block.timestamp) revert InvalidTime();
if (endTime < startTime + MINIMUM_ASSERTION_WINDOW) revert InvalidTime();
assertions[assertionId] = EventAssertion({
asserter: msg.sender,
proposer: address(0),
disputer: address(0),
proposedOutcome: false,
resolvedOutcome: false,
reward: msg.value,
bond: msg.value * 2,
startTime: startTime,
endTime: endTime,
claimed: false,
winner: address(0),
description: description
});
emit EventAsserted(assertionId, msg.sender, description, msg.value);
return assertionId;
}
/**
* @notice Proposes the outcome (true or false) for an asserted event
@@ -150,7 +178,22 @@ contract OptimisticOracle {
* @param assertionId The unique identifier of the assertion to propose an outcome for
* @param outcome The proposed boolean outcome (true or false) for the event
*/
function proposeOutcome(uint256 assertionId, bool outcome) external payable {}
function proposeOutcome(uint256 assertionId, bool outcome) external payable {
if (assertionId >= nextAssertionId) revert AssertionNotFound();
EventAssertion storage eventAssertion = assertions[assertionId];
if (eventAssertion.proposer != address(0)) revert AssertionProposed();
uint256 currentTime = block.timestamp;
if (currentTime < eventAssertion.startTime || currentTime > eventAssertion.endTime) revert InvalidTime();
if (msg.value != eventAssertion.bond) revert InvalidValue();
eventAssertion.proposer = msg.sender;
eventAssertion.proposedOutcome = outcome;
eventAssertion.endTime = currentTime + DISPUTE_WINDOW;
emit OutcomeProposed(assertionId, msg.sender, outcome);
}
/**
* @notice Disputes a proposed outcome by bonding ETH
@@ -158,7 +201,19 @@ contract OptimisticOracle {
* and must be within the dispute window after proposal.
* @param assertionId The unique identifier of the assertion to dispute
*/
function disputeOutcome(uint256 assertionId) external payable {}
function disputeOutcome(uint256 assertionId) external payable {
EventAssertion storage eventAssertion = assertions[assertionId];
if (eventAssertion.asserter == address(0)) revert AssertionNotFound();
if (eventAssertion.proposer == address(0)) revert NotProposedAssertion();
if (eventAssertion.disputer != address(0)) revert ProposalDisputed();
if (block.timestamp > eventAssertion.endTime) revert InvalidTime();
if (msg.value != eventAssertion.bond) revert InvalidValue();
eventAssertion.disputer = msg.sender;
emit OutcomeDisputed(assertionId, msg.sender);
}
/**
* @notice Claims reward for undisputed assertions after dispute window expires
@@ -166,7 +221,22 @@ contract OptimisticOracle {
* Can only be called after dispute window has passed without disputes.
* @param assertionId The unique identifier of the assertion to claim rewards for
*/
function claimUndisputedReward(uint256 assertionId) external {}
function claimUndisputedReward(uint256 assertionId) external {
EventAssertion storage assertion = assertions[assertionId];
if (assertion.proposer == address(0)) revert NotProposedAssertion();
if (assertion.disputer != address(0)) revert ProposalDisputed();
if (block.timestamp <= assertion.endTime) revert InvalidTime();
if (assertion.claimed) revert AlreadyClaimed();
assertion.claimed = true;
assertion.resolvedOutcome = assertion.proposedOutcome;
assertion.winner = assertion.proposer;
uint256 totalValue = assertion.reward + assertion.bond;
(bool success,) = payable(assertion.proposer).call{value: totalValue}("");
if (!success) revert TransferFailed();
emit RewardClaimed(assertionId, assertion.proposer, totalValue);
}
/**
* @notice Claims reward for disputed assertions after decider settlement
@@ -174,7 +244,23 @@ contract OptimisticOracle {
* Can only be called after decider has settled the dispute.
* @param assertionId The unique identifier of the disputed assertion to claim rewards for
*/
function claimDisputedReward(uint256 assertionId) external {}
function claimDisputedReward(uint256 assertionId) external {
EventAssertion storage assertion = assertions[assertionId];
if (assertion.proposer == address(0)) revert NotProposedAssertion();
if (assertion.disputer == address(0)) revert NotDisputedAssertion();
if (assertion.winner == address(0)) revert AwaitingDecider();
if (assertion.claimed) revert AlreadyClaimed();
assertion.claimed = true;
(bool successDecider,) = payable(decider).call{value: assertion.bond}("");
if (!successDecider) revert TransferFailed();
uint256 totalValue = assertion.reward + assertion.bond;
(bool success,) = payable(assertion.winner).call{value: totalValue}("");
if (!success) revert TransferFailed();
emit RewardClaimed(assertionId, assertion.winner, totalValue);
}
/**
* @notice Claims refund for assertions that receive no proposals before deadline
@@ -182,7 +268,20 @@ contract OptimisticOracle {
* Can only be called after assertion deadline has passed without any proposals.
* @param assertionId The unique identifier of the expired assertion to claim refund for
*/
function claimRefund(uint256 assertionId) external {}
function claimRefund(uint256 assertionId) external {
EventAssertion storage assertion = assertions[assertionId];
if (assertion.asserter == address(0)) revert AssertionNotFound();
if (assertion.proposer != address(0)) revert AssertionProposed();
if (block.timestamp <= assertion.endTime) revert InvalidTime();
if (assertion.claimed) revert AlreadyClaimed();
assertion.claimed = true;
uint256 totalValue = assertion.reward;
(bool success,) = payable(assertion.asserter).call{value: totalValue}("");
if (!success) revert TransferFailed();
emit RefundClaimed(assertionId, assertion.asserter, totalValue);
}
/**
* @notice Resolves disputed assertions by determining the correct outcome (only decider)
@@ -190,7 +289,17 @@ contract OptimisticOracle {
* @param assertionId The unique identifier of the disputed assertion to settle
* @param resolvedOutcome The decider's determination of the true outcome
*/
function settleAssertion(uint256 assertionId, bool resolvedOutcome) external onlyDecider {}
function settleAssertion(uint256 assertionId, bool resolvedOutcome) external onlyDecider {
EventAssertion storage assertion = assertions[assertionId];
if (assertion.proposer == address(0)) revert NotProposedAssertion();
if (assertion.disputer == address(0)) revert NotDisputedAssertion();
if (assertion.winner != address(0)) revert AlreadySettled();
assertion.resolvedOutcome = resolvedOutcome;
assertion.winner = assertion.proposedOutcome == resolvedOutcome ? assertion.proposer : assertion.disputer;
emit AssertionSettled(assertionId, resolvedOutcome, assertion.winner);
}
/**
* @notice Returns the current state of an assertion based on its lifecycle stage
@@ -198,7 +307,19 @@ contract OptimisticOracle {
* @param assertionId The unique identifier of the assertion to check state for
* @return The current State enum value representing the assertion's status
*/
function getState(uint256 assertionId) external view returns (State) {}
function getState(uint256 assertionId) external view returns (State) {
EventAssertion memory assertion = assertions[assertionId];
if (assertion.asserter == address(0)) return State.Invalid;
if (assertion.winner != address(0)) return State.Settled;
if (assertion.disputer != address(0)) return State.Disputed;
if (assertion.proposer != address(0)) {
if (block.timestamp >= assertion.endTime) return State.Settled;
return State.Proposed;
}
if (block.timestamp >= assertion.endTime) return State.Expired;
return State.Asserted;
}
/**
* @notice Returns the final resolved outcome of a settled assertion
@@ -207,5 +328,18 @@ contract OptimisticOracle {
* @param assertionId The unique identifier of the assertion to get resolution for
* @return The final boolean outcome of the assertion
*/
function getResolution(uint256 assertionId) external view returns (bool) {}
function getResolution(uint256 assertionId) external view returns (bool) {
EventAssertion memory assertion = assertions[assertionId];
if (assertion.asserter == address(0)) revert AssertionNotFound();
if (assertion.proposer == address(0)) revert NotProposedAssertion();
if (assertion.winner != address(0)) return assertion.resolvedOutcome;
if (assertion.disputer == address(0)) {
if (block.timestamp <= assertion.endTime) revert InvalidTime();
return assertion.proposedOutcome;
}
revert AwaitingDecider();
}
}