DEV Community

Cover image for Solidity and Smart Contracts

Solidity and Smart Contracts

cpustejovsky profile image Charles Clinton Pustejovsky III Updated on ・3 min read

Comment or make a PR if you have any suggestions, criticism, or other feedback

Smart Contracts

Smart contracts are accounts on the Ethereum network that don't have private keys like the Ethereum you may own on a wallet. Why would you want to use smart cntracts? I've talked about this elsewhere if you want to read more.

mmmmm... smart contracts

The name however is confusing. According to Andreas Antonopoulos and Gavin Wood

"Ethereum smart contracts are neither smart nor legal contracts [but rather are] immutable computer programs that run deterministically in the context of an Ethereum Virtual Machine as [EVM]part of the Ethereum network protocol..."

Simpler Definition

  • Smart contracts cannot be changed once set live. The only way to change them is to set up a new smart contract with those changes.
    • The opcode for this is SELFDESTRUCT
  • Given the same varibles, a smart contract will run the same way every time. It's not random.
  • The EVM provides a sandboxed environment: if you have it running on your computer, it can access transaction details and network details, but it can't look at all the memes you have saved on your hard drive.

How Programs Run, the EVM, and Executing Smart Contracts

There are several ways programs can run. They can be compiled, interpretted, or make use of virtual machines.


Some programming languages like C, C++, and Go are compiled into machine code your processor understands and then are run.

Interpretted or Just-In-Time(JIT) Compiled

Other programming languages like JavaScript, Python, and Ruby compile at run-time.

Virtual Machines and Byte-Code

Java compiles into byte-code that the Java Virtual Machine runs. That's why Java has the tag-line "write once and run anywhere".

Solidity compiles into EVM byte-code. This byte-code is then deployed to the Ethereum platform.

You generally run a program by double-clicking it or putting ./ in front of it but smart contracts only run when called by a transaction.

Diagram of EVM


You could build smart contracts in JavaScript or C++ or Python or something, but because there are specific constraints and contexts related to the EVM, it's easier to build a language specifically for the task.

Currently, Solidity is the most popular language for programming smart contracts. It is an imperative programming language like C++ or Java because as Andreas/Gavin points out, "Programmers, like most humans, resist change!"

It uses semantic versioning

Example Code

I'm new to Solidity, so I used CryptoZombies to learn the basics. This is what I created from Lesson 1:

//sets the version of solidity that this smart contract will work with
pragma solidity ^0.4.25;

//fundamental building block of ETH applications
contract ZombieFactory {

    // event declaration
    event NewZombie(uint zombieId, string name, uint dna);

    //uint: unsigned integers (means you know they are positive)
    uint dnaDigits = 16;
    uint dnaModulus = 10 ** dnaDigits;

    //like an object in JavaScript, allows for more complex storage of key-value pairs
    struct Zombie {
        string name;
        uint dna;
    //public array that adds Zombie structs
    Zombie[] public zombies;

    //private function; only set functions public if you want to expose them to the world
    function _createZombie(string _name, uint _dna) private {
        zombies.push(Zombie(_name, _dna));
        uint id = zombies.push(Zombie(_name, _dna)) - 1;
        emit NewZombie(id, _name, _dna);

    function _generateRandomDna(string _str) private view returns (uint) {
        uint rand = uint(keccak256(abi.encodePacked(_str)));
        return rand % dnaModulus;
    //public: means any person or contract can call this function and execute it's code
    function createRandomZombie(string _name) public {
        uint randDna = _generateRandomDna(_name);
        _createZombie(_name, randDna);


Found here


Discussion (0)

Forem Open with the Forem app