Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: N03 #68

Merged
merged 3 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions src/UmaCtfAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ contract UmaCtfAdapter is IUmaCtfAdapter, Auth, BulletinBoard, IOptimisticReques
IAddressWhitelist public immutable collateralWhitelist;

/// @notice Time period after which an admin can emergency resolve a condition
uint256 public constant emergencySafetyPeriod = 2 days;
uint256 public constant EMERGENCY_SAFETY_PERIOD = 2 days;

/// @notice Unique query identifier for the Optimistic Oracle
bytes32 public constant yesOrNoIdentifier = "YES_OR_NO_QUERY";
/// From UMIP-107
bytes32 public constant YES_OR_NO_IDENTIFIER = "YES_OR_NO_QUERY";

/// @notice Maximum ancillary data length
uint256 public constant maxAncillaryData = 8139;
/// From OOV2 function OO_ANCILLARY_DATA_LIMIT
uint256 public constant MAX_ANCILLARY_DATA = 8139;

/// @notice Mapping of questionID to QuestionData
mapping(bytes32 => QuestionData) public questions;
Expand Down Expand Up @@ -82,7 +84,7 @@ contract UmaCtfAdapter is IUmaCtfAdapter, Auth, BulletinBoard, IOptimisticReques
if (!collateralWhitelist.isOnWhitelist(rewardToken)) revert UnsupportedToken();

bytes memory data = AncillaryDataLib._appendAncillaryData(msg.sender, ancillaryData);
if (ancillaryData.length == 0 || data.length > maxAncillaryData) revert InvalidAncillaryData();
if (ancillaryData.length == 0 || data.length > MAX_ANCILLARY_DATA) revert InvalidAncillaryData();

questionID = keccak256(data);

Expand Down Expand Up @@ -135,7 +137,7 @@ contract UmaCtfAdapter is IUmaCtfAdapter, Auth, BulletinBoard, IOptimisticReques

// Fetches price from OO
int256 price = optimisticOracle.getRequest(
address(this), yesOrNoIdentifier, questionData.requestTimestamp, questionData.ancillaryData
address(this), YES_OR_NO_IDENTIFIER, questionData.requestTimestamp, questionData.ancillaryData
).resolvedPrice;

return _constructPayouts(price);
Expand Down Expand Up @@ -184,8 +186,9 @@ contract UmaCtfAdapter is IUmaCtfAdapter, Auth, BulletinBoard, IOptimisticReques

if (!_isInitialized(questionData)) revert NotInitialized();
if (_isFlagged(questionData)) revert Flagged();
if (questionData.resolved) revert Resolved();

questionData.emergencyResolutionTimestamp = block.timestamp + emergencySafetyPeriod;
questionData.emergencyResolutionTimestamp = block.timestamp + EMERGENCY_SAFETY_PERIOD;
questionData.paused = true;

emit QuestionFlagged(questionID);
Expand Down Expand Up @@ -225,6 +228,7 @@ contract UmaCtfAdapter is IUmaCtfAdapter, Auth, BulletinBoard, IOptimisticReques
QuestionData storage questionData = questions[questionID];

if (!_isInitialized(questionData)) revert NotInitialized();
if (questionData.resolved) revert Resolved();

questionData.paused = true;
emit QuestionPaused(questionID);
Expand Down Expand Up @@ -308,14 +312,14 @@ contract UmaCtfAdapter is IUmaCtfAdapter, Auth, BulletinBoard, IOptimisticReques
}

// Send a price request to the Optimistic oracle
optimisticOracle.requestPrice(yesOrNoIdentifier, requestTimestamp, ancillaryData, IERC20(rewardToken), reward);
optimisticOracle.requestPrice(YES_OR_NO_IDENTIFIER, requestTimestamp, ancillaryData, IERC20(rewardToken), reward);

// Ensure the price request is event based
optimisticOracle.setEventBased(yesOrNoIdentifier, requestTimestamp, ancillaryData);
optimisticOracle.setEventBased(YES_OR_NO_IDENTIFIER, requestTimestamp, ancillaryData);

// Ensure that the dispute callback flag is set
optimisticOracle.setCallbacks(
yesOrNoIdentifier,
YES_OR_NO_IDENTIFIER,
requestTimestamp,
ancillaryData,
false, // DO NOT set callback on priceProposed
Expand All @@ -324,9 +328,9 @@ contract UmaCtfAdapter is IUmaCtfAdapter, Auth, BulletinBoard, IOptimisticReques
);

