Skip to content

Commit

Permalink
Index all memos (v1 and v2) and use cache in index_payment_router (#8400
Browse files Browse the repository at this point in the history
)
  • Loading branch information
rickyrombo committed May 8, 2024
1 parent 94b4f5a commit a144f6a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
3 changes: 3 additions & 0 deletions packages/discovery-provider/src/solana/solana_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,8 @@ def get_derived_address(base, hashed_eth_pk, spl_token_id):
# Static Memo Program ID
MEMO_PROGRAM_ID = "Memo1UhkJRfHyvLMcVucJwxXeuD728EqVDDwQDxFMNo"

# Static Memo Program ID
MEMO_V2_PROGRAM_ID = "MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"

# Static Jupiter Swap Program ID
JUPITER_PROGRAM_ID = "JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4"
12 changes: 10 additions & 2 deletions packages/discovery-provider/src/tasks/index_payment_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
has_log,
)
from src.utils.prometheus_metric import save_duration_metric
from src.utils.redis_cache import get_solana_transaction_key
from src.utils.redis_constants import (
latest_sol_payment_router_db_tx_key,
latest_sol_payment_router_program_tx_key,
Expand Down Expand Up @@ -156,10 +157,15 @@ def check_config():


def get_sol_tx_info(
solana_client_manager: SolanaClientManager,
tx_sig: str,
solana_client_manager: SolanaClientManager, tx_sig: str, redis: Redis
):
try:
existing_tx = redis.get(get_solana_transaction_key(tx_sig))
if existing_tx is not None and existing_tx != "":
logger.info(f"index_payment_router.py | Cache hit: {tx_sig}")
tx_info = GetTransactionResp.from_json(existing_tx.decode("utf-8"))
return (tx_info, tx_sig)
logger.info(f"index_payment_router.py | Cache miss: {tx_sig}")
tx_info = solana_client_manager.get_sol_tx_info(tx_sig)
return (tx_info, tx_sig)
except SolanaTransactionFetchError:
Expand Down Expand Up @@ -679,6 +685,7 @@ def process_route_instruction(
f"index_payment_router.py | tx: {tx_sig} | $AUDIO payment router transactions are not yet indexed. Skipping instruction indexing."
)
elif is_usdc:
logger.debug(f"index_payment_router.py | Parsing memos: {memos}")
memo = parse_route_transaction_memo(
session=session, memos=memos, timestamp=timestamp
)
Expand Down Expand Up @@ -874,6 +881,7 @@ def process_payment_router_txs() -> None:
get_sol_tx_info,
solana_client_manager,
str(tx_sig),
redis
): tx_sig
for tx_sig in tx_sig_batch
}
Expand Down
38 changes: 25 additions & 13 deletions packages/discovery-provider/src/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from web3 import Web3

from src import exceptions
from src.solana.solana_helpers import MEMO_PROGRAM_ID
from src.solana.solana_helpers import MEMO_PROGRAM_ID, MEMO_V2_PROGRAM_ID

from . import multihash

Expand Down Expand Up @@ -500,20 +500,32 @@ def get_solana_tx_token_balance_changes(
return balance_changes


def decode_all_solana_memos(tx_message: Message):
"""Finds all memo instructions in a transaction and base58 decodes their instruction data as a string"""
def get_memo_program_index(tx_message: Message):
try:
memo_program_index = tx_message.account_keys.index(
Pubkey.from_string(MEMO_PROGRAM_ID)
)
return [
base58.b58decode(instruction.data).decode("utf8")
for instruction in tx_message.instructions
if instruction.program_id_index == memo_program_index
]
except:
return tx_message.account_keys.index(Pubkey.from_string(MEMO_PROGRAM_ID))
except ValueError:
# Do nothing, there's no memos
return None


def get_memo_v2_program_index(tx_message: Message):
try:
return tx_message.account_keys.index(Pubkey.from_string(MEMO_V2_PROGRAM_ID))
except ValueError:
# Do nothing, there's no memos
return []
return None


def decode_all_solana_memos(tx_message: Message):
"""Finds all memo instructions in a transaction and base58 decodes their instruction data as a string"""
memo_program_index = get_memo_program_index(tx_message)
memo_v2_program_index = get_memo_v2_program_index(tx_message)
return [
base58.b58decode(instruction.data).decode("utf8")
for instruction in tx_message.instructions
if instruction.program_id_index == memo_program_index
or instruction.program_id_index == memo_v2_program_index
]


def get_account_owner_from_balance_change(
Expand Down

0 comments on commit a144f6a

Please sign in to comment.