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: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ jobs:
python -m site
python -m pip install -r requirements.txt
- name: "Run tests targets for ${{ matrix.python-version }}"
run: "python3 -m unittest discover -s tests"
run: "python3 -m unittest discover -t ."
- name: "Test bundle package with setup.py ${{ matrix.python-version }}"
run: "python3 setup.py sdist"
43 changes: 29 additions & 14 deletions tests/test_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

from thirdweb import SdkOptions, ThirdwebSdk
from thirdweb.modules.bundle import BundleModule
from thirdweb.modules.collection import CollectionModule
from thirdweb.modules.currency import CurrencyModule
from thirdweb.modules.nft import NftModule

from .constants import (TEST_BUNDLE_CONTRACT_ADDRESS,
TEST_CURRENCY_CONTRACT_ADDRESS,
Expand All @@ -12,47 +13,61 @@

class TestRoles(unittest.TestCase):
sdk: ThirdwebSdk
module: BundleModule
old_module: CollectionModule
bundle_module: BundleModule
nft_module: NftModule
currency_module: CurrencyModule

@classmethod
def setUpClass(self):
self.sdk = ThirdwebSdk(SdkOptions(
private_key=environ['PKEY']
), "https://rpc-mumbai.maticvigil.com")
contract_address = TEST_BUNDLE_CONTRACT_ADDRESS
self.module = self.sdk.get_bundle_module(contract_address)
self.old_module = self.sdk.get_collection_module(contract_address)
self.bundle_module = self.sdk.get_bundle_module(
TEST_BUNDLE_CONTRACT_ADDRESS)
self.nft_module = self.sdk.get_nft_module(TEST_NFT_CONTRACT_ADDRESS)
self.currency_module = self.sdk.get_currency_module(
TEST_CURRENCY_CONTRACT_ADDRESS)

def test_bundle_get_all(self):
"""
Test that tries to instantiate the NFT module
"""
result = self.module.get_all()
result = self.bundle_module.get_all()
self.assertGreater(
len(result), 0, "There should be at least 1 token in the contract")

def test_collection_get_all(self):
"""
Test that tries to instantiate the Collection module
"""
result = self.old_module.get_all()
result = self.bundle_module.get_all()
self.assertGreater(
len(result), 0, "There should be at least 1 token in the contract")

def test_bundle_create(self):
"""
Test that tries to instantiate the Bundle module
"""
result = self.module.create({"name": "test"})
self.assertIsNotNone(result, "The result should not be None")
result = self.bundle_module.create({"name": "test"})
self.assertIsNotNone(result, "The resulting bundle should not be None")

def test_bundle_create_with_token(self):
# def test_bundle_create_with_token(self):
# """
# Test that tries to instantiate the Bundle module
# """
# self.currency_module.mint(20)
# result = self.bundle_module.create_with_token(
# TEST_CURRENCY_CONTRACT_ADDRESS, 20, {})
# self.assertIsNotNone(result, "The resulting bundle should not be None")
# self.assertEqual(result, 20)

def test_create(self):
"""
Test that tries to instantiate the Bundle module
Call create method to mint token with 0 supply
"""
result = self.module.create_with_token(
TEST_CURRENCY_CONTRACT_ADDRESS, 20, {})
created = self.bundle_module.create(metadata={"name": "test"})
self.assertEqual(created.metadata.name, "test")
self.assertEqual(created.supply, 0)

# def test_bundle_create_with_nft(self):
# """
Expand Down
74 changes: 38 additions & 36 deletions thirdweb/modules/bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

from thirdweb_web3 import Web3

from ..abi.nft_collection import NFTCollection as NFTBundle
# from ..types.collection import (BundleMetadata, CreateBundleArg,
# MintBundleArg)
from ..abi.erc20 import ERC20
from ..abi.nft import NFT
from ..abi.nft_collection import NFTCollection as NFTBundle
from ..constants import ZeroAddress

from ..types.bundle import (BundleMetadata, CreateBundleArg, MintBundleArg)
from ..types.bundle import BundleMetadata, CreateBundleArg, MintBundleArg
from ..types.metadata import Metadata
from ..types.nft import NftMetadata
from .base import BaseModule
from ..abi.nft import NFT


class BundleModule(BaseModule):
address: str
Expand All @@ -36,32 +34,28 @@ def get(self, token_id: int) -> BundleMetadata:
)