// Update the proposal bond on the Optimistic oracle if necessary
if (bond > 0) optimisticOracle.setBond(yesOrNoIdentifier, requestTimestamp, ancillaryData, bond);
if (bond > 0) optimisticOracle.setBond(YES_OR_NO_IDENTIFIER, requestTimestamp, ancillaryData, bond);
if (liveness > 0) {
optimisticOracle.setCustomLiveness(yesOrNoIdentifier, requestTimestamp, ancillaryData, liveness);
optimisticOracle.setCustomLiveness(YES_OR_NO_IDENTIFIER, requestTimestamp, ancillaryData, liveness);
}
}

Expand Down Expand Up @@ -358,7 +362,7 @@ contract UmaCtfAdapter is IUmaCtfAdapter, Auth, BulletinBoard, IOptimisticReques
function _resolve(bytes32 questionID, QuestionData storage questionData) internal {
// Get the price from the OO
int256 price = optimisticOracle.settleAndGetPrice(
yesOrNoIdentifier, questionData.requestTimestamp, questionData.ancillaryData
YES_OR_NO_IDENTIFIER, questionData.requestTimestamp, questionData.ancillaryData
);

// If the OO returns the ignore price, reset the question
Expand All @@ -378,7 +382,7 @@ contract UmaCtfAdapter is IUmaCtfAdapter, Auth, BulletinBoard, IOptimisticReques

function _hasPrice(QuestionData storage questionData) internal view returns (bool) {
return optimisticOracle.hasPrice(
address(this), yesOrNoIdentifier, questionData.requestTimestamp, questionData.ancillaryData
address(this), YES_OR_NO_IDENTIFIER, questionData.requestTimestamp, questionData.ancillaryData
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/libraries/AncillaryDataLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity 0.8.15;

library AncillaryDataLib {
string private constant initializerPrefix = ",initializer:";
string private constant INITIALIZER_PREFIX = ",initializer:";

/// @notice Appends the initializer address to the ancillaryData
/// @param initializer - The initializer address
Expand All @@ -12,7 +12,7 @@ library AncillaryDataLib {
pure
returns (bytes memory)
{
return abi.encodePacked(ancillaryData, initializerPrefix, _toUtf8BytesAddress(initializer));
return abi.encodePacked(ancillaryData, INITIALIZER_PREFIX, _toUtf8BytesAddress(initializer));
}

/// @notice Returns a UTF8-encoded address
Expand Down
39 changes: 35 additions & 4 deletions src/test/UmaCtfAdapter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,21 @@ contract UmaCtfAdapterTest is AdapterHelper {
adapter.unpause(questionID);
}

function testPauseRevertAlreadyResolved() public {
vm.prank(admin);
adapter.initialize(ancillaryData, usdc, 1_000_000, 10_000_000_000, 0);

QuestionData memory data = adapter.getQuestion(questionID);
proposeAndSettle(1 ether, data.requestTimestamp, data.ancillaryData);

adapter.resolve(questionID);

// Pausing an already resolved question reverts
vm.expectRevert(Resolved.selector);
vm.prank(admin);
adapter.pause(questionID);
}

function testReady() public {
// Valid case
vm.prank(admin);
Expand Down Expand Up @@ -433,6 +448,22 @@ contract UmaCtfAdapterTest is AdapterHelper {
adapter.flag(questionID);
}

function testFlagRevertAlreadyResolved() public {
vm.prank(admin);
adapter.initialize(ancillaryData, usdc, 1_000_000, 10_000_000_000, 0);

QuestionData memory data = adapter.getQuestion(questionID);
proposeAndSettle(1 ether, data.requestTimestamp, data.ancillaryData);

// Resolve the question
adapter.resolve(questionID);

// Flag an already resolved question
vm.expectRevert(Resolved.selector);
vm.prank(admin);
adapter.flag(questionID);
}

function testEmergencyResolve() public {
fastForward(100);

Expand All @@ -449,8 +480,8 @@ contract UmaCtfAdapterTest is AdapterHelper {
assertTrue(data.paused);
assertTrue(data.emergencyResolutionTimestamp > 0);

// Fast forward time past the emergencySafetyPeriod
fastForward(adapter.emergencySafetyPeriod());
// Fast forward time past the EMERGENCY_SAFETY_PERIOD
fastForward(adapter.EMERGENCY_SAFETY_PERIOD());

uint256[] memory payouts = new uint256[](2);
payouts[0] = 1;
Expand Down Expand Up @@ -483,8 +514,8 @@ contract UmaCtfAdapterTest is AdapterHelper {
// Flag the question for emergency resolution
adapter.flag(questionID);

// Fast forward time past the emergencySafetyPeriod
fastForward(adapter.emergencySafetyPeriod());
// Fast forward time past the EMERGENCY_SAFETY_PERIOD
fastForward(adapter.EMERGENCY_SAFETY_PERIOD());

uint256[] memory payouts = new uint256[](2);
payouts[0] = 1;
Expand Down