Contract item address github
Data logic structures are separate, base data structure is fixed, and external logic update does not affect data contract.
struct LotteryRule {
uint startTime; // Lottery starting time stamp
uint endTime; // Lottery ending time stamp
uint daysStartTime; // Daily lottery starting time. 0 for no limit
uint daysEndTime; // Daily lottery ending time. 0 for no limit
uint participateCnt; // Lottery drawing times limit. 0 for no limit
uint perAddressPartCnt; // Drawing times of each address. 0 for no limit
}
The parameters of new lottery can be user-defined lottery rules according to the needs
| Prize Name | Prizewinning Probability | Prizewinning Array | -------- | -------- | -------- | --------------- | | Prize A | 1% | 100 | [0] | | Prize B | 2% | 50 | [1, 2] | | Prize C | 5% | 20 | [3, 4, 5, 6, 7] |
As above: The lowest common multiple of prizewinning probability reciprocal of three prizes is 100, and divided by probability reciprocal of each prize. The result is used as the length of prizewinning array. The values of the results increase in sequence.
When the user draws the lottery, uint value in uint(keccak256(combining string (blockhash of last block + msg.sender +global auto-incrementing index)))
at the time of drawing is the random value. Modulo operation is carried out for the above calculated lowest common multiple to get random value. If random value is from the above prizewinning array, it means the successful drawing of the corresponding prize.
hash relies on the hash of last block when the transaction of random value is created. ThunderChain's transaction confirmation is second-level confirmation, but will not immediately synchronize with external transaction record so as to ensure hash of last block transaction will not be for malicious use.
Contract item address github
The contract achieves main functions of lottery jackpot model, and mainly includes two contracts, namely LotteryControl control contract and LotteryData data contract. Control contract provides the entry for the developers to access the contract, and processes data contract and saves relevant data.
Deploy LotteryData contract
Deploy LotteryControl contract
Call setLotteryControl method of LotteryData contract, and set LotteryControl contract address
The developer or user carries out contract interaction through calling LotteryControl contract method.
The developer calls newLottery method to create lottery. The method returns event ID, and the developer should save event ID. Subsequent contract method call requires the input of the lottery ID;
After creation, call setLotteryConfig to set event rules. The developer can input parameters for rule configuration according to the needs. The description of configured parameters is as follows.
// Set lottery configuration
function setLotteryConfig (
uint lotteryID, // lottery ID
uint startTime, // event starting time, Unix time stamp
uint endTime, // event ending time, Unix time stamp
bool dayLimit, // whether to set daily lottery drawing time
uint dayStartTime, // daily lottery starting time
uint dayEndTime, // daily lottery ending time
int timesPerItem, // participant lottery drawing times limit
uint amountPerAction, // lottery amount per time unit:LinkToken
uint openCondition, // open condition. The lottery mode is 0:jackpot mode. When the jackpot amount reaches prescribed amount, the lottery will be open (unit: LinkToken) 1: opening time mode. Lottery opening at prescribed time (Unix time stamp) 2: address mode. When the participating user account addresses reach prescribed number, the lottery will be open
uint copies // how many equal shares of the prize to be distributed
)
After setLotteryConfig is called, lottery automatically takes effect.
The user participates in lottery by calling joinLottery method. If the user meets the lottery rules, it will be successful participation. The user can check lottery result after the lottery is open;
fixed jackpot amount and participating user number mode
When the lottery meets the opening conditions, the contract internally triggers OpenLottery, and the developer can monitor the to judge that lottery opening can be carried out and call openLottery to open the lottery. If the lottery ends, and the monitoring event does not happen, the developer can directly call openLottery to open the lottery.
Fixed lottery opening time mode
When the lottery opening time comes, the developer calls openLottery to open the lottery.
The lottery finally has the following two statuses after lottery opening. The developer or user can call queryLotteryResult method to check the result.
All the LinkToken in the jackpot is equally divided into N parts (the developer can revise contract parameter configuration), and each award will be randomly distributed to lottery participating users successively. One user can get several awards.