summaryrefslogtreecommitdiffstats
path: root/accept.py
blob: 71aa1ca2b9aa9b8e821785a2f7c7444d47520a84 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import websocket
import time
import json
import threading
import pprint
import requests

INPUT_ADDR = '12fkW5EBb3uBy1zD8pan4TcbabP5Fjato7' # bitvend addr
#INPUT_ADDR = '1MZ6UbznUjoc34pkYyyofWJY42fAoA6k22' # test addr

def get_exchange_rate(currency='PLN'):
    return requests.get('https://blockchain.info/pl/ticker').json()[currency]['last']

def to_local_currency(sat):
    # Returns satoshi in local lowest denomination currency (grosze)
    rate = get_exchange_rate()
    return int(sat / 1000000.0 * rate)

def process_transaction(tx):
    tx_size = tx['x']['size']
    tx_hash = tx['x']['hash']
    tx_value = sum([
        o['value'] for o in tx['x']['out'] if o['addr'] == INPUT_ADDR
        ], 0)
    fee = sum([i['prev_out']['value'] for i in tx['x']['inputs']]) - \
        sum([o['value'] for o in tx['x']['out']])

    fee_byte = fee / tx_size

    print(tx_size, tx_hash, tx_value, fee, fee_byte)
    print(to_local_currency(tx_value))

def on_message(ws, message):
    #print message
    data = json.loads(message)
    if data['op'] == 'utx':
        process_transaction(data)
        pprint.pprint(data)
        for d in data['x']['out']:
            pprint.pprint(d)

def on_error(ws, error):
    print(error)

def on_close(ws):
    print("### closed ###")

def on_open(ws):
    print("### connected ###")
    ws.send(json.dumps({
        "op": "addr_sub",
        "addr": INPUT_ADDR
        }))
    ws.send(json.dumps({
        "op": "addr_sub",
        "addr": "1MZ6UbznUjoc34pkYyyofWJY42fAoA6k22"
        }))

    def run(*args):
        while True:
            time.sleep(20)
            ws.send(json.dumps({
                "op": "ping"
                }))

    threading.Thread(target=run, daemon=True).start()


if __name__ == "__main__":
    '''
    process_transaction({'op': 'utx',
 'x': {'hash': '03ed0015c29dfc3bfd3d8a215490d82e3562fe4e8bf5f2ffa737ac8fdc850cc4',
       'inputs': [{'prev_out': {'addr': '1infuHrvt7tuGY8nJcfTwB3wwAR1XwUcW',
                                'n': 0,
                                'script': '76a91407e72dad6fc3a60ae5c4973bf2a23750713870b188ac',
                                'spent': False,
                                'tx_index': 93845150,
                                'type': 0,
                                'value': 300000},
                   'script': '483045022100980b532c7b417b6a7ce4ef1b5e509dda61762ccbbfd35189c2ed7985e6a35d1c02205afacbda7b0e10c6b41100ecee5fc05ab47ca00c85b842923324d55c0db529f8014104e2a76bdeaa387cae1cf920a9a8b54ee4c5e7378ea1985a638f2f3ec609a1d6a54e49e85d10bec55ce9321c5a45e2b21ca8eb1a3e18635405c4812d8467339e9c',
                   'sequence': 4294967295},
                  {'prev_out': {'addr': '1infuHrvt7tuGY8nJcfTwB3wwAR1XwUcW',
                                'n': 0,
                                'script': '76a91407e72dad6fc3a60ae5c4973bf2a23750713870b188ac',
                                'spent': False,
                                'tx_index': 182282259,
                                'type': 0,
                                'value': 2978800},
                   'script': '483045022100a560606252d83635c784139ae271db498e6528d561105d72c892dc80369036d3022048bfe431f3055d115f8faf0bf2b582912fa226a2f871b44f8bef348a278c8cea014104e2a76bdeaa387cae1cf920a9a8b54ee4c5e7378ea1985a638f2f3ec609a1d6a54e49e85d10bec55ce9321c5a45e2b21ca8eb1a3e18635405c4812d8467339e9c',
                   'sequence': 4294967295},
                  {'prev_out': {'addr': '17qC9bmMCyaReEPRWksYt99tr8s8YWLrDK',
                                'n': 0,
                                'script': '76a9144aee06e79fe2f92c3916b0cc0a78478e2434680e88ac',
                                'spent': False,
                                'tx_index': 93651246,
                                'type': 0,
                                'value': 407500},
                   'script': '47304402207b0946f973566ec65f4fb3878285274410e10b758806d8b46407d5219ffb36a1022017fdde92072e94bde68271754000636aa94952b91c2d818ed6d88a08cbf900fb0121029f8261faed04d668a78eadf7a3ef845de409e500dace54e604c547702a67b4fc',
                   'sequence': 4294967295}],
       'lock_time': 0,
       'out': [{'addr': '1MZ6UbznUjoc34pkYyyofWJY42fAoA6k22',
                'n': 0,
                'script': '76a914e17465c3ef40d44508c7e923140d54c3cd8ced3d88ac',
                'spent': True,
                'tx_index': 209616370,
                'type': 0,
                'value': 1251574},
               {'addr': '1infuHrvt7tuGY8nJcfTwB3wwAR1XwUcW',
                'n': 1,
                'script': '76a91407e72dad6fc3a60ae5c4973bf2a23750713870b188ac',
                'spent': True,
                'tx_index': 209616370,
                'type': 0,
                'value': 2424726}],
       'relayed_by': '5.189.53.123',
       'size': 585,
       'time': 1484319619,
       'tx_index': 209616370,
       'ver': 1,
       'vin_sz': 3,
       'vout_sz': 2}})
    exit(0)
    '''
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp("wss://ws.blockchain.info/inv",
                              on_message = on_message,
                              on_error = on_error,
                              on_close = on_close)
    ws.on_open = on_open
    print("### running... ###")
    ws.run_forever()