|
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
|
|
|
|
|
|
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 amount) external returns (bool);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function allowance(address owner, address spender) external view returns (uint256);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function approve(address spender, uint256 amount) external returns (bool);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function transferFrom(
|
|
address from,
|
|
address to,
|
|
uint256 amount
|
|
) external returns (bool);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abstract contract Context {
|
|
function _msgSender() internal view virtual returns (address) {
|
|
return msg.sender;
|
|
}
|
|
|
|
function _msgData() internal view virtual returns (bytes calldata) {
|
|
return msg.data;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interface IERC20Metadata is IERC20 {
|
|
|
|
|
|
|
|
function name() external view returns (string memory);
|
|
|
|
|
|
|
|
|
|
function symbol() external view returns (string memory);
|
|
|
|
|
|
|
|
|
|
function decimals() external view returns (uint8);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
contract ERC20 is Context, IERC20, IERC20Metadata {
|
|
mapping(address => uint256) private _balances;
|
|
|
|
mapping(address => mapping(address => uint256)) private _allowances;
|
|
|
|
uint256 private _totalSupply;
|
|
|
|
string private _name;
|
|
string private _symbol;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor(string memory name_, string memory symbol_) {
|
|
_name = name_;
|
|
_symbol = symbol_;
|
|
}
|
|
|
|
|
|
|
|
|
|
function name() public view virtual override returns (string memory) {
|
|
return _name;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function symbol() public view virtual override returns (string memory) {
|
|
return _symbol;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function decimals() public view virtual override returns (uint8) {
|
|
return 18;
|
|
}
|
|
|
|
|
|
|
|
|
|
function totalSupply() public view virtual override returns (uint256) {
|
|
return _totalSupply;
|
|
}
|
|
|
|
|
|
|
|
|
|
function balanceOf(address account) public view virtual override returns (uint256) {
|
|
return _balances[account];
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function transfer(address to, uint256 amount) public virtual override returns (bool) {
|
|
address owner = _msgSender();
|
|
_transfer(owner, to, amount);
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
|
|
function allowance(address owner, address spender) public view virtual override returns (uint256) {
|
|
return _allowances[owner][spender];
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function approve(address spender, uint256 amount) public virtual override returns (bool) {
|
|
address owner = _msgSender();
|
|
_approve(owner, spender, amount);
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function transferFrom(
|
|
address from,
|
|
address to,
|
|
uint256 amount
|
|
) public virtual override returns (bool) {
|
|
address spender = _msgSender();
|
|
_spendAllowance(from, spender, amount);
|
|
_transfer(from, to, amount);
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
|
|
address owner = _msgSender();
|
|
_approve(owner, spender, allowance(owner, spender) + addedValue);
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
|
|
address owner = _msgSender();
|
|
uint256 currentAllowance = allowance(owner, spender);
|
|
require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
|
|
unchecked {
|
|
_approve(owner, spender, currentAllowance - subtractedValue);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _transfer(
|
|
address from,
|
|
address to,
|
|
uint256 amount
|
|
) internal virtual {
|
|
require(from != address(0), "ERC20: transfer from the zero address");
|
|
require(to != address(0), "ERC20: transfer to the zero address");
|
|
|
|
_beforeTokenTransfer(from, to, amount);
|
|
|
|
uint256 fromBalance = _balances[from];
|
|
require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");
|
|
unchecked {
|
|
_balances[from] = fromBalance - amount;
|
|
}
|
|
_balances[to] += amount;
|
|
|
|
emit Transfer(from, to, amount);
|
|
|
|
_afterTokenTransfer(from, to, amount);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _mint(address account, uint256 amount) internal virtual {
|
|
require(account != address(0), "ERC20: mint to the zero address");
|
|
|
|
_beforeTokenTransfer(address(0), account, amount);
|
|
|
|
_totalSupply += amount;
|
|
_balances[account] += amount;
|
|
emit Transfer(address(0), account, amount);
|
|
|
|
_afterTokenTransfer(address(0), account, amount);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _burn(address account, uint256 amount) internal virtual {
|
|
require(account != address(0), "ERC20: burn from the zero address");
|
|
|
|
_beforeTokenTransfer(account, address(0), amount);
|
|
|
|
uint256 accountBalance = _balances[account];
|
|
require(accountBalance >= amount, "ERC20: burn amount exceeds balance");
|
|
unchecked {
|
|
_balances[account] = accountBalance - amount;
|
|
}
|
|
_totalSupply -= amount;
|
|
|
|
emit Transfer(account, address(0), amount);
|
|
|
|
_afterTokenTransfer(account, address(0), amount);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _approve(
|
|
address owner,
|
|
address spender,
|
|
uint256 amount
|
|
) internal virtual {
|
|
require(owner != address(0), "ERC20: approve from the zero address");
|
|
require(spender != address(0), "ERC20: approve to the zero address");
|
|
|
|
_allowances[owner][spender] = amount;
|
|
emit Approval(owner, spender, amount);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _spendAllowance(
|
|
address owner,
|
|
address spender,
|
|
uint256 amount
|
|
) internal virtual {
|
|
uint256 currentAllowance = allowance(owner, spender);
|
|
if (currentAllowance != type(uint256).max) {
|
|
require(currentAllowance >= amount, "ERC20: insufficient allowance");
|
|
unchecked {
|
|
_approve(owner, spender, currentAllowance - amount);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _beforeTokenTransfer(
|
|
address from,
|
|
address to,
|
|
uint256 amount
|
|
) internal virtual {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _afterTokenTransfer(
|
|
address from,
|
|
address to,
|
|
uint256 amount
|
|
) internal virtual {}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abstract contract Ownable is Context {
|
|
address private _owner;
|
|
|
|
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
_transferOwnership(_msgSender());
|
|
}
|
|
|
|
|
|
|
|
|
|
function owner() public view virtual returns (address) {
|
|
return _owner;
|
|
}
|
|
|
|
|
|
|
|
|
|
modifier onlyOwner() {
|
|
require(owner() == _msgSender(), "Ownable: caller is not the owner");
|
|
_;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function renounceOwnership() public virtual onlyOwner {
|
|
_transferOwnership(address(0));
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function transferOwnership(address newOwner) public virtual onlyOwner {
|
|
require(newOwner != address(0), "Ownable: new owner is the zero address");
|
|
_transferOwnership(newOwner);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function _transferOwnership(address newOwner) internal virtual {
|
|
address oldOwner = _owner;
|
|
_owner = newOwner;
|
|
emit OwnershipTransferred(oldOwner, newOwner);
|
|
}
|
|
}
|
|
|
|
contract BetToken is ERC20, Ownable {
|
|
constructor(string memory _nameAndSymbol) ERC20(_nameAndSymbol, _nameAndSymbol) {}
|
|
|
|
function mint(address _to, uint _amount) external onlyOwner {
|
|
_mint(_to, _amount);
|
|
}
|
|
|
|
function burn(address _from, uint _amount) external onlyOwner {
|
|
_burn(_from, _amount);
|
|
}
|
|
}
|
|
interface IUniswapV2Router01 {
|
|
function factory() external pure returns (address);
|
|
function WETH() external pure returns (address);
|
|
|
|
function addLiquidity(
|
|
address tokenA,
|
|
address tokenB,
|
|
uint amountADesired,
|
|
uint amountBDesired,
|
|
uint amountAMin,
|
|
uint amountBMin,
|
|
address to,
|
|
uint deadline
|
|
) external returns (uint amountA, uint amountB, uint liquidity);
|
|
function addLiquidityETH(
|
|
address token,
|
|
uint amountTokenDesired,
|
|
uint amountTokenMin,
|
|
uint amountETHMin,
|
|
address to,
|
|
uint deadline
|
|
) external payable returns (uint amountToken, uint amountETH, uint liquidity);
|
|
function removeLiquidity(
|
|
address tokenA,
|
|
address tokenB,
|
|
uint liquidity,
|
|
uint amountAMin,
|
|
uint amountBMin,
|
|
address to,
|
|
uint deadline
|
|
) external returns (uint amountA, uint amountB);
|
|
function removeLiquidityETH(
|
|
address token,
|
|
uint liquidity,
|
|
uint amountTokenMin,
|
|
uint amountETHMin,
|
|
address to,
|
|
uint deadline
|
|
) external returns (uint amountToken, uint amountETH);
|
|
function removeLiquidityWithPermit(
|
|
address tokenA,
|
|
address tokenB,
|
|
uint liquidity,
|
|
uint amountAMin,
|
|
uint amountBMin,
|
|
address to,
|
|
uint deadline,
|
|
bool approveMax, uint8 v, bytes32 r, bytes32 s
|
|
) external returns (uint amountA, uint amountB);
|
|
function removeLiquidityETHWithPermit(
|
|
address token,
|
|
uint liquidity,
|
|
uint amountTokenMin,
|
|
uint amountETHMin,
|
|
address to,
|
|
uint deadline,
|
|
bool approveMax, uint8 v, bytes32 r, bytes32 s
|
|
) external returns (uint amountToken, uint amountETH);
|
|
function swapExactTokensForTokens(
|
|
uint amountIn,
|
|
uint amountOutMin,
|
|
address[] calldata path,
|
|
address to,
|
|
uint deadline
|
|
) external returns (uint[] memory amounts);
|
|
function swapTokensForExactTokens(
|
|
uint amountOut,
|
|
uint amountInMax,
|
|
address[] calldata path,
|
|
address to,
|
|
uint deadline
|
|
) external returns (uint[] memory amounts);
|
|
function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)
|
|
external
|
|
payable
|
|
returns (uint[] memory amounts);
|
|
function swapTokensForExactETH(uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline)
|
|
external
|
|
returns (uint[] memory amounts);
|
|
function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)
|
|
external
|
|
returns (uint[] memory amounts);
|
|
function swapETHForExactTokens(uint amountOut, address[] calldata path, address to, uint deadline)
|
|
external
|
|
payable
|
|
returns (uint[] memory amounts);
|
|
|
|
function quote(uint amountA, uint reserveA, uint reserveB) external pure returns (uint amountB);
|
|
function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) external pure returns (uint amountOut);
|
|
function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) external pure returns (uint amountIn);
|
|
function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts);
|
|
function getAmountsIn(uint amountOut, address[] calldata path) external view returns (uint[] memory amounts);
|
|
}
|
|
interface IMintBurn {
|
|
function mint(address, uint) external;
|
|
function burn(address, uint) external;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
library Address {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function isContract(address account) internal view returns (bool) {
|
|
|
|
|
|
|
|
|
|
return account.code.length > 0;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function sendValue(address payable recipient, uint256 amount) internal {
|
|
require(address(this).balance >= amount, "Address: insufficient balance");
|
|
|
|
(bool success, ) = recipient.call{value: amount}("");
|
|
require(success, "Address: unable to send value, recipient may have reverted");
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
|
|
return functionCall(target, data, "Address: low-level call failed");
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function functionCall(
|
|
address target,
|
|
bytes memory data,
|
|
string memory errorMessage
|
|
) internal returns (bytes memory) {
|
|
return functionCallWithValue(target, data, 0, errorMessage);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function functionCallWithValue(
|
|
address target,
|
|
bytes memory data,
|
|
uint256 value
|
|
) internal returns (bytes memory) {
|
|
return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function functionCallWithValue(
|
|
address target,
|
|
bytes memory data,
|
|
uint256 value,
|
|
string memory errorMessage
|
|
) internal returns (bytes memory) {
|
|
require(address(this).balance >= value, "Address: insufficient balance for call");
|
|
require(isContract(target), "Address: call to non-contract");
|
|
|
|
(bool success, bytes memory returndata) = target.call{value: value}(data);
|
|
return verifyCallResult(success, returndata, errorMessage);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
|
|
return functionStaticCall(target, data, "Address: low-level static call failed");
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function functionStaticCall(
|
|
address target,
|
|
bytes memory data,
|
|
string memory errorMessage
|
|
) internal view returns (bytes memory) {
|
|
require(isContract(target), "Address: static call to non-contract");
|
|
|
|
(bool success, bytes memory returndata) = target.staticcall(data);
|
|
return verifyCallResult(success, returndata, errorMessage);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
|
|
return functionDelegateCall(target, data, "Address: low-level delegate call failed");
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function functionDelegateCall(
|
|
address target,
|
|
bytes memory data,
|
|
string memory errorMessage
|
|
) internal returns (bytes memory) {
|
|
require(isContract(target), "Address: delegate call to non-contract");
|
|
|
|
(bool success, bytes memory returndata) = target.delegatecall(data);
|
|
return verifyCallResult(success, returndata, errorMessage);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function verifyCallResult(
|
|
bool success,
|
|
bytes memory returndata,
|
|
string memory errorMessage
|
|
) internal pure returns (bytes memory) {
|
|
if (success) {
|
|
return returndata;
|
|
} else {
|
|
|
|
if (returndata.length > 0) {
|
|
|
|
|
|
assembly {
|
|
let returndata_size := mload(returndata)
|
|
revert(add(32, returndata), returndata_size)
|
|
}
|
|
} else {
|
|
revert(errorMessage);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
library SafeERC20 {
|
|
using Address for address;
|
|
|
|
function safeTransfer(
|
|
IERC20 token,
|
|
address to,
|
|
uint256 value
|
|
) internal {
|
|
_callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
|
|
}
|
|
|
|
function safeTransferFrom(
|
|
IERC20 token,
|
|
address from,
|
|
address to,
|
|
uint256 value
|
|
) internal {
|
|
_callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function safeApprove(
|
|
IERC20 token,
|
|
address spender,
|
|
uint256 value
|
|
) internal {
|
|
|
|
|
|
|
|
require(
|
|
(value == 0) || (token.allowance(address(this), spender) == 0),
|
|
"SafeERC20: approve from non-zero to non-zero allowance"
|
|
);
|
|
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
|
|
}
|
|
|
|
function safeIncreaseAllowance(
|
|
IERC20 token,
|
|
address spender,
|
|
uint256 value
|
|
) internal {
|
|
uint256 newAllowance = token.allowance(address(this), spender) + value;
|
|
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
|
|
}
|
|
|
|
function safeDecreaseAllowance(
|
|
IERC20 token,
|
|
address spender,
|
|
uint256 value
|
|
) internal {
|
|
unchecked {
|
|
uint256 oldAllowance = token.allowance(address(this), spender);
|
|
require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
|
|
uint256 newAllowance = oldAllowance - value;
|
|
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _callOptionalReturn(IERC20 token, bytes memory data) private {
|
|
|
|
|
|
|
|
|
|
bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
|
|
if (returndata.length > 0) {
|
|
|
|
require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abstract contract ReentrancyGuard {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint256 private constant _NOT_ENTERED = 1;
|
|
uint256 private constant _ENTERED = 2;
|
|
|
|
uint256 private _status;
|
|
|
|
constructor() {
|
|
_status = _NOT_ENTERED;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
modifier nonReentrant() {
|
|
|
|
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
|
|
|
|
|
|
_status = _ENTERED;
|
|
|
|
_;
|
|
|
|
|
|
|
|
_status = _NOT_ENTERED;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
contract BetETH is ReentrancyGuard {
|
|
using SafeERC20 for IERC20;
|
|
|
|
|
|
uint public unlockTime;
|
|
address[] public tokenPath;
|
|
|
|
string public name;
|
|
|
|
|
|
address public betYesToken;
|
|
address public betNoToken;
|
|
|
|
|
|
uint public betYesDeposiedWei;
|
|
uint public betNoDeposiedWei;
|
|
|
|
uint public initialPrice;
|
|
uint public finalPrice;
|
|
|
|
IUniswapV2Router01 public v2Router;
|
|
|
|
|
|
uint256 constant internal INITIAL_SHARES = 10 ** 18;
|
|
|
|
uint public oneTokenInWei;
|
|
|
|
address public constant paymentToken = address(0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE);
|
|
|
|
uint public platformFeePercent;
|
|
|
|
uint public creatorFeePercent;
|
|
|
|
address public platformWallet;
|
|
|
|
address public creator;
|
|
|
|
uint public constant totalFeePercent = 1000;
|
|
|
|
|
|
constructor(
|
|
uint _lockTime,
|
|
address[] memory _tokenPath,
|
|
string memory _name,
|
|
address _v2Router,
|
|
uint _initialPrice,
|
|
uint _oneTokenInWei,
|
|
uint _platformFeePercent,
|
|
uint _creatorFeePercent,
|
|
address _platformWallet,
|
|
address _creator
|
|
)
|
|
{
|
|
unlockTime = block.timestamp + _lockTime;
|
|
name = _name;
|
|
tokenPath = _tokenPath;
|
|
betYesToken = address(new BetToken("YES"));
|
|
betNoToken = address(new BetToken("NO"));
|
|
v2Router = IUniswapV2Router01(_v2Router);
|
|
initialPrice = _initialPrice;
|
|
oneTokenInWei = _oneTokenInWei;
|
|
platformFeePercent = _platformFeePercent;
|
|
creatorFeePercent = _creatorFeePercent;
|
|
platformWallet = _platformWallet;
|
|
creator = _creator;
|
|
}
|
|
|
|
|
|
function betForYes() external payable {
|
|
_bet(betYesToken, msg.value, betYesDeposiedWei);
|
|
betYesDeposiedWei += msg.value;
|
|
}
|
|
|
|
|
|
function betForNo() external payable {
|
|
_bet(betNoToken, msg.value, betNoDeposiedWei);
|
|
betNoDeposiedWei += msg.value;
|
|
}
|
|
|
|
|
|
function withdrawForYes(uint sharesAmount) external {
|
|
require(block.timestamp < unlockTime, "locked");
|
|
uint withdrawed = _withdraw(betYesToken, sharesAmount, betYesDeposiedWei);
|
|
betYesDeposiedWei -= withdrawed;
|
|
}
|
|
|
|
function withdrawForNo(uint sharesAmount) external {
|
|
require(block.timestamp < unlockTime, "locked");
|
|
uint withdrawed = _withdraw(betNoToken, sharesAmount, betNoDeposiedWei);
|
|
betNoDeposiedWei -= withdrawed;
|
|
}
|
|
|
|
|
|
function getReward() external{
|
|
require(block.timestamp > unlockTime, "early");
|
|
|
|
if(finalPrice == 0)
|
|
finalPrice = getPrice();
|
|
|
|
address token = finalPrice > initialPrice
|
|
? betYesToken
|
|
: betNoToken;
|
|
|
|
_withdraw(
|
|
token,
|
|
IERC20(token).balanceOf(msg.sender),
|
|
address(this).balance
|
|
);
|
|
}
|
|
|
|
|
|
|
|
function getPrice() public view returns(uint){
|
|
uint[] memory res = v2Router.getAmountsOut(oneTokenInWei, tokenPath);
|
|
return res[1];
|
|
}
|
|
|
|
|
|
|
|
|
|
function _withdraw(
|
|
address betShareToken,
|
|
uint sharesAmount,
|
|
uint balance
|
|
)
|
|
internal
|
|
nonReentrant
|
|
returns (uint withdrawed)
|
|
{
|
|
require(sharesAmount > 0, "Zerro shares");
|
|
require(IERC20(betShareToken).balanceOf(msg.sender) >= sharesAmount, "not enough shares");
|
|
|
|
uint totalShares = IERC20(betShareToken).totalSupply();
|
|
uint amount = balance * sharesAmount / totalShares;
|
|
|
|
require(address(this).balance >= amount, "exceeds eth");
|
|
|
|
uint userAmount = amount;
|
|
|
|
|
|
if(platformFeePercent > 0){
|
|
uint platformFee = amount / totalFeePercent * platformFeePercent;
|
|
userAmount = userAmount - platformFee;
|
|
payable(platformWallet).transfer(platformFee);
|
|
}
|
|
|
|
|
|
if(creatorFeePercent > 0){
|
|
uint creatorFee = amount / totalFeePercent * creatorFeePercent;
|
|
userAmount = userAmount - creatorFee;
|
|
payable(creator).transfer(creatorFee);
|
|
}
|
|
|
|
|
|
payable(msg.sender).transfer(userAmount);
|
|
|
|
|
|
IMintBurn(betShareToken).burn(msg.sender, sharesAmount);
|
|
|
|
withdrawed = amount;
|
|
}
|
|
|
|
|
|
function _bet(address betShareToken, uint ethAmount, uint totalDeposited) internal {
|
|
require(block.timestamp < unlockTime, "locked");
|
|
require(initialPrice > 0, "require initial price");
|
|
require(ethAmount > 0, "wrong bet fee");
|
|
|
|
|
|
IMintBurn(betShareToken).mint(
|
|
msg.sender,
|
|
_calculateSharesToMint(
|
|
ethAmount,
|
|
IERC20(betShareToken).totalSupply(),
|
|
totalDeposited
|
|
)
|
|
);
|
|
}
|
|
|
|
|
|
function _calculateSharesToMint(uint ethAmount, uint sharesSupply, uint totalDeposited)
|
|
internal
|
|
view
|
|
returns(uint)
|
|
{
|
|
if (sharesSupply == 0)
|
|
return INITIAL_SHARES;
|
|
|
|
return ethAmount * sharesSupply / totalDeposited;
|
|
}
|
|
|
|
|
|
function winnerBalance(address account) external view returns(uint){
|
|
uint _finalPrice = finalPrice > 0
|
|
? finalPrice
|
|
: getPrice();
|
|
|
|
address token = _finalPrice > initialPrice
|
|
? betYesToken
|
|
: betNoToken;
|
|
|
|
return IERC20(token).balanceOf(account);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
contract BetERC20 is ReentrancyGuard {
|
|
using SafeERC20 for IERC20;
|
|
|
|
|
|
uint public unlockTime;
|
|
address[] public tokenPath;
|
|
|
|
string public name;
|
|
|
|
|
|
address public betYesToken;
|
|
address public betNoToken;
|
|
|
|
|
|
uint public betYesDeposiedWei;
|
|
uint public betNoDeposiedWei;
|
|
|
|
uint public initialPrice;
|
|
uint public finalPrice;
|
|
|
|
IUniswapV2Router01 public v2Router;
|
|
|
|
|
|
uint256 constant internal INITIAL_SHARES = 10 ** 18;
|
|
|
|
uint public oneTokenInWei;
|
|
|
|
address public paymentToken;
|
|
|
|
uint public platformFeePercent;
|
|
|
|
uint public creatorFeePercent;
|
|
|
|
address public platformWallet;
|
|
|
|
address public creator;
|
|
|
|
uint public constant totalFeePercent = 1000;
|
|
|
|
|
|
constructor(
|
|
uint _lockTime,
|
|
address[] memory _tokenPath,
|
|
string memory _name,
|
|
address _v2Router,
|
|
uint _initialPrice,
|
|
uint _oneTokenInWei,
|
|
address _paymentToken,
|
|
uint _platformFeePercent,
|
|
uint _creatorFeePercent,
|
|
address _platformWallet,
|
|
address _creator
|
|
)
|
|
{
|
|
unlockTime = block.timestamp + _lockTime;
|
|
name = _name;
|
|
tokenPath = _tokenPath;
|
|
betYesToken = address(new BetToken("YES"));
|
|
betNoToken = address(new BetToken("NO"));
|
|
v2Router = IUniswapV2Router01(_v2Router);
|
|
initialPrice = _initialPrice;
|
|
oneTokenInWei = _oneTokenInWei;
|
|
paymentToken = _paymentToken;
|
|
platformFeePercent = _platformFeePercent;
|
|
creatorFeePercent = _creatorFeePercent;
|
|
platformWallet = _platformWallet;
|
|
creator = _creator;
|
|
}
|
|
|
|
|
|
function betForYes(uint tokenAmount) external {
|
|
IERC20(paymentToken).safeTransferFrom(msg.sender, address(this), tokenAmount);
|
|
_bet(betYesToken, tokenAmount, betYesDeposiedWei);
|
|
betYesDeposiedWei += tokenAmount;
|
|
}
|
|
|
|
|
|
function betForNo(uint tokenAmount) external {
|
|
IERC20(paymentToken).safeTransferFrom(msg.sender, address(this), tokenAmount);
|
|
_bet(betNoToken, tokenAmount, betNoDeposiedWei);
|
|
betNoDeposiedWei += tokenAmount;
|
|
}
|
|
|
|
|
|
function withdrawForYes(uint sharesAmount) external {
|
|
require(block.timestamp < unlockTime, "locked");
|
|
uint withdrawed = _withdraw(betYesToken, sharesAmount, betYesDeposiedWei);
|
|
betYesDeposiedWei -= withdrawed;
|
|
}
|
|
|
|
function withdrawForNo(uint sharesAmount) external {
|
|
require(block.timestamp < unlockTime, "locked");
|
|
uint withdrawed = _withdraw(betNoToken, sharesAmount, betNoDeposiedWei);
|
|
betNoDeposiedWei -= withdrawed;
|
|
}
|
|
|
|
|
|
function getReward() external{
|
|
require(block.timestamp > unlockTime, "early");
|
|
|
|
if(finalPrice == 0)
|
|
finalPrice = getPrice();
|
|
|
|
address token = finalPrice > initialPrice
|
|
? betYesToken
|
|
: betNoToken;
|
|
|
|
_withdraw(
|
|
token,
|
|
IERC20(token).balanceOf(msg.sender),
|
|
IERC20(paymentToken).balanceOf(address(this))
|
|
);
|
|
}
|
|
|
|
|
|
|
|
function getPrice() public view returns(uint){
|
|
uint[] memory res = v2Router.getAmountsOut(oneTokenInWei, tokenPath);
|
|
return res[1];
|
|
}
|
|
|
|
|
|
|
|
|
|
function _withdraw(
|
|
address betShareToken,
|
|
uint sharesAmount,
|
|
uint balance
|
|
)
|
|
internal
|
|
nonReentrant
|
|
returns (uint withdrawed)
|
|
{
|
|
require(sharesAmount > 0, "Zerro shares");
|
|
require(IERC20(betShareToken).balanceOf(msg.sender) >= sharesAmount, "not enough shares");
|
|
|
|
uint totalShares = IERC20(betShareToken).totalSupply();
|
|
uint amount = balance * sharesAmount / totalShares;
|
|
|
|
require(IERC20(paymentToken).balanceOf(address(this)) >= amount, "exceeds erc20");
|
|
require(amount > 0, "zerro withdraw");
|
|
|
|
uint userAmount = amount;
|
|
|
|
|
|
if(platformFeePercent > 0){
|
|
uint platformFee = amount / totalFeePercent * platformFeePercent;
|
|
userAmount = userAmount - platformFee;
|
|
IERC20(paymentToken).safeTransfer(platformWallet, platformFee);
|
|
}
|
|
|
|
|
|
if(creatorFeePercent > 0){
|
|
uint creatorFee = amount / totalFeePercent * creatorFeePercent;
|
|
userAmount = userAmount - creatorFee;
|
|
IERC20(paymentToken).safeTransfer(creator, creatorFee);
|
|
}
|
|
|
|
|
|
IERC20(paymentToken).safeTransfer(msg.sender, userAmount);
|
|
|
|
|
|
IMintBurn(betShareToken).burn(msg.sender, sharesAmount);
|
|
|
|
withdrawed = amount;
|
|
}
|
|
|
|
|
|
function _bet(address betShareToken, uint tokenAmount, uint totalDeposited) internal {
|
|
require(block.timestamp < unlockTime, "locked");
|
|
require(initialPrice > 0, "require initial price");
|
|
require(tokenAmount > 0, "wrong bet fee");
|
|
|
|
|
|
IMintBurn(betShareToken).mint(
|
|
msg.sender,
|
|
_calculateSharesToMint(
|
|
tokenAmount,
|
|
IERC20(betShareToken).totalSupply(),
|
|
totalDeposited
|
|
)
|
|
);
|
|
}
|
|
|
|
|
|
function _calculateSharesToMint(uint tokenAmount, uint sharesSupply, uint totalDeposited)
|
|
internal
|
|
view
|
|
returns(uint)
|
|
{
|
|
if (sharesSupply == 0)
|
|
return INITIAL_SHARES;
|
|
|
|
return tokenAmount * sharesSupply / totalDeposited;
|
|
}
|
|
|
|
|
|
function winnerBalance(address account) external view returns(uint){
|
|
uint _finalPrice = finalPrice > 0
|
|
? finalPrice
|
|
: getPrice();
|
|
|
|
address token = _finalPrice > initialPrice
|
|
? betYesToken
|
|
: betNoToken;
|
|
|
|
return IERC20(token).balanceOf(account);
|
|
}
|
|
}
|
|
|
|
|
|
contract BetFactory is Ownable {
|
|
address public platformWallet;
|
|
uint public platformFee = 10;
|
|
uint public creatorFee = 10;
|
|
|
|
uint public constant totalFeePercent = 1000;
|
|
|
|
mapping(address => address) public latestBetPerSender;
|
|
mapping (address => bool) public isETHBased;
|
|
address[] public bets;
|
|
|
|
constructor(address _platformWallet){
|
|
platformWallet = _platformWallet;
|
|
}
|
|
|
|
|
|
function createETHBet(
|
|
uint _lockTime,
|
|
address[] memory _tokenPath,
|
|
string memory _name,
|
|
address _v2Router,
|
|
uint _initialPrice,
|
|
uint _oneTokenInWei
|
|
)
|
|
external
|
|
returns (address)
|
|
{
|
|
address bet = address(
|
|
new BetETH(
|
|
_lockTime,
|
|
_tokenPath,
|
|
_name,
|
|
_v2Router,
|
|
_initialPrice,
|
|
_oneTokenInWei,
|
|
platformFee,
|
|
creatorFee,
|
|
platformWallet,
|
|
msg.sender
|
|
)
|
|
);
|
|
latestBetPerSender[msg.sender] = bet;
|
|
bets.push(bet);
|
|
|
|
isETHBased[bet] = true;
|
|
|
|
return bet;
|
|
}
|
|
|
|
function createERC20Bet(
|
|
uint _lockTime,
|
|
address[] memory _tokenPath,
|
|
string memory _name,
|
|
address _v2Router,
|
|
uint _initialPrice,
|
|
uint _oneTokenInWei,
|
|
address _paymentToken
|
|
)
|
|
external
|
|
returns (address)
|
|
{
|
|
address bet = address(
|
|
new BetERC20(
|
|
_lockTime,
|
|
_tokenPath,
|
|
_name,
|
|
_v2Router,
|
|
_initialPrice,
|
|
_oneTokenInWei,
|
|
_paymentToken,
|
|
platformFee,
|
|
creatorFee,
|
|
platformWallet,
|
|
msg.sender
|
|
)
|
|
);
|
|
|
|
latestBetPerSender[msg.sender] = bet;
|
|
bets.push(bet);
|
|
|
|
return bet;
|
|
}
|
|
|
|
function getAllBetsAddresses() external view returns(address[] memory){
|
|
return bets;
|
|
}
|
|
|
|
function getAllBetsLength() external view returns(uint){
|
|
return bets.length;
|
|
}
|
|
|
|
|
|
|
|
function updatePlatformFee(uint _platformFee) external onlyOwner {
|
|
require(platformFee < totalFeePercent / 10, "Fee overflow");
|
|
platformFee = _platformFee;
|
|
}
|
|
|
|
function updateCreatorFee(uint _creatorFee) external onlyOwner {
|
|
require(creatorFee < totalFeePercent / 10, "Fee overflow");
|
|
creatorFee = _creatorFee;
|
|
}
|
|
|
|
function updatePlatformWallet(address _platformWallet) external onlyOwner {
|
|
platformWallet = _platformWallet;
|
|
}
|
|
} |