如何根据类型/可见性将使用web3.py函数与智能合约区分开来



我正在使用web3.py.连接到一个智能合约

我是与部署合同的地址相同的合同所有者。

我可以打印所有函数,但我想根据它们的类型和可见性(如只读或修改状态(以及可见性(如:内部、外部、公共等(来分离每个函数。

有没有办法用web3.py库或任何其他库做到这一点?

这是我的代码:

# CONNECT TO CHAIN WITH WEB3
url = 'https://ropsten.infura.io/v3/e30d3bc3dfba4f92949b789dc797d82e'
w3 = Web3(Web3.HTTPProvider(url))
# CONNECT TO SMART CONTRACT
address = address_ABI.address                   # Importing from other file for more readability
abi = json.loads(address_ABI.abi)               # Importing from other file for more readability
contract = w3.eth.contract(address=address, abi=abi)
# ACCOUNTS:
account_0 = wallets.account_0                   # Importing from other file for more readability
private_key_0 = wallets.private_key_0           # Importing from other file for more readability
# LIST ALL CONTRACT FUNCTIONS
contract_functions = contract.all_functions()   # Returns a list with all functions
print(len(contract_functions))                  # Print number of functions
for function in contract_functions:             # Print function one by one
print(function)

如何按不同类型和可见性打印函数?像公共的、应付的、只读的、内部的、外部的等等?

我找到了一个解决方案,不是最优雅的,但可以工作:

你可以从contract.abi中提取所有元素,然后用下面的语句从字典中调用你想要的任何东西:"stateMutability"。这将返回您的状态,如:纯、视图、非应付等。这是一个示例代码:

for element in contract.abi:
if element['type'] == 'function':
print(element['name'], element['stateMutability'])
else:
pass

您还可以提取其他类型,如";event"而不是"function"。希望这能有所帮助。

最新更新