Contract Source Code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// solhint-disable-next-line interface-starts-with-i
interface AggregatorV3Interface {
function decimals() external view returns (uint8);
function description() external view returns (string memory);
function version() external view returns (uint256);
function getRoundData(
uint80 _roundId
) external view returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound);
function latestRoundData()
external
view
returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound);
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (token/ERC20/IERC20.sol)
pragma solidity ^0.8.20;
/**
* @dev Interface of the ERC-20 standard as defined in the ERC.
*/
interface IERC20 {
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
/**
* @dev Returns the value of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the value of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves a `value` amount of tokens from the caller's account to `to`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address to, uint256 value) external returns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);
/**
* @dev Sets a `value` amount of tokens as the allowance of `spender` over the
* caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 value) external returns (bool);
/**
* @dev Moves a `value` amount of tokens from `from` to `to` using the
* allowance mechanism. `value` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(address from, address to, uint256 value) external returns (bool);
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/ReentrancyGuard.sol)
pragma solidity ^0.8.20;
/**
* @dev Contract module that helps prevent reentrant calls to a function.
*
* Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
* available, which can be applied to functions to make sure there are no nested
* (reentrant) calls to them.
*
* Note that because there is a single `nonReentrant` guard, functions marked as
* `nonReentrant` may not call one another. This can be worked around by making
* those functions `private`, and then adding `external` `nonReentrant` entry
* points to them.
*
* TIP: If EIP-1153 (transient storage) is available on the chain you're deploying at,
* consider using {ReentrancyGuardTransient} instead.
*
* TIP: If you would like to learn more about reentrancy and alternative ways
* to protect against it, check out our blog post
* https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
*/
abstract contract ReentrancyGuard {
// Booleans are more expensive than uint256 or any type that takes up a full
// word because each write operation emits an extra SLOAD to first read the
// slot's contents, replace the bits taken up by the boolean, and then write
// back. This is the compiler's defense against contract upgrades and
// pointer aliasing, and it cannot be disabled.
// The values being non-zero value makes deployment a bit more expensive,
// but in exchange the refund on every call to nonReentrant will be lower in
// amount. Since refunds are capped to a percentage of the total
// transaction's gas, it is best to keep them low in cases like this one, to
// increase the likelihood of the full refund coming into effect.
uint256 private constant NOT_ENTERED = 1;
uint256 private constant ENTERED = 2;
uint256 private _status;
/**
* @dev Unauthorized reentrant call.
*/
error ReentrancyGuardReentrantCall();
constructor() {
_status = NOT_ENTERED;
}
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `nonReentrant` function from another `nonReentrant`
* function is not supported. It is possible to prevent this from happening
* by making the `nonReentrant` function external, and making it call a
* `private` function that does the actual work.
*/
modifier nonReentrant() {
_nonReentrantBefore();
_;
_nonReentrantAfter();
}
function _nonReentrantBefore() private {
// On the first call to nonReentrant, _status will be NOT_ENTERED
if (_status == ENTERED) {
revert ReentrancyGuardReentrantCall();
}
// Any calls to nonReentrant after this point will fail
_status = ENTERED;
}
function _nonReentrantAfter() private {
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_status = NOT_ENTERED;
}
/**
* @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
* `nonReentrant` function in the call stack.
*/
function _reentrancyGuardEntered() internal view returns (bool) {
return _status == ENTERED;
}
}
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.26;
interface IManager {
function getContract(string memory name) external view returns (address);
function owner() external view returns (address);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
interface IOTombPlus {
function mint(address, uint256) external;
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
interface ITSharePlus is IERC20 {
function WLMint(uint256 amount, address _to) external;
function burnFrom(address account, uint256 amount) external;
}
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.26;
import {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/shared/interfaces/AggregatorV3Interface.sol";
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import "./interfaces/IManager.sol";
import "./interfaces/ITsharePlus.sol";
import "./interfaces/IOTombPlus.sol";
contract Locker is ReentrancyGuard {
IManager private Manager;
ITSharePlus private Share;
IOTombPlus private OTomb;
struct UserGlobal {
uint256 deposited;
uint256 lastRoundClaimed;
}
mapping(address => UserGlobal) public userGlobalDetails;
struct UserRound {
uint256 engaged;
uint8 position; // 0 = unknown, 1 = long, 2 = short
}
mapping(address => mapping(uint256 => UserRound)) public userRoundsDetails;
uint256 public lastRoundId = 1;
uint256 public roundDuration = 1 hours;
uint256 private _bonus = 50; // 0.5%
struct Round {
uint256 startPrice;
uint256 endPrice;
uint256 startTime;
uint256 endTime;
uint256 bonus;
uint8 winningPosition; // 0 = unknown, 1 = long, 2 = short
}
mapping(uint256 => Round) public rounds;
uint256 private _deviationThreshold = 50; // 0.05%
AggregatorV3Interface internal dataFeed;
constructor(address _manager) {
Manager = IManager(_manager);
dataFeed = AggregatorV3Interface(
0xc76dFb89fF298145b417d221B2c747d84952e01d
);
}
modifier onlyOwner() {
require(msg.sender == Manager.owner(), "Not Authorized");
_;
}
function setManager(address _manager) external onlyOwner {
Manager = IManager(_manager);
}
function setDataFeed(address _dataFeed) external onlyOwner {
dataFeed = AggregatorV3Interface(_dataFeed);
}
function setRoundDuration(uint256 _roundDuration) external onlyOwner {
roundDuration = _roundDuration;
}
function setBonus(uint256 __bonus) external onlyOwner {
_bonus = __bonus;
}
function updateTombPlus() external onlyOwner {
Share = ITSharePlus(_getContract("Share"));
OTomb = IOTombPlus(_getContract("OTomb"));
}
// TESTNET
bool public testnet = true;
uint256 public currentPrice = 100;
function setTestnet(bool _testnet) external onlyOwner {
testnet = _testnet;
}
function setCurrentPrice(uint256 _currentPrice) external onlyOwner {
currentPrice = _currentPrice;
}
// END TESTNET
function startRounds() external onlyOwner {
rounds[lastRoundId] = Round({
startPrice: testnet ? currentPrice : uint256(getChainlinkDataFeedLatestAnswer()),
endPrice: 0,
startTime: block.timestamp,
endTime: block.timestamp + roundDuration,
bonus: _bonus,
winningPosition: 0
});
}
function catchEndPrice() external {
uint256 roundId = lastRoundId;
Round storage round = rounds[roundId];
require(round.endPrice == 0, "round already ended");
require(block.timestamp >= round.endTime, "round not ended yet");
round.endPrice = testnet ? currentPrice : uint256(getChainlinkDataFeedLatestAnswer());
round.winningPosition = round.endPrice > round.startPrice ? 1 : 2;
lastRoundId++;
Round storage nextRound = rounds[lastRoundId];
nextRound.startPrice = round.endPrice;
nextRound.startTime = round.endTime;
nextRound.endTime = block.timestamp + roundDuration;
nextRound.bonus = _bonus;
nextRound.winningPosition = 0;
}
function deposit(uint256 amount) external nonReentrant {
UserGlobal storage userGlobal = userGlobalDetails[msg.sender];
userGlobal.deposited += amount;
userGlobal.lastRoundClaimed = lastRoundId;
Share.transferFrom(msg.sender, address(this), amount);
}
function withdraw(uint256 amount) external nonReentrant {
UserGlobal storage userGlobal = userGlobalDetails[msg.sender];
require(userGlobal.deposited >= amount, "Not enough deposited");
require(userRoundsDetails[msg.sender][lastRoundId].position == 0, "user already engaged in round");
unchecked {
userGlobal.deposited -= amount;
}
Share.transfer(msg.sender, amount);
}
function vote(uint8 position) external nonReentrant {
UserRound storage user = userRoundsDetails[msg.sender][lastRoundId];
require(user.position == 0, "user already engaged");
user.engaged = userGlobalDetails[msg.sender].deposited;
user.position = position;
}
function claim() external nonReentrant {
uint256 rewards = _rewardsAccByUser(msg.sender);
if (rewards == 0) {
return;
}
userGlobalDetails[msg.sender].lastRoundClaimed = lastRoundId - 1;
OTomb.mint(msg.sender, rewards);
}
function getChainlinkDataFeedLatestAnswer() public view returns (int) {
// prettier-ignore
(
/* uint80 roundID */,
int answer,
/*uint startedAt*/,
/*uint timeStamp*/,
/*uint80 answeredInRound*/
) = dataFeed.latestRoundData();
return answer;
}
function _rewardsAccByUser(address user) private view returns (uint256) {
uint256 _currentRoundId = lastRoundId;
uint256 _lastRoundClaimed = userGlobalDetails[user].lastRoundClaimed;
if (_currentRoundId == _lastRoundClaimed) {
return 0;
}
uint256 sum = 0;
for (uint256 i = _lastRoundClaimed; i < _currentRoundId; i++) {
UserRound memory _user = userRoundsDetails[user][i];
Round memory round = rounds[i];
if (_user.position == round.winningPosition) {
sum += (_user.engaged * round.bonus) / 10000;
}
}
return sum;
}
function getPendingRewards(address user) external view returns (uint256) {
return _rewardsAccByUser(user);
}
function _getContract(
string memory contractName
) internal view returns (address) {
return Manager.getContract(contractName);
}
}