Bancor classifier

This commit is contained in:
Shea Ketsdever 2021-11-28 14:51:24 -08:00
parent 26caaa04e1
commit cd01298ba6
6 changed files with 148 additions and 3 deletions

File diff suppressed because one or more lines are too long

View File

@ -55,6 +55,43 @@ def create_swap_from_transfers(
)
def create_swap_from_transfers_not_including_pool(
trace: DecodedCallTrace,
pool_address: str,
recipient_address: str,
prior_transfers: List[Transfer],
child_transfers: List[Transfer],
) -> Optional[Swap]:
transfers_from_recipient = _filter_transfers(
[*prior_transfers, *child_transfers], from_address=recipient_address
)
transfers_to_recipient = _filter_transfers(
child_transfers, to_address=recipient_address
)
if len(transfers_from_recipient) != 1 or len(transfers_to_recipient) != 1:
return None
transfer_in = transfers_from_recipient[0]
transfer_out = transfers_to_recipient[0]
return Swap(
abi_name=trace.abi_name,
transaction_hash=trace.transaction_hash,
block_number=trace.block_number,
trace_address=trace.trace_address,
contract_address=pool_address,
protocol=trace.protocol,
from_address=transfer_in.from_address,
to_address=transfer_out.to_address,
token_in_address=transfer_in.token_address,
token_in_amount=transfer_in.amount,
token_out_address=transfer_out.token_address,
token_out_amount=transfer_out.amount,
error=trace.error,
)
def _build_eth_transfer(trace: ClassifiedTrace) -> Transfer:
return Transfer(
block_number=trace.block_number,

View File

@ -12,6 +12,7 @@ from .zero_ex import ZEROX_CLASSIFIER_SPECS
from .balancer import BALANCER_CLASSIFIER_SPECS
from .compound import COMPOUND_CLASSIFIER_SPECS
from .cryptopunks import CRYPTOPUNKS_CLASSIFIER_SPECS
from .bancor import BANCOR_CLASSIFIER_SPECS
ALL_CLASSIFIER_SPECS = (
ERC20_CLASSIFIER_SPECS
@ -23,6 +24,7 @@ ALL_CLASSIFIER_SPECS = (
+ BALANCER_CLASSIFIER_SPECS
+ COMPOUND_CLASSIFIER_SPECS
+ CRYPTOPUNKS_CLASSIFIER_SPECS
+ BANCOR_CLASSIFIER_SPECS
)
_SPECS_BY_ABI_NAME_AND_PROTOCOL: Dict[

View File

@ -0,0 +1,48 @@
from typing import Optional, List
from mev_inspect.schemas.transfers import Transfer
from mev_inspect.schemas.swaps import Swap
from mev_inspect.schemas.traces import (
DecodedCallTrace,
Protocol,
)
from mev_inspect.schemas.classifiers import (
ClassifierSpec,
SwapClassifier,
)
from mev_inspect.classifiers.helpers import (
create_swap_from_transfers_not_including_pool,
)
BANCOR_NETWORK_ABI_NAME = "BancorNetwork"
BANCOR_NETWORK_CONTRACT_ADDRESS = "0x2F9EC37d6CcFFf1caB21733BdaDEdE11c823cCB0"
class BancorSwapClassifier(SwapClassifier):
@staticmethod
def parse_swap(
trace: DecodedCallTrace,
prior_transfers: List[Transfer],
child_transfers: List[Transfer],
) -> Optional[Swap]:
recipient_address = trace.from_address
swap = create_swap_from_transfers_not_including_pool(
trace,
BANCOR_NETWORK_CONTRACT_ADDRESS,
recipient_address,
prior_transfers,
child_transfers,
)
return swap
BANCOR_NETWORK_SPEC = ClassifierSpec(
abi_name=BANCOR_NETWORK_ABI_NAME,
protocol=Protocol.bancor,
classifiers={
"convertByPath(address[],uint256,uint256,address,address,uint256)": BancorSwapClassifier,
},
valid_contract_addresses=[BANCOR_NETWORK_CONTRACT_ADDRESS],
)
BANCOR_CLASSIFIER_SPECS = [BANCOR_NETWORK_SPEC]

View File

@ -47,6 +47,7 @@ class Protocol(Enum):
compound_v2 = "compound_v2"
cream = "cream"
cryptopunks = "cryptopunks"
bancor = "bancor"
class ClassifiedTrace(Trace):

View File

@ -4,6 +4,10 @@ from mev_inspect.classifiers.specs.uniswap import (
UNISWAP_V2_PAIR_ABI_NAME,
UNISWAP_V3_POOL_ABI_NAME,
)
from mev_inspect.classifiers.specs.bancor import (
BANCOR_NETWORK_ABI_NAME,
BANCOR_NETWORK_CONTRACT_ADDRESS,
)
from mev_inspect.schemas.traces import Protocol
from .helpers import (
@ -23,12 +27,14 @@ def test_swaps(
first_transaction_hash,
second_transaction_hash,
third_transaction_hash,
] = get_transaction_hashes(3)
fourth_transaction_hash,
] = get_transaction_hashes(4)
[
alice_address,
bob_address,
carl_address,
danielle_address,
first_token_in_address,
first_token_out_address,
first_pool_address,
@ -38,7 +44,10 @@ def test_swaps(
third_token_in_address,
third_token_out_address,
third_pool_address,
] = get_addresses(12)
fourth_token_in_address,
fourth_token_out_address,
first_converter_address,
] = get_addresses(16)
first_token_in_amount = 10
first_token_out_amount = 20
@ -46,6 +55,8 @@ def test_swaps(
second_token_out_amount = 40
third_token_in_amount = 50
third_token_out_amount = 60
fourth_token_in_amount = 70
fourth_token_out_amount = 80
traces = [
make_unknown_trace(block_number, first_transaction_hash, []),
@ -139,11 +150,41 @@ def test_swaps(
recipient_address=bob_address,
recipient_input_key="recipient",
),
make_transfer_trace(
block_number,
fourth_transaction_hash,
trace_address=[2],
from_address=danielle_address,
to_address=first_converter_address,
token_address=fourth_token_in_address,
amount=fourth_token_in_amount,
),
make_transfer_trace(
block_number,
fourth_transaction_hash,
trace_address=[1, 2],
from_address=first_converter_address,
to_address=danielle_address,
token_address=fourth_token_out_address,
amount=fourth_token_out_amount,
),
make_swap_trace(
block_number,
fourth_transaction_hash,
trace_address=[],
from_address=danielle_address,
contract_address=BANCOR_NETWORK_CONTRACT_ADDRESS,
abi_name=BANCOR_NETWORK_ABI_NAME,
protocol=Protocol.bancor,
function_signature="convertByPath(address[],uint256,uint256,address,address,uint256)",
recipient_address=danielle_address,
recipient_input_key="recipient",
),
]
swaps = get_swaps(traces)
assert len(swaps) == 3
assert len(swaps) == 4
for swap in swaps:
if swap.abi_name == UNISWAP_V2_PAIR_ABI_NAME:
@ -152,6 +193,8 @@ def test_swaps(
uni_v3_swap = swap
elif swap.abi_name == BALANCER_V1_POOL_ABI_NAME:
bal_v1_swap = swap
elif swap.abi_name == BANCOR_NETWORK_ABI_NAME:
bancor_swap = swap
else:
assert False
@ -193,3 +236,16 @@ def test_swaps(
assert bal_v1_swap.token_in_amount == third_token_in_amount
assert bal_v1_swap.token_out_address == third_token_out_address
assert bal_v1_swap.token_out_amount == third_token_out_amount
assert bancor_swap.abi_name == BANCOR_NETWORK_ABI_NAME
assert bancor_swap.transaction_hash == fourth_transaction_hash
assert bancor_swap.block_number == block_number
assert bancor_swap.trace_address == []
assert bancor_swap.protocol == Protocol.bancor
assert bancor_swap.contract_address == BANCOR_NETWORK_CONTRACT_ADDRESS
assert bancor_swap.from_address == danielle_address
assert bancor_swap.to_address == danielle_address
assert bancor_swap.token_in_address == fourth_token_in_address
assert bancor_swap.token_in_amount == fourth_token_in_amount
assert bancor_swap.token_out_address == fourth_token_out_address
assert bancor_swap.token_out_amount == fourth_token_out_amount