MCPcopy Index your code
hub / github.com/RubyRedOwl08/defi-trading-bot

github.com/RubyRedOwl08/defi-trading-bot @v1.0.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.0.0 ↗ · + Follow
134 symbols 278 edges 43 files 0 documented · 0%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Introduction

This is a mini DeFi trading Bot project on decentralized exchange operating on BSC chain.

Here is the article of why I set this up and steps of how to use it. 👉  Article


Stack architecture

I use Nest.js framework for Node.js server to connect to BSC chain by using library nestjs-ethers and using aws kms to manage wallet private key. You can use it easily by using the private key of the server directly (but I don’t recommend this way.) Use PostgreSQL for collecting data, and use WardenSwap SDK for getting the token price via WardenSwap router contract for swapping token.

Please note that I’ll not demonstrate the AWS CLI, AWS KMS, PostgreSQL setting up process in this document.

Prerequisites

Please make sure that Node.js > v14.0.0 is installed on your operating system.

Installation

  1. Install all dependencies
$ yarn install
  1. Create a .env file and use config file from.env.sample

  2. Manage wallet private key

  3. Option 1

    Use AWS KMS (Recommend)

    I use kms (Key Management Service) for encrypting wallet private key and store cipher text base64

WALLET_ENCRYPT_BASE64 in .env file when the server start, the system uses cipher text base64 to decrypt the private key.

 Ref.
 [Managing Encryption Keys With AWS KMS In Node.js](https://medium.com/hackernoon/managing-encryption-keys-with-aws-kms-in-node-js-c320c860019a)

 - **Step 1**


   Open `.env` file to set `USE_WALLET_TYPE=ENCRYPT_BASE64`, `AWS_REGION`, `AWS_KMS_KEY_ID`,`AWS_CREDENTIAL_PROFILE_NAME`, `WALLET_PRIVATE_KEY_FOR_ENCRYPTION`


   For running script encryption, temporarily put wallet private key in this process.
 - **Step 2**


   run `yarn encryptionPrivateKey` script. When encryption is done, search for `resultFromScripts/encryptData.txt` path folder. Input data with .env file. The key is `WALLET_ENCRYPT_BASE64`


 - **Step 3**


   Remove key and value `WALLET_PRIVATE_KEY_FOR_ENCRYPTION`
  • Option2

    Set USE_WALLET_TYPE=PRIVATE_KEY and WALLET_PRIVATE_KEY for instant use.

  • Create PostgreSQL database name defi-trading-bot-manager

Running the app

# development
$ yarn start

# watch mode
$ yarn start:dev

# production mode
$ yarn start:prod

ENV File

Name Description
USE_WALLET_TYPE Access wallet with private key or encryption data, You can choose to enter 2 values. ENCRYPT_BASE64 or PRIVATE_KEY
WALLET_PRIVATE_KEY Private key of wallet
WALLET_ENCRYPT_BASE64 Encryption private key from aws kms
POSTGRES_HOST PostgreSQL host name
POSTGRES_PORT PostgreSQL TCP port
POSTGRES_USERNAME PostgreSQL username
POSTGRES_PASSWORD PostgreSQL password
POSTGRES_DATABASE PostgreSQL database name
POSTGRES_AUTO_LOAD_ENTITIES If true, models will be loaded automatically.
POSTGRES_SYNCHRONIZE If true, automatically loaded models will be synchronized.
AWS_REGION AWS Region
AWS_CREDENTIAL_PROFILE_NAME AWS CLI profile name
AWS_KMS_KEY_ID AWS KMS key ID for encryption and decryption

REST API

POST /orderbook

Create order trade

Name Type In Description
srcTokenAddress string body Source token address to swap
destTokenAddress string body Destination token address to receive
srcAmount string body Amount of source token to swap
orderType string body Order type you want open, You can choose to enter MARKET, STOP_LIMIT
stopPrice string body The price in a stop order that triggers the creation of a market order

GET /orderbook

List all orderbook

Name Type In Description
orderStatus string query Status of orderbook

GET /orderbook/{orderId}

Get orderbook by order ID

Name Type In Description
orderId string path Get orderbook data from order ID

PATCH /orderbook/{orderId}/cancel

Cancel an active order

Name Type In Description
orderId string path Cancle order form order ID

GET /wardenswap/current-price

Get the current price

Name Type In Description
srcTokenAddress string query Source token address to swap
destTokenAddress string query Destination token address to receive
srcAmount string query Amount of source token to swap

Security Vulnerabilities

If you discover a security vulnerability, please send us an e-mail at ruby.red.owl.08@gmail.com

Special thanks

License

defi-trading-Bot is licensed under the MIT license.

See LICENSE for the full license text.

Developed with ❤️ and 🍺

Extension points exported contracts — how you extend this code

Token (Interface)
(no doc)
src/wardenswap/interfaces/wardenswap.interface.ts
TransactionSummary (Interface)
(no doc)
src/wardenswap/interfaces/wardenswap.interface.ts

Core symbols most depended-on inside this repo

getTokenData
called by 14
src/utils/utils.service.ts
updateOrderBookById
called by 6
src/orderbook/orderbook.repository.ts
getRate
called by 5
src/wardenswap/wardenswap.service.ts
checkBestRateAmountOut
called by 4
src/utils/utils.service.ts
delay
called by 3
src/utils/utils.service.ts
getTokenContract
called by 3
src/ethersConnect/ethers.service.ts
getOrderbookById
called by 3
src/orderbook/orderbook.service.ts
getTokenPriceUsd
called by 2
src/wardenswap/wardenswap.service.ts

Shape

Method 62
Class 60
Enum 7
Function 3
Interface 2

Languages

TypeScript100%

Modules by API surface

src/ethersConnect/ethers.service.ts13 symbols
src/wardenswap/wardenswap.service.ts11 symbols
src/utils/utils.service.ts10 symbols
src/orderbook/orderbook.service.ts7 symbols
src/orderbook/orderbook.controller.ts7 symbols
src/botManager/bot-manager.service.ts7 symbols
src/orderbook/orderbook.repository.ts6 symbols
src/botManager/bot-manager.task.ts5 symbols
src/wardenswap/wardenswap.controller.ts4 symbols
src/tradebook/tradebook.service.ts4 symbols
src/postgresConfig/postgres-config.service.ts4 symbols
src/awsSdkConfig/aws-sdk-config.service.ts4 symbols

For agents

$ claude mcp add defi-trading-bot \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page