avatarAlex Roan

Summary

The provided web content is a comprehensive guide on how to create a basic Ethereum smart contract using the Solidity programming language, aimed at beginners with some programming background.

Abstract

The article titled "Get Started With Your First Smart Contract" serves as an introductory tutorial for individuals with beginner-intermediate programming skills who are interested in Ethereum smart contracts. It explains the concept of smart contracts as self-executing contracts with the terms of the agreement directly written into code, which are stored on the Ethereum blockchain and automatically enforced. The tutorial covers the basics of Solidity, the programming language used for writing Ethereum smart contracts, including data types, access modifiers, and the structure of a smart contract. It also provides a practical example of a simple wallet contract that can hold, send, and receive Ether, demonstrating key Solidity concepts such as state variables, constructors, and functions, including the use of globally available variables to interact with the blockchain. The article emphasizes the importance of smart contracts having an address and being able to hold Ether like a wallet, but with programmable rules that are immutable once deployed. The tutorial concludes by encouraging readers to expand upon the provided code and explore further blockchain development resources.

Opinions

  • The author suggests that understanding smart contracts is essential for anyone interested in blockchain development, particularly within the Ethereum ecosystem.
  • Solidity is presented as an accessible language for those with object-oriented programming experience, with familiar concepts such as classes, data types, and access modifiers.
  • The immutability of smart contracts is highlighted as a key feature, ensuring that once deployed, the rules encoded within them cannot be altered.
  • The article promotes the idea that anyone can create and deploy smart contracts on the Ethereum blockchain, democratizing the ability to define rule-based operations without the need for traditional intermediaries.
  • By providing a simple wallet contract example, the author implies that creating functional smart contracts is within reach for readers who follow the tutorial, fostering a hands-on approach to learning.
  • The encouragement to add new features to the provided code and share them in the comments section reflects the author's view on the importance of community engagement and collaborative learning in the blockchain development space.
  • The recommendation of further reading materials and resources indicates the author's belief in continuous learning and the value of staying updated with the latest developments in blockchain technology.

Get Started With Your First Smart Contract

What are smart contracts and how can we write them using Ethereum’s Solidity?

Photo by David McBee from Pexels.

Prerequisite: This article is aimed at those with beginner-intermediate programming experience who are aware of Ethereum and Smart Contracts but don’t quite know where to start. By the end, you should be able to understand and write simple smart contracts with Solidity.

Think of a Bitcoin wallet as a bank account that you have complete control over. Every wallet has an address on the Blockchain. If you own the private key (which is like an unbreakable password), you can send Bitcoin from that wallet to any other address.

Ethereum is no different. If you have an Ether wallet, it has an address, and provided you have the private key, you can send ETH to another address.

What About Smart Contracts?

Smart contracts have an address just like any other wallet. They can also hold ETH just like any other wallet. As far as the underlying ledger is concerned, it’s a wallet.

It’s a wallet with rules.

Instead of a person being in control of the wallet through an interface like Metamask or Exodus, code is in control. You make decisions on where to send ETH from your wallet, and smart contracts automate those decisions based on their rules without human interaction. Once deployed, those rules are immutable to that address.

Being immutable means that once those rules are deployed, they can never be changed.

These rules are defined in code and written in a language called Solidity, which was created by Vitalik Buterin and the Ethereum Foundation. Anyone can make up some rules, write the code, and deploy them to the Ethereum blockchain in the form of a smart contract.

Making Up the Rules

If you’re familiar with object-oriented programming languages, think of a Solidity smart contract as a class.

Like any other language, Solidity has a range of data types including:

  • Booleans
  • Integers
  • Strings
  • Addresses
  • Fixed-point numbers
  • Arrays

There are access modifiers that define who and what can access variables and functions:

  • Public
  • Private
  • Internal
  • External

A smart contract can have state and local variables, a constructor, and functions.

Crucially, Solidity allows any contract to access Blockchain information (like how much ETH a particular address has) using globally available variables. These are vital to performing safe rule-based operations like sending ETH to other addresses.

So, if a smart contract is a wallet with rules, let's use that as an example. Let’s make a simple wallet contract that can hold, send, and receive ETH.

Let’s go from top to bottom, then check that we meet all the criteria that we set out for our wallet.

Define Solidity version

Define the solidity compiler version with pragma solidity …. We’re using 0.6.0 here.

Define contract name

contract MyWallet {

Owner state variable

We need to keep track of who owns this wallet. This is stored in a state variable called owner, which we define inside the contract but outside of any function. This means all functions have access to the value stored in this variable.

It has private visibility, meaning only functions in this contract can access it. And it stores address payable type data. payable means that this contract can send Ether to it.

The constructor

constructor() is executed when the contract is created on the blockchain. The only line inside this function is to store the address of the deployer. We already know that every smart contract has an address, but they must be deployed using an existing address.

This is where we see the first usage of a globally available variable. msg.sender represents the address of the deployer (the person/wallet/smart contract that executed this function).

Any address can deploy a smart contract or call any function in a smart contract, whether it’s a human-controlled wallet or another smart contract.

At this point, there are two addresses in play: the deployer address (msg.sender) and the address to which this contract has been deployed (more on this as we work our way through the code).

Receive Ether

For this contract to be able to receive ETH from addresses sending to it, it needs a receive() function. This is a special function defined in Solidity that any contract wishing to receive ETH must implement. It must have the access modifier external so outside addresses can reach it, and it must be payable, meaning it can receive ETH (much like our owner can receive ETH and is therefore payable also).

Send Ether

The last function is called sendEther(). This is what facilitates the owner sending ETH from this wallet contract to another address. It takes two parameters: the address to send the ETH to and the amount to send.

Notice there are two require statements at the beginning of this function. These ensure that certain criteria are true before the function can proceed. If any requirements fail, the transaction is completely reverted.

The first requirement:

require(msg.sender == owner, "...");

This ensures that the address that called this function is the owner of the wallet. We don’t want anyone who isn’t the owner being able to send ETH to someone. Only the owner should be able to do that.

The second requirement:

require(address(this).balance >= _amount, "...");

This ensures that the wallet contract has enough ETH to send. It can’t transfer more than it owns.

This is another example of using globally accessible variables to query the amount of ETH this contract has. address(this) gets the address that this smart contract is deployed to, then .balance returns the amount of ETH the address has. As long as it’s greater than or equal to the amount the function wants to send (_amount), then we can continue!

Finally, the function sends the ETH to the target address using _to.transfer(_amount);.

Does It Do What We Want?

We wanted a smart contract that acted as a wallet and could hold, send, and receive ETH. We know we can receive and hold it using the receive() function, and we can also send ETH to other addresses using the sendEther() function. Also, only the owner of this contract can call sendEther(), making it secure.

It does do what we want!

As far as wallets go, this is extremely primitive and does not fulfil much of the functionality we would expect from a wallet.

Now It’s Your Turn

Take this code and try adding some functionality to it. First, add a function that returns the balance of the wallet.

If you think you’ve implemented some cool new features, add your code in the comments.

To find out what Smart Contracts are used for, read the next instalment explaining how to create ERC20 tokens:

Further Reading

If you’re interested in blockchain development, I write tutorials, walkthroughs, hints, and tips on how to get started and build a portfolio.

Check out some of these resources:

Bitcoin
Blockchain
Programming
Ethereum
Smart Contracts
Recommended from ReadMedium