The Solidity Dev

Some commented code examples to get you started on Solidity development

View the Project on GitHub

Welcome to The Solidity Dev!

Come in fellow humans to this first example of smart contract. You can view the full source in Github and follow along.

If you are completely new to smart contract development, please go to ethereumdev.io to get the basics. It should not take long to learn, it took me ~2 weeks to learn the basics and run some experiments but your mileage may vary.

The best resource for debugging is the Remix IDE

Lottery

The purpose of this contract is to run a simple lottery where the contract creator is in charge of supplying the lucky numbers. I will discuss later on why generating random numbers in a smart contract is almost never a good idea. Now, let me explain the structure of this exercise and some tools involved. The contract written in solidity (.sol file) is loaded and deployed with Python (>3.4) using Web3.py.

Lets go step by step so you can follow along:

  1. All the heavy work is done by contract_loader.py. This file is a dummy class (to be improved) to encapsulate some of the most common work around contract loading and deployment. This file is imported and executed through a helper script that is also in charge of running our contract’s methods.
  2. We use a class for interacting with the Ethereum network called blockchain.py. This file connects to out testrpc service running in our local machine and performs some low level operations like deploying and getting accounts.
  3. When the contract is deployed, we receive a tx_hash from the network acknowledging the transaction. This particular ‘electronic receipt’ can be used as many times as you want to recover the contract from the network using blockchain.py class.
  4. Method get_contract_instance() is then used to access the contract instance. All the Solidity methods are then accessible now at python level using the instance returned by this method.

Lottery general discussion

There is nothing really fancy in this contract. Like in most Solidity contracts, most of the coding is spent dealing with array logic. This particular Lottery runs for as long as the owner wants and is closed by supplying the winning numbers manually. The winners are then copied to a separate array to split the prize evenly. For a complete discussion on how to securely generate a random number in a Solidity contract, please refer to this thread.

Final words

Please, feel free to send me any questions you might have regarding smart contracts or this one in particular. I will be more than happy to help => me @ jesusfloressanjose.com