Last active
June 19, 2018 19:08
-
-
Save Livshitz/4d603425df591ab35e52532105023b0f to your computer and use it in GitHub Desktop.
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
console.log('> LotaryContract: Init: v 0.32') | |
const _ = require('lodash'); | |
// Define new contract | |
var contract = {}; | |
// Reset/Init the contract | |
contract.reset = function() { | |
contract.balance = 0; | |
contract.players = []; | |
contract.threshold = 3; | |
} | |
// Get random winner and send him a TX with the collective reward | |
contract.executeLotary = function() { | |
var w = contract.getWinner(); | |
var reward = contract.balance * 100000000; | |
console.log('> observer:executeLotary: Got a winner and sending his reward: ', w, reward); | |
app.serverWallet.sendTx('reward', w.address, Math.floor(reward)).then(()=> { | |
contract.reset(); | |
}).fail((err)=>{ | |
console.log('> observer:executeLotary: Failed to send the reward. Will retry once.', err); | |
app.serverWallet.sendTx('reward', w.address, Math.floor(reward)); | |
}) | |
} | |
// Helper: Get a random winner | |
contract.getWinner = function(){ | |
var winnerIndex = Math.floor(Math.random() * (contract.players.length)); | |
return { address: contract.players[winnerIndex], index: winnerIndex }; | |
} | |
// Event that is triggered on TX that was sent to our contract | |
app.observer.onDetectPayment = function(data) { | |
var d = {}; | |
// Get the relevant input for our contract | |
var input = _.filter(data.payload.transaction.inputs, (v)=> { | |
return !_.includes(v.addresses, addr); | |
}) | |
// If input was not found, or empty, ignore event | |
if (input == null || input.length == 0 || input[0].addresses[0] == addr) { | |
console.log('> observer:onDetectPayment: Detected outgoing transaction, ignoring'); | |
return; | |
} | |
// Grab the originator address | |
d.from = input[0].addresses; | |
var output = _.filter(data.payload.transaction.outputs, (v)=> { | |
return _.includes(v.addresses, addr); | |
}) | |
// Grab the amount | |
d.amount = output[0].value_int / 100000000; | |
// Aggregate the amount into total contract balance | |
contract.balance = d.amount + (contract.balance || 0); | |
// Collect the new participant | |
if (contract.players == null) contract.players = []; | |
contract.players.push(d.from[0]); | |
// Check if reached the max players / threshold, if so, execute the lotary | |
if (contract.players.length >= contract.threshold) { | |
console.log('> observer:onDetectPayment: reached threshold! rolling the dice'); | |
contract.executeLotary(); | |
} | |
} | |
// Set the contract to be used in the app | |
app.contract = contract; | |
// Assign the contract address | |
contract.address = app.contractAddress; | |
// Init the contract | |
app.contract.reset(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment