-
Notifications
You must be signed in to change notification settings - Fork 1
/
partial_volume.py
executable file
·132 lines (115 loc) · 4.08 KB
/
partial_volume.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
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
132
#!/usr/bin/env python3
import requests
import time
import platform
import os
import re
import json
import sys
from slickrpc import Proxy
import pdb
# define data dir
def def_data_dir():
operating_system = platform.system()
if operating_system == 'Darwin':
ac_dir = os.environ['HOME'] + '/Library/Application Support/Komodo'
elif operating_system == 'Linux':
ac_dir = os.environ['HOME'] + '/.komodo'
elif operating_system == 'Windows':
ac_dir = '%s/komodo/' % os.environ['APPDATA']
return(ac_dir)
# fucntion to define rpc_connection
def def_credentials(chain):
rpcport = '';
ac_dir = def_data_dir()
if chain == 'KMD':
coin_config_file = str(ac_dir + '/komodo.conf')
else:
coin_config_file = str(ac_dir + '/' + chain + '/' + chain + '.conf')
with open(coin_config_file, 'r') as f:
for line in f:
l = line.rstrip()
if re.search('rpcuser', l):
rpcuser = l.replace('rpcuser=', '')
elif re.search('rpcpassword', l):
rpcpassword = l.replace('rpcpassword=', '')
elif re.search('rpcport', l):
rpcport = l.replace('rpcport=', '')
if len(rpcport) == 0:
if chain == 'KMD':
rpcport = 7771
else:
print("rpcport not in conf file, exiting")
print("check " + coin_config_file)
exit(1)
return (Proxy("http://%s:%[email protected]:%d" % (rpcuser, rpcpassword, int(rpcport))))
# this should be used instead of all_p2sh if you don't need all historical swaps
def some_p2sh(rpc, timestamp):
getinfo = rpc.getinfo()
height = getinfo['blocks']
tip_time = getinfo['tiptime']
all_tx = []
while tip_time > timestamp:
block = rpc.getblock(str(height), 2)
all_tx += block['tx']
height -= 1
tip_time = block['time']
txids = []
for tx in all_tx:
for vout in tx['vout']:
if vout['scriptPubKey']['type'] == 'scripthash':
txids.append([tx['txid'], vout['n']])
return(txids)
# find scriptsig that should be mutual between alice and bob
def mutual_scripts(rpc, txids):
mutuals = {}
for txid in txids:
tx = rpc.getrawtransaction(txid[0], 1)
if 'spentTxId' in tx['vout'][txid[1]]:
spent_txid = tx['vout'][txid[1]]['spentTxId']
else: # this can pick up failed or in progress 2/5 or 3/5 swaps if desired
continue
spent_tx = rpc.getrawtransaction(spent_txid, 2)
#bob_addr = spent_tx['vout'][0]['scriptPubKey']['addresses'][0]
if spent_tx['vout']:
spend_amount = spent_tx['vout'][0]['valueSat']
else:
spend_amount = abs(spent_tx['valueBalance'])
mutual = spent_tx['vin'][0]['scriptSig']['hex'][-284:-220]
if len(mutual) == 64:
mutuals[mutual] = [[txid[0], tx['vout'][txid[1]]['valueSat']], [spent_txid, spend_amount]]
return(mutuals)
ALICE_CHAIN = sys.argv[1]
BOB_CHAIN = sys.argv[2]
ALICE_RPC = def_credentials(ALICE_CHAIN)
BOB_RPC = def_credentials(BOB_CHAIN)
try:
days = int(input('Please input amount of previous days: '))
except:
sys.exit('days must be whole number')
now = int(time.time())
start_time = now - days*86400
alice_p2sh = some_p2sh(ALICE_RPC, start_time)
bob_p2sh = some_p2sh(BOB_RPC, start_time)
alice_mutuals = mutual_scripts(ALICE_RPC, alice_p2sh)
bob_mutuals = mutual_scripts(BOB_RPC, bob_p2sh)
swaps = []
for bob_mutual in bob_mutuals:
if bob_mutual in alice_mutuals:
swap = {
"1_alice_b": alice_mutuals[bob_mutual][0],
"2_bob_b": bob_mutuals[bob_mutual][0],
"3_b_alice": alice_mutuals[bob_mutual][1],
"4_b_bob": bob_mutuals[bob_mutual][1]
}
swaps.append(swap)
f = open(ALICE_CHAIN + "_" + BOB_CHAIN + "_partial.json", "w+")
f.write(json.dumps(swaps))
alice_vol = 0
bob_vol = 0
for swap in swaps:
alice_vol += swap['1_alice_b'][1]
bob_vol += swap['2_bob_b'][1]
print(ALICE_CHAIN + ' volume:', alice_vol/100000000)
print(BOB_CHAIN + ' volume:', bob_vol/100000000)
print('total succesful swaps:', len(swaps))