generated from ApeWorX/project-template
-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b0d9d7a
commit 33f63cc
Showing
118 changed files
with
11,018 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Sphinx build info version 1 | ||
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done. | ||
config: ec4f6b7323256e570d47128dc12840e1 | ||
tags: 645f666f9bcd5a90fca523b33c5a78b7 |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
run | ||
*** | ||
|
||
.. click:: silverback._cli:run | ||
:prog: run | ||
:nested: none |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Silverback Docs | ||
|
||
```{eval-rst} | ||
.. toctree:: | ||
:caption: User Guides | ||
:maxdepth: 1 | ||
|
||
userguides/quickstart | ||
userguides/development | ||
``` | ||
|
||
```{eval-rst} | ||
.. toctree:: | ||
:caption: CLI Reference | ||
:maxdepth: 1 | ||
|
||
commands/run.rst | ||
``` | ||
|
||
```{eval-rst} | ||
.. toctree:: | ||
:caption: Python Reference | ||
:maxdepth: 1 | ||
|
||
methoddocs/application.md | ||
methoddocs/runner.md | ||
methoddocs/middlewares.md | ||
methoddocs/subscriptions.md | ||
methoddocs/exceptions.md | ||
methoddocs/utils.md | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# silverback.application | ||
|
||
The `silverback.application` module contains the high-level implementation of the the user's | ||
Silverback application, meant to be used to expose method handlers and other functionality. | ||
|
||
```{eval-rst} | ||
.. automodule:: silverback.application | ||
:members: | ||
:show-inheritance: | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# silverback.exceptions | ||
|
||
```{eval-rst} | ||
.. automodule:: silverback.exceptions | ||
:members: | ||
:show-inheritance: | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# silverback.middlewares | ||
|
||
The `silverback.middlewares` module contains middleware intended to improve the usability of | ||
silverback as a whole, and add integrations for the silverback platform as well. | ||
|
||
```{eval-rst} | ||
.. automodule:: silverback.middlewares | ||
:members: | ||
:show-inheritance: | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# silverback.runner | ||
|
||
The `silverback.runner` module contains implementations for running Silverback apps in a variety | ||
of different scenarios and trigger methods. | ||
|
||
```{eval-rst} | ||
.. automodule:: silverback.runner | ||
:members: | ||
:show-inheritance: | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# silverback.subscriptions | ||
|
||
The `silverback.subscriptions` module contains an implementation of a Websocket subscription queue, | ||
used for connected to an RPC node via websockets that implements the `eth_subscribe` RPC method. | ||
|
||
```{eval-rst} | ||
.. automodule:: silverback.subscriptions | ||
:members: | ||
:show-inheritance: | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# silverback.utils | ||
|
||
```{eval-rst} | ||
.. automodule:: silverback.utils | ||
:members: | ||
:show-inheritance: | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# Developing a Silverback Application | ||
|
||
In this guide, we are going to show you more details on how to build an application with Silverback. | ||
|
||
## Prerequisites | ||
|
||
You should have a python project with Silverback installed. | ||
You can install Silverback via `pip install silverback` | ||
|
||
## Creating an Application | ||
|
||
Creating a Silverback Application is easy, to do so initialize the `silverback.SilverbackApp` class: | ||
|
||
```py | ||
from silverback import SilverbackApp | ||
|
||
app = SilverbackApp() | ||
``` | ||
|
||
The SilverbackApp class handles state and configuration. | ||
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. | ||
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. | ||
|
||
However, by default an app has no configured event handlers, so it won't be very useful. | ||
This is where adding event handlers is useful via the `app.on_` method. | ||
This method lets us specify which event will trigger the execution of our handler as well as which handler to execute. | ||
|
||
## New Block Events | ||
|
||
To add a block handler, you will do the following: | ||
|
||
```py | ||
from ape import chain | ||
|
||
@app.on_(chain.blocks) | ||
def handle_new_block(block): | ||
... | ||
``` | ||
|
||
Inside of `handle_new_block` you can define any logic that you want to handle each new `block` detected by the silverback client. | ||
You can return any serializable data structure from this function and that will be stored in the results database as a trackable metric for the execution of this handler. | ||
Any errors you raise during this function will get captured by the client, and recorded as a failure to handle this `block`. | ||
|
||
## New Event Logs | ||
|
||
Similarly to blocks, you can handle events emitted by a contract by adding an event handler: | ||
|
||
``` | ||
from ape import Contract | ||
|
||
TOKEN = Contract(<your token address here>) | ||
|
||
@app.on_(TOKEN.Transfer) | ||
def handle_token_transfer_events(transfer): | ||
... | ||
``` | ||
|
||
Inside of `handle_token_transfer_events` you can define any logic that you want to handle each new `transfer` event that gets emitted by `TOKEN.Transfer` detected by the silverback client. | ||
Again, you can return any serializable data structure from this function and that will be stored in the results database as a trackable metric for the execution of this handler. | ||
Any errors you raise during this function will get captured by the client, and recorded as a failure to handle this `transfer` event log. | ||
|
||
## Startup and Shutdown | ||
|
||
If you have heavier resources you want to load during startup, or otherwise perform some data collection prior to starting the bot, you can add a startup function like so: | ||
|
||
```py | ||
@app.on_startup() | ||
def handle_on_worker_startup(state): | ||
... | ||
``` | ||
|
||
This function comes a parameter `state` that you can use for storing the results of your startup computation or resources that you have provisioned. | ||
It's import to note that this is useful for ensuring that your workers (of which there can be multiple) have the resources necessary to properly handle any updates you want to make in your handler functions, such as connecting to the Telegram API, an SQL or NoSQL database connection, or something else. | ||
The `state` variable is also useful as this gets made available to each handler method so other stateful quantities can be maintained for other uses. | ||
|
||
TODO: Add more information about `state` | ||
|
||
## Running your Application | ||
|
||
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. | ||
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. | ||
|
||
```sh | ||
# Run your bot on the Ethereum Sepolia testnet, with your own signer: | ||
$ silverback run my_bot:app --network :sepolia --account acct-name | ||
``` | ||
|
||
It's important to note that signers are optional, if not configured in the application then `app.signer` will be `None`. | ||
You can use this in your application to enable a "test execution" mode, something like this: | ||
|
||
```py | ||
# Compute some metric that might lead to creating a transaction | ||
if app.signer: | ||
# Execute a transaction via `sender=app.signer` | ||
else: | ||
# Log what the transaction *would* have done, had a signer been enabled | ||
``` | ||
|
||
```note | ||
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. | ||
Always test your applications throughly before deploying. | ||
``` | ||
|
||
## Testing your Application | ||
|
||
TODO: Add backtesting mode w/ `silverback test` | ||
|
||
## Deploying to the Silverback Platform | ||
|
||
TODO: Add packaging and deployment to the Silverback platform, once available. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
```{include} ../../README.md | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* Compatability shim for jQuery and underscores.js. | ||
* | ||
* Copyright Sphinx contributors | ||
* Released under the two clause BSD licence | ||
*/ | ||
|
||
/** | ||
* small helper function to urldecode strings | ||
* | ||
* See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent#Decoding_query_parameters_from_a_URL | ||
*/ | ||
jQuery.urldecode = function(x) { | ||
if (!x) { | ||
return x | ||
} | ||
return decodeURIComponent(x.replace(/\+/g, ' ')); | ||
}; | ||
|
||
/** | ||
* small helper function to urlencode strings | ||
*/ | ||
jQuery.urlencode = encodeURIComponent; | ||
|
||
/** | ||
* This function returns the parsed url parameters of the | ||
* current request. Multiple values per key are supported, | ||
* it will always return arrays of strings for the value parts. | ||
*/ | ||
jQuery.getQueryParameters = function(s) { | ||
if (typeof s === 'undefined') | ||
s = document.location.search; | ||
var parts = s.substr(s.indexOf('?') + 1).split('&'); | ||
var result = {}; | ||
for (var i = 0; i < parts.length; i++) { | ||
var tmp = parts[i].split('=', 2); | ||
var key = jQuery.urldecode(tmp[0]); | ||
var value = jQuery.urldecode(tmp[1]); | ||
if (key in result) | ||
result[key].push(value); | ||
else | ||
result[key] = [value]; | ||
} | ||
return result; | ||
}; | ||
|
||
/** | ||
* highlight a given string on a jquery object by wrapping it in | ||
* span elements with the given class name. | ||
*/ | ||
jQuery.fn.highlightText = function(text, className) { | ||
function highlight(node, addItems) { | ||
if (node.nodeType === 3) { | ||
var val = node.nodeValue; | ||
var pos = val.toLowerCase().indexOf(text); | ||
if (pos >= 0 && | ||
!jQuery(node.parentNode).hasClass(className) && | ||
!jQuery(node.parentNode).hasClass("nohighlight")) { | ||
var span; | ||
var isInSVG = jQuery(node).closest("body, svg, foreignObject").is("svg"); | ||
if (isInSVG) { | ||
span = document.createElementNS("http://www.w3.org/2000/svg", "tspan"); | ||
} else { | ||
span = document.createElement("span"); | ||
span.className = className; | ||
} | ||
span.appendChild(document.createTextNode(val.substr(pos, text.length))); | ||
node.parentNode.insertBefore(span, node.parentNode.insertBefore( | ||
document.createTextNode(val.substr(pos + text.length)), | ||
node.nextSibling)); | ||
node.nodeValue = val.substr(0, pos); | ||
if (isInSVG) { | ||
var rect = document.createElementNS("http://www.w3.org/2000/svg", "rect"); | ||
var bbox = node.parentElement.getBBox(); | ||
rect.x.baseVal.value = bbox.x; | ||
rect.y.baseVal.value = bbox.y; | ||
rect.width.baseVal.value = bbox.width; | ||
rect.height.baseVal.value = bbox.height; | ||
rect.setAttribute('class', className); | ||
addItems.push({ | ||
"parent": node.parentNode, | ||
"target": rect}); | ||
} | ||
} | ||
} | ||
else if (!jQuery(node).is("button, select, textarea")) { | ||
jQuery.each(node.childNodes, function() { | ||
highlight(this, addItems); | ||
}); | ||
} | ||
} | ||
var addItems = []; | ||
var result = this.each(function() { | ||
highlight(this, addItems); | ||
}); | ||
for (var i = 0; i < addItems.length; ++i) { | ||
jQuery(addItems[i].parent).before(addItems[i].target); | ||
} | ||
return result; | ||
}; | ||
|
||
/* | ||
* backward compatibility for jQuery.browser | ||
* This will be supported until firefox bug is fixed. | ||
*/ | ||
if (!jQuery.browser) { | ||
jQuery.uaMatch = function(ua) { | ||
ua = ua.toLowerCase(); | ||
|
||
var match = /(chrome)[ \/]([\w.]+)/.exec(ua) || | ||
/(webkit)[ \/]([\w.]+)/.exec(ua) || | ||
/(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) || | ||
/(msie) ([\w.]+)/.exec(ua) || | ||
ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) || | ||
[]; | ||
|
||
return { | ||
browser: match[ 1 ] || "", | ||
version: match[ 2 ] || "0" | ||
}; | ||
}; | ||
jQuery.browser = {}; | ||
jQuery.browser[jQuery.uaMatch(navigator.userAgent).browser] = true; | ||
} |
Oops, something went wrong.