Skip to content

Instantly share code, notes, and snippets.

@andreystarkov
Created June 26, 2018 10:08
Show Gist options
  • Save andreystarkov/39a7f1eebcdf8b97ffb862242450bc18 to your computer and use it in GitHub Desktop.
Save andreystarkov/39a7f1eebcdf8b97ffb862242450bc18 to your computer and use it in GitHub Desktop.
import { put, call, select, fork, all } from 'redux-saga/effects'
import { delay } from 'redux-saga'
import TicketsActions from 'Redux/TicketsRedux'
import LotteryActions from 'Redux/LotteryRedux'
import { callContract, callERC20 } from 'Services/Eth'
import { selectCountOfDraws, selectDrawsPage, selectDrawsLoading } from 'Utils/Selectors'
import { prepareTicketNumbers } from 'Utils/Tickets'
import { splitTransactions } from 'Utils/Transactions'
import { CONTRACT_ADDRESS } from 'Constants/Eth'
import { LOTTERY_DRAWS_PER_PAGE, LOTTERY_TICKETS_PER_PAGE } from 'Constants'
const MAX_STATS_ENTRIES = 10
export async function getWinTicketDraws (drawId) {
const winTicketDraws = await window.contractLottery.methods.listWinTicketDraws(drawId).call()
return winTicketDraws
}
export function * refreshLottery (isStartup) {
yield call(getCountOfDraws)
yield all([
yield fork(getLotteryDrawsPage),
yield fork(getLotteryStatus),
yield fork(getLotteryTicketsPage),
yield fork(getWinTicketChoosen),
yield fork(getSellOverBlock),
yield fork(getLotteryWinNumbers),
yield fork(getTicketPrice),
yield fork(getCountOfDraws),
yield fork(getJackpot),
yield fork(getTotalWinners)
])
}
export function * getTicketPrice () {
const ticketPrice = yield call(callContract, 'priceTicket', 'bet')
yield put(LotteryActions.lotteryUpdate({ ticketPrice }))
}
export function * getWinTicketChoosen () {
const isWinTicket = yield call(callContract, 'winTicketChoosen')
yield put(LotteryActions.lotteryUpdate({ isWinTicket }))
}
export function * getLotteryWinNumbers () {
const winNumbers = yield call(callContract, 'winTicket')
yield put(LotteryActions.lotteryUpdate({ winNumbers }))
}
export function * getCountOfDraws () {
const countOfDraws = yield call(callContract, 'countOfDraws', 'int')
yield put(LotteryActions.lotteryUpdate({ countOfDraws }))
}
export function * getTotalWinners () {
let totalWinners = yield call(callContract, 'totalWinners', 'int')
yield put(LotteryActions.lotteryUpdate({ totalWinners }))
for (let i = 0; i < totalWinners && i <= MAX_STATS_ENTRIES; i++) {
let reward = yield call(callContract, 'listUsersRewards', 'bet', i)
yield put(LotteryActions.lotteryWinnersAdd({ id: i + 1, reward }))
}
}
// export function * getLotteryTickets (lotteryId) {
// let lotteryTickets = []
// let ticketsDraws = yield call(callContract, 'listTicketsDraws', 'int', lotteryId)
// for (let v = 1; v < ticketsDraws && v <= MAX_STATS_ENTRIES; v++) {
// let lotteryTicket = yield call(callContract, 'drawsTickets', false, lotteryId, v)
// lotteryTickets.push(lotteryTicket)
// }
// const tickets = { [lotteryId]: lotteryTickets }
// // console.log('getLotteryTickets', { lotteryId, ticketsDraws, tickets })
// // yield put(LotteryActions.lotteryTicketsAdd(tickets))
// }
export function * getLotteryTicketsPage () {
let counter = 0
let ticketsMap = yield select(state => state.lottery.lotteryTicketsMap)
let lotteryTicketsMap = ticketsMap.asMutable()
const countOfDraws = yield select(selectCountOfDraws)
const page = yield select(state => state.lottery.lotteryTicketsPage)
const isLoading = yield select(state => state.lottery.lotteryTicketsLoading)
const prevLotteryTickets = yield select(state => state.lottery.lotteryTickets)
const lastTicket = prevLotteryTickets && prevLotteryTickets[prevLotteryTickets.length - 1]
let lastPosition = lastTicket ? lastTicket.position : false
let lastDraw = lastTicket && lastTicket.drawId ? lastTicket.drawId : countOfDraws
// console.log({ prevLotteryTickets, lastTicket, lastPosition, lastDraw, lastDrawTotal })
if (lastPosition === 0) {
lastDraw = lastDraw - 1
lastPosition = false
}
if (countOfDraws > 0 && !isLoading) {
yield put(LotteryActions.lotteryUpdate({ lotteryTicketsLoading: true }))
for (let drawId = lastDraw; drawId >= 1; drawId--) {
let ticketsDraws = yield call(callContract, 'listTicketsDraws', 'int', drawId)
lotteryTicketsMap[drawId] = ticketsDraws
let entryPoint = lastPosition || ticketsDraws
entryPoint = entryPoint - 1
// console.log({ drawId, entryPoint, ticketsDraws, lastPosition, lastDraw })
if (entryPoint > -1) {
for (let v = entryPoint; v >= 0; v--) {
// console.log({ v, lastPosition, ticketsDraws })
if (counter < LOTTERY_TICKETS_PER_PAGE) {
let numbers = yield call(callContract, 'drawsTickets', false, drawId, v)
const ticket = {
combination: prepareTicketNumbers(numbers),
numbers,
drawId,
id: `${drawId}-${v}`,
position: v
}
yield put(LotteryActions.lotteryTicketsAdd(ticket))
lastPosition = false
}
counter++
}
}
}
let lotteryTicketsTotal = 0
Object.keys(lotteryTicketsMap).map(e => { lotteryTicketsTotal += parseInt(lotteryTicketsMap[e]) })
console.group('Fetching lottery tickets page #' + (page + 1))
const lotteryUpdates = {
lotteryTicketsPage: page + 1,
lotteryTicketsLoading: false,
lotteryTicketsMap,
lotteryTicketsTotal
}
console.log({ page, lotteryUpdates, countOfDraws, lotteryTicketsTotal, counter, lotteryTicketsMap })
console.groupEnd()
yield put(LotteryActions.lotteryUpdate(lotteryUpdates))
}
}
export function * getLotteryDrawsPage () {
const countOfDraws = yield select(selectCountOfDraws)
const page = yield select(selectDrawsPage)
const isLoading = yield select(selectDrawsLoading)
if (countOfDraws > 0 && !isLoading) {
yield put(LotteryActions.lotteryUpdate({ lastDrawsLoading: true }))
let startId = countOfDraws - (LOTTERY_DRAWS_PER_PAGE * page)
if (startId < 1) startId = 1
let endId = startId - LOTTERY_DRAWS_PER_PAGE + 1
if (endId < 1) endId = 1
let lastDraws = []
// console.log('getLotteryDrawsPage', { startId, endId, page, countOfDraws })
for (let drawId = startId; drawId >= endId; drawId--) {
let winTicketDraws = yield call(getWinTicketDraws, drawId)
let jackpotDraws = yield call(callContract, 'listJackpotDraws', 'bet', drawId) // джекпот каждого розыгрыша
let ticketsDraws = yield call(callContract, 'listTicketsDraws', 'int', drawId) // кол-во проданных билетов каждого розыгрыша
let winnersDraws = yield call(callContract, 'listWinnersDraws', 'int', drawId) // выигрышные билеты каждого розыгрыша
const draw = {
drawId,
winTicketDraws,
combination: prepareTicketNumbers(winTicketDraws),
jackpotDraws,
winnersDraws,
ticketsDraws
}
yield put(LotteryActions.lotteryDrawsAdd(draw))
lastDraws.push(draw)
}
const status = { lastDrawsPage: page + 1, lastDrawsLoading: false }
yield put(LotteryActions.lotteryUpdate(status))
// yield put(LotteryActions.lotteryUpdate({ lastDraws }))
}
}
export function * watchCurrentBlock () {
const closeLotteryBlock = yield call(callContract, 'closeLotteryBlock', 'int')
const blockForRandom = yield call(callContract, 'blockForRandom', 'int')
yield put(LotteryActions.lotteryUpdate({ closeLotteryBlock, blockForRandom }))
let i = 0
while (true) {
let currentBlock = yield call(window.web3.eth.getBlockNumber)
currentBlock = parseInt(currentBlock)
if (i === 0) {
yield put(LotteryActions.lotteryUpdate({ startLotteryBlock: currentBlock + 500 }))
}
yield put(LotteryActions.lotteryUpdate({ currentBlock }))
yield delay(20000)
i++
}
}
export function * getSellOverBlock () {
const sellOverBlock = yield call(callContract, 'sellOverBlock', 'int')
yield put(LotteryActions.lotteryUpdate({ sellOverBlock }))
}
export function * getJackpot () {
const jackpot = yield call(callContract, 'jackpot', 'bet')
if (jackpot > 0) {
yield put(LotteryActions.lotteryUpdate({ jackpot }))
} else {
yield call(getLotteryBalance)
}
}
export function * getLotteryBalance () {
const lotteryBalanceBET = yield call(callERC20, 'balanceOf', 'bet', CONTRACT_ADDRESS)
yield put(LotteryActions.lotteryUpdate({ lotteryBalanceBET, jackpot: lotteryBalanceBET }))
}
export function * getLotteryStatus () {
const isActive = yield call(callContract, 'activeLottery')
yield put(LotteryActions.lotteryUpdate({ isActive }))
}
export function * getLotteryTransactions (esApi) {
const openKey = CONTRACT_ADDRESS
const response = yield call(esApi.getTransactions, openKey)
console.groupCollapsed('Lottery transactions request')
console.log({ openKey, response })
if (response.ok) {
const data = response.data
if (data.status === '1') {
const transactions = splitTransactions(data.result, openKey)
console.log({ transactions })
yield put(LotteryActions.lotteryUpdate({ transactions }))
}
}
console.groupEnd()
}
export function * getPrizes () {
let dataPrize = {}
let dataPowerPlay = {}
dataPrize[50] = yield call(callContract, 'dataPrize', 'bet', 50)
dataPrize[41] = yield call(callContract, 'dataPrize', 'bet', 41)
dataPrize[40] = yield call(callContract, 'dataPrize', 'bet', 40)
dataPrize[31] = yield call(callContract, 'dataPrize', 'bet', 31)
dataPrize[30] = yield call(callContract, 'dataPrize', 'bet', 30)
dataPrize[21] = yield call(callContract, 'dataPrize', 'bet', 21)
dataPrize[11] = yield call(callContract, 'dataPrize', 'bet', 11)
dataPrize[1] = yield call(callContract, 'dataPrize', 'bet', 1)
dataPowerPlay[1] = yield call(callContract, 'dataPowerPlay', 'int', 1)
dataPowerPlay[2] = yield call(callContract, 'dataPowerPlay', 'int', 2)
dataPowerPlay[3] = yield call(callContract, 'dataPowerPlay', 'int', 3)
dataPowerPlay[4] = yield call(callContract, 'dataPowerPlay', 'int', 4)
dataPowerPlay[5] = yield call(callContract, 'dataPowerPlay', 'int', 5)
let multiplier = yield call(callContract, 'multiplier')
yield put(TicketsActions.ticketUpdate({ dataPrize, multiplier, dataPowerPlay }))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment