1515from privy .lib .authorization_signatures import get_authorization_signature
1616from cryptography .hazmat .primitives import serialization
1717from solders .keypair import Keypair # type: ignore
18+ from solders .pubkey import Pubkey # type: ignore
1819from solders .transaction import VersionedTransaction # type: ignore
1920from solders .message import to_bytes_versioned # type: ignore
2021
2728
2829logger = logging .getLogger (__name__ )
2930
31+ # Jupiter Referral Program ID
32+ JUPITER_REFERRAL_PROGRAM_ID = Pubkey .from_string (
33+ "REFER4ZgmyYx9c6He5XfaTMiGfdLwRnkV4RPp9t9iF3"
34+ )
35+
36+
37+ def _derive_referral_token_account (
38+ referral_account : str , token_mint : str
39+ ) -> str : # pragma: no cover
40+ """
41+ Derive the referral token account PDA for a specific token mint.
42+
43+ Jupiter Trigger requires a token-specific referral account, not the main
44+ referral account. This derives the correct PDA using the Jupiter Referral
45+ Program seeds: ["referral_ata", referral_account, mint]
46+
47+ Args:
48+ referral_account: The main Jupiter referral account pubkey
49+ token_mint: The token mint address to derive the account for
50+
51+ Returns:
52+ The derived referral token account pubkey as a string
53+ """
54+ referral_pubkey = Pubkey .from_string (referral_account )
55+ mint_pubkey = Pubkey .from_string (token_mint )
56+
57+ referral_token_account , _ = Pubkey .find_program_address (
58+ [b"referral_ata" , bytes (referral_pubkey ), bytes (mint_pubkey )],
59+ JUPITER_REFERRAL_PROGRAM_ID ,
60+ )
61+
62+ return str (referral_token_account )
63+
3064
3165def _convert_key_to_pkcs8_pem (key_string : str ) -> str : # pragma: no cover
3266 """Convert a private key to PKCS#8 PEM format for the Privy SDK."""
@@ -501,6 +535,16 @@ async def _create_order( # pragma: no cover
501535 payer_keypair = Keypair .from_base58_string (self ._payer_private_key )
502536 payer_pubkey = str (payer_keypair .pubkey ())
503537
538+ # Derive the referral token account for the output mint if referral is configured
539+ fee_account = None
540+ if self ._referral_account and output_mint :
541+ fee_account = _derive_referral_token_account (
542+ self ._referral_account , output_mint
543+ )
544+ logger .info (
545+ f"Derived referral token account for { output_mint } : { fee_account } "
546+ )
547+
504548 result = await trigger .create_order (
505549 input_mint = input_mint ,
506550 output_mint = output_mint ,
@@ -510,7 +554,7 @@ async def _create_order( # pragma: no cover
510554 payer = payer_pubkey ,
511555 expired_at = expired_at ,
512556 fee_bps = self ._referral_fee ,
513- fee_account = self . _referral_account ,
557+ fee_account = fee_account ,
514558 )
515559
516560 if not result .success :
0 commit comments