Skip to content

Instantly share code, notes, and snippets.

@pslaulhe
Created March 3, 2018 18:53
Show Gist options
  • Save pslaulhe/18d96a19f60b1615413d560bee4bddf2 to your computer and use it in GitHub Desktop.
Save pslaulhe/18d96a19f60b1615413d560bee4bddf2 to your computer and use it in GitHub Desktop.
var Migrations = artifacts.require("./Migrations.sol");
module.exports = function(deployer) {
deployer.deploy(Migrations);
}
;
var Apuestas = artifacts.require("Apuestas");
module.exports = function(deployer) {
deployer.deploy(Apuestas);
};
App = {
web3Provider: null,
contracts: {},
init: function() {
// Load pets.
return App.initWeb3();
},
initWeb3: function() {
// Is there an injected web3 instance?
if (typeof web3 !== 'undefined') {
App.web3Provider = web3.currentProvider;
} else {
// If no injected web3 instance is detected, fall back to Ganache
App.web3Provider = new Web3.providers.HttpProvider('http://localhost:7545');
}
web3 = new Web3(App.web3Provider);
return App.initContract();
},
initContract: function() {
$.getJSON('Apuestas.json', function(data) {
// Get the necessary contract artifact file and instantiate it with truffle-contract
var AdoptionArtifact = data;
App.contracts.Apuestas = TruffleContract(AdoptionArtifact);
// Set the provider for our contract
App.contracts.Apuestas.setProvider(App.web3Provider);
// Use our contract to retrieve and mark the adopted pets
return App.markAdopted();
});
return App.bindEvents();
},
bindEvents: function() {
$(document).on('click', '.btn-adopt', App.handleAdopt);
},
markAdopted: function(adopters, account) {
var apuestasInstance;
App.contracts.Apuestas.deployed().then(function(instance) {
apuestasInstance = instance;
var petTemplate = $('#petTemplate');
var petsRow = $('#petsRow');
for (i = 0; i < 10; i++) {
apuestasInstance.partidos.call(i).then(function(partidos) {
if (partidos[1]) {
petTemplate.find('.panel-title').text(partidos[1] + '-' + partidos[2]);
petsRow.append(petTemplate.html());
}
}).catch(function(err) {
console.log(err.message);
});
}});
},
handleAdopt: function(event) {
event.preventDefault();
var petId = parseInt($(event.target).data('id'));
var adoptionInstance;
web3.eth.getAccounts(function(error, accounts) {
if (error) {
console.log(error);
}
var account = accounts[0];
App.contracts.Adoption.deployed().then(function(instance) {
adoptionInstance = instance;
// Execute adopt as a transaction by sending account
return adoptionInstance.adopt(petId, {from: account});
}).then(function(result) {
return App.markAdopted();
}).catch(function(err) {
console.log(err.message);
});
});
}
};
$(function() {
$(window).load(function() {
App.init();
});
});
pragma solidity ^0.4.17;
contract Apuestas {
struct Apuesta {
uint256 id;
uint256 ganar;
uint256 perder;
address creador;
address consumidor;
uint8 golesLocal;
uint8 golesVisitante;
bool procesado;
}
struct Partido {
uint256 idPartido;
string local;
string visitante;
}
uint256 currentApuestaId = 1;
mapping(uint256 => mapping (uint256 => Apuesta)) public apuestas;
mapping(uint256 => Partido) public partidos;
address authorized;
function Apuestas() public payable {
authorized = msg.sender;
partidos[1] = Partido(1, "Madrid", "Barcelona");
partidos[2] = Partido(2, "Atletico de Madrid", "Athletic de Bilbao");
}
event IdApuesta(uint256 idApuesta);
function crearApuesta(uint256 beneficio, uint256 perdidas, uint8 golesLocal, uint8 golesVisitante, uint256 idPartido) public payable {
require(msg.value == perdidas);
require(existsMatch(idPartido));
apuestas[idPartido][currentApuestaId] = Apuesta(
currentApuestaId,
beneficio,
perdidas,
msg.sender,
address(0),
golesLocal,
golesVisitante,
false
);
IdApuesta(currentApuestaId);
currentApuestaId++;
}
function aceptarApuesta(uint256 idPartido, uint256 idApuesta) public payable {
require(existsMatch(idPartido));
require(apuestas[idPartido][idApuesta].id != 0);
require(apuestas[idPartido][idApuesta].consumidor == address(0));
require(msg.value == apuestas[idPartido][idApuesta].ganar);
apuestas[idPartido][idApuesta].consumidor = msg.sender;
}
function solveApuestas(uint256 idPartido, uint8 golesLocal, uint8 golesVisitante) public {
require(msg.sender == authorized);
require(existsMatch(idPartido));
for(uint i=1;i<currentApuestaId;i++){
Apuesta ap = apuestas[idPartido][i];
if(!ap.procesado){
ap.procesado = true;
if(ap.golesLocal == golesLocal && ap.golesVisitante == golesVisitante ){
ap.creador.transfer(ap.ganar + ap.perder);
} else {
ap.consumidor.transfer(ap.perder + ap.perder);
}
}
}
}
function startMatch(uint256 idPartido){
require(msg.sender == authorized);
require(existsMatch(idPartido));
for(uint i=1;i<currentApuestaId;i++){
Apuesta ap = apuestas[idPartido][i];
if(!ap.procesado && ap.consumidor == address(0)){
ap.procesado = true;
ap.creador.transfer(ap.perder);
}
}
}
function existsMatch(uint256 idPartido) private returns (bool){
return partidos[idPartido].idPartido == idPartido && idPartido > 0;
}
}
pragma solidity ^0.4.2;
contract Migrations {
address public owner;
uint public last_completed_migration;
modifier restricted() {
if (msg.sender == owner) _;
}
function Migrations() {
owner = msg.sender;
}
function setCompleted(uint completed) restricted {
last_completed_migration = completed;
}
function upgrade(address new_address) restricted {
Migrations upgraded = Migrations(new_address);
upgraded.setCompleted(last_completed_migration);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment