Add token_factory
This commit is contained in:
@@ -29,7 +29,7 @@ from tycho_client.stream import TychoStream
|
|||||||
from .adapter_handler import AdapterContractHandler
|
from .adapter_handler import AdapterContractHandler
|
||||||
from .evm import get_token_balance, get_block_header
|
from .evm import get_token_balance, get_block_header
|
||||||
from .tycho import TychoRunner
|
from .tycho import TychoRunner
|
||||||
from .utils import build_snapshot_message
|
from .utils import build_snapshot_message, token_factory
|
||||||
|
|
||||||
|
|
||||||
class TestResult:
|
class TestResult:
|
||||||
@@ -74,6 +74,7 @@ class TestRunner:
|
|||||||
db_url, with_binary_logs, self.config["initialized_accounts"]
|
db_url, with_binary_logs, self.config["initialized_accounts"]
|
||||||
)
|
)
|
||||||
self.tycho_rpc_client = TychoRPCClient()
|
self.tycho_rpc_client = TychoRPCClient()
|
||||||
|
self._token_factory_func = token_factory(self.tycho_rpc_client)
|
||||||
self.db_url = db_url
|
self.db_url = db_url
|
||||||
self._vm_traces = vm_traces
|
self._vm_traces = vm_traces
|
||||||
self._chain = Chain.ethereum
|
self._chain = Chain.ethereum
|
||||||
@@ -244,14 +245,10 @@ class TestRunner:
|
|||||||
)
|
)
|
||||||
|
|
||||||
decoder = ThirdPartyPoolTychoDecoder(
|
decoder = ThirdPartyPoolTychoDecoder(
|
||||||
adapter_contract=adapter_contract, minimum_gas=0, trace=self._vm_traces
|
token_factory_func=self._token_factory_func,
|
||||||
)
|
adapter_contract=adapter_contract,
|
||||||
|
minimum_gas=0,
|
||||||
stream_adapter = TychoStream(
|
trace=self._vm_traces,
|
||||||
tycho_url="0.0.0.0:4242",
|
|
||||||
exchanges=[protocol],
|
|
||||||
min_tvl=Decimal("0"),
|
|
||||||
blockchain=self._chain,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
snapshot_message: Snapshot = build_snapshot_message(
|
snapshot_message: Snapshot = build_snapshot_message(
|
||||||
|
|||||||
@@ -1,13 +1,20 @@
|
|||||||
from logging import getLogger
|
from logging import getLogger
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
from protosim_py.evm.pool_state import ThirdPartyPool
|
from protosim_py.evm.pool_state import ThirdPartyPool
|
||||||
|
from protosim_py.models import EthereumToken
|
||||||
from tycho_client.dto import (
|
from tycho_client.dto import (
|
||||||
ResponseProtocolState,
|
ResponseProtocolState,
|
||||||
ProtocolComponent,
|
ProtocolComponent,
|
||||||
ResponseAccount,
|
ResponseAccount,
|
||||||
ComponentWithState,
|
ComponentWithState,
|
||||||
Snapshot,
|
Snapshot,
|
||||||
|
HexBytes,
|
||||||
|
TokensParams,
|
||||||
|
PaginationParams,
|
||||||
|
ResponseToken,
|
||||||
)
|
)
|
||||||
|
from tycho_client.rpc_client import TychoRPCClient
|
||||||
|
|
||||||
log = getLogger(__name__)
|
log = getLogger(__name__)
|
||||||
|
|
||||||
@@ -32,3 +39,35 @@ def build_snapshot_message(
|
|||||||
|
|
||||||
states = {id_: ComponentWithState(**state) for id_, state in states.items()}
|
states = {id_: ComponentWithState(**state) for id_, state in states.items()}
|
||||||
return Snapshot(states=states, vm_storage=vm_storage)
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user