Skip to main content

Posts

Showing posts from August, 2022

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 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 7: Storage & Memory. Internal & External in Solidity

LEARN SOLIDITY LESSON Storage & Memory in Solidity. Internal & External in Solidity Storage & Memory in Solidity Storage refers to variables stored permanently on the blockchain.  Memory variables are temporary, and are erased between external function calls to your contract.  Think of it like your computer's hard disk vs RAM. Sandwich[] sandwiches; function eatSandwich(uint _index) public { // So instead, you should declare with the ` storage ` keyword Sandwich storage mySandwich = sandwiches[_index]; mySandwich.status = "Eaten!"; // If you just want a copy, you can use ` memory `: Sandwich memory anotherSandwich = sandwiches[_index + 1]; anotherSandwich.status = "Eaten!"; sandwiches[_index + 1] = anotherSandwich; } Internal & External in Solidity Internal is the same as private, except that it's also accessible to contracts that inherit from this contract.  External is similar to public, except

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 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. //SOLIDITY event IntegersAdded(uint x, uint y, uint result); function add(

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 2: Math Operations. Structs. Arrays in solidity

LEARN SOLIDITY LESSON Math Operations. Structs. Arrays in solidity Math Operations in Solidity Math in Solidity is pretty straightforward. The following operations are the same as in most programming languages: pragma solidity >=0.5.0 <0.6.0; contract HelloWorld {      uint x2 = 5 + 2; //= 7      uint x1 = 5 ** 2; //5^2 = 25    uint x2 = 13 % 5; //= 3 } Structs and Arrays in Solidity Structs Structs allow you to create more complicated data types that have multiple properties. struct Person { uint age; string name; } Arrays When you want a collection of something, you can use an array. There are two types of arrays in Solidity: fixed arrays and dynamic arrays: uint[2] fixedArray; string[5] stringArray; uint[] dynamicArray; struct Person { uint age; string name; } Person[] people; Structs and Arrays struct Person { uint age; string name; } Person[] public people; Person satoshi = Person(172, "Satoshi"); people.push(satoshi);

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; }