Solidity

呼叫事務時出錯

  • March 19, 2022
Constantly getting this error
INFO: Could not find files for the given pattern(s).
0x42f4abcd31ac145518d9275352a55960b2aa24496bca313dfd665749b2ae093c
Traceback (most recent call last):
File "C:\Users\Rishi Garg personal\demos\web3_py_simple_storage\deploy.py", line 106, in
transaction = SimpleStorage.constructor().buildTransaction(
File "C:\anaconda\lib\site-packages\eth_utils\decorators.py", line 18, in _wrapper
return self.method(obj, *args, **kwargs)
File "C:\anaconda\lib\site-packages\web3\contract.py", line 684, in buildTransaction
return fill_transaction_defaults(self.web3, built_transaction)
File "cytoolz\functoolz.pyx", line 250, in cytoolz.functoolz.curry.call
File "C:\anaconda\lib\site-packages\web3_utils\transactions.py", line 114, in fill_transaction_defaults
default_val = default_getter(web3, transaction)
File "C:\anaconda\lib\site-packages\web3_utils\transactions.py", line 60, in
'gas': lambda web3, tx: web3.eth.estimate_gas(tx),
File "C:\anaconda\lib\site-packages\web3\eth.py", line 855, in estimate_gas
return self._estimate_gas(transaction, block_identifier)
File "C:\anaconda\lib\site-packages\web3\module.py", line 57, in caller
File "C:\anaconda\lib\site-packages\web3\manager.py", line 198, in request_blocking
return self.formatted_response(response,
File "C:\anaconda\lib\site-packages\web3\manager.py", line 170, in formatted_response
apply_error_formatters(error_formatters, response)
File "C:\anaconda\lib\site-packages\web3\manager.py", line 70, in apply_error_formatters
formatted_resp = pipe(response, error_formatters)
File "cytoolz\functoolz.pyx", line 667, in cytoolz.functoolz.pipe
File "cytoolz\functoolz.pyx", line 642, in cytoolz.functoolz.c_pipe
File "C:\anaconda\lib\site-packages\web3_utils\method_formatters.py", line 561, in raise_solidity_error_on_revert
raise ContractLogicError(f'execution reverted: {response["error"]["message"]}')
web3.exceptions.ContractLogicError: execution reverted: Reverting to invalid state checkpoint failed
PS C:\Users\Rishi Garg personal\demos\web3_py_simple_storage> python deploy.py
INFO: Could not find files for the given pattern(s).
0x42f4abcd31ac145518d9275352a55960b2aa24496bca313dfd665749b2ae093c
Traceback (most recent call last):
File "C:\Users\Rishi Garg personal\demos\web3_py_simple_storage\deploy.py", line 106, in
transaction = SimpleStorage.constructor().buildTransaction(
File "C:\anaconda\lib\site-packages\eth_utils\decorators.py", line 18, in _wrapper
return self.method(obj, *args, **kwargs)
File "C:\anaconda\lib\site-packages\web3\contract.py", line 684, in buildTransaction
return fill_transaction_defaults(self.web3, built_transaction)
File "cytoolz\functoolz.pyx", line 250, in cytoolz.functoolz.curry.call
File "C:\anaconda\lib\site-packages\web3_utils\transactions.py", line 114, in fill_transaction_defaults
default_val = default_getter(web3, transaction)
File "C:\anaconda\lib\site-packages\web3_utils\transactions.py", line 60, in
'gas': lambda web3, tx: web3.eth.estimate_gas(tx),
File "C:\anaconda\lib\site-packages\web3\eth.py", line 855, in estimate_gas
return self._estimate_gas(transaction, block_identifier)
File "C:\anaconda\lib\site-packages\web3\module.py", line 57, in caller
PS C:\Users\Rishi Garg personal\demos\web3_py_simple_storage> python deploy.py
INFO: Could not find files for the given pattern(s).
0x42f4abcd31ac145518d9275352a55960b2aa24496bca313dfd665749b2ae093c
Traceback (most recent call last):
File "C:\Users\Rishi Garg personal\demos\web3_py_simple_storage\deploy.py", line 106, in
transaction = SimpleStorage.constructor().buildTransaction(
File "C:\anaconda\lib\site-packages\eth_utils\decorators.py", line 18, in _wrapper
return self.method(obj, *args, **kwargs)
File "C:\anaconda\lib\site-packages\web3\contract.py", line 684, in buildTransaction
return fill_transaction_defaults(self.web3, built_transaction)
File "cytoolz\functoolz.pyx", line 250, in cytoolz.functoolz.curry.call
File "C:\anaconda\lib\site-packages\web3_utils\transactions.py", line 114, in fill_transaction_defaults
default_val = default_getter(web3, transaction)
File "C:\anaconda\lib\site-packages\web3_utils\transactions.py", line 60, in
'gas': lambda web3, tx: web3.eth.estimate_gas(tx),
File "C:\anaconda\lib\site-packages\web3\eth.py", line 855, in estimate_gas
return self._estimate_gas(transaction, block_identifier)
File "C:\anaconda\lib\site-packages\web3\module.py", line 57, in caller
File "C:\anaconda\lib\site-packages\web3\manager.py", line 198, in request_blocking
return self.formatted_response(response,
File "C:\anaconda\lib\site-packages\web3\manager.py", line 170, in formatted_response
apply_error_formatters(error_formatters, response)
File "C:\anaconda\lib\site-packages\web3\manager.py", line 70, in apply_error_formatters
formatted_resp = pipe(response, error_formatters)
File "cytoolz\functoolz.pyx", line 667, in cytoolz.functoolz.pipe
File "cytoolz\functoolz.pyx", line 642, in cytoolz.functoolz.c_pipe
File "C:\anaconda\lib\site-packages\web3_utils\method_formatters.py", line 561, in raise_solidity_error_on_revert
raise ContractLogicError(f'execution reverted: {response["error"]["message"]}')
web3.exceptions.ContractLogicError: execution reverted: Reverting to invalid state checkpoint failed
PS C:\Users\Rishi Garg personal\demos\web3_py_simple_storage> python deploy.py
INFO: Could not find files for the given pattern(s).
0x42f4abcd31ac145518d9275352a55960b2aa24496bca313dfd665749b2ae093c
Traceback (most recent call last):
File "C:\Users\Rishi Garg personal\demos\web3_py_simple_storage\deploy.py", line 106, in
transaction = SimpleStorage.constructor().buildTransaction(
File "C:\anaconda\lib\site-packages\eth_utils\decorators.py", line 18, in _wrapper
return self.method(obj, *args, **kwargs)
File "C:\anaconda\lib\site-packages\web3\contract.py", line 684, in buildTransaction
return fill_transaction_defaults(self.web3, built_transaction)
File "cytoolz\functoolz.pyx", line 250, in cytoolz.functoolz.curry.call
File "C:\anaconda\lib\site-packages\web3_utils\transactions.py", line 114, in fill_transaction_defaults
default_val = default_getter(web3, transaction)
File "C:\anaconda\lib\site-packages\web3_utils\transactions.py", line 60, in
'gas': lambda web3, tx: web3.eth.estimate_gas(tx),
File "C:\anaconda\lib\site-packages\web3\eth.py", line 855, in estimate_gas
return self._estimate_gas(transaction, block_identifier)
File "C:\anaconda\lib\site-packages\web3\module.py", line 57, in caller
File "C:\anaconda\lib\site-packages\web3\manager.py", line 198, in request_blocking
PS C:\Users\Rishi Garg personal\demos\web3_py_simple_storage> python deploy1.py
INFO: Could not find files for the given pattern(s).
Installing...
Traceback (most recent call last):
File "C:\Users\Rishi Garg personal\demos\web3_py_simple_storage\deploy1.py", line 64, in
transaction = SimpleStorage.constructor().buildTransaction(
File "C:\anaconda\lib\site-packages\eth_utils\decorators.py", line 18, in _wrapper
return self.method(obj, *args, **kwargs)
File "C:\anaconda\lib\site-packages\web3\contract.py", line 684, in buildTransaction
return fill_transaction_defaults(self.web3, built_transaction)
File "cytoolz\functoolz.pyx", line 250, in cytoolz.functoolz.curry.call
File "C:\anaconda\lib\site-packages\web3_utils\transactions.py", line 114, in fill_transaction_defaults
default_val = default_getter(web3, transaction)
File "C:\anaconda\lib\site-packages\web3_utils\transactions.py", line 60, in
'gas': lambda web3, tx: web3.eth.estimate_gas(tx),
File "C:\anaconda\lib\site-packages\web3\eth.py", line 855, in estimate_gas
return self._estimate_gas(transaction, block_identifier)
File "C:\anaconda\lib\site-packages\web3\module.py", line 57, in caller
PS C:\Users\Rishi Garg personal\demos\web3_py_simple_storage> python deploy.py
INFO: Could not find files for the given pattern(s).
0x42f4abcd31ac145518d9275352a55960b2aa24496bca313dfd665749b2ae093c
Traceback (most recent call last):
File "C:\Users\Rishi Garg personal\demos\web3_py_simple_storage\deploy.py", line 106, in
transaction = SimpleStorage.constructor().buildTransaction(
File "C:\anaconda\lib\site-packages\eth_utils\decorators.py", line 18, in _wrapper
return self.method(obj, *args, **kwargs)
File "C:\anaconda\lib\site-packages\web3\contract.py", line 684, in buildTransaction
return fill_transaction_defaults(self.web3, built_transaction)
File "cytoolz\functoolz.pyx", line 250, in cytoolz.functoolz.curry.call
File "C:\anaconda\lib\site-packages\web3_utils\transactions.py", line 114, in fill_transaction_defaults
default_val = default_getter(web3, transaction)
File "C:\anaconda\lib\site-packages\web3_utils\transactions.py", line 60, in
'gas': lambda web3, tx: web3.eth.estimate_gas(tx),
File "C:\anaconda\lib\site-packages\web3\eth.py", line 855, in estimate_gas
return self._estimate_gas(transaction, block_identifier)
File "C:\anaconda\lib\site-packages\web3\module.py", line 57, in caller
result = w3.manager.request_blocking(method_str,
File "C:\anaconda\lib\site-packages\web3\manager.py", line 198, in request_blocking
return self.formatted_response(response,
File "C:\anaconda\lib\site-packages\web3\manager.py", line 170, in formatted_response
apply_error_formatters(error_formatters, response)
File "C:\anaconda\lib\site-packages\web3\manager.py", line 70, in apply_error_formatters
formatted_resp = pipe(response, error_formatters)
File "cytoolz\functoolz.pyx", line 667, in cytoolz.functoolz.pipe
File "cytoolz\functoolz.pyx", line 642, in cytoolz.functoolz.c_pipe
File "C:\anaconda\lib\site-packages\web3_utils\method_formatters.py", line 561, in raise_solidity_error_on_revert
raise ContractLogicError(f'execution reverted: {response["error"]["message"]}')
web3.exceptions.ContractLogicError: execution reverted: Reverting to invalid state checkpoint failed

下面是程式碼

#'first we read the solidity file'
# we use py-solc-x as our compiler
# always use formatter to be blsck (python formatting provider for better code writing)

from itertools import chain
from numpy import signedinteger
from solcx import compile_standard, install_solc
import json
from web3 import Web3, HTTPProvider

import os  # for getting private key
from dotenv import load_dotenv


load_dotenv()  # this load_dotenv() looks for this .env file and automatically imports it into our script


with open("./SimpleStorage.sol", "r") as file:
   simple_storage_file = file.read()
   # print(simple_storage_file)

   # compile our solidity
install_solc("0.6.0")

compiled_sol = compile_standard(  # this is equal to us calling this compile standard function where we are using the compiler
   {
       "language": "Solidity",
       "sources": {"SimpleStorage.sol": {"content": simple_storage_file}},
       "settings": {
           "outputSelection": {
               "*": {"*": ["abi", "metadata", "evm.bytecode", "evm.sourceMap"]}
           }
       },
   },
   solc_version="0.6.0",
)


# print(compiled_sol)  # gives the low level code

with open("compiled_code.json", "w") as file:
   json.dump(
       compiled_sol, file
   )  # this line will take our compiled_sol json variable and just dump it into this file but it will keep it in the json syntax

# to deploy, we first need the bytecode of the file so that we can actually deploy it

bytecode = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["evm"][
   "bytecode"
]["object"]
# what we are doing above is when we're typing in all these words like contract, simplestorage, simple storage, is we're walking down the json file here
# its really low level stuff

# getting abi

abi = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["abi"]
# print(abi)

# to test this, we use ganache, which is a simulated or a fake blockchain that we can use to deploy our smart contracts to, interact like its a real blockchain
# ganache is going to allow us to spin up our own blockchain. Ganache is our javascript Vm
# in remix, metamask is our http provider
# we will use the rpc server url in ganache(top middle situated) to connect to this blockchain


# for connecting to ganache
w3 = Web3(
   Web3.HTTPProvider("http://127.0.0.1:7545")
)  # remember to use the documentation for web3 web3py.readthedocs.io
# next we are gonna need is the chain id or the network id
chain_id = 1337
my_address = "0x85207a32FBbB9a032E2f8564d4b526Ea17FaC4F5"
# private_key = "0x112690d5ad13f5c21795a3fa1056a3193061556e42ddad0aa78f141462cbf941"  # private Keys must start with 0x (just add it at the front). python is always gonna look for the hexadecimal version of the private key
# never hardcode your private keys, as if we push to github, they can see our private keys.
# environment variables: they are variables that we can set in our terminal and our command lines
# exported environment variables only last the duration of the shell/terminal
private_key = os.getenv(
   "PRIVATE_KEY"
)  # using this command, our python script can pull our private key from our environment variable
# .env file is where people store envioronment variables. if we do the .env file way, create another file called .gitignore which will make it harder for us to accidently push my .envfolder or .envfile to github
# loading .env in python, gotta install pip install python-dotenv and then go at the top where we write the import statement

print(
   os.getenv("PRIVATE_KEY")
)  # look at the .env file for the PRIVATE_KEY variable. we can also print private_key


# deploying

SimpleStorage = w3.eth.contract(
   abi=abi, bytecode=bytecode
)  # this doesnt mean we have deployed, it just means we have a contract down
# print(SimpleStorage)

# definition for nonce(in cryptography): an arbitrary number that can be used just once in a cryptographic communication, this is when we studied about nonce initially

# over here the account nonces just rack the number of transactions made by our account

# we can actually get our nonce by just grabbing our latest transaction count. getting latest transaction
nonce = w3.eth.getTransactionCount(my_address)
# print(nonce)
# to deploy this contract, we need to make a transaction
# we need to build our transactions. 1. Build the contract deploy transaction
# 2. Sign the transaction
# 3. Send the transaction
transaction = SimpleStorage.constructor().buildTransaction(
   {
       "gasPrice": w3.eth.gas_price,  # Otherwise you get a value error if you don't set the gasPrice
       "chainId": chain_id,
       "from": my_address,
       "nonce": nonce,
   }
)  # every contract has atleast an implied constructor
# we need to have transaction parameters (atleast a couple of parameters we always have to give)
# so we have this transaction buut we need to sign it from somebody. Since we are sending it from our address, our private key is gonna be the only key thats going to work to sign this

# print(transaction)
signed_txn = w3.eth.account.sign_transaction(transaction, private_key=private_key)

# print(signed_txn)  # this is a signed transaction

# now we want to send this signed transaction to the blockhain

# send this signed transaction
tx_hash = w3.eth.send_raw_transaction(
   signed_txn.rawTransaction
)  # we dont print anything but the transaction did go through, if we look at ganache

# one other thing thats really good practice whenever sending a transaction is we wait for some block transactions to happen

tx_receipt = w3.eth.wait_for_transaction_receipt(
   tx_hash
)  # this will have our code stop and wait for this transaction hash to go through


# now we have deployed the contract, but how do we actually interact and work with the contract

# working with a contract
# we need contract address and the contract abi

simple_storage = w3.eth.contract(
   address=tx_receipt.contractAddress, abi=abi
)  # creating this simple storage contract so we can interact with it

# when making transactions in the blockchain, there are actually 2 ways that we can interaact with them
# call -> Simulate making the call and getting a return value; calls dont make a state change; likein remix, the blue buttons were the calls, orange ones were the transacts

# transact -> actually make a state change
print(
   simple_storage.functions.retrieve().call()
)  # initial value for our retireve function (favourite number)
# print(simple_storage.functions.store(15).call())  # in simplestorage, the store function returns a value(over here 15)

# now since we want to make a transaction to actually store some value into this contract. since we want to make a transaction,
# we got to go through the same process as when we deployed this contract
# remember nonce can only be used once for each transaction, thats why this nonce is different from the previous one we used earlier at the top
store_transaction = simple_storage.functions.store(13).buildTransaction(
   {
       "chainId": chain_id,
       "from": my_address,
       "nonce": nonce + 1,
   }  # this is creating a transaction
)
# then we will sign the transaction
signed_store_txn = w3.eth.account.sign_transaction(
   store_transaction, private_key=private_key
)
# we send the transaction
send_store_tx = w3.eth.send_raw_transaction(signed_store_txn.rawTransaction)
# we wait for the transaction to finish
tx_receipt = w3.eth.wait_for_transaction_receipt(send_store_tx) ```

在 buildTransaction() 中輸入“gasPrice”:w3.eth.gas_price

如果您將solidity 版本更新為0.8.13,您將能夠擺脫這些錯誤。

快樂的黑客:)

引用自:https://ethereum.stackexchange.com/questions/122661