Created
August 13, 2018 13:52
-
-
Save iOSLearner-dev/62332d55154e468733b37d2c5835366d 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.4.24+commit.e67f0147.js&optimize=true&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.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. | |
function Ballot(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 constant 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
pragma solidity ^0.4.0; | |
interface ERC20 { | |
function totalSupply() public view returns (uint256 totalSupply); //[Get the total token supply] | |
function balanceOf(address _owner) public view returns (uint256 balance); //[Get the account balance of another account with address _owner] | |
function transfer(address _to, uint256 _value) public returns (bool success); //[Send _value amount of tokens to address _to] | |
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success); //[Send _value amount of tokens from address _from to address _to] | |
function approve(address _spender, uint256 _value) public returns (bool success); //[Allow _spender to withdraw from your account, multiple times, up to the _value amount. If this function is called again it overwrites the current allowance with _value] | |
function allowance(address _owner, address _spender) public view returns (uint256 remaining); //[Returns the amount which _spender is still allowed to withdraw from _owner] | |
event Transfer(address indexed _from, address indexed _to, uint256 _value); // [Triggered when tokens are transferred.] | |
event Approval(address indexed _owner, address indexed _spender, uint256 _value); //[Triggered whenever approve(address _spender, uint256 _value) is called.] | |
} | |
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; | |
import "browser/ERC20.sol"; | |
contract MyFirstToken is ERC20 { | |
// Declaring Variabels | |
string public constant symbol = "MFT"; // Declaring symbol of MyToken like BCT for BitCoin | |
string public constant name = "My First Token"; | |
uint8 public constant decimals = 18; | |
uint private constant __totalSupply = 1000; // Total Supply Count | |
mapping (address => uint) private __balanceOf; | |
mapping (address => mapping (address => uint)) private __allowances; | |
function MyFirstToken() { | |
__balanceOf[msg.sender] = __totalSupply; | |
} | |
function totalSupply() constant returns (uint _totalSupply) { | |
_totalSupply = __totalSupply; | |
} | |
function balanceOf(address _addr) constant returns (uint balance) { | |
return __balanceOf[_addr]; | |
} | |
function transfer(address _to, uint _value) returns (bool success) { | |
if(_value > 0 && _value <= balanceOf(msg.sender)) { // checking if sending value is > 0 and sending value is < sender balance | |
__balanceOf[msg.sender] -= _value; | |
__balanceOf[_to] += _value; | |
return true; | |
} | |
return false; | |
} | |
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) { | |
if(__allowances[_from][msg.sender] > 0 && | |
_value > 0 && | |
__allowances[_from][msg.sender] > _value) { | |
__balanceOf[_from] -= _value; | |
__balanceOf[_to] += _value; | |
// Missed from the video | |
__allowances[_from][msg.sender] -= _value; | |
return true; | |
} | |
return false; | |
} | |
function approve(address _spender, uint256 _value) public returns (bool success) { | |
__allowances[msg.sender][_spender] = _value; | |
return true; | |
} | |
function allowance(address _owner, address _spender) public view returns (uint256 remaining) { | |
return __allowances[_owner][_spender]; | |
} | |
} |
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; | |
contract owned { | |
address public owner; | |
constructor() public { | |
owner = msg.sender; | |
} | |
modifier onlyOwner { | |
require(msg.sender == owner); | |
_; | |
} | |
function transferableOwnership(address newOwner) onlyOwner public { | |
owner = newOwner; | |
} | |
} | |
interface tokenRecepient { | |
function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) external; | |
} | |
contract TokenERC20 { | |
// Public variables of Token | |
string public name; | |
string public symbol; | |
uint8 public decimal = 18; // 18 decimal values is strongly suggested. Avoid changing it | |
uint256 public totalSupply; | |
// This creates an array with all balances | |
mapping (address => uint256) public balanceof; | |
mapping (address => mapping (address => uint256)) public allowance; | |
// This generates a public events on blockchain that will notify clients | |
event Transfer(address indexed from, address indexed to , uint256 value); | |
// This generates a public events on blockchain that will notify clients | |
event Approval(address indexed _owner, address indexed _spender, uint256 _value); | |
// This notified client ablout the amount burns | |
event Burn(address indexed From, uint256 value); | |
// /** | |
// * constructor function | |
// * | |
// * Initializes contract with initial supply tokens with creator of contract | |
// */ | |
constructor(uint256 initialSupply, string tokenName, string tokenSymbol) public { | |
totalSupply = initialSupply * 10 ** uint256(decimal); // update total supply with decimal amount | |
balanceof | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment