# Loe Dei – Tron Smart Contract Example

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 {
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);

enum BetType { Lo, De }

//Construction
constructor () public //creation settings
{
_owner = msg.sender;
minBet=1;               //1 trx
maxBet=1000;            //1000 trx
Mutexlock=false;
}
{
//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;
{
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);
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;
{
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);
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);
}

}
{
if (_affiliate != msg.sender)
_affiliate.transfer(_amount);
}

//Select one of 27 random number as random factor
uint randomfactor = now%27;
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;
}

return _owner;
}
function getOwnerBalance() public constant returns(uint256 _balance) {
return _owner.balance;
}
function getContractBalance() public constant returns(uint256 _contractBalance) {
}
//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');