Skip to main content

Posts

Showing posts with the label Learn solidity

LEARN SOLIDITY LESSON 10: Function Modifier Solidity. OpenZeppelin's Ownable contract

LEARN SOLIDITY LESSON Function Modifier Solidity. OpenZeppelin's Ownable contract Function Modifier Solidity A function modifier looks just like a function, but uses the keyword modifier instead of the keyword function. And it can't be called directly like a function can — instead we can attach the modifier's name at the end of a function definition to change that function's behavior. modifier onlyOwner() { require(isOwner()); _; } function renounceOwnership() public onlyOwner { emit OwnershipTransferred(_owner, address(0)); _owner = address(0); } //Step code run when call: renounceOwnership 1. require(isOwner()); 2. _; 3. emit OwnershipTransferred(_owner, address(0)); 4. _owner = address(0); OpenZeppelin's Ownable contract Below is the Ownable contract taken from the OpenZeppelin Solidity library. OpenZeppelin is a library of secure and community-vetted smart contracts that you can use in your own DApps.  /** * @title Ownable * @dev...

LEARN SOLIDITY LESSON 9: Handling Multiple Return Values in Solidity

LEARN SOLIDITY LESSON Return Values . Multiple Return Values in Solidity Multiple Return Values in Solidity This multipleReturns function is the first example we've seen that returns multiple values. Let's look at how to handle them: function multipleReturns() internal returns(uint a, uint b, uint c) { return (1, 2, 3); } function processMultipleReturns() external { uint a; uint b; uint c; // get all A,B,C (a, b, c) = multipleReturns(); } function getLastReturnValue() external { uint c; (,,c) = multipleReturns(); //only C }

LEARN SOLIDITY LESSON 8: Interacting with other contracts. Interface Solidity

LEARN SOLIDITY LESSON Interface Solidity. Interacting with other contracts.  Interface Solidity.  Define an interface of the LuckyNumber contract: contract LuckyNumber { mapping(address => uint) numbers; function setNum(uint _num) public { numbers[msg.sender] = _num; } function getNum(address _myAddress) public view returns (uint) { return numbers[_myAddress]; } } contract NumberInterface { function getNum(address _myAddress) public view returns (uint); } 1. For one, we're only declaring the functions we want to interact with — in this case getNum — and we don't mention any of the other functions or state variables. 2. Secondly, we're not defining the function bodies. Instead of curly braces ({ and }), we're simply ending the function declaration with a semi-colon (;). Interacting with other contracts.  In this way, your contract can interact with any other contract on the Ethereum blockchain, as long they expose those functions as public or ext...

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...

LEARN SOLIDITY LESSON 5: Mappings Solidity. Addresses Ethereum. Msg.sender Solidity

LEARN SOLIDITY LESSON Mappings Solidity. Addresses Ethereum. Msg.sender Solidity Addresses Ethereum The Ethereum blockchain is made up of accounts. Each account has an address, which you can think of like a bank account number. It's a unique identifier that points to that account, and it looks like this: 0x0cE446255506E92DF41614C46F1d6df9Cc969169 Mappings Solidity A mapping is essentially a key-value store for storing and looking up data. In the first example, the key is an address and the value is a uint, and in the second example the key is a uint and the value a string. mapping (address => uint) favoriteNumber; function setMyNumber(uint _myNumber) public { favoriteNumber[msg.sender] = _myNumber; } Msg.sender Solidity In Solidity, there are certain global variables that are available to all functions. One of these is msg.sender, which refers to the address of the person (or smart contract) who called the current function. mapping (address => uint) favoriteNumber; ...

LEARN SOLIDITY LESSON 3: Function Declarations. Private / Public Functions. Return Values of Functions.

LEARN SOLIDITY LESSON Function Declarations. Private / Public Functions. Return Values of Functions. Function Declarations A function declaration in solidity looks like the following: function eatHamburgers(string memory _name, uint _amount) public { } //CALL eatHamburgers("vitalik", 100); Private and Public Functions In Solidity, functions are public by default. Need practice to mark your functions as private by default, and then only make public the functions you want to expose to the world. uint[] numbers; //private function _addToArray(uint _number) private { } //public function addToArray(uint _number) public { } Return Values of Functions function declaration contains the type of the return value. To return a value from a function, the declaration looks like this: Summary for English Visiter string strHello = "learn-solidity.blogspot.com"; function sayHello() public  view returns (string memory) { return strHello; } function _multiply(uint a, uint b...

LEARN SOLIDITY LESSON 1: Contracts. Pragma. State Variables. Integers

LEARN SOLIDITY LESSON 1 Contracts. Pragma. State Variables. Integers An empty contract named HelloWorld would look like this: All solidity source code should start with a "version pragma" — a declaration of the version of the Solidity compiler this code should use.  State variables are permanently stored in contract storage. This means they're written to the Ethereum blockchain. Think of them like writing to a DB. pragma solidity >=0.5.0 <0.6.0; contract HelloWorld {   uint myUnsignedInteger = 10; }