// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (access/AccessControl.sol)
pragma solidity ^0.8.20;
import {IAccessControl} from "./IAccessControl.sol";
import {Context} from "../utils/Context.sol";
import {ERC165} from "../utils/introspection/ERC165.sol";
/**
* @dev Contract module that allows children to implement role-based access
* control mechanisms. This is a lightweight version that doesn't allow enumerating role
* members except through off-chain means by accessing the contract event logs. Some
* applications may benefit from on-chain enumerability, for those cases see
* {AccessControlEnumerable}.
*
* Roles are referred to by their `bytes32` identifier. These should be exposed
* in the external API and be unique. The best way to achieve this is by
* using `public constant` hash digests:
*
* ```solidity
* bytes32 public constant MY_ROLE = keccak256("MY_ROLE");
* ```
*
* Roles can be used to represent a set of permissions. To restrict access to a
* function call, use {hasRole}:
*
* ```solidity
* function foo() public {
* require(hasRole(MY_ROLE, msg.sender));
* ...
* }
* ```
*
* Roles can be granted and revoked dynamically via the {grantRole} and
* {revokeRole} functions. Each role has an associated admin role, and only
* accounts that have a role's admin role can call {grantRole} and {revokeRole}.
*
* By default, the admin role for all roles is `DEFAULT_ADMIN_ROLE`, which means
* that only accounts with this role will be able to grant or revoke other
* roles. More complex role relationships can be created by using
* {_setRoleAdmin}.
*
* WARNING: The `DEFAULT_ADMIN_ROLE` is also its own admin: it has permission to
* grant and revoke this role. Extra precautions should be taken to secure
* accounts that have been granted it. We recommend using {AccessControlDefaultAdminRules}
* to enforce additional security measures for this role.
*/
abstract contract AccessControl is Context, IAccessControl, ERC165 {
struct RoleData {
mapping(address account => bool) hasRole;
bytes32 adminRole;
}
mapping(bytes32 role => RoleData) private _roles;
bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00;
/**
* @dev Modifier that checks that an account has a specific role. Reverts
* with an {AccessControlUnauthorizedAccount} error including the required role.
*/
modifier onlyRole(bytes32 role) {
_checkRole(role);
_;
}
/**
* @dev See {IERC165-supportsInterface}.
*/
function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
return interfaceId == type(IAccessControl).interfaceId || super.supportsInterface(interfaceId);
}
/**
* @dev Returns `true` if `account` has been granted `role`.
*/
function hasRole(bytes32 role, address account) public view virtual returns (bool) {
return _roles[role].hasRole[account];
}
/**
* @dev Reverts with an {AccessControlUnauthorizedAccount} error if `_msgSender()`
* is missing `role`. Overriding this function changes the behavior of the {onlyRole} modifier.
*/
function _checkRole(bytes32 role) internal view virtual {
_checkRole(role, _msgSender());
}
/**
* @dev Reverts with an {AccessControlUnauthorizedAccount} error if `account`
* is missing `role`.
*/
function _checkRole(bytes32 role, address account) internal view virtual {
if (!hasRole(role, account)) {
revert AccessControlUnauthorizedAccount(account, role);
}
}
/**
* @dev Returns the admin role that controls `role`. See {grantRole} and
* {revokeRole}.
*
* To change a role's admin, use {_setRoleAdmin}.
*/
function getRoleAdmin(bytes32 role) public view virtual returns (bytes32) {
return _roles[role].adminRole;
}
/**
* @dev Grants `role` to `account`.
*
* If `account` had not been already granted `role`, emits a {RoleGranted}
* event.
*
* Requirements:
*
* - the caller must have ``role``'s admin role.
*
* May emit a {RoleGranted} event.
*/
function grantRole(bytes32 role, address account) public virtual onlyRole(getRoleAdmin(role)) {
_grantRole(role, account);
}
/**
* @dev Revokes `role` from `account`.
*
* If `account` had been granted `role`, emits a {RoleRevoked} event.
*
* Requirements:
*
* - the caller must have ``role``'s admin role.
*
* May emit a {RoleRevoked} event.
*/
function revokeRole(bytes32 role, address account) public virtual onlyRole(getRoleAdmin(role)) {
_revokeRole(role, account);
}
/**
* @dev Revokes `role` from the calling account.
*
* Roles are often managed via {grantRole} and {revokeRole}: this function's
* purpose is to provide a mechanism for accounts to lose their privileges
* if they are compromised (such as when a trusted device is misplaced).
*
* If the calling account had been revoked `role`, emits a {RoleRevoked}
* event.
*
* Requirements:
*
* - the caller must be `callerConfirmation`.
*
* May emit a {RoleRevoked} event.
*/
function renounceRole(bytes32 role, address callerConfirmation) public virtual {
if (callerConfirmation != _msgSender()) {
revert AccessControlBadConfirmation();
}
_revokeRole(role, callerConfirmation);
}
/**
* @dev Sets `adminRole` as ``role``'s admin role.
*
* Emits a {RoleAdminChanged} event.
*/
function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual {
bytes32 previousAdminRole = getRoleAdmin(role);
_roles[role].adminRole = adminRole;
emit RoleAdminChanged(role, previousAdminRole, adminRole);
}
/**
* @dev Attempts to grant `role` to `account` and returns a boolean indicating if `role` was granted.
*
* Internal function without access restriction.
*
* May emit a {RoleGranted} event.
*/
function _grantRole(bytes32 role, address account) internal virtual returns (bool) {
if (!hasRole(role, account)) {
_roles[role].hasRole[account] = true;
emit RoleGranted(role, account, _msgSender());
return true;
} else {
return false;
}
}
/**
* @dev Attempts to revoke `role` to `account` and returns a boolean indicating if `role` was revoked.
*
* Internal function without access restriction.
*
* May emit a {RoleRevoked} event.
*/
function _revokeRole(bytes32 role, address account) internal virtual returns (bool) {
if (hasRole(role, account)) {
_roles[role].hasRole[account] = false;
emit RoleRevoked(role, account, _msgSender());
return true;
} else {
return false;
}
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.1) (utils/Context.sol)
pragma solidity ^0.8.20;
/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
function _contextSuffixLength() internal view virtual returns (uint256) {
return 0;
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/introspection/ERC165.sol)
pragma solidity ^0.8.20;
import {IERC165} from "./IERC165.sol";
/**
* @dev Implementation of the {IERC165} interface.
*
* Contracts that want to implement ERC165 should inherit from this contract and override {supportsInterface} to check
* for the additional interface id that will be supported. For example:
*
* ```solidity
* function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
* return interfaceId == type(MyInterface).interfaceId || super.supportsInterface(interfaceId);
* }
* ```
*/
abstract contract ERC165 is IERC165 {
/**
* @dev See {IERC165-supportsInterface}.
*/
function supportsInterface(bytes4 interfaceId) public view virtual returns (bool) {
return interfaceId == type(IERC165).interfaceId;
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (access/IAccessControl.sol)
pragma solidity ^0.8.20;
/**
* @dev External interface of AccessControl declared to support ERC165 detection.
*/
interface IAccessControl {
/**
* @dev The `account` is missing a role.
*/
error AccessControlUnauthorizedAccount(address account, bytes32 neededRole);
/**
* @dev The caller of a function is not the expected one.
*
* NOTE: Don't confuse with {AccessControlUnauthorizedAccount}.
*/
error AccessControlBadConfirmation();
/**
* @dev Emitted when `newAdminRole` is set as ``role``'s admin role, replacing `previousAdminRole`
*
* `DEFAULT_ADMIN_ROLE` is the starting admin for all roles, despite
* {RoleAdminChanged} not being emitted signaling this.
*/
event RoleAdminChanged(bytes32 indexed role, bytes32 indexed previousAdminRole, bytes32 indexed newAdminRole);
/**
* @dev Emitted when `account` is granted `role`.
*
* `sender` is the account that originated the contract call, an admin role
* bearer except when using {AccessControl-_setupRole}.
*/
event RoleGranted(bytes32 indexed role, address indexed account, address indexed sender);
/**
* @dev Emitted when `account` is revoked `role`.
*
* `sender` is the account that originated the contract call:
* - if using `revokeRole`, it is the admin role bearer
* - if using `renounceRole`, it is the role bearer (i.e. `account`)
*/
event RoleRevoked(bytes32 indexed role, address indexed account, address indexed sender);
/**
* @dev Returns `true` if `account` has been granted `role`.
*/
function hasRole(bytes32 role, address account) external view returns (bool);
/**
* @dev Returns the admin role that controls `role`. See {grantRole} and
* {revokeRole}.
*
* To change a role's admin, use {AccessControl-_setRoleAdmin}.
*/
function getRoleAdmin(bytes32 role) external view returns (bytes32);
/**
* @dev Grants `role` to `account`.
*
* If `account` had not been already granted `role`, emits a {RoleGranted}
* event.
*
* Requirements:
*
* - the caller must have ``role``'s admin role.
*/
function grantRole(bytes32 role, address account) external;
/**
* @dev Revokes `role` from `account`.
*
* If `account` had been granted `role`, emits a {RoleRevoked} event.
*
* Requirements:
*
* - the caller must have ``role``'s admin role.
*/
function revokeRole(bytes32 role, address account) external;
/**
* @dev Revokes `role` from the calling account.
*
* Roles are often managed via {grantRole} and {revokeRole}: this function's
* purpose is to provide a mechanism for accounts to lose their privileges
* if they are compromised (such as when a trusted device is misplaced).
*
* If the calling account had been granted `role`, emits a {RoleRevoked}
* event.
*
* Requirements:
*
* - the caller must be `callerConfirmation`.
*/
function renounceRole(bytes32 role, address callerConfirmation) external;
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
interface IDataStreamsVerifier {
function lastRetrievedPrice() external view returns (int192);
function getPrice() external view returns (int192);
function verifyReportWithTimestamp(
bytes memory unverifiedReport,
uint8 feedNumber
) external returns (int192, uint32);
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/introspection/IERC165.sol)
pragma solidity ^0.8.20;
/**
* @dev Interface of the ERC165 standard, as defined in the
* https://eips.ethereum.org/EIPS/eip-165[EIP].
*
* Implementers can declare support of contract interfaces, which can then be
* queried by others ({ERC165Checker}).
*
* For an implementation, see {ERC165}.
*/
interface IERC165 {
/**
* @dev Returns true if this contract implements the interface defined by
* `interfaceId`. See the corresponding
* https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
* to learn more about how these ids are created.
*
* This function call must use less than 30 000 gas.
*/
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
interface ITreasury {
struct PermitData {
uint256 deadline;
uint8 v;
bytes32 r;
bytes32 s;
}
function DISTRIBUTOR_ROLE() external view returns (bytes32);
function grantRole(bytes32 role, address account) external;
function increaseFee(uint256 amount) external;
function depositAndLock(uint256 amount, address from) external;
function depositAndLockWithPermit(
uint256 amount,
address from,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external;
function lock(uint256 amount, address from) external;
function upkeep() external view returns (address);
function distribute(uint256 amount, address to, uint256 gameFee) external;
function distributeBullseye(
uint256 amount,
address to,
uint256 gameFee
) external;
function approvedToken() external returns (address);
function refund(uint256 amount, address to) external;
function refundWithFees(
uint256 amount,
address to,
uint256 refundFee
) external;
function distributeWithoutFee(
uint256 rate,
address to,
uint256 usedFee,
uint256 initialDeposit
) external;
function calculateSetupRate(
uint256 lostTeamTotal,
uint256 wonTeamTotal,
uint256 setupFee,
address initiator
) external returns (uint256, uint256);
function calculateUpDownRate(
uint256 lostTeamTotal,
uint256 wonTeamTotal,
uint256 updownFee
) external returns (uint256 rate);
}
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;
import {AccessControl} from "@openzeppelin/contracts/access/AccessControl.sol";
import {ITreasury} from "./interfaces/ITreasury.sol";
import {IDataStreamsVerifier} from "./interfaces/IDataStreamsVerifier.sol";
contract OneVsOneExactPrice is AccessControl {
event NewTreasury(address newTreasury);
event ExactPriceCreated(
bytes32 gameId,
uint8 feedNumber,
address opponent,
uint32 startTime,
uint32 endTime,
address initiator,
uint32 initiatorPrice,
uint32 depositAmount
);
event ExactPriceAccepted(
bytes32 gameId,
address opponent,
uint32 opponentPrice
);
event ExactPriceCancelled(bytes32 gameId);
event ExactPriceFinalized(
bytes32 gameId,
uint256 winnerGuessPrice,
uint256 loserGuessPrice,
int192 finalPrice,
Status gameStatus
);
enum Status {
Default,
Created,
Cancelled,
Started,
Finished
}
struct GameInfo {
uint8 feedNumber;
address initiator;
uint256 startTime;
uint256 endTime;
address opponent;
uint256 depositAmount;
uint256 initiatorPrice;
uint256 opponentPrice;
uint256 finalPrice;
Status gameStatus;
}
struct GameInfoPacked {
uint256 packedData;
uint256 packedData2;
}
bytes32 public constant GAME_MASTER_ROLE = keccak256("GAME_MASTER_ROLE");
mapping(bytes32 => GameInfoPacked) public games;
address public treasury;
uint256 public fee = 500;
uint256 public refundFee = 1000;
uint256 public minDuration = 280;
uint256 public maxDuration = 4 weeks;
constructor() {
_grantRole(DEFAULT_ADMIN_ROLE, msg.sender);
}
/**
* Creates 1vs1 exact price mode game and deposit funds
* @param opponent address of the opponent
* @param endTime when the game will end
* @param initiatorPrice game initiator picked asset price
* @param depositAmount amount to enter the game
*/
function createGame(
uint8 feedNumber,
address opponent,
uint32 endTime,
uint32 initiatorPrice,
uint16 depositAmount
) public {
require(opponent != msg.sender, "Wrong opponent");
require(
endTime - block.timestamp >= minDuration,
"Min game duration must be higher"
);
require(
endTime - block.timestamp <= maxDuration,
"Max game duration must be lower"
);
ITreasury(treasury).depositAndLock(depositAmount, msg.sender);
bytes32 gameId = keccak256(
abi.encodePacked(endTime, block.timestamp, msg.sender, opponent)
);
require(games[gameId].packedData == 0, "Game exists");
uint256 packedData = uint(uint160(opponent));
uint256 packedData2 = uint(uint160(msg.sender));
packedData |= uint256(endTime) << 160;
packedData |= uint256(initiatorPrice) << 192;
packedData2 |= block.timestamp << 160;
packedData2 |= uint256(depositAmount) << 192;
packedData2 |= uint256(Status.Created) << 208;
packedData2 |= uint256(feedNumber) << 216;
games[gameId].packedData = packedData;
games[gameId].packedData2 = packedData2;
emit ExactPriceCreated(
gameId,
feedNumber,
opponent,
uint32(block.timestamp),
endTime,
msg.sender,
initiatorPrice,
depositAmount
);
}
/**
* Creates 1vs1 exact price mode game with deposited funds
* @param opponent address of the opponent
* @param endTime when the game will end
* @param initiatorPrice game initiator picked asset price
* @param depositAmount amount to enter the game
*/
function createGameWithDeposit(
uint8 feedNumber,
address opponent,
uint32 endTime,
uint32 initiatorPrice,
uint16 depositAmount
) public {
require(opponent != msg.sender, "Wrong opponent");
require(
endTime - block.timestamp >= minDuration,
"Min game duration must be higher"
);
require(
endTime - block.timestamp <= maxDuration,
"Max game duration must be lower"
);
ITreasury(treasury).lock(depositAmount, msg.sender);
bytes32 gameId = keccak256(
abi.encodePacked(endTime, block.timestamp, msg.sender, opponent)
);
require(games[gameId].packedData == 0, "Game exists");
uint256 packedData = uint(uint160(opponent));
uint256 packedData2 = uint(uint160(msg.sender));
packedData |= uint256(endTime) << 160;
packedData |= uint256(initiatorPrice) << 192;
packedData2 |= block.timestamp << 160;
packedData2 |= uint256(depositAmount) << 192;
packedData2 |= uint256(Status.Created) << 208;
packedData2 |= uint256(feedNumber) << 216;
games[gameId].packedData = packedData;
games[gameId].packedData2 = packedData2;
emit ExactPriceCreated(
gameId,
feedNumber,
opponent,
uint32(block.timestamp),
endTime,
msg.sender,
initiatorPrice,
depositAmount
);
}
/**
* Creates 1vs1 exact price mode game and deposit funds
* @param opponent address of the opponent
* @param endTime when the game will end
* @param initiatorPrice game initiator picked asset price
* @param depositAmount amount to enter the game
*/
function createGameWithPermit(
uint8 feedNumber,
address opponent,
uint32 endTime,
uint32 initiatorPrice,
uint16 depositAmount,
ITreasury.PermitData calldata permitData
) public {
require(opponent != msg.sender, "Wrong opponent");
require(
endTime - block.timestamp >= minDuration,
"Min game duration must be higher"
);
require(
endTime - block.timestamp <= maxDuration,
"Max game duration must be lower"
);
ITreasury(treasury).depositAndLockWithPermit(
depositAmount,
msg.sender,
permitData.deadline,
permitData.v,
permitData.r,
permitData.s
);
bytes32 gameId = keccak256(
abi.encodePacked(endTime, block.timestamp, msg.sender, opponent)
);
uint256 packedData = uint(uint160(opponent));
uint256 packedData2 = uint(uint160(msg.sender));
packedData |= uint256(endTime) << 160;
packedData |= uint256(initiatorPrice) << 192;
packedData2 |= block.timestamp << 160;
packedData2 |= uint256(depositAmount) << 192;
packedData2 |= uint256(Status.Created) << 208;
packedData2 |= uint256(feedNumber) << 216;
games[gameId].packedData = packedData;
games[gameId].packedData2 = packedData2;
emit ExactPriceCreated(
gameId,
feedNumber,
opponent,
uint32(block.timestamp),
endTime,
msg.sender,
initiatorPrice,
depositAmount
);
}
/**
* Accepts 1vs1 exact price mode game and deposit funds
* @param gameId game id
* @param opponentPrice picked asset price
*/
function acceptGame(bytes32 gameId, uint32 opponentPrice) public {
GameInfo memory game = decodeData(gameId);
require(game.gameStatus == Status.Created, "Wrong status!");
require(
game.startTime + (game.endTime - game.startTime) / 3 >=
block.timestamp,
"Game is closed for new players"
);
require(game.initiatorPrice != opponentPrice, "Same asset prices");
// If game is not private address should be 0
if (game.opponent != address(0)) {
require(
msg.sender == game.opponent,
"Only certain account can accept"
);
} else {
require(msg.sender != game.initiator, "Wrong opponent");
games[gameId].packedData |= uint256(uint160(msg.sender));
}
games[gameId].packedData |= uint256(opponentPrice) << 224;
ITreasury(treasury).depositAndLock(game.depositAmount, msg.sender);
//rewrites status
games[gameId].packedData2 =
(games[gameId].packedData2 & ~(uint256(0xFF) << 208)) |
(uint256(uint8(Status.Started)) << 208);
emit ExactPriceAccepted(gameId, msg.sender, opponentPrice);
}
/**
* Accepts 1vs1 exact price mode game with deposited funds
* @param gameId game id
* @param opponentPrice picked asset price
*/
function acceptGameWithDeposit(
bytes32 gameId,
uint32 opponentPrice
) public {
GameInfo memory game = decodeData(gameId);
require(game.gameStatus == Status.Created, "Wrong status!");
require(
game.startTime + (game.endTime - game.startTime) / 3 >=
block.timestamp,
"Game is closed for new players"
);
require(game.initiatorPrice != opponentPrice, "Same asset prices");
// If game is not private address should be 0
if (game.opponent != address(0)) {
require(
msg.sender == game.opponent,
"Only certain account can accept"
);
} else {
require(msg.sender != game.initiator, "Wrong opponent");
games[gameId].packedData |= uint256(uint160(msg.sender));
}
games[gameId].packedData |= uint256(opponentPrice) << 224;
ITreasury(treasury).lock(game.depositAmount, msg.sender);
//rewrites status
games[gameId].packedData2 =
(games[gameId].packedData2 & ~(uint256(0xFF) << 208)) |
(uint256(uint8(Status.Started)) << 208);
emit ExactPriceAccepted(gameId, msg.sender, opponentPrice);
}
/**
* Accepts 1vs1 exact price mode game and deposit funds
* @param gameId game id
* @param opponentPrice picked asset price
*/
function acceptGameWithPermit(
bytes32 gameId,
uint32 opponentPrice,
ITreasury.PermitData calldata permitData
) public {
GameInfo memory game = decodeData(gameId);
require(game.gameStatus == Status.Created, "Wrong status!");
require(
game.startTime + (game.endTime - game.startTime) / 3 >=
block.timestamp,
"Game is closed for new players"
);
require(game.initiatorPrice != opponentPrice, "Same asset prices");
// If game is not private address should be 0
if (game.opponent != address(0)) {
require(
msg.sender == game.opponent,
"Only certain account can accept"
);
} else {
require(msg.sender != game.initiator, "Wrong opponent");
games[gameId].packedData |= uint256(uint160(msg.sender));
}
games[gameId].packedData |= uint256(opponentPrice) << 224;
ITreasury(treasury).depositAndLockWithPermit(
game.depositAmount,
msg.sender,
permitData.deadline,
permitData.v,
permitData.r,
permitData.s
);
//rewrites status
games[gameId].packedData2 =
(games[gameId].packedData2 & ~(uint256(0xFF) << 208)) |
(uint256(uint8(Status.Started)) << 208);
emit ExactPriceAccepted(gameId, msg.sender, opponentPrice);
}
/**
* Closes game and refunds tokens
* @param gameId game id
*/
function closeGame(bytes32 gameId) public {
GameInfo memory game = decodeData(gameId);
require(game.initiator == msg.sender, "Wrong sender");
require(game.gameStatus == Status.Created, "Wrong status!");
ITreasury(treasury).refund(game.depositAmount, game.initiator);
//rewrites status
games[gameId].packedData2 =
(games[gameId].packedData2 & ~(uint256(0xFF) << 208)) |
(uint256(uint8(Status.Cancelled)) << 208);
emit ExactPriceCancelled(gameId);
}
/**
* Allows admin to close old\outdated games
* @param gameId game id
*/
function liquidateGame(bytes32 gameId) public onlyRole(GAME_MASTER_ROLE) {
GameInfo memory game = decodeData(gameId);
require(block.timestamp - game.endTime >= 1 weeks, "Too early");
require(game.gameStatus == Status.Created, "Wrong status!");
ITreasury(treasury).refundWithFees(
game.depositAmount,
game.initiator,
refundFee
);
//rewrites status
games[gameId].packedData2 =
(games[gameId].packedData2 & ~(uint256(0xFF) << 208)) |
(uint256(uint8(Status.Cancelled)) << 208);
emit ExactPriceCancelled(gameId);
}
/**
* Finalizes 1vs1 exact price mode game and distributes rewards to players
* @param gameId game id
* @param unverifiedReport Chainlink DataStreams report
*/
function finalizeGame(
bytes32 gameId,
bytes memory unverifiedReport
) public onlyRole(GAME_MASTER_ROLE) {
address upkeep = ITreasury(treasury).upkeep();
GameInfo memory game = decodeData(gameId);
(int192 finalPrice, uint32 priceTimestamp) = IDataStreamsVerifier(
upkeep
).verifyReportWithTimestamp(unverifiedReport, game.feedNumber);
require(game.gameStatus == Status.Started, "Wrong status!");
require(block.timestamp >= game.endTime, "Too early to finish");
require(
priceTimestamp - game.endTime <= 1 minutes ||
block.timestamp - priceTimestamp <= 1 minutes,
"Old chainlink report"
);
uint256 diff1 = game.initiatorPrice > uint192(finalPrice) / 1e14
? game.initiatorPrice - uint192(finalPrice) / 1e14
: uint192(finalPrice) / 1e14 - game.initiatorPrice;
uint256 diff2 = game.opponentPrice > uint192(finalPrice) / 1e14
? game.opponentPrice - uint192(finalPrice) / 1e14
: uint192(finalPrice) / 1e14 - game.opponentPrice;
if (diff1 < diff2) {
ITreasury(treasury).distribute(
game.depositAmount * 2,
game.initiator,
fee
);
emit ExactPriceFinalized(
gameId,
game.initiatorPrice,
game.opponentPrice,
finalPrice,
Status.Finished
);
} else if (diff1 > diff2) {
ITreasury(treasury).distribute(
game.depositAmount * 2,
game.opponent,
fee
);
emit ExactPriceFinalized(
gameId,
game.opponentPrice,
game.initiatorPrice,
finalPrice,
Status.Finished
);
} else {
ITreasury(treasury).refund(game.depositAmount, game.initiator);
ITreasury(treasury).refund(game.depositAmount, game.opponent);
emit ExactPriceCancelled(gameId);
}
//rewrites status
games[gameId].packedData2 =
(games[gameId].packedData2 & ~(uint256(0xFF) << 208)) |
(uint256(uint8(Status.Finished)) << 208);
games[gameId].packedData2 |= uint256(uint192(finalPrice / 1e14)) << 224;
}
/**
* Returns decoded game data
* @param gameId game id
*/
function decodeData(
bytes32 gameId
) public view returns (GameInfo memory gameData) {
uint256 packedData = games[gameId].packedData;
uint256 packedData2 = games[gameId].packedData2;
gameData.opponent = address(uint160(packedData));
gameData.endTime = uint256(uint32(packedData >> 160));
gameData.initiatorPrice = uint256(uint32(packedData >> 192));
gameData.opponentPrice = uint256(uint32(packedData >> 224));
gameData.initiator = address(uint160(packedData2));
gameData.startTime = uint256(uint32(packedData2 >> 160));
gameData.depositAmount = uint256(uint16(packedData2 >> 192));
gameData.gameStatus = Status(uint8(packedData2 >> 208));
gameData.feedNumber = uint8(packedData2 >> 216);
gameData.finalPrice = uint256(uint32(packedData2 >> 224));
}
/**
* Changes min and max game limits
* @param newMaxDuration new max game duration
* @param newMinDuration new min game duration
*/
function changeGameDuration(
uint256 newMaxDuration,
uint256 newMinDuration
) public onlyRole(DEFAULT_ADMIN_ROLE) {
minDuration = newMinDuration;
maxDuration = newMaxDuration;
}
/**
* Change treasury address
* @param newTreasury new treasury address
*/
function setTreasury(
address newTreasury
) public onlyRole(DEFAULT_ADMIN_ROLE) {
require(newTreasury != address(0), "Zero address");
treasury = newTreasury;
emit NewTreasury(newTreasury);
}
/**
* Change fee
* @param newFee new fee in bp
*/
function setFee(uint256 newFee) public onlyRole(DEFAULT_ADMIN_ROLE) {
fee = newFee;
}
/**
* Change refund fee
* @param newRefundFee new fee in bp
*/
function setRefundFee(
uint256 newRefundFee
) public onlyRole(DEFAULT_ADMIN_ROLE) {
refundFee = newRefundFee;
}
}
{
"compilationTarget": {
"contracts/OneVsOneExactPrice.sol": "OneVsOneExactPrice"
},
"evmVersion": "paris",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"AccessControlBadConfirmation","type":"error"},{"inputs":[{"internalType":"address","name":"account","type":"address"},{"internalType":"bytes32","name":"neededRole","type":"bytes32"}],"name":"AccessControlUnauthorizedAccount","type":"error"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"bytes32","name":"gameId","type":"bytes32"},{"indexed":false,"internalType":"address","name":"opponent","type":"address"},{"indexed":false,"internalType":"uint32","name":"opponentPrice","type":"uint32"}],"name":"ExactPriceAccepted","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"bytes32","name":"gameId","type":"bytes32"}],"name":"ExactPriceCancelled","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"bytes32","name":"gameId","type":"bytes32"},{"indexed":false,"internalType":"uint8","name":"feedNumber","type":"uint8"},{"indexed":false,"internalType":"address","name":"opponent","type":"address"},{"indexed":false,"internalType":"uint32","name":"startTime","type":"uint32"},{"indexed":false,"internalType":"uint32","name":"endTime","type":"uint32"},{"indexed":false,"internalType":"address","name":"initiator","type":"address"},{"indexed":false,"internalType":"uint32","name":"initiatorPrice","type":"uint32"},{"indexed":false,"internalType":"uint32","name":"depositAmount","type":"uint32"}],"name":"ExactPriceCreated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"bytes32","name":"gameId","type":"bytes32"},{"indexed":false,"internalType":"uint256","name":"winnerGuessPrice","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"loserGuessPrice","type":"uint256"},{"indexed":false,"internalType":"int192","name":"finalPrice","type":"int192"},{"indexed":false,"internalType":"enum OneVsOneExactPrice.Status","name":"gameStatus","type":"uint8"}],"name":"ExactPriceFinalized","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"newTreasury","type":"address"}],"name":"NewTreasury","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"role","type":"bytes32"},{"indexed":true,"internalType":"bytes32","name":"previousAdminRole","type":"bytes32"},{"indexed":true,"internalType":"bytes32","name":"newAdminRole","type":"bytes32"}],"name":"RoleAdminChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"role","type":"bytes32"},{"indexed":true,"internalType":"address","name":"account","type":"address"},{"indexed":true,"internalType":"address","name":"sender","type":"address"}],"name":"RoleGranted","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"role","type":"bytes32"},{"indexed":true,"internalType":"address","name":"account","type":"address"},{"indexed":true,"internalType":"address","name":"sender","type":"address"}],"name":"RoleRevoked","type":"event"},{"inputs":[],"name":"DEFAULT_ADMIN_ROLE","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"GAME_MASTER_ROLE","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"gameId","type":"bytes32"},{"internalType":"uint32","name":"opponentPrice","type":"uint32"}],"name":"acceptGame","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"gameId","type":"bytes32"},{"internalType":"uint32","name":"opponentPrice","type":"uint32"}],"name":"acceptGameWithDeposit","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"gameId","type":"bytes32"},{"internalType":"uint32","name":"opponentPrice","type":"uint32"},{"components":[{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"internalType":"struct ITreasury.PermitData","name":"permitData","type":"tuple"}],"name":"acceptGameWithPermit","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"newMaxDuration","type":"uint256"},{"internalType":"uint256","name":"newMinDuration","type":"uint256"}],"name":"changeGameDuration","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"gameId","type":"bytes32"}],"name":"closeGame","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint8","name":"feedNumber","type":"uint8"},{"internalType":"address","name":"opponent","type":"address"},{"internalType":"uint32","name":"endTime","type":"uint32"},{"internalType":"uint32","name":"initiatorPrice","type":"uint32"},{"internalType":"uint16","name":"depositAmount","type":"uint16"}],"name":"createGame","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint8","name":"feedNumber","type":"uint8"},{"internalType":"address","name":"opponent","type":"address"},{"internalType":"uint32","name":"endTime","type":"uint32"},{"internalType":"uint32","name":"initiatorPrice","type":"uint32"},{"internalType":"uint16","name":"depositAmount","type":"uint16"}],"name":"createGameWithDeposit","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint8","name":"feedNumber","type":"uint8"},{"internalType":"address","name":"opponent","type":"address"},{"internalType":"uint32","name":"endTime","type":"uint32"},{"internalType":"uint32","name":"initiatorPrice","type":"uint32"},{"internalType":"uint16","name":"depositAmount","type":"uint16"},{"components":[{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"internalType":"struct ITreasury.PermitData","name":"permitData","type":"tuple"}],"name":"createGameWithPermit","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"gameId","type":"bytes32"}],"name":"decodeData","outputs":[{"components":[{"internalType":"uint8","name":"feedNumber","type":"uint8"},{"internalType":"address","name":"initiator","type":"address"},{"internalType":"uint256","name":"startTime","type":"uint256"},{"internalType":"uint256","name":"endTime","type":"uint256"},{"internalType":"address","name":"opponent","type":"address"},{"internalType":"uint256","name":"depositAmount","type":"uint256"},{"internalType":"uint256","name":"initiatorPrice","type":"uint256"},{"internalType":"uint256","name":"opponentPrice","type":"uint256"},{"internalType":"uint256","name":"finalPrice","type":"uint256"},{"internalType":"enum OneVsOneExactPrice.Status","name":"gameStatus","type":"uint8"}],"internalType":"struct OneVsOneExactPrice.GameInfo","name":"gameData","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"fee","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"gameId","type":"bytes32"},{"internalType":"bytes","name":"unverifiedReport","type":"bytes"}],"name":"finalizeGame","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"name":"games","outputs":[{"internalType":"uint256","name":"packedData","type":"uint256"},{"internalType":"uint256","name":"packedData2","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"}],"name":"getRoleAdmin","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"grantRole","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"hasRole","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"gameId","type":"bytes32"}],"name":"liquidateGame","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"maxDuration","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"minDuration","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"refundFee","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"callerConfirmation","type":"address"}],"name":"renounceRole","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"revokeRole","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"newFee","type":"uint256"}],"name":"setFee","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"newRefundFee","type":"uint256"}],"name":"setRefundFee","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newTreasury","type":"address"}],"name":"setTreasury","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes4","name":"interfaceId","type":"bytes4"}],"name":"supportsInterface","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"treasury","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"}]