Skip to content

Instantly share code, notes, and snippets.

@ObjSal
Created October 17, 2020 17:33
Show Gist options
  • Save ObjSal/8d3254114287f3f25e8be610ec3a34b3 to your computer and use it in GitHub Desktop.
Save ObjSal/8d3254114287f3f25e8be610ec3a34b3 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.6.12+commit.27d51765.js&optimize=false&gist=
// Code snippet from Blockchain in Action
pragma solidity ^0.6.0;
contract AccountsDemo {
address public whoDeposited;
uint public depositAmount;
uint public accountBalance;
function deposit() public payable {
whoDeposited = msg.sender;
depositAmount = msg.value;
accountBalance = address(this).balance;
}
}
// Code snippet from Blockchain in Action
pragma solidity ^0.6.0;
contract Airlines {
address chairperson;
struct details {
uint escrow;
uint status;
uint hashOfDetails;
}
mapping (address => details) public balanceDetails;
mapping (address => uint) membership;
modifier onlyChairperson {
require(msg.sender == chairperson);
_;
}
modifier onlyMember {
require(membership[msg.sender] == 1);
_;
}
constructor() public payable {
chairperson = msg.sender;
membership[msg.sender] = 1;
balanceDetails[msg.sender].escrow = msg.value;
}
function register() public payable {
membership[msg.sender] = 1;
balanceDetails[msg.sender].escrow = msg.value;
}
// Only the chairperson can unregister airlines
function unregister(address payable airline) onlyChairperson public {
// This condition might be unnecessary because it already has
// the 'onlyChairperson' modifier
if (chairperson != msg.sender) {
revert();
}
// Remove membership
membership[airline] = 0;
// Return money
uint escrow = balanceDetails[airline].escrow;
balanceDetails[airline].escrow = 0;
airline.transfer(escrow);
}
function request(address toAirline, uint hashOfDetails) onlyMember public {
if (membership[toAirline] != 1) {
revert();
}
balanceDetails[msg.sender].status = 0;
balanceDetails[msg.sender].hashOfDetails = hashOfDetails;
}
function response(address fromAirline, uint hashOfDetails, uint status) onlyMember public {
if (membership[fromAirline] != 1) {
revert();
}
// book has a typo, it set the status for the sender instead of using 'fromAirline'
balanceDetails[fromAirline].status = status;
balanceDetails[fromAirline].hashOfDetails = hashOfDetails;
}
function settlePayment(address payable toAirline) onlyMember payable public {
balanceDetails[toAirline].escrow += msg.value;
balanceDetails[msg.sender].escrow -= msg.value;
toAirline.transfer(msg.value);
}
}
// SPDX-License-Identifier: Free
// Code snippet from Blockchain in Action
pragma solidity >= 0.4.2 <= 0.6.12;
contract Ballot {
struct Voter {
uint weight;
bool voted;
uint vote;
}
struct Proposal {
uint voteCount;
}
enum Phase { Init, Regs, Vote, Done }
address chairperson;
mapping (address => Voter) voters;
Proposal[] proposals;
Phase public state = Phase.Init;
modifier validPhase(Phase reqPhase) {
require(state == reqPhase, "Required state phase is invalid");
_;
}
modifier onlyChair {
require(msg.sender == chairperson, "Only the chairperson can call this function");
_;
}
constructor (uint numProposals) public {
chairperson = msg.sender;
voters[chairperson].weight = 2;
for (uint prop = 0; prop < numProposals; prop++) {
proposals.push(Proposal(0));
}
state = Phase.Regs;
}
function changeState(Phase s) onlyChair public {
require(s > state, "state must be greater than the current state");
state = s;
}
function register(address voter) validPhase(Phase.Regs) onlyChair public {
require(!voters[voter].voted, "Already voted");
voters[voter].weight = 1;
// voters[voter].voted = false;
}
function vote(uint prop) validPhase(Phase.Vote) public {
Voter memory sender = voters[msg.sender];
require(!sender.voted, "Already voted");
require(prop < proposals.length, "Invalid proposal");
sender.voted = true;
sender.vote = prop;
proposals[prop].voteCount += sender.weight;
}
function reqWinner() public validPhase(Phase.Done) view returns (uint winningProposal) {
uint winningVoteCount = 0;
for (uint prop = 0; prop < proposals.length; prop++) {
if (proposals[prop].voteCount > winningVoteCount) {
winningVoteCount = proposals[prop].voteCount;
winningProposal = prop;
}
}
// If the below line is uncommented, it will make the whole contract invalid
// if the winnint vote is below 3.
// assert(winningVoteCount >= 3);
}
}
// 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;
// }
// }
// }
// pragma solidity >=0.4.22 <0.6.0;
// 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;
// }
// }
// Code snippet from Blockchain in Action
pragma solidity ^0.6.0;
contract Counter {
uint public value;
function initialize(uint n) public {
value = n;
}
function get() view public returns (uint) {
return value;
}
function increment(uint n) public {
value += n;
}
function decrement(uint n) public {
value -= n;
}
}
// pragma solidity ^0.4.24;
// contract Message {
// string myMessage;
// function setMessage(string x) public {
// myMessage = x;
// }
// function getMessage() public view returns (string) {
// return myMessage;
// }
// }
// pragma solidity >=0.4.24;
// contract myToken {
// string public constant name = "Udacity Token";
// string public constant symbol = "UDC";
// uint8 public constant decimals = 18; // 18 is the most common number of decimal places
// uint _totalSupply;
// // Balances for each account stored using a mapping
// mapping(address => uint256) balances;
// // Owner of the account approves the allowance of another account
// // Create an allowance mapping
// // The first key is the owner of the tokens
// // In the 2nd mapping, its says who can spend on your behalf, and how many
// // So, we are creating a mapping, where the kep is an address,
// // The value is further a mapping of address to amount
// mapping(address => mapping (address => uint256)) allowance;
// event Transfer(address indexed from, address indexed to, uint tokens);
// event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
// // Called automatically when contract is initiated
// // Sets to total initial _totalSupply, as per the input argument
// // Also gives the initial supply to msg.sender...who creates the contract
// constructor(uint amount) public {
// _totalSupply = amount;
// balances[msg.sender] = amount;
// }
// // Returns the total supply of tokens
// function totalSupply() public view returns (uint256) {
// return _totalSupply;
// }
// // Get the token balance for account `tokenOwner`
// // Anyone can query and find the balance of an address
// function balanceOf(address tokenOwner) public constant returns (uint balance) {
// return balances[tokenOwner];
// }
// // Transfer the balance from owner's account to another account
// // Decreases the balance of "from" account
// // Increases the balance of "to" account
// // Emits Transfer event
// function transfer(address to, uint tokens) public returns (bool success) {
// if(tokens < 1){
// revert("Not enough Ether provided.");
// }
// require(tokens <= balances[msg.sender]);
// balances[msg.sender] = balances[msg.sender] - tokens;
// balances[to] = balances[to] + tokens;
// emit Transfer(msg.sender, to, tokens);
// return true;
// }
// // Send amount of tokens from address `from` to address `to`
// // The transferFrom method is used to allow contracts to spend
// // tokens on your behalf
// // Decreases the balance of "from" account
// // Decreases the allowance of "msg.sender"
// // Increases the balance of "to" account
// // Emits Transfer event
// function transferFrom(address from, address to, uint tokens) public returns (bool success) {
// balances[from] = balances[from] - tokens;
// allowance[from][msg.sender] = allowance[from][msg.sender] - tokens;
// balances[to] = balances[to] + tokens;
// emit Transfer(from, to, tokens);
// return true;
// }
// // Approves the `spender` to withdraw from your account, multiple times, up to the `tokens` amount.
// // So the msg.sender is approving the spender to spend these many tokens
// // from msg.sender's account
// // Setting up allowance mapping accordingly
// // Emits approval event
// function approve(address spender, uint tokens) public returns (bool success) {
// allowance[msg.sender][spender] = tokens;
// emit Approval(msg.sender, spender, tokens);
// return true;
// }
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment