Interact with Gnosis Safes with Python

In this article we will discuss Gnosis Safes with Python. Gnosis Safes is a powerful smart contract wallet on the Ethereum network that allows users to securely manage their cryptocurrency assets. By using Python and the library, you can easily write programs that interact with Gnosis Safes to perform various tasks such as executing transactions and retrieving wallet information.

In this article, we’ll walk through a sample Python program that interacts with Gnosis Safes step-by-step. We’ll cover everything from setting up your development environment to retrieving wallet information, so that you can get started with writing your own programs to interact with Gnosis Safes.

Setting Up Your Python Development Environment

Before we can start writing Python code to interact with Gnosis Safes, we need to set up our development environment. To get started, we’ll need to install the necessary software:

  • Python: We’ll be using Python 3 for this tutorial, so make sure you have it installed on your system.
  • This is the Python library we’ll be using to interact with the Ethereum network. You can install it using pip:
pip install web3
  • Gnosis Safe: This is the Python module we’ll be using to interact with Gnosis Safes. You can install it using pip:
pip install gnosis-safe

With these tools installed, we’re ready to start writing Python code to interact with Gnosis Safes.

Connecting to the Ethereum Network

The first step in interacting with Gnosis Safes through Python is connecting to the Ethereum network. We’ll be using Infura, a popular API service for interacting with Ethereum nodes. To connect to the network using Infura, you’ll need to sign up for a free account and create a project.

Once you’ve created your Infura project, you’ll need to get your project ID and endpoint URL. You can find these in the “Settings” tab of your project dashboard. The endpoint URL will look something like this:

Make sure to replace “YOUR_PROJECT_ID” with your actual Infura project ID.

Retrieving Gnosis Safe Information in Python

Now that we’ve connected to the Ethereum network, we can start retrieving information about a Gnosis Safe. To do this, we’ll need to create a Gnosis Safe object using the Safe module and the address of an existing Gnosis Safe.

Here’s the Python code to create a Gnosis Safe object:

from import Safe

# Create a Gnosis Safe object using the Safe module
safe_address = '0x1234567890123456789012345678901234567890'
safe = Safe(safe_address, web3)

Make sure to replace “0x1234567890123456789012345678901234567890” with the address of your actual Gnosis Safe.

One of the most basic things we can do with the Safe object is retrieve information about the wallet. Here are a few examples of how to do this:

# Get the address of the Gnosis Safe
safe_address = safe.address

# Get the owners of the Gnosis Safe
owners = safe.get_owners()

# Get the threshold for executing transactions on the Gnosis Safe
threshold = safe.get_threshold()

# Get the balance of the Gnosis Safe in ETH
eth_balance = safe.get_balance()

# Get the balance of a specific ERC20 token in the Gnosis Safe
token_address = '0x1234567890123456789012345678901234567890'
token_balance = safe.get_token_balance(token_address)

These methods allow us to retrieve basic information about the Gnosis Safe, including its owners, transaction threshold, and balance.

Executing Gnosis Safe transactions in Python

Another powerful capability of the Gnosis Safe Python module is the ability to execute transactions on the wallet programmatically. Here’s an example of how to do this. The code creates a SafeTx object to represent the transaction, including the recipient address, amount to transfer, gas price, and other details. It then executes the transaction on the Gnosis Safe using the safe.execute_transaction.

from import SafeTx

# Ethereum address of the recipient
recipient = '0x0987654321098765432109876543210987654321'

# Amount to transfer in wei
amount = 1000000000000000000  # 1 ETH

# Create a SafeTx object to represent the transaction
tx = SafeTx.from_dict({
    'to': recipient,
    'value': amount,
    'data': '',
    'operation': 0,
    'gas': 21000,
    'gasPrice': 1000000000,
    'nonce': 0,

# Execute the transaction
safe_tx_hash = safe.execute_transaction(tx)

The code below executes a transaction on the Gnosis Safe that sends 1 Ether to the target address and

# Target address of transaction
target_address = '0x0987654321098765432109876543210987654321'

# Amount of Ether to send in transaction
value = web3.toWei(1, 'ether')

# Arguments for function being called in transaction
arguments = [1, 2, 3]

# Execute transaction
tx_hash = safe.execute_transaction(target_address, value, arguments)

# Wait for transaction to be confirmed

print('Transaction executed successfully.')