Contract Name:
sdaemon0x_GameCenter
Contract Source Code:
// File: contracts\openzeppelin\contracts\token\ERC20\IERC20.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
interface IERC20 {
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address to, uint256 value) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 value) external returns (bool);
function transferFrom(address from, address to, uint256 value) external returns (bool);
}
// File: contracts\openzeppelin\contracts\utils\Context.sol
pragma solidity ^0.8.19;
/**
* @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;
}
}
// File: contracts\openzeppelin\contracts\access\Ownable.sol
// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable.sol)
pragma solidity ^0.8.19;
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* The initial owner is set to the address provided by the deployer. This can
* later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/
abstract contract Ownable is Context {
address private _owner;
/**
* @dev The caller account is not authorized to perform an operation.
*/
error OwnableUnauthorizedAccount(address account);
/**
* @dev The owner is not a valid owner account. (eg. `address(0)`)
*/
error OwnableInvalidOwner(address owner);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the address provided by the deployer as the initial owner.
*/
constructor(address initialOwner) {
if (initialOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(initialOwner);
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
_checkOwner();
_;
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if the sender is not the owner.
*/
function _checkOwner() internal view virtual {
if (owner() != _msgSender()) {
revert OwnableUnauthorizedAccount(_msgSender());
}
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby disabling any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
if (newOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
// File: contracts/sdaemon0x/games/sdaemon0xGameCenter.sol
pragma solidity ^0.8.19;
interface ISdaemon0x_GameCenter {
function addGame(address creator, uint256 tokenId, address gameAddr, uint256 amount) external;
function claimGameFee(address gameAddr) external;
function removeGame(address gameAddr) external;
function compute_game_price(uint256 amount) external view returns (uint256, uint256);
function betGame(uint256 amount) external;
function payPlayer(address player, uint256 amount) external;
}
interface TreasuryHolding {
function sync() external;
function getStakerVolume(address staker) external view returns (uint256);
function totalStaked() external view returns (uint256);
function get_nb_stake() external view returns (uint256);
function get_active(uint256 tokenId) external view returns (bool);
function get_amount(uint256 tokenId) external view returns (uint256);
}
contract sdaemon0x_GameCenter is ISdaemon0x_GameCenter, Ownable {
IERC20 public sdeamon = IERC20(0x16a0BfC1C6e331788E0A5096911cDCd9943D2C9c);
uint256 public gameFeeRate = 1000;
uint256 public internal_tax = 0;
TreasuryHolding public stakingContract;
function initStakingContract(address addr) external onlyOwner {
stakingContract = TreasuryHolding(addr);
}
mapping(address => bool) public MastersOfGames;
function set_masterOfGames(address master_, bool enable) public onlyOwner {
MastersOfGames[master_] = enable;
}
modifier onlyMasterOfGames() {
require(MastersOfGames[msg.sender] == true);
_;
}
struct Game {
address game;
address owner;
uint256 tokenId;
uint256 amountBanked;
uint256 lastClaimGame;
}
mapping(address => Game) public games;
Game[] public gamesArray;
event GameBet(address indexed player, uint256 amount);
event PlayerWin(address indexed player, uint256 amount);
constructor() Ownable(0x88524E752144C15dc1a12BA3978c2d700dc97498) {
MastersOfGames[0x88524E752144C15dc1a12BA3978c2d700dc97498] = true;
}
// not get in the player price view!
function setGameFeeRate(uint256 _gameFeeRate) external onlyOwner {
gameFeeRate = _gameFeeRate;
}
// get on part of game fee! 0 is prefered but maybe nice to equalize the game fee
function setInternalTax(uint256 tax) external onlyOwner {
internal_tax = tax;
}
function addGame(address creator_, uint256 tokenId, address gameAddr, uint256 amount) external onlyMasterOfGames {
require(gameAddr != address(0));
require(games[gameAddr].game == address(0));
if (amount > 0) {
sdeamon.transferFrom(msg.sender, address(this), amount);
}
games[gameAddr] = Game(gameAddr, creator_, tokenId, amount, block.timestamp);
gamesArray.push(games[gameAddr]);
}
function claimGameFee(address gameAddr) external {
require(games[gameAddr].game == gameAddr);
require((games[gameAddr].lastClaimGame + 1 days) < block.timestamp);
require(games[gameAddr].owner == msg.sender);
require(games[gameAddr].amountBanked > 0);
uint256 tax_claim = (games[gameAddr].amountBanked * gameFeeRate) / 10000;
sdeamon.transfer(msg.sender, games[gameAddr].amountBanked - tax_claim);
sdeamon.transfer(address(stakingContract), tax_claim);
stakingContract.sync();
games[gameAddr].amountBanked = 0;
games[gameAddr].lastClaimGame = block.timestamp;
}
function removeGame(address gameAddr) external onlyMasterOfGames {
require(games[gameAddr].game == gameAddr);
uint256 tax_claim = (games[gameAddr].amountBanked * gameFeeRate) / 10000;
sdeamon.transfer(msg.sender, games[gameAddr].amountBanked - tax_claim);
sdeamon.transfer(address(stakingContract), tax_claim);
stakingContract.sync();
//reset game
games[gameAddr].amountBanked = 0;
games[gameAddr].game = address(0);
//erase gamesArray
for (uint256 i = 0; i < gamesArray.length; i++) {
if (gamesArray[i].game == gameAddr) {
if (i < gamesArray.length - 1) gamesArray[i] = gamesArray[gamesArray.length - 1];
gamesArray.pop();
return;
}
}
}
modifier onlyGame() {
require(games[msg.sender].game == msg.sender);
_;
}
function compute_game_price(uint256 amount) public view returns (uint256, uint256) {
uint256 feeForReward = (amount * gameFeeRate) / 10000;
return (amount + feeForReward, feeForReward);
}
function betGame(uint256 amount) external onlyGame {
require(amount > 0, "Invalid fee amount");
(uint256 totalAmoutToPay, uint256 fee) = compute_game_price(amount);
uint256 feeInternal = (internal_tax > 0) ? (amount * internal_tax) / 10000 : 0;
uint256 feeHolders = fee + feeInternal;
sdeamon.transferFrom(msg.sender, address(this), totalAmoutToPay);
sdeamon.transfer(address(stakingContract), feeHolders);
stakingContract.sync();
games[msg.sender].amountBanked += amount;
emit GameBet(msg.sender, amount);
}
function payPlayer(address player, uint256 amount) external onlyGame {
if (amount > 0) {
require(amount <= games[msg.sender].amountBanked);
games[msg.sender].amountBanked -= amount;
sdeamon.transfer(player, amount);
emit PlayerWin(player, amount);
}
}
}