Skip to content

Commit

Permalink
Pricing bot (#2)
Browse files Browse the repository at this point in the history
* works on my machine, lol

* dockerize price bot WIP

* clean up

* add docs for setting up discord bots

* set up logging 🌳

* refactor to multibot setup

* fix build

* add tests placeholder

* clean up

* lint, test, secure, CI

* clean up pyproject.toml

* get rid of prints

* move tests to docker image

* clean up

* clean up CI yaml

* clean up env vars

* add basic smoke tests for the data layer

* lint

* clean up unused vars

* update WEB3_PROVIDER_URI

* fix linter
  • Loading branch information
callmephilip authored Oct 30, 2023
1 parent 63c363d commit 5c5a732
Show file tree
Hide file tree
Showing 20 changed files with 2,634 additions and 1 deletion.
15 changes: 15 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
DISCORD_TOKEN_PRICING=
WEB3_PROVIDER_URI=https://mainnet.optimism.io
LP_SUGAR_ADDRESS=0xa1F09427fa89b92e9B4e4c7003508C8614F19791
PRICE_ORACLE_ADDRESS=0x07F544813E9Fb63D57a92f28FbD3FF0f7136F5cE
# token we are converting from
TOKEN_ADDRESS=0x9560e827aF36c94D2Ac33a39bCE1Fe78631088Db
CONNECTOR_TOKENS_ADDRESSES=0x9560e827aF36c94D2Ac33a39bCE1Fe78631088Db,0x4200000000000000000000000000000000000042,0x4200000000000000000000000000000000000006,0x8c6f28f2f1a3c87f0f938b96d27520d9751ec8d9,0x1f32b1c2345538c0c6f582fcb022739c4a194ebb,0xbfd291da8a403daaf7e5e9dc1ec0aceacd4848b9,0xc3864f98f2a61a7caeb95b039d031b4e2f55e0e9,0x9485aca5bbbe1667ad97c7fe7c4531a624c8b1ed,0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1
# token we are converting to
STABLE_TOKEN_ADDRESS=0x7F5c764cBc14f9669B88837ca1490cCa17c31607
# how often does the bot tick
BOT_TICKER_INTERVAL_MINUTES=1
# caching for Sugar tokens
SUGAR_TOKENS_CACHE_MINUTES=10
# caching for oracle pricing
ORACLE_PRICES_CACHE_MINUTES=10
5 changes: 5 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[flake8]
select = B,B9,BLK,C,E,F,S,W
ignore = E203,E501,W503
per-file-ignores = tests/*:S101
max-line-length = 88
11 changes: 11 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: Tests
on: push
jobs:
tests:
runs-on: ubuntu-latest
name: Run tests
steps:
- uses: actions/checkout@v4

- name: Run tests
run: docker build --no-cache --target test ./
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.10.9
50 changes: 50 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
FROM python:3.10-slim as python-base

ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PIP_NO_CACHE_DIR=off \
PIP_DISABLE_PIP_VERSION_CHECK=on \
PIP_DEFAULT_TIMEOUT=100 \
POETRY_HOME="/opt/poetry" \
POETRY_VIRTUALENVS_IN_PROJECT=true \
POETRY_NO_INTERACTION=1 \
PYSETUP_PATH="/opt/pysetup" \
VENV_PATH="/opt/pysetup/.venv"
ENV PATH="$POETRY_HOME/bin:$VENV_PATH/bin:$PATH"

FROM python-base as builder-base

RUN apt-get update \
&& apt-get install --no-install-recommends -y \
curl \
build-essential
RUN curl -sSL https://install.python-poetry.org | python3 -

WORKDIR $PYSETUP_PATH
COPY ./poetry.lock ./pyproject.toml ./
RUN poetry install --only main

FROM builder-base as test

COPY --from=builder-base $VENV_PATH $VENV_PATH

COPY . /app
WORKDIR /app

RUN poetry install --only dev
RUN pip install --upgrade nox
RUN poetry run nox

FROM python-base as runtime

COPY --from=builder-base $VENV_PATH $VENV_PATH

COPY ./docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh

COPY ./bots /app/bots
WORKDIR /app

ENTRYPOINT /docker-entrypoint.sh $0 $@

CMD [ "python", "-m", "bots.__main__"]
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,39 @@
# bots
Discord Bots

## Pre-reqs

- [Poetry](https://python-poetry.org/)
- [Nox](https://nox.thea.codes/en/stable/)

## Running locally

- `cp .env.example .env`
- set `DISCORD_TOKEN` for your bot
- `poetry install`
- `poetry run python -m bots.__main__`

## Before you push

```bash
poetry run nox
```

Got issues with formatting?

```bash
poetry run nox -rs black
```

## Setting up Bots on Discord

- head to [Discord Developer Portal](https://discord.com/developers/applications)
- click `New Application` (top right corner)
- come up with a sensible name for the bot, accept TOS, click `Create`
- click `Bot` in the left hand side panel
- click `Reset Token`
- copy newly created token and keep it to share with your trusted dev
- click `OAuth2` in the left had side panel
- click `URL Generator` in the sub menu
- in `Scopes`, select `bot`
- in `Bot Permissions`, select `Change Nickname`
- copy `Generated URL` and open it in a new tab to add bot to a server you control
1 change: 1 addition & 0 deletions bots/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "0.1.0"
34 changes: 34 additions & 0 deletions bots/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import asyncio
import logging

import discord

from .settings import (
DISCORD_TOKEN_PRICING,
TOKEN_ADDRESS,
STABLE_TOKEN_ADDRESS,
)
from .data import Token
from .helpers import LOGGING_HANDLER, LOGGING_LEVEL
from .price import PriceBot


async def main():
"""Main function."""

# configure discord logging handler
discord_logger = logging.getLogger("discord")
discord_logger.setLevel(LOGGING_LEVEL)
discord_logger.addHandler(LOGGING_HANDLER)

token = await Token.get_by_token_address(TOKEN_ADDRESS)
stable = await Token.get_by_token_address(STABLE_TOKEN_ADDRESS)

bot = PriceBot(
source_token=token, target_token=stable, intents=discord.Intents.default()
)
await bot.start(DISCORD_TOKEN_PRICING)


if __name__ == "__main__":
asyncio.run(main())
Loading

0 comments on commit 5c5a732

Please sign in to comment.