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.

Commit a3c265b

Browse files
authored
Merge pull request #3 from nftlabs/ayush-examples
2 parents be989e2 + 25abf34 commit a3c265b

File tree

12 files changed

+124
-113
lines changed

12 files changed

+124
-113
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ nftlabs/.DS_Store
1414
nftlabs/modules/docs/*
1515
nftlabs/modules/.DS_Store
1616
.DS_Store
17+
x.sh
18+
examples/test.py

nftlabs/modules/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
"""All Modules"""
2-
from .currency import *
32

43
from .nft import *
54
from .nft_types import *
6-
from .currency_types import *
75
from .currency import *
86
from .market import *
97
from .market_types import *
108
from .pack import *
11-
from .pack_types import *
9+
from .collection import *

nftlabs/modules/collection.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from zero_ex.contract_wrappers import TxParams
44

5-
from .base import _BaseModule
5+
from .base import BaseModule
66
from ..types.metadata import Metadata
77
from ..types.nft import NftMetadata
88
from ..errors import NoSignerException
@@ -13,7 +13,7 @@
1313
from ..types.collection import CollectionMetadata, CreateCollectionArg, MintCollectionArg
1414

1515

16-
class CollectionModule(_BaseModule):
16+
class CollectionModule(BaseModule):
1717
address: str
1818
__abi_module: NFTCollection
1919

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

116116
def mint_batch_to(self, to_address, args: List[MintCollectionArg]):
117-
ids = [a.id for a in args]
117+
ids = [a.token_id for a in args]
118118
amounts = [a.amount for a in args]
119-
self.execute_tx(self.__abi_module.mint_batch.build_transaction(
120-
to_address, ids, amounts, self.get_transact_opts()
121-
))
119+
tx = self.__abi_module.mint_batch.build_transaction(
120+
to_address, ids, amounts, self.get_transact_opts())
121+
self.execute_tx(tx)
122122

123123
def burn(self, args: MintCollectionArg):
124124
self.burn_from(self.get_signer_address(), args)

nftlabs/modules/currency.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from ..errors import NoSignerException
33
from ..types import Role
44
from ..abi.coin import Coin
5-
from .base import _BaseModule
5+
from .base import BaseModule
66
from ..types.currency import Currency, CurrencyValue
77
from ..abi.erc20 import ERC20
88
from web3 import Web3
@@ -44,7 +44,7 @@ def balance_of(self, address: str) -> int:
4444
"""
4545
return self.__abi_module.balance_of.call(address)
4646

47-
def balance(self):
47+
def balance(self) -> int:
4848
"""
4949
Gets the balance of the current address
5050
"""

nftlabs/modules/market.py

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
from . import BaseModule
55
from ..types import Role
66
from ..abi.market import Market
7-
from ..types.market import ListArg, Filter
7+
from ..types.market import ListArg, Filter, MarketListing
88
from ..types.listing import Listing
99
from ..abi.erc20 import ERC20
1010
from ..abi.erc165 import ERC165
1111
from ..abi.erc1155 import ERC1155
1212
from ..abi.nft import NFT
13-
13+
nulladdress = "0x0000000000000000000000000000000000000000"
1414

1515
class MarketModule(BaseModule):
1616
"""
@@ -21,8 +21,7 @@ class MarketModule(BaseModule):
2121
Address of the market contract.
2222
"""
2323
__abi_module: Market
24-
25-
def __init__(self, client: Web3, address: str):
24+
def __init__(self, address: str, client: Web3, ):
2625
"""
2726
Initialize the Market Module.
2827
"""
@@ -31,38 +30,30 @@ def __init__(self, client: Web3, address: str):
3130
self.address = address
3231
self.__abi_module = Market(client, address)
3332

34-
#todo: return types
3533
def list(self, arg: ListArg):
3634
"""
3735
List an asset for sale.
3836
"""
3937
from_address = self.get_signer_address()
4038
client = self.get_client()
4139
erc165 = ERC165(client, arg.asset_contract)
42-
isERC721 = erc165.supports_interface(
43-
client, interface_id=bytearray.fromhex("80ac58cd"))
40+
isERC721 = erc165.supports_interface.call( bytearray.fromhex("80ac58cd"))
4441
if isERC721:
4542
asset = NFT(client, arg.asset_contract)
46-
approved = asset.is_approved_for_all(
47-
from_address, self.address)
43+
approved = asset.is_approved_for_all.call( from_address, self.address)
4844
if not approved:
49-
asset.is_approve_for_all(from_address, arg.asset_contract)
50-
is_token_approved = (asset.is_approved_for_all(
51-
arg.token_id).lower() == self.address.lower())
45+
is_token_approved = asset.get_approved.call(arg.token_id).lower() == self.address.lower()
5246
if not is_token_approved:
53-
asset.set_approval_for_all(arg.asset_contract, True)
47+
self.execute_tx(asset.set_approval_for_all.build_transaction(self.address, True, self.get_transact_opts()))
48+
5449
else:
5550
asset = ERC1155(client, arg.asset_contract)
56-
approved = asset.is_approved_for_all(from_address, self.address)
51+
approved = asset.is_approved_for_all.call(from_address, self.address)
5752

5853
if not approved:
59-
asset.set_approval_for_all(from_address, arg.asset_contract)
60-
is_token_approved = (asset.get_approved(
61-
arg.token_id).lower() == self.address.lower())
62-
if not is_token_approved:
63-
asset.set_approval_for_all(self.address, True)
54+
asset.set_approval_for_all.call(self.address, True)
6455

65-
tx = self.__abi_module.list.build_transaction(
56+
tx = self.__abi_module._list.build_transaction(
6657
arg.asset_contract,
6758
arg.token_id,
6859
arg.currency_contract,
@@ -100,19 +91,19 @@ def buy(self, listing_id: int, quantity: int):
10091
"""
10192
Buy a listing.
10293
"""
103-
listing = get(listing_id)
94+
item = self.get(listing_id)
10495
owner = self.get_signer_address()
10596
spender = self.address
106-
total_price = listing.price_per_token * quantity
107-
if listing.currency_contract is not None and listing.currency_contract != "0x0000000000000000000000000000000000000000":
108-
erc20 = ERC20(self.get_client(), listing.currency_contract)
109-
allowance = erc20.allowance(owner, spender)
97+
total_price = item.pricePerToken * quantity
98+
if item.currency is not None and item.currency != nulladdress:
99+
erc20 = ERC20(self.get_client(), item.currency)
100+
allowance = erc20.allowance.call(owner, spender)
110101
if allowance < total_price:
111-
erc20.increase_allowance(
112-
spender,
102+
tx = erc20.increase_allowance.build_transaction( spender,
113103
total_price,
114-
self.get_transact_opts()
115-
)
104+
self.get_transact_opts())
105+
self.execute_tx(tx)
106+
116107
tx = self.__abi_module.buy.build_transaction(
117108
listing_id,
118109
quantity,
@@ -131,7 +122,6 @@ def set_market_fee_bps(self, amount: int):
131122
self.get_transact_opts())
132123
self.execute_tx(tx)
133124

134-
def get(self, listing_id) -> List:
135125
"""
136126
Get a listing.
137127
"""
@@ -142,6 +132,12 @@ def get_all_listings(self, search_filter: Filter = None) -> List[Listing]:
142132
Returns all the listings.
143133
"""
144134
return self.get_all(search_filter)
135+
136+
def get(self, listing_id) -> MarketListing:
137+
"""
138+
Get a listing.
139+
"""
140+
return MarketListing(**self.__abi_module.get_listing.call(listing_id))
145141

146142
def set_module_metadata(metadata: str):
147143
"""
@@ -183,4 +179,4 @@ def total_listings(self) -> int:
183179
"""
184180
Returns the total supply of the market.
185181
"""
186-
self.__abi_module.total_listings.call()
182+
return self.__abi_module.total_listings.call()

nftlabs/modules/market_types.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

nftlabs/modules/nft.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
from web3 import Web3
77
from zero_ex.contract_wrappers import TxParams
88

9-
from .base import _BaseModule
9+
from .base import BaseModule
1010
from typing import Dict, List
1111
from ..abi.nft import NFT
1212

1313
from ..types import Role
1414
from ..types.nft import MintArg, NftMetadata as NftType
1515

1616

17-
class NftModule(_BaseModule):
17+
class NftModule(BaseModule):
1818
"""
1919
NFT Methods
2020
"""
@@ -56,8 +56,8 @@ def mint_to(
5656
'properties': final_properties
5757
}
5858

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

112112
tx = self.__abi_module.mint_nft_batch.build_transaction(
113113
to_address, uris, self.get_transact_opts())
@@ -125,7 +125,7 @@ def burn(self, token_id: int):
125125
"""
126126
tx = self.__abi_module.burn.build_transaction(
127127
token_id,
128-
self.__get_transact_opts()
128+
self.get_transact_opts()
129129
)
130130
self.execute_tx(tx)
131131

@@ -137,7 +137,7 @@ def transfer_from(self, from_address: str, to_address: str, token_id: int):
137137
from_address,
138138
to_address,
139139
token_id,
140-
self.__get_transact_opts()
140+
self.get_transact_opts()
141141
)
142142
self.execute_tx(tx)
143143

@@ -146,10 +146,10 @@ def transfer(self, to_address: str, token_id: int):
146146
Transfers NFT from the current signers wallet to another wallet
147147
"""
148148
tx = self.__abi_module.safe_transfer_from1.build_transaction(
149-
self.__get_signer_address(),
149+
self.get_signer_address(),
150150
to_address,
151151
token_id,
152-
self.__get_transact_opts()
152+
self.get_transact_opts()
153153
)
154154
self.execute_tx(tx)
155155

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

180180
balance = self.__abi_module.balance_of.call(address)
181181
owned_tokens = [self.__token_of_owner_by_index(
@@ -225,9 +225,9 @@ def set_approval(self, operator: str, approved: bool = True):
225225
"""
226226
Sets approval for specified operator, defaults to grant approval
227227
"""
228-
self.execute_tx(self.__abi_module.set_approval_for_all.call(
229-
operator, approved, self.__get_transact_opts()
230-
))
228+
tx = self.__abi_module.set_approval_for_all.build_transaction(operator, approved, self.get_transact_opts() )
229+
self.execute_tx(tx)
230+
231231

232232
def grant_role(self, role: Role, address: str):
233233
"""
@@ -248,12 +248,12 @@ def revoke_role(self, role: Role, address: str):
248248
"""
249249
role_hash = role.get_hash()
250250
self.execute_tx(self.__abi_module.revoke_role.build_transaction(
251-
role_hash, address, self.__get_transact_opts()
251+
role_hash, address, self.get_transact_opts()
252252
))
253253

254254
def set_restricted_transfer(self, restricted: bool = True):
255255
self.execute_tx(self.__abi_module.set_restricted_transfer.build_transaction(
256-
restricted, self.__get_transact_opts()
256+
restricted, self.get_transact_opts()
257257
))
258258

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

298298
def get_role_member(self, role: Role, index: int):
299299
"""
@@ -306,8 +306,8 @@ def get_all_role_members(self):
306306
Returns all the members of all the roles
307307
"""
308308
return {
309-
admin: [self.get_role_members(Role.admin) for admin in self.get_role_members(Role.admin)],
310-
transfer: [self.get_role_members(Role.transfer) for transfer in self.get_role_members(Role.transfer)],
311-
minter: [self.get_role_members(Role.minter) for minter in self.get_role_members(Role.minter)],
312-
pauser: [self.get_role_members(Role.pauser) for pauser in self.get_role_members(Role.pauser)],
309+
"admin": [item for item in self.get_role_members(Role.admin)],
310+
"transfer": [item for item in self.get_role_members(Role.transfer)],
311+
"minter": [item for item in self.get_role_members(Role.minter)],
312+
"pauser": [item for item in self.get_role_members(Role.pauser)],
313313
}

nftlabs/modules/pack.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
from ..types.nft import NftMetadata
55
from ..types.currency import Currency, CurrencyValue
66
from ..abi.pack import Pack
7-
from .base import _BaseModule
7+
from .base import BaseModule
88
from ..abi.erc20 import ERC20
99
from web3 import Web3
1010
from typing import List, Dict
1111

1212

13-
class PackModule(_BaseModule):
13+
class PackModule(BaseModule):
1414
address: str
1515
__abi_module: Pack
1616

@@ -23,9 +23,7 @@ def get(self, pack_id: int) -> PackMetadata:
2323
uri = self.__abi_module.token_uri.call(pack_id)
2424
if uri == "":
2525
raise AssetNotFoundException(pack_id)
26-
print("uri = ", uri)
2726
metadata = self.get_storage().get(uri)
28-
print("pack metadata -", metadata)
2927
return None
3028

3129
def open(self, pack_id: int) -> List[NftMetadata]:

0 commit comments

Comments
 (0)