Skip to content

Instantly share code, notes, and snippets.

@imartemy1524
Created August 21, 2023 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imartemy1524/d725b82a1efa10b73e57a93a78c2a273 to your computer and use it in GitHub Desktop.
Save imartemy1524/d725b82a1efa10b73e57a93a78c2a273 to your computer and use it in GitHub Desktop.
Count mempool spam transactions count
import time
import traceback
import requests
mempool_api_url = "https://mempool.space/api/mempool/txids"
print("Downlaoding data....\nIt can take some time - up to 1-2 minutes with slow internet connection")
# Fetch mempool transactions
response = requests.get(mempool_api_url)
mempool_transactions = response.json()
def split_into_blocks(array: list, block_size=10):
for i in range(0, len(array), block_size):
yield array[i:i + block_size]
def get_transactions_details(txid: list):
for ids in split_into_blocks(txid):
transaction_api_url = f"https://api.blockchair.com/bitcoin/dashboards/transactions/{','.join(ids)}"
for _ in range(3):
try:
time.sleep(4)
response = requests.get(transaction_api_url)
yield from [i['transaction'] for i in response.json()['data'].values()]
break
except:
traceback.print_exc()
continue
# Filter transactions with 1 input, 1 output, and fee < 1200 satoshi and fee more than a half of send amount
# filtered_transactions = []
print("Starting to count...")
c = 0
for i, tx in enumerate(get_transactions_details(mempool_transactions)):
# tx - {'block_id': -1, 'id': 2023082100302680, 'hash': '5ed54fd09a33b5ffc9fb38be229c6ed8d95fab2912d1319c31cf31a6aa6b0000', 'date': '2023-08-21', 'time': '2023-08-21 09:11:57', 'size': 299, 'weight': 545, 'version': 1, 'lock_time': 0, 'is_coinbase': False, 'has_witness': True, 'input_count': 1, 'output_count': 1, 'input_total': 1100, 'input_total_usd': 0.2881, 'output_total': 294, 'output_total_usd': 0.077, 'fee': 806, 'fee_usd': 0.2111, 'fee_per_kb': 2695, 'fee_per_kb_usd': 0.7058, 'fee_per_kwu': 1478, 'fee_per_kwu_usd': 0.3871, 'cdd_total': 2.64e-10, 'is_rbf': True}
if tx['input_count'] == 1 and tx['output_count'] == 1:
fee = tx['fee']
if fee < 1200 and tx['input_total'] < fee * 2:
# filtered_transactions.append(tx)
c += 1
print(
f"{i / len(mempool_transactions):.0%}: {i} / {len(mempool_transactions)}, found: {c}/{len(mempool_transactions)}",
end='\r')
print(f"Found: {c}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment