Skip to content

Commit

Permalink
Remove duplicate create_erc20 and deploy_datatoken functions. (#1143)
Browse files Browse the repository at this point in the history
* Remove duplicate create_erc20 and deploy_datatoken functions.
* Fix readme call of create_datatoken.
  • Loading branch information
calina-c authored Dec 2, 2022
1 parent 6510af4 commit b2b6100
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 165 deletions.
2 changes: 1 addition & 1 deletion READMEs/data-nfts-and-datatokens-flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ In the same Python console:
```python
# Create datatoken related to the above NFT.

datatoken = data_nft.create_datatoken("Datatoken 1", "DT1", from_wallet=alice_wallet)
datatoken = data_nft.create_datatoken("Datatoken 1", "DT1", {"from": alice_wallet})
print(f"Created datatoken. Its address is {datatoken.address}")
```

Expand Down
13 changes: 2 additions & 11 deletions conftest_ganache.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from ocean_lib.models.data_nft import DataNFT
from ocean_lib.models.data_nft_factory import DataNFTFactoryContract
from ocean_lib.models.datatoken import Datatoken
from ocean_lib.models.datatoken_enterprise import DatatokenEnterprise
from ocean_lib.models.factory_router import FactoryRouter
from ocean_lib.ocean.util import get_address_of_type
from ocean_lib.web3_internal.constants import ZERO_ADDRESS
Expand Down Expand Up @@ -166,7 +165,7 @@ def data_nft(config, publisher_wallet, data_nft_factory):

@pytest.fixture
def datatoken(config, data_nft, publisher_wallet, data_nft_factory):
receipt = data_nft.create_erc20(
return data_nft.create_datatoken(
template_index=1,
name="DT1",
symbol="DT1Symbol",
Expand All @@ -179,14 +178,10 @@ def datatoken(config, data_nft, publisher_wallet, data_nft_factory):
transaction_parameters={"from": publisher_wallet},
)

dt_address = receipt.events["TokenCreated"]["newTokenAddress"]

return Datatoken(config, dt_address)


@pytest.fixture
def datatoken_enterprise_token(config, data_nft, publisher_wallet, data_nft_factory):
receipt = data_nft.create_erc20(
return data_nft.create_datatoken(
template_index=2,
name="DT1",
symbol="DT1Symbol",
Expand All @@ -200,10 +195,6 @@ def datatoken_enterprise_token(config, data_nft, publisher_wallet, data_nft_fact
datatoken_cap=Web3.toWei(100, "ether"),
)

dt_address = receipt.events["TokenCreated"]["newTokenAddress"]

return DatatokenEnterprise(config, dt_address)


@pytest.fixture
def publisher_addr():
Expand Down
99 changes: 36 additions & 63 deletions ocean_lib/models/data_nft.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,43 +71,11 @@ def set_metadata_token_uri(
transaction_parameters,
)

@enforce_types
def create_erc20(
self,
template_index: int,
name: str,
symbol: str,
minter: str,
fee_manager: str,
publish_market_order_fee_address: str,
publish_market_order_fee_token: str,
publish_market_order_fee_amount: int,
bytess: List[bytes],
transaction_parameters: dict,
datatoken_cap: Optional[int] = None,
) -> str:
if template_index == 2 and not datatoken_cap:
raise Exception("Cap is needed for Datatoken Enterprise token deployment.")
datatoken_cap = datatoken_cap if template_index == 2 else MAX_UINT256
return self.contract.createERC20(
template_index,
[name, symbol],
[
ContractBase.to_checksum_address(minter),
ContractBase.to_checksum_address(fee_manager),
ContractBase.to_checksum_address(publish_market_order_fee_address),
ContractBase.to_checksum_address(publish_market_order_fee_token),
],
[datatoken_cap, publish_market_order_fee_amount],
bytess,
transaction_parameters,
)

def create_datatoken(
self,
name: str,
symbol: str,
from_wallet,
transaction_parameters: dict,
template_index: Optional[int] = 1,
minter: Optional[str] = None,
fee_manager: Optional[str] = None,
Expand All @@ -116,50 +84,55 @@ def create_datatoken(
publish_market_order_fee_amount: Optional[int] = 0,
bytess: Optional[List[bytes]] = None,
datatoken_cap: Optional[int] = None,
wrap_as_object: Optional[bool] = True,
) -> Datatoken:

initial_list = self.getTokensList()

local_values = locals().copy()
create_args = {
lv_index: local_values[lv_index]
for lv_index in [
"template_index",
"name",
"symbol",
"from_wallet",
"minter",
"fee_manager",
"publish_market_order_fee_address",
"publish_market_order_fee_token",
"publish_market_order_fee_amount",
"bytess",
]
}

for default_attribute in [
"minter",
"fee_manager",
"publish_market_order_fee_address",
]:
if create_args[default_attribute] is None:
create_args[default_attribute] = from_wallet.address

from_address = (
transaction_parameters["from"].address
if isinstance(transaction_parameters["from"], object)
else transaction_parameters["from"]
)

if minter is None:
minter = from_address

if fee_manager is None:
fee_manager = from_address

if publish_market_order_fee_address is None:
publish_market_order_fee_address = from_address

if publish_market_order_fee_token is None:
create_args["publish_market_order_fee_token"] = ZERO_ADDRESS
publish_market_order_fee_token = ZERO_ADDRESS

if bytess is None:
create_args["bytess"] = [b""]
bytess = [b""]

if template_index == 2 and not datatoken_cap:
raise Exception("Cap is needed for Datatoken Enterprise token deployment.")

if template_index == 2:
create_args["datatoken_cap"] = datatoken_cap
datatoken_cap = datatoken_cap if template_index == 2 else MAX_UINT256

create_args["transaction_parameters"] = {"from": create_args["from_wallet"]}
create_args.pop("from_wallet")
contract_call = self.contract.createERC20(
template_index,
[name, symbol],
[
ContractBase.to_checksum_address(minter),
ContractBase.to_checksum_address(fee_manager),
ContractBase.to_checksum_address(publish_market_order_fee_address),
ContractBase.to_checksum_address(publish_market_order_fee_token),
],
[datatoken_cap, publish_market_order_fee_amount],
bytess,
transaction_parameters,
)

self.create_erc20(**create_args)
if not wrap_as_object:
return contract_call

new_elements = [
item for item in self.getTokensList() if item not in initial_list
Expand Down
36 changes: 20 additions & 16 deletions ocean_lib/models/test/test_data_nft.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def test_fails_update_metadata(consumer_wallet, consumer_addr, data_nft):


@pytest.mark.unit
def test_create_erc20(
def test_create_datatoken(
publisher_wallet,
publisher_addr,
consumer_addr,
Expand All @@ -301,7 +301,7 @@ def test_create_erc20(
"""Tests calling create an ERC20 by the owner."""
assert data_nft.getPermissions(publisher_addr)[DataNFTPermissions.DEPLOY_DATATOKEN]

receipt = data_nft.create_erc20(
receipt = data_nft.create_datatoken(
template_index=1,
name="DT1",
symbol="DT1Symbol",
Expand All @@ -312,13 +312,14 @@ def test_create_erc20(
publish_market_order_fee_amount=0,
bytess=[b""],
transaction_parameters={"from": publisher_wallet},
wrap_as_object=False,
)
assert receipt, "Could not create ERC20."

assert receipt.events["TokenCreated"], "Cannot find TokenCreated event."

with pytest.raises(Exception, match="Cap is needed for Datatoken Enterprise"):
data_nft.create_erc20(
data_nft.create_datatoken(
template_index=2,
name="DatatokenEnterpriseDT1",
symbol="DatatokenEnterpriseDT1Symbol",
Expand All @@ -336,10 +337,10 @@ def test_create_erc20(
template_index=2,
name="DatatokenEnterpriseDT1",
symbol="DatattokenEnterpriseDT1Symbol",
from_wallet=publisher_wallet,
transaction_parameters={"from": publisher_wallet},
)

tx = data_nft.create_erc20(
tx = data_nft.create_datatoken(
template_index=2,
name="DatatokenEnterpriseDT1",
symbol="DatatokenEnterpriseDT1Symbol",
Expand All @@ -351,16 +352,18 @@ def test_create_erc20(
bytess=[b""],
transaction_parameters={"from": publisher_wallet},
datatoken_cap=Web3.toWei("0.1", "ether"),
wrap_as_object=False,
)
assert tx, "Could not create datatoken Enterprise with explicit parameters"

tx = data_nft.create_datatoken(
template_index=2,
name="DatatokenEnterpriseDT1",
symbol="DatatokenEnterpriseDT1Symbol",
datatoken_cap=Web3.toWei("0.1", "ether"),
from_wallet=publisher_wallet,
transaction_parameters={"from": publisher_wallet},
)
assert tx, "Could not create datatoken Enterprise using create_datatoken."
assert tx, "Could not create datatoken Enterprise with implicit parameters."


def test_create_datatoken_with_usdc_order_fee(
Expand All @@ -372,7 +375,7 @@ def test_create_datatoken_with_usdc_order_fee(
"""Create an ERC20 with order fees ( 5 USDC, going to publishMarketAddress)"""
usdc = Datatoken(config, get_address_of_type(config, "MockUSDC"))
publish_market_order_fee_amount_in_wei = Web3.toWei(5, "ether")
receipt = data_nft.create_erc20(
dt = data_nft.create_datatoken(
template_index=1,
name="DT1",
symbol="DT1Symbol",
Expand All @@ -384,9 +387,6 @@ def test_create_datatoken_with_usdc_order_fee(
bytess=[b""],
transaction_parameters={"from": publisher_wallet},
)
dt_address = receipt.events["TokenCreated"]["newTokenAddress"]

dt = Datatoken(config, dt_address)

# Check publish fee info
(
Expand Down Expand Up @@ -420,7 +420,7 @@ def test_create_datatoken_with_non_owner(
]

# Consumer creates ERC20
receipt = data_nft.create_erc20(
receipt = data_nft.create_datatoken(
template_index=1,
name="DT1",
symbol="DT1Symbol",
Expand All @@ -431,6 +431,7 @@ def test_create_datatoken_with_non_owner(
publish_market_order_fee_amount=0,
bytess=[b""],
transaction_parameters={"from": consumer_wallet},
wrap_as_object=False,
)
assert receipt, "Failed to create ERC20 token."

Expand All @@ -452,7 +453,7 @@ def test_fail_creating_erc20(consumer_wallet, publisher_addr, consumer_addr, dat
data_nft.getPermissions(consumer_addr)[DataNFTPermissions.DEPLOY_DATATOKEN]
)
with pytest.raises(Exception, match="NOT ERC20DEPLOYER_ROLE"):
data_nft.create_erc20(
data_nft.create_datatoken(
template_index=1,
name="DT1",
symbol="DT1Symbol",
Expand Down Expand Up @@ -520,7 +521,7 @@ def test_erc721_datatoken_functions(
assert data_nft.balanceOf(publisher_addr) == 0
assert data_nft.ownerOf(1) == consumer_addr
assert data_nft.getPermissions(consumer_addr)[DataNFTPermissions.DEPLOY_DATATOKEN]
data_nft.create_erc20(
data_nft.create_datatoken(
template_index=1,
name="DT1",
symbol="DT1Symbol",
Expand All @@ -531,6 +532,7 @@ def test_erc721_datatoken_functions(
publish_market_order_fee_amount=0,
bytess=[b""],
transaction_parameters={"from": consumer_wallet},
wrap_as_object=False,
)
with pytest.raises(Exception, match="NOT MINTER"):
datatoken.mint(
Expand Down Expand Up @@ -651,7 +653,7 @@ def test_transfer_nft(
assert data_nft.isERC20Deployer(consumer_addr)

# Creates an ERC20
receipt = data_nft.create_erc20(
receipt = data_nft.create_datatoken(
template_index=1,
name="DT1",
symbol="DT1Symbol",
Expand All @@ -662,6 +664,7 @@ def test_transfer_nft(
publish_market_order_fee_amount=0,
bytess=[b""],
transaction_parameters={"from": consumer_wallet},
wrap_as_object=False,
)
assert receipt, "Failed to create ERC20 token."

Expand Down Expand Up @@ -840,7 +843,7 @@ def test_transfer_nft_with_erc20_pool_fre(
assert data_nft.symbol() == "NFTtT"

# Creates an ERC20
receipt = data_nft.create_erc20(
receipt = data_nft.create_datatoken(
template_index=1,
name="DT1",
symbol="DT1Symbol",
Expand All @@ -851,6 +854,7 @@ def test_transfer_nft_with_erc20_pool_fre(
publish_market_order_fee_amount=0,
bytess=[b""],
transaction_parameters={"from": publisher_wallet},
wrap_as_object=False,
)
assert receipt, "Failed to create ERC20 token."
registered_token_event = receipt.events["TokenCreated"]
Expand Down
Loading

0 comments on commit b2b6100

Please sign in to comment.