Created
July 21, 2022 20:48
-
-
Save martinsnelson/8f5de62af52aee3539d5f58701b465a9 to your computer and use it in GitHub Desktop.
Smart contract example with solidity
This file contains hidden or 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.0 <0.8.15; | |
contract Bolao { | |
address private gerente; | |
address[] private jogadores; | |
constructor() public | |
{ | |
gerente = msg.sender; | |
} | |
/// Ação de login do jogador requerendo 1 ether, obs tentar login com valor menor que 1 ether perde o gas e não entra | |
function entrar() public payable | |
{ | |
require(msg.value >= 1 ether); | |
jogadores.push(msg.sender); | |
} | |
/// Ação executada pelo gerente, escolhe o ganhador e transfer todo saldo do bolao para o jogador ganhador | |
function escolherGanhador() public restritoGerente | |
{ | |
uint index = randomico() % jogadores.length; | |
jogadores[index].transfer(address(this).balance); | |
limpar(); | |
} | |
/// Validação de quem está executado a ação de escolher o ganhador, obs o ganhador e aleatório | |
modifier restritoGerente() | |
{ | |
require(msg.sender == gerente); | |
_; | |
} | |
/// Função que lista todos os jogadores(quantos jogadores tem no bolao) | |
function getJogadores() public view returns(address[]) | |
{ | |
return jogadores; | |
} | |
/// Função que lista o gerente(quem é o gerente) | |
function getGerente() public view returns(address) | |
{ | |
return gerente; | |
} | |
/// Função que retorna o saldo do bolão | |
function getSaldo() public view returns(uint) | |
{ | |
return address(this).balance; | |
} | |
/// Limpar o array de jogadores | |
function limpar() private | |
{ | |
jogadores = new address[](0); | |
} | |
/// Função que faz o randomico dos jogadores do bolão | |
function randomico() private view returns(uint) | |
{ | |
return uint(keccak256(abi.encodePacked(block.difficulty, now, jogadores))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment