Last active
November 30, 2022 06:14
-
-
Save lag945/e20512d50a9822063308e8fceb47e940 to your computer and use it in GitHub Desktop.
Web3 Taiwan Lottery Smart Contract
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.25; | |
contract TaiwanLottery { | |
//參考範例 https://ithelp.ithome.com.tw/articles/10205760 | |
//賭注資料結構 | |
struct BettingData | |
{ | |
address Player; | |
uint8 A1; | |
uint8 A2; | |
uint8 A3; | |
uint8 A4; | |
uint8 A5; | |
uint8 A6; | |
uint8 B1; | |
} | |
//https://ethereum.stackexchange.com/questions/1038/is-there-a-theoretical-limit-for-amount-of-data-that-a-contract-can-store | |
//儲存上限1.46GB | |
BettingData[] private m_Datas; | |
// 管理者 | |
address private m_Owner; | |
uint private m_StartTime; | |
uint private m_EndTime; | |
// 狀態 | |
enum State { Started, Ended, Processing } | |
State private m_State; | |
//中獎通知 | |
event CongratsNotice(address addr, uint amount); | |
//一注至少0.1 ether | |
modifier OverMinPrice() { require(msg.value != 0 && msg.value >= 0.1 ether); _; } | |
modifier Started() { require(m_State == State.Started); _; } | |
modifier Ended() { require(m_State == State.Ended); _; } | |
modifier Owner() { require(msg.sender == m_Owner); _; } | |
modifier Alive() { require(now >= m_StartTime && now < m_EndTime); _; } | |
modifier Dead() { require(now >= m_EndTime); _; } | |
//建構子 | |
constructor() public { | |
m_Owner = msg.sender; | |
m_State = State.Ended; | |
} | |
//開始新一期樂透 | |
function StartLottery() Ended public returns (bool Ret) | |
{ | |
Ret = false; | |
//驗證 unix-time https://www.epochconverter.com/ | |
uint st = 1544698800;//2018年12月13日星期四 19:00:00 GMT+08:00 | |
uint n = 0; | |
while(st<now){ | |
m_StartTime = st; | |
if(n%2==0){ | |
st += 4 days; | |
} | |
else{ | |
st += 3 days; | |
} | |
n++; | |
} | |
m_EndTime = st; | |
m_State = State.Started; | |
Ret = true; | |
return Ret; | |
} | |
//開獎 | |
function DoLottery(uint8 A1,uint8 A2,uint8 A3,uint8 A4,uint8 A5,uint8 A6,uint8 B1) payable Started Owner /*Dead*/ public returns(bool Ret) { | |
//懶得排序了外面自己排好 A:01-38 B:01-08 | |
require(A1 >= 1 && A2 > A1 && A3 > A2 && A4 >A3 && A5>A4 && A6 > A5 && A6 <=38 && B1>=1 && B1<=8); | |
Ret = false; | |
for(uint i=0;i<m_Datas.length;i++){ | |
if(A1 == m_Datas[i].A1 && A2 == m_Datas[i].A2 && A3 == m_Datas[i].A3 && A4 == m_Datas[i].A4 && A5 == m_Datas[i].A5 && A6 == m_Datas[i].A6 && B1 == m_Datas[i].B1){ | |
uint amount = address(this).balance; | |
address user = m_Datas[i].Player; | |
m_Datas[i].Player.transfer(address(this).balance); | |
emit CongratsNotice(user, amount); | |
break; | |
} | |
} | |
m_Datas.length = 0; | |
m_State = State.Ended; | |
Ret = true; | |
return Ret; | |
} | |
//下注 | |
function Betting (uint8 A1,uint8 A2,uint8 A3,uint8 A4,uint8 A5,uint8 A6,uint8 B1) payable Started Alive OverMinPrice public returns(bool Ret) { | |
//懶得排序了外面自己排好 A:01-38 B:01-08 | |
require(A1 >= 1 && A2 > A1 && A3 > A2 && A4 >A3 && A5>A4 && A6 > A5 && A6 <=38 && B1>=1 && B1<=8); | |
Ret = false; | |
BettingData memory data=BettingData(msg.sender,A1,A2,A3,A4,A5,A6,B1); | |
m_Datas.push(data); | |
return true; | |
} | |
//取得下注資訊 | |
function GetData (uint Index) public view returns(address Player,uint8 A1,uint8 A2,uint8 A3, uint8 A4, uint8 A5, uint8 A6, uint8 B1) { | |
Player = m_Datas[Index].Player; | |
A1 = m_Datas[Index].A1; | |
A2 = m_Datas[Index].A2; | |
A3 = m_Datas[Index].A3; | |
A4 = m_Datas[Index].A4; | |
A5 = m_Datas[Index].A5; | |
A6 = m_Datas[Index].A6; | |
B1 = m_Datas[Index].B1; | |
return (Player,A1,A2,A3,A4,A5,A6,B1); | |
} | |
//取得全部下注數 | |
function GetDataSize () public view returns(uint Size) { | |
Size = m_Datas.length; | |
return Size; | |
} | |
//取得總賭注,單位wei | |
function GetBalance () public view returns(uint Balance) { | |
Balance = address(this).balance; | |
return Balance; | |
} | |
//樂透開始時間 | |
function GetStartTime() public view returns (uint Ret) { | |
return (m_StartTime); | |
} | |
//熱透結束時間 | |
function GetEndTime() public view returns (uint Ret) { | |
return (m_EndTime); | |
} | |
//做慈善 | |
function DoCharity() public payable returns (address Address, uint Value) { | |
//把餘額轉給合約主人或指定位置 | |
Value = address(this).balance; | |
//Address = 0x14723a09acff6d2a60dcdf7aa4aff308fddc160c; | |
Address = m_Owner; | |
Address.transfer(Value); | |
return (Address,Value); | |
} | |
//取得目前狀態 | |
function GetState() public view returns (string Ret){ | |
if(m_State==State.Started) { | |
return "Started"; | |
} | |
else if(m_State==State.Ended) { | |
return "Ended"; | |
} | |
else if(m_State==State.Processing) { | |
return "Processing"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment