Structure Of A Smart Contract In Ethereum

Posted By : Shashwat Gupta | 27-Mar-2018

 

 

 

Let’s take a look at very basic smart contract structure which has written in Solidity.

pragma solidity ^0.4.2;
 

 contract Customer {
    string id;

    function setId(string serial) public {
          id = serial;
    }

    function getId() public constant returns (string) {
          return id;
    }
 }
 

 

 

 

Structure of a Contract

classes in object-oriented languages and Contracts in Solidity are very same . all contract could consist of State Variables, Modifiers and functions, Events, Struct Types and Enum Types. moreover, you  can use other contracts. using inheritance.

 

 

State Variables

 

State variables are  stored in contract storage.

pragma solidity ^0.4.0;

contract myContract {
    uint myData; // State variable
    // ...
}

 

Function

 

Functions are like a few lines of code that can perform something within the contract.

contract Customer {

    string id = 34;
    

    function getId() public constant returns (string) { // function
          return id;
    }
 }

 

Function Modifiers

 

Function modifiers are like a function that checks the validation rules before executing the main function.


contract Purchase {
    address public seller;

    modifier onlySeller() { // Modifier
        require(msg.sender == seller);
        _;
    }

    function abort() public onlySeller { // Modifier usage
        // ...
    }
}

 

Struct Types

 

Struct is custom defined data types that consists of several variables declared in it.

pragma solidity ^0.4.0;

contract Node {
    struct caller { // Struct
        uint id;
        bool isEXists;
        address delegate;
        uint age;
    }
}

 

 

Events

 

Events are functions that are bubbled up after compilation of function execution if event code written there .it provide  EVM logging facilities.

pragma solidity ^0.4.21;

contract Auction {
    event HighestBid(address user, uint amount); // Event

    function bid() public payable {
        // ...
        emit HighestBid(msg.sender, msg.value); // Triggering event
    }
}

 

 

Enum Types

 

Enum is used to create custom types and it has fix the set of values 

 

 

pragma solidity ^0.4.0;

contract shoping {
    enum KIT { cricket, hokey, table-tansis } // Enum
}

 

Structure of a Smart Contract in Ethereum

pragma solidity ^0.4.0;

import "browser/ERC20.sol";

contract MyFirstToken is ERC20 {
    string public constant symbol = "MFT";
    
    uint8 public constant decimals = 18;

    uint private constant __totalSupply = 1000;
    
    mapping (address => mapping (address => uint)) private __allowances;

    function MyFirstToken() {
            __balanceOf[msg.sender] = __totalSupply;
    }

    function totalSupply() constant returns (uint _totalSupply) {
        _totalSupply = __totalSupply;
    }

    function balanceOf(address _addr) constant returns (uint balance) {
        return __balanceOf[_addr];
    }

    function transfer(address _to, uint _value) returns (bool success) {
        if (_value > 0 && _value <= balanceOf(msg.sender)) {
            __balanceOf[msg.sender] -= _value;
            __balanceOf[_to] += _value;
            return true;
        }
        return false;
    }

    function transferFrom(address _from, address _to, uint _value) returns (bool success) {
        if (__allowances[_from][msg.sender] > 0 &&
            _value > 0 &&
            __allowances[_from][msg.sender] >= _value && 
            __balanceOf[_from] >= _value) {
            __balanceOf[_from] -= _value;
            __balanceOf[_to] += _value;
            // Missed from the video
            __allowances[_from][msg.sender] -= _value;
            return true;
        }
        return false;
    }

    function approve(address _spender, uint _value) returns (bool success) {
        __allowances[msg.sender][_spender] = _value;
        return true;
  }

About Author

Author Image
Shashwat Gupta

Shashwat is a bright Mean Stack Developer . He has good experience in development of complex UI's of web application and hybrid mobile applications.

Request for Proposal

Name is required

Comment is required

Sending message..