Skip to content

Instantly share code, notes, and snippets.

@annu18priya
Created August 10, 2018 13:18
Show Gist options
  • Save annu18priya/ce6d8e7a2a035527602cc5b2a348549c to your computer and use it in GitHub Desktop.
Save annu18priya/ce6d8e7a2a035527602cc5b2a348549c 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=undefined&optimize=false&gist=
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;
}
}
}
pragma solidity ^0.4.7;
import "remix_tests.sol"; // this import is automatically injected by Remix.
import "./ballot.sol";
contract test3 {
Ballot ballotToTest;
function beforeAll () {
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 constant returns (bool) {
return ballotToTest.winningProposal() == 1;
}
}
pragma solidity ^0.4.0;
contract DataType {
bool myBool = false;
int8 myInt = -128;
uint8 myUint = 255;
string myString;
uint8[] myStringArr;
byte myValue;
bytes1 myBytes1;
bytes32 myBytes32;
//fixed256x8 myFixed = 1; //255.0
//ufixed myFixed = 1;
enum Action {ADD, REMOVE, UPDATE}
Action myAction = Action.ADD;
address myAddress;
function assignAddress() {
myAddress = msg.sender; // It means that the person who actually deployed the contract
myAddress.balance;
myAddress.transfer(10);
}
// Defining Array with not fixed length
uint[] myIntArr = [1, 2, 3];
function arrFunc() {
myIntArr.push(4);
myIntArr.length;
myIntArr[0];
}
// Defining Array of fixed length
uint[10]myFixedArr;
// Defining struct
struct Account {
uint balance;
uint dailyLimit;
}
Account myAccount;
function structFunc() {
myAccount.balance = 100;
}
mapping (address => Account) _accounts;
function() payable {
_accounts[msg.sender].balance += msg.value;
}
function getBalance() returns (uint) {
return _accounts[msg.sender].balance;
}
}
pragma solidity ^0.4.0;
contract Debugging {
uint[] private vars;
// assignment is storing values in stack
function assignment() {
uint myVal1 = 1;
uint myVal2 = 2;
assert(myVal1 == myVal2);
}
function memoryAlloc() {
string memory myString = "test";
assert(bytes(myString).length == 10);
}
function storageAlloc() {
vars.push(3);
vars.push(4);
assert(vars.length == 4);
}
}
pragma solidity ^0.4.0;
library Strings {
function concat(string _base, string _value) internal returns (string) {
// converting string to bytes
bytes memory _baseBytes = bytes(_base); // Type casting values of string to bytes and defining the storage type of it.
bytes memory _valueBytes = bytes(_value);
string memory _tmpValue = new string(_baseBytes.length + _valueBytes.length); // Type casting bytes to string
bytes memory _newValue = bytes(_tmpValue); // Now again type casting string to bytes
uint i; // its iterator over given value. i.e, _baseBytes, _valueBytes
uint j; // its iterator over new value
for(i=0; i< _baseBytes.length; i++) {
_newValue[j++] = _baseBytes[i];
}
for(i=0; i< _valueBytes.length; i++) {
_newValue[j++] = _valueBytes[i];
}
return string(_newValue);
}
function strPos(string _base, string _value) internal returns(int) {
bytes memory _baseBytes = bytes(_base);
bytes memory _valueBytes = bytes(_value);
assert(_valueBytes.length == 1);
for(uint i = 0; i< _baseBytes.length; i++) {
if(_baseBytes[i] == _valueBytes[0]) {
return int(i);
}
}
return -1;
}
function substring(string str, uint startIndex, uint endIndex) constant returns (string) {
bytes memory strBytes = bytes(str);
bytes memory result = new bytes(endIndex-startIndex);
for(uint i = startIndex; i < endIndex; i++) {
result[i-startIndex] = strBytes[i];
}
return string(result);
}
}
contract TestStrings {
using Strings for string;
function testConcat(string _base) returns (string) {
return _base.concat("_suffix");
}
function needleInHayStack(string _base) returns (int) {
return _base.strPos("t");
}
function findSubStringFromString(string _base) returns (string) {
return _base.substring(4, 25);
}
}
@annu18priya
Copy link
Author

pragma solidity ^0.4.0;

contract DataType {

bool myBool = false;
int8 myInt = -128;
uint8 myUint = 255;

string myString;
uint8[] myStringArr;

byte myValue;
bytes1 myBytes1;
bytes32 myBytes32;

//fixed256x8 myFixed = 1; //255.0
//ufixed myFixed = 1;

enum Action {ADD, REMOVE, UPDATE}
Action myAction = Action.ADD;

address myAddress;

function assignAddress() {
    myAddress = msg.sender; // It means that the person who actually deployed the contract
    myAddress.balance;
    myAddress.transfer(10);
}

// Defining Array with not fixed length

uint[] myIntArr = [1, 2, 3];

function arrFunc() {
    myIntArr.push(4);
    myIntArr.length;
    myIntArr[0];
}

// Defining Array of fixed length

uint[10]myFixedArr;

// Defining struct

struct Account {
    uint balance;
    uint dailyLimit;
}

Account myAccount;

function structFunc() {
    myAccount.balance = 100;
}


// mapping (_KeyType => _ValueType) mapName.
// Here we can look for the Ethereum address of Account and then fetch the values of those Account. i.e, balance, dailyLimit
mapping (address => Account) _accounts;


// payable is a keyword which means that you can collect/receive funds in your contract.
// All the ethers sent to payable functions are owned by contract. 
// Payable fallback functions : A function without any name and annotated with payable keyword is called payable fallback function. There could be only one such function in contract.


function() payable {
    _accounts[msg.sender].balance += msg.value;
}

function getBalance() returns (uint) {
    return _accounts[msg.sender].balance;
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment