Convert Figma logo to code with AI

ethereum logosolidity

Solidity, the Smart Contract Programming Language

22,999
5,694
22,999
499

Top Related Projects

OpenZeppelin Contracts is a library for secure smart contract development.

A guide to smart contract security best practices

14,020

:warning: The Truffle Suite is being sunset. For information on ongoing support, migration options and FAQs, visit the Consensys blog. Thank you for all the support over the years.

4,837

Pythonic Smart Contract Language for the EVM

1,343

Polkadot's ink! to write smart contracts.

Quick Overview

Solidity is a contract-oriented, high-level programming language used for implementing smart contracts on the Ethereum blockchain. It is designed to be readable and writable for developers and aims to be a robust and secure language for building decentralized applications (dApps) on the Ethereum network.

Pros

  • Ethereum Integration: Solidity is the primary language for developing smart contracts on the Ethereum blockchain, making it a crucial tool for building dApps on this popular platform.
  • Statically Typed: Solidity is a statically typed language, which helps catch errors at compile-time and provides better type safety compared to dynamically typed languages.
  • Robust Ecosystem: The Solidity community is active and growing, with a wealth of resources, libraries, and tools available to support developers.
  • Versatile: Solidity can be used to create a wide range of decentralized applications, from simple token contracts to complex financial instruments and games.

Cons

  • Complexity: Solidity can be challenging to learn, especially for developers new to blockchain and decentralized technologies.
  • Gas Costs: Executing Solidity code on the Ethereum network can be expensive due to the gas costs associated with each transaction.
  • Security Concerns: Smart contracts written in Solidity can be vulnerable to security issues, such as reentrancy attacks and integer overflow/underflow bugs, which can have serious consequences.
  • Evolving Language: Solidity is a relatively young language, and it is still evolving, which can lead to breaking changes and compatibility issues.

Code Examples

Here are a few examples of Solidity code:

  1. Simple Token Contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract SimpleToken {
    mapping(address => uint256) private _balances;
    uint256 private _totalSupply;

    function mint(uint256 amount) public {
        _totalSupply += amount;
        _balances[msg.sender] += amount;
    }

    function transfer(address recipient, uint256 amount) public {
        require(_balances[msg.sender] >= amount, "Insufficient balance");
        _balances[msg.sender] -= amount;
        _balances[recipient] += amount;
    }

    function balanceOf(address account) public view returns (uint256) {
        return _balances[account];
    }
}
  1. Auction Contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Auction {
    address payable public beneficiary;
    uint public auctionEndTime;
    uint public highestBid;
    address public highestBidder;
    bool ended;

    event HighestBidIncreased(address bidder, uint amount);
    event AuctionEnded(address winner, uint amount);

    constructor(uint _biddingTime, address payable _beneficiary) {
        beneficiary = _beneficiary;
        auctionEndTime = block.timestamp + _biddingTime;
    }

    function bid() public payable {
        require(block.timestamp < auctionEndTime, "Auction already ended.");
        require(msg.value > highestBid, "There already is a higher bid.");

        if (highestBid != 0) {
            beneficiary.transfer(highestBid);
        }
        highestBidder = msg.sender;
        highestBid = msg.value;
        emit HighestBidIncreased(msg.sender, msg.value);
    }

    function auctionEnd() public {
        require(block.timestamp >= auctionEndTime, "Auction not yet ended.");
        require(!ended, "auctionEnd has already been called.");

        ended = true;
        emit AuctionEnded(highestBidder, highestBid);
        beneficiary.transfer(highestBid);
    }
}
  1. Voting Contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Voting {
    mapping(

Competitor Comparisons

OpenZeppelin Contracts is a library for secure smart contract development.

Pros of openzeppelin-contracts

  • Provides a comprehensive library of reusable smart contracts
  • Regularly audited and battle-tested, enhancing security
  • Simplifies development by offering standardized implementations

Cons of openzeppelin-contracts

  • Limited to contract implementations, not a full language
  • May introduce dependencies and increase gas costs
  • Less flexibility compared to writing contracts from scratch

Code Comparison

Solidity (basic ERC20 token):

pragma solidity ^0.8.0;

contract SimpleToken {
    mapping(address => uint256) private _balances;
    uint256 private _totalSupply;

    function transfer(address to, uint256 amount) public returns (bool) {
        // Implementation details
    }
}

openzeppelin-contracts (ERC20 token):

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract MyToken is ERC20 {
    constructor() ERC20("MyToken", "MTK") {
        _mint(msg.sender, 1000000 * 10 ** decimals());
    }
}

The Solidity repository focuses on the language itself, providing the compiler and core functionality. In contrast, openzeppelin-contracts offers pre-built, secure contract implementations that developers can easily integrate into their projects, reducing development time and potential security risks.

A guide to smart contract security best practices

Pros of smart-contract-best-practices

  • Focuses on security and best practices for Solidity development
  • Provides comprehensive guidelines for writing secure smart contracts
  • Regularly updated with community contributions and latest security findings

Cons of smart-contract-best-practices

  • Not an official language specification or compiler
  • May lack detailed technical implementation details
  • Doesn't provide direct tooling or development environment

Code Comparison

smart-contract-best-practices (example of a security pattern):

function withdraw(uint256 amount) public {
    require(balances[msg.sender] >= amount, "Insufficient balance");
    balances[msg.sender] -= amount;
    (bool success, ) = msg.sender.call{value: amount}("");
    require(success, "Transfer failed");
}

solidity (example of language syntax):

pragma solidity ^0.8.0;

contract SimpleStorage {
    uint256 private storedData;

    function set(uint256 x) public {
        storedData = x;
    }
}

Summary

While solidity is the official repository for the Solidity programming language, including its compiler and documentation, smart-contract-best-practices focuses on providing guidelines and best practices for secure smart contract development. The solidity repository is essential for language development and tooling, while smart-contract-best-practices serves as a valuable resource for developers to write more secure and robust smart contracts.

14,020

:warning: The Truffle Suite is being sunset. For information on ongoing support, migration options and FAQs, visit the Consensys blog. Thank you for all the support over the years.

Pros of Truffle

  • Provides a complete development environment with built-in testing framework and asset pipeline
  • Offers automated contract deployments and migrations
  • Includes a console for direct contract interaction

Cons of Truffle

  • Steeper learning curve for beginners compared to using Solidity directly
  • May introduce additional complexity for simple projects
  • Requires additional setup and configuration

Code Comparison

Solidity (contract deployment):

pragma solidity ^0.8.0;

contract SimpleStorage {
    uint256 storedData;

    function set(uint256 x) public {
        storedData = x;
    }
}

Truffle (migration script):

const SimpleStorage = artifacts.require("SimpleStorage");

module.exports = function(deployer) {
  deployer.deploy(SimpleStorage);
};

Summary

Solidity is the core smart contract programming language for Ethereum, while Truffle is a development framework built on top of Solidity. Solidity offers direct control over contract code, while Truffle provides a more comprehensive development environment with additional tools and features. The choice between them depends on project complexity and developer preferences.

4,837

Pythonic Smart Contract Language for the EVM

Pros of Vyper

  • Simpler syntax and easier to read, reducing the likelihood of errors
  • Built-in security features, making it harder to write vulnerable code
  • Designed for auditability, enhancing contract transparency

Cons of Vyper

  • Less mature ecosystem with fewer tools and resources available
  • More limited functionality compared to Solidity's extensive features
  • Smaller developer community, potentially leading to slower issue resolution

Code Comparison

Solidity:

contract SimpleStorage {
    uint256 private storedData;
    
    function set(uint256 x) public {
        storedData = x;
    }
    
    function get() public view returns (uint256) {
        return storedData;
    }
}

Vyper:

storedData: uint256

@external
def set(x: uint256):
    self.storedData = x

@external
@view
def get() -> uint256:
    return self.storedData

The Vyper code is more concise and resembles Python syntax, making it easier to read and understand. Solidity's syntax is more verbose and C-like, which may be familiar to developers with traditional programming backgrounds but can be more complex for newcomers to blockchain development.

1,343

Polkadot's ink! to write smart contracts.

Pros of ink!

  • Designed for WebAssembly, offering better performance and cross-platform compatibility
  • Leverages Rust's safety features and powerful type system
  • Simpler syntax and easier to learn for developers familiar with Rust

Cons of ink!

  • Smaller ecosystem and community compared to Solidity
  • Limited tooling and integration with existing Ethereum infrastructure
  • Fewer learning resources and documentation available

Code Comparison

Solidity:

contract SimpleStorage {
    uint256 private storedData;
    function set(uint256 x) public {
        storedData = x;
    }
    function get() public view returns (uint256) {
        return storedData;
    }
}

ink!:

#[ink::contract]
mod simple_storage {
    #[ink(storage)]
    pub struct SimpleStorage {
        value: u32,
    }
    impl SimpleStorage {
        #[ink(constructor)]
        pub fn new(init_value: u32) -> Self {
            Self { value: init_value }
        }
        #[ink(message)]
        pub fn set(&mut self, new_value: u32) {
            self.value = new_value;
        }
        #[ink(message)]
        pub fn get(&self) -> u32 {
            self.value
        }
    }
}

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

The Solidity Contract-Oriented Programming Language

Matrix Chat Gitter Chat Solidity Forum X Follow Mastodon Follow

You can talk to us on Gitter and Matrix, tweet at us on X (previously Twitter) or create a new topic in the Solidity forum. Questions, feedback, and suggestions are welcome!

Solidity is a statically typed, contract-oriented, high-level language for implementing smart contracts on the Ethereum platform.

For a good overview and starting point, please check out the official Solidity Language Portal.

Table of Contents

Background

Solidity is a statically-typed curly-braces programming language designed for developing smart contracts that run on the Ethereum Virtual Machine. Smart contracts are programs that are executed inside a peer-to-peer network where nobody has special authority over the execution, and thus they allow anyone to implement tokens of value, ownership, voting, and other kinds of logic.

When deploying contracts, you should use the latest released version of Solidity. This is because breaking changes, as well as new features and bug fixes, are introduced regularly. We currently use a 0.x version number to indicate this fast pace of change.

Build and Install

Instructions about how to build and install the Solidity compiler can be found in the Solidity documentation.

Example

A "Hello World" program in Solidity is of even less use than in other languages, but still:

// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.9.0;

contract HelloWorld {
    function helloWorld() external pure returns (string memory) {
        return "Hello, World!";
    }
}

To get started with Solidity, you can use Remix, which is a browser-based IDE. Here are some example contracts:

  1. Voting
  2. Blind Auction
  3. Safe remote purchase
  4. Micropayment Channel

Documentation

The Solidity documentation is hosted using Read the Docs.

Development

Solidity is still under development. Contributions are always welcome! Please follow the Developers Guide if you want to help.

You can find our current feature and bug priorities for forthcoming releases in the projects section.

Maintainers

The Solidity programming language and compiler are open-source community projects governed by a core team. The core team is sponsored by the Ethereum Foundation.

License

Solidity is licensed under GNU General Public License v3.0.

Some third-party code has its own licensing terms.

Security

The security policy may be found here.

NPM DownloadsLast 30 Days