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

Commit 52c30bf

Browse files
authored
Fix contract.call and storage (#131)
1 parent d4b71f9 commit 52c30bf

File tree

11 files changed

+87
-60
lines changed

11 files changed

+87
-60
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ packages = [
66
{include = "thirdweb"},
77
]
88
readme = "README.md"
9-
version = "3.0.2a2"
9+
version = "3.0.2"
1010

1111
[tool.poetry.dependencies]
1212
python = ">=3.7.1"

thirdweb/contracts/custom.py

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -102,51 +102,7 @@ def __init__(
102102
self.erc1155 = self._detect_erc_1155()
103103

104104
def call(self, fn: str, *args) -> Any:
105-
func = cast(ContractFunction, getattr(self.functions, fn, None))
106-
if func is None:
107-
raise Exception(
108-
f"Function {fn} not found on contract {self.get_address()}. "
109-
+ "Check your dashboard for the list of available functions."
110-
)
111-
112-
# We need this to set params properly on func + throws good errors
113-
func.args = args
114-
func.kwargs = {}
115-
func._set_function_info()
116-
117-
if len(func.abi["inputs"]) != len(args):
118-
signature = (
119-
"("
120-
+ ", ".join(
121-
[(i["name"] + ": " + i["type"]) for i in func.abi["inputs"]]
122-
)
123-
+ ")"
124-
)
125-
raise Exception(
126-
f"Function {fn} expects {len(func.arguments)} arguments, "
127-
f"but {len(args)} were provided.\nExpected function signature: {signature}"
128-
)
129-
130-
if func.abi["stateMutability"] == "view" or func.abi["stateMutability"] == "pure":
131-
return func(*args).call()
132-
else:
133-
provider = self._contract_wrapper.get_provider()
134-
signer = self._contract_wrapper.get_signer()
135-
136-
if signer is None:
137-
raise NoSignerException
138-
139-
nonce = provider.eth.get_transaction_count(signer.address) # type: ignore
140-
141-
tx = func(*args).buildTransaction(
142-
TxParams(gas_price=provider.eth.gas_price).as_dict()
143-
)
144-
tx["nonce"] = nonce
145-
146-
signed_tx = signer.sign_transaction(tx) # type: ignore
147-
tx_hash = provider.eth.send_raw_transaction(signed_tx.rawTransaction)
148-
149-
return provider.eth.wait_for_transaction_receipt(tx_hash)
105+
return self._contract_wrapper.call(fn, *args)
150106

151107
"""
152108
INTERNAL FUNCTIONS

thirdweb/contracts/edition.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
from thirdweb.types.nft import EditionMetadata, EditionMetadataInput
2323
from thirdweb.types.sdk import SDKOptions
24-
from typing import Final, Optional, List
24+
from typing import Any, Final, Optional, List
2525

2626
from thirdweb.types.settings.metadata import EditionContractMetadata
2727
from thirdweb.types.tx import TxResultWithId
@@ -210,3 +210,6 @@ def mint_batch_to(
210210
"""
211211

212212
return self._erc1155.mint_batch_to(to, metadatas_with_supply)
213+
214+
def call(self, fn: str, *args) -> Any:
215+
return self._contract_wrapper.call(fn, *args)

thirdweb/contracts/edition_drop.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Final, List, Optional
1+
from typing import Any, Final, List, Optional
22

33
from web3 import Web3
44
from thirdweb.abi import DropERC1155
@@ -164,3 +164,6 @@ def claim(
164164
:return: tx receipt of the claim
165165
"""
166166
return self._erc1155.claim(token_id, quantity)
167+
168+
def call(self, fn: str, *args) -> Any:
169+
return self._contract_wrapper.call(fn, *args)

thirdweb/contracts/marketplace.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,9 @@ def allow_listing_from_any_asset(self) -> TxReceipt:
342342

343343
return self._contract_wrapper.multi_call(encoded)
344344

345+
def call(self, fn: str, *args) -> Any:
346+
return self._contract_wrapper.call(fn, *args)
347+
345348
"""
346349
INTERNAL FUNCTIONS
347350
"""
@@ -366,4 +369,4 @@ def _get_all_listings_no_filter(
366369
except:
367370
pass
368371

369-
return [listing for listing in listings if listing is not None]
372+
return [listing for listing in listings if listing is not None]

thirdweb/contracts/multiwrap.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Final, List, Optional, Union
1+
from typing import Any, Final, List, Optional, Union
22

33
from web3 import Web3
44
from web3.eth import TxReceipt
@@ -234,6 +234,9 @@ def unwrap(
234234
"unwrap", [wrapped_token_id, recipient_address]
235235
)
236236

237+
def call(self, fn: str, *args) -> Any:
238+
return self._contract_wrapper.call(fn, *args)
239+
237240
"""
238241
INTERNAL FUNCTIONS
239242
"""
@@ -333,4 +336,4 @@ def _to_token_struct_list(self, contents: TokensToWrap) -> List[ITokenBundleToke
333336
}
334337
)
335338

336-
return tokens
339+
return tokens

thirdweb/contracts/nft_collection.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from thirdweb.core.classes.contract_royalty import ContractRoyalty
99
from thirdweb.core.classes.contract_sales import ContractPrimarySale
1010
from thirdweb.core.classes.contract_wrapper import ContractWrapper
11-
from thirdweb.common.nft import upload_or_extract_uri, upload_or_extract_uris
1211
from thirdweb.core.classes.erc_721_standard import ERC721Standard
1312
from thirdweb.core.classes.erc_721_signature_minting import ERC721SignatureMinting
1413
from thirdweb.abi import TokenERC721
@@ -20,7 +19,7 @@
2019
from web3 import Web3
2120

2221
from thirdweb.types.sdk import SDKOptions
23-
from typing import Final, Optional, List, Union
22+
from typing import Any, Final, Optional, List, Union
2423

2524
from thirdweb.types.settings.metadata import NFTCollectionContractMetadata
2625
from thirdweb.types.tx import TxResultWithId
@@ -212,3 +211,6 @@ def mint_batch_to(
212211
"""
213212

214213
return self._erc721.mint_batch_to(to, metadatas)
214+
215+
def call(self, fn: str, *args) -> Any:
216+
return self._contract_wrapper.call(fn, *args)

thirdweb/contracts/nft_drop.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Final, List, Optional
1+
from typing import Any, Final, List, Optional
22
from thirdweb.abi import DropERC721
33
from thirdweb.abi.drop_erc721 import IDropAllowlistProof
44
from thirdweb.constants.role import Role
@@ -13,7 +13,6 @@
1313
from thirdweb.core.classes.erc_721_standard import ERC721Standard
1414
from thirdweb.core.classes.ipfs_storage import IpfsStorage
1515
from thirdweb.types.contract import ContractType
16-
from zero_ex.contract_wrappers.tx_params import TxParams
1716
from thirdweb.types.nft import (
1817
NFTMetadata,
1918
NFTMetadataInput,
@@ -250,3 +249,6 @@ def claim(
250249
:return: List of tx results with ids for claimed NFTs.
251250
"""
252251
return self._erc721.claim(quantity)
252+
253+
def call(self, fn: str, *args) -> Any:
254+
return self._contract_wrapper.call(fn, *args)

thirdweb/contracts/token.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Interface for interacting with a token contract"""
22

3-
from typing import Final, List, Optional
3+
from typing import Any, Final, List, Optional
44
from thirdweb.abi import TokenERC20
55
from web3 import Web3
66
from web3.eth import TxReceipt
@@ -178,3 +178,6 @@ def delegate_to(self, delegatee_address: str) -> TxReceipt:
178178
"""
179179

180180
return self._contract_wrapper.send_transaction("delegate", [delegatee_address])
181+
182+
def call(self, fn: str, *args) -> Any:
183+
return self._contract_wrapper.call(fn, *args)

thirdweb/core/classes/contract_wrapper.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from web3 import Web3
55
from web3.datastructures import AttributeDict
6-
from web3.contract import Contract
6+
from web3.contract import Contract, ContractFunction
77
from thirdweb.common.error import NoSignerException
88
from web3._utils.events import EventLogErrorFlags
99
from thirdweb.common.sign import EIP712Domain, sign_typed_data_internal
@@ -95,6 +95,53 @@ def get_events(self, event: str, receipt: TxReceipt) -> Tuple[AttributeDict]:
9595
.processReceipt(receipt, errors=EventLogErrorFlags.Discard)
9696
)
9797

98+
def call(self, fn: str, *args) -> Any:
99+
func = cast(ContractFunction, getattr(self.get_contract_interface().functions, fn, None))
100+
if func is None:
101+
raise Exception(
102+
f"Function {fn} not found on contract {self._contract_abi.contract_address}. "
103+
+ "Check your dashboard for the list of available functions."
104+
)
105+
106+
# We need this to set params properly on func + throws good errors
107+
func.args = args
108+
func.kwargs = {}
109+
func._set_function_info()
110+
111+
if len(func.abi["inputs"]) != len(args):
112+
signature = (
113+
"("
114+
+ ", ".join(
115+
[(i["name"] + ": " + i["type"]) for i in func.abi["inputs"]]
116+
)
117+
+ ")"
118+
)
119+
raise Exception(
120+
f"Function {fn} expects {len(func.arguments)} arguments, "
121+
f"but {len(args)} were provided.\nExpected function signature: {signature}"
122+
)
123+
124+
if func.abi["stateMutability"] == "view" or func.abi["stateMutability"] == "pure":
125+
return func(*args).call()
126+
else:
127+
provider = self.get_provider()
128+
signer = self.get_signer()
129+
130+
if signer is None:
131+
raise NoSignerException
132+
133+
nonce = provider.eth.get_transaction_count(signer.address) # type: ignore
134+
135+
tx = func(*args).buildTransaction(
136+
TxParams(gas_price=provider.eth.gas_price).as_dict()
137+
)
138+
tx["nonce"] = nonce
139+
140+
signed_tx = signer.sign_transaction(tx) # type: ignore
141+
tx_hash = provider.eth.send_raw_transaction(signed_tx.rawTransaction)
142+
143+
return provider.eth.wait_for_transaction_receipt(tx_hash)
144+
98145
def send_transaction(self, fn: str, args: List[Any], overrides: TxParams = None) -> TxReceipt:
99146
"""
100147
Send and execute a transaction and return the receipt.

0 commit comments

Comments
 (0)