Skip to main content

Telegram Bot

The GHOST Telegram bot (ghost-tg) provides a conversational interface for interacting with the protocol directly from Telegram. It supports multi wallet management, encrypted lending, collateralized borrowing, and real time notifications.

Technology Stack

ComponentTechnology
RuntimeBun
Bot FrameworkgrammY
Blockchainethers.js v6 on Sepolia
Encryptioneciesjs (secp256k1 ECIES)
External WalletsWalletConnect v2
AuthenticationEIP 712 typed data signatures

Wallet Management

The bot supports three wallet types:

Wallet TypeDescription
EmbeddedGenerated and stored locally by the bot. Private key held in bot storage.
ImportedUser provides an existing private key. Stored encrypted in bot storage.
WalletConnectConnect an external wallet (MetaMask, Rainbow, etc.) via WalletConnect v2. Signing requests are forwarded to the external wallet.

Users can switch between wallets at any time. All protocol operations use the currently active wallet for signing.

Commands

Wallet Commands

CommandDescription
/startWelcome message and wallet detection
/walletView active wallet, create new, import, or connect via WalletConnect

Lending Commands

CommandDescription
/lendStart a new lend intent flow (amount, rate, deposit, confirm)
/cancel_lendCancel an active lend intent and reclaim funds
/lend_statusView all active lend positions

Borrowing Commands

CommandDescription
/borrowStart a new borrow intent flow (amount, collateral, max rate)
/cancel_borrowCancel a pending borrow intent
/proposalsView and respond to match proposals
/borrow_statusView all borrow positions and proposals

Loan Management

CommandDescription
/loansView all active loans
/repayRepay an active loan in full
/claim_collateralClaim excess collateral from an overcollateralized position

Transfers and Swaps

CommandDescription
/transferExecute a private transfer to another shielded address
/withdrawWithdraw funds from the vault to an on chain address
/swapSwap between gUSD and gETH with live quotes

Information

CommandDescription
/balancesView private vault balances and on chain balances
/creditView credit score, tier, and collateral multiplier
/poolView pool status and liquidity
/helpList all commands
/alertsToggle real time notifications on or off

Lending Flow

The lending flow is a guided 4 step conversation:

  1. Amount. Bot prompts for the lending amount in gUSD.
  2. Rate. Bot prompts for the desired interest rate. The rate is encrypted client side using the CRE public key before submission.
  3. Deposit. Bot instructs the user to deposit tokens into the vault and private transfer to the pool. Provides the pool's shielded address.
  4. Confirm. Bot submits the lend intent with the EIP 712 signature and encrypted rate.

Borrowing Flow

  1. Amount. Bot prompts for the borrow amount in gUSD.
  2. Collateral. Bot calculates required collateral based on the user's credit tier and current ETH price. User confirms the collateral amount.
  3. Max Rate. Bot prompts for the maximum acceptable rate. Encrypted before submission.
  4. Submit. Bot submits the borrow intent with EIP 712 signature.

After submission, the bot automatically polls for match proposals and notifies the user when one arrives.

Notifications

The bot includes a polling based notification system (notifier.ts) that checks for:

  • New match proposals requiring acceptance or rejection
  • Proposal auto acceptance (when the 5 second window expires)
  • Loan status changes (repaid, liquidated)
  • Transfer completions

Users can toggle notifications with the /alerts command.

Architecture

User (Telegram) <-> grammY Bot <-> GHOST Server API
<-> External Vault API
<-> WalletConnect v2 (optional)

The bot acts as a thin client. All protocol logic runs on the server and CRE. The bot handles:

  • Conversation state (multi step flows)
  • Wallet management and signing
  • Rate encryption
  • API calls to the GHOST server
  • User notification polling

Running the Bot

cd ghost-tg
bun install

Set environment variables:

BOT_TOKEN=<telegram-bot-token>
GHOST_SERVER_URL=<ghost-api-url>
EXTERNAL_API_URL=<vault-api-url>
CRE_PUBLIC_KEY=<secp256k1-public-key-hex>
WALLETCONNECT_PROJECT_ID=<wc-project-id>

Start:

bun run src/index.ts
# or with hot reload
bun run --hot src/index.ts

Source Structure

FilePurpose
src/index.tsBot entry point, command registration
src/config.tsEnvironment configuration
src/constants.tsABIs, EIP 712 type definitions
src/api.tsGHOST server and vault API clients
src/wallet.tsWallet creation, import, storage
src/wc.tsWalletConnect v2 session management
src/notifier.tsPolling notification system
src/ui.tsTelegram message formatting helpers
src/middleware.tsWallet guard middleware
src/commands/Individual command handlers (9 modules)