Skip to main content

Command Palette

Search for a command to run...

Writing Our First Smart Contract

Updated
4 min readView as Markdown
Writing Our First Smart Contract

Intro to Smart Contracts

  1. A smart contract is a self-executing contract with the terms of the agreement between buyer and seller being directly written into lines of code.

  2. The code and the agreements contained therein exist on a decentralized blockchain network, meaning that the contract is stored and executed on a distributed network of computers instead of a central authority.

  3. Smart contracts allow for automation and decentralization of a wide range of processes, including financial transactions, voting systems, and supply chain management.

  4. Once the conditions of the smart contract are met, the contract is automatically executed, without requiring intermediaries, saving time and money. Smart contracts are a key innovation of blockchain technology and have the potential to revolutionize the way we do business and interact with one another.

Writing Our First Smart Contract

pragma solidity ^0.8.0;
contract SimpleStorage { uint storedData;

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

function get() public view returns (uint) { return storedData; } }
  1. The first line in a smart contract is the pragma line following are the features of the pragma line.

    • The pragma statement in a Solidity smart contract specifies the version of the Solidity compiler that should be used to compile the contract.

    • It is typically included at the beginning of the contract code, before any other code.

    • The pragma statement has the following syntax:

        phpCopy codepragma solidity <version>;
      
    • Where <version> is the version of the Solidity compiler that should be used to compile the contract.

    • The version can be specified using a range of different formats, depending on the level of specificity that is required.

    • For example, ^0.8.0 specifies that any version of the compiler that is compatible with version 0.8.0 or higher can be used, while >=0.7.0 <0.8.0 specifies that any version of the compiler between 0.7.0 and 0.8.0 (excluding 0.8.0) can be used.

    • Specifying the version of the Solidity compiler that should be used is important because newer versions of the compiler may introduce breaking changes that could cause older contracts to behave unexpectedly.

    • By specifying the version of the compiler that should be used, contract developers can ensure that their contracts will be compiled correctly and will behave as expected.

  2. In Solidity, a contract is a class-like structure that defines a set of data and functions.

  3. It is similar to a class in object-oriented programming, but with some differences due to the decentralized nature of smart contracts.

  4. In Solidity, a contract is defined using the contract keyword, followed by the name of the contract and its contents inside curly braces. Here is an example of a simple contract:

     pragma solidity ^0.8.0;
    
     contract MyContract {
         uint public myVariable;
    
         function setMyVariable(uint newValue) public {
             myVariable = newValue;
         }
     }
    
  5. In this example, MyContract is the name of the contract. The uint public myVariable statement declares a state variable called myVariable that is of type uint (unsigned integer) and is accessible to anyone on the blockchain. The function setMyVariable declares a function that can be used to set the value of myVariable

  6. SPDX (Software Package Data Exchange) is a standard for communicating software licenses, copyrights, and other related information about software packages.

  7. The SPDX license list is a comprehensive list of commonly found open source and free software licenses, maintained by the SPDX community.

  8. The SPDX license list includes a short identifier (such as MIT or GPL-2.0-only), the full name of the license, and a link to the license text.

  9. The SPDX license list and standard are useful for software development projects that need to track and communicate license information about the software packages they use or distribute.

  10. The SPDX standard provides a consistent and machine-readable way to communicate license information, making it easier for developers to manage and track license information in their software projects.

  11. By using SPDX, developers can more easily and accurately track the licenses that apply to the software packages they use, and ensure compliance with those licenses.

  12. SPDX also helps with license compatibility analysis and other license-related tasks.

More from this blog