Skip to content

Commit 8087ca8

Browse files
Refactor search_exchange_by_data_token to get the events in chunks (#532)
* Added a tweak for search function and used get_event_log. Fixed a typo in the FRE README. * Fixed typo. Added from_block parameter to find easier the events. * Fixed unit test for search function. * Refactored search function to use data_token address again. * Added black formatting. Co-authored-by: Călina Cenan <calina@kaolin.tech>
1 parent a361075 commit 8087ca8

3 files changed

Lines changed: 33 additions & 9 deletions

File tree

READMEs/fixed-rate-exchange-flow.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ data_token.approve(ocean.exchange._exchange_address, to_wei(100), alice_wallet)
130130
In the same python console:
131131
```python
132132
bob_private_key = os.getenv('TEST_PRIVATE_KEY2')
133-
bob_wallet = Wallet(ocean.web3, bob_private_key, config.block_confirmations. config transaction_timeout)
133+
bob_wallet = Wallet(ocean.web3, bob_private_key, config.block_confirmations, config.transaction_timeout)
134134
print(f"bob_wallet.address = '{bob_wallet.address}'")
135135

136136
#Verify that Bob has ganache ETH
@@ -155,7 +155,8 @@ exchanges for a certain data token, it can be searched by
155155
providing the data token address.
156156

157157
```python
158-
#Search for exchange_id for a certain data token address (e.g. token_address).
158+
#Search for exchange_id from a specific block retrieved at 3rd step
159+
#for a certain data token address (e.g. token_address).
159160
logs = ocean.exchange.search_exchange_by_data_token(token_address)
160161
print(logs)
161162
#E.g. First exchange is the wanted one.

ocean_lib/ocean/ocean_exchange.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
from ocean_lib.config import Config
1212
from ocean_lib.exceptions import InsufficientBalance, VerifyTxFailed
1313
from ocean_lib.models.data_token import DataToken
14+
from ocean_lib.models.dtfactory import DTFactory
1415
from ocean_lib.models.fixed_rate_exchange import FixedRateExchange
16+
from ocean_lib.ocean.util import get_dtfactory_address
1517
from ocean_lib.web3_internal.currency import pretty_ether_and_wei
1618
from ocean_lib.web3_internal.wallet import Wallet
1719
from web3.exceptions import ValidationError
@@ -63,15 +65,24 @@ def get_exchange_id_fallback_dt_and_owner(
6365

6466
@enforce_types
6567
def search_exchange_by_data_token(self, data_token: str) -> List[AttributeDict]:
68+
dtfactory_address = get_dtfactory_address(
69+
self._config.address_file, web3=self._web3
70+
)
71+
dtfactory = DTFactory(self._web3, dtfactory_address)
72+
token_registered_log = dtfactory.get_token_registered_event(
73+
0, self._web3.eth.block_number, data_token
74+
)
75+
assert (
76+
token_registered_log
77+
), f"No token with '{data_token}' address was created before."
78+
from_block = token_registered_log.blockNumber
6679
fre = self._exchange_contract()
67-
events = fre.events
68-
exchange_event = getattr(events, "ExchangeCreated")
6980
filter_args = {"dataToken": data_token}
70-
logs = fre.getLogs(
71-
exchange_event,
72-
argument_filters=filter_args,
73-
fromBlock=0,
74-
toBlock=self._web3.eth.block_number,
81+
logs = fre.get_event_logs(
82+
event_name="ExchangeCreated",
83+
from_block=from_block,
84+
to_block=self._web3.eth.block_number,
85+
filters=filter_args,
7586
)
7687
return logs
7788

ocean_lib/ocean/test/test_ocean_exchange.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ def _get_exchange_address(config):
2121
]
2222

2323

24+
def test_search_exchange_by_nonexistent_data_token(publisher_ocean_instance):
25+
"""Tests searching exchanges with a nonexistent data token address."""
26+
ocn = publisher_ocean_instance
27+
foo_data_token = "0xcd2a3d9f938e13cd947ec05abc7fe734df8dd826"
28+
with pytest.raises(AssertionError) as err:
29+
ocn.exchange.search_exchange_by_data_token(foo_data_token)
30+
assert (
31+
err.value.args[0]
32+
== f"No token with '{foo_data_token}' address was created before."
33+
)
34+
35+
2436
def test_search_exchange_by_data_token(publisher_ocean_instance):
2537
"""Tests searching exchanges which have matching data token address."""
2638
ocn = publisher_ocean_instance

0 commit comments

Comments
 (0)