Custom Triggers
- Dominant language
- Python
- Stars
- 134
- Forks
- 14
- PR merge metrics
- No merged PRs in 30d
Description
### Overview
Sometimes there is a need to have custom triggers, which allows defining logic in response to off-chain events such as CEX websocket feeds, Pyth API updates, etc.
### Specification
Define a new module `.triggers` where we define `BaseTrigger` that is inherited by all of our supported triggers, then add new method `SilverbackBot.on_trigger` that accepts `*triggers: BaseTrigger` defined as a handler for OR combination of all `trigger` condition execution. Lastly, redefine `SilverbackBot.on_` and `SilverbackBot.cron` to re-map to this new class system, and simplify the internal logic of handling different operational modes for backtesting, etc.
Usage:
```py
...
from pydantic import BaseModel
from silverback import SilverbackBot
from silverback.triggers import BaseTrigger
bot = SilverbackBot()
class CustomAPIModel(BaseModel):
pair: str
time: datetime
price_updates: list[float]
class CustomTrigger(BaseTrigger):
def __init__(self, *custom_args):
...
# NOTE: For "backtesting" use (aka historical data)
async def backfill(
self, start_time: datetime, stop_time: datetime
) -> AsyncIterator[CustomAPIModel]:
yield from map(
CustomAPIModel.model_load,
await self.api.get(
"/historical",
params=dict(start_time=str(start), stop_time=str(stop)),
).json(),
)
# NOTE: For "runtime" use
async def __aiter__(self) -> AsyncIterator[CustomAPIModel]:
async for item in self.api.get("/streaming", stream=True):
yield CustomAPIModel.model_load(item.json())
# NOTE: This allows defining OR triggers (whenever one OR the other has an update)
@bot.on_trigger(CustomTrigger("ETH/USD"), CustomTrigger("BTC/USD"), ...)
async def on_custom_trigger(value: CustomAPIModel):
... # do stuff
```
Using this, we can also refactor how we use the data from Ape for events logs and blocks, also how we represent cron triggers internally
### Dependencies
n/a
Contributor guide
Research direction
Start by locating SilverbackBot and the existing trigger-handling paths, then read the current on_ and cron implementations. Trace how backtesting, runtime iteration, Ape event logs, and blocks are represented. Done means the .triggers module and BaseTrigger support the specified runtime and backfill behavior, on_trigger supports OR combinations, and on_ and cron use the new class system.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100