-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_trade_cmd.py
More file actions
1068 lines (831 loc) · 34.3 KB
/
test_trade_cmd.py
File metadata and controls
1068 lines (831 loc) · 34.3 KB
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Tests for trade commands — buy, sell, claim-cashback, close-volume-acc, migrate."""
import json
from unittest.mock import AsyncMock, patch
from typer.testing import CliRunner
from pumpfun_cli.cli import app
runner = CliRunner()
# A valid base58 Solana pubkey for use in tests that need to pass mint validation.
_FAKE_MINT = "11111111111111111111111111111111"
def test_buy_no_rpc_exits_1():
result = runner.invoke(app, ["buy", _FAKE_MINT, "0.1"])
assert result.exit_code == 1
assert "RPC" in result.output or "rpc" in result.output or "Password" in result.output
def test_sell_no_rpc_exits_1():
result = runner.invoke(app, ["sell", _FAKE_MINT, "all"])
assert result.exit_code == 1
def test_claim_cashback_no_rpc_exits_1():
result = runner.invoke(app, ["claim-cashback"])
assert result.exit_code == 1
def test_close_volume_acc_no_rpc_exits_1():
result = runner.invoke(app, ["close-volume-acc"])
assert result.exit_code == 1
def test_collect_creator_fee_no_rpc_exits_1():
result = runner.invoke(app, ["collect-creator-fee"])
assert result.exit_code != 0
def test_migrate_no_rpc_exits_1():
result = runner.invoke(app, ["migrate", _FAKE_MINT])
assert result.exit_code == 1
def _strip_ansi(text: str) -> str:
import re
return re.sub(r"\x1b\[[0-9;]*m", "", text)
def test_buy_with_force_amm_flag_help():
result = runner.invoke(app, ["buy", "--help"])
assert "--force-amm" in _strip_ansi(result.output)
def test_sell_with_force_amm_flag_help():
result = runner.invoke(app, ["sell", "--help"])
assert "--force-amm" in _strip_ansi(result.output)
def test_buy_graduated_fallback_json(tmp_path, monkeypatch):
"""Buy auto-falls back to PumpSwap when bonding curve says graduated."""
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path))
monkeypatch.setenv("PUMPFUN_PASSWORD", "testpass")
# Create a wallet
from solders.keypair import Keypair
from pumpfun_cli.crypto import encrypt_keypair
config_dir = tmp_path / "pumpfun-cli"
config_dir.mkdir()
encrypt_keypair(Keypair(), "testpass", config_dir / "wallet.enc")
with (
patch("pumpfun_cli.commands.trade.buy_token", new_callable=AsyncMock) as mock_buy,
patch("pumpfun_cli.commands.trade.buy_pumpswap", new_callable=AsyncMock) as mock_pumpswap,
):
mock_buy.return_value = {"error": "graduated", "message": "Token has graduated."}
mock_pumpswap.return_value = {
"action": "buy",
"venue": "pumpswap",
"mint": _FAKE_MINT,
"sol_spent": 0.01,
"tokens_received": 100.0,
"signature": "sig",
"explorer": "https://solscan.io/tx/sig",
}
result = runner.invoke(
app,
[
"--json",
"--rpc",
"http://rpc",
"buy",
_FAKE_MINT,
"0.01",
],
)
assert result.exit_code == 0
data = json.loads(result.output)
assert data["venue"] == "pumpswap"
def test_sell_error_json(tmp_path, monkeypatch):
"""Sell with error returns proper exit code."""
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path))
monkeypatch.setenv("PUMPFUN_PASSWORD", "testpass")
from solders.keypair import Keypair
from pumpfun_cli.crypto import encrypt_keypair
config_dir = tmp_path / "pumpfun-cli"
config_dir.mkdir()
encrypt_keypair(Keypair(), "testpass", config_dir / "wallet.enc")
with patch("pumpfun_cli.commands.trade.sell_token", new_callable=AsyncMock) as mock_sell:
mock_sell.return_value = {"error": "no_tokens", "message": "No tokens to sell."}
result = runner.invoke(
app,
[
"--rpc",
"http://rpc",
"sell",
_FAKE_MINT,
"all",
],
)
assert result.exit_code == 3
def test_claim_cashback_success_json(tmp_path, monkeypatch):
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path))
monkeypatch.setenv("PUMPFUN_PASSWORD", "testpass")
from solders.keypair import Keypair
from pumpfun_cli.crypto import encrypt_keypair
config_dir = tmp_path / "pumpfun-cli"
config_dir.mkdir()
encrypt_keypair(Keypair(), "testpass", config_dir / "wallet.enc")
with patch(
"pumpfun_cli.commands.trade.claim_cashback_core", new_callable=AsyncMock
) as mock_cb:
mock_cb.return_value = {
"action": "claim_cashback",
"signature": "cbsig",
"explorer": "https://solscan.io/tx/cbsig",
}
result = runner.invoke(
app,
[
"--json",
"--rpc",
"http://rpc",
"claim-cashback",
],
)
assert result.exit_code == 0
data = json.loads(result.output)
assert data["action"] == "claim_cashback"
def test_migrate_success_json(tmp_path, monkeypatch):
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path))
monkeypatch.setenv("PUMPFUN_PASSWORD", "testpass")
from solders.keypair import Keypair
from pumpfun_cli.crypto import encrypt_keypair
config_dir = tmp_path / "pumpfun-cli"
config_dir.mkdir()
encrypt_keypair(Keypair(), "testpass", config_dir / "wallet.enc")
with patch("pumpfun_cli.commands.trade.migrate_token", new_callable=AsyncMock) as mock_mig:
mock_mig.return_value = {
"action": "migrate",
"mint": _FAKE_MINT,
"signature": "migsig",
"explorer": "https://solscan.io/tx/migsig",
}
result = runner.invoke(
app,
[
"--json",
"--rpc",
"http://rpc",
"migrate",
_FAKE_MINT,
],
)
assert result.exit_code == 0
data = json.loads(result.output)
assert data["action"] == "migrate"
def test_sell_graduated_fallback_json(tmp_path, monkeypatch):
"""Sell auto-falls back to PumpSwap when bonding curve says graduated."""
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path))
monkeypatch.setenv("PUMPFUN_PASSWORD", "testpass")
from solders.keypair import Keypair
from pumpfun_cli.crypto import encrypt_keypair
config_dir = tmp_path / "pumpfun-cli"
config_dir.mkdir()
encrypt_keypair(Keypair(), "testpass", config_dir / "wallet.enc")
with (
patch("pumpfun_cli.commands.trade.sell_token", new_callable=AsyncMock) as mock_sell,
patch("pumpfun_cli.commands.trade.sell_pumpswap", new_callable=AsyncMock) as mock_pumpswap,
):
mock_sell.return_value = {"error": "graduated", "message": "Token has graduated."}
mock_pumpswap.return_value = {
"action": "sell",
"venue": "pumpswap",
"mint": _FAKE_MINT,
"sol_received": 0.01,
"tokens_sold": 100.0,
"signature": "sig",
"explorer": "https://solscan.io/tx/sig",
}
result = runner.invoke(
app,
[
"--json",
"--rpc",
"http://rpc",
"sell",
_FAKE_MINT,
"all",
],
)
assert result.exit_code == 0
data = json.loads(result.output)
assert data["venue"] == "pumpswap"
def test_buy_invalid_mint_format():
"""Buy with invalid base58 mint exits with error."""
result = runner.invoke(app, ["--rpc", "https://fake.rpc", "buy", "not-valid-base58!", "0.001"])
assert result.exit_code != 0
assert "Invalid mint address" in result.output
def test_sell_invalid_mint_format():
"""Sell with invalid base58 mint exits with error."""
result = runner.invoke(app, ["--rpc", "https://fake.rpc", "sell", "not-valid-base58!", "all"])
assert result.exit_code != 0
assert "Invalid mint address" in result.output
def test_buy_priority_fee_flag_forwarded(tmp_path, monkeypatch):
"""--priority-fee and --compute-units flags are forwarded to buy_token."""
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path))
monkeypatch.setenv("PUMPFUN_PASSWORD", "testpass")
from solders.keypair import Keypair
from pumpfun_cli.crypto import encrypt_keypair
config_dir = tmp_path / "pumpfun-cli"
config_dir.mkdir()
encrypt_keypair(Keypair(), "testpass", config_dir / "wallet.enc")
with patch("pumpfun_cli.commands.trade.buy_token", new_callable=AsyncMock) as mock_buy:
mock_buy.return_value = {
"action": "buy",
"mint": _FAKE_MINT,
"sol_spent": 0.01,
"tokens_received": 100.0,
"signature": "sig",
"explorer": "https://solscan.io/tx/sig",
}
result = runner.invoke(
app,
[
"--json",
"--rpc",
"http://rpc",
"--priority-fee",
"50000",
"--compute-units",
"200000",
"buy",
_FAKE_MINT,
"0.01",
],
)
assert result.exit_code == 0
call_kwargs = mock_buy.call_args
assert call_kwargs.kwargs.get("priority_fee") == 50000
assert call_kwargs.kwargs.get("compute_units") == 200000
def test_buy_dry_run_flag(tmp_path, monkeypatch):
"""--dry-run flag produces simulation output with dry_run: true."""
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path))
monkeypatch.setenv("PUMPFUN_PASSWORD", "testpass")
from solders.keypair import Keypair
from pumpfun_cli.crypto import encrypt_keypair
config_dir = tmp_path / "pumpfun-cli"
config_dir.mkdir()
encrypt_keypair(Keypair(), "testpass", config_dir / "wallet.enc")
with patch("pumpfun_cli.commands.trade.buy_token", new_callable=AsyncMock) as mock_buy:
mock_buy.return_value = {
"dry_run": True,
"action": "buy",
"venue": "bonding_curve",
"mint": _FAKE_MINT,
"sol_in": 0.01,
"expected_tokens": 1234.56,
"effective_price_sol": 0.0000081,
"spot_price_sol": 0.000008,
"price_impact_pct": 1.25,
"min_tokens_out": 1049.87,
"slippage_pct": 15,
}
result = runner.invoke(
app,
["--json", "--rpc", "http://rpc", "buy", "--dry-run", _FAKE_MINT, "0.01"],
)
assert result.exit_code == 0
data = json.loads(result.output)
assert data["dry_run"] is True
assert data["action"] == "buy"
assert "signature" not in data
call_kwargs = mock_buy.call_args
assert call_kwargs.kwargs.get("dry_run") is True
def test_sell_dry_run_flag(tmp_path, monkeypatch):
"""--dry-run flag on sell produces simulation output."""
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path))
monkeypatch.setenv("PUMPFUN_PASSWORD", "testpass")
from solders.keypair import Keypair
from pumpfun_cli.crypto import encrypt_keypair
config_dir = tmp_path / "pumpfun-cli"
config_dir.mkdir()
encrypt_keypair(Keypair(), "testpass", config_dir / "wallet.enc")
with patch("pumpfun_cli.commands.trade.sell_token", new_callable=AsyncMock) as mock_sell:
mock_sell.return_value = {
"dry_run": True,
"action": "sell",
"venue": "bonding_curve",
"mint": _FAKE_MINT,
"tokens_in": 1000.0,
"expected_sol": 0.082,
"effective_price_sol": 0.000082,
"spot_price_sol": 0.000083,
"price_impact_pct": -1.2,
"min_sol_out": 0.0697,
"slippage_pct": 15,
}
result = runner.invoke(
app,
["--json", "--rpc", "http://rpc", "sell", "--dry-run", _FAKE_MINT, "1000"],
)
assert result.exit_code == 0
data = json.loads(result.output)
assert data["dry_run"] is True
assert data["action"] == "sell"
def test_buy_insufficient_balance_json(tmp_path, monkeypatch):
"""CLI-level test: core returns insufficient_balance error, verify exit code 1 and message."""
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path))
monkeypatch.setenv("PUMPFUN_PASSWORD", "testpass")
from solders.keypair import Keypair
from pumpfun_cli.crypto import encrypt_keypair
config_dir = tmp_path / "pumpfun-cli"
config_dir.mkdir()
encrypt_keypair(Keypair(), "testpass", config_dir / "wallet.enc")
with patch("pumpfun_cli.commands.trade.buy_token", new_callable=AsyncMock) as mock_buy:
mock_buy.return_value = {
"error": "insufficient_balance",
"message": "Insufficient SOL balance.",
"available_sol": 0.000001,
"required_sol": 0.01293,
}
result = runner.invoke(
app,
["--rpc", "http://rpc", "buy", _FAKE_MINT, "0.01"],
)
assert result.exit_code == 1
assert "Insufficient" in result.output
def test_sell_insufficient_balance_json(tmp_path, monkeypatch):
"""CLI-level test: core returns insufficient_balance error for sell."""
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path))
monkeypatch.setenv("PUMPFUN_PASSWORD", "testpass")
from solders.keypair import Keypair
from pumpfun_cli.crypto import encrypt_keypair
config_dir = tmp_path / "pumpfun-cli"
config_dir.mkdir()
encrypt_keypair(Keypair(), "testpass", config_dir / "wallet.enc")
with patch("pumpfun_cli.commands.trade.sell_token", new_callable=AsyncMock) as mock_sell:
mock_sell.return_value = {
"error": "insufficient_balance",
"message": "Insufficient token balance.",
"available_tokens": 0.5,
"required_tokens": 1000.0,
}
result = runner.invoke(
app,
["--rpc", "http://rpc", "sell", _FAKE_MINT, "1000"],
)
assert result.exit_code == 1
assert "Insufficient" in result.output
def test_buy_slippage_negative():
"""Buy with negative slippage exits with error."""
result = runner.invoke(
app, ["--rpc", "https://fake.rpc", "buy", "--slippage", "-5", _FAKE_MINT, "0.01"]
)
assert result.exit_code != 0
assert "Slippage must be between 0 and 100" in result.output
def test_buy_slippage_above_100():
"""Buy with slippage above 100 exits with error."""
result = runner.invoke(
app, ["--rpc", "https://fake.rpc", "buy", "--slippage", "999", _FAKE_MINT, "0.01"]
)
assert result.exit_code != 0
assert "Slippage must be between 0 and 100" in result.output
def test_sell_slippage_negative():
"""Sell with negative slippage exits with error."""
result = runner.invoke(
app, ["--rpc", "https://fake.rpc", "sell", "--slippage", "-5", _FAKE_MINT, "all"]
)
assert result.exit_code != 0
assert "Slippage must be between 0 and 100" in result.output
def test_sell_slippage_above_100():
"""Sell with slippage above 100 exits with error."""
result = runner.invoke(
app, ["--rpc", "https://fake.rpc", "sell", "--slippage", "999", _FAKE_MINT, "all"]
)
assert result.exit_code != 0
assert "Slippage must be between 0 and 100" in result.output
def test_buy_slippage_zero(tmp_path, monkeypatch):
"""Buy with slippage=0 is valid (boundary)."""
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path))
monkeypatch.setenv("PUMPFUN_PASSWORD", "testpass")
from solders.keypair import Keypair
from pumpfun_cli.crypto import encrypt_keypair
config_dir = tmp_path / "pumpfun-cli"
config_dir.mkdir()
encrypt_keypair(Keypair(), "testpass", config_dir / "wallet.enc")
with patch("pumpfun_cli.commands.trade.buy_token", new_callable=AsyncMock) as mock_buy:
mock_buy.return_value = {
"action": "buy",
"mint": _FAKE_MINT,
"sol_spent": 0.01,
"tokens_received": 100.0,
"signature": "sig",
"explorer": "https://solscan.io/tx/sig",
}
result = runner.invoke(
app,
["--json", "--rpc", "http://rpc", "buy", "--slippage", "0", _FAKE_MINT, "0.01"],
)
assert result.exit_code == 0
assert "Slippage must be between" not in result.output
def test_buy_slippage_100(tmp_path, monkeypatch):
"""Buy with slippage=100 is valid (boundary)."""
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path))
monkeypatch.setenv("PUMPFUN_PASSWORD", "testpass")
from solders.keypair import Keypair
from pumpfun_cli.crypto import encrypt_keypair
config_dir = tmp_path / "pumpfun-cli"
config_dir.mkdir()
encrypt_keypair(Keypair(), "testpass", config_dir / "wallet.enc")
with patch("pumpfun_cli.commands.trade.buy_token", new_callable=AsyncMock) as mock_buy:
mock_buy.return_value = {
"action": "buy",
"mint": _FAKE_MINT,
"sol_spent": 0.01,
"tokens_received": 100.0,
"signature": "sig",
"explorer": "https://solscan.io/tx/sig",
}
result = runner.invoke(
app,
["--json", "--rpc", "http://rpc", "buy", "--slippage", "100", _FAKE_MINT, "0.01"],
)
assert result.exit_code == 0
assert "Slippage must be between" not in result.output
def test_sell_slippage_zero(tmp_path, monkeypatch):
"""Sell with slippage=0 is valid (boundary)."""
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path))
monkeypatch.setenv("PUMPFUN_PASSWORD", "testpass")
from solders.keypair import Keypair
from pumpfun_cli.crypto import encrypt_keypair
config_dir = tmp_path / "pumpfun-cli"
config_dir.mkdir()
encrypt_keypair(Keypair(), "testpass", config_dir / "wallet.enc")
with patch("pumpfun_cli.commands.trade.sell_token", new_callable=AsyncMock) as mock_sell:
mock_sell.return_value = {
"action": "sell",
"mint": _FAKE_MINT,
"sol_received": 0.01,
"tokens_sold": 100.0,
"signature": "sig",
"explorer": "https://solscan.io/tx/sig",
}
result = runner.invoke(
app,
["--json", "--rpc", "http://rpc", "sell", "--slippage", "0", _FAKE_MINT, "all"],
)
assert result.exit_code == 0
assert "Slippage must be between" not in result.output
def test_sell_slippage_100(tmp_path, monkeypatch):
"""Sell with slippage=100 is valid (boundary)."""
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path))
monkeypatch.setenv("PUMPFUN_PASSWORD", "testpass")
from solders.keypair import Keypair
from pumpfun_cli.crypto import encrypt_keypair
config_dir = tmp_path / "pumpfun-cli"
config_dir.mkdir()
encrypt_keypair(Keypair(), "testpass", config_dir / "wallet.enc")
with patch("pumpfun_cli.commands.trade.sell_token", new_callable=AsyncMock) as mock_sell:
mock_sell.return_value = {
"action": "sell",
"mint": _FAKE_MINT,
"sol_received": 0.01,
"tokens_sold": 100.0,
"signature": "sig",
"explorer": "https://solscan.io/tx/sig",
}
result = runner.invoke(
app,
["--json", "--rpc", "http://rpc", "sell", "--slippage", "100", _FAKE_MINT, "all"],
)
assert result.exit_code == 0
assert "Slippage must be between" not in result.output
# --- slippage / tx_error exit code tests ---
def test_buy_slippage_error_exit_code_3(tmp_path, monkeypatch):
"""Core returning slippage error results in exit code 3."""
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path))
monkeypatch.setenv("PUMPFUN_PASSWORD", "testpass")
from solders.keypair import Keypair
from pumpfun_cli.crypto import encrypt_keypair
config_dir = tmp_path / "pumpfun-cli"
config_dir.mkdir()
encrypt_keypair(Keypair(), "testpass", config_dir / "wallet.enc")
with patch("pumpfun_cli.commands.trade.buy_token", new_callable=AsyncMock) as mock_buy:
mock_buy.return_value = {
"error": "slippage",
"message": "Transaction failed: slippage tolerance exceeded.",
"error_code": 6002,
}
result = runner.invoke(
app,
["--rpc", "http://rpc", "buy", _FAKE_MINT, "0.01"],
)
assert result.exit_code == 3
assert "slippage" in result.output.lower()
def test_sell_slippage_error_exit_code_3(tmp_path, monkeypatch):
"""Core returning slippage error on sell results in exit code 3."""
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path))
monkeypatch.setenv("PUMPFUN_PASSWORD", "testpass")
from solders.keypair import Keypair
from pumpfun_cli.crypto import encrypt_keypair
config_dir = tmp_path / "pumpfun-cli"
config_dir.mkdir()
encrypt_keypair(Keypair(), "testpass", config_dir / "wallet.enc")
with patch("pumpfun_cli.commands.trade.sell_token", new_callable=AsyncMock) as mock_sell:
mock_sell.return_value = {
"error": "slippage",
"message": "Transaction failed: slippage tolerance exceeded.",
"error_code": 6003,
}
result = runner.invoke(
app,
["--rpc", "http://rpc", "sell", _FAKE_MINT, "all"],
)
assert result.exit_code == 3
assert "slippage" in result.output.lower()
def test_buy_tx_error_exit_code_1(tmp_path, monkeypatch):
"""Core returning tx_error results in exit code 1."""
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path))
monkeypatch.setenv("PUMPFUN_PASSWORD", "testpass")
from solders.keypair import Keypair
from pumpfun_cli.crypto import encrypt_keypair
config_dir = tmp_path / "pumpfun-cli"
config_dir.mkdir()
encrypt_keypair(Keypair(), "testpass", config_dir / "wallet.enc")
with patch("pumpfun_cli.commands.trade.buy_token", new_callable=AsyncMock) as mock_buy:
mock_buy.return_value = {
"error": "tx_error",
"message": "Transaction failed on-chain: error 6020",
"error_code": 6020,
}
result = runner.invoke(
app,
["--rpc", "http://rpc", "buy", _FAKE_MINT, "0.01"],
)
assert result.exit_code == 1
def test_buy_json_output_has_expected_keys(tmp_path, monkeypatch):
"""Verify JSON buy output has all expected keys."""
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path))
monkeypatch.setenv("PUMPFUN_PASSWORD", "testpass")
from solders.keypair import Keypair
from pumpfun_cli.crypto import encrypt_keypair
config_dir = tmp_path / "pumpfun-cli"
config_dir.mkdir()
encrypt_keypair(Keypair(), "testpass", config_dir / "wallet.enc")
with (
patch("pumpfun_cli.commands.trade.buy_token", new_callable=AsyncMock) as mock_buy,
patch("pumpfun_cli.commands.trade.buy_pumpswap", new_callable=AsyncMock) as mock_pumpswap,
):
mock_buy.return_value = {"error": "graduated", "message": "Token has graduated."}
mock_pumpswap.return_value = {
"action": "buy",
"venue": "pumpswap",
"mint": _FAKE_MINT,
"sol_spent": 0.01,
"tokens_received": 100.0,
"signature": "sig",
"explorer": "https://solscan.io/tx/sig",
}
result = runner.invoke(
app,
[
"--json",
"--rpc",
"http://rpc",
"buy",
_FAKE_MINT,
"0.01",
],
)
assert result.exit_code == 0
data = json.loads(result.output)
expected_keys = {
"action",
"venue",
"mint",
"sol_spent",
"tokens_received",
"signature",
"explorer",
}
assert expected_keys.issubset(data.keys())
# --- auto-routing command-layer tests ---
def _setup_wallet(tmp_path, monkeypatch):
"""Create a wallet and set env vars for command-layer tests."""
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path))
monkeypatch.setenv("PUMPFUN_PASSWORD", "testpass")
from solders.keypair import Keypair
from pumpfun_cli.crypto import encrypt_keypair
config_dir = tmp_path / "pumpfun-cli"
config_dir.mkdir()
encrypt_keypair(Keypair(), "testpass", config_dir / "wallet.enc")
def test_buy_graduated_fallback_forwards_slippage(tmp_path, monkeypatch):
"""--slippage 5 forwarded to buy_pumpswap on graduated fallback."""
_setup_wallet(tmp_path, monkeypatch)
with (
patch("pumpfun_cli.commands.trade.buy_token", new_callable=AsyncMock) as mock_buy,
patch("pumpfun_cli.commands.trade.buy_pumpswap", new_callable=AsyncMock) as mock_pumpswap,
):
mock_buy.return_value = {"error": "graduated", "message": "Token has graduated."}
mock_pumpswap.return_value = {
"action": "buy",
"venue": "pumpswap",
"mint": _FAKE_MINT,
"sol_spent": 0.01,
"tokens_received": 100.0,
"signature": "sig",
"explorer": "https://solscan.io/tx/sig",
}
result = runner.invoke(
app,
["--json", "--rpc", "http://rpc", "buy", "--slippage", "5", _FAKE_MINT, "0.01"],
)
assert result.exit_code == 0
call_args = mock_pumpswap.call_args
assert call_args[0][5] == 5 or call_args.kwargs.get("slippage") == 5
def test_sell_graduated_fallback_forwards_slippage(tmp_path, monkeypatch):
"""--slippage 5 forwarded to sell_pumpswap on graduated fallback."""
_setup_wallet(tmp_path, monkeypatch)
with (
patch("pumpfun_cli.commands.trade.sell_token", new_callable=AsyncMock) as mock_sell,
patch("pumpfun_cli.commands.trade.sell_pumpswap", new_callable=AsyncMock) as mock_pumpswap,
):
mock_sell.return_value = {"error": "graduated", "message": "Token has graduated."}
mock_pumpswap.return_value = {
"action": "sell",
"venue": "pumpswap",
"mint": _FAKE_MINT,
"sol_received": 0.01,
"tokens_sold": 100.0,
"signature": "sig",
"explorer": "https://solscan.io/tx/sig",
}
result = runner.invoke(
app,
["--json", "--rpc", "http://rpc", "sell", "--slippage", "5", _FAKE_MINT, "all"],
)
assert result.exit_code == 0
call_args = mock_pumpswap.call_args
assert call_args[0][5] == 5 or call_args.kwargs.get("slippage") == 5
def test_buy_graduated_fallback_forwards_priority_fee(tmp_path, monkeypatch):
"""--priority-fee + --compute-units forwarded to buy_pumpswap on graduated fallback."""
_setup_wallet(tmp_path, monkeypatch)
with (
patch("pumpfun_cli.commands.trade.buy_token", new_callable=AsyncMock) as mock_buy,
patch("pumpfun_cli.commands.trade.buy_pumpswap", new_callable=AsyncMock) as mock_pumpswap,
):
mock_buy.return_value = {"error": "graduated", "message": "Token has graduated."}
mock_pumpswap.return_value = {
"action": "buy",
"venue": "pumpswap",
"mint": _FAKE_MINT,
"sol_spent": 0.01,
"tokens_received": 100.0,
"signature": "sig",
"explorer": "https://solscan.io/tx/sig",
}
result = runner.invoke(
app,
[
"--json",
"--rpc",
"http://rpc",
"--priority-fee",
"55000",
"--compute-units",
"350000",
"buy",
_FAKE_MINT,
"0.01",
],
)
assert result.exit_code == 0
call_kwargs = mock_pumpswap.call_args.kwargs
assert call_kwargs.get("priority_fee") == 55000
assert call_kwargs.get("compute_units") == 350000
def test_buy_graduated_fallback_forwards_dry_run(tmp_path, monkeypatch):
"""--dry-run forwarded to buy_pumpswap on graduated fallback."""
_setup_wallet(tmp_path, monkeypatch)
with (
patch("pumpfun_cli.commands.trade.buy_token", new_callable=AsyncMock) as mock_buy,
patch("pumpfun_cli.commands.trade.buy_pumpswap", new_callable=AsyncMock) as mock_pumpswap,
):
mock_buy.return_value = {"error": "graduated", "message": "Token has graduated."}
mock_pumpswap.return_value = {
"dry_run": True,
"action": "buy",
"venue": "pumpswap",
"mint": _FAKE_MINT,
"sol_in": 0.01,
"expected_tokens": 100.0,
"effective_price_sol": 0.0001,
"spot_price_sol": 0.00009,
"price_impact_pct": 1.0,
"min_tokens_out": 95.0,
"slippage_pct": 15,
}
result = runner.invoke(
app,
["--json", "--rpc", "http://rpc", "buy", "--dry-run", _FAKE_MINT, "0.01"],
)
assert result.exit_code == 0
data = json.loads(result.output)
assert data["dry_run"] is True
assert data["venue"] == "pumpswap"
call_kwargs = mock_pumpswap.call_args
assert call_kwargs.kwargs.get("dry_run") is True or call_kwargs[1].get("dry_run") is True
def test_sell_graduated_fallback_forwards_dry_run(tmp_path, monkeypatch):
"""--dry-run forwarded to sell_pumpswap on graduated fallback."""
_setup_wallet(tmp_path, monkeypatch)
with (
patch("pumpfun_cli.commands.trade.sell_token", new_callable=AsyncMock) as mock_sell,
patch("pumpfun_cli.commands.trade.sell_pumpswap", new_callable=AsyncMock) as mock_pumpswap,
):
mock_sell.return_value = {"error": "graduated", "message": "Token has graduated."}
mock_pumpswap.return_value = {
"dry_run": True,
"action": "sell",
"venue": "pumpswap",
"mint": _FAKE_MINT,
"tokens_in": 100.0,
"expected_sol": 0.01,
"effective_price_sol": 0.0001,
"spot_price_sol": 0.00009,
"price_impact_pct": -1.0,
"min_sol_out": 0.0085,
"slippage_pct": 15,
}
result = runner.invoke(
app,
["--json", "--rpc", "http://rpc", "sell", "--dry-run", _FAKE_MINT, "100"],
)
assert result.exit_code == 0
data = json.loads(result.output)
assert data["dry_run"] is True
assert data["venue"] == "pumpswap"
def test_buy_graduated_fallback_forwards_confirm(tmp_path, monkeypatch):
"""--confirm forwarded to buy_pumpswap on graduated fallback."""
_setup_wallet(tmp_path, monkeypatch)
with (
patch("pumpfun_cli.commands.trade.buy_token", new_callable=AsyncMock) as mock_buy,
patch("pumpfun_cli.commands.trade.buy_pumpswap", new_callable=AsyncMock) as mock_pumpswap,
):
mock_buy.return_value = {"error": "graduated", "message": "Token has graduated."}
mock_pumpswap.return_value = {
"action": "buy",
"venue": "pumpswap",
"mint": _FAKE_MINT,
"sol_spent": 0.01,
"tokens_received": 100.0,
"signature": "sig",
"explorer": "https://solscan.io/tx/sig",
"confirmed": True,
}
result = runner.invoke(
app,
["--json", "--rpc", "http://rpc", "buy", "--confirm", _FAKE_MINT, "0.01"],
)
assert result.exit_code == 0
call_kwargs = mock_pumpswap.call_args
assert call_kwargs.kwargs.get("confirm") is True or call_kwargs[1].get("confirm") is True
def test_buy_not_found_no_pumpswap_fallback(tmp_path, monkeypatch):
"""not_found does NOT trigger pumpswap fallback."""
_setup_wallet(tmp_path, monkeypatch)
with (
patch("pumpfun_cli.commands.trade.buy_token", new_callable=AsyncMock) as mock_buy,
patch("pumpfun_cli.commands.trade.buy_pumpswap", new_callable=AsyncMock) as mock_pumpswap,
):
mock_buy.return_value = {"error": "not_found", "message": "No bonding curve found."}
result = runner.invoke(
app,
["--rpc", "http://rpc", "buy", _FAKE_MINT, "0.01"],
)
assert result.exit_code != 0
mock_pumpswap.assert_not_called()
def test_sell_not_found_no_pumpswap_fallback(tmp_path, monkeypatch):
"""not_found does NOT trigger pumpswap fallback for sell."""
_setup_wallet(tmp_path, monkeypatch)
with (