Skip to content

Commit

Permalink
Replicate solution for fres from another branch.
Browse files Browse the repository at this point in the history
  • Loading branch information
calina-c committed Oct 31, 2023
1 parent 56fa373 commit 28d8ae0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
13 changes: 7 additions & 6 deletions ocean_lib/models/datatoken1.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from enforce_typing import enforce_types

from ocean_lib.models.datatoken_base import DatatokenBase, TokenFeeInfo
from ocean_lib.ocean.util import from_wei, get_address_of_type, get_from_address, to_wei
from ocean_lib.ocean.util import from_wei, get_from_address, to_wei
from ocean_lib.web3_internal.constants import ZERO_ADDRESS
from ocean_lib.web3_internal.contract_base import ContractBase

Expand Down Expand Up @@ -219,10 +219,10 @@ def dispense_and_order(

bal = from_wei(self.balanceOf(buyer_addr))
if bal < 1.0:
dispenser_addr = get_address_of_type(self.config_dict, "Dispenser")
from ocean_lib.models.dispenser import Dispenser # isort: skip
dispensers = self.get_dispensers()

dispenser = Dispenser(self.config_dict, dispenser_addr)
assert dispensers, "there are no dispensers for this datatoken"
dispenser = dispensers[0]

# catch key failure modes
st = dispenser.status(self.address)
Expand Down Expand Up @@ -256,7 +256,8 @@ def buy_DT_and_order(
if not consumer:
consumer = get_from_address(tx_dict)

fre_address = get_address_of_type(self.config_dict, "FixedPrice")
exchanges = self.get_exchanges()
assert exchanges, "there are no fixed rate exchanges for this datatoken"

# import now, to avoid circular import
from ocean_lib.models.fixed_rate_exchange import OneExchange
Expand All @@ -265,7 +266,7 @@ def buy_DT_and_order(
consume_market_fees = TokenFeeInfo()

if not isinstance(exchange, OneExchange):
exchange = OneExchange(fre_address, exchange)
exchange = exchanges[0]

exchange.buy_DT(
datatoken_amt=to_wei(1),
Expand Down
14 changes: 9 additions & 5 deletions ocean_lib/models/datatoken2.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from enforce_typing import enforce_types

from ocean_lib.models.datatoken_base import DatatokenBase, TokenFeeInfo
from ocean_lib.ocean.util import get_address_of_type, get_from_address, to_wei
from ocean_lib.ocean.util import get_from_address, to_wei
from ocean_lib.web3_internal.constants import ZERO_ADDRESS
from ocean_lib.web3_internal.contract_base import ContractBase

Expand Down Expand Up @@ -41,13 +41,14 @@ def buy_DT_and_order(
if not consumer:
consumer = get_from_address(tx_dict)

fre_address = get_address_of_type(self.config_dict, "FixedPrice")
exchanges = self.get_exchanges()
assert exchanges, "there are no fixed rate exchanges for this datatoken"

# import now, to avoid circular import
from ocean_lib.models.fixed_rate_exchange import OneExchange

if not isinstance(exchange, OneExchange):
exchange = OneExchange(fre_address, exchange)
exchange = exchanges[0]

if not consume_market_fees:
consume_market_fees = TokenFeeInfo()
Expand Down Expand Up @@ -97,7 +98,10 @@ def dispense_and_order(
if not consumer:
consumer = get_from_address(tx_dict)

dispenser_address = get_address_of_type(self.config_dict, "Dispenser")
dispensers = self.get_dispensers()
assert dispensers, "there are no dispensers for this datatoken"
dispenser = dispensers[0]

return self.buyFromDispenserAndOrder(
(
ContractBase.to_checksum_address(consumer),
Expand All @@ -114,6 +118,6 @@ def dispense_and_order(
),
consume_market_fees.to_tuple(),
),
ContractBase.to_checksum_address(dispenser_address),
ContractBase.to_checksum_address(dispenser.address),
tx_dict,
)
32 changes: 26 additions & 6 deletions ocean_lib/models/datatoken_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def create_datatoken(self, data_nft, tx_dict, with_services=False):
new_elements = [
item for item in data_nft.getTokensList() if item not in initial_list
]
assert len(new_elements) == 1, "new data token has no address"
assert len(new_elements) == 1, "new datatoken has no address"

datatoken = DatatokenBase.get_typed(config_dict, new_elements[0])

Expand Down Expand Up @@ -311,17 +311,22 @@ def create_exchange(
return (exchange, tx) if kwargs.get("full_info") else exchange

@enforce_types
def get_exchanges(self) -> list:
def get_exchanges(self, only_active=True) -> list:
"""return List[OneExchange] - all the exchanges for this datatoken"""
# import now, to avoid circular import
from ocean_lib.models.fixed_rate_exchange import OneExchange
from ocean_lib.models.fixed_rate_exchange import FixedRateExchange, OneExchange

FRE = self._FRE()
exchanges = []
addrs_and_exchange_ids = self.getFixedRates()
exchanges = [
OneExchange(FRE, exchange_id) for _, exchange_id in addrs_and_exchange_ids
OneExchange(FixedRateExchange(self.config_dict, address), exchange_id)
for address, exchange_id in addrs_and_exchange_ids
]
return exchanges

if not only_active:
return exchanges

return [exchange for exchange in exchanges if exchange.is_active()]

@enforce_types
def _FRE(self):
Expand Down Expand Up @@ -362,6 +367,21 @@ def create_dispenser(self, tx_dict: dict, *args, **kwargs):

return tx

@enforce_types
def get_dispensers(self, only_active=True) -> list:
"""return List[Dispenser] - all the dispensers for this datatoken"""
# import here to avoid circular import
from ocean_lib.models.dispenser import Dispenser

dispensers = []
addrs = self.getDispensers()
dispensers = [Dispenser(self.config_dict, address) for address in addrs]

if not only_active:
return dispensers

return [disp for disp in dispensers if disp.status(self.address)]

@enforce_types
def dispense(self, amount: Union[int, str], tx_dict: dict):
"""
Expand Down

0 comments on commit 28d8ae0

Please sign in to comment.