The Complete Guide to Using the Safe-eth-py Library for Ethereum Development

·

The safe-eth-py library provides a comprehensive set of Python tools designed to streamline interactions with the Ethereum blockchain and enhance development workflows for Ethereum-based projects. This powerful collection of utilities simplifies complex tasks, making it an essential resource for developers building decentralized applications (dApps), smart contract interfaces, and blockchain analytics tools.

What Is Safe-eth-py?

Safe-eth-py is an open-source Python library specifically developed to facilitate Ethereum blockchain interactions and support the Safe ecosystem (formerly known as Gnosis Safe). It offers a robust wrapper around Web3.py with additional functionalities that address common challenges in Ethereum development.

The library encompasses several key components:

Installation and Setup

Getting started with safe-eth-py is straightforward. You can install the package using pip:

pip install safe-eth-py

For projects utilizing Django framework components, install the Django extension:

pip install safe-eth-py[django]

Note: Some users might encounter installation issues with the coincurve dependency. If you experience build problems, ensure you have the required system libraries installed as detailed in the coincurve installation documentation.

Core Components and Functionality

Ethereum Client Utilities

The EthereumClient class serves as the foundation for Ethereum interactions, connecting to nodes and executing operations:

from safe_eth.eth import EthereumClient

# Initialize with your Ethereum node URL
ethereum_client = EthereumClient(ETHEREUM_NODE_URL)

One of the most powerful features is the batch call capability, which significantly improves performance by combining multiple read-only contract calls into a single request:

from safe_eth.eth import EthereumClient
from safe_eth.eth.contracts import get_erc721_contract

ethereum_client = EthereumClient(ETHEREUM_NODE_URL)
erc721_contract = get_erc721_contract(self.w3, token_address)

# Batch multiple calls for efficiency
name, symbol = ethereum_client.batch_call([
    erc721_contract.functions.name(),
    erc721_contract.functions.symbol(),
])

You can also directly access the underlying Web3.py instance for more granular control:

block_data = ethereum_client.w3.eth.get_block(57)

Ethereum Constants and Address Utilities

The library provides essential constants used throughout Ethereum development:

Price Oracle Integrations

Safe-eth-py includes comprehensive price oracle support for major decentralized exchanges:

from safe_eth.eth import EthereumClient
from safe_eth.eth.oracles import UniswapV2Oracle

ethereum_client = EthereumClient(ETHEREUM_NODE_URL)
uniswap_oracle = UniswapV2Oracle(ethereum_client)

# Get price between two tokens
price = uniswap_oracle.get_price(gno_token_address, weth_token_address)

Supported oracles include Uniswap (V1 and V2), Kyber, SushiSwap, Aave, Balancer, Curve, Mooniswap, and Yearn, providing extensive coverage for DeFi price feed requirements.

Ethereum Utility Functions

The library offers practical utilities for Ethereum operations, including:

Django Integration for Web Applications

For developers building web applications with Django, safe-eth-py provides extensive integration capabilities:

Django Models and Fields

Django REST Framework Components

Safe-specific Serializers

All Django components include comprehensive test suites built on Django's testing framework, ensuring reliability and consistency.

Safe Transaction Service API Integration

The library provides seamless integration with the Safe Transaction Service API, enabling developers to manage Safes, transactions, delegates, and messages programmatically.

API Authentication Setup

To use the default Transaction Service, you need an API key from the Safe Developer Portal. You can set this key as an environment variable:

export SAFE_TRANSACTION_SERVICE_API_KEY=[your-api-key]

Alternatively, you can pass the API key directly to the constructor or configure a custom service endpoint if needed.

API Usage Example

from safe_eth.eth import EthereumNetwork
from safe_eth.safe.api import TransactionServiceApi

# Initialize API for Gnosis chain
transaction_service_api = TransactionServiceApi(EthereumNetwork.GNOSIS)

# Retrieve transactions for a specific Safe address
transactions = transaction_service_api.get_transactions("0xAedF684C1c41B51CbD228116e11484425d2FACB9")

👉 Explore advanced API integration techniques

Adding Support for New Blockchain Networks

The Ethereum ecosystem continues to expand with new chains and layer-2 solutions. If you need to add Safe Smart Account support for a new blockchain network, the process is straightforward:

  1. Open a new issue on the safe-eth-py GitHub repository using the "Add Safe Address New Chain" template
  2. Provide the necessary chain information and configuration details
  3. The automated validation system will verify the information
  4. If validation passes, a pull request will be automatically generated
  5. The Safe team reviews and merges the pull request

This streamlined process ensures that new chain support can be added efficiently while maintaining quality and security standards.

Contributing to the Project

The safe-eth-py project welcomes contributions from the developer community. To set up a development environment:

# Clone the repository
git clone [repository-url]

# Create and activate virtual environment
python -m venv venv
source venv/bin/activate

# Install development dependencies
pip install -r requirements-dev.txt

# Set up pre-commit hooks
pre-commit install -f

The project maintains comprehensive contribution guidelines to ensure code quality and consistency. Before submitting changes, ensure all tests pass and that new functionality includes appropriate test coverage.

Frequently Asked Questions

What are the main advantages of using safe-eth-py over plain Web3.py?
Safe-eth-py provides enhanced utilities including batch calls for improved performance, pre-built price oracles for major DEXs, specialized Safe functionality, and Django integration components that would otherwise require significant custom development.

How does the batch call feature improve performance?
By combining multiple read-only contract calls into a single RPC request, batch calls significantly reduce network latency and node load, often improving response times by 5-10x compared to sequential requests.

Can I use custom Ethereum nodes with safe-eth-py?
Yes, the EthereumClient supports any HTTP/HTTPS Ethereum node endpoint. You can use nodes from providers like Infura, Alchemy, or your own privately managed nodes.

What chains are supported by the Safe Transaction Service API?
The API supports multiple networks including Ethereum Mainnet, Gnosis Chain, Polygon, Arbitrum, Optimism, and other Ethereum Virtual Machine (EVM) compatible chains. 👉 View real-time supported networks

How often are price oracles updated?
Price oracles fetch real-time data from on-chain sources, providing up-to-date pricing information with each request. The actual update frequency depends on the specific exchange and pool activity.

Is safe-eth-py suitable for production applications?
Yes, the library is battle-tested and used in production by many projects in the Safe ecosystem. However, as with any financial software, thorough testing and security audits are recommended before deploying to production.

Conclusion

The safe-eth-py library represents a comprehensive toolkit for Ethereum developers, particularly those working with Safe smart accounts and DeFi applications. By abstracting complex blockchain interactions and providing ready-to-use components for both backend and web development, it significantly accelerates development workflows while maintaining security and reliability.

Whether you're building a simple DApp interface, a complex DeFi dashboard, or integrating Safe functionality into your application, safe-eth-py provides the necessary building blocks to implement robust Ethereum functionality with minimal boilerplate code.

The library continues to evolve with the Ethereum ecosystem, regularly adding support for new chains, protocols, and standards. For the latest developments and contribution opportunities, visit the project's GitHub repository and consider joining the active community of developers maintaining and improving this valuable resource.