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 @@ -20,3 +20,5 @@ examples/test.py
**/*.pyc

*.egg-info

./docs/*
141 changes: 89 additions & 52 deletions thirdweb/modules/nft.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,47 @@


class NftModule(BaseModule):
"""
"""
NFT Methods
"""

address: str
__abi_module: NFT


def __init__(self, address: str, client: Web3):
"""
Initializing the class attributes
"""
super().__init__()
self.address = address
"""
NFT module contract address [https://thirdweb.com/: Dashboard: Project ➝ NFT Module]
"""

self.__abi_module = NFT(client, address)
"""
The ABI makes calls the EVM. Client is default 'Web3' and Address is nft module contract address.
"""

def mint(self, arg: MintArg) -> NftType:
"""
Mints a new token
Mints a new token to the signer.
- Arguments passed: Note, a class is used -> MintArg(name, description, image_uri, properties)
- Returns the `NftMetadata(name,description,image,properties,id,uri)` *Preferrably, using a link
"""
return self.mint_to(self.get_signer_address(), arg)


def mint_to(
self,
to_address: str,
arg: MintArg,
self,
to_address: str,
arg: MintArg,
) -> NftType:
"""
Mints a new token
Mints a new token to an address
- Arguments passed: `to_address` and a class -> `MintArg(name, description, image_uri, properties)`
- Returns the `NftMetadata(name,description,image,properties,id,uri)` *Preferrably, using a link
"""
final_properties: Dict
if arg.properties is None:
Expand All @@ -46,20 +62,21 @@ def mint_to(
storage = self.get_storage()

meta = {
'name': arg.name,
'description': arg.description,
'image': arg.image_uri,
'properties': final_properties
"name": arg.name,
"description": arg.description,
"image": arg.image_uri,
"properties": final_properties,
}

uri = storage.upload(json.dumps(meta), self.address,
self.get_signer_address())
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())
to_address, uri, self.get_transact_opts()
)
receipt = self.execute_tx(tx)
result = self.__abi_module.get_minted_event(
tx_hash=receipt.transactionHash.hex())
token_id = result[0]['args']['tokenId']
tx_hash=receipt.transactionHash.hex()
)
token_id = result[0]["args"]["tokenId"]
return self.get(token_id)

def total_supply(self) -> int:
Expand All @@ -85,8 +102,7 @@ def __get_metadata_uri(self, token_id: int):
"""
uri = self.__abi_module.token_uri.call(token_id)
if uri == "":
raise Exception(
"Could not find NFT metadata, are you sure it exists?")
raise Exception("Could not find NFT metadata, are you sure it exists?")
return uri

def mint_batch(self, args: List[MintArg]):
Expand All @@ -99,29 +115,41 @@ def mint_batch_to(self, to_address: str, args: List[MintArg]):
"""
Mints a batch of tokens to the given address
"""
uris = [self.get_storage().upload(json.dumps({
'name': arg.name,
'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]
uris = [
self.get_storage().upload(
json.dumps(
{
"name": arg.name,
"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
]

tx = self.__abi_module.mint_nft_batch.build_transaction(
to_address, uris, self.get_transact_opts())
to_address, uris, self.get_transact_opts()
)

receipt = self.execute_tx(tx)
result = self.__abi_module.get_minted_batch_event(
tx_hash=receipt.transactionHash.hex())
token_ids = result[0]['args']['tokenIds']
tx_hash=receipt.transactionHash.hex()
)
token_ids = result[0]["args"]["tokenIds"]
return [self.get(i) for i in token_ids]

def burn(self, token_id: int):
"""
"""
Burns a given token
"""
tx = self.__abi_module.burn.build_transaction(
token_id,
self.get_transact_opts()
token_id, self.get_transact_opts()
)
self.execute_tx(tx)

Expand All @@ -130,10 +158,7 @@ def transfer_from(self, from_address: str, to_address: str, token_id: int):
Transfers a token from one address to another
"""
tx = self.__abi_module.transfer_from.build_transaction(
from_address,
to_address,
token_id,
self.get_transact_opts()
from_address, to_address, token_id, self.get_transact_opts()
)
self.execute_tx(tx)

Expand All @@ -142,10 +167,7 @@ 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(),
to_address,
token_id,
self.get_transact_opts()
self.get_signer_address(), to_address, token_id, self.get_transact_opts()
)
self.execute_tx(tx)

Expand All @@ -154,7 +176,8 @@ def set_royalty_bps(self, amount: int):
Sets the royalty percentage for the NFT
"""
tx = self.__abi_module.set_royalty_bps.build_transaction(
amount, self.get_transact_opts())
amount, self.get_transact_opts()
)

self.execute_tx(tx)

Expand All @@ -174,22 +197,33 @@ def get_owned(self, address: str = "") -> List[NftType]:
address = self.get_signer_address()

balance = self.__abi_module.balance_of.call(address)
owned_tokens = [self.__token_of_owner_by_index(
address, i) for i in range(balance)]
owned_tokens = [
self.__token_of_owner_by_index(address, i) for i in range(balance)
]
return [self.get(i) for i in owned_tokens]

def __token_of_owner_by_index(self, address: str, token_id: int) -> int:
return self.__abi_module.token_of_owner_by_index.call(address, token_id)


def balance(self) -> int:
"""
Returns balance of the current signers wallet
- Arguments: none. Method refers to nft module class.
- Use-case: Use this method if you want to use the currently connected wallet
- Dashboard: Project ➝ NFT Module ➝ Total amount of NFT's

"""

return self.__abi_module.balance_of.call(self.get_signer_address())


def balance_of(self, address: str) -> int:
"""
Returns balance of the given address
Returns balance of the given addressss
- Arguments: Pass the address of which to check the balance
- Use-case: Use this method if you don't want to use the connected wallet, but want to check another wallet.
- Dashboard: Project ➝ NFT Module ➝ Total amount of NFT's
"""
return self.__abi_module.balance_of.call(address)

Expand Down Expand Up @@ -221,33 +255,35 @@ def set_approval(self, operator: str, approved: bool = True):
Sets approval for specified operator, defaults to grant approval
"""
tx = self.__abi_module.set_approval_for_all.build_transaction(
operator, approved, self.get_transact_opts())
operator, approved, self.get_transact_opts()
)
self.execute_tx(tx)

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

def get_with_owner(self, token_id: int, owner: str):
"""
Returns the NFT with the given token id and owner
"""
owner = self.owner_of(token_id)
meta = self.get_metadata(token_id)
return {
owner: owner,
meta: meta
}
return {owner: owner, meta: meta}

def set_module_metadata(self, metadata: str):
"""
Sets the metadata for the module
"""
uri = self.get_storage().upload_metadata(
metadata, self.address, self.get_signer_address())
metadata, self.address, self.get_signer_address()
)
tx = self.__abi_module.set_contract_uri.build_transaction(
uri, self.get_transact_opts())
uri, self.get_transact_opts()
)
self.execute_tx(tx)

def set_restricted_transfer(self, restricted: bool = False):
Expand All @@ -256,7 +292,8 @@ def set_restricted_transfer(self, restricted: bool = False):
"""

tx = self.__abi_module.set_restricted_transfer.build_transaction(
restricted, self.get_transact_opts())
restricted, self.get_transact_opts()
)
self.execute_tx(tx)

def get_abi_module(self) -> NFT:
Expand Down