-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecode_mintTxFee.py
64 lines (56 loc) · 2.12 KB
/
decode_mintTxFee.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import requests
import json
from dotenv import load_dotenv
import os
load_dotenv()
def get_bitcoin_price():
url = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd"
response = requests.get(url)
data = response.json()
price = data['bitcoin']['usd']
return price
def get_bitcoin_transactions(address, price):
# Request URL
url = f'https://mempool.space/api/address/{address}/txs'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0'
}
# Sending the request and loading data
res = requests.get(url, headers=headers)
data = json.loads(res.text)
# Processing each transaction
for item in data:
print("Transaction Details:")
# Transaction input details
vin_length = len(item["vin"])
for i in range(vin_length):
txid = item["vin"][0]['txid']
if item["vin"][i]["prevout"]["scriptpubkey_address"]==address:
in_value = item["vin"][i]["prevout"]["value"]
else:
continue
print(f"Input Transaction ID: {txid}")
print(f"Input Value: {in_value} satoshis")
# Transaction output details
out_length = len(item["vout"])
for i in range(out_length):
if item["vout"][i]["scriptpubkey_address"] == address:
print(f"Output Transaction Index: {i}")
out_value = item["vout"][i]["value"]
cost = in_value - out_value
print(f"Output Value: {out_value} satoshis")
else:
cost = in_value
# Convert Satoshis to Bitcoins
btc_amount = cost / 100000000
print(f"Cost value: {cost} satoshis")
print(f"Bitcoin Current Price: ${price}")
print(f"Transaction Fee Equals: ${price*btc_amount:.2f}")
print("\n")
def main():
# Example usage
my_address = txid = os.environ.get("my_address")
price = get_bitcoin_price()
get_bitcoin_transactions(my_address, price)
if __name__ == "__main__":
main()