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 44f79c8

Browse files
committed
Make private key methods public again until I can figure out a way to wrap them properly, implemented create/create batch/create & mint batch methods in collection module
1 parent dc8fac2 commit 44f79c8

File tree

9 files changed

+92
-65
lines changed

9 files changed

+92
-65
lines changed

nftlabs/modules/base.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,28 @@
1010
from zero_ex.contract_wrappers import TxParams
1111

1212

13-
class BaseModule:
13+
class _BaseModule:
1414
get_client: Optional[Callable[[], Web3]]
1515
get_storage: Optional[Callable[[], IpfsStorage]]
16-
__get_signer_address: Optional[Callable[[], str]]
17-
__get_private_key: Optional[Callable[[], str]]
18-
__get_transact_opts: Optional[Callable[[], TxParams]]
16+
get_signer_address: Optional[Callable[[], str]]
17+
get_private_key: Optional[Callable[[], str]]
18+
get_transact_opts: Optional[Callable[[], TxParams]]
1919

20-
__get_account: Optional[Callable[[], LocalAccount]]
20+
get_account: Optional[Callable[[], LocalAccount]]
2121
get_options: Optional[Callable[[], SdkOptions]]
2222

2323
def __init__(self):
2424
self.get_client = None
2525
self.get_storage = None
26-
self.__get_signer_address = None
27-
self.__get_private_key = None
28-
self.__get_transact_opts = None
29-
self.__get_account = None
26+
self.get_signer_address = None
27+
self.get_private_key = None
28+
self.get_transact_opts = None
29+
self.get_account = None
3030
self.get_options = None
3131

3232
def execute_tx(self, tx) -> TxReceipt:
3333
client = self.get_client()
34-
nonce = client.eth.get_transaction_count(self.__get_signer_address())
34+
nonce = client.eth.get_transaction_count(self.get_signer_address())
3535
tx['nonce'] = nonce
3636
del tx['from']
3737
signed_tx = self.__sign_tx(tx)
@@ -42,7 +42,7 @@ def execute_tx(self, tx) -> TxReceipt:
4242
)
4343

4444
def __sign_tx(self, tx):
45-
signed_tx = self.__get_account().sign_transaction(tx)
45+
signed_tx = self.get_account().sign_transaction(tx)
4646
return signed_tx
4747

4848

nftlabs/modules/collection.py

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22

33
from zero_ex.contract_wrappers import TxParams
44

5-
from .base import BaseModule
5+
from .base import _BaseModule
6+
from ..types.metadata import Metadata
67
from ..types.nft import NftMetadata
78
from ..errors import NoSignerException
89
from ..types.role import Role
910
from ..abi.nft_collection import NFTCollection
1011
from web3 import Web3
1112

12-
from ..types.collection import CollectionMetadata
13+
from ..types.collection import CollectionMetadata, CreateCollectionArg
1314

1415

15-
class CollectionModule(BaseModule):
16+
class CollectionModule(_BaseModule):
1617
address: str
1718
__abi_module: NFTCollection
1819

