Skip to content

Commit

Permalink
Merge pull request #14 from tablelandnetwork/dtb/catch-rpc-error
Browse files Browse the repository at this point in the history
Catch Glif RPC error via Basin HTTP API & add migration
  • Loading branch information
dtbuchholz authored Apr 5, 2024
2 parents 1098153 + fd9e712 commit 3cce436
Show file tree
Hide file tree
Showing 11 changed files with 598 additions and 40 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ DISCORD_WEBHOOK_ID_INTERNAL=server_discord_webhook_id
DISCORD_WEBHOOK_TOKEN_INTERNAL=server_discord_webhook_token
DISCORD_WEBHOOK_ID_EXTERNAL=server_discord_webhook_id
DISCORD_WEBHOOK_TOKEN_EXTERNAL=server_discord_webhook_token
PRIVATE_KEY=your_private_key
PRIVATE_KEY=your_private_key
NODE_ENV=development # or production
44 changes: 44 additions & 0 deletions .github/workflows/migration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Used only on manual triggers in case Tableland supported chains are altered,
# thus, resulting in the previous vault data having chains that should not be
# used. It's a pseudo-migration because it just creates a fresh db that gets
# written to the vault.
name: Migrate vault data
on:
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Cache node modules
id: cache-npm
uses: actions/cache@v3
env:
cache-name: cache-node-modules
with:
path: ~/.npm
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
- name: Install dependencies
run: npm install
- name: Build app
run: |
npm run build
- name: Run vault migration
run: |
npm run migrate
env:
DISCORD_BOT_TOKEN: ${{ secrets.DISCORD_BOT_TOKEN }}
DISCORD_WEBHOOK_ID_INTERNAL: ${{ secrets.DISCORD_WEBHOOK_ID_INTERNAL }}
DISCORD_WEBHOOK_TOKEN_INTERNAL: ${{ secrets.DISCORD_WEBHOOK_TOKEN_INTERNAL }}
DISCORD_WEBHOOK_ID_EXTERNAL: ${{ secrets.DISCORD_WEBHOOK_ID_EXTERNAL }}
DISCORD_WEBHOOK_TOKEN_EXTERNAL: ${{ secrets.DISCORD_WEBHOOK_TOKEN_EXTERNAL }}
PRIVATE_KEY: ${{ secrets.PRIVATE_KEY }}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
.vscode
.DS_Store
.env
.env.local
.env.local
basin-config-dev.json
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,23 @@ https://github.com/tablelandnetwork/discord-sql-logs
To get started, run `npm install` and then `npm run build` command; this will compile the package to the `dist` directory. To run the app, you will need to set the following environment variables in your `.env` file:

- `DISCORD_BOT_TOKEN`: The Discord bot token comes from creating a bot in the Discord Developer Portal at [https://discord.com/developers/applications](https://discord.com/developers/applications) and getting a token under the `Bot` tab and `Reset Token` button.
- `DISCORD_WEBHOOK_ID`, `DISCORD_WEBHOOK_TOKEN`: These webhook variables come from the server's URL of the webhook, which you create in the Discord channel settings: `https://discord.com/api/webhooks/DISCORD_WEBHOOK_ID/DISCORD_WEBHOOK_TOKEN`
- `DISCORD_WEBHOOK_ID_<INTERNAL|EXTERNAL>`, `DISCORD_WEBHOOK_TOKEN_<INTERNAL|EXTERNAL>`: These webhook variables come from the server's URL of the webhook, which you create in the Discord channel settings: `https://discord.com/api/webhooks/DISCORD_WEBHOOK_ID/DISCORD_WEBHOOK_TOKEN`. Note that each webhook is for a different channel, and the bot will separate messages based on if they are tables that are part of applications build by the Tableland team.
- `PRIVATE_KEY`: A private key for creating and writing to a Basin vault, which stores the SQLite database (retrieves it before each run, writes the database to the vault after each run).
- `NODE_ENV`: Optional, but can be set to `development` to run the bot in development mode. Otherwise, it will run in production mode.

A vault name must be set in the `basin-config.json` file and is then created for the private key provided, which also signs subsequent writes to the vault. For example, this bot uses the `sql_logs_bot_state.db` vault, but keep in mind that **vault names must be unique and cannot be reused**. If you'd like to see the events for this vault, it's owned by `0xc2c3d5FFB6d60FFA48abBAFadCEfDf2bD4FD1905` and [viewable here](https://basin.tableland.xyz/vaults/sql_logs_bot_state.db/events).

Once those are set, you can run the app with `npm run start`, and it will start the bot. First, it runs through initialization steps to fetch events from the vault, and then it begins post the latest SQL logs to the Discord channel's webhook. Note that it only runs once, so it will exit until you run it again manually—e.g., this repo uses a GitHub Action cron job and runs the app every 15 minutes.

## Development

Follow the steps in the [Usage](#usage) section to get started. There are also a few other commands you can use:
Follow the steps in the [Usage](#usage) section to get started. In particular, you can set up a `.env` file with a `NODE_ENV=development` variable to run the bot in development mode, which will also run from the `src/` directory instead of the `dist/` directory. The development mode also requires you create a filename called `basin-config-dev.json` in the root directory with the same format as `basin-config.json` but with a different vault name.

There are also a few other commands you can use:

- `npm run build`: Compile the package to the `dist` directory.
- `npm run start`: Start the bot.
- `npm run dev`: Start the bot with development settings defined in `.env` and build from `src/`.
- `npm run lint`: Lint the codebase with `eslint` (along with the `lint:fix` option).
- `npm run prettier`: Prettify the code format with `prettier` (along with the `prettier:fix` option).
- `npm run format`: Both lint and format the codebase with `eslint` and `prettier`, also fixing any issues it can.
Expand Down
Loading

0 comments on commit 3cce436

Please sign in to comment.