Ethereum and Smart Contracts (Part 1)

Introduction

In this article we’ll be discussion the Ethereum blockchain and the technology of “Smart Contracts” that exist within that blockchain ecosystem. If you’re brand new to Ethereum, the “Ether” cryptoasset and Smart Contracts, I recommend reading the Ethereum Foundation’s FAQ.

For further reading:

What is Ethereum?

Ethereum is a “turing-complete” open-source, distributed, p2p blockchain technology using modified Nakamoto consensus. It’s essentially a simplified virtual computer, running across thousands of individual nodes who all agree on what the output should be. Anyone running an Ethereum node (a piece of software that communicates with the rest of the Ethereum network) is part of this global virtual computer. Ethereum mining in concept is much like Bitcoin, securing the network and confirming blocks. There is a cryptoasset integrated directly into Ethereum called Ether. Miniscule amounts of Ether (referred to as Gas) are used to pay for transactions on the blockchain. With a maximum Gas limit per transaction (and the potential for you to run out of Gas, literally), this ensures that only the “necessary” transactions will continue to be computed on the chain. I use the term “necessary” here in the sense that someone or something is still paying for these transactions to be executed.

What does it look like to me?

An Ethereum wallet address is of the format 0x999999cf1046e68e36E1aA2E0E07105eDDD1f08E

As an end user you’d probably use a wallet application to keep track of your Ether as well as any other ERC20 assets you’ve collected.

  • MetaMask, the best Ethereum browser plugin (in my opinion)
  • MyEtherWallet is an online wallet for the Ethereum blockchain
  • Exodus is a multi-functional wallet that supports lots of cryptoassets, including Ether

You may also run a full node which manages its own copy of the Ethereum blockchain (80Gb+ with full sync). Often it’s a GUI-less daemon that you communicate with via RPC, much like the bitcoin daemon.

You can browse the Ethereum blockchain via EtherScan or other blockchain explorers.

What is Ether?

Ether is the name of the cryptoasset integrated directly into the Ethereum blockchain. It is both a cryptocurrency and – since it’s also used in tiny amounts as fuel for transactions on the chain – a “cryptoresource”. Ether is traded on pretty much every big cryptoexchange market and several startups have their entire bankrolls invested in this stuff. As of the time of writing Ether is trading at around $116 USD/1 ETH.

What does it look like to me?

image
Some Ether. Also yes, those are CryptoKitties.

Or from a code perspective, it might look like this (simplified):

contract FakeEther {
    address minter;
    mapping (address => uint) balances;
    function FakeEther() {
        minter = msg.sender;
    }
    function mint(address owner, uint amount) {
        if (msg.sender != minter) return;
        balances[owner] += amount;
    }
    function send(address receiver, uint amount) {
        if (balances[msg.sender] < amount) return;
        balances[msg.sender] -= amount;
        balances[receiver] += amount;
    }
    function queryBalance(address addr) constant returns (uint balance) {
        return balances[addr];
    }
}

The above Solidity code is a very simplified smart contract that allows one to mint a currency conveniently called FakeEther. You can mint some FakeEther, send it to other valid addresses, and check your FakeEther balance! Look at us, we just established a decentralized currency that can withstand even the most oppressive regime as long as they don’t cut off electricity or internet access!

What is Gas?

Gas is the tiny amount of Ether used to pay for transactions computed on the Ethereum blockchain. Gas is technically not another currency but simply a way to refer to the miniscule amounts of Ether used in these processes. Gas is measured in Wei, the smallest unit of Ether. When sending a transaction on the Ethereum blockchain, you can set the maximum amount of Gas you’re willing to spend on getting that transaction processed. Most clients will calculate a rough estimate for you, and the unused amount gets returned to your wallet. The reliance on Gas to send transactions means that smart contracts need a small supply of Ether in order to “live” on the blockchain, or their transactions will automatically fail.

What does it look like to me?

Gas is a mostly behind-the-scenes thing but can rear its ugly head when you’re trying to play around with smart contracts. Specifying Gas Limits, knowing Gas Prices, these are all things you’ll become intimately familiar with as we venture further into the world of Smart Contracts.

image
MetaMask complaining about a lack of funds, Gas is still just Ether.

What is a Smart Contract?

A Smart Contract on the Ethereum blockchain is simply compiled bytecode that’s been inserted into the blockchain at an address. It operates as a program, with inputs and outputs, and you interact with it via Ethereum transactions.

Normally when transacting on Ethereum, you’d probably send some Ether from your wallet to Bill’s wallet. With Smart Contracts, Bill’s wallet might actually be a contract address that runs some code when it receives Ether. Maybe it pays Bill directly, maybe it sends money to his ex-wife first. By pasting the address into EtherScan and examining it further, one could see immediately that Bill’s using a contract to receive his Ether.

image

Notice that it says “Contract” right in the To: field, this is because EtherScan knows immediately that this is a contract address. Aside from this it still functions like a normal Ethereum wallet. It can hold Ether and pay Gas prices.

What does it look like to me?

Smart Contract developers today write in Solidity. It’s a statically-typed language designed for developing smart contracts on the Ethereum blockchain. As stated above, it compiles to bytecode that lives on the blockchain itself. It supports inheritance, structs, and some other pleasures of modern languages.

Example:

pragma solidity >=0.4.22 <0.6.0;

contract Mortal {
    /* Define variable owner of the type address */
    address owner;

    /* This constructor is executed at initialization and sets the owner of the contract */
    constructor() public { owner = msg.sender; }

    /* Function to recover the funds on the contract */
    function kill() public { if (msg.sender == owner) selfdestruct(msg.sender); }
}

contract Greeter is Mortal {
    /* Define variable greeting of the type string */
    string greeting;

    /* This runs when the contract is executed */
    constructor(string memory _greeting) public {
        greeting = _greeting;
    }

    /* Main function */
    function greet() public view returns (string memory) {
        return greeting;
    }
}

Thanks to ReMix, we’re able to compile our Solidity smart contracts right on the web. We can deploy them directly into the Ethereum ecosystem using a combination of ReMix and the MetaMask browser plugin.

The Ethereum Foundation has some great tutorials on writing your first smart contracts, I highly recommend checking those out before we venture into too far into the realm of smart contract security.

And these are just the beginning!

Conclusion

The light side of smart contracts is all these wonderful tools for creating decentralized communities, currencies, projects. The dark side is a lot of immutable ponzi and pyramid schemes. Guess which we’ll be looking at in part 2? Stay tuned.

This article is a work in progress. Please feel free to reply with corrections and I’ll edit as necessary. I appreciate all feedback!

13 Likes

Great to see an article about smart contracts! Can’t wait for the next part :smiley:

3 Likes

This topic was automatically closed after 30 days. New replies are no longer allowed.