Add token_factory

This commit is contained in:
Thales Lima
2024-08-06 17:28:47 +02:00
committed by tvinagre
parent d0c248fcb6
commit cb6e997375
2 changed files with 45 additions and 9 deletions

View File

@@ -29,7 +29,7 @@ from tycho_client.stream import TychoStream
from .adapter_handler import AdapterContractHandler
from .evm import get_token_balance, get_block_header
from .tycho import TychoRunner
from .utils import build_snapshot_message
from .utils import build_snapshot_message, token_factory
class TestResult:
@@ -74,6 +74,7 @@ class TestRunner:
db_url, with_binary_logs, self.config["initialized_accounts"]
)
self.tycho_rpc_client = TychoRPCClient()
self._token_factory_func = token_factory(self.tycho_rpc_client)
self.db_url = db_url
self._vm_traces = vm_traces
self._chain = Chain.ethereum
@@ -244,14 +245,10 @@ class TestRunner:
)
decoder = ThirdPartyPoolTychoDecoder(
adapter_contract=adapter_contract, minimum_gas=0, trace=self._vm_traces
)
stream_adapter = TychoStream(
tycho_url="0.0.0.0:4242",
exchanges=[protocol],
min_tvl=Decimal("0"),
blockchain=self._chain,
token_factory_func=self._token_factory_func,
adapter_contract=adapter_contract,
minimum_gas=0,
trace=self._vm_traces,
)
snapshot_message: Snapshot = build_snapshot_message(

View File

@@ -1,13 +1,20 @@
from logging import getLogger
from typing import Union
from protosim_py.evm.pool_state import ThirdPartyPool
from protosim_py.models import EthereumToken
from tycho_client.dto import (
ResponseProtocolState,
ProtocolComponent,
ResponseAccount,
ComponentWithState,
Snapshot,
HexBytes,
TokensParams,
PaginationParams,
ResponseToken,
)
from tycho_client.rpc_client import TychoRPCClient
log = getLogger(__name__)
@@ -32,3 +39,35 @@ def build_snapshot_message(
states = {id_: ComponentWithState(**state) for id_, state in states.items()}
return Snapshot(states=states, vm_storage=vm_storage)
def token_factory(rpc_client: TychoRPCClient) -> callable(HexBytes):
_client = rpc_client
_token_cache: dict[HexBytes, EthereumToken] = {}
def factory(addresses: Union[HexBytes, list[HexBytes]]) -> list[EthereumToken]:
if not isinstance(addresses, list):
addresses = [addresses]
response = dict()
to_fetch = []
for address in addresses:
if address in _token_cache:
response[address] = _token_cache[address]
else:
to_fetch.append(address)
if to_fetch:
pagination = PaginationParams(page_size=len(to_fetch), page=0)
params = TokensParams(token_addresses=to_fetch, pagination=pagination)
tokens = _client.get_tokens(params)
for token in tokens:
eth_token = EthereumToken(**token.dict())
response[token.address] = eth_token
_token_cache[token.address] = eth_token
return [response[address] for address in addresses]
return factory