def get_all(self) -> List[BundleMetadata]:
'''
Returns all the bundles in the contract
'''
return [self.get(i) for i in range(self.__abi_module.next_token_id.call())]

'''
Returns the balance for a given token at owned by a specific address
'''

def balance_of(self, address: str, token_id: int) -> int:
'''
Returns the balance for a given token at owned by a specific address
'''
return self.__abi_module.balance_of.call(address, token_id)

'''
Returns the balance for a given token id for the current signers address
'''

def balance(self, token_id: int) -> int:
'''
Returns the balance for a given token id for the current signers address
'''
return self.__abi_module.balance_of.call(
self.get_signer_address(),
token_id
)

def is_approved(self, address: str, operator: str, token_contract: str = None, token_id: int = None) -> bool:
if not token_contract: return self.__abi_module.is_approved_for_all.call(address,operator)
asset = NFT(self.get_client(), token_contract)
approved = asset.is_approved_for_all.call(address, operator)
is_token_approved = asset.get_approved.call(token_id).lower() == self.address.lower()
return approved or is_token_approved

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

def set_approval(self, operator: str, approved: bool = True):
self.execute_tx(self.__abi_module.set_approval_for_all.build_transaction(
Expand Down Expand Up @@ -97,20 +91,28 @@ def create_and_mint_batch(self, meta_with_supply: List[CreateBundleArg]) -> List
return [self.get(i) for i in token_ids]

def create_with_token(self, token_contract: str, token_amount: int, metadata: dict = None):
"""
WIP: This method is not yet complete.
"""
if token_contract == "" or token_contract is None or not self.get_client().isAddress(token_contract):
raise Exception("token_contract not a valid address")
if token_amount <= 0:
raise Exception("token_amount must be greater than 0")

uri = self.upload_metadata(metadata)
if token_contract is not None and token_contract != ZeroAddress:
erc20 = ERC20(self.get_client(), token_contract)
allowance = erc20.allowance.call(self.get_signer_address(), self.address)
if allowance < token_amount:
tx = erc20.increase_allowance.build_transaction(self.address,
token_amount,
self.get_transact_opts())
self.execute_tx(tx)
erc20 = ERC20(self.get_client(), token_contract)
allowance = erc20.allowance.call(
self.get_signer_address(), self.address)
if allowance < token_amount:
tx = erc20.increase_allowance.build_transaction(self.address,
token_amount,
self.get_transact_opts())
self.execute_tx(tx)

self.execute_tx(self.__abi_module.wrap_erc20.build_transaction(
token_contract, token_amount, token_amount, uri, self.get_transact_opts()
))

def create_with_nft(self, token_contract: str, token_id: int, metadata):
"""
WIP: This method is not yet complete.
Expand All @@ -119,9 +121,9 @@ def create_with_nft(self, token_contract: str, token_id: int, metadata):
nft_module = NFT(self.get_client(), token_contract)

asset = NFT(self.get_client(), token_contract)
approved = asset.is_approved_for_all.call(self.get_signer_address(), self.address)

approved = asset.is_approved_for_all.call(
self.get_signer_address(), self.address)

if not approved:
is_token_approved = asset.get_approved.call(
token_id).lower() == self.address.lower()
Expand All @@ -135,10 +137,10 @@ def create_with_nft(self, token_contract: str, token_id: int, metadata):
))

def create_with_erc721(self, token_contract: str, token_id: int, metadata):
return create_with_nft(token_contract, token_id, metadata)
return self.create_with_nft(token_contract, token_id, metadata)

def create_with_erc20(self, token_contract: str, token_amount: int, metadata):
return create_with_token(token_contract, token_amount, metadata)
return self.create_with_token(token_contract, token_amount, metadata)

def mint(self, args: MintBundleArg):
self.mint_to(self.get_signer_address(), args)
Expand Down