This repository was archived by the owner on May 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvalidator.py
More file actions
61 lines (40 loc) · 1.78 KB
/
Copy pathvalidator.py
File metadata and controls
61 lines (40 loc) · 1.78 KB
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
import json
from eth_utils import to_checksum_address
def _get_canonical_address(addr):
""" as ether may be represented with multiple addresses.
this function helps make it a single address.
return addr
"""
if addr == '0x0000000000000000000000000000000000000000':
return '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE'
return to_checksum_address(addr)
def validate_swap_path(whole_swap,swaps):
""" validate the swap path
whole_swap: the swap parsed from 1inch swap event.
swaps: the swaps parsed from each swap in the swap path
returns: true(valid swap path), false(some swap is missing in the path)
"""
balances = dict()
src_token, dst_token, amount_in, amount_out, sender, receiver = whole_swap
balances[src_token] = amount_in
balances[dst_token] = - amount_out
for src_token, dst_token, amount_in, amount_out, sender, receiver in swaps:
# if the token is ether
# we want to use a single address to represent it.
src_token = _get_canonical_address(src_token)
dst_token = _get_canonical_address(dst_token)
if src_token not in balances:
balances[src_token] = 0
if dst_token not in balances:
balances[dst_token] = 0
balances[src_token] = balances[src_token] - amount_in
balances[dst_token] = balances[dst_token] + amount_out
if True:
print('\nValidating swaps (The sum of inputs and outputs of each token should be zero.) ... :\n\n' +
json.dumps(balances,indent=' '))
for key, value in balances.items():
if value > 0:
print("\nWarning: Some swaps may not be correctly parsed.\n")
return False
print("\nSwaps validated: okay\n")
return True