Skip to content

Commit

Permalink
2.0 Release (#14)
Browse files Browse the repository at this point in the history
Features:

    Subscribe to daily claim payouts for validator/nominator wallets
    Monitor commission change events

Closes #3
  • Loading branch information
dadatuputi authored Jul 21, 2023
1 parent 6c015a5 commit e28944e
Show file tree
Hide file tree
Showing 39 changed files with 2,019 additions and 892 deletions.
55 changes: 47 additions & 8 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,63 @@ DISCORD_TOKEN=
APP_ID=
# dev server ID for testing
DEV_GUILD_ID=
# discord bot appearance
## optional: sets username at login
BOT_USERNAME=xx monitor bot
## optional: sets what the bot is 'listening to' at login
BOT_STATUS=user commands
## optional: sets the avatar at login
BOT_AVATAR=res/xx_logo_color.png
## optional: sends action failures (e.g. claims) to provided channel id, or sends to user if set to 'dm'
ADMIN_NOTIFY_CHANNEL=

BOT_USERNAME=xx monitor bot # this is updated on bot login
BOT_STATUS=user commands # this is updated on bot login
BOT_AVATAR=res/xx_logo_color.png # this is not updated on bot login
## optional: provides a donate address to the /donate command - required for /donate
DONATE_WALLET=

# XX API variables
ENDPOINT=https://dashboard-api.xx.network/v1/nodes
ENDPOINT_CRON=* * * * * # how often (in seconds) to pull the current node list (default is every minute)
DASHBOARD_URL=https://dashboard.xx.network/nodes # used to build dashbord URIs
## api endpoint to pull cmix node status
CMIX_API_ENDPOINT=https://dashboard-api.xx.network/v1/nodes
## cron expression for polling the cmix node list (default is every minute)
CMIX_API_CRON=* * * * *
## base url to build dashbord URIs
CMIX_DASH_URL=https://dashboard.xx.network/nodes
## base url to build explorer URIs
EXPLORER_URL=https://explorer.xx.network/accounts

# XX Chain variables
## optional: location for cert for connecting to chain rpc. required if using endpoint with self-signed cert
NODE_EXTRA_CA_CERTS=
## xx chain RPC endpoint (wss://...)
CHAIN_RPC_ENDPOINT=

# XX Claim variables
## cron expression for regular payout (default is every day at 7:05 AM)
CLAIM_CRON_DAILY=5 7 * * *
## optional cron expression for irregular payout (default is off, or every Monday at 7:05 AM) - if this is set, /claim weekly will be activated
#CLAIM_CRON_WEEKLY=5 7 * * 1
## maximun number of transactions to batch together
CLAIM_BATCH=10
## JSON string from exported wallet
CLAIM_WALLET=''
## password string for exported wallet
CLAIM_PASSWORD=''
## optional: external claim endpoint if hosting claim wallet list externally
CLAIM_ENDPOINT=''
## optional: key for external claim endpoint
CLAIM_ENDPOINT_KEY=''

# MongoDb variables
MONGO_INITDB_ROOT_USERNAME=root
MONGO_INITDB_ROOT_PASSWORD= # just generate a random password here
## generate a random password here
MONGO_INITDB_ROOT_PASSWORD=
MONGO_CONTAINER_NAME=mongo
MONGO_PORT=27017
MONGO_URI=mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@${MONGO_CONTAINER_NAME}:${MONGO_PORT}/
ME_CONFIG_BASICAUTH_USERNAME=bot_admin
ME_CONFIG_BASICAUTH_PASSWORD= # put your mongo-express password here
## choose a password to access mongo-express password here
ME_CONFIG_BASICAUTH_PASSWORD=
## port used to access mongo express when in development
ME_PORT=8081

# Watchtower variables
# Timezone - used for update cron
Expand Down
10 changes: 8 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@
"request": "launch",
"runtimeArgs": [
"-r",
"ts-node/register"
"ts-node/register",
"--loader",
"ts-node/esm"
],
"args": [
"${workspaceFolder}/src/index.ts"
],
"env": {
"TS_NODE_PROJECT": "${workspaceFolder}/tsconfig.json"
},
"envFile": "${workspaceFolder}/.env"
"envFile": "${workspaceFolder}/.env",
"outFiles": [
"${workspaceFolder}/dist/**/*.js",
"!**/node_modules/**"
]
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:16
FROM node:20-alpine

# Create app directory
WORKDIR /usr/src/app
Expand Down
195 changes: 7 additions & 188 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,195 +2,14 @@

The latest version of this bot is accessible at the [engul.ph discord server](https://discord.gg/Y7jQEbv5za).

# Getting Started
# Features

Clone this repo into your linux environment.
- Monitor xx node status
- Monitor xx node commission changes
- Monitor xx node name changes
- Subscribe to daily payouts for your nominator or validator

### Requirements
# Installation

* `docker`
Instructions are found on the [wiki](https://github.com/dadatuputi/xx_monitor_bot/wiki/Installation)

### Development Requirements

If you want to change the code and test it outside docker, install:

* `nodejs`
* `npm`

## Step 1: Create a Discord Application on the Discord Developer Portal

Visit the [Discord Developer Portal](https://discord.com/developers/applications) and create an application.

Make note of your:

1. `Application ID` found under `General Information`
2. `Bot Token` found under `Bot`

## Optional: Create a Discord Server for Testing / Development

Create a private discord server to test the bot on.

## Step 2: Authorize the Bot on your Server

To authorize the bot with the necessary permissions (`Create commands in a server`) to join your server, build the url as follows (taken from the [`discord.js` guide](https://discordjs.guide/preparations/adding-your-bot-to-servers.html)):

```
https://discord.com/api/oauth2/authorize?client_id=<APPLICATION_ID>&permissions=0&scope=bot%20applications.commands
```

## Step 3: Set up environmental variables

Copy `.env.template` to `.env`:

```bash
$ cp .env.template .env
$ vi .env
```

Fill in missing variables in `.env`, such as the Discord Token and App ID. Edit any others as desired. If you are using a test server, copy the server id to the `DEV_GUILD_ID` variable. The server id is found in Discord through these steps:

1. Enable `Developer Mode` in the Discord App
2. Right click on the server and select `Copy ID`

## Step 4: Build the Docker Image

Run the following command to build an image from source:

```bash
$ docker compose build
```

## Step 5: Publish the commands to your server

Use the `bot-utils.js` script to deploy the bot commands to your dev server or globally to all servers. You can either run the script in a container, or locally if you have `node.js` installed.

### Using `bot-utils.js` in docker

Run this command to view the script options:

```
$ docker run --env-file .env -it xx_monitor_bot-bot node built/bot-utils.js
```

### **Required:** Deploy commands to server or globally

You may choose to deploy globally or to a specific server. Deploying globally allows users to interact with the bot over Direct Messages. Don't deploy to both, however, because the commands will appear multiple times in the Discord UI.

To deploy globally:

```
$ docker run --env-file .env -it xx_monitor_bot-bot node built/bot-utils.js deploy --global
```

To deploy to a server:

```
$ docker run --env-file .env -it xx_monitor_bot-bot node built/bot-utils.js deploy
```

To un-deploy:

```
$ docker run --env-file .env -it xx_monitor_bot-bot node built/bot-utils.js deploy --reset
```

**To deploy changes you make to the `SlashCommandBuilder` object in your commands**:

Just deploy as you originally did, either globally or to a server.

### Update username
```
$ docker run --env-file .env -it xx_monitor_bot-bot node built/bot-utils.js username "xx monitor bot"
```

### Update avatar
```
$ docker run --env-file .env --volume <image path, e.g. ./image.jpg>:/image -it xx_monitor_bot-bot node built/bot-utils.js avatar /image
```

### Using `bot-utils.js` locally

Use the following commands to install the required packages and publish the slash commands to your server:

```bash
$ npm install
$ node built/bot-utils.js help
$ [... command output ...]
```

You can also set the bot username and status with the `BOT_USERNAME` and `BOT_STATUS` variables in `.env` and they will be set each time the bot starts.


## Step 6: Start the Bot

Run the following command to start the bot in the background:

```bash
$ docker compose up -d
```

To view the logs of the running containers, try the following commands:

```bash
$ docker compose logs # shows most recent logs from all containers
$ docker compose logs -f # follows the log output of all containers continuously
$ docker compose logs -f bot # follows the bot console continuously
```

## Step 7: Interact with the bot

The slash commands are self-documenting. In your server, start typing `/` and a list of available slash commands will display.

### Commands

#### `/monitor_node id name`

Enter this command to monitor a node of the given `id`. Because node status changes are made over DM, the bot will try to send you a DM. If it is successful, it will start monitoring the node for future status changes. `name` is optional. If not set, the bot will try to use the name provided by the API.

#### `/list_monitored_nodes format`

This command will show a list of nodes that you are monitoring. `format` is optional, and is either `Text` (default) or `Buttons`.

#### `/unmonitor_node id`

This command will instruct the bot to stop monitor the given node `id` for you.

# Development & Testing

## Running bot in development mode

To run the bot outside docker for testing or development, first start up an ephemeral mongodb service:

```bash
$ cd bot
$ docker compose -f mongo-only-compose.yml --env-file .env up
```

From another terminal, run:

```bash
$ npm install # if it hasn't been run before
$ npm run start
```

Now you can run the app without building a docker image for each test run.

When you are done testing, you can clean up the containers by returning to the `docker compose` terminal and stopping the `mongo` services with `Ctrl-c` and:

```bash
$ docker compose -f mongo-only-compose.yml -v
```

## Deploying Updates

To deploy updates, simply pull the changes from git, rebuild the container in docker, bring the new container online, and optionally redeploy the commands (if the `SlashCommandBuilder` object in any of the commands has been updated):

```bash
$ git pull
...
$ docker compose build
...
$ docker compose up -d
...
$ docker run --env-file .env -it xx_monitor_bot-bot node built/bot-utils.js deploy --global # note! deploy how you originally deployed
```
22 changes: 22 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: '3.1'
name: xx-monitor-bot-dev
services:

mongo-express:
image: mongo-express
restart: always
depends_on:
- mongo
ports:
- ${ME_PORT}:8081
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=${MONGO_INITDB_ROOT_USERNAME}
- ME_CONFIG_MONGODB_ADMINPASSWORD=${MONGO_INITDB_ROOT_PASSWORD}
- ME_CONFIG_MONGODB_SERVER=${MONGO_CONTAINER_NAME}
- ME_CONFIG_MONGODB_PORT=${MONGO_PORT}
- ME_CONFIG_BASICAUTH_USERNAME
- ME_CONFIG_BASICAUTH_PASSWORD

bot:
environment:
- NODE_ENV=development
17 changes: 11 additions & 6 deletions mongo-only-compose.yml → docker-compose.mongo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,28 @@ version: '3.1'
services:

mongo:
image: mongo
restart: always
image: mongo:6.0.6
environment:
- MONGO_INITDB_ROOT_USERNAME # Value-less variables are pulled directly from .env
- MONGO_INITDB_ROOT_PASSWORD
volumes:
- ./mongo/db:/data/db
ports:
- ${MONGO_PORT}:${MONGO_PORT}
command: --port ${MONGO_PORT} --bind_ip 0.0.0.0 --quiet
- ${MONGO_PORT}:27017
command: --quiet

mongo-express:
image: mongo-express
restart: always
depends_on:
- mongo
ports:
- 8081:8081
- ${ME_PORT}:8081
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=${MONGO_INITDB_ROOT_USERNAME}
- ME_CONFIG_MONGODB_ADMINPASSWORD=${MONGO_INITDB_ROOT_PASSWORD}
- ME_CONFIG_MONGODB_URL=${MONGO_URI}
- ME_CONFIG_MONGODB_SERVER=${MONGO_CONTAINER_NAME}
- ME_CONFIG_MONGODB_PORT=${MONGO_PORT}
- ME_CONFIG_BASICAUTH_USERNAME
- ME_CONFIG_BASICAUTH_PASSWORD

Loading

0 comments on commit e28944e

Please sign in to comment.