C#

Basic Programs in Solidity

In this article, I will provide some Basic Programs in Solidity.

  1. Hello World Program.
pragma solidity ^0.8.0;

contract HelloWorld {
    string greeting = "Hello, World!";

    function sayHello() public view returns (string memory) {
        return greeting;
    }
}
  1. Simple Math Operations.
pragma solidity ^0.8.0;

contract SimpleMath {
    uint256 public sum;
    uint256 public product;

    function add(uint256 a, uint256 b) public {
        sum = a + b;
    }

    function multiply(uint256 a, uint256 b) public {
        product = a * b;
    }
}
  1. Simple Storage.
pragma solidity ^0.8.0;

contract SimpleStorage {
    uint256 storedData;

    function set(uint256 x) public {
        storedData = x;
    }

    function get() public view returns (uint256) {
        return storedData;
    }
}
  1. Structs and Arrays.
pragma solidity ^0.8.0;

contract StructsAndArrays {
    struct Person {
        string name;
        uint256 age;
    }

    Person[] public people;

    function addPerson(string memory _name, uint256 _age) public {
        people.push(Person(_name, _age));
    }
}
  1. Mapping.
pragma solidity ^0.8.0;

contract MappingExample {
    mapping (uint256 => string) public names;

    function addName(uint256 id, string memory name) public {
        names[id] = name;
    }
}
  1. Function Modifiers.
pragma solidity ^0.8.0;

contract FunctionModifiers {
    address public owner;

    constructor() {
        owner = msg.sender;
    }

    modifier onlyOwner() {
        require(msg.sender == owner, "Only owner can call this function.");
        _;
    }

    function doSomething() public onlyOwner {
        // function code goes here
    }
}
  1. Inheritance.
pragma solidity ^0.8.0;

contract ParentContract {
    uint256 public x;

    function setX(uint256 _x) public {
        x = _x;
    }
}

contract ChildContract is ParentContract {
    function doSomething() public {
        // function code goes here
    }
}
  1. Events.
pragma solidity ^0.8.0;

contract EventExample {
    event NewPerson(string name, uint256 age);

    function addPerson(string memory _name, uint256 _age) public {
        emit NewPerson(_name, _age);
    }
}
  1. Payable Functions.
pragma solidity ^0.8.0;

contract PayableFunction {
    function buySomething() public payable {
        // function code goes here
    }
}
  1. Time-Based Functions.
pragma solidity ^0.8.0;

contract TimeBasedFunction {
    uint256 public startTime;

    function start() public {
        startTime = block.timestamp;
    }

    function elapsedTime() public view returns (uint256) {
        return block.timestamp - startTime;
    }
}
  1. Random Number Generation.
pragma solidity ^0.8.0;

contract RandomNumber {
    function getRandomNumber(uint256 max) public view returns (uint256) {
        uint256 randomNumber = uint256(keccak256(abi.encodePacked(block.timestamp, msg.sender))) % max;
        return randomNumber;
    }
}
  1. Enums.
pragma solidity ^0.8.0;

contract EnumsExample {
    enum State { PENDING, APPROVED, REJECTED }
    State public state;

    function setState(uint


Further Reading

Some Examples of MongoDB Documents

20 Project Ideas on Finance in PHP for College Students

Getting Started with Solidity

20+ Interview Questions on Solidity

Overview of Mean Stack

Creating Single Page Applications with Angular

Angular 10 Data Binding in Different Ways

Creating Some Angular Components

Examples of Array Functions in PHP

Basic Programs in PHP

princites.com

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *