Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on May 3, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ nftlabs/.DS_Store
nftlabs/modules/docs/*
nftlabs/modules/.DS_Store
.DS_Store
x.sh
examples/test.py
4 changes: 1 addition & 3 deletions nftlabs/modules/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
"""All Modules"""
from .currency import *

from .nft import *
from .nft_types import *
from .currency_types import *
from .currency import *
from .market import *
from .market_types import *
from .pack import *
from .pack_types import *
from .collection import *
12 changes: 6 additions & 6 deletions nftlabs/modules/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from zero_ex.contract_wrappers import TxParams

from .base import _BaseModule
from .base import BaseModule
from ..types.metadata import Metadata
from ..types.nft import NftMetadata
from ..errors import NoSignerException
Expand All @@ -13,7 +13,7 @@
from ..types.collection import CollectionMetadata, CreateCollectionArg, MintCollectionArg


class CollectionModule(_BaseModule):
class CollectionModule(BaseModule):
address: str
__abi_module: NFTCollection

Expand Down Expand Up @@ -114,11 +114,11 @@ def mint_batch(self, args: List[MintCollectionArg]):
self.mint_batch_to(self.get_signer_address(), args)

def mint_batch_to(self, to_address, args: List[MintCollectionArg]):
ids = [a.id for a in args]
ids = [a.token_id for a in args]
amounts = [a.amount for a in args]
self.execute_tx(self.__abi_module.mint_batch.build_transaction(
to_address, ids, amounts, self.get_transact_opts()
))
tx = self.__abi_module.mint_batch.build_transaction(
to_address, ids, amounts, self.get_transact_opts())
self.execute_tx(tx)

def burn(self, args: MintCollectionArg):
self.burn_from(self.get_signer_address(), args)
Expand Down
4 changes: 2 additions & 2 deletions nftlabs/modules/currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from ..errors import NoSignerException
from ..types import Role
from ..abi.coin import Coin
from .base import _BaseModule
from .base import BaseModule
from ..types.currency import Currency, CurrencyValue
from ..abi.erc20 import ERC20
from web3 import Web3
Expand Down Expand Up @@ -44,7 +44,7 @@ def balance_of(self, address: str) -> int:
"""
return self.__abi_module.balance_of.call(address)

def balance(self):
def balance(self) -> int:
"""
Gets the balance of the current address
"""
Expand Down
58 changes: 27 additions & 31 deletions nftlabs/modules/market.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
from . import BaseModule
from ..types import Role
from ..abi.market import Market
from ..types.market import ListArg, Filter
from ..types.market import ListArg, Filter, MarketListing
from ..types.listing import Listing
from ..abi.erc20 import ERC20
from ..abi.erc165 import ERC165
from ..abi.erc1155 import ERC1155
from ..abi.nft import NFT

nulladdress = "0x0000000000000000000000000000000000000000"

class MarketModule(BaseModule):
"""
Expand All @@ -21,8 +21,7 @@ class MarketModule(BaseModule):
Address of the market contract.
"""
__abi_module: Market

def __init__(self, client: Web3, address: str):
def __init__(self, address: str, client: Web3, ):
"""
Initialize the Market Module.
"""
Expand All @@ -31,38 +30,30 @@ def __init__(self, client: Web3, address: str):
self.address = address
self.__abi_module = Market(client, address)

#todo: return types
def list(self, arg: ListArg):
"""
List an asset for sale.
"""
from_address = self.get_signer_address()
client = self.get_client()
erc165 = ERC165(client, arg.asset_contract)
isERC721 = erc165.supports_interface(
client, interface_id=bytearray.fromhex("80ac58cd"))
isERC721 = erc165.supports_interface.call( bytearray.fromhex("80ac58cd"))
if isERC721:
asset = NFT(client, arg.asset_contract)
approved = asset.is_approved_for_all(
from_address, self.address)
approved = asset.is_approved_for_all.call( from_address, self.address)
if not approved:
asset.is_approve_for_all(from_address, arg.asset_contract)
is_token_approved = (asset.is_approved_for_all(
arg.token_id).lower() == self.address.lower())
is_token_approved = asset.get_approved.call(arg.token_id).lower() == self.address.lower()
if not is_token_approved:
asset.set_approval_for_all(arg.asset_contract, True)
self.execute_tx(asset.set_approval_for_all.build_transaction(self.address, True, self.get_transact_opts()))

else:
asset = ERC1155(client, arg.asset_contract)
approved = asset.is_approved_for_all(from_address, self.address)
approved = asset.is_approved_for_all.call(from_address, self.address)

if not approved:
asset.set_approval_for_all(from_address, arg.asset_contract)
is_token_approved = (asset.get_approved(
arg.token_id).lower() == self.address.lower())
if not is_token_approved:
asset.set_approval_for_all(self.address, True)
asset.set_approval_for_all.call(self.address, True)

tx = self.__abi_module.list.build_transaction(
tx = self.__abi_module._list.build_transaction(
arg.asset_contract,
arg.token_id,
arg.currency_contract,
Expand Down Expand Up @@ -100,19 +91,19 @@ def buy(self, listing_id: int, quantity: int):
"""
Buy a listing.
"""
listing = get(listing_id)
item = self.get(listing_id)
owner = self.get_signer_address()
spender = self.address
total_price = listing.price_per_token * quantity
if listing.currency_contract is not None and listing.currency_contract != "0x0000000000000000000000000000000000000000":
erc20 = ERC20(self.get_client(), listing.currency_contract)
allowance = erc20.allowance(owner, spender)
total_price = item.pricePerToken * quantity
if item.currency is not None and item.currency != nulladdress:
erc20 = ERC20(self.get_client(), item.currency)
allowance = erc20.allowance.call(owner, spender)
if allowance < total_price:
erc20.increase_allowance(
spender,
tx = erc20.increase_allowance.build_transaction( spender,
total_price,
self.get_transact_opts()
)
self.get_transact_opts())
self.execute_tx(tx)

tx = self.__abi_module.buy.build_transaction(
listing_id,
quantity,
Expand All @@ -131,7 +122,6 @@ def set_market_fee_bps(self, amount: int):
self.get_transact_opts())
self.execute_tx(tx)

def get(self, listing_id) -> List:
"""
Get a listing.
"""
Expand All @@ -142,6 +132,12 @@ def get_all_listings(self, search_filter: Filter = None) -> List[Listing]:
Returns all the listings.
"""
return self.get_all(search_filter)

def get(self, listing_id) -> MarketListing:
"""
Get a listing.
"""
return MarketListing(**self.__abi_module.get_listing.call(listing_id))

def set_module_metadata(metadata: str):
"""
Expand Down Expand Up @@ -183,4 +179,4 @@ def total_listings(self) -> int:
"""
Returns the total supply of the market.
"""
self.__abi_module.total_listings.call()
return self.__abi_module.total_listings.call()
32 changes: 0 additions & 32 deletions nftlabs/modules/market_types.py

This file was deleted.

40 changes: 20 additions & 20 deletions nftlabs/modules/nft.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
from web3 import Web3
from zero_ex.contract_wrappers import TxParams

from .base import _BaseModule
from .base import BaseModule
from typing import Dict, List
from ..abi.nft import NFT

from ..types import Role
from ..types.nft import MintArg, NftMetadata as NftType


class NftModule(_BaseModule):
class NftModule(BaseModule):
"""
NFT Methods
"""
Expand Down Expand Up @@ -56,8 +56,8 @@ def mint_to(
'properties': final_properties
}

uri = storage.upload(json.dumps(meta), self.address, self.__get_signer_address())
tx = self.__abi_module.mint_nft.build_transaction(to_address, uri, self.__get_transact_opts())
uri = storage.upload(json.dumps(meta), self.address, self.get_signer_address())
tx = self.__abi_module.mint_nft.build_transaction(to_address, uri, self.get_transact_opts())
receipt = self.execute_tx(tx)
result = self.__abi_module.get_minted_event(
tx_hash=receipt.transactionHash.hex())
Expand Down Expand Up @@ -107,7 +107,7 @@ def mint_batch_to(self, to_address: str, args: List[MintArg]):
'description': arg.description,
'image': arg.image_uri,
'properties': arg.properties if arg.properties is not None else {}
}), self.address, self.__get_signer_address()) for arg in args]
}), self.address, self.get_signer_address()) for arg in args]

tx = self.__abi_module.mint_nft_batch.build_transaction(
to_address, uris, self.get_transact_opts())
Expand All @@ -125,7 +125,7 @@ def burn(self, token_id: int):
"""
tx = self.__abi_module.burn.build_transaction(
token_id,
self.__get_transact_opts()
self.get_transact_opts()
)
self.execute_tx(tx)

Expand All @@ -137,7 +137,7 @@ def transfer_from(self, from_address: str, to_address: str, token_id: int):
from_address,
to_address,
token_id,
self.__get_transact_opts()
self.get_transact_opts()
)
self.execute_tx(tx)

Expand All @@ -146,10 +146,10 @@ def transfer(self, to_address: str, token_id: int):
Transfers NFT from the current signers wallet to another wallet
"""
tx = self.__abi_module.safe_transfer_from1.build_transaction(
self.__get_signer_address(),
self.get_signer_address(),
to_address,
token_id,
self.__get_transact_opts()
self.get_transact_opts()
)
self.execute_tx(tx)

Expand All @@ -175,7 +175,7 @@ def get_owned(self, address: str = "") -> List[NftType]:
if the address parameter is not supplied
"""
if address == "":
address = self.__get_signer_address()
address = self.get_signer_address()

balance = self.__abi_module.balance_of.call(address)
owned_tokens = [self.__token_of_owner_by_index(
Expand Down Expand Up @@ -225,9 +225,9 @@ def set_approval(self, operator: str, approved: bool = True):
"""
Sets approval for specified operator, defaults to grant approval
"""
self.execute_tx(self.__abi_module.set_approval_for_all.call(
operator, approved, self.__get_transact_opts()
))
tx = self.__abi_module.set_approval_for_all.build_transaction(operator, approved, self.get_transact_opts() )
self.execute_tx(tx)


def grant_role(self, role: Role, address: str):
"""
Expand All @@ -248,12 +248,12 @@ def revoke_role(self, role: Role, address: str):
"""
role_hash = role.get_hash()
self.execute_tx(self.__abi_module.revoke_role.build_transaction(
role_hash, address, self.__get_transact_opts()
role_hash, address, self.get_transact_opts()
))

def set_restricted_transfer(self, restricted: bool = True):
self.execute_tx(self.__abi_module.set_restricted_transfer.build_transaction(
restricted, self.__get_transact_opts()
restricted, self.get_transact_opts()
))

def get_with_owner(self, token_id: int, owner: str):
Expand Down Expand Up @@ -293,7 +293,7 @@ def get_role_members(self, role: Role):
"""
Returns the members of the given role
"""
return [self.get_role_member(role, x) for x in range(stop=self.get_role_member_count(role))]
return [self.get_role_member(role, x) for x in range(self.get_role_member_count(role))]

def get_role_member(self, role: Role, index: int):
"""
Expand All @@ -306,8 +306,8 @@ def get_all_role_members(self):
Returns all the members of all the roles
"""
return {
admin: [self.get_role_members(Role.admin) for admin in self.get_role_members(Role.admin)],
transfer: [self.get_role_members(Role.transfer) for transfer in self.get_role_members(Role.transfer)],
minter: [self.get_role_members(Role.minter) for minter in self.get_role_members(Role.minter)],
pauser: [self.get_role_members(Role.pauser) for pauser in self.get_role_members(Role.pauser)],
"admin": [item for item in self.get_role_members(Role.admin)],
"transfer": [item for item in self.get_role_members(Role.transfer)],
"minter": [item for item in self.get_role_members(Role.minter)],
"pauser": [item for item in self.get_role_members(Role.pauser)],
}
6 changes: 2 additions & 4 deletions nftlabs/modules/pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
from ..types.nft import NftMetadata
from ..types.currency import Currency, CurrencyValue
from ..abi.pack import Pack
from .base import _BaseModule
from .base import BaseModule
from ..abi.erc20 import ERC20
from web3 import Web3
from typing import List, Dict


class PackModule(_BaseModule):
class PackModule(BaseModule):
address: str
__abi_module: Pack

Expand All @@ -23,9 +23,7 @@ def get(self, pack_id: int) -> PackMetadata:
uri = self.__abi_module.token_uri.call(pack_id)
if uri == "":
raise AssetNotFoundException(pack_id)
print("uri = ", uri)
metadata = self.get_storage().get(uri)
print("pack metadata -", metadata)
return None

def open(self, pack_id: int) -> List[NftMetadata]:
Expand Down
Loading