Created
July 5, 2020 14:00
-
-
Save maellson/c6664bfa94bad888230995de39e99c98 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.5.3+commit.10d17f24.js&optimize=false&gist=
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
pragma solidity ^0.5.17; | |
library SafeMath { | |
//funcoes pure nao alteram nem consultam coisas na blockchain e sao gratuitas | |
function sum(uint num1, uint num2) internal pure returns(uint){ | |
uint num3 = num1+num2; | |
require(num3 >=num1, "Erro de soma: Sum Overflow"); | |
return num3; | |
} | |
function sub(uint num1, uint num2) internal pure returns(uint){ | |
require(num2 <= num1, "Erro de Subtracao: Sub Underflow"); | |
uint num3 = num1 - num2; | |
return num3; | |
} | |
function prod(uint num1, uint num2) internal pure returns(uint){ | |
if (num1 == 0){ | |
return 0; | |
} | |
uint num3 = num1 * num2; | |
require(num3 / num1 == num2, "Erro multiplicacao: Prod Overflow"); | |
return num3; | |
} | |
function div(uint num1, uint num2) internal pure returns(uint){ | |
uint num3 = num1 / num2; | |
return num3; | |
} | |
} | |
contract Ownable { | |
address payable public owner; // variavei para enderecos que enviam ou recebem eth devem ser payable. | |
//evento que pode ser disparado ao ser executada uma determinada tarefa do contrato. | |
event OwnershipTranferred(address newOwner); | |
// a funcao constructor, por fdefinicao, so e chamada uma vez, nao podendo ser alterada. | |
constructor()public { | |
owner = msg.sender; | |
} | |
//exige que o usuario seja o dono do contrato para executar a funcao que vier posterior. | |
modifier onlyOwner() { | |
require (msg.sender == owner, "You are not the owner!"); | |
_; //executa o resto da funcao, ou seja, o que vier apos quando for chamado. | |
} | |
// essa so executada apos tere certeza que o usuario e o dono do contrato para poder executa-la. | |
function transferOwnerShip(address payable newOwner) onlyOwner public{ | |
owner = newOwner; | |
emit OwnershipTranferred(owner); | |
} | |
} | |
contract desafio_01 is Ownable{ | |
using SafeMath for uint; | |
string public retorno; | |
// mapping (address => uint) public balances;//saldo da conta; | |
//mapping (address => uint) public hasInteracted; | |
//essa variavel recebe a quantidade de vezes em que houve interacao no contrato(modelo_2) | |
function setNumber (uint myNumber) public payable{ | |
require(myNumber <=10, "Number out of range"); | |
require (msg.value == 25 finney, "Insufficient ETH sent."); | |
if(myNumber > 5){ | |
retorno = "maior que 5"; | |
} else { | |
retorno = "menor ou igual a 5";} | |
} | |
//podia ser implementada direto na funcao de cima, | |
//mas prefiro uma funcao especifica para exibir a string. | |
function getString() public view returns (string memory) { | |
return retorno; | |
} | |
} |
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
pragma solidity ^0.5.17; | |
library SafeMath { | |
//funcoes pure nao alteram nem consultam coisas na blockchain e sao gratuitas | |
function sum(uint num1, uint num2) internal pure returns(uint){ | |
uint num3 = num1+num2; | |
require(num3 >=num1, "Erro de soma: Sum Overflow"); | |
return num3; | |
} | |
function sub(uint num1, uint num2) internal pure returns(uint){ | |
require(num2 <= num1, "Erro de Subtracao: Sub Underflow"); | |
uint num3 = num1 - num2; | |
return num3; | |
} | |
function prod(uint num1, uint num2) internal pure returns(uint){ | |
if (num1 == 0){ | |
return 0; | |
} | |
uint num3 = num1 * num2; | |
require(num3 / num1 == num2, "Erro multiplicacao: Prod Overflow"); | |
return num3; | |
} | |
function div(uint num1, uint num2) internal pure returns(uint){ | |
uint num3 = num1 / num2; | |
return num3; | |
} | |
} | |
contract Ownable { | |
address payable public owner; // variavei para enderecos que enviam ou recebem eth devem ser payable. | |
//evento que pode ser disparado ao ser executada uma determinada tarefa do contrato. | |
event OwnershipTranferred(address newOwner); | |
// a funcao constructor, por fdefinicao, so e chamada uma vez, nao podendo ser alterada. | |
constructor()public { | |
owner = msg.sender; | |
} | |
//exige que o usuario seja o dono do contrato para executar a funcao que vier posterior. | |
modifier onlyOwner() { | |
require (msg.sender == owner, "You are not the owner!"); | |
_; //executa o resto da funcao, ou seja, o que vier apos quando for chamado. | |
} | |
// essa so executada apos tere certeza que o usuario e o dono do contrato para poder executa-la. | |
function transferOwnerShip(address payable newOwner) onlyOwner public{ | |
owner = newOwner; | |
emit OwnershipTranferred(owner); | |
} | |
} | |
contract desafio_02 is Ownable{ | |
using SafeMath for uint; | |
uint public price = 25 finney;//definiu uma variavel numerica | |
event NewPrice(uint newPrice); | |
//mapping (address => uint) public balances;//saldo da conta; | |
// mapping (address => uint) public hasInteracted;//essa variavel recebe a quantidade de vezes em que houve interacao no contrato(modelo_2) | |
function setNumber (uint myNumber) public payable returns (string memory) { | |
require(myNumber <=10, "Number out of range"); | |
require (msg.value == price, "Insufficient ETH sent."); | |
doublePrice(); | |
if(myNumber > 5){ | |
return "maior que 5"; | |
} | |
return "menor ou igual a 5"; | |
} | |
function doublePrice() private { | |
price = price.prod(2); | |
emit NewPrice(price); | |
} | |
} | |
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
pragma solidity ^0.5.17; | |
library SafeMath { | |
//funcoes pure nao alteram nem consultam coisas na blockchain e sao gratuitas | |
function sum(uint num1, uint num2) internal pure returns(uint){ | |
uint num3 = num1+num2; | |
require(num3 >=num1, "Erro de soma: Sum Overflow"); | |
return num3; | |
} | |
function sub(uint num1, uint num2) internal pure returns(uint){ | |
require(num2 <= num1, "Erro de Subtracao: Sub Underflow"); | |
uint num3 = num1 - num2; | |
return num3; | |
} | |
function prod(uint num1, uint num2) internal pure returns(uint){ | |
if (num1 == 0){ | |
return 0; | |
} | |
uint num3 = num1 * num2; | |
require(num3 / num1 == num2, "Erro multiplicacao: Prod Overflow"); | |
return num3; | |
} | |
function div(uint num1, uint num2) internal pure returns(uint){ | |
uint num3 = num1 / num2; | |
return num3; | |
} | |
} | |
contract Ownable { | |
address payable public owner; // variavei para enderecos que enviam ou recebem eth devem ser payable. | |
//evento que pode ser disparado ao ser executada uma determinada tarefa do contrato. | |
event OwnershipTranferred(address newOwner); | |
// a funcao constructor, por fdefinicao, so e chamada uma vez, nao podendo ser alterada. | |
constructor()public { | |
owner = msg.sender; | |
} | |
//exige que o usuario seja o dono do contrato para executar a funcao que vier posterior. | |
modifier onlyOwner() { | |
require (msg.sender == owner, "You are not the owner!"); | |
_; //executa o resto da funcao, ou seja, o que vier apos quando for chamado. | |
} | |
// essa so executada apos tere certeza que o usuario e o dono do contrato para poder executa-la. | |
function transferOwnerShip(address payable newOwner) onlyOwner public{ | |
owner = newOwner; | |
emit OwnershipTranferred(owner); | |
} | |
} | |
contract desafio_02 is Ownable{ | |
using SafeMath for uint; | |
uint public price = 25 finney;//definiu uma variavel numerica | |
event NewPrice(uint newPrice); | |
mapping (address => uint) public balances;//saldo da conta; | |
// mapping (address => uint) public hasInteracted;//essa variavel recebe a quantidade de vezes em que houve interacao no contrato(modelo_2) | |
function setNumber (uint myNumber) public payable returns (string memory) { | |
require(myNumber <=10, "Number out of range"); | |
require (msg.value == price, "Insufficient ETH sent."); | |
//Como todo o dinherio pertence ao dono do contrato, o saldo(balanco) continua indo pra ele não precisa da funcao abaixo | |
//balances[owner] = balances[owner].sum(msg.value); | |
doublePrice(); | |
if(myNumber > 5){ | |
return "maior que 5"; | |
} | |
return "menor ou igual a 5"; | |
} | |
function doublePrice() private { | |
price = price.prod(2); | |
emit NewPrice(price); | |
} | |
function withdraw(uint myAmount) onlyOwner public { | |
//require(balances[owner] >= myAmount, "Insufficient funds."); | |
// balances[owner] = balances[owner].sub(myAmount);//mod 1 | |
//esse modelo serve para diferenciar a quem pertence cada valor no contrato | |
// mas neste desafio todo o dinherio pertence a um unico dono | |
//sendo assim a forma mais simples seria da maneira abaixo. | |
require(address(this).balance >= myAmount, "Insufficient funds."); | |
//assim nem precisa se preucupar em chamar a funcao de subtracao | |
//pois o proprio contrato ja realiza todas as operacoes | |
owner.transfer(myAmount); | |
} | |
} | |
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
pragma solidity ^0.4.0; | |
contract GuardaLoteria { | |
uint numeroSorteado; | |
function set(uint enviado) public{ | |
numeroSorteado = enviado; | |
} | |
function get() public view returns (uint) { | |
return numeroSorteado; | |
} | |
} |
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
pragma solidity ^0.5.17; | |
/** | |
* The SafeMath library does this and that... | |
* essa library contem os methodos matemáticos que podem ser importados por outras classes que pertençam ao diretorio | |
* quando um determinado contrato quer usarlas só precisa instaciar. | |
*/ | |
library SafeMath { | |
//funcoes pure nao alteram nem consultam coisas na blockchain e sao gratuitas | |
function sum(uint num1, uint num2) internal pure returns(uint){ | |
uint num3 = num1+num2; | |
require(num3 >=num1, "Erro de soma: Sum Overflow"); | |
return num3; | |
} | |
function sub(uint num1, uint num2) internal pure returns(uint){ | |
require(num2 <= num1, "Erro de Subtracao: Sub Underflow"); | |
uint num3 = num1 - num2; | |
return num3; | |
} | |
function prod(uint num1, uint num2) internal pure returns(uint){ | |
if (num1 == 0){ | |
return 0; | |
} | |
uint num3 = num1 * num2; | |
require(num3 / num1 == num2, "Erro multiplicacao: Prod Overflow"); | |
return num3; | |
} | |
function div(uint num1, uint num2) internal pure returns(uint){ | |
uint num3 = num1 / num2; | |
return num3; | |
} | |
} | |
/** | |
* The Ownable contract does this and that... | |
* classe com methods para serem herdados por outras classes. | |
* funcoes ownnable permitem que o contrato saiba quem é o dono dele. | |
**/ | |
contract Ownable { | |
address payable public owner; // variavei para enderecos que enviam ou recebem eth devem ser payable. | |
//evento que pode ser disparado ao ser executada uma determinada tarefa do contrato. | |
event OwnershipTranferred(address newOwner); | |
// a funcao constructor, por fdefinicao, so e chamada uma vez, nao podendo ser alterada. | |
constructor()public { | |
owner = msg.sender; | |
} | |
//exige que o usuario seja o dono do contrato para executar a funcao que vier posterior. | |
modifier onlyOwner() { | |
require (msg.sender == owner, "You are not the owner!"); | |
_; //executa o resto da funcao, ou seja, o que vier apos quando for chamado. | |
} | |
// essa so executada apos tere certeza que o usuario e o dono do contrato para poder executa-la. | |
function transferOwnerShip(address payable newOwner) onlyOwner public{ | |
owner = newOwner; | |
emit OwnershipTranferred(owner); | |
} | |
} | |
/** | |
* esse contrato HelloWorld agora sabe tanto guardar o endereco do Ownable como executar suas funcoes | |
*Esse é o melhor exemplo de | |
** | |
*/ | |
contract HelloWorld is Ownable{ | |
using SafeMath for uint; | |
string public text;//defeiniu uma string como sendo publica de acesso global | |
uint public number;//definiu uma variavel numerica | |
address payable public userAddress;//definiu uma variavel do tipo endereco | |
bool public answer;// definiu uma variavel booleana | |
//mapping (address => bool) public hasInteracted;//definiu uma variavel mapeada que informa se um endereco interagiu no contrato (modelo_1) | |
mapping (address => uint) public hasInteracted;//essa variavel recebe a quantidade de vezes em que houve interacao no contrato(modelo_2) | |
mapping (address => uint) public balances;//saldo da conta; | |
function setText (string memory myText) onlyOwner public { | |
text = myText; | |
setInteracted(); | |
} | |
function setNumber (uint myNumber) public payable{ | |
require (msg.value >= 1 ether, "Insufficient ETH sent."); | |
balances[msg.sender] = balances[msg.sender].sum(msg.value); //procura o endereco pra fazer o incremento do valor | |
number = myNumber; | |
setInteracted(); | |
} | |
//esta funcao ler de imediato de qual endereco partiu a mensagem ou os termos do contrato. | |
function setUserAddress() public { | |
userAddress = msg.sender; | |
setInteracted(); | |
} | |
//variaveis booleana so aceitam dois valores true ou false, caso seja inserido outro valor, sera interpretado como true | |
function setAnswer (bool myAnswer) public { | |
answer = myAnswer; | |
setInteracted(); | |
} | |
//(modelo_1)guarda dentro da variavel hasInteracted o valor true, pra dizer que aquele endereco interegiu dentro do contrato | |
//(modelo_2) Incrementa o valor da varivael para contar quantas vezes houve interacoes no contrato. | |
//toda ver que um metodo acima e chamado ess funcao e executada. | |
function setInteracted()private{ | |
//hasInteracted[msg.sender] = true; //modelo_1 | |
hasInteracted[msg.sender] = hasInteracted[msg.sender].sum(1); //modelo_2 | |
} | |
//envia eth para uma conta payable | |
function sendETH(address payable targetAddress) public payable{ | |
targetAddress.transfer(msg.value); | |
} | |
function withdraw() public { | |
require (balances[msg.sender] > 0, "Insufficient funds!"); | |
uint amount = balances[msg.sender];//variavel de escopo e sera esquecida | |
balances[msg.sender] =0;// o saldo e zerado antes da tranferencia impedido loop de saques | |
msg.sender.transfer(amount);// usa o valor do amount e verifica o saldo, como agora esta zerado, nao podera ser executada novamente | |
} | |
//funcoes do tipo view fazem consulta na blockchain. ela esta consultando o valor de number | |
function sumStored (uint num1) public view returns(uint){ | |
return num1.sum(number); | |
} | |
} |
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
pragma solidity 0.5.3; | |
library SafeMath { | |
function add(uint a, uint b) internal pure returns(uint) { | |
uint c = a + b; | |
require(c >= a, "Sum Overflow!"); | |
return c; | |
} | |
function sub(uint a, uint b) internal pure returns(uint) { | |
require(b <= a, "Sub Underflow!"); | |
uint c = a - b; | |
return c; | |
} | |
function mul(uint a, uint b) internal pure returns(uint) { | |
if(a == 0) { | |
return 0; | |
} | |
uint c = a * b; | |
require(c / a == b, "Mul Overflow!"); | |
return c; | |
} | |
function div(uint a, uint b) internal pure returns(uint) { | |
uint c = a / b; | |
return c; | |
} | |
} | |
contract Ownable { | |
address payable public owner; | |
event OwnershipTransferred(address newOwner); | |
constructor() public { | |
owner = msg.sender; | |
} | |
modifier onlyOwner() { | |
require(msg.sender == owner, "You are not the owner!"); | |
_; | |
} | |
function transferOwnership(address payable newOwner) onlyOwner public { | |
owner = newOwner; | |
emit OwnershipTransferred(owner); | |
} | |
} | |
contract BasicToken is Ownable { | |
using SafeMath for uint; | |
string public constant name = "Unidade Fiscal Digital de Campina Grande"; | |
string public constant symbol = "UFID-CG"; | |
uint8 public constant decimals = 18; | |
uint public totalSupply; | |
mapping(address => uint) balances; | |
event Mint(address indexed to, uint tokens); | |
event Transfer(address indexed from, address indexed to, uint tokens); | |
function mint(address to, uint tokens) onlyOwner public { | |
balances[to] = balances[to].add(tokens); | |
totalSupply = totalSupply.add(tokens); | |
emit Mint(to, tokens); | |
} | |
function transfer(address to, uint tokens) public { | |
require(balances[msg.sender] >= tokens); | |
require(to != address(0)); | |
balances[msg.sender] = balances[msg.sender].sub(tokens); | |
balances[to] = balances[to].add(tokens); | |
emit Transfer(msg.sender, to, tokens); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment