Top 10 Python Packages for Crypto

5/5 - (2 votes)
Packages go from most downloaded to least downloaded. The number of downloads is up to the time of writing this article.

As you know, Web3 is a new online era that differs from Web2 in its foundational technology, the blockchain. 

  • Blockchain is a disruptive technology characterized by being a decentralized solution for communication and transactions.
  • Decentralization means that there are no controlled databases by third-party service providers. These providers can own and hold your accounts or other information data concerning you. 

🗒️ A blockchain consists of a series of blocks that form a database, rather than a database of tables and rows as we are more familiar with. Each block has a reference to the block that came before it, resulting in an immutable data structure. The blocks also have an exact timestamp of when each block was added to the chain. Currently, there are at least one thousand blockchains—not all decentralized, though. Examples include Bitcoin or Ethereum.

The Python programming language is one of the most popular languages for developing blockchain applications. The fact that Python allows writing effective code in fewer lines of code compared to other programming languages makes it a great tool for blockchain coding. This is because Python provides numerous toolkits in its official repository https://pypi.org/ that are designed to make it easier to interact with the blockchain and create decentralized applications and smart contracts.

Figuring out which ones are best for developers required a bit of research.

In addition to searching the repository for projects and searching online, it was necessary to consult the Python community of developers dedicated to Python Ethereum blockchain development, as well as the crypto developer community. One of the things I like about Web3 is the community where people help each other because they need each other.

In this article, you will find a list of the most popular Python packages for Python blockchain development in 2022, so you can start developing right away!  🐍

After compiling a list of the most commonly used packages, they were ranked from 1 to 10 based on the total number of downloads on the https://pepy.tech/ website.

Note that there are many packages in this list and beyond that work with different blockchains and have different features, such as performing transactions, signing transactions, deploying smart contracts, and even the ability to create your own blockchain, etc. I hope this is a start so you can dive deep into the World Wide Web3. 

Let’s begin!

Package #1 – IPython

Although IPython is not a package used only for Web3, it is a user-friendly, powerful, interactive Python interpreter that blockchain developers like to use to see what happens when they explore interactions with the blockchain.

When you read code in tutorials that start with a >>>, it’s meant to be run in a Python interpreter like IPython. It also has features like tab completion of keywords, variables, and function names, testing, debugging, and pretty prints by default, to name a few. Ultimately, it helps you get the most out of Python. It is similar to what you get when you type python or python3 on the command line, but even more helpful.

To get the default Python interpreter, type python and you’ll get the command prompt >>>, which you can work with. To get the IPython interpreter, you must first install it with pip install Ipython. You type IPython and you get In [1]: as a command prompt and In [2]: for the next command.

The following screenshot shows a setup for interacting with the Ethereum blockchain:

.

Package #2 – eth_utils

+

The second package with more downloads includes some popular functions and classes that blockchain developers use to interact with Ethereum. According to this site, the most popular of these are:

  • encode_hex() – returns a value encoded in a hexadecimal representation with a 0x prefix.
>>> from eth_utils import encode_hex
>>> encode_hex(b'\x01\x02\x03')
'0x010203'

  • to_bytes() – converts values using standard practices in the Ethereum ecosystem. For example, strings are encoded in binary using UTF-8.
>>> to_bytes(0)
 B'\x00'

  • to_checksum_address() – for each valid representation of an address, the checksummed representation is returned.
>>> to_checksum_address('0xd3cda913deb6f67967b99d67acdfa1712c293601')
'0xd3CdA913deB6f67967B99D67aCDFa1712C293601'

  • decode_hex() – returns the value decoded into a byte string. Accepts any string with or without the 0x prefix.
>>> decode_hex('0x123456')
b'\x124V'

  • keccak() – the function generates a code hash which provides a higher level of security compared to older hash algorithms
>>> keccak(text='')
b'\x85\xe8\x07"\xeb\x93\r\xe9;\xcc\xa8{\xa5\xdf\xda\x89\n\xa12\x95\xae\xad.\xec\xc9\x0b\xb2\xd9z\x14\x93\x16'

Package #3 – web3.py

+

This is the default package for interacting with Ethereum.

You can use Solidity to develop smart contracts that run on the blockchain or to develop clients.

Web3.py allows you to develop clients that interact with the Ethereum blockchain. These are not necessarily “clients” like user-facing applications (e.g., web applications), but “clients” that interact with the blockchain by reading information from it, writing new transaction data to it, or executing business logic with smart contracts.

This is the ideal package for getting started with Ethereum, which also offers some practical features.

In an Ethereum application, you often need to convert currency units. The Web3 module provides some helper methods for this: fromWei and toWei.

Other helper methods provided by the Web3 module include data format converters (e.g., toHex), address helper methods (e.g., isAddress), and hash functions (e.g., keccak).

To view all available methods and properties, use IPython’s autocomplete by typing Web3. and pressing Tab twice after the dot.

🌍 Source: https://ethereum.org/ka/developers/tutorials/a-developers-guide-to-ethereum-part-one/

An example of a sanity check,

In [5]: w3.isConnected()
2
Out[5]: True

Package #4 – eth-tester

+

Since Ethereum is one of the most popular and widely used blockchains, there are tools for testing Ethereum-based applications, for example, testing smart contracts.

Eth-tester is one of the most downloaded packages, as it creates a development environment that bypasses the synchronization process with the blockchain, which takes hours and is unnecessary if you just want a development environment.

This tester links to a simulated Ethereum node with loose permissions and a fake currency to play with. The simulated node is called eth-tester.

>>> from eth_tester import EthereumTester
>>> t = EthereumTester()
>>> t.get_accounts()
('0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf',
 '0x2B5AD5c4795c026514f8317c7a215E218DcCD6cF',
 '0x6813Eb9362372EEF6200f3b1dbC3f819671cBA69',
 '0x1efF47bc3a10a45D4B230B5d10E37751FE6AA718',
 '0xe1AB8145F7E55DC933d51a18c793F901A3A0b276',
 '0xE57bFE9F44b819898F47BF37E5AF72a0783e1141',
 '0xd41c057fd1c78805AAC12B0A94a405c0461A6FBb',
 '0xF1F6619B38A98d6De0800F1DefC0a6399eB6d30C',
 '0xF7Edc8FA1eCc32967F827C9043FcAe6ba73afA5c',
 '0x4CCeBa2d7D2B4fdcE4304d3e09a1fea9fbEb1528')

>>> t.get_balance('0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf')
1000000000000000000000000

Package #5 – py-EVM

Py-EVM is an implementation of EVM (Ethereum Virtual Machine) in Python, originally written by Vitalik.

Its purpose is to test features and protocol changes. It includes the low-level primitives for the existing Ethereum 1.0 chain, as well as emerging support for the upcoming Ethereum 2.0.

One of the main use cases of the py-EVM library is to allow developers to create applications that want to interact with the Ethereum ecosystem.

With py-EVM, you can create a simple script that uses the py-EVM library to create a new blockchain with a pre-funded address, read the balance of that address using the regular py-EVM APIs, or develop other exciting use cases.

Example 

>>> from eth import constants
>>> from eth.chains.mainnet import MainnetChain
>>> from eth.db.atomic import AtomicDB

>>> from eth_utils import to_wei, encode_hex


>>> MOCK_ADDRESS = constants.ZERO_ADDRESS
>>> DEFAULT_INITIAL_BALANCE = to_wei(10000, 'ether')

>>> GENESIS_PARAMS = {
...    'difficulty': constants.GENESIS_DIFFICULTY,
... }

>>> GENESIS_STATE = {
...    MOCK_ADDRESS: {
...        "balance": DEFAULT_INITIAL_BALANCE,
...        "nonce": 0,
...        "code": b'',
...        "storage": {}
...    }
... }

>>> chain = MainnetChain.from_genesis(AtomicDB(), GENESIS_PARAMS, GENESIS_STATE)

>>> mock_address_balance = chain.get_vm().state.get_balance(MOCK_ADDRESS)

>>> print("The balance of address {} is {} wei".format(
...    encode_hex(MOCK_ADDRESS),
...    mock_address_balance)
... )
The balance of address 0x0000000000000000000000000000000000000000 is 10000000000000000000000 wei

Package #6 – indy-node

Indy node, which is part of Hyperledger Indy, is a technology for private, secure, and powerful identity. The code base is open source and contributed as the initial code to Hyperledger Indy, a blockchain project that is now under the Linux Foundation.

