Copied


Smart Contract 101: What is the Structure of a Contract?

Johnny J   Oct 28, 2020 06:45 2 Min Read


20201028_what is the sturucture of a contract_feature.jpg

Below is an example of a simple smart contract. Let's observe the structure of the contract and go through each line of its code.

pragma solidity >=0.4.16 <0.8.0;
contract FirstContract{
    string _name;
    function setName(string name) public{
        _name = name;
    }
    function getName() view public returns(string){
        return _name;
    }
}

The First Line pragma solidity >=0.4.22 <0.6.0; is telling the smart contract compiler which version of solidity compiler code you are using.

At line 2, FirstContract could be replaced by any text, this is the place for you to name or title your contract.

Inside the curly brackets, there is a string variable called _name, this line of code will record a value to be stored on the blockchain

Also, there are two functions: setName() and getName().

setName takes a string input name. Its type of visibility is public, which means it can be called both internally and externally. Inside the function, the parameter name will be replacing the existing value stored by _name. As this function will change data on the blockchain, it will cost ether.

Another function getName, view means the function is read-only. This function returns a string value _name which is the same as the one we stored on the blockchain.

The combination of the above components creates a simple smart contract.

In the next lesson, we will describe the life-cycle of developing smart contracts.


More Tool: BTC to USD Price Calculator

Read More
The Hong Kong Monetary Authority has issued a warning about a fraudulent website posing as OCBC Bank (Hong Kong) Limited, urging public vigilance.
BitMEX has changed the Mark Method for NILUSDTH25 and REDUSDTZ25 to Fair Price marking, effective March 25, 2025, enhancing price accuracy.
Bitcoin remains vulnerable to downward pressure due to tight liquidity conditions and weak investor sentiment, with ETF outflows and cautious market behavior persisting.
Vodafone implements AI-driven solutions using LangChain and LangGraph to optimize data operations and improve performance metrics monitoring and information retrieval across its data centers.
BitMEX introduces NILUSDT perpetual swaps, offering traders up to 50x leverage. This new listing enhances trading options on the platform.
Cronos (CRO) Labs has appointed Mirko Zhao as its new leader, succeeding Ken Timsit. Zhao aims to enhance the blockchain’s growth and community engagement.
BitMEX announces the introduction of NILUSDT perpetual swap listing, offering traders up to 50x leverage. The NIL token will be available for trading starting March 25, 2024.
Cronos (CRO) Labs announces Mirko Zhao as the new Head of Product and Engineering, succeeding Ken Timsit, to lead the blockchain ecosystem's innovative growth.