@@ -47,7 +48,7 @@ def balance_of(self, address: str, token_id: int) -> int:
4748
'''
4849
def balance(self, token_id: int) -> int:
4950
return self.__abi_module.balance_of.call(
50-
self.__get_signer_address(),
51+
self.get_signer_address(),
5152
token_id,
5253
TxParams(from_=self.address)
5354
)
@@ -61,22 +62,33 @@ def is_approved(self, address: str, operator: str) -> bool:
6162

6263
def set_approval(self, operator: str, approved: bool = True):
6364
self.execute_tx(self.__abi_module.set_approval_for_all.build_transaction(
64-
operator, approved, self.__get_transact_opts()
65+
operator, approved, self.get_transact_opts()
6566
))
6667

6768
def transfer(self, to_address: str, token_id: int, amount: int):
6869
self.execute_tx(self.__abi_module.safe_transfer_from.build_transaction(
69-
self.__get_signer_address(), to_address, token_id, amount, "", self.__get_transact_opts()
70+
self.get_signer_address(), to_address, token_id, amount, "", self.get_transact_opts()
7071
))
7172

72-
def create(self, metadata) -> CollectionMetadata:
73-
pass
73+
def create(self, metadata: Metadata) -> CollectionMetadata:
74+
return self.create_batch([metadata])[0]
7475

75-
def create_batch(self, metas) -> List[CollectionMetadata]:
76-
pass
76+
def create_batch(self, metas: List[Metadata]) -> List[CollectionMetadata]:
77+
meta_with_supply = [CreateCollectionArg(metadata=m, supply=0) for m in metas]
78+
return self.create_and_mint_batch(meta_with_supply)
7779

78-
def create_and_mint(self, meta_with_supply) -> List[CollectionMetadata]:
79-
pass
80+
def create_and_mint(self, meta_with_supply: CreateCollectionArg) -> CollectionMetadata:
81+
return self.create_and_mint_batch([meta_with_supply])[0]
82+
83+
def create_and_mint_batch(self, meta_with_supply: List[CreateCollectionArg]) -> List[CollectionMetadata]:
84+
uris = [self.get_storage().upload(meta.to_json(), self.address, self.get_signer_address()) for meta in meta_with_supply]
85+
supplies = [a.supply for a in meta_with_supply]
86+
receipt = self.execute_tx(self.__abi_module.create_native_tokens.build_transaction(
87+
self.get_signer_address(), uris, supplies, "", self.get_transact_opts()
88+
))
89+
result = self.__abi_module.get_native_tokens_event(tx_hash=receipt.transactionHash.hex())
90+
token_ids = result[0]['args']['tokenIds']
91+
return [self.get(i) for i in token_ids]
8092

8193
def create_with_erc20(self, token_contract: str, token_amount: int, metadata):
8294
pass
@@ -116,31 +128,31 @@ def transfer_batch_from(self, from_address: str, to_address: str, args):
116128

117129
def set_royalty_bps(self, amount: int):
118130
self.execute_tx(self.__abi_module.set_royalty_bps.build_transaction(
119-
amount, self.__get_transact_opts()
131+
amount, self.get_transact_opts()
120132
))
121133

122134
def grant_role(self, role: Role, address: str):
123135
role_hash = role.get_hash()
124136
self.execute_tx(self.__abi_module.grant_role.build_transaction(
125-
role_hash, address, self.__get_transact_opts()
137+
role_hash, address, self.get_transact_opts()
126138
))
127139

128140
def revoke_role(self, role: Role, address: str):
129141
role_hash = role.get_hash()
130142

131143
try:
132-
signer_address = self.__get_signer_address()
144+
signer_address = self.get_signer_address()
133145
if signer_address.lower() != address.lower():
134146
pass
135147
self.execute_tx(self.__abi_module.renounce_role.build_transaction(
136-
role_hash, address, self.__get_transact_opts()
148+
role_hash, address, self.get_transact_opts()
137149
))
138150
return
139151
except NoSignerException:
140152
pass
141153

142154
self.execute_tx(self.__abi_module.revoke_role.build_transaction(
143-
role_hash, address, self.__get_transact_opts()
155+
role_hash, address, self.get_transact_opts()
144156
))
145157

146158
def get_role_members(self, role: Role) -> List[str]:

nftlabs/modules/currency.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
from ..errors import NoSignerException
22
from ..types import Role
33
from ..abi.coin import Coin
4-
from .base import BaseModule
4+
from .base import _BaseModule
55
from ..types.currency import Currency, CurrencyValue
66
from ..abi.erc20 import ERC20
77
from web3 import Web3
88
from typing import List, Dict
99

1010

11-
class CurrencyModule(BaseModule):
11+
class CurrencyModule(_BaseModule):
1212
address: str
1313
__abi_module: Coin
1414

@@ -31,66 +31,66 @@ def balance_of(self, address: str) -> int:
3131
return self.__abi_module.balance_of.call(address)
3232

3333
def balance(self):
34-
return self.__abi_module.balance_of.call(self.__get_signer_address())
34+
return self.__abi_module.balance_of.call(self.get_signer_address())
3535

3636
def allowance(self, spender: str) -> int:
37-
return self.__abi_module.allowance.call(self.__get_signer_address(), spender)
37+
return self.__abi_module.allowance.call(self.get_signer_address(), spender)
3838

3939
def allowance_of(self, owner: str, spender: str) -> int:
4040
return self.__abi_module.allowance.call(owner, spender)
4141

4242
def set_allowance(self, spender: str, amount: int):
4343
return self.execute_tx(self.__abi_module.approve.build_transaction(
44-
spender, amount, self.__get_transact_opts()
44+
spender, amount, self.get_transact_opts()
4545
))
4646

4747
def mint_to(self, to: str, amount: int):
4848
return self.execute_tx(self.__abi_module.mint.build_transaction(
49-
to, amount, self.__get_transact_opts()
49+
to, amount, self.get_transact_opts()
5050
))
5151

5252
def mint(self, amount: int):
5353
return self.execute_tx(self.__abi_module.mint.build_transaction(
54-
self.__get_signer_address(), amount, self.__get_transact_opts()
54+
self.get_signer_address(), amount, self.get_transact_opts()
5555
))
5656

5757
def burn(self, amount: int):
5858
return self.execute_tx(self.__abi_module.burn.build_transaction(
59-
amount, self.__get_transact_opts()
59+
amount, self.get_transact_opts()
6060
))
6161

6262
def burn_from(self, from_address: str, amount: int):
6363
return self.execute_tx(self.__abi_module.burn_from.build_transaction(
64-
from_address, amount, self.__get_transact_opts()
64+
from_address, amount, self.get_transact_opts()
6565
))
6666

6767
def transfer_from(self, from_address: str, to_address: str, amount: int):
6868
return self.execute_tx(self.__abi_module.transfer_from.build_transaction(
69-
from_address, to_address, amount, self.__get_transact_opts()
69+
from_address, to_address, amount, self.get_transact_opts()
7070
))
7171

7272
def grant_role(self, role: Role, address: str):
7373
role_hash = role.get_hash()
7474
self.execute_tx(self.__abi_module.grant_role.build_transaction(
75-
role_hash, address, self.__get_transact_opts()
75+
role_hash, address, self.get_transact_opts()
7676
))
7777

7878
def revoke_role(self, role: Role, address: str):
7979
role_hash = role.get_hash()
8080

8181
try:
82-
signer_address = self.__get_signer_address()
82+
signer_address = self.get_signer_address()
8383
if signer_address.lower() != address.lower():
8484
pass
8585
self.execute_tx(self.__abi_module.renounce_role.build_transaction(
86-
role_hash, address, self.__get_transact_opts()
86+
role_hash, address, self.get_transact_opts()
8787
))
8888
return
8989
except NoSignerException:
9090
pass
9191

9292
self.execute_tx(self.__abi_module.revoke_role.build_transaction(
93-
role_hash, address, self.__get_transact_opts()
93+
role_hash, address, self.get_transact_opts()
9494
))
9595

9696
def get_role_members(self, role: Role) -> List[str]:

nftlabs/modules/nft.py

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

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

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

1515

16-
class NftModule(BaseModule):
16+
class NftModule(_BaseModule):
1717
address: str
1818
__abi_module: NFT
1919

nftlabs/modules/pack.py

Lines changed: 13 additions & 13 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

@@ -41,33 +41,33 @@ def balance_of(self, address: str, token_id: int) -> int:
4141
return self.__abi_module.balance_of.call(address, token_id)
4242

4343
def balance(self, token_id: int) -> int:
44-
return self.__abi_module.balance_of.call(self.__get_signer_address(), token_id)
44+
return self.__abi_module.balance_of.call(self.get_signer_address(), token_id)
4545

4646
def is_approved(self, address: str, operator: str) -> bool:
4747
return self.__abi_module.is_approved_for_all.call(address, operator)
4848

4949
def set_approval(self, operator: str, approved: bool):
5050
return self.execute_tx(self.__abi_module.set_approval_for_all.build_transaction(
51-
operator, approved, self.__get_transact_opts()
51+
operator, approved, self.get_transact_opts()
5252
))
5353

5454
def transfer(self, to_address: str, token_id: int, amount: int):
5555
return self.execute_tx(self.__abi_module.safe_transfer_from.build_transaction(
56-
self.__get_signer_address(), to_address, token_id, amount, "", self.__get_transact_opts(),
56+
self.get_signer_address(), to_address, token_id, amount, "", self.get_transact_opts(),
5757
))
5858

5959
def create(self, arg: CreatePackArg) -> PackMetadata:
6060
pass
6161

6262
def transfer_from(self, from_address: str, to_address: str, args: AssetAmountPair):
6363
return self.execute_tx(self.__abi_module.safe_transfer_from.build_transaction(
64-
from_address, to_address, args.token_id, args.amount, "", self.__get_transact_opts(),
64+
from_address, to_address, args.token_id, args.amount, "", self.get_transact_opts(),
6565
))
6666

6767
def transfer_batch_from(self, from_address: str, to_address: str, args: List[AssetAmountPair]):
6868
ids, amounts = [i.token_id for i in args], [i.amount for i in args]
6969
return self.execute_tx(self.__abi_module.safe_batch_transfer_from.build_transaction(
70-
from_address, to_address, ids, amounts, "", self.__get_transact_opts(),
70+
from_address, to_address, ids, amounts, "", self.get_transact_opts(),
7171
))
7272

7373
def get_link_balance(self) -> CurrencyValue:
@@ -81,36 +81,36 @@ def withdraw_link(self, to_address: str, amount: int):
8181

8282
def set_royalty_bps(self, amount: int):
8383
return self.execute_tx(self.__abi_module.set_royalty_bps.build_transaction(
84-
amount, self.__get_transact_opts()
84+
amount, self.get_transact_opts()
8585
))
8686

8787
def set_restricted_transfer(self, restricted: bool = True):
8888
return self.execute_tx(self.__abi_module.set_restricted_transfer.build_transaction(
89-
restricted, self.__get_transact_opts()
89+
restricted, self.get_transact_opts()
9090
))
9191

9292
def grant_role(self, role: Role, address: str):
9393
role_hash = role.get_hash()
9494
self.execute_tx(self.__abi_module.grant_role.build_transaction(
95-
role_hash, address, self.__get_transact_opts()
95+
role_hash, address, self.get_transact_opts()
9696
))
9797

9898
def revoke_role(self, role: Role, address: str):
9999
role_hash = role.get_hash()
100100

101101
try:
102-
signer_address = self.__get_signer_address()
102+
signer_address = self.get_signer_address()
103103
if signer_address.lower() != address.lower():
104104
pass
105105
self.execute_tx(self.__abi_module.renounce_role.build_transaction(
106-
role_hash, address, self.__get_transact_opts()
106+
role_hash, address, self.get_transact_opts()
107107
))
108108
return
109109
except NoSignerException:
110110
pass
111111

112112
self.execute_tx(self.__abi_module.revoke_role.build_transaction(
113-
role_hash, address, self.__get_transact_opts()
113+
role_hash, address, self.get_transact_opts()
114114
))
115115

116116
def get_role_members(self, role: Role) -> List[str]:

0 commit comments

Comments
 (0)