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 f4ec4fc

Browse files
committed
collection -> bundle
1 parent eb90910 commit f4ec4fc

File tree

14 files changed

+311
-21
lines changed

14 files changed

+311
-21
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ $ pip install thirdweb-sdk
3333
nftlabs
3434
├── abi // contains autogenerated ABI contract wrappers
3535
├── errors // commonly thrown errors
36-
├── modules // NFT, Currency, Marketplace, Pack, Collection, etc modules
36+
├── modules // NFT, Currency, Marketplace, Pack, Bundle, etc modules
3737
├── options // Options classes used throughout the SDK
3838
├── sdk.py // NftlabsSdk class, wrapper for the entire package
3939
├── storage // Distributed file storage helper classes

tests/test_constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
TEST_NFT_CONTRACT_ADDRESS = "0xEeD541b524Ae738c48211Be91EB81E97739A0A29"
22
TEST_PACK_CONTRACT_ADDRESS = "0x54ec360704b2e9E4e6499a732b78094D6d78e37B"
33
TEST_MARKET_CONTRACT_ADDRESS = "0x325a98B6081ef88C6356d63c56f48Fa1d0d2DD0D"
4-
TEST_COLLECTION_CONTRACT_ADDRESS = "0x6Da734b14e4CE604f1e18efb7E7f7ef022e96616"
4+
TEST_BUNDLE_CONTRACT_ADDRESS = "0x6Da734b14e4CE604f1e18efb7E7f7ef022e96616"
55
TEST_CURRENCY_CONTRACT_ADDRESS = "0xF18FEb8b2F58691d67C98dB98B360840df340e74"
66

77
TEST_COMPANION_WALLET_ADDRESS = "0x4d36d531D9cB40b8694763123D52170FAE5e1195"

tests/test_import.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ def test_init_currency_module(self):
2424
sdk = NftlabsSdk(SdkOptions(), "https://rpc-mumbai.maticvigil.com")
2525
currency_module = sdk.get_currency_module("0xF18FEb8b2F58691d67C98dB98B360840df340e74")
2626

27-
def test_init_collection_module(self):
27+
28+
def test_init_bundle_module(self):
2829
"""
29-
Test that tries to instantiate the Currency module
30+
Test that tries to instantiate the Bundle module
3031
"""
3132
sdk = NftlabsSdk(SdkOptions(), "https://rpc-mumbai.maticvigil.com")
32-
collection_module = sdk.get_collection_module("0x6Da734b14e4CE604f1e18efb7E7f7ef022e96616")
33+
bundle_module = sdk.get_bundle_module("0x6Da734b14e4CE604f1e18efb7E7f7ef022e96616")
3334

