Top Related Projects
Solidity, the Smart Contract Programming Language
OpenZeppelin Contracts is a library for secure smart contract development.
A guide to smart contract security best practices
: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.
A python interface for interacting with the Ethereum blockchain and ecosystem.
Static Analyzer for Solidity and Vyper
Quick Overview
Vyper is a contract-oriented, pythonic programming language that targets the Ethereum Virtual Machine (EVM). It is designed to be more secure and easier to audit than Solidity, with a focus on simplicity and readability. Vyper aims to provide developers with a safer and more straightforward way to write smart contracts for the Ethereum blockchain.
Pros
- Improved security features and design choices that help prevent common vulnerabilities
- Pythonic syntax that is easy to read and understand for Python developers
- Stricter language with fewer features, making it easier to write secure code
- Built-in overflow checking and strong typing to prevent unexpected behavior
Cons
- Limited features compared to Solidity, which may restrict some complex contract designs
- Smaller ecosystem and community compared to Solidity
- Fewer learning resources and tools available
- Not as widely adopted in the industry, potentially limiting job opportunities
Code Examples
- Simple Token Contract:
# SPDX-License-Identifier: MIT
# @version ^0.3.7
totalSupply: public(uint256)
balanceOf: public(HashMap[address, uint256])
@external
def __init__(_initialSupply: uint256):
self.totalSupply = _initialSupply
self.balanceOf[msg.sender] = _initialSupply
@external
def transfer(_to: address, _value: uint256) -> bool:
assert self.balanceOf[msg.sender] >= _value, "Insufficient balance"
self.balanceOf[msg.sender] -= _value
self.balanceOf[_to] += _value
return True
This example shows a basic token contract with a total supply and transfer functionality.
- Simple Auction Contract:
# SPDX-License-Identifier: MIT
# @version ^0.3.7
struct Bid:
bidder: address
amount: uint256
highestBid: public(Bid)
ended: public(bool)
owner: public(address)
@external
def __init__():
self.owner = msg.sender
self.ended = False
@external
@payable
def bid():
assert not self.ended, "Auction has ended"
assert msg.value > self.highestBid.amount, "Bid not high enough"
if self.highestBid.bidder != empty(address):
send(self.highestBid.bidder, self.highestBid.amount)
self.highestBid = Bid({bidder: msg.sender, amount: msg.value})
@external
def endAuction():
assert msg.sender == self.owner, "Only owner can end auction"
assert not self.ended, "Auction already ended"
self.ended = True
send(self.owner, self.highestBid.amount)
This example demonstrates a simple auction contract where users can place bids, and the owner can end the auction.
Getting Started
To get started with Vyper:
- Install Vyper:
pip install vyper
-
Create a new Vyper file (e.g.,
example.vy
) and write your contract code. -
Compile the contract:
vyper example.vy
- Use the compiled bytecode and ABI with your preferred Ethereum development framework (e.g., web3.py, Brownie, or Truffle) to deploy and interact with the contract on the Ethereum network or a local testnet.
Competitor Comparisons
Solidity, the Smart Contract Programming Language
Pros of Solidity
- More mature and widely adopted ecosystem
- Extensive documentation and community support
- Broader range of features and flexibility
Cons of Solidity
- More complex syntax and learning curve
- Higher potential for security vulnerabilities
- Less emphasis on readability and simplicity
Code Comparison
Solidity:
pragma solidity ^0.8.0;
contract SimpleStorage {
uint256 private storedData;
function set(uint256 x) public {
storedData = x;
}
}
Vyper:
# @version ^0.3.0
storedData: uint256
@external
def set(x: uint256):
self.storedData = x
The Vyper code is more concise and Python-like, emphasizing readability. Solidity's syntax is more verbose and C-like. Vyper enforces stricter safety measures by default, while Solidity offers more flexibility but requires careful handling to avoid potential vulnerabilities.
Solidity remains the dominant smart contract language due to its maturity and extensive ecosystem. However, Vyper is gaining traction for its simplicity and focus on security, making it an attractive alternative for developers prioritizing these aspects in their smart contract development.
OpenZeppelin Contracts is a library for secure smart contract development.
Pros of openzeppelin-contracts
- Extensive library of secure, audited smart contract implementations
- Well-documented and widely adopted in the Ethereum ecosystem
- Regular updates and maintenance by a dedicated team
Cons of openzeppelin-contracts
- Limited to Solidity language, which may have a steeper learning curve
- Larger codebase and potential for increased gas costs due to comprehensive features
Code Comparison
Vyper (simple token contract):
token_name: public(string[64])
token_symbol: public(string[32])
total_supply: public(uint256)
@external
def __init__(_name: string[64], _symbol: string[32], _supply: uint256):
self.token_name = _name
self.token_symbol = _symbol
self.total_supply = _supply
OpenZeppelin (ERC20 token contract):
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20 {
constructor(uint256 initialSupply) ERC20("MyToken", "MTK") {
_mint(msg.sender, initialSupply);
}
}
The Vyper code is more concise and readable, while the OpenZeppelin implementation provides a full ERC20-compliant token with additional features and security measures out of the box.
A guide to smart contract security best practices
Pros of smart-contract-best-practices
- Comprehensive guide for secure smart contract development
- Language-agnostic, applicable to various blockchain platforms
- Regularly updated with community contributions
Cons of smart-contract-best-practices
- Not a programming language or development tool
- Requires developers to implement best practices manually
- May not cover all edge cases or specific language quirks
Code comparison
smart-contract-best-practices (Solidity example):
function transfer(address recipient, uint256 amount) public returns (bool) {
require(balanceOf[msg.sender] >= amount, "Insufficient balance");
balanceOf[msg.sender] -= amount;
balanceOf[recipient] += amount;
emit Transfer(msg.sender, recipient, amount);
return true;
}
Vyper:
@external
def transfer(_to: address, _value: uint256) -> bool:
assert self.balanceOf[msg.sender] >= _value, "Insufficient balance"
self.balanceOf[msg.sender] -= _value
self.balanceOf[_to] += _value
log Transfer(msg.sender, _to, _value)
return True
Summary
smart-contract-best-practices offers valuable guidelines for secure smart contract development across platforms, while Vyper is a specific programming language designed for Ethereum smart contracts. The former provides best practices, while the latter implements some of these practices by design, offering a more opinionated and potentially safer development experience.
: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
- Comprehensive development environment with built-in testing framework
- Extensive documentation and large community support
- Supports multiple smart contract languages (Solidity, Vyper)
Cons of Truffle
- Steeper learning curve for beginners
- Can be overkill for simple projects
- Slower compilation and deployment times for large projects
Code Comparison
Vyper (contract example):
@external
def greet(name: String[100]) -> String[200]:
return concat("Hello, ", name)
Truffle (migration script example):
const MyContract = artifacts.require("MyContract");
module.exports = function(deployer) {
deployer.deploy(MyContract);
};
Key Differences
- Vyper focuses on contract language, while Truffle is a full development suite
- Vyper aims for simplicity and security, Truffle for comprehensive tooling
- Vyper is Python-like, Truffle uses JavaScript for configuration and testing
Use Cases
- Vyper: Security-critical contracts, auditable code
- Truffle: Full-stack dApp development, complex project management
A python interface for interacting with the Ethereum blockchain and ecosystem.
Pros of web3.py
- Broader functionality for interacting with Ethereum, including account management, transaction handling, and smart contract interaction
- More mature and widely adopted library with extensive documentation and community support
- Supports multiple Ethereum networks and providers
Cons of web3.py
- Steeper learning curve due to its comprehensive feature set
- Larger codebase and dependencies, potentially leading to longer build times and larger project sizes
- Not specifically designed for smart contract development, unlike Vyper
Code Comparison
web3.py
from web3 import Web3
w3 = Web3(Web3.HTTPProvider('https://mainnet.infura.io/v3/YOUR-PROJECT-ID'))
balance = w3.eth.get_balance('0x742d35Cc6634C0532925a3b844Bc454e4438f44e')
print(w3.from_wei(balance, 'ether'))
Vyper
@external
def get_balance(address: address) -> uint256:
return self.balance[address]
@external
def transfer(to: address, amount: uint256):
self.balance[msg.sender] -= amount
self.balance[to] += amount
Static Analyzer for Solidity and Vyper
Pros of Slither
- Comprehensive static analysis tool for Solidity smart contracts
- Detects a wide range of security vulnerabilities and code smells
- Integrates well with CI/CD pipelines for automated security checks
Cons of Slither
- Limited to analyzing Solidity contracts, not applicable to Vyper
- Requires additional setup and configuration compared to using Vyper directly
- May produce false positives that need manual verification
Code Comparison
Vyper (contract definition):
contract Token:
balanceOf: public(map(address, uint256))
totalSupply: public(uint256)
@external
def transfer(to: address, amount: uint256) -> bool:
self.balanceOf[msg.sender] -= amount
self.balanceOf[to] += amount
return True
Slither (example output for a Solidity contract):
INFO:Detectors:
Reentrancy in Token.transfer(address,uint256) (contracts/Token.sol#15-20):
External calls:
- (success) = to.call.value(amount)() (contracts/Token.sol#18)
State variables written after the call(s):
- balances[msg.sender] -= amount (contracts/Token.sol#19)
Reference: https://github.com/crytic/slither/wiki/Detector-Documentation#reentrancy-vulnerabilities
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Getting Started
See Installing Vyper to install vyper. See Tools and Resources for an additional list of framework and tools with vyper support. See Documentation for the documentation and overall design goals of the Vyper language.
See learn.vyperlang.org for learning Vyper by building a Pokémon game. See try.vyperlang.org to use Vyper in a hosted jupyter environment!
Note: Vyper is beta software, use with care
Installation
See the Vyper documentation for build instructions.
Compiling a contract
To compile a contract, use:
vyper your_file_name.vy
generate bytecode
vyper -f bytecode file-name.vy > file-name.bin
generate abi
vyper -f abi file-name.vy > file-name.abi
There is also an online compiler available you can use to experiment with
the language and compile to bytecode
and/or IR
.
Note: While the vyper version of the online compiler is updated on a regular basis it might be a bit behind the latest version found in the master branch of this repository.
Testing (using pytest)
(Complete installation steps first.)
make dev-init
python setup.py test
Developing (working on the compiler)
A useful script to have in your PATH is something like the following:
$ cat ~/.local/bin/vyc
#!/usr/bin/env bash
PYTHONPATH=. python vyper/cli/vyper_compile.py "$@"
To run a python performance profile (to find compiler perf hotspots):
PYTHONPATH=. python -m cProfile -s tottime vyper/cli/vyper_compile.py "$@"
To get a call graph from a python profile, https://stackoverflow.com/a/23164271/ is helpful.
Contributing
- See Issues tab, and feel free to submit your own issues
- Add PRs if you discover a solution to an existing issue
- For further discussions and questions, post in Discussions or talk to us on Discord
- For more information, see Contributing
Top Related Projects
Solidity, the Smart Contract Programming Language
OpenZeppelin Contracts is a library for secure smart contract development.
A guide to smart contract security best practices
: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.
A python interface for interacting with the Ethereum blockchain and ecosystem.
Static Analyzer for Solidity and Vyper
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot