Please follow and like us:

This is the Tron smart contract written in Solidity for the game Loe Dei. I hope this will bring transparency to my betting dapp. Please try to play it at https://www.tron2moon.com/loedei

This code is for educational purposes, if you have any question or need smart contract service, please ask.

pragma solidity ^0.4.23;
 contract Lode1102 {
     address _owner;
     uint32 maxBet;
     uint32 minBet; 
     uint constant decimals = 6;        //ether is 18 and sun is 6
     uint8[] public ranNumbers;
     bool Mutexlock;
     //Events
     event RandomNumber(uint256 _rand,uint8[] array27);
     event LoseBet(address _address,uint8 _two_digit_no,uint256 _betTRX,BetType _betType,uint8[] array27);
     event Rewarded(address _address,uint8 _two_digit_no,uint256 _reward,uint256 _betTRX,BetType _betType,uint8[] array27);
     event ErrorSendReward(address _address,uint8 _two_digit_no,uint256 _reward,uint256 _betTRX,BetType _betType,uint8[] array27);
     event PotHasNoFund(address _address,uint8 _two_digit_no,uint256 _reward,uint256 _betTRX,BetType _betType,uint8[] array27);
     event SentAllBalanceToWinner(address _address,uint8 _two_digit_no,uint256 _reward,uint256 _betTRX,BetType _betType,uint8[] array27);

enum BetType { Lo, De }

//Construction
constructor () public //creation settings
{ 
    _owner = msg.sender;
    minBet=1;               //1 trx
    maxBet=1000;            //1000 trx
    Mutexlock=false;
}
function addNewBet(BetType _type,address _affiliate) payable public returns(uint8 winloseerror,uint8 _two_digit_no,uint256 _reward)
{
    //1 is win M
    //2 is all pool
    //0 is lose
    require(!Mutexlock,'MutexLock is false');
    require(msg.value >= minBet*10**uint256(decimals),'minBet required');
    require(msg.value <= maxBet*10**uint256(decimals),'maxBet required');
    require((_type == BetType.Lo)||(_type == BetType.De),'BetType 0 or 1 required');

    uint8 two_digit_no = random1(msg.sender);

    uint wincount = 0;
    bool firstNumber = false;
    for (uint j=0;j<27;j++)
    {
      if (two_digit_no == ranNumbers[j])
      {
            if (j==0)
                firstNumber = true;

            wincount++;
      }
    }
    uint256 reward = 0;
    uint balance= 0;
    if ((_type == BetType.De) && (firstNumber))
    {
        reward = msg.value * 70;
        if (reward<address(this).balance)
        {
            msg.sender.transfer(reward);
            emit Rewarded(msg.sender,two_digit_no,reward,msg.value,_type,ranNumbers);
            _owner.transfer(msg.value * 10 /100);
            sendAffiliate(_affiliate,msg.value * 10 /100);
            return (1,two_digit_no,reward);
        }
        else{
            emit PotHasNoFund(msg.sender,two_digit_no,reward,msg.value,_type,ranNumbers);
            balance = address(this).balance;
            msg.sender.transfer(balance);
            emit SentAllBalanceToWinner(msg.sender,two_digit_no,balance,msg.value,_type,ranNumbers);
            return (2,two_digit_no,balance);

        }  

    }
    else if ((_type == BetType.Lo) && (wincount>0)){
        reward = msg.value * 3;
        if (reward<address(this).balance)
        {
            msg.sender.transfer(reward);
            emit Rewarded(msg.sender,two_digit_no,reward,msg.value,_type,ranNumbers);
            _owner.transfer(msg.value * 10 /100);
            sendAffiliate(_affiliate,msg.value * 10 /100);
            return (1,two_digit_no,reward);
        }
        else{
            emit PotHasNoFund(msg.sender,two_digit_no,reward,msg.value,_type,ranNumbers);
            balance = address(this).balance;
            msg.sender.transfer(balance);
            emit SentAllBalanceToWinner(msg.sender,two_digit_no,balance,msg.value,_type,ranNumbers);
            return (2,two_digit_no,balance);
        }
    }
    else
    {
        emit LoseBet(msg.sender,two_digit_no,msg.value,_type,ranNumbers);
        _owner.transfer(msg.value * 10 /100);
        sendAffiliate(_affiliate,msg.value * 10 /100);
        return (0,two_digit_no,0);
    }


}
function sendAffiliate(address _affiliate,uint256 _amount) internal
{
    if (_affiliate != msg.sender)
        _affiliate.transfer(_amount);
}

function random1(address _add) internal view returns(uint8) {
    //Select one of 27 random number as random factor
    uint randomfactor = now%27;
    uint256 randno = uint256(keccak256(abi.encodePacked(block.timestamp, _add,ranNumbers[randomfactor])));
    return uint8((randno>>(now%8))%100);
}
function random27() public {
    //require(Mutexlock);
    require (msg.sender == _owner,'Not Owner');
    Mutexlock = true;
    delete ranNumbers;
    uint256 randno = uint256(keccak256(abi.encodePacked(block.timestamp, now%256)));

    for (uint8 i=0;i<27;i++)
    {
        ranNumbers.push(uint8(uint256(randno>>(i*2))%100) );
    }
    emit RandomNumber(randno,ranNumbers);
    Mutexlock = false;
}

function getRandomNumbers() public constant returns (uint8[] _randNumbers){
    return ranNumbers;
}

function getOwner() public constant returns(address _oAddress) {
    return _owner;
}
function getOwnerBalance() public constant returns(uint256 _balance) {
    return _owner.balance;
}
function getContractBalance() public constant returns(uint256 _contractBalance) {    
    return address(this).balance;
}
//Change min max Bet
function changeParas(uint32 _minBet, uint32 _maxBet) public {
    require (msg.sender == _owner,'Not Owner');
    minBet=_minBet;               //1 trx
    maxBet=_maxBet;            //1000 trx

}
//Protect the pool in case of hacking
function kill() public {
    require (msg.sender == _owner,'Not Owner');
    _owner.transfer(address(this).balance);
    selfdestruct(_owner);
}
}

Please follow and like us:
Loe Dei – Tron Smart Contract Example
Tagged on:                         

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!