3435
def test_init_pack_module(self):
3536
"""

tests/test_market.py

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

33
from nftlabs import NftlabsSdk, SdkOptions
44

5-
from test_constants import (TEST_COLLECTION_CONTRACT_ADDRESS,
5+
from test_constants import (TEST_BUNDLE_CONTRACT_ADDRESS,
66
TEST_MARKET_CONTRACT_ADDRESS,
77
TEST_NFT_CONTRACT_ADDRESS)
88

@@ -16,11 +16,11 @@ def test_init_marketplace_module(self):
1616
market_module = sdk.get_market_module(TEST_MARKET_CONTRACT_ADDRESS)
1717

1818
self.assertFalse(market_module.is_erc721(
19-
TEST_COLLECTION_CONTRACT_ADDRESS), "A collection is not a 721 contract")
19+
TEST_BUNDLE_CONTRACT_ADDRESS), "A bundle is not a 721 contract")
2020
self.assertTrue(market_module.is_erc721(
2121
TEST_NFT_CONTRACT_ADDRESS), "A nft contract is a 721 contract")
2222

2323
self.assertTrue(market_module.is_erc1155(
24-
TEST_COLLECTION_CONTRACT_ADDRESS), "A collection is a 1155 contract")
24+
TEST_BUNDLE_CONTRACT_ADDRESS), "A bundle is a 1155 contract")
2525
self.assertFalse(market_module.is_erc1155(
2626
TEST_NFT_CONTRACT_ADDRESS), "A nft contract is not a 1155 contract")

tests/test_roles.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from nftlabs import NftlabsSdk, SdkOptions
44
from nftlabs.types.role import Role
55
from os import environ
6-
from test_constants import TEST_COLLECTION_CONTRACT_ADDRESS, TEST_CURRENCY_CONTRACT_ADDRESS, TEST_MARKET_CONTRACT_ADDRESS, TEST_NFT_CONTRACT_ADDRESS, TEST_COMPANION_WALLET_ADDRESS, TEST_PACK_CONTRACT_ADDRESS
6+
from test_constants import TEST_BUNDLE_CONTRACT_ADDRESS, TEST_CURRENCY_CONTRACT_ADDRESS, TEST_MARKET_CONTRACT_ADDRESS, TEST_NFT_CONTRACT_ADDRESS, TEST_COMPANION_WALLET_ADDRESS, TEST_PACK_CONTRACT_ADDRESS
77

88

99
class TestRoles(unittest.TestCase):
@@ -20,8 +20,8 @@ def test_grant_and_revoke_role(self):
2020
TEST_NFT_CONTRACT_ADDRESS),
2121
sdk.get_market_module(
2222
TEST_MARKET_CONTRACT_ADDRESS),
23-
sdk.get_collection_module(
24-
TEST_COLLECTION_CONTRACT_ADDRESS),
23+
sdk.get_bundle_module(
24+
TEST_BUNDLE_CONTRACT_ADDRESS),
2525
sdk.get_pack_module(
2626
TEST_PACK_CONTRACT_ADDRESS),
2727
sdk.get_currency_module(

thirdweb/abi/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
from .pack import *
55

66
from .nft_collection import *
7+
#todo?

thirdweb/modules/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
from .market import *
77
from .pack import *
88
from .collection import *
9+
from .bundle import *

thirdweb/modules/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
from ..abi.erc165 import ERC165
1313
from ..abi.market import Market
1414
from ..abi.nft import NFT
15-
from ..abi.nft_collection import NFTCollection
15+
from ..abi.nft_collection import NFTCollection as NFTBundle
1616
from ..abi.pack import Pack
1717
from ..constants.erc_interfaces import InterfaceIdErc721, InterfaceIdErc1155
1818
from ..errors import NoSignerException
1919
from ..options import SdkOptions
2020
from ..storage import IpfsStorage
2121
from ..types.role import Role
2222

23-
ModuleTypes = Union[NFT, Market, Pack, NFTCollection, Coin]
23+
ModuleTypes = Union[NFT, Market, Pack, NFTBundle, Coin]
2424

2525

2626
class BaseModule(ABC):

thirdweb/modules/bundle.py

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
from typing import List
2+
3+
from thirdweb_web3 import Web3
4+
5+
from ..abi.nft_collection import NFTCollection as NFTBundle
6+
# from ..types.collection import (BundleMetadata, CreateBundleArg,
7+
# MintBundleArg)
8+
from ..types.bundle import (BundleMetadata, CreateBundleArg, MintBundleArg)
9+
from ..types.metadata import Metadata
10+
from ..types.nft import NftMetadata
11+
from .base import BaseModule
12+
13+
14+
class BundleModule(BaseModule):
15+
address: str
16+
__abi_module: NFTBundle
17+
18+
def __init__(self, address: str, client: Web3):
19+
super().__init__()
20+
self.address = address
21+
self.__abi_module = NFTBundle(client, address)
22+
23+
def get(self, token_id: int) -> BundleMetadata:
24+
uri = self.__abi_module.uri.call(token_id)
25+
meta_str = self.get_storage().get(uri)
26+
meta: NftMetadata = NftMetadata.from_json(meta_str)
27+
meta.id = token_id
28+
return BundleMetadata(
29+
metadata=meta,
30+
supply=self.__abi_module.total_supply.call(token_id),
31+
creator=self.__abi_module.creator.call(token_id),
32+
id=token_id
33+
)
34+
35+
def get_all(self) -> List[BundleMetadata]:
36+
return [self.get(i) for i in range(self.__abi_module.next_token_id.call())]
37+
38+
'''
39+
Returns the balance for a given token at owned by a specific address
40+
'''
41+
42+
def balance_of(self, address: str, token_id: int) -> int:
43+
return self.__abi_module.balance_of.call(address, token_id)
44+
45+
'''
46+
Returns the balance for a given token id for the current signers address
47+
'''
48+
49+
def balance(self, token_id: int) -> int:
50+
return self.__abi_module.balance_of.call(
51+
self.get_signer_address(),
52+
token_id
53+
)
54+
55+
def is_approved(self, address: str, operator: str) -> bool:
56+
return self.__abi_module.is_approved_for_all.call(
57+
address,
58+
operator
59+
)
60+
61+
def set_approval(self, operator: str, approved: bool = True):
62+
self.execute_tx(self.__abi_module.set_approval_for_all.build_transaction(
63+
operator, approved, self.get_transact_opts()
64+
))
65+
66+
def transfer(self, to_address: str, token_id: int, amount: int):
67+
self.execute_tx(self.__abi_module.safe_transfer_from.build_transaction(
68+
self.get_signer_address(), to_address, token_id, amount, "", self.get_transact_opts()
69+
))
70+
71+
def create(self, metadata: Metadata) -> BundleMetadata:
72+
return self.create_batch([metadata])[0]
73+
74+
def create_batch(self, metas: List[Metadata]) -> List[BundleMetadata]:
75+
meta_with_supply = [CreateBundleArg(
76+
metadata=m, supply=0) for m in metas]
77+
return self.create_and_mint_batch(meta_with_supply)
78+
79+
def create_and_mint(self, meta_with_supply: CreateBundleArg) -> BundleMetadata:
80+
return self.create_and_mint_batch([meta_with_supply])[0]
81+
82+
def create_and_mint_batch(self, meta_with_supply: List[CreateBundleArg]) -> List[BundleMetadata]:
83+
uris = [self.get_storage().upload(meta.to_json(), self.address,
84+
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(
90+
tx_hash=receipt.transactionHash.hex())
91+
token_ids = result[0]['args']['tokenIds']
92+
return [self.get(i) for i in token_ids]
93+
94+
def create_with_erc20(self, token_contract: str, token_amount: int, arg: CreateBundleArg):
95+
uri = self.get_storage().upload(
96+
arg.metadata, self.address, self.get_signer_address())
97+
self.execute_tx(self.__abi_module.wrap_erc20.build_transaction(
98+
token_contract, token_amount, arg.supply, uri, self.get_transact_opts()
99+
))
100+
101+
def create_with_erc721(self, token_contract: str, token_id: int, metadata):
102+
uri = self.get_storage().upload(
103+
metadata.metadata, self.address, self.get_signer_address())
104+
self.execute_tx(self.__abi_module.wrap_erc721.build_transaction(
105+
token_contract, token_id, uri, self.get_transact_opts()
106+
))
107+
108+
def mint(self, args: MintBundleArg):
109+
self.mint_to(self.get_signer_address(), args)
110+
111+
def mint_to(self, to_address: str, arg: MintBundleArg):
112+
self.execute_tx(self.__abi_module.mint.build_transaction(
113+
to_address, arg.token_id, arg.amount, "", self.get_transact_opts()
114+
))
115+
116+
def mint_batch(self, args: List[MintBundleArg]):
117+
self.mint_batch_to(self.get_signer_address(), args)
118+
119+
def mint_batch_to(self, to_address, args: List[MintBundleArg]):
120+
ids = [a.token_id for a in args]
121+
amounts = [a.amount for a in args]
122+
tx = self.__abi_module.mint_batch.build_transaction(
123+
to_address, ids, amounts, self.get_transact_opts())
124+
self.execute_tx(tx)
125+
126+
def burn(self, args: MintBundleArg):
127+
self.burn_from(self.get_signer_address(), args)
128+
129+
def burn_batch(self, args: List[MintBundleArg]):
130+
self.burn_batch_from(self.get_signer_address(), args)
131+
132+
def burn_from(self, account: str, args: MintBundleArg):
133+
self.execute_tx(self.__abi_module.burn.build_transaction(
134+
account, args.token_id, args.amount, self.get_transact_opts()
135+
))
136+
137+
def burn_batch_from(self, account: str, args: List[MintBundleArg]):
138+
self.execute_tx(self.__abi_module.burn_batch.build_transaction(
139+
account, [i.id for i in args], [
140+
i.amount for i in args], self.get_transact_opts()
141+
))
142+
143+
def transfer_from(self, from_address: str, to_address: str, args: MintBundleArg):
144+
self.execute_tx(self.__abi_module.safe_transfer_from.build_transaction(
145+
from_address, to_address, args.token_id, args.amount, "", self.get_transact_opts()
146+
))
147+
148+
def transfer_batch_from(self, from_address: str, to_address: str, args):
149+
self.execute_tx(self.__abi_module.safe_batch_transfer_from.build_transaction(
150+
from_address, to_address, args.token_id, args.amount, "", self.get_transact_opts()
151+
))
152+
153+
def set_royalty_bps(self, amount: int):
154+
self.execute_tx(self.__abi_module.set_royalty_bps.build_transaction(
155+
amount, self.get_transact_opts()
156+
))
157+
158+
def get_abi_module(self) -> NFTBundle:
159+
return self.__abi_module

0 commit comments

Comments
 (0)