Ethereum • Solidity • Blockchain Automation

Smart Contracts

Smart contracts are self-executing blockchain programs that can automate agreements, transfers, digital assets and business workflows without relying on a traditional middle entity.

Introduction

In a blockchain network, a smart contract can automatically execute a transaction when predefined conditions are met. For example, one user may send cryptocurrency to another user in exchange for something of value, and the exchange can be governed by smart contract logic.

Smart contracts can also support digital collectibles, bidding systems, Internet of Things payments, supply chain transactions and many other automated blockchain workflows.

Simple idea: A smart contract is blockchain code that executes agreement logic automatically when specific conditions are satisfied.
A

Automatic

Executes when programmed conditions are met.

T

Transparent

The contract logic and transaction history can be verified on-chain.

I

Irreversible

Blockchain transactions are difficult to reverse after confirmation.

Definition of Smart Contract

A smart contract can be defined as computer code that facilitates the exchange of money, content, property, shares, digital assets or anything of value among parties without a traditional middle entity.

When installed in a blockchain system, it behaves like a self-operating program. It executes according to programmed terms and conditions and runs on the blockchain network.

A smart contract is a programmable agreement that can run on a decentralized blockchain network.

A Brief History of Smart Contracts

Smart contracts were not originally invented by Ethereum. The idea was introduced by computer scientist and cryptographer Nick Szabo in the 1990s. He described smart contracts using the idea of a digital vending machine: users input value or data and receive an output according to rules.

Ethereum later made smart contracts widely accessible by providing a programmable blockchain platform for decentralized applications.

Smart Contract Platforms

Ethereum is the best-known smart contract platform, but it is not the only one. Enterprise blockchain platforms can also support their own versions of smart contract logic.

Ethereum

Uses the Ethereum Virtual Machine and smart contracts commonly written in Solidity.

Hyperledger Fabric

Uses chaincode, often deployed in containerized environments.

Hyperledger Sawtooth

Enterprise blockchain platform that can support different transaction families.

Corda

Enterprise distributed ledger platform often used for business and finance workflows.

Solidity

Solidity is a high-level programming language used to create and implement smart contracts on Ethereum. Smart contracts written in Solidity can be used for financial transactions, crowdfunding, voting, supply chain management, IoT implementation, ride-sharing automation, smart city systems and other blockchain applications.

Solidity has influences from languages such as Python, C++ and JavaScript, making it approachable for developers who already know these programming languages.

Common Uses

Tokens, voting, escrow, crowdfunding, DeFi, NFTs and workflow automation.

Developer Background

Useful for programmers familiar with JavaScript-style syntax and object-oriented concepts.

Writing and Deploying with Remix IDE

Remix is a browser-based integrated development environment used to write, compile, test and deploy Solidity smart contracts. It includes a compiler and a runtime environment, making it convenient for beginners.

1

Write

Create a Solidity file and write the smart contract code.

2

Compile

Use the Solidity compiler to check and build the contract.

3

Deploy

Deploy the compiled smart contract to a test environment or blockchain network.

The original article used older Ethereum testnet examples. Always verify the current recommended testnet, wallet setup and compiler version before deploying any contract.

The Smart Contract Code

The article introduces a simple Solidity contract named MyStorage. It stores a single unsigned integer and provides two functions: one to set the value and one to read it.

MyStorage.sol
pragma solidity ^0.4.25;

contract MyStorage {
    uint storedData;

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

    function get() public view returns (uint) {
        return storedData;
    }
}
This code reflects the original tutorial version. For new projects, choose a current Solidity compiler version and update the syntax where needed.

Understanding the Code

The first line tells the compiler which Solidity version the source code is written for. The keyword pragma is an instruction to the compiler.

The line uint storedData; declares a state variable called storedData. This variable is stored in the contract state on the blockchain.

contract MyStorage

Defines a smart contract named MyStorage.

uint storedData

Creates a state variable for storing an unsigned integer.

set(uint x)

Updates the stored number with a new value.

get()

Returns the current value stored in the contract.

Smart Contract Deployment Workflow

The original article demonstrates a deployment process using Remix, MetaMask, a test network, test Ether and Etherscan-style transaction viewing. The overall workflow remains useful as a learning model.

Write the Solidity contract in Remix
Compile the contract with the matching Solidity compiler
Connect a wallet such as MetaMask to a test network
Deploy the contract and confirm the transaction
Check the transaction and interact with the deployed contract

Important Safety Notes

  • Test contracts on a test network before using real funds.
  • Do not deploy to mainnet until the contract is reviewed, tested and audited.
  • Use current Solidity versions and current wallet/testnet instructions.
  • Understand gas fees, deployment cost and irreversible blockchain transactions.

Summary

  • Smart contracts are self-executing blockchain programs.
  • They can automate exchanges of value, digital assets and agreement logic.
  • Nick Szabo introduced the smart contract idea before Ethereum became popular.
  • Solidity is commonly used to write Ethereum smart contracts.
  • Remix is a beginner-friendly IDE for writing, compiling, testing and deploying contracts.

Continue Learning