Telegram trading bot guide
This guide will walk through setting up a Telegram Solana trading bot using Privy’s server wallets and the Jup.ag API.
Resources
Github starter repository
A starter repository for building a Solana Telegram trading bot.
Jup.ag API
The Jup.ag API allows Privy wallets to interact with the Jupiter DEX.
Privy Wallets
Privy Wallets are a powerful tool for building Telegram trading bots
Configure your Telegram bot
Create a new bot on telegram and get bot token, learn more here
Get set up with managed wallets
If your app does not have access to managed wallets, request managed wallets access on your dashboard here
Privy managed wallets provide a secure way to manage wallets on your backend, allowing you to create and control wallets programmatically. Learn more about getting started with managed wallets.
Using wallets
In our example application, we will build three basic interactions with Privy wallets:
In this trading bot example, we will create a single wallet for each user that interacts with our bot. We can use the id
associated from the sender to verifiably identify the user. This value comes directly from Telegram’s servers and is only sent from verified users.
Create a wallet (/start
)
Create a new wallet for every user that interacts with your bot.
Create Telegram bot command
Create a Telegram bot command that will be used to start the bot.
Create a wallet
Create a wallet with the createWallet
function. Learn more about the createWallet
function here.
Save wallet to database
Save the wallet id to your database and associate it with the user’s id.
(optional) Block users from creating multiple wallets
Block users from creating multiple wallets by checking if a wallet already exists for the user.
Get a wallet (/getwallet
)
Retrieve the wallet address for the user and fetch the balance of the wallet.
Create a /getwallet command
Get a wallet with the /getwallet
command. Learn more here.
Get balance
Swap tokens (/swap
)
To swap tokens in our trading bot, we will be utilizing the Jupiter Ultra API. With just a few lines of code, we can allow users to swap tokens.
Create a swap command
Take in the token address and amount from the user /swap <token_address> <amount>
. We will validate the token address and amount before proceeding.
Create order
We will use a helper function getJupiterUltraOrder
to create an order with the Jupiter Ultra API.
Call this helper function in the /swap
command.
Sign transaction
Sign the transaction with the signTransaction
function. Learn more about the signTransaction
function here.