Skip to content

Commit

Permalink
Implement CPFP into fee rate calculation (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
bboerst authored Jun 14, 2024
1 parent c0db91a commit cdfede8
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions webapp/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,40 @@ def get_transaction_fee_rate(first_transaction):

# Check if the transaction result is already cached
if first_transaction in transaction_cache:
cached_result, expiration_time = transaction_cache[first_transaction]
cached_result, cpfp_result, expiration_time = transaction_cache[first_transaction]
if time.time() < expiration_time:
return cached_result
return cpfp_result if cpfp_result is not None else cached_result

fee_rate = None
cpfp_fee_rate = None

try:
# Check if the transaction is a CPFP transaction
cpfp_response = requests.get(f'https://mempool.space/api/v1/cpfp/{first_transaction}')
logger.info(f"mempool.space API - CPFP tx response: {cpfp_response.status_code} - {first_transaction}")
if cpfp_response.status_code == 200:
cpfp_data = cpfp_response.json()
if 'effectiveFeePerVsize' in cpfp_data:
cpfp_fee_rate = round(cpfp_data['effectiveFeePerVsize'])
# Cache the CPFP result for 5 minutes
expiration_time = time.time() + 300 # 5 minutes in seconds
transaction_cache[first_transaction] = (None, cpfp_fee_rate, expiration_time)
return cpfp_fee_rate

# If not a CPFP transaction, calculate normal fee rate
response = requests.get(f'https://mempool.space/api/tx/{first_transaction}')
logger.info(f"mempool.space API - Reg tx response: {response.status_code} - {first_transaction}")
if response.status_code == 200:
data = response.json()
fee = data.get('fee')
weight = data.get('weight')
if fee is not None and weight is not None:
fee_rate = round(fee / (weight / 4))
# Cache the result for 5 minutes
# Cache the normal result for 5 minutes
expiration_time = time.time() + 300 # 5 minutes in seconds
transaction_cache[first_transaction] = (fee_rate, expiration_time)
transaction_cache[first_transaction] = (fee_rate, None, expiration_time)
return fee_rate

return 'not found'
except requests.exceptions.RequestException as e:
logger.exception(f"Error fetching transaction fee rate for {first_transaction}")
Expand Down

0 comments on commit cdfede8

Please sign in to comment.