Created
March 1, 2018 22:43
-
-
Save whiteyhat/4d05731b66cbc5dbbcc446f3a60829b9 to your computer and use it in GitHub Desktop.
Solidity introduction
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 DataType{ | |
//BASIC DATA TYPES | |
bool myBool = false; | |
int8 myInt = -125; | |
uint8 myUint = 255; | |
string myString; | |
string[] myStringArr; | |
byte myByte; | |
bytes1 aByte; | |
bytes32 bytess; | |
//ENUM | |
enum Action{UP, DOWN, LEFT, RIGHT} | |
Action action = Action.UP; | |
//ADDRESS | |
address private owner; | |
function assignAddress(){ | |
owner = msg.sender; | |
owner.balance; | |
owner.transfer(25); | |
} | |
// ARRAYS | |
uint[] myIntArray = [2, 5 ]; | |
function addSlotArr(){ | |
myIntArray.push(1); | |
} | |
function getArrLength() returns (uint){ | |
return myIntArray.length; | |
} | |
//STRUCTS | |
struct Account { | |
uint balance; | |
string name; | |
uint dailyLimit; | |
} | |
Account myAccount; | |
function assignStructValues (){ | |
myAccount.balance = 50; | |
myAccount.name = "Carlos"; | |
myAccount.dailyLimit = 1500; | |
} | |
//MAPPING | |
//mapping + (2 objects to map) + variable name | |
mapping(address => Account) _accounts; | |
function () payable{ | |
_accounts[msg.sender].balance += msg.value; | |
} | |
function getBalance() returns (uint){ | |
return _accounts[msg.sender].balance; | |
} | |
} |
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; | |
library IntExtended { | |
function increment(uint _self) returns (uint){ | |
return _self+1; | |
} | |
function decrement(uint _self) returns (uint){ | |
return _self-1; | |
} | |
function incrementByValue(uint _self, uint _incrementer) returns (uint){ | |
return _self + _incrementer; | |
} | |
function decrementByValue(uint _self, uint _incrementer) returns (uint){ | |
return _self - _incrementer; | |
} | |
} |
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; | |
library Strings{ | |
function concat(string _base, string _value) | |
internal returns (string){ | |
bytes memory _baseBytes = bytes(_base); | |
bytes memory _baseValue = bytes(_value); | |
string memory _tempValue = new string(_baseBytes.length + | |
_baseValue.length); | |
bytes memory result = bytes(_tempValue); | |
//create iterators | |
uint i; | |
uint j; | |
//for each char | |
for(i=0;i< _baseBytes.length; i++){ | |
result[j++] = _baseBytes[i]; | |
} | |
for(i=0; i<_baseValue.length; i++){ | |
result[j++] = _baseValue[i]; | |
} | |
return string(result); | |
} | |
} | |
contract TestString { | |
using Strings for string; | |
function testConcat (string s) returns (string){ | |
return s.concat(" Daniel"); | |
} | |
} |
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; | |
import "browser/Library.sol"; | |
contract UserLibraries{ | |
using IntExtended for uint; | |
function testLibrary(uint test, uint value) returns (uint){ | |
return test.incrementByValue(value); | |
} | |
} |
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 Transaction{ | |
event SenderLogger (address); | |
event ValueLogger (uint); | |
address private owner; | |
modifier JustOwner{ | |
owner == msg.sender; | |
_; | |
} | |
modifier ValidValue{ | |
assert(msg.value >= 1); | |
_; | |
} | |
function Transaction(){ | |
owner = msg.sender; | |
} | |
function () payable JustOwner ValidValue{ | |
SenderLogger(msg.sender); | |
ValueLogger(msg.value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment