Cover photo

Ape Scream

The On-Chain Bear-Market Void-Screaming Protocol: Tutorial by Ninjagod1251

Ape Scream

Project Overview

At ApeWorX we're used to creating bots and plugins to serve a useful purpose and make people's lives easier.

So as a break from all that we thought "What's the silliest Silverback bot we could make?" and came up with the idea of making a bot that screams into the void every time you make a transaction.

My name is Chris, and in this tutorial I'm going to show you how you combine Warpcast Frames, Generative AI, Vyper and Silverback to make a unique picture based on specific conditions on the blockchain and post it to Farcaster

To follow along with this project, clone: https://github.com/ApeAcademy/SilverbackEcho

Project Inspiration

The inspiration for this project came from a Telegram channel called Bear Market Screaming Therapy, dedicated to audio messages of people screaming. I wanted to automate a similar concept using Silverback and Farcaster.

Perfect. Now let's put it on chain (or at least a testnet).

Project Implementation

This project involves integrating a simple Vyper contract called echo.vy with a Silverback bot connected to Warpcast and a Generative AI service to post a unique picture by watching for an Event to emit a Received message.

Basically on-chain void-screaming

Silverback Hollaback.py breakdown:

Hollaback.py is the Silverback bot itself. This is shinning star of the whole project. If you take away anything from this, it is this right here. This tool can be expanded on and to form so many projects integrating the off-chain and on-chain worlds. So let's breakdown the code:

from silverback import SilverbackApp

# Initialize the Silverback app first
app = SilverbackApp()

We want to trigger our logic based off of the event we want to watch. To do that, we add the a decorator @app.on_(my_contract.Received) to define what we are going to watch for from on-chain.

For more help on creating an application, see the Silverback Docs

Initializing the app creates a network connection using the Ape configuration of your local project, making it easy to add a Silverback bot to your project in order to perform automation of any necessary on-chain interactions. We will keep this app in a separate folder called bots/.

When the event Received is emitted, we are going to cast a message "AH" with the number of H's based on an equation we made (text="A" + "H" * floor(log2(log.amount))), if the amount of wei in the log is >= 0.001 ether. We will cast the message using client.post_cast(text=scream).

Here is the simplified code:

from ape import convert
from ape_farcaster import Warpcast
from silverback import SilverbackApp

app = SilverbackApp()
client = Warpcast(app.signer)


@app.on_(my_contract.Received) # defined what event to watch
def payment_received(log): # execute this on every log
   client.post_cast(text="A" + "H" * floor(log2(log.amount))) #A + xH do the math, and then using the post_cast(text=AHHHHH)

In order to make this project more appealing and creative. We are going to add some creative ways to generate some fun.

Generating AI images

client.post_cast(text="A" + "H" * floor(log2(log.amount))) is the single line of code to publish a message to Farcaster. Casting an image requires client.post_cast(embeds=<direct_url_link_to_image>). So the game plan and puesdo code goes as follows:

app.on_(my_contract.Received)
def payment_received(log):
   client.post_cast(embeds=<direct_url_link_to_image>)
"""
log provides 2 values address of sender and amount of weth sent.
1. create prompt
2. create image(prompt)
3. upload image to Pinata
4. Cast message with address of sender and image 
"""

def create_prompt(number_adj: int) -> str:
"""
1. This method creates a prompt based off of the address sending eth.
"""

def createImage(prompt) -> str:
"""
 2. Creates an image based off the prompt.
"""

def uploadToPinata(image):
"""
3 . Uploads image to pinata and returns an ipfs hash.
"""

Creating the prompt

Creating the prompt uses a library called wonderwords this helps us generate adjectives to fill in the prompt " An ape {adjective_string} ape that is screaming AHHHHH".

Create an Image

Using the Stability.ai documentation on how to create an image via post request we imported our own API key and the prompt and return the file name of the generated image.

Note there is a chance that the prompt has a word that is not approved so that is why we have the prompt in a 10 try for loop.

Upload Image to Pinata

Once the image is created, it needs to be uploaded to a place where a direct link to the image can be referenced. We decided to use Pinata.cloud to host it since it is easy to use. I used a combination of their docs and stackoverflow to find a way to pin a file to ipfs.

At this point, you understand how the whole hollaback script works and you just need to let the magic fly!

Running the Project

To run the project: first deploy the Vyper contract using this tutorial to deploy contracts. Since we only need to deploy this contract once, I just use ape console --network :sepolia and then record the contract address emitted by the script.

Then, run the Silverback App via silverback run bots.hollaback:app --network :sepolia --account <your Farcaster address>. Finally, to trigger the interaction, use ape run holla --network :sepolia --account <your payment account> in a separate terminal. The Silverback App will monitor the contract event emitted when you run the holla script, which will trigger the post as long as the conditions are met.

Conclusion

And ta-da! You now have a bot that you can use to vent your frustration and tokenize any and all existential crisis.

If you want to learn more about Silverback feel free to check out our Discord, and if if you liked this tutorial you'll love our tutorial on how to build a defi app like an aerospace engineer.

Loading...
highlight
Collect this post to permanently own it.
ApeWorX LTD logo
Subscribe to ApeWorX LTD and never miss a post.
#silverback#bot#generative-ai#farcaster