https://gist.github.com/glozow/797bb412868ce959dcd0a2981322fd2a
I used the data from here and asked chatgtp to make me a script that will fetch current mempool data, calculate the necessary feerates, and determine the best transaction replacement strategy based on the provided example transactions and RBF rules. Adjust the transactions and rules as needed to fit specific Puzzle case.
import requests
def get_mempool_data():
try:
response = requests.get('https://mempool.space/api/v1/fees/recommended')
response.raise_for_status() # Raises HTTPError for bad responses (4xx and 5xx)
return response.json()
except requests.exceptions.HTTPError as http_err:
print(f"HTTP error occurred: {http_err}") # HTTP error
except requests.exceptions.RequestException as req_err:
print(f"Request error occurred: {req_err}") # Other request errors
except ValueError as json_err:
print(f"JSON decode error: {json_err}") # JSON decode error
return None
def get_feerates(mempool_data):
next_block_feerate = mempool_data['fastestFee']
bottom_mempool_feerate = mempool_data['minimumFee']
return next_block_feerate, bottom_mempool_feerate
def calculate_replacement_strategy(transactions, next_block_feerate, bottom_mempool_feerate):
def get_mineable_feerate(transaction, ancestors_fee, ancestors_size):
return (ancestors_fee + transaction['fee']) / (ancestors_size + transaction['size'])
results = []
for tx_name, tx in transactions.items():
ancestors_fee = sum(t['fee'] for n, t in transactions.items() if n < tx_name)
ancestors_size = sum(t['size'] for n, t in transactions.items() if n < tx_name)
mineable_feerate = get_mineable_feerate(tx, ancestors_fee, ancestors_size)
results.append((tx_name, mineable_feerate))
results.sort(key=lambda x: x[1], reverse=True)
return results
# Fetch and calculate
mempool_data = get_mempool_data()
if mempool_data is not None:
next_block_feerate, bottom_mempool_feerate = get_feerates(mempool_data)
# Example transactions
transactions = {
'E': {'fee': 1250000, 'size': 50000},
'F': {'fee': 5000, 'size': 200},
'B': {'fee': 16500, 'size': 250},
'C': {'fee': 67000, 'size': 1000},
}
replacement_strategy = calculate_replacement_strategy(transactions, next_block_feerate, bottom_mempool_feerate)
for tx_name, feerate in replacement_strategy:
print(f"Transaction {tx_name} with mineable feerate: {feerate:.2f} sat/vB")
else:
print("Failed to fetch mempool data.")
Transaction C with mineable feerate: 66.80 sat/vB
Transaction B with mineable feerate: 66.00 sat/vB
Transaction E with mineable feerate: 26.02 sat/vB
Transaction F with mineable feerate: 26.02 sat/vB