Solidity

Solidity lets you program on Ethereum, a blockchain-based virtual machine that allows the creation and execution of smart contracts, without requiring centralized or trusted parties.

Solidity is a statically typed, contract programming language that has similarities to Javascript and C. Like objects in OOP, each contract contains state variables, functions, and common data types. Contract-specific features include modifier (guard) clauses, event notifiers for listeners, and custom global variables.

Some Ethereum contract examples include crowdfunding, voting, decentralized finance, and blind auctions.

There is a high risk and high cost of errors in Solidity code, so you must be very careful to test and slowly rollout. WITH THE RAPID CHANGES IN ETHEREUM, THIS DOCUMENT IS UNLIKELY TO STAY UP TO DATE, SO YOU SHOULD FOLLOW THE SOLIDITY CHAT ROOM AND ETHEREUM BLOG FOR THE LATEST. ALL CODE HERE IS PROVIDED AS IS, WITH SUBSTANTIAL RISK OF ERRORS OR DEPRECATED CODE PATTERNS.

Unlike other code, you may also need to add in design patterns like pausing, deprecation, and throttling usage to reduce risk. This document primarily discusses syntax, and so excludes many popular design patterns.

As Solidity and Ethereum are under active development, experimental or beta features are typically marked, and subject to change. Pull requests welcome.

Working with Remix and Metamask

One of the easiest ways to build, deploy, and test solidity code is by using the:

To get started, download the Metamask Browser Extension.

Once installed, we will be working with Remix. The below code will be pre-loaded, but before we head over there, let’s look at a few tips to get started with remix. Load it all by hitting this link.

  1. Choose the Solidity compiler

Solidity-in-remix
  1. Open the file loaded by that link

Solidity-choose-file
  1. Compile the file

Solidity-compile
  1. Deploy

Solidity-deploy
  1. Play with contracts

Solidity-deploy

You’ve deployed your first contract! Congrats!

You can test out and play with the functions defined. Check out the comments to learn about what each does.

Working on a testnet

Deploying and testing on a testnet is the most accurate way to test your smart contracts in solidity. To do this let’s first get some testnet ETH from the Kovan testnet.

Pop into this Gitter Channel and drop your metamask address in.

In your metamask, you’ll want to change to the Kovan testnet.

Solidity-in-remix

You’ll be given some free test Ethereum. Ethereum is needed to deploy smart contracts when working with a testnet.

In the previous example, we didn’t use a testnet, we deployed to a fake virtual environment. When working with a testnet, we can actually see and interact with our contracts in a persistent manner.

To deploy to a testnet, on the #4 Deploy step, change your environment to injected web3. This will use whatever network is currently selected in your metamask as the network to deploy to.

Solidity-in-remix

For now, please continue to use the Javascript VM unless instructed otherwise. When you deploy to a testnet, metamask will pop up to ask you to “confirm” the transaction. Hit yes, and after a delay, you’ll get the same contract interface at the bottom of your screen.

Work with the full example below using the Javascript VM in remix here.

Some more functions.

Additional resources

Smart Contract Development Frameworks

Important libraries

  • Zeppelin: Libraries that provide common contract patterns (crowdfuding, safemath, etc)

  • Chainlink: Code that allows you to interact with external data

Sample contracts

Security

Style

Editors

Last updated