How to write, compile & deploy a simple Solidity smart contract
Learn to create a simple token using Solidity.
This is lesson 2 of the Smart Contracts 101 free course by Rohas Nagpal.
This is the Solidity code of a simple token:
Let’s analyze this line by line:
pragma solidity ^0.8.0;
This is a version pragma, specifying the minimum version of the Solidity programming language that this contract was written for. In this case, it's version 0.8.0 or higher.
contract SimpleToken
This is the start of the contract definition. The name of the contract is "SimpleToken".
mapping(address => uint256) public balanceOf;
This creates a public mapping of addresses to unsigned integers, called balanceOf, which will be used to store the token balances of each address.
string public name;
This creates a public string variable called name.
string public symbol;
This creates a public string variable called symbol.
uint8 public decimals;
This creates a public unsigned integer variable called decimals.
uint256 public totalSupply;
This creates a public unsigned integer variable called totalSupply.
constructor() public
This is the constructor function of the contract, which is called when the contract is first created.
name = "Rohas Nagpal";
This sets the value of the name variable to "Rohas Nagpal".
symbol = "ROHAS";
This sets the value of the symbol variable to "ROHAS".
decimals = 18;
This sets the value of the decimals variable to 18.
totalSupply = 1000000000000000000;
This sets the value of the totalSupply variable to 1,000,000,000,000,000,000.
balanceOf[msg.sender] = totalSupply;
This sets the balance of the contract creator (who is also the sender of the transaction) to the total supply of tokens.
function transfer(address payable to, uint256 value) public
This is a public function called transfer, which takes in two parameters: an address called to, and an unsigned integer called value.
require(balanceOf[msg.sender] >= value && value > 0);
This checks that the sender of the transaction has enough tokens to transfer, and that the value of tokens being transferred is greater than zero.
balanceOf[msg.sender] -= value;
This subtracts the transferred tokens from the sender's balance.
balanceOf[to] += value;
This adds the transferred tokens to the recipient's balance.
Step 1: Writing the smart contract code
The first step is to write the code defining the contract's logic. In this case, we have used Solidity.
Stage 2: Compiling the smart contract
Once the code for the smart contract has been written, it needs to be compiled into bytecode that can be executed by the Ethereum Virtual Machine (EVM).
This is typically done using an Integrated Development Environment (IDE) like Remix.
An IDE is a software application that provides tools for writing, testing, and debugging code.
Note: Make sure to use the same version of a compiler that is mentioned in your code.
Step 3: Deploying the smart contract
Once the smart contract has been written & compiled, it can be deployed to the blockchain (e.g. Ethereum). This involves sending a transaction to the blockchain network that contains the bytecode for the smart contract.
Note: You need to elect, the environment / test network, then select your account address, and then click on deploy.
What next?
In the next posts, you will learn how to use the OpenZeppelin Contracts Wizard to create ERC20, ERC721, and ERC1155 tokens.