Skip to content

Commit 227b8ef

Browse files
committed
Change data paths, minor fixes
1 parent 138b723 commit 227b8ef

16 files changed

+92
-67
lines changed

data-scripts/exchanges.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@
5151

5252
bancor_relayers = open('data/bancor_relayers').read().strip().splitlines()
5353
kyber_relayers = open('data/kyber_relayers').read().strip().splitlines()
54-
uniswap_relayers = open('data/uniswap_relayers').read().strip().splitlines()
55-
uniswapv2_relayers = open('data/uniswapv2_relayers').read().strip().splitlines()
56-
sushiswap_relayers = open('data/sushiswap_relayers').read().strip().splitlines()
54+
uniswap_relayers = open('latest-data/uniswap_relayers').read().strip().splitlines()
5755

5856

5957
dex_list = dex_list + bancor_relayers + kyber_relayers + uniswap_relayers
@@ -217,7 +215,6 @@ def get_trade_data_from_log_item(topics, data, address):
217215
exchange = "Kyber"
218216
parser = parse_kyber
219217
elif address in uniswap_relayers:
220-
print("PARSING", topics)
221218
if topics[0] == '0x7f4091b46c33e918a0f3aa42307641d17bb67029427a5369e54b353984238705':
222219
exchange = "Uniswap"
223220
parser = parse_uniswap_ethpurchase

data-scripts/get_top_uniswapv2_pairs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
pairs_to_tokens = {}
99

1010

11-
df = pd.read_csv('data/all_logs_uniswapv2_factory.csv')
11+
df = pd.read_csv('latest-data/all_logs_uniswapv2_factory.csv')
1212
for _, row in df.iterrows():
1313
topics = row['topics'][1:-1]
1414
data = row['data'][2:]
@@ -29,8 +29,8 @@
2929
pairs_to_tokens[(address, pair_address)] = (token0_addr, token1_addr)
3030

3131

32-
uniswapv2_top_tokens = 'data/uniswapv2_top_tokens.csv'
33-
uniswapv2_pairs = 'data/uniswapv2_pairs.csv'
32+
uniswapv2_top_tokens = 'latest-data/uniswapv2_top_tokens.csv'
33+
uniswapv2_pairs = 'latest-data/uniswapv2_pairs.csv'
3434

3535
with open(uniswapv2_top_tokens, 'w') as csvfile:
3636
spamwriter = csv.writer(csvfile, delimiter=',',

data-scripts/get_uniswapv2_pairs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import csv, os
77
from google.cloud import bigquery
88

9-
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "etharbskey.json"
9+
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "bq.json"
1010
client = bigquery.Client()
1111

1212

@@ -27,7 +27,7 @@
2727
job_config=job_config) # API request - starts the query
2828

2929

30-
with open('data/all_logs_uniswapv2_factory.csv', 'w') as csvfile:
30+
with open('latest-data/all_logs_uniswapv2_factory.csv', 'w') as csvfile:
3131
spamwriter = csv.writer(csvfile, delimiter=',',
3232
quotechar='"', quoting=csv.QUOTE_MINIMAL)
3333

data-scripts/get_uniswapv2_relayers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
uniswapv2_pairs = set()
1010

1111

12-
df = pd.read_csv('data/all_logs_uniswapv2_factory.csv')
12+
df = pd.read_csv('latest-data/all_logs_uniswapv2_factory.csv')
1313
for _, row in df.iterrows():
1414
topics = row['topics'][1:-1]
1515
data = row['data'][2:]
@@ -35,12 +35,12 @@
3535
print(pair_address)
3636

3737

38-
sushiswap_file = 'data/sushiswap_relayers'
38+
sushiswap_file = 'latest-data/sushiswap_relayers'
3939
f_sushiswap = open(sushiswap_file, 'w')
4040
for pair_address in sushiswap_pairs:
4141
f_sushiswap.write('0x'+ pair_address + '\n')
4242

43-
uniswapv2_file = 'data/uniswapv2_relayers'
43+
uniswapv2_file = 'latest-data/uniswapv2_relayers'
4444
f_uniswapv2 = open(uniswapv2_file, 'w')
4545
for pair_address in uniswapv2_pairs:
4646
f_uniswapv2.write('0x' + pair_address + '\n')

data-scripts/get_uswapv2_logs.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import csv, os
22
from google.cloud import bigquery
3-
from exchanges import uniswapv2_relayers, sushiswap_relayers
43

5-
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "etharbskey.json"
4+
5+
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "bq.json"
66
client = bigquery.Client()
77

88

