Created
February 14, 2019 11:49
-
-
Save thiagonobrega/acbd342487e706d1e29ef167c488582e 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.4-nightly.2019.2.7+commit.caecdfab.js&optimize=false&gist=
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.22 <0.6.0; | |
contract Ballot { | |
struct Voter { | |
uint weight; | |
bool voted; | |
uint8 vote; | |
address delegate; | |
} | |
struct Proposal { | |
uint voteCount; | |
} | |
address chairperson; | |
mapping(address => Voter) voters; | |
Proposal[] proposals; | |
/// Create a new ballot with $(_numProposals) different proposals. | |
constructor(uint8 _numProposals) public { | |
chairperson = msg.sender; | |
voters[chairperson].weight = 1; | |
proposals.length = _numProposals; | |
} | |
/// Give $(toVoter) the right to vote on this ballot. | |
/// May only be called by $(chairperson). | |
function giveRightToVote(address toVoter) public { | |
if (msg.sender != chairperson || voters[toVoter].voted) return; | |
voters[toVoter].weight = 1; | |
} | |
/// Delegate your vote to the voter $(to). | |
function delegate(address to) public { | |
Voter storage sender = voters[msg.sender]; // assigns reference | |
if (sender.voted) return; | |
while (voters[to].delegate != address(0) && voters[to].delegate != msg.sender) | |
to = voters[to].delegate; | |
if (to == msg.sender) return; | |
sender.voted = true; | |
sender.delegate = to; | |
Voter storage delegateTo = voters[to]; | |
if (delegateTo.voted) | |
proposals[delegateTo.vote].voteCount += sender.weight; | |
else | |
delegateTo.weight += sender.weight; | |
} | |
/// Give a single vote to proposal $(toProposal). | |
function vote(uint8 toProposal) public { | |
Voter storage sender = voters[msg.sender]; | |
if (sender.voted || toProposal >= proposals.length) return; | |
sender.voted = true; | |
sender.vote = toProposal; | |
proposals[toProposal].voteCount += sender.weight; | |
} | |
function winningProposal() public view returns (uint8 _winningProposal) { | |
uint256 winningVoteCount = 0; | |
for (uint8 prop = 0; prop < proposals.length; prop++) | |
if (proposals[prop].voteCount > winningVoteCount) { | |
winningVoteCount = proposals[prop].voteCount; | |
_winningProposal = prop; | |
} | |
} | |
} |
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
import "remix_tests.sol"; // this import is automatically injected by Remix. | |
import "./ballot.sol"; | |
contract test3 { | |
Ballot ballotToTest; | |
function beforeAll () public { | |
ballotToTest = new Ballot(2); | |
} | |
function checkWinningProposal () public { | |
ballotToTest.vote(1); | |
Assert.equal(ballotToTest.winningProposal(), uint(1), "1 should be the winning proposal"); | |
} | |
function checkWinninProposalWithReturnValue () public view returns (bool) { | |
return ballotToTest.winningProposal() == 1; | |
} | |
} |
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.22 <0.6.0; | |
contract CCContract{ | |
/** @dev Calculates a rectangle's surface and perimeter. | |
* @param w Width of the rectangle. | |
* @param h Height of the rectangle. | |
* @return s The calculated surface. | |
* @return p The calculated perimeter. | |
*/ | |
function rectangle(uint w, uint h) public pure returns (uint s, uint p) { | |
s = w * h; | |
p = 2 * (w + h); | |
} | |
function compare(string e1, string e2) public view returns (bool r){ | |
r = false | |
if (e1 == e2) return true; | |
return false; | |
} | |
// enviar wei na transmissao | |
contract InfoFeed { | |
function info() public payable returns (uint ret) { return 42; } | |
} | |
contract Consumer { | |
InfoFeed feed; | |
function setFeed(InfoFeed addr) public { feed = addr; } | |
function callFeed() public { feed.info.value(10).gas(800)(); } | |
} | |
} | |
// criar contratos | |
pragma solidity ^0.5.0; | |
contract D { | |
uint public x; | |
constructor(uint a) public payable { | |
x = a; | |
} | |
} | |
contract C { | |
D d = new D(4); // will be executed as part of C's constructor | |
function createD(uint arg) public { | |
D newD = new D(arg); | |
newD.x(); | |
} | |
function createAndEndowD(uint arg, uint amount) public payable { | |
// Send ether along with the creation | |
D newD = (new D).value(amount)(arg); | |
newD.x(); | |
} | |
} |
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.22 <0.6.0; | |
contract OwnedToken { | |
// `TokenCreator` is a contract type that is defined below. | |
// It is fine to reference it as long as it is not used | |
// to create a new contract. | |
TokenCreator creator; | |
address owner; | |
bytes32 name; | |
// This is the constructor which registers the | |
// creator and the assigned name. | |
constructor(bytes32 _name) public { | |
// State variables are accessed via their name | |
// and not via e.g. `this.owner`. Functions can | |
// be accessed directly or through `this.f`, | |
// but the latter provides an external view | |
// to the function. Especially in the constructor, | |
// you should not access functions externally, | |
// because the function does not exist yet. | |
// See the next section for details. | |
owner = msg.sender; | |
// We do an explicit type conversion from `address` | |
// to `TokenCreator` and assume that the type of | |
// the calling contract is `TokenCreator`, there is | |
// no real way to check that. | |
creator = TokenCreator(msg.sender); | |
name = _name; | |
} | |
function changeName(bytes32 newName) public { | |
// Only the creator can alter the name -- | |
// the comparison is possible since contracts | |
// are explicitly convertible to addresses. | |
if (msg.sender == address(creator)) | |
name = newName; | |
} | |
function transfer(address newOwner) public { | |
// Only the current owner can transfer the token. | |
if (msg.sender != owner) return; | |
// We ask the creator contract if the transfer | |
// should proceed by using a function of the | |
// `TokenCreator` contract defined below. If | |
// the call fails (e.g. due to out-of-gas), | |
// the execution also fails here. | |
if (creator.isTokenTransferOK(owner, newOwner)) | |
owner = newOwner; | |
} | |
} | |
contract TokenCreator { | |
function createToken(bytes32 name) | |
public | |
returns (OwnedToken tokenAddress) | |
{ | |
// Create a new `Token` contract and return its address. | |
// From the JavaScript side, the return type is | |
// `address`, as this is the closest type available in | |
// the ABI. | |
return new OwnedToken(name); | |
} | |
function changeName(OwnedToken tokenAddress, bytes32 name) public { | |
// Again, the external type of `tokenAddress` is | |
// simply `address`. | |
tokenAddress.changeName(name); | |
} | |
// Perform checks to determine if transferring a token to the | |
// `OwnedToken` contract should proceed | |
function isTokenTransferOK(address currentOwner, address newOwner) | |
public | |
pure | |
returns (bool ok) | |
{ | |
// Check an arbitrary condition to see if transfer should proceed | |
return keccak256(abi.encodePacked(currentOwner, newOwner))[0] == 0x7f; | |
} | |
} |
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.22 <0.6.0; | |
contract Mortal { | |
/* Define variable owner of the type address */ | |
address owner; | |
/* This constructor is executed at initialization and sets the owner of the contract */ | |
constructor() public { owner = msg.sender; } | |
/* Function to recover the funds on the contract */ | |
function kill() public { if (msg.sender == owner) selfdestruct(msg.sender); } | |
} | |
contract Greeter is Mortal { | |
/* Define variable greeting of the type string */ | |
string greeting; | |
/* This runs when the contract is executed */ | |
constructor(string memory _greeting) public { | |
greeting = _greeting; | |
} | |
/* Main function */ | |
function greet() public view returns (string memory) { | |
return greeting; | |
} | |
} |
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
contract SimpleStorage { | |
uint storedData; | |
function set(uint x) { | |
storedData = x; | |
} | |
function get() constant returns (uint retVal) { | |
return storedData; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment