One of the most groundbreaking changes in the Ethereum ecosystem was the creation of ERC-4626, a method for tokenizing yield earning vaults.
In this guide, we’ll walk you through the steps to clone an ERC4626 bot project, configure your environment, and run a bot built using the Ape Framework and Silverback. Because of how the yield is abstracted into a token it becomes harder to figure out how much true yield the protocol is generating vs what's stated on the protocol or issuers site. This means that unless you’re calculating the returns you’re getting from an ERC-4626 token every day then you have to trust that the posted yield is what you’re actually getting. By the end of this tutorial, you’ll be able to observe real-time Ethereum block data and vault share prices right from your terminal.
But this is crypto. Trust is bullshit. Here’s how you can build a bot to verify the yield for you.
Silverback is a framework designed to help you create bots that interact with the blockchain. It simplifies tasks like:
Listening for events (e.g., new blocks, transactions)
Fetching live data (e.g., token prices, vault share values)
Automating DeFi strategies( Advance )
You can easily run your bots with a minimal amount of code while taking advantage of Silverback's modular design and asynchronous task management. Let’s dive in!
The bot you're setting up is a price checker for an ERC4626-compliant vault. It listens for new blocks on the Ethereum blockchain and retrieves the current share price from a vault every time a block is mined. Specifically, the bot does the following:
Monitors New Ethereum Blocks: It uses the Silverback framework to listen for block events in real-time on the Ethereum mainnet.
Fetches Vault Share Price: Each time a new block is created, the bot queries the share price of the ERC4626 vault. The share price represents the value of vault shares, which fluctuate as assets are deposited, withdrawn, or accrue yield within the vault.
Logs the Results: The bot logs the block number, block hash, and the updated share price, allowing users to see how the vault's value evolves with each block.
In simple terms, this bot provides live data on the value of an ERC4626 vault, giving you insights into how the vault performs in real-time as transactions occur on the Ethereum network.
The code for this project is available here
First, let’s clone the repository from GitHub make sure your copy the demo branch
git clone https://github.com/ApeAcademy/erc-4626.git
git checkout remotes/origin/demo
cd erc-4626
This project contains all the code necessary to run a bot that interacts with ERC4626-compliant vaults.
In this tutorial I export
the variable in my cli session so I don't have to create an .env
file. Here is how to to do that:
export WEB3_ALCHEMY_PROJECT_ID=your-alchemy-id
export ERC4626_VAULT_ADDRESS=your-vault-address
echo $WEB3_ALCHEMY_PROJECT_ID
echo $ERC4626_VAULT_ADDRESS
Replace your-alchemy-id
with your actual API key from Alchemy, and replace your-vault-address
with the address of the ERC4626 vault you want to interact with. I used a yearn vault ERC4626_VAULT_ADDRESS=0x583019fF0f430721aDa9cfb4fac8F06cA104d0B4
Next, we need to install the required Python packages and Ape Framework plugins. Run the following command:
pip install -r requirements.txt
ape plugins install . -U
This will install Silverback, Ape Framework, and any other dependencies required to run the bot.
Ensure your ape-config.yaml
file is configured to use Alchemy as the default provider for Ethereum mainnet. If you want to learn more how to configure your ape-config.yaml
If you cloned the project you ape-config.yaml should look like this:
name: ERC4626
plugins:
- name: vyper
- name: alchemy
- name: etherscan
ethereum:
default_network: mainnet
mainnet:
default_provider: alchemy
With one simple command you can run the project with:
silverback run bots.silverback_yield:bot
The bot will start monitoring Ethereum blocks and printing out the share price of the ERC4626 vault every time a new block is mined. Here's a sample output you can expect:
Price Event: 0.9547177472995926
SUCCESS: update_shareprice[block_number=20915791,block_hash=0xb0b2215bce3801b0412e39b75eb73ef2f7ad8d9618c32d1460086c1cb41ed9eb] - 0.340s (2.6%)
Price Event: 0.9547177355383596
SUCCESS: update_shareprice[block_number=20915792,block_hash=0x27c1c49273640ac02ea87ed934fa7dd367aea4803005373b1b13d0c34201600a] - 0.360s (2.8%)
Below is the core code for our Silverback bot that monitors an ERC4626 vault:
import os
from ape import Contract, chain, networks
from silverback import SilverbackBot
bot = SilverbackBot
with networks.ethereum.mainnet.use_provider("alchemy"): # or "infura"
vault = Contract(os.environ.get("ERC4626_VAULT_ADDRESS"))
one_share = 10 ** vault.decimals()
@bot.on_(chain.blocks)
def update_shareprice(_):
"""
Convert shares to a set price unit.
"""
price = vault.convertToShares(one_share) / one_share
# Total number of shares in the vault divide by
# the current unit price of a share = gwei
print(f"Price Event: {price}")
Network Connection: The bot connects to the Ethereum mainnet using either Alchemy or Infura as the provider.
Vault Contract: The Contract
object connects to the specified ERC4626 vault via the contract address defined in your environment variables.
Price Update: The bot listens for new blocks (@bot.on_(chain.blocks)
) and triggers the update_shareprice
function every time a new block is mined. It retrieves the vault’s current share price by calling the convertToShares
method on the vault contract and logs the price event.
Cluster: In the cluster, if you return data from one of the handlers, the cluster will collect that data for later review
Missing .env
file: Ensure your .env
file is in the root of your project directory and formatted correctly.
Provider Errors: Double-check that your WEB3_ALCHEMY_PROJECT_ID
is correct and that you’ve set the correct default network in ape-config.yaml
.
FileNotFoundError: If you get an error related to missing directories, verify that your bots/
folder is in the root of the project.
Now that you’ve successfully set up and run your first Silverback bot, consider expanding its functionality:
Track Multiple Vaults: Extend the bot to monitor multiple ERC4626 vaults at the same time.
Automate Transactions: Add logic to trigger a transaction when certain conditions are met, such as depositing or withdrawing assets based on share price thresholds.
Notify Users: Integrate email or messaging alerts when certain events occur, such as a significant change in vault value.
With Silverback, you’re not just limited to monitoring prices—automating DeFi strategies is just as easy!
Check out the main branch of this demo, it has advance yield calculations for a year's worth of time. It uses a local database via SQ-lite to store the data. The data can accessed by a bot on a Silverback cluster to perform strategies such as liquidate, buy, sell, and so much more.
One of the most groundbreaking changes in the Ethereum ecosystem was the creation of ERC-4626, a method for tokenizing yield earning vaults.
Yield is abstracted into a token it becomes harder to figure out how much true value the protocol is generating vs what's stated on the protocol or issuers site. This means that unless you’re calculating the returns you’re getting from an ERC-4626 token every day then you have to trust that the posted yield is what you’re actually getting.
Subscribe so you can see the next tutorials where we teach you everything from how to make an automated trading bot to building a bot that scans the chain in real time to one that makes a hotdog powered stablecoin.
Also if you want the latest on Silverback and to see our experiments, join the hidden Telegram chat where we post our latest tools and tips on botcrafting so you can build your robot army:
Telegram Silverback
If you want more Apey goodness, check out our Discord where we go deep into the Ape Framework, crypto engineering, and how you can customize your setup to let you work faster than you could ever have imagined.
ApeWorX Discord
ApeWorX LTD and Ninjagod
Over 200 subscribers