Skip to content

Instantly share code, notes, and snippets.

View sepisoltani's full-sized avatar
🎯
Focusing

Sepehr Soltani sepisoltani

🎯
Focusing
View GitHub Profile
contract NumberInterface {
function getNum(address _myAddress) public view returns (uint);
}
contract Sandwich {
uint private sandwichesEaten = 0;
function eat() internal {
sandwichesEaten++;
}
}
contract BLT is Sandwich {
uint private baconSandwichesEaten = 0;
import "./someothercontract.sol";
contract newContract is SomeOtherContract {
}
contract Animal {
function walk() public returns (string memory) {
return "walking";
}
}
contract Cat is Animal {
function run() public returns (string memory) {
return "running";
}
contract Example{
function sayHiToVitalik(string memory _name) public returns (string memory) {
// Compares if _name equals "Vitalik". Throws an error and exits if not true.
// (Side note: Solidity doesn't have native string comparison, so we
// compare their keccak256 hashes to see if the strings are equal)
require(keccak256(abi.encodePacked(_name)) == keccak256(abi.encodePacked("Vitalik")));
// If it's true, proceed with the function:
return "Hi!";
}
contract Example{
mapping (address => uint) favoriteNumber;
function setMyNumber(uint _myNumber) public {
// Update our `favoriteNumber` mapping to store `_myNumber` under `msg.sender`
favoriteNumber[msg.sender] = _myNumber;
// ^ The syntax for storing data in a mapping is just like with arrays
}
contract Example{
// For a financial app, storing a uint that holds the user's account balance:
mapping (address => uint) public accountBalance;
}
YourContract.IntegersAdded(function(error, result) {
alert("event fired")
})
contract Example{
// declare the event
event IntegersAdded(uint x, uint y, uint result);
function add(uint _x, uint _y) public returns (uint) {
uint result = _x + _y;
// fire an event to let the app know the function was called:
emit IntegersAdded(_x, _y, result);
return result;
contract Example{
uint8 a = 5;
uint b = 6;
// throws an error because a * b returns a uint, not uint8:
uint8 c = a * b;
// we have to typecast b as a uint8 to make it work:
uint8 c = a * uint8(b);