Create And Deploy Smart Contracts with Solidity

Ronald Abreu
4 min readNov 1, 2020

--

Solidity is an object oriented programming language widely used for writing and deploying smart contracts to the main Ethereum platform as well as several other private blockchains.

With Solidity we can develop applications capable of processing self enforcing business logic described in our smart contracts while leaving a permanent record of the transaction. Once a contract as been deployed it is impossible to change it.

Solidity was designed to be assimilated quickly by web developers so if you have some knowledge of JavaScript (or any other object-oriented language) you should be able to follow along with me as I create and deploy our very own smart contract to a blockchain running on our own machine!

Before we jump into it I would like to quickly cover some of the Solidity characteristics that you may find in other languages like JS, as well as areas where Solidity may be different.

Solidity has a class like structure, it supports functions, imports, and has it’s own control structures such as — For and While loops as well as If/Else statements. It also supports several common data types like

Array:

type arrayName [ arraySize ];

The arraySize must be an integer constant greater than zero and type can be any valid data type.

Mapping:

mapping(_KeyType => _ValueType)

which is a reference type, or kind of hash.

Address:

0x557af2Cab087A42e71d5F70D80Be13991cB287B2

A Solidity Address type holds a 20 byte value and serves as the base for all Solidity smart contracts.

Struct:

struct Request {
bytes data;
function(bytes memory) external callback;
}

Struct are used to represent a record. Think of it as an Instance of a Class.

Units:

In Solidity Units are literal numbers that take suffixes, There are Ether Units and Time Units:

Ether Units:

2 ether == 2000 finney

Time Units:

  • 1 == 1 seconds
  • 1 minutes == 60 seconds
  • 1 hours == 60 minutes
  • 1 days == 24 hours
  • 1 weeks == 7 days
  • 1 years == 365 days

and Boolean:

Where the possible values are constant

True/False

Where Solidity differs greatly from other object-oriented programming languages is in its lack of support for floats. There are no floating points in Solidity, however we may access them via libraries, such as ABDKMathQuad.

Now that we are a bit more familiar with the Solidity language, let’s look at how we can actually use it to write, test and deploy our smart contracts.

For now we will use a template called the ERC20 Standard token as the base to our contracts. Then, the compiling process will produce a Bytecode file and an ABI file.

We will deploy the Bytecode file to a local blockchain that we will set up using Truffle and Ganache. The ABI file is essentially a json file containing all the contract interaction data.

So buckle up, and let’s begin!

The ERC20 Standard token often includes some of the following methods:

You may download an ERC20 copy or generate one using Truffle. Once we have our Solidity contract ready will use Truffle to deploy it.

To install Truflle:

npm install -g truffle

We are also going to need a running Ethereum client which supports the standard JSON RPC API.

Since we are using Truffle we will also use Ganache which is part of the Truffle suite.

Head over to Ganache and download it.

With Truffle installed in your contract directory you may use the command:

truffle unbox metacoin

form the Truffle docs:

Note: You can use the `truffle unbox ` command to download any of the other Truffle Boxes.

Note: To create a bare Truffle project with no smart contracts included, use `truffle init`

this will generate a basic project structure and a .sol file called MetaCoin, this will be similar to our ERC20 file.

The project structure will include:

  • contracts/
  • migrations/
  • test/
  • truffle.js

While it is possible to create this blockchain and interact with it using Truffle Develop, I will be using Ganache as our Ethereum Client because I find the GUI easier to use, specially if you are just getting started with Truffle.

Ganache is a personal blockchain for rapid Ethereum and Corda distributed application development. You can use Ganache across the entire development cycle; enabling you to develop, deploy, and test your dApps in a safe and deterministic environment. — Ganache docs

However in order to use Ganache we have to edit the Truffle configuration with Ganache’s default connection parameters.

Open truffle-config.js in a text editor. Replace the content with the following:

module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 7545,
network_id: "*"
}
}
};

Now open Ganache, you will be prompted to load, create or quick-start a Workspace.

We can also choose to start an Ethereum Node or Corda Network from the drop down menu.

For this example we will use an Etherum Node.

Once our Workplace has been created you will be able to see a list of a number of accounts, each with a balance of 100.00 ETH.

Click on Contracts to add your project’s truffle.config file to your Workplace.

Now from the project console run:

truffle migrate

If our migration from truffle was successful we should be able to see our transactions and new Token listed in our WorkPlace by clicking on the transactions tab and Contracts tab and this means our local blockchain is up and running.

Congrats! you just deployed your first smart contract!

Stay tuned for my next article in which we will be learning about MetaMask, a crypto wallet & gateway to blockchain apps that we can install as a Chrome extension and use to access our local blockchain directly from our own web applications.

--

--