solidity Logo

Solidity Online Compiler

Practice Solidity using this online Solidity interpreter. Run Solidity code within your browser without downloading any software.

Start Coding
A BRIEF INTRODUCTION

Solidity & Smart Contracts

Solidity is an object-oriented, high-level language for implementing smart contracts. Smart contracts are programs that govern the behavior of accounts within the Ethereum state. Solidity is a curly-bracket language designed to target the Ethereum Virtual Machine (EVM). It is influenced by C++, Python, and JavaScript. It is also statically typed and supports inheritance, libraries, and complex user-defined types among other features.

With Solidity, you can create contracts for uses such as voting, crowdfunding, blind auctions, and multi-signature wallets. When deploying contracts, you should use the latest released version of Solidity. Apart from exceptional cases, only the latest version receives most security fixes.

Smart Contract

A smart contract is a program written/stored on the Blockchain. And solidity is an object-oriented programming language for implementing smart contracts on various blockchain platforms, most notably, Ethereum.

Use the solidity online compiler to create your own smart contract from scratch. If you are new to solidity and don't know where to start then this can be the first step for you in your solidity smart contract journey!

Code A Lottery Smart Contract

Algorithm

In the lottery smart contract, there will be two entities -

  • Manager
  • Players

Manager's Task

The manager will deploy the smart contract. Fetch the balance of the smart contract. Pick the winner of the lottery. At least three people are required to pick the winner of the lottery.

Player's Task

Participate in the lottery by paying 0.1 ether.

Building the Smart Contract

//SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.5.0 <0.9.0;

// Pragmas are instructions to the compiler on how to treat the code
// The above code states that it is compatible with compilers of versions greater than and equal to 0.5.0 and less than version 0.9.0.

contract Lottery {

	// State variables are the variables that are declared at the contract level
    address payable[] public players; // Dynamic array of the players who will participate in the lottery
    address public manager; // Publicly visible address of the manager

	// Constructor is a special function which runs automatically on deployment.
    constructor(){
		// Contains the address of the user who is calling the constructor()
        manager = msg.sender;
    }

	// Called automatically when someone pays ether to the contract
    receive () payable external{
        require(msg.value == 0.1 ether);
        players.push(payable(msg.sender));
    }

	// Fetch the balance of the smart contract
    function getBalance() public view returns(uint){
        require(msg.sender == manage,”You are not the manager”r);
        return address(this).balance;
    }

	// Generate a random number for the winner of the lottery
    function random() internal view returns(uint){
       return uint(keccak256(abi.encodePacked(block.difficulty, block.timestamp, players.length)));
    }

	// Pick the winner of the lottery.
    function pickWinner() public{
        require(msg.sender == manager);
        require(players.length >= 3);
        uint r = random();
        address payable winner;
        uint index = r % players.length;
        winner = players[index];
        winner.transfer(getBalance());
        players = new address payable[](0);
    }
}

So, at first, the contract is checking whether the function has been called by the manager and also if the number of players is greater than 3 or not. The variable r will contain the random number generated by the random() function. Then the modulo (%) is used to get the index of the array element who is the winner. Then the winner's address is fetched and the contract will transfer the balance eth to the winners account. In the end, the player's array is made empty by new address payable so that the lottery can re-start.

Codedamn Solidity Compiler

Compiling the Smart Contracts which you are writing on your own desktop environment can be a hassle at times as it involves installing multiple libraries and packages for it to compile and deploy. The online compiler removes this hassle by compiling your code on the browser itself where it already has the required packages running in the background. The online IDE helps you write, debug and run smart contracts within the browser itself.

The online solidity compiler compiles all your smart contracts which are written in Solidity. It outputs all the code into bytecode and various other artifacts which are needed for deploying your Smart Contract to the Ethereum Blockchain. Earlier Solidity was part of the Geth installation, now it has been removed from Geth and has been given its own package.

Frequently asked questions

Upgrade to codedamn Pro Plan and unlock more courses for accelerated learning. Unlimited courses, interactive learning and more.

Free

  • HD video content
  • Certificate of completion
  • Mentorship from codedamn staff
  • Full learning path unlocked
  • Unlimited playgrounds/compiler usage

Pro

  • HD video content
  • Certificate of completion
  • Mentorship from codedamn staff
  • All exclusive courses unlocked
  • Unlimited playground/compiler usage
Try Pro (7-day risk-free moneyback guarantee)