9+
uniswapv2_relayers = open('latest-data/uniswapv2_relayers').read().strip().splitlines()
10+
sushiswap_relayers = open('latest-data/sushiswap_relayers').read().strip().splitlines()
11+
12+
913
query = """SELECT log_index,transaction_hash,logs.transaction_index,address,data,topics,logs.block_timestamp,logs.block_number,gas,gas_price,receipt_gas_used FROM
1014
`bigquery-public-data.crypto_ethereum.logs` AS logs
1115
JOIN `bigquery-public-data.crypto_ethereum.transactions` AS transactions ON logs.transaction_hash = transactions.hash
@@ -22,7 +26,7 @@
2226
# Location must match that of the dataset(s) referenced in the query.
2327
location='US',
2428
job_config=job_config) # API request - starts the query
25-
with open('data/all_logs_%s.csv' % (exchange_relayers[0]), 'w') as csvfile:
29+
with open('latest-data/all_logs_%s.csv' % (exchange_relayers[0]), 'w') as csvfile:
2630
spamwriter = csv.writer(csvfile, delimiter=',',
2731
quotechar='"', quoting=csv.QUOTE_MINIMAL)
2832

data-scripts/uniswapv2_reserves.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626

2727
exchange_name = args.exchange
2828

29-
uniswapv2_logs = 'data/all_logs_uniswapv2.csv'
30-
sushiswap_logs = 'data/all_logs_sushiswap.csv'
29+
uniswapv2_logs = 'latest-data/all_logs_uniswapv2.csv'
30+
sushiswap_logs = 'latest-data/all_logs_sushiswap.csv'
3131

3232
exchange_logs = {'uniswapv2' : uniswapv2_logs, 'sushiswap' : sushiswap_logs}
3333

34-
uniswapv2_pairs = pd.read_csv('data/uniswapv2_pairs.csv').set_index('pair')
34+
uniswapv2_pairs = pd.read_csv('latest-data/uniswapv2_pairs.csv').set_index('pair')
3535

3636
logsdict = csv.DictReader(open(exchange_logs[exchange_name]), delimiter=',',
3737
quotechar='"', quoting=csv.QUOTE_MINIMAL)
@@ -78,7 +78,7 @@
7878
logger.info("Parsed %d" %(parsed))
7979

8080

81-
filepath = '%s-reserves.csv' % (exchange_name)
81+
filepath = 'latest-data/%s-reserves.csv' % (exchange_name)
8282

8383
logger.info("Writing to %s" % (filepath))
8484

data-scripts/uniswapv2_trades.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import csv, csv_hack, os
1+
import csv, os
22
import pandas as pd
33
import logging
44

@@ -11,13 +11,13 @@
1111

1212
exchange_name = 'uniswapv2'
1313

14-
uniswapv2_logs = 'data/all_logs_uniswapv2.csv'
15-
sushiswap_logs = 'data/all_logs_sushiswap.csv'
14+
uniswapv2_logs = 'latest-data/all_logs_uniswapv2.csv'
15+
sushiswap_logs = 'latest-data/all_logs_sushiswap.csv'
1616

1717
exchange_logs = {'uniswapv2' : uniswapv2_logs, 'sushiswap' : sushiswap_logs}
18-
outputdir = exchange_name + '-processed'
18+
outputdir = 'latest-data/' + exchange_name + '-processed'
1919

20-
uniswapv2_pairs = pd.read_csv('data/uniswapv2_pairs.csv').set_index('pair')
20+
uniswapv2_pairs = pd.read_csv('latest-data/uniswapv2_pairs.csv').set_index('pair')
2121

2222
logsdict = csv.DictReader(open(exchange_logs[exchange_name]), delimiter=',',
2323
quotechar='"', quoting=csv.QUOTE_MINIMAL)

find_mev_krun_maker.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,33 @@
99
def all_orderings(all_transactions):
1010
ret = list(itertools.permutations(all_transactions))
1111
random.shuffle(ret)
12+
print("Num all reorderings ", len(ret))
13+
ret = [x for x in ret if valid_ordering(x)]
14+
print("Num valid reorderings", len(ret))
1215
return ret
1316

14-
def reordering_mev(program, program_file, outfile, acc, tokens, balances, pre_price, post_price, pair_address, maker_prologue):
17+
def valid_ordering(transaction_ordering):
18+
for transaction in transaction_ordering:
19+
if 'locks' in transaction:
20+
return True
21+
elif 'draws' in transaction:
22+
return False
23+
return True
24+
25+
def reordering_mev(program, program_file, outfile, acc, tokens, balances, pre_price, post_price, pair_address, maker_prologue, maker_epilogue):
1526

1627
program = program.strip()
1728

1829
addresses = set()
1930
transactions = program.split('\n')
2031
all_transactions = [transaction for transaction in transactions if not transaction.strip().startswith('//')]
21-
print(all_transactions)
32+
#print(all_transactions)
2233
for i in range(0, len(all_transactions)):
2334
chunks = all_transactions[i].split()
24-
print(chunks)
35+
#print(chunks)
2536
addresses.add(chunks[0])
2637

27-
print(addresses)
38+
#print(addresses)
2839

2940
lower_balance_bounds = {}
3041
upper_balance_bounds = {}
@@ -47,7 +58,7 @@ def reordering_mev(program, program_file, outfile, acc, tokens, balances, pre_pr
4758
output = ""
4859
Path(os.path.dirname(program_file)).mkdir(parents=True, exist_ok=True)
4960
#print("Writing program to", program_file)
50-
open(program_file, "w").write(PROLOGUE + '\n'.join(transaction_ordering))
61+
open(program_file, "w").write(PROLOGUE + '\n'.join(transaction_ordering) + maker_epilogue)
5162
sys.stdout.flush()
5263
pipe = Popen("krun " + program_file, shell=True, stdout=PIPE, stderr=PIPE)
5364
output = pipe.stdout.read() + pipe.stderr.read()

find_mev_uniswapv1.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ def reordering_mev(program, program_file, outfile, exchange_acc, tokens, balance
5151

5252
Path(os.path.dirname(program_file)).mkdir(parents=True, exist_ok=True)
5353
fout = open(program_file, 'w')
54-
#path_to_mev = {}
54+
path_to_mev = {}
5555

56-
#path_num = 0
56+
path_num = 0
5757
for transaction_ordering in all_orderings(all_transactions):
5858
u = UniswapV1({tokens[0] : balances[0], tokens[1] : balances[1]}, exchange_acc)
5959
for transaction in transaction_ordering:
6060
u.process(transaction)
6161
token_balances = u.config()
62-
#mev = 0
62+
mev = 0
6363
for acc in token_balances:
6464
if acc == exchange_acc:
6565
continue
@@ -72,21 +72,27 @@ def reordering_mev(program, program_file, outfile, exchange_acc, tokens, balance
7272
if total_balance > upper_bounds[acc][token0]:
7373
upper_bounds[acc][token0] = total_balance
7474
upper_bound_paths[acc] = ('\n'.join(transaction_ordering), token_balances)
75-
#extortion = upper_bounds[acc][token0] - lower_bounds[acc][token0]
75+
extortion = upper_bounds[acc][token0] - lower_bounds[acc][token0]
7676
#mev += extortion
77-
#path_num += 1
78-
#path_to_mev[path_num] = mev
77+
mev = max(mev,extortion)
78+
path_num += 1
79+
path_to_mev[path_num] = mev
7980

80-
#sorted_items = sorted(path_to_mev.items())
81+
sorted_items = sorted(path_to_mev.items())
8182
#print("Writing hill climbing data to {} ...".format(program_file))
82-
#fout.write('pathnum,mev\n')
83-
#fout.write('\n'.join(["{},{}".format(path_num, mev) for path_num, mev in sorted_items]))
83+
fout.write('pathnum,mev\n')
84+
fout.write('\n'.join(["{},{}".format(path_num, mev) for path_num, mev in sorted_items]))
8485

8586

86-
mev = 0
87+
mev = 0
88+
argmax_acc = 0
8789
for acc in lower_bounds:
8890
extortion = upper_bounds[acc][token0] - lower_bounds[acc][token0]
89-
mev += extortion
91+
#mev += extortion
92+
if extortion > mev :
93+
mev = extortion
94+
argmax_acc = acc
95+
print(argmax_acc)
9096
'''
9197
print(acc, extortion)
9298

find_mev_uniswapv2.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ def reordering_mev(program, program_file, outfile, exchange_acc, tokens, balance
7373
upper_bounds[acc][token0] = total_balance
7474
upper_bound_paths[acc] = ('\n'.join(transaction_ordering), token_balances)
7575
extortion = upper_bounds[acc][token0] - lower_bounds[acc][token0]
76-
mev += extortion
76+
#mev += extortion
77+
mev = max(mev, extortion)
7778
path_num += 1
7879
path_to_mev[path_num] = mev
7980

@@ -86,7 +87,9 @@ def reordering_mev(program, program_file, outfile, exchange_acc, tokens, balance
8687
mev = 0
8788
for acc in lower_bounds:
8889
extortion = upper_bounds[acc][token0] - lower_bounds[acc][token0]
89-
mev += extortion
90+
# mev += extortion
91+
mev = max(mev,extortion)
92+
'''
9093
print(acc, extortion)
9194
9295
for acc in lower_bounds:
@@ -96,7 +99,6 @@ def reordering_mev(program, program_file, outfile, exchange_acc, tokens, balance
9699
print(default_to_regular(upper_bound_paths[acc][0]))
97100
print('')
98101
print(default_to_regular(lower_bound_paths[acc][0]))
99-
'''
100102
'''
101103

102104
print(exchange_acc, pair_address, token0, token1, block, len(all_transactions), mev, sep=',')

0 commit comments

Comments
 (0)