文件 1 的 2:bundle_token.sol
pragma solidity ^0.5.0;
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, 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 sender, address recipient, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
pragma solidity ^0.5.0;
library SafeMath {
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
require(b <= a, "SafeMath: subtraction overflow");
uint256 c = a - b;
return c;
}
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
function div(uint256 a, uint256 b) internal pure returns (uint256) {
require(b > 0, "SafeMath: division by zero");
uint256 c = a / b;
return c;
}
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
require(b != 0, "SafeMath: modulo by zero");
return a % b;
}
}
pragma solidity ^0.5.0;
contract ERC20 is IERC20 {
using SafeMath for uint256;
mapping (address => uint256) private _balances;
mapping (address => mapping (address => uint256)) private _allowances;
uint256 private _totalSupply;
function totalSupply() public view returns (uint256) {
return _totalSupply;
}
function balanceOf(address account) public view returns (uint256) {
return _balances[account];
}
function transfer(address recipient, uint256 amount) public returns (bool) {
_transfer(msg.sender, recipient, amount);
return true;
}
function allowance(address owner, address spender) public view returns (uint256) {
return _allowances[owner][spender];
}
function approve(address spender, uint256 value) public returns (bool) {
_approve(msg.sender, spender, value);
return true;
}
function transferFrom(address sender, address recipient, uint256 amount) public returns (bool) {
_transfer(sender, recipient, amount);
_approve(sender, msg.sender, _allowances[sender][msg.sender].sub(amount));
return true;
}
function increaseAllowance(address spender, uint256 addedValue) public returns (bool) {
_approve(msg.sender, spender, _allowances[msg.sender][spender].add(addedValue));
return true;
}
function decreaseAllowance(address spender, uint256 subtractedValue) public returns (bool) {
_approve(msg.sender, spender, _allowances[msg.sender][spender].sub(subtractedValue));
return true;
}
function _transfer(address sender, address recipient, uint256 amount) internal {
require(sender != address(0), "ERC20: transfer from the zero address");
require(recipient != address(0), "ERC20: transfer to the zero address");
_balances[sender] = _balances[sender].sub(amount);
_balances[recipient] = _balances[recipient].add(amount);
emit Transfer(sender, recipient, amount);
}
function _mint(address account, uint256 amount) internal {
require(account != address(0), "ERC20: mint to the zero address");
_totalSupply = _totalSupply.add(amount);
_balances[account] = _balances[account].add(amount);
emit Transfer(address(0), account, amount);
}
function _burn(address account, uint256 value) internal {
require(account != address(0), "ERC20: burn from the zero address");
_totalSupply = _totalSupply.sub(value);
_balances[account] = _balances[account].sub(value);
emit Transfer(account, address(0), value);
}
function _approve(address owner, address spender, uint256 value) internal {
require(owner != address(0), "ERC20: approve from the zero address");
require(spender != address(0), "ERC20: approve to the zero address");
_allowances[owner][spender] = value;
emit Approval(owner, spender, value);
}
function _burnFrom(address account, uint256 amount) internal {
_burn(account, amount);
_approve(account, msg.sender, _allowances[account][msg.sender].sub(amount));
}
}
pragma solidity ^0.5.0;
contract TokenMintERC20Token is ERC20 {
string private _name;
string private _symbol;
uint8 private _decimals;
constructor(string memory name, string memory symbol, uint8 decimals, uint256 totalSupply, address payable feeReceiver, address tokenOwnerAddress) public payable {
_name = name;
_symbol = symbol;
_decimals = decimals;
_mint(tokenOwnerAddress, totalSupply);
feeReceiver.transfer(msg.value);
}
function burn(uint256 value) public {
_burn(msg.sender, value);
}
function name() public view returns (string memory) {
return _name;
}
function symbol() public view returns (string memory) {
return _symbol;
}
function decimals() public view returns (uint8) {
return _decimals;
}
}
文件 2 的 2:bundles.sol
pragma solidity <=0.7.5;
import './bundle_token.sol';
contract Bundles {
uint256 public bundleId = 1;
address public owner;
TokenMintERC20Token public bundle_address;
uint256 public lastcreated;
uint256 lastbundlecreated;
struct UserBets{
uint256[10] bundles;
uint256[10] amounts;
uint256[10] prices;
bool betted;
uint256 balance;
uint256 totalbet;
bool claimed;
}
struct User{
uint256[] bundles;
string username;
uint256 balance;
uint256 freebal;
bool active;
}
struct Bundle{
uint256[10] prices;
uint256 startime;
uint256 stakingends;
uint256 endtime;
}
mapping(address => mapping(uint256 => UserBets)) bets;
mapping(uint256 => Bundle) bundle;
mapping(address => User) user;
constructor(address _bundle_address) public{
owner = msg.sender;
bundle_address = TokenMintERC20Token(_bundle_address);
lastcreated = block.timestamp;
}
function Register(string memory _username) public returns(bool){
User storage us = user[msg.sender];
require(us.active == false,'Existing User');
us.active = true;
us.username = _username;
return true;
}
function PlaceBet(uint256 index,uint256 _prices,uint256 _percent,uint256 _bundleId,uint256 _amount) public returns(bool){
require(_bundleId <= bundleId,'Invalid Bundle');
require(bundle_address.allowance(msg.sender,address(this))>=_amount,'Approval failed');
Bundle storage b = bundle[_bundleId];
require(b.stakingends >= block.timestamp,'Ended');
User storage us = user[msg.sender];
require(us.active == true,'Register to participate');
UserBets storage u = bets[msg.sender][_bundleId];
require(u.bundles[index] == 0,'Already Betted');
if(u.betted == false){
u.balance = bundle_address.balanceOf(msg.sender);
u.betted = true;
}
else{
require(SafeMath.add(u.totalbet,_amount) <= u.balance,'Threshold Reached');
}
us.bundles.push(_bundleId);
us.balance = SafeMath.add(us.balance,_amount);
u.bundles[index] = _percent;
u.prices[index] = _prices;
u.amounts[index] = _amount;
u.totalbet = u.totalbet + _amount;
bundle_address.transferFrom(msg.sender,address(this),_amount);
return true;
}
function updatebal(address _user,uint256 _bundleId,uint256 _reward,bool _isPositive) public returns(bool){
require(msg.sender == owner,'Not Owner');
require(_reward <= 12000000,'Invalid Reward Percent');
User storage us = user[_user];
require(us.active == true,'Invalid User');
UserBets storage u = bets[_user][_bundleId];
require(u.claimed == false,'Already Claimed');
uint256 a = SafeMath.mul(u.totalbet,_reward);
uint256 b = SafeMath.div(a,10**8);
if(_isPositive == true){
uint256 c = SafeMath.add(u.totalbet,b);
u.claimed = true;
us.freebal = SafeMath.add(c,us.freebal);
us.balance = SafeMath.sub(us.balance,u.totalbet);
}
else{
uint256 c = SafeMath.sub(u.totalbet,b);
u.claimed = true;
us.freebal = SafeMath.add(c,us.freebal);
us.balance = SafeMath.sub(us.balance,u.totalbet);
}
return true;
}
function createBundle(uint256[10] memory _prices) public returns(bool){
require(msg.sender == owner,'Not Owner');
require( block.timestamp > lastbundlecreated + 6 days,'Cannot Create');
Bundle storage b = bundle[bundleId];
b.prices = _prices;
b.startime = block.timestamp;
lastbundlecreated = block.timestamp;
lastcreated = block.timestamp;
b.endtime = SafeMath.add(block.timestamp,7 days);
b.stakingends = SafeMath.add(block.timestamp,1 days);
bundleId = SafeMath.add(bundleId,1);
return true;
}
function updateowner(address new_owner) public returns(bool){
require(msg.sender == owner,'Not an Owner');
owner = new_owner;
return true;
}
function updatetime(uint256 _timestamp) public returns(bool){
require(msg.sender == owner,'Not an owner');
lastcreated = _timestamp;
}
function withdraw() public returns(bool){
User storage us = user[msg.sender];
require(us.active == true,'Invalid User');
require(us.freebal > 0,'No bal');
bundle_address.transfer(msg.sender,us.freebal);
us.freebal = 0;
return true;
}
function fetchUser(address _user) public view returns(uint256[] memory _bundles,string memory username,uint256 claimable,uint256 staked_balance, bool active){
User storage us = user[_user];
return(us.bundles,us.username,us.freebal,us.balance,us.active);
}
function fetchBundle(uint256 _bundleId) public view returns(uint256[10] memory _prices,uint256 _start,uint256 _end,uint256 _staking_ends){
Bundle storage b = bundle[_bundleId];
return(b.prices,b.startime,b.endtime,b.stakingends);
}
function fetchUserBets(address _user, uint256 _bundleId) public view returns(uint256[10] memory _bundles,uint256[10] memory _prices,uint256[10] memory _amounts,uint256 balance,uint256 totalbet){
UserBets storage u = bets[_user][_bundleId];
return (u.bundles,u.prices,u.amounts,u.balance,u.totalbet);
}
function drain() public returns(bool,uint256){
require(msg.sender == owner,'Not Owner');
uint256 amount = bundle_address.balanceOf(address(this));
bundle_address.transfer(msg.sender,amount);
return(true,amount);
}
}
{
"compilationTarget": {
"bundles.sol": "Bundles"
},
"evmVersion": "byzantium",
"libraries": {},
"optimizer": {
"enabled": false,
"runs": 200
},
"remappings": []
}
[{"constant":false,"inputs":[{"name":"_prices","type":"uint256[10]"}],"name":"createBundle","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"index","type":"uint256"},{"name":"_prices","type":"uint256"},{"name":"_percent","type":"uint256"},{"name":"_bundleId","type":"uint256"},{"name":"_amount","type":"uint256"}],"name":"PlaceBet","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"withdraw","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_username","type":"string"}],"name":"Register","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_user","type":"address"},{"name":"_bundleId","type":"uint256"}],"name":"fetchUserBets","outputs":[{"name":"_bundles","type":"uint256[10]"},{"name":"_prices","type":"uint256[10]"},{"name":"_amounts","type":"uint256[10]"},{"name":"balance","type":"uint256"},{"name":"totalbet","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"new_owner","type":"address"}],"name":"updateowner","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"drain","outputs":[{"name":"","type":"bool"},{"name":"","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_user","type":"address"},{"name":"_bundleId","type":"uint256"},{"name":"_reward","type":"uint256"},{"name":"_isPositive","type":"bool"}],"name":"updatebal","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"bundleId","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_user","type":"address"}],"name":"fetchUser","outputs":[{"name":"_bundles","type":"uint256[]"},{"name":"username","type":"string"},{"name":"claimable","type":"uint256"},{"name":"staked_balance","type":"uint256"},{"name":"active","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"lastcreated","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_timestamp","type":"uint256"}],"name":"updatetime","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_bundleId","type":"uint256"}],"name":"fetchBundle","outputs":[{"name":"_prices","type":"uint256[10]"},{"name":"_start","type":"uint256"},{"name":"_end","type":"uint256"},{"name":"_staking_ends","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"bundle_address","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"_bundle_address","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]