Giter Club home page Giter Club logo

lesson-03's Introduction

Lesson 3 - Syntax and structure

Function definition

  • Replacing memory with calldata when stack is enough
  • Definitions
    • Visibility
    • State mutability
    • Modifiers
    • Virtual
    • Override

References

https://docs.soliditylang.org/en/latest/introduction-to-smart-contracts.html#storage-memory-and-the-stack

Code references

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;

contract HelloWorld {
    string private text;

    constructor() {
        text = "Hello World";
    }

    function helloWorld() public view returns (string memory)  {
        return text;
    }

    function setText(string calldata newText) public {
        text = newText;
    }
}

Variable declaration and definition

  • Elementary types
    • Booleans
    • Integers
    • Fixed
    • Address
    • Bytes
    • Strings
  • State Variables
  • Constants
  • Data locations (again)
  • Arrays
  • Mappings

References

https://docs.soliditylang.org/en/latest/types.html

Exercises

  • Extending new features
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract HelloWorld {
    string private text;

    constructor() {
        text = pureText();
    }

    function helloWorld() public view returns (string memory) {
        return text;
    }

    function setText(string calldata newText) public {
        text = newText;
    }

    function pureText() public pure returns (string memory) {
        return "Hello World";
    }

    function _isPure() internal view returns (bool _check) {
        _check = keccak256(bytes(text)) == keccak256(bytes(pureText()));
    }

    function isPure() public view returns (bool _returnValue) {
        _returnValue = _isPure();
    }

    function _restore() internal {
        text = pureText();
    }

    function restore() public returns (bool) {
        if (_isPure()) return false;
        _restore();
        return true;
    }
}

Common Solidity Global Variables

  • Reserved words and global variables that a programmer should know
  • Global variables about blockchain state
  • Global variables about the transaction
  • Global variables about the transaction message

References

https://docs.soliditylang.org/en/latest/units-and-global-variables.html

Assertion and Modifiers

  • How errors are handled on solidity (briefly)
  • Assertion
  • Require statements
  • Modifiers
  • Where to use modifiers

References

https://docs.soliditylang.org/en/latest/control-structures.html#error-handling-assert-require-revert-and-exceptions

https://docs.soliditylang.org/en/latest/structure-of-a-contract.html#function-modifiers

Homework

  • Create Github Issues with your questions about this lesson
  • Read the references

lesson-03's People

Contributors

matheusdaros avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.