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