Skip to content

Commit b4840db

Browse files
committed
feat(bot): token2022 support in cleanup flow
1 parent 677f032 commit b4840db

File tree

4 files changed

+43
-11
lines changed

4 files changed

+43
-11
lines changed

src/cleanup/manager.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,19 @@ def __init__(
3434
self.use_priority_fee = use_priority_fee
3535
self.close_with_force_burn = force_burn
3636

37-
async def cleanup_ata(self, mint: Pubkey) -> None:
37+
async def cleanup_ata(self, mint: Pubkey, token_program_id: Pubkey | None = None) -> None:
3838
"""
3939
Attempt to burn any remaining tokens and close the ATA.
4040
Skips if account doesn't exist or is already empty/closed.
41+
42+
Args:
43+
mint: Token mint address
44+
token_program_id: Token program (TOKEN or TOKEN_2022). Defaults to TOKEN_2022_PROGRAM
4145
"""
42-
ata = self.wallet.get_associated_token_address(mint)
46+
if token_program_id is None:
47+
token_program_id = SystemAddresses.TOKEN_2022_PROGRAM
48+
49+
ata = self.wallet.get_associated_token_address(mint, token_program_id)
4350
solana_client = await self.client.get_client()
4451

4552
priority_fee = (
@@ -70,7 +77,7 @@ async def cleanup_ata(self, mint: Pubkey) -> None:
7077
mint=mint,
7178
owner=self.wallet.pubkey,
7279
amount=balance,
73-
program_id=SystemAddresses.TOKEN_PROGRAM,
80+
program_id=token_program_id,
7481
)
7582
)
7683
instructions.append(burn_ix)
@@ -89,7 +96,7 @@ async def cleanup_ata(self, mint: Pubkey) -> None:
8996
account=ata,
9097
dest=self.wallet.pubkey,
9198
owner=self.wallet.pubkey,
92-
program_id=SystemAddresses.TOKEN_PROGRAM,
99+
program_id=token_program_id,
93100
)
94101
)
95102
instructions.append(close_ix)

src/cleanup/modes.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ async def handle_cleanup_after_failure(
2020
client,
2121
wallet,
2222
mint,
23+
token_program_id,
2324
priority_fee_manager,
2425
cleanup_mode,
2526
cleanup_with_prior_fee,
@@ -30,13 +31,14 @@ async def handle_cleanup_after_failure(
3031
manager = AccountCleanupManager(
3132
client, wallet, priority_fee_manager, cleanup_with_prior_fee, force_burn
3233
)
33-
await manager.cleanup_ata(mint)
34+
await manager.cleanup_ata(mint, token_program_id)
3435

3536

3637
async def handle_cleanup_after_sell(
3738
client,
3839
wallet,
3940
mint,
41+
token_program_id,
4042
priority_fee_manager,
4143
cleanup_mode,
4244
cleanup_with_prior_fee,
@@ -47,13 +49,14 @@ async def handle_cleanup_after_sell(
4749
manager = AccountCleanupManager(
4850
client, wallet, priority_fee_manager, cleanup_with_prior_fee, force_burn
4951
)
50-
await manager.cleanup_ata(mint)
52+
await manager.cleanup_ata(mint, token_program_id)
5153

5254

5355
async def handle_cleanup_post_session(
5456
client,
5557
wallet,
5658
mints,
59+
token_program_ids,
5760
priority_fee_manager,
5861
cleanup_mode,
5962
cleanup_with_prior_fee,
@@ -64,5 +67,5 @@ async def handle_cleanup_post_session(
6467
manager = AccountCleanupManager(
6568
client, wallet, priority_fee_manager, cleanup_with_prior_fee, force_burn
6669
)
67-
for mint in mints:
68-
await manager.cleanup_ata(mint)
70+
for mint, token_program_id in zip(mints, token_program_ids):
71+
await manager.cleanup_ata(mint, token_program_id)

src/core/wallet.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from solders.pubkey import Pubkey
88
from spl.token.instructions import get_associated_token_address
99

10+
from core.pubkeys import SystemAddresses
11+
1012

1113
class Wallet:
1214
"""Manages a Solana wallet for trading operations."""
@@ -30,16 +32,21 @@ def keypair(self) -> Keypair:
3032
"""Get the keypair for signing transactions."""
3133
return self._keypair
3234

33-
def get_associated_token_address(self, mint: Pubkey) -> Pubkey:
35+
def get_associated_token_address(
36+
self, mint: Pubkey, token_program_id: Pubkey | None = None
37+
) -> Pubkey:
3438
"""Get the associated token account address for a mint.
3539
3640
Args:
3741
mint: Token mint address
42+
token_program_id: Token program (TOKEN or TOKEN_2022). Defaults to TOKEN_2022_PROGRAM
3843
3944
Returns:
4045
Associated token account address
4146
"""
42-
return get_associated_token_address(self.pubkey, mint)
47+
if token_program_id is None:
48+
token_program_id = SystemAddresses.TOKEN_2022_PROGRAM
49+
return get_associated_token_address(self.pubkey, mint, token_program_id)
4350

4451
@staticmethod
4552
def _load_keypair(private_key: str) -> Keypair:

src/trading/universal_trader.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ def __init__(
193193

194194
# State tracking
195195
self.traded_mints: set[Pubkey] = set()
196+
self.traded_token_programs: dict[str, Pubkey] = {} # Maps mint (as string) to token_program_id
196197
self.token_queue: asyncio.Queue = asyncio.Queue()
197198
self.processing: bool = False
198199
self.processed_tokens: set[str] = set()
@@ -325,10 +326,17 @@ async def _cleanup_resources(self) -> None:
325326
if self.traded_mints:
326327
try:
327328
logger.info(f"Cleaning up {len(self.traded_mints)} traded token(s)...")
329+
# Build parallel lists of mints and token_program_ids
330+
mints_list = list(self.traded_mints)
331+
token_program_ids = [
332+
self.traded_token_programs.get(str(mint))
333+
for mint in mints_list
334+
]
328335
await handle_cleanup_post_session(
329336
self.solana_client,
330337
self.wallet,
331-
list(self.traded_mints),
338+
mints_list,
339+
token_program_ids,
332340
self.priority_fee_manager,
333341
self.cleanup_mode,
334342
self.cleanup_with_priority_fee,
@@ -447,6 +455,10 @@ async def _handle_successful_buy(
447455
buy_result.tx_signature,
448456
)
449457
self.traded_mints.add(token_info.mint)
458+
# Track token program for cleanup
459+
mint_str = str(token_info.mint)
460+
if token_info.token_program_id:
461+
self.traded_token_programs[mint_str] = token_info.token_program_id
450462

451463
# Choose exit strategy
452464
if not self.marry_mode:
@@ -469,6 +481,7 @@ async def _handle_failed_buy(
469481
self.solana_client,
470482
self.wallet,
471483
token_info.mint,
484+
token_info.token_program_id,
472485
self.priority_fee_manager,
473486
self.cleanup_mode,
474487
self.cleanup_with_priority_fee,
@@ -521,6 +534,7 @@ async def _handle_time_based_exit(self, token_info: TokenInfo) -> None:
521534
self.solana_client,
522535
self.wallet,
523536
token_info.mint,
537+
token_info.token_program_id,
524538
self.priority_fee_manager,
525539
self.cleanup_mode,
526540
self.cleanup_with_priority_fee,
@@ -590,6 +604,7 @@ async def _monitor_position_until_exit(
590604
self.solana_client,
591605
self.wallet,
592606
token_info.mint,
607+
token_info.token_program_id,
593608
self.priority_fee_manager,
594609
self.cleanup_mode,
595610
self.cleanup_with_priority_fee,

0 commit comments

Comments
 (0)