Ethereum: Contract.functions “not found” in Web3.py
I can provide you with a well-structured and informative article by using the Python and Web3.py directory.
Test Ethereum contracts with Python and Web3.py
Ethereum is a decentralized, open source Blockchain platform that allows you to create intelligent contracts and decentralized applications (DAPP). One of the most important benefits of using Ethereum is the ability to test and install intelligent contracts without having to set up a real Ethereum network. However, testing these contracts can be a challenge due to the complexity of the Ethereum virtual machine (EVM) and the need for a reliable test environment.
Problem: Contract functions have not been found
When you try to test the contract on the Ethereum network using web3.py, one of the most common problems you encounter when trying to use the contract with the contract with the ABI (application -based interface) function. In particular, the “contract.functions” method returns an error message that is not found in “trying to draw the function of the contract.
Why do this happen?
The reason for this mistake is that Ethereum is to manage contract functions. When you create a new contract, you will not register features with ABI (Ethereum Binary Surface). Instead, all characteristics must be registered separately by the “contract functions” method.
Solving a problem: Contract register and their functions
In order to solve the problem, we must register our Ethereum agreement and its functions manually. This can be done by creating a copy of the web3 'class and by defining a service provider (such as a local node or remote service provider such as infrastructure), which is used to interact with the block chain.
Python
Bring your requests
Bring JSON
Eth_Account Innont Account
Web3 Import Web3, httpprovider
Set Ethereum Service Provider (Local Node or Remote Provider)
Service Provider = httpprovider (‘http: // Localhost: 8545’)
Create a new account using a private key
Account = account
Set a copy of the contract
Agreement_address = ‘0xb9 * F832860DBD’
Controlntabi = ‘…’
Change your contract abbi
De Test_Contract ():
Get contracts from the service provider
Agreement = Web3 (Service Provider) .Load (Agreement_address, To = Account)
Register the contract features
Func Compharabi:
If Haatr (Agreement, Func):
Contract.funks [fufc] .transact ()
`
Test sampling agreement
Suppose we have a simple contract with three functions: “Hi”, “farewell” and “Sayhello. You can make ABI from this agreement with the "Web3" directory.
Python
Controlntabi = {
‘Permanent’: Fake,
“Income”: [],
‘Name’: ‘Hi’,
“Results”: [
{‘Internap’: ‘string’, ‘name’ ”, ‘type’: ‘string’}
],],
“Payable”: Wrong,
‘Stemutability’: ”,
‘Type’: ‘Function’
}
De test_hello ():
Get contracts expression from the service provider
Agreement = Web3 (Service Provider) .Load (Agreement_address, To = Account)
Call the Hello feature
Result = contract.functions.Hello (). Transact ()
Print the result
Print (result)
`
In this example, we create our ABI “Hello” contract with three activities: “Hello”, “Farewell” and “Sayhello”. Individual functions are then tested separately using the `body_hello () function.
conclusion
Testing Ethereum agreements can be a challenge due to the complexity of EVM and the need for a reliable test environment. However, by registering contracts and their functions manually, we can ensure that they are properly tested. This approach also facilitates the identification of potential problems or errors necessary to maintain stable and safe intelligent contract ecosystems.