Skip to content
This repository was archived by the owner on May 3, 2024. It is now read-only.

Commit 5e9338e

Browse files
authored
Add extensions for erc20, erc721, and erc1155 (#126)
* Fix super() * Update * Fix standards with functions * Add erc20, erc721, and erc1155 extensions * Fix duplicate functions
1 parent 358a64a commit 5e9338e

File tree

12 files changed

+1408
-400
lines changed

12 files changed

+1408
-400
lines changed

thirdweb/contracts/custom.py

Lines changed: 19 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from thirdweb.abi.token_erc20 import TokenERC20
88
from thirdweb.abi.token_erc721 import TokenERC721
99
from thirdweb.common.error import NoSignerException
10-
from thirdweb.common.feature_detection import matches_interface
1110
from thirdweb.constants.role import ALL_ROLES
1211
from thirdweb.core.classes.base_contract import BaseContract
1312
from thirdweb.core.classes.contract_metadata import ContractMetadata
@@ -152,65 +151,37 @@ def call(self, fn: str, *args) -> Any:
152151
"""
153152

154153
def _detect_roles(self):
155-
interface_to_match = self._get_interface_functions(IPermissionsEnumerable.abi())
156-
157-
if matches_interface(self.functions, interface_to_match):
158-
contract_wrapper = self._get_contract_wrapper(IPermissionsEnumerable)
159-
return ContractRoles(contract_wrapper, ALL_ROLES)
160-
return None
154+
contract_wrapper = self._get_contract_wrapper(IPermissionsEnumerable)
155+
return ContractRoles(contract_wrapper, ALL_ROLES)
161156

162157
def _detect_royalties(self):
163-
interface_to_match = self._get_interface_functions(IRoyalty.abi())
164-
165-
if matches_interface(self.functions, interface_to_match):
166-
contract_wrapper = self._get_contract_wrapper(IRoyalty)
167-
metadata = ContractMetadata(
168-
contract_wrapper,
169-
self._storage,
170-
CustomContractMetadata,
171-
)
172-
return ContractRoyalty(contract_wrapper, metadata)
173-
return None
158+
contract_wrapper = self._get_contract_wrapper(IRoyalty)
159+
metadata = ContractMetadata(
160+
contract_wrapper,
161+
self._storage,
162+
CustomContractMetadata,
163+
)
164+
return ContractRoyalty(contract_wrapper, metadata)
174165

175166
def _detect_primary_sales(self):
176-
interface_to_match = self._get_interface_functions(IPrimarySale.abi())
177-
178-
if matches_interface(self.functions, interface_to_match):
179-
contract_wrapper = self._get_contract_wrapper(IPrimarySale)
180-
return ContractPrimarySale(contract_wrapper)
181-
return None
167+
contract_wrapper = self._get_contract_wrapper(IPrimarySale)
168+
return ContractPrimarySale(contract_wrapper)
182169

183170
def _detect_platform_fee(self):
184-
interface_to_match = self._get_interface_functions(IPlatformFee.abi())
185-
186-
if matches_interface(self.functions, interface_to_match):
187-
contract_wrapper = self._get_contract_wrapper(IPlatformFee)
188-
return ContractPlatformFee(contract_wrapper)
189-
return None
171+
contract_wrapper = self._get_contract_wrapper(IPlatformFee)
172+
return ContractPlatformFee(contract_wrapper)
190173

191174
def _detect_erc_20(self):
192-
interface_to_match = self._get_interface_functions(TokenERC20.abi())
193-
194-
if matches_interface(self.functions, interface_to_match):
195-
contract_wrapper = self._get_contract_wrapper(TokenERC20)
196-
return ERC20(contract_wrapper, self._storage)
197-
return None
175+
contract_wrapper = self._get_contract_wrapper(TokenERC20)
176+
return ERC20(contract_wrapper, self._storage)
198177

199178
def _detect_erc_721(self):
200-
interface_to_match = self._get_interface_functions(TokenERC721.abi())
201-
202-
if matches_interface(self.functions, interface_to_match):
203-
contract_wrapper = self._get_contract_wrapper(TokenERC721)
204-
return ERC721(contract_wrapper, self._storage)
205-
return None
179+
contract_wrapper = self._get_contract_wrapper(TokenERC721)
180+
return ERC721(contract_wrapper, self._storage)
206181

207182
def _detect_erc_1155(self):
208-
interface_to_match = self._get_interface_functions(TokenERC1155.abi())
209-
210-
if matches_interface(self.functions, interface_to_match):
211-
contract_wrapper = self._get_contract_wrapper(TokenERC1155)
212-
return ERC1155(contract_wrapper, self._storage)
213-
return None
183+
contract_wrapper = self._get_contract_wrapper(TokenERC1155)
184+
return ERC1155(contract_wrapper, self._storage)
214185

215186
def _get_interface_functions(self, abi: str) -> ContractFunctions:
216187
return (

thirdweb/contracts/edition.py

Lines changed: 6 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,7 @@ def mint(
9494
:returns: receipt, id, and metadata of the mint
9595
"""
9696

97-
return self.mint_to(
98-
self._contract_wrapper.get_signer_address(), metadata_with_supply
99-
)
97+
return self._erc1155.mint(metadata_with_supply)
10098

10199
def mint_to(
102100
self, to: str, metadata_with_supply: EditionMetadataInput
@@ -129,18 +127,7 @@ def mint_to(
129127
:returns: receipt, id, and metadata of the mint
130128
"""
131129

132-
uri = upload_or_extract_uri(metadata_with_supply.metadata, self._storage)
133-
receipt = self._contract_wrapper.send_transaction(
134-
"mint_to", [to, int(MAX_INT, 16), uri, metadata_with_supply.supply]
135-
)
136-
events = self._contract_wrapper.get_events("TransferSingle", receipt)
137-
138-
if len(events) == 0:
139-
raise Exception("No TransferSingle event found")
140-
141-
id = events[0].get("args").get("id") # type: ignore
142-
143-
return TxResultWithId(receipt, id=id, data=lambda: self.get(id))
130+
return self._erc1155.mint_to(to, metadata_with_supply)
144131

145132
def mint_additional_supply(
146133
self, token_id: int, additional_supply: int
@@ -153,9 +140,7 @@ def mint_additional_supply(
153140
:returns: receipt, id, and metadata of the mint
154141
"""
155142

156-
return self.mint_additional_supply_to(
157-
self._contract_wrapper.get_signer_address(), token_id, additional_supply
158-
)
143+
return self._erc1155.mint_additional_supply(token_id, additional_supply)
159144

160145
def mint_additional_supply_to(
161146
self, to: str, token_id: int, additional_supply: int
@@ -169,11 +154,7 @@ def mint_additional_supply_to(
169154
:returns: receipt, id, and metadata of the mint
170155
"""
171156

172-
metadata = self._erc1155._get_token_metadata(token_id)
173-
receipt = self._contract_wrapper.send_transaction(
174-
"mint_to", [to, token_id, metadata.uri, additional_supply]
175-
)
176-
return TxResultWithId(receipt, id=token_id, data=lambda: self.get(token_id))
157+
return self._erc1155.mint_additional_supply_to(to, token_id, additional_supply)
177158

178159
def mint_batch(
179160
self, metadatas_with_supply: List[EditionMetadataInput]
@@ -185,9 +166,7 @@ def mint_batch(
185166
:returns: receipts, ids, and metadatas of the mint
186167
"""
187168

188-
return self.mint_batch_to(
189-
self._contract_wrapper.get_signer_address(), metadatas_with_supply
190-
)
169+
return self._erc1155.mint_batch(metadatas_with_supply)
191170

192171
def mint_batch_to(
193172
self, to: str, metadatas_with_supply: List[EditionMetadataInput]
@@ -230,28 +209,4 @@ def mint_batch_to(
230209
:returns: receipts, ids, and metadatas of the mint
231210
"""
232211

233-
metadatas = [a.metadata for a in metadatas_with_supply]
234-
supplies = [a.supply for a in metadatas_with_supply]
235-
uris = upload_or_extract_uris(metadatas, self._storage)
236-
237-
encoded = []
238-
interface = self._contract_wrapper.get_contract_interface()
239-
for index, uri in enumerate(uris):
240-
encoded.append(
241-
interface.encodeABI(
242-
"mintTo", [to, int(MAX_INT, 16), uri, supplies[index]]
243-
)
244-
)
245-
246-
receipt = self._contract_wrapper.multi_call(encoded)
247-
events = self._contract_wrapper.get_events("TokensMinted", receipt)
248-
249-
if len(events) == 0 and len(events) < len(metadatas):
250-
raise Exception("No TokensMinted event found, minting failed")
251-
252-
results = []
253-
for event in events:
254-
id = event.get("args").get("tokenIdMinted") # type: ignore
255-
results.append(TxResultWithId(receipt, id=id, data=lambda: self.get(id)))
256-
257-
return results
212+
return self._erc1155.mint_batch_to(to, metadatas_with_supply)

thirdweb/contracts/edition_drop.py

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

33
from web3 import Web3
44
from thirdweb.abi import DropERC1155
5-
from thirdweb.abi.drop_erc721 import IDropAllowlistProof
6-
from thirdweb.common.claim_conditions import prepare_claim
75
from thirdweb.constants.role import Role
86
from thirdweb.core.classes.contract_events import ContractEvents
97
from thirdweb.core.classes.contract_metadata import ContractMetadata
@@ -15,15 +13,13 @@
1513
from thirdweb.core.classes.erc_1155_standard import ERC1155Standard
1614
from thirdweb.core.classes.ipfs_storage import IpfsStorage
1715
from thirdweb.types.contract import ContractType
18-
from thirdweb.types.contracts.claim_conditions import ClaimVerification
1916
from thirdweb.types.nft import NFTMetadata, NFTMetadataInput
2017
from thirdweb.types.sdk import SDKOptions
2118
from thirdweb.types.settings.metadata import EditionDropContractMetadata
2219
from eth_account.account import LocalAccount
2320
from thirdweb.core.classes.drop_erc1155_claim_conditions import (
2421
DropERC1155ClaimConditions,
2522
)
26-
from zero_ex.contract_wrappers.tx_params import TxParams
2723
from thirdweb.types.tx import TxResultWithId
2824
from web3.eth import TxReceipt
2925

@@ -123,41 +119,7 @@ def create_batch(
123119
:return: List of tx results with ids for created NFTs.
124120
"""
125121

126-
start_file_number = (
127-
self._contract_wrapper._contract_abi.next_token_id_to_mint.call()
128-
)
129-
batch = self._storage.upload_metadata_batch(
130-
[metadata.to_json() for metadata in metadatas],
131-
start_file_number,
132-
self._contract_wrapper._contract_abi.contract_address,
133-
self._contract_wrapper.get_signer_address(),
134-
)
135-
base_uri = batch.base_uri
136-
137-
receipt = self._contract_wrapper.send_transaction(
138-
"lazy_mint",
139-
[
140-
len(batch.metadata_uris),
141-
base_uri if base_uri.endswith("/") else base_uri + "/",
142-
Web3.toBytes(text=""),
143-
],
144-
)
145-
146-
events = self._contract_wrapper.get_events("TokensLazyMinted", receipt)
147-
start_index = events[0].get("args").get("startTokenId") # type: ignore
148-
ending_index = events[0].get("args").get("endTokenId") # type: ignore
149-
results = []
150-
151-
for id in range(start_index, ending_index + 1):
152-
results.append(
153-
TxResultWithId(
154-
receipt,
155-
id=id,
156-
data=lambda: self._erc1155._get_token_metadata(id),
157-
)
158-
)
159-
160-
return results
122+
return self._erc1155.create_batch(metadatas)
161123

162124
def claim_to(
163125
self,
@@ -186,29 +148,7 @@ def claim_to(
186148
:return: tx receipt of the claim
187149
"""
188150

189-
claim_verification = self._prepare_claim(token_id, quantity)
190-
overrides: TxParams = TxParams(value=claim_verification.value)
191-
192-
proof = IDropAllowlistProof(
193-
proof=claim_verification.proofs,
194-
quantityLimitPerWallet=claim_verification.max_claimable,
195-
pricePerToken=claim_verification.price_in_proof,
196-
currency=claim_verification.currency_address_in_proof
197-
)
198-
199-
return self._contract_wrapper.send_transaction(
200-
"claim",
201-
[
202-
destination_address,
203-
token_id,
204-
quantity,
205-
claim_verification.currency_address,
206-
claim_verification.price,
207-
proof,
208-
"",
209-
],
210-
overrides
211-
)
151+
return self._erc1155.claim_to(destination_address, token_id, quantity)
212152

213153
def claim(
214154
self,
@@ -223,30 +163,4 @@ def claim(
223163
:param proofs: List of merkle proofs.
224164
:return: tx receipt of the claim
225165
"""
226-
return self.claim_to(
227-
self._contract_wrapper.get_signer_address(),
228-
token_id,
229-
quantity,
230-
)
231-
232-
"""
233-
INTERNAL FUNCTIONS
234-
"""
235-
236-
def _prepare_claim(
237-
self,
238-
token_id: int,
239-
quantity: int,
240-
) -> ClaimVerification:
241-
address_to_claim = self._contract_wrapper.get_signer_address()
242-
active = self.claim_conditions.get_active(token_id)
243-
merkle_metadata = self.metadata.get().merkle
244-
245-
return prepare_claim(
246-
address_to_claim,
247-
quantity,
248-
active,
249-
merkle_metadata,
250-
self._contract_wrapper,
251-
self._storage,
252-
)
166+
return self._erc1155.claim(token_id, quantity)

thirdweb/contracts/nft_collection.py

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def mint(
132132
:returns: receipt, id, and metadata for the mint
133133
"""
134134

135-
return self.mint_to(self._contract_wrapper.get_signer_address(), metadata)
135+
return self._erc721.mint(metadata)
136136

137137
def mint_to(
138138
self, to: str, metadata: Union[NFTMetadataInput, str]
@@ -162,16 +162,7 @@ def mint_to(
162162
:returns: receipt, id, and metadata for the mint
163163
"""
164164

165-
uri = upload_or_extract_uri(metadata, self._storage)
166-
receipt = self._contract_wrapper.send_transaction("mint_to", [to, uri])
167-
events = self._contract_wrapper.get_events("Transfer", receipt)
168-
169-
if len(events) == 0:
170-
raise Exception("No Transfer event found")
171-
172-
id = events[0].get("args").get("tokenId") # type: ignore
173-
174-
return TxResultWithId(receipt, id=id, data=lambda: self.get(id))
165+
return self._erc721.mint_to(to, metadata)
175166

176167
def mint_batch(
177168
self, metadatas: List[Union[NFTMetadataInput, str]]
@@ -183,9 +174,7 @@ def mint_batch(
183174
:returns: receipts, ids, and metadatas for each mint
184175
"""
185176

186-
return self.mint_batch_to(
187-
self._contract_wrapper.get_signer_address(), metadatas
188-
)
177+
return self._erc721.mint_batch(metadatas)
189178

190179
def mint_batch_to(
191180
self, to: str, metadatas: List[Union[NFTMetadataInput, str]]
@@ -222,22 +211,4 @@ def mint_batch_to(
222211
:returns: receipts, ids, and metadatas for each mint
223212
"""
224213

225-
uris = upload_or_extract_uris(metadatas, self._storage)
226-
227-
encoded = []
228-
interface = self._contract_wrapper.get_contract_interface()
229-
for uri in uris:
230-
encoded.append(interface.encodeABI("mintTo", [to, uri]))
231-
232-
receipt = self._contract_wrapper.multi_call(encoded)
233-
events = self._contract_wrapper.get_events("TokensMinted", receipt)
234-
235-
if len(events) == 0 or len(events) < len(metadatas):
236-
raise Exception("No TokensMinted event found, minting failed")
237-
238-
results = []
239-
for event in events:
240-
id = event.get("args").get("tokenIdMinted") # type: ignore
241-
results.append(TxResultWithId(receipt, id=id, data=lambda: self.get(id)))
242-
243-
return results
214+
return self._erc721.mint_batch_to(to, metadatas)

0 commit comments

Comments
 (0)