You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* refactor!: change AppState to StateSnapshot to better express it
* feat!: add `app.state`
Adds 2 new system tasks to load and store a snapshot of state.
Currently this is just `last_block_processed` and `last_block_seen`.
This paves the way for the `Parameter` feature, but does not include
that just yet.
BREAKING CHANGE: state snapshotting migrates from runner to worker
* refactor: update from peer review
* docs: add more color in docs for `app.state`
Also updated docstrings within `silverback/application.py`
* fix: don't raise error on exit, only note the error
* refactor: have runner load and save the snapshot in datastore
* docs: add a note to example about using worker startup event
* docs: show off `app.state` and `CircuitBreaker` with more common example
* docs: add another commit about triggering a txn to userguide
* refactor(middlware): don't display INFO logs for system task start
* feat!: change SilverbackApp to SilverbackBot and all changes surrounding it
* fix: dockerfile
* fix: bad isort
* fix: docs surrounding app vs bot
* fix: minor rewording in development docs
* fix: add taskiq-redis
* fix: typo
---------
Co-authored-by: johnson2427 <blakeejohnson39@gmail.com>
Copy file name to clipboardExpand all lines: README.md
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,19 +1,19 @@
1
1
# Quick Start
2
2
3
3
Silverback lets you create and deploy your own Python bots that respond to on-chain events.
4
-
The Silverback library leverages the [Ape](https://docs.apeworx.io/ape/stable/userguides/quickstart) development framework as well as it's ecosystem of plugins and packages to enable you to develop simple-yet-sophisticated automated applications that can listen and respond to live chain data.
4
+
The Silverback library leverages the [Ape](https://docs.apeworx.io/ape/stable/userguides/quickstart) development framework as well as it's ecosystem of plugins and packages to enable you to develop simple-yet-sophisticated automated bots that can listen and respond to live chain data.
5
5
6
-
Silverback applications are excellent for use cases that involve continuously monitoring and responding to on-chain events, such as newly confirmed blocks or contract event logs.
6
+
Silverback bots are excellent for use cases that involve continuously monitoring and responding to on-chain events, such as newly confirmed blocks or contract event logs.
7
7
8
-
Some examples of these types of applications:
8
+
Some examples of these types of bots:
9
9
10
10
- Monitoring new pool creations, and depositing liquidity
11
11
- Measuring trading activity of popular pools
12
12
- Listening for large swaps to update a telegram group
13
13
14
14
## Documentation
15
15
16
-
Please read the [development userguide](https://docs.apeworx.io/silverback/stable/userguides/development.html) for more information on how to develop an application.
16
+
Please read the [development userguide](https://docs.apeworx.io/silverback/stable/userguides/development.html) for more information on how to develop a bot.
17
17
18
18
## Dependencies
19
19
@@ -72,11 +72,11 @@ Silverback will automatically register files in this folder as separate bots tha
72
72
73
73
```{note}
74
74
It is also suggested that you treat this as a scripts folder, and do not include an __init__.py
75
-
If you have a complicated project, follow the previous example to ensure you run the application correctly.
75
+
If you have a complicated project, follow the previous example to ensure you run the bot correctly.
76
76
```
77
77
78
78
```{note}
79
-
A final suggestion would be to name your `SilverbackApp` object `bot`. Silverback automatically searches
79
+
A final suggestion would be to name your `SilverbackBot` object `bot`. Silverback automatically searches
80
80
for this object name when running. If you do not do so, once again, ensure you replace `example` with
ape_alchemy.exceptions.MissingProjectKeyError: Must set one of $WEB3_ALCHEMY_PROJECT_ID, $WEB3_ALCHEMY_API_KEY, $WEB3_ETHEREUM_MAINNET_ALCHEMY_PROJECT_ID, $WEB3_ETHEREUM_MAINNET_ALCHEMY_API_KEY.
140
140
```
141
141
142
-
Go to [Alchemy](https://alchemy.com), create an account, then create an application in their dashboard, and copy the API Key.
142
+
Go to [Alchemy](https://alchemy.com), create an account, then create an bot in their dashboard, and copy the API Key.
143
143
144
144
Another requirement for the command from `Docker Usage` to run the given example is that it uses [ape-tokens](https://github.com/ApeWorX/ape-tokens) plugin to look up token interfaces by symbol.
145
145
In order for this to work, you should have installed and configured that plugin using a token list that includes both YFI and USDC on Ethereum mainnet.
Copy file name to clipboardExpand all lines: docs/userguides/development.md
+66-27Lines changed: 66 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
-
# Developing Applications
1
+
# Developing Bots
2
2
3
-
In this guide, we are going to show you more details on how to build an application with Silverback.
3
+
In this guide, we are going to show you more details on how to build an bot with Silverback.
4
4
5
5
## Prerequisites
6
6
@@ -15,15 +15,15 @@ There are 3 suggested ways to structure your project. In the root directory of y
15
15
16
16
2. Create a `bots/` folder. Then develop bots in this folder as separate scripts (Do not include a __init__.py file).
17
17
18
-
3. Create a `bot/` folder with a `__init__.py` file that will include the instantiation of your `SilverbackApp()` object.
18
+
3. Create a `bot/` folder with a `__init__.py` file that will include the instantiation of your `SilverbackBot()` object.
19
19
20
20
The `silverback` cli automatically searches for python scripts to run as bots in specific locations relative to the root of your project.
21
21
It will also be able to detect the scripts inside your `bots/` directory and let you run those by name (in case you have multiple bots in your project).
22
22
23
23
If `silverback` finds a module named `bot` in the root directory of the project, then it will use that by default.
24
24
25
25
```{note}
26
-
It is suggested that you create the instance of your `SilverbackApp()` object by naming the variable `bot`, since `silverback` will autodetect that variable name when loading your script file.
26
+
It is suggested that you create the instance of your `SilverbackBot()` object by naming the variable `bot`, since `silverback` will autodetect that variable name when loading your script file.
27
27
```
28
28
29
29
Another way you can structure your bot is to create a `bot` folder and define a runner inside of that folder as `__init__.py`.
@@ -43,7 +43,7 @@ If your bot's module name is `example.py` (for example), you can run it like thi
43
43
silverback run example --network your:network:of:choice
44
44
```
45
45
46
-
If the variable that you call the `SilverbackApp()` object is something other than `bot`, you can specific that by adding `:{variable-name}`:
46
+
If the variable that you call the `SilverbackBot()` object is something other than `bot`, you can specific that by adding `:{variable-name}`:
47
47
48
48
```bash
49
49
silverback run example:my_bot --network your:network:of:choice
@@ -52,7 +52,7 @@ silverback run example:my_bot --network your:network:of:choice
52
52
We will automatically detect all scripts under the `bots/` folder automatically, but if your bot resides in a location other than `bots/` then you can use this to run it:
53
53
54
54
```bash
55
-
silverback run folder.example:app --network your:network:of:choice
55
+
silverback run folder.example:bot --network your:network:of:choice
56
56
```
57
57
58
58
Note that with a `bot/__init__.py` setup, silverback will also autodetect it, and you can run it with:
@@ -69,21 +69,21 @@ For the most streamlined experience, develop your bots as scripts, and avoid rel
69
69
If you follow these suggestions, your Silverback deployments will be easy to use and require almost no thought.
70
70
```
71
71
72
-
## Creating an Application
72
+
## Creating a Bot
73
73
74
-
Creating a Silverback Application is easy, to do so initialize the `silverback.SilverbackApp` class:
74
+
Creating a Silverback Bot is easy, to do so initialize the `silverback.SilverbackBot` class:
75
75
76
76
```py
77
-
from silverback importSilverbackApp
77
+
from silverback importSilverbackBot
78
78
79
-
bot =SilverbackApp()
79
+
bot =SilverbackBot()
80
80
```
81
81
82
-
The SilverbackApp class handles state and configuration.
82
+
The SilverbackBot class handles state and configuration.
83
83
Through this class, we can hook up event handlers to be executed each time we encounter a new block or each time a specific event is emitted.
84
-
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 necessary on-chain interactions required.
84
+
Initializing the bot 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 necessary on-chain interactions required.
85
85
86
-
However, by default an app has no configured event handlers, so it won't be very useful.
86
+
However, by default an bot has no configured event handlers, so it won't be very useful.
87
87
This is where adding event handlers is useful via the `bot.on_` method.
88
88
This method lets us specify which event will trigger the execution of our handler as well as which handler to execute.
You can also add an application startup and shutdown handler that will be **executed once upon every application startup**. This may be useful for things like processing historical events since the application was shutdown or other one-time actions to perform at startup.
166
+
You can also add an bot startup and shutdown handler that will be **executed once upon every bot startup**. This may be useful for things like processing historical events since the bot was shutdown or other one-time actions to perform at startup.
167
167
168
168
```py
169
169
@bot.on_startup()
@@ -180,7 +180,46 @@ def handle_on_shutdown():
180
180
...
181
181
```
182
182
183
-
*Changed in 0.2.0*: The behavior of the `@bot.on_startup()` decorator and handler signature have changed. It is now executed only once upon application startup and worker events have moved on `@bot.on_worker_startup()`.
183
+
*Changed in 0.2.0*: The behavior of the `@bot.on_startup()` decorator and handler signature have changed. It is now executed only once upon bot startup and worker events have moved on `@bot.on_worker_startup()`.
184
+
185
+
## Bot State
186
+
187
+
Sometimes it is very useful to have access to values in a shared state across your workers.
188
+
For example you might have a value or complex reference type that you wish to update during one of your tasks, and read during another.
189
+
Silverback provides `bot.state` to help with these use cases.
190
+
191
+
For example, you might want to pre-populate a large dataframe into state on startup, keeping that dataframe in sync with the chain through event logs,
192
+
and then use that data to determine a signal under which you want trigger transactions to commit back to the chain.
bot.state.table =...# Update using stuff from `log`
206
+
207
+
208
+
@bot.on_(chain.blocks)
209
+
defuse_table(blk):
210
+
if bot.state.table[...].mean() > bot.state.table[...].sum():
211
+
# Trigger your bot to send a transaction from `bot.signer`
212
+
contract.myMethod(..., sender=bot.signer)
213
+
...
214
+
```
215
+
216
+
```{warning}
217
+
You can use `bot.state` to store any python variable type, however note that the item is not networked nor threadsafe so it is not recommended to have multiple tasks write to the same value in state at the same time.
218
+
```
219
+
220
+
```{note}
221
+
Bot startup and bot runtime event triggers (e.g. block or event container) are handled distinctly and can be trusted not to execute at the same time.
222
+
```
184
223
185
224
### Signing Transactions
186
225
@@ -192,10 +231,10 @@ While not recommended, you can use keyfile accounts for automated signing.
192
231
See [this guide](https://docs.apeworx.io/ape/stable/userguides/accounts.html#automation) to learn more about how to do that.
193
232
```
194
233
195
-
## Running your Application
234
+
## Running your Bot
196
235
197
236
Once you have programmed your bot, it's really useful to be able to run it locally and validate that it does what you expect it to do.
198
-
To run your bot locally, we have included a really useful cli command [`run`](../commands/run) that takes care of connecting to the proper network, configuring signers (using your local Ape accounts), and starting up the application client and in-memory task queue workers.
237
+
To run your bot locally, we have included a really useful cli command [`run`](../commands/run) that takes care of connecting to the proper network, configuring signers (using your local Ape accounts), and starting up the bot client and in-memory task queue workers.
199
238
200
239
```sh
201
240
# Run your bot on the Ethereum Sepolia testnet, with your own signer:
`my_bot:bot` is not required for silverback run if you follow the suggested folder structure at the start of this page, you can just call it via `my_bot`.
207
246
```
208
247
209
-
It's important to note that signers are optional, if not configured in the application then `bot.signer` will be `None`.
210
-
You can use this in your application to enable a "test execution" mode, something like this:
248
+
It's important to note that signers are optional, if not configured in the bot then `bot.signer` will be `None`.
249
+
You can use this in your bot to enable a "test execution" mode, something like this:
211
250
212
251
```py
213
252
# Compute some metric that might lead to creating a transaction
214
253
if bot.signer:
215
-
# Execute a transaction via `sender=app.signer`
254
+
# Execute a transaction via `sender=bot.signer`
216
255
else:
217
256
# Log what the transaction *would* have done, had a signer been enabled
218
257
```
219
258
220
259
```{warning}
221
-
If you configure your application to use a signer, and that signer signs anything given to it, remember that you can lose substational amounts of funds if you deploy this to a production network.
222
-
Always test your applications throughly before deploying, and always use a dedicated key for production signing with your application in a remote setting.
260
+
If you configure your bot to use a signer, and that signer signs anything given to it, remember that you can lose substational amounts of funds if you deploy this to a production network.
261
+
Always test your bots throughly before deploying, and always use a dedicated key for production signing with your bot in a remote setting.
223
262
```
224
263
225
264
```{note}
@@ -230,7 +269,7 @@ Use segregated keys and limit your risk by controlling the amount of funds that
230
269
### Distributed Execution
231
270
232
271
Using only the `silverback run ...` command in a default configuration executes everything in one process and the job queue is completely in-memory with a shared state.
233
-
In some high volume environments, you may want to deploy your Silverback application in a distributed configuration using multiple processes to handle the messages at a higher rate.
272
+
In some high volume environments, you may want to deploy your Silverback bot in a distributed configuration using multiple processes to handle the messages at a higher rate.
234
273
235
274
The primary components are the client and workers. The client handles Silverback events (blocks and contract event logs) and creates jobs for the workers to process in an asynchronous manner.
236
275
@@ -265,10 +304,10 @@ silverback worker -w 2
265
304
266
305
The client will send tasks to the 2 worker subprocesses, and all task queue and results data will be go through Redis.
267
306
268
-
## Testing your Application
307
+
## Testing your Bot
269
308
270
309
TODO: Add backtesting mode w/ `silverback test`
271
310
272
-
## Deploying your Application
311
+
## Deploying your Bot
273
312
274
-
Check out the [Platform Deployment Userguide](./platform.html) for more information on how to deploy your application to the [Silverback Platform](https://silverback.apeworx.io).
313
+
Check out the [Platform Deployment Userguide](./platform.html) for more information on how to deploy your bot to the [Silverback Platform](https://silverback.apeworx.io).
Copy file name to clipboardExpand all lines: docs/userguides/platform.md
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,14 @@
1
-
# Deploying Applications
1
+
# Deploying Bots
2
2
3
3
In this guide, we are going to show you more details on how to deploy your application to the [Silverback Platform](https://silverback.apeworx.io).
4
4
5
5
## Creating a Cluster
6
6
7
-
The Silverback Platform runs your Applications (or "Bots") on dedicated managed application Clusters.
7
+
The Silverback Platform runs your Bots on dedicated managed application Clusters.
8
8
These Clusters will take care to orchestrate infrastructure, monitor, run your triggers, and collect metrics for your applications.
9
9
Each Cluster is bespoke for an individual or organization, and isolates your applications from others on different infrastructure.
10
10
11
-
Before we deploy our Application, we have to create a Cluster.
11
+
Before we deploy our Bot, we have to create a Cluster.
12
12
If you haven't yet, please sign up for Silverback at [https://silverback.apeworx.io](https://silverback.apeworx.io).
13
13
14
14
Once you have signed up, you can actually create (and pay for) your Clusters from the Silverback CLI utility by first
@@ -44,7 +44,7 @@ For instance, to list all your available bots on your cluster, use [`silverback
44
44
To obtain general information about your cluster, just use [`silverback cluster info`][silverback-cluster-info],
45
45
or [`silverback cluster health`][silverback-cluster-health] to see the current status of your Cluster.
46
46
47
-
If you have no bots, we will first have to containerize our Applications and upload them to a container registry that our Cluster is configured to access.
47
+
If you have no bots, we will first have to containerize our Bots and upload them to a container registry that our Cluster is configured to access.
48
48
49
49
```{note}
50
50
Building a container for your application can be an advanced topic, we have included the `silverback build` subcommand to help assist in generating Dockerfiles.
@@ -108,7 +108,7 @@ Silverback Clusters include an environment variable management system for exactl
108
108
which you can manage using [`silverback cluster vars`][silverback-cluster-vars] subcommand.
109
109
110
110
The environment variable management system makes use of a concept called "Variable Groups" which are distinct collections of environment variables meant to be used together.
111
-
These variable groups will help in managing the runtime environment of your Applications by allowing you to segregate different variables depending on each bot's needs.
111
+
These variable groups will help in managing the runtime environment of your Bots by allowing you to segregate different variables depending on each bot's needs.
112
112
113
113
To create an environment group, use the [`silverback cluster vars new`][silverback-cluster-vars-new] command and give it a name and a set of related variables.
114
114
For instance, it may make sense to make a group of variables for your favorite Ape plugins or services, such as RPC Providers, Blockchain Data Indexers, Etherscan, etc.
@@ -199,7 +199,7 @@ Any task execution that experiences an error will abort execution (and therefore
199
199
All errors encountered during task exeuction are reported to the Cluster for later review by any users with appriopiate access.
200
200
Tasks do not retry (by default), but updates to `app.state` are maintained up until the point an error occurs.
201
201
202
-
It is important to keep track of these errors and ensure that none of them are in fact critical to the operation of your Application,
202
+
It is important to keep track of these errors and ensure that none of them are in fact critical to the operation of your Bot,
203
203
and to take corrective or preventative action if it is determined that it should be treated as a more critical failure condition.
0 commit comments