forked from warp-id/solana-trading-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
305 lines (269 loc) · 10.8 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import { MarketCache, PoolCache } from './cache';
import { Listeners } from './listeners';
import { Connection, KeyedAccountInfo, Keypair } from '@solana/web3.js';
import { LIQUIDITY_STATE_LAYOUT_V4, MARKET_STATE_LAYOUT_V3, Token, TokenAmount } from '@raydium-io/raydium-sdk';
import { AccountLayout, getAssociatedTokenAddressSync } from '@solana/spl-token';
import { Bot, BotConfig } from './bot';
import { DefaultTransactionExecutor, TransactionExecutor } from './transactions';
import {
getToken,
getWallet,
logger,
COMMITMENT_LEVEL,
RPC_ENDPOINT,
RPC_WEBSOCKET_ENDPOINT,
PRE_LOAD_EXISTING_MARKETS,
LOG_LEVEL,
QUOTE_MINT,
MAX_POOL_SIZE,
MIN_POOL_SIZE,
QUOTE_AMOUNT,
PRIVATE_KEY,
USE_SNIPE_LIST,
AUTO_SELL_DELAY,
MAX_SELL_RETRIES,
AUTO_SELL,
MAX_BUY_RETRIES,
AUTO_BUY_DELAY,
COMPUTE_UNIT_LIMIT,
COMPUTE_UNIT_PRICE,
CACHE_NEW_MARKETS,
TAKE_PROFIT,
STOP_LOSS,
BUY_SLIPPAGE,
SELL_SLIPPAGE,
PRICE_CHECK_DURATION,
PRICE_CHECK_INTERVAL,
SNIPE_LIST_REFRESH_INTERVAL,
TRANSACTION_EXECUTOR,
CUSTOM_FEE,
FILTER_CHECK_INTERVAL,
FILTER_CHECK_DURATION,
CONSECUTIVE_FILTER_MATCHES,
MAX_TOKENS_AT_THE_TIME,
CHECK_IF_MINT_IS_RENOUNCED,
CHECK_IF_FREEZABLE,
CHECK_IF_BURNED,
CHECK_IF_MUTABLE,
CHECK_IF_SOCIALS,
TRAILING_STOP_LOSS,
SKIP_SELLING_IF_LOST_MORE_THAN,
MAX_LAG,
CHECK_HOLDERS,
CHECK_ABNORMAL_DISTRIBUTION,
CHECK_TOKEN_DISTRIBUTION,
TELEGRAM_CHAT_ID,
BLACKLIST_REFRESH_INTERVAL,
MACD_SHORT_PERIOD,
MACD_LONG_PERIOD,
MACD_SIGNAL_PERIOD,
RSI_PERIOD,
TELEGRAM_BOT_TOKEN,
AUTO_SELL_WITHOUT_SELL_SIGNAL,
BUY_SIGNAL_TIME_TO_WAIT,
BUY_SIGNAL_PRICE_INTERVAL,
BUY_SIGNAL_FRACTION_TIME_TO_WAIT,
BUY_SIGNAL_LOW_VOLUME_THRESHOLD,
USE_TELEGRAM,
USE_TA
} from './helpers';
import { WarpTransactionExecutor } from './transactions/warp-transaction-executor';
import { JitoTransactionExecutor } from './transactions/jito-rpc-transaction-executor';
import { TechnicalAnalysisCache } from './cache/technical-analysis.cache';
const connection = new Connection(RPC_ENDPOINT, {
wsEndpoint: RPC_WEBSOCKET_ENDPOINT,
commitment: COMMITMENT_LEVEL,
});
function printDetails(wallet: Keypair, quoteToken: Token, bot: Bot) {
logger.info(`
.. :-===++++-
.-==+++++++- =+++++++++-
..:::--===+=.=: .+++++++++++:=+++++++++:
.==+++++++++++++++=:+++: .+++++++++++.=++++++++-.
.-+++++++++++++++=:=++++- .+++++++++=:.=+++++-::-.
-:+++++++++++++=:+++++++- .++++++++-:- =+++++=-:
-:++++++=++++=:++++=++++= .++++++++++- =+++++:
-:++++-:=++=:++++=:-+++++:+++++====--:::::::.
::=+-:::==:=+++=::-:--::::::::::---------::.
::-: .::::::::. --------:::..
:- .:.-:::.
WARP DRIVE ACTIVATED 🚀🐟
Made with ❤️ by humans.
`);
const botConfig = bot.config;
logger.info('------- CONFIGURATION START -------');
logger.info(`Wallet: ${wallet.publicKey.toString()}`);
logger.info('- Bot -');
logger.info(`Using transaction executor: ${TRANSACTION_EXECUTOR}`);
if (bot.isWarp || bot.isJito) {
logger.info(`${TRANSACTION_EXECUTOR} fee: ${CUSTOM_FEE}`);
} else {
logger.info(`Compute Unit limit: ${botConfig.unitLimit}`);
logger.info(`Compute Unit price (micro lamports): ${botConfig.unitPrice}`);
}
logger.info(`Max tokens at the time: ${botConfig.maxTokensAtTheTime}`);
logger.info(`Pre load existing markets: ${PRE_LOAD_EXISTING_MARKETS}`);
logger.info(`Cache new markets: ${CACHE_NEW_MARKETS}`);
logger.info(`Log level: ${LOG_LEVEL}`);
logger.info(`Max lag: ${MAX_LAG}`);
logger.info('- Buy -');
logger.info(`Buy amount: ${botConfig.quoteAmount.toFixed()} ${botConfig.quoteToken.name}`);
logger.info(`Auto buy delay: ${botConfig.autoBuyDelay} ms`);
logger.info(`Max buy retries: ${botConfig.maxBuyRetries}`);
logger.info(`Buy amount (${quoteToken.symbol}): ${botConfig.quoteAmount.toFixed()}`);
logger.info(`Buy slippage: ${botConfig.buySlippage}%`);
logger.info('- Sell -');
logger.info(`Auto sell: ${AUTO_SELL}`);
logger.info(`Auto sell delay: ${botConfig.autoSellDelay} ms`);
logger.info(`Max sell retries: ${botConfig.maxSellRetries}`);
logger.info(`Sell slippage: ${botConfig.sellSlippage}%`);
logger.info(`Price check interval: ${botConfig.priceCheckInterval} ms`);
logger.info(`Price check duration: ${botConfig.priceCheckDuration} ms`);
logger.info(`Take profit: ${botConfig.takeProfit}%`);
logger.info(`Stop loss: ${botConfig.stopLoss}%`);
logger.info(`Trailing stop loss: ${botConfig.trailingStopLoss}`);
logger.info(`Skip selling if lost more than: ${botConfig.skipSellingIfLostMoreThan}%`);
logger.info('- Snipe list -');
logger.info(`Snipe list: ${botConfig.useSnipeList}`);
logger.info(`Snipe list refresh interval: ${SNIPE_LIST_REFRESH_INTERVAL} ms`);
if (botConfig.useSnipeList) {
logger.info('- Filters -');
logger.info(`Filters are disabled when snipe list is on`);
} else {
logger.info('- Filters -');
logger.info(`Filter check interval: ${botConfig.filterCheckInterval} ms`);
logger.info(`Filter check duration: ${botConfig.filterCheckDuration} ms`);
logger.info(`Consecutive filter matches: ${botConfig.consecutiveMatchCount}`);
logger.info(`Check renounced: ${CHECK_IF_MINT_IS_RENOUNCED}`);
logger.info(`Check freezable: ${CHECK_IF_FREEZABLE}`);
logger.info(`Check burned: ${CHECK_IF_BURNED}`);
logger.info(`Check mutable: ${CHECK_IF_MUTABLE}`);
logger.info(`Check socials: ${CHECK_IF_SOCIALS}`);
logger.info(`Min pool size: ${botConfig.minPoolSize.toFixed()}`);
logger.info(`Max pool size: ${botConfig.maxPoolSize.toFixed()}`);
}
logger.info(`Check Holders: ${botConfig.checkHolders}`);
logger.info(`Check Token Distribution: ${botConfig.checkTokenDistribution}`);
logger.info(`Check Abnormal Distribution: ${botConfig.checkAbnormalDistribution}`);
logger.info(`Blacklist refresh interval: ${BLACKLIST_REFRESH_INTERVAL}`);
logger.info(`Buy signal MACD: ${MACD_SHORT_PERIOD}/${MACD_LONG_PERIOD}/${MACD_SIGNAL_PERIOD}`);
logger.info(`Buy signal RSI: ${RSI_PERIOD}`);
logger.info('------- CONFIGURATION END -------');
logger.info('Bot is running! Press CTRL + C to stop it.');
}
const runListener = async () => {
logger.level = LOG_LEVEL;
logger.info('Bot is starting...');
const marketCache = new MarketCache(connection);
const poolCache = new PoolCache();
const technicalAnalysisCache = new TechnicalAnalysisCache();
let txExecutor: TransactionExecutor;
switch (TRANSACTION_EXECUTOR) {
case 'warp': {
txExecutor = new WarpTransactionExecutor(CUSTOM_FEE);
break;
}
case 'jito': {
txExecutor = new JitoTransactionExecutor(CUSTOM_FEE, connection);
break;
}
default: {
txExecutor = new DefaultTransactionExecutor(connection);
break;
}
}
const wallet = getWallet(PRIVATE_KEY.trim());
const quoteToken = getToken(QUOTE_MINT);
const botConfig = <BotConfig>{
wallet,
quoteAta: getAssociatedTokenAddressSync(quoteToken.mint, wallet.publicKey),
minPoolSize: new TokenAmount(quoteToken, MIN_POOL_SIZE, false),
maxPoolSize: new TokenAmount(quoteToken, MAX_POOL_SIZE, false),
quoteToken,
quoteAmount: new TokenAmount(quoteToken, QUOTE_AMOUNT, false),
maxTokensAtTheTime: MAX_TOKENS_AT_THE_TIME,
useSnipeList: USE_SNIPE_LIST,
autoSell: AUTO_SELL,
autoSellDelay: AUTO_SELL_DELAY,
maxSellRetries: MAX_SELL_RETRIES,
autoBuyDelay: AUTO_BUY_DELAY,
maxBuyRetries: MAX_BUY_RETRIES,
unitLimit: COMPUTE_UNIT_LIMIT,
unitPrice: COMPUTE_UNIT_PRICE,
takeProfit: TAKE_PROFIT,
stopLoss: STOP_LOSS,
trailingStopLoss: TRAILING_STOP_LOSS,
skipSellingIfLostMoreThan: SKIP_SELLING_IF_LOST_MORE_THAN,
buySlippage: BUY_SLIPPAGE,
sellSlippage: SELL_SLIPPAGE,
priceCheckInterval: PRICE_CHECK_INTERVAL,
priceCheckDuration: PRICE_CHECK_DURATION,
filterCheckInterval: FILTER_CHECK_INTERVAL,
filterCheckDuration: FILTER_CHECK_DURATION,
consecutiveMatchCount: CONSECUTIVE_FILTER_MATCHES,
checkHolders:CHECK_HOLDERS,
checkTokenDistribution:CHECK_TOKEN_DISTRIBUTION,
checkAbnormalDistribution:CHECK_ABNORMAL_DISTRIBUTION,
telegramChatId:TELEGRAM_CHAT_ID,
telegramBotToken: TELEGRAM_BOT_TOKEN,
blacklistRefreshInterval: BLACKLIST_REFRESH_INTERVAL,
MACDLongPeriod: MACD_LONG_PERIOD,
MACDShortPeriod: MACD_SHORT_PERIOD,
MACDSignalPeriod: MACD_SIGNAL_PERIOD,
RSIPeriod: RSI_PERIOD,
autoSellWithoutSellSignal: AUTO_SELL_WITHOUT_SELL_SIGNAL,
buySignalTimeToWait: BUY_SIGNAL_TIME_TO_WAIT,
buySignalPriceInterval: BUY_SIGNAL_PRICE_INTERVAL,
buySignalFractionPercentageTimeToWait: BUY_SIGNAL_FRACTION_TIME_TO_WAIT,
buySignalLowVolumeThreshold: BUY_SIGNAL_LOW_VOLUME_THRESHOLD,
useTelegram: USE_TELEGRAM,
useTechnicalAnalysis: USE_TA
};
const bot = new Bot(connection, marketCache, poolCache, txExecutor, technicalAnalysisCache, botConfig);
const valid = await bot.validate();
if (!valid) {
logger.info('Bot is exiting...');
process.exit(1);
}
if (PRE_LOAD_EXISTING_MARKETS) {
await marketCache.init({ quoteToken });
}
const runTimestamp = Math.floor(new Date().getTime() / 1000);
const listeners = new Listeners(connection);
await listeners.start({
walletPublicKey: wallet.publicKey,
quoteToken,
autoSell: AUTO_SELL,
cacheNewMarkets: CACHE_NEW_MARKETS,
});
listeners.on('market', (updatedAccountInfo: KeyedAccountInfo) => {
const marketState = MARKET_STATE_LAYOUT_V3.decode(updatedAccountInfo.accountInfo.data);
marketCache.save(updatedAccountInfo.accountId.toString(), marketState);
});
listeners.on('pool', async (updatedAccountInfo: KeyedAccountInfo) => {
const poolState = LIQUIDITY_STATE_LAYOUT_V4.decode(updatedAccountInfo.accountInfo.data);
const poolOpenTime = parseInt(poolState.poolOpenTime.toString());
const exists = await poolCache.get(poolState.baseMint.toString());
let currentTimestamp = Math.floor(new Date().getTime() / 1000);
let lag = currentTimestamp - poolOpenTime;
if (!exists && poolOpenTime > runTimestamp) {
poolCache.save(updatedAccountInfo.accountId.toString(), poolState);
if(MAX_LAG != 0 && lag > MAX_LAG){
logger.trace(`Lag too high: ${lag} sec`);
return;
} else {
logger.trace(`Lag: ${lag} sec`);
await bot.buy(updatedAccountInfo.accountId, poolState, lag);
}
}
});
listeners.on('wallet', async (updatedAccountInfo: KeyedAccountInfo) => {
const accountData = AccountLayout.decode(updatedAccountInfo.accountInfo.data);
if (accountData.mint.equals(quoteToken.mint)) {
return;
}
await bot.sell(updatedAccountInfo.accountId, accountData);
});
printDetails(wallet, quoteToken, bot);
};
runListener();