Skip to main content

LEARN SOLIDITY LESSON 4: Keccak256. Typecasting. Events Solidity

LEARN SOLIDITY LESSON

Keccak256 Solidity. Typecasting Solidity. Events Solidity


Keccak256 Solidity

Ethereum has the hash function keccak256 built in, which is a version of SHA3. A hash function basically maps an input into a random 256-bit hexadecimal number. A slight change in the input will cause a large change in the hash.
//6e91ec6b618bb462a4a6ee5aa2cb0e9cf30f7a052bb467b0ba58b8748c00d2e5
keccak256(abi.encodePacked("aaaab"));
//b1f078126895a1424524de5321b339ab00408010b7cf0e6ed451514981e58aa9
keccak256(abi.encodePacked("aaaac"));

Typecasting Solidity

Sometimes you need to convert between data types. Take the following example:

int8 a = 5;
uint b = 6;
uint8 c = a * uint8(b);

Events Solidity

Events are a way for your contract to communicate that something happened on the blockchain to your app front-end, which can be 'listening' for certain events and take action when they happen.
//SOLIDITYevent IntegersAdded(uint x, uint y, uint result);

function add(uint _x, uint _y) public returns (uint) {
  uint result = _x + _y;
  // run
  emit IntegersAdded(_x, _y, result);
  return result;
}//JAVASCRIPTYourContract.IntegersAdded(function(error, result) {
  // do something with result
})


Comments

Popular posts from this blog

LEARN SOLIDITY LESSON 11: Payable function. Withdraws Ether & Transfer ETH. Check Balance in SOLIDITY

LEARN SOLIDITY LESSON 11 Payable function. Withdraws Ether & Transfer ETH. Check Balance in SOLIDITY Payable function payable functions are part of what makes Solidity and Ethereum so cool — they are a special type of function that can receive Ether. contract OnlineStore { function buySomething() external payable { // Check to make sure 0.001 ether was sent to the function call: require(msg.value == 0.001 ether); // If so, some logic to transfer the digital item to the caller of the function: transferThing(msg.sender); } } Note: If a function is not marked payable and you try to send Ether to it as above, the function will reject your transaction. Withdraws Ether. Get Balance ETH You can write a function to withdraw Ether from the contract as follows: contract GetPaid is Ownable { function withdraw() external onlyOwner { address payable _owner = address(uint160(owner())); _owner.transfer(address(this).balance); } } It is important to note that you c

LEARN SOLIDITY LESSON 6: Require. Inheritance. Import Solidity

LEARN SOLIDITY LESSON Require Solidity. Inheritance Solidity. Import Solidity Require Solidity For that we use require. require makes it so that the function will throw an error and stop executing if some condition is not true function sayHiToBean(string memory _name) public returns (string memory) { // Compares if _name equals "Bean". Throws an error and exits if no require(keccak256(abi.encodePacked(_name)) == keccak256(abi.encodePacked("Bean"))); // If it's true, proceed with the function: return "Hi!"; } Inheritance Solidity This can be used for logical inheritance (such as with a subclass, a Cat is an Animal). But it can also be used simply for organizing your code by grouping similar logic together into different contracts. contract Cat { function catchphrase() public returns (string memory) { return "So Wow CryptoCat"; } } contract BabyCat is Cat { function anotherCatchphrase() public returns (string memory) { re