Indy Node contains everything needed to run the node of a distributed ledger for Self-Sovereign Identity (SSI).

In all models of identity management, a digital identity requires identifiers to ensure that the user is who they say they are.

However, in self-sovereign identity, the identifiers do not require an intermediary. This means that a user’s self-sovereign identity can be registered in a claim, such as a block in a blockchain. The person can then share the identifying data when, for example, making a transaction with a bank.

Indy-node is interoperable with other blockchains or can be used standalone to drive identity decentralization of identity. If you want to run your own Indy distributed ledger (DLT) network, use indy-node. There is also the SKD repository in case you want to build SSI applications.

Hyperledger Indy comes with a good example that simulates a real case. Please watch the video below. 

IndyWorld Demo by IBM

Package #7 – my-thril

Mythril is a security analysis tool designed to detect a variety of vulnerabilities in smart contracts developed for Ethereum, Hedera, Quorum, Vechain, Roostock, Tron, and other EVM-compatible blockchains.

Mythril detects a number of security issues, including integer underflows, owner-overwrite-to-Ether-withdrawal, and others. Note that Mythril aims to find general vulnerabilities and is not able to detect issues in the business logic of an application. 

The following screenshot shows a result of the analysis

$ myth analyze ether_send.sol
==== Unprotected Ether Withdrawal ====
SWC ID: 105
Severity: High
Contract: Crowdfunding
Function name: withdrawfunds()
PC address: 730
Estimated Gas Usage: 1132 - 1743
Anyone can withdraw ETH from the contract account.
Arbitrary senders other than the contract creator can withdraw ETH from the contract account without previously having sent an equivalent amount of ETH to it. This is likely to be a vulnerability.
--------------------
In file: tests/testdata/input_contracts/ether_send.sol:21
 
msg.sender.transfer(address(this).balance)

Package #8 – py-solc-x

When developing decentralized applications, and especially when writing smart contracts, there are many repetitive tasks to take care of.

These include compiling the source code. Smart contracts are written in a human-friendly language (e.g. Solidity) and to compile Solidity we need to use the solc compiler.

This package contains the solcx compiler. Note that solcx can only be run if web3.py is installed.

To deploy a contract, you need the compile_standard and install_solc modules of solcx. The first to compile and the second to call a solc version you want to use.

The example shows the compile_standard, which has a json-based layout where the developer can change the inputs and tell the compiler what to expect as output.

compiled_sol = compile_standard(
   {
       "language": "Solidity",
       "sources": {"simpleStorage.sol": {"content": simple_storage_contract}},
       "settings": {
           "outputSelection": {
               "*": {"*": ["abi", "metadata", "evm.bytecode", "evm.sourceMap"]}
           }
       },
   },
   solc_version="0.6.0",
)

Package #9 – vyper

Vyper is a smart-contract-oriented high-level programming language with Python-like syntax that targets the Ethereum Virtual Machine (EVM). It doesn’t have nearly as many uses as its EVM sibling Solidity; however, if you like Python, this is a language you might prefer over Solidity.

This is an example of a vyper contract found in the vyper documentation for a crowdfunding contract where potential participants can contribute funds to a campaign.

Package #10 – bigchaindb

BigchainDB (BDB) is an open-source queryable database with blockchain properties. It is an immutable system of record; transactions can be ordered and grouped into blocks, there are timestamps, and multiple parties can write to the database and have copies of the dataset to verify transactions and agree on the truth.

Ultimately, this is a blockchain itself. Certain useful features are needed for developers to create blockchains that provide transparency. This has tremendous value for supply chains, payments, capital markets, and trading platforms, to name a few.

This is an example of how the module connects to the database.

from bigchaindb_driver import BigchainDB

conn = BigchainDB('https://test.ipdb.io')

You can integrate blockchains, e.g. Ethereum, by using BigchainDB as a registry for smart contracts in Ethereum, integration through a parent blockchain application where BDB & Ethereum are complementary (BigchainDB as DB; Ethereum as smart contracts or currency), and other possibilities.

Example of integration:

Summary

Here was a list of the most commonly used Python packages for the blockchain.

Most of the packages are for interacting with the Ethereum blockchain, but there are also very interesting packages like indy-node and BigchainDB that can be another example of how blockchain technology can be applied and integrated with other blockchains.