'Python/Rinkeby: Problem setting gas allowance flag
I'm trying to send a first transaction with Infura/Metamask - with Ganache it worked great but here I get an error message saying that:
ValueError: {'code': -32000, 'message': 'gas required exceeds allowance (0)'}
I'm very new to this, is it possible maybe to add the allowance manually to store_transaction?
This is the deploy.py file:
from solcx import compile_standard, install_solc
import json
from web3 import Web3
import os
from dotenv import load_dotenv
load_dotenv()
install_solc("0.6.0")
with open("./SimpleStorage.sol", "r") as file:
simple_storage_file = file.read()
# compile solidity
compiled_sol = compile_standard(
{
"language": "Solidity",
"sources": {"SimpleStorage.sol": {"content": simple_storage_file}},
"settings": {
"outputSelection": {
"*": {"*": ["abi", "metadata", "evm.bytecode", "evm.sourceMap"]}
}
},
},
solc_version="0.6.0",
)
with open("compiled_code.json", "w") as file:
json.dump(compiled_sol, file)
# get bytecode
bytecode = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["evm"][
"bytecode"
]["object"]
# get abi
abi = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["abi"]
# for connecting to rinkeby
w3 = Web3(
Web3.HTTPProvider(
"https://rinkeby.infura.io/v3/f6f502ef337540f2ad6c48981e5b410d"
)
)
chain_id = 4
my_address = "0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1"
private_key = os.getenv("PRIVATE_KEY")
# Create the contract in python
SimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode)
# Get the latest transaction
nonce = w3.eth.getTransactionCount(my_address)
# 1. Build a transaction
# 2. Sign a transaction
# 3. Send a transaction
transaction = SimpleStorage.constructor().buildTransaction(
{
"gasPrice": w3.eth.gas_price,
"chainId": chain_id,
"from": my_address,
"nonce": nonce,
}
)
signed_txn = w3.eth.account.sign_transaction(transaction, private_key=private_key)
# Send this signed transaction
print("Deploying contract ...")
tx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
print("Contract deployed!")
# Working with the contract
# Contract Address
# Contract ABI
simple_storage = w3.eth.contract(address=tx_receipt.contractAddress, abi=abi)
# Call --> Simulate making the call and getting a value
# Transact --> Actually make a state change
print("Updating contract ...")
print(simple_storage.functions.retrieve().call())
store_transaction = simple_storage.functions.store(15).buildTransaction(
{
"gasPrice": w3.eth.gas_price,
"chainId": chain_id,
"from": my_address,
"nonce": nonce + 1,
}
)
signed_store_txn = w3.eth.account.sign_transaction(
store_transaction, private_key=private_key
)
send_store_tx = w3.eth.send_raw_transaction(signed_store_txn.rawTransaction)
tx_receipt = w3.eth.wait_for_transaction_receipt(send_store_tx)
print(simple_storage.functions.retrieve().call())
print("Updated!")
The error log that I get is as follows:
Traceback (most recent call last): File "deploy.py", line 56, in transaction = SimpleStorage.constructor().buildTransaction( File "/home/enrique/.local/lib/python3.8/site-packages/eth_utils/decorators.py", line 18, in _wrapper return self.method(obj, *args, **kwargs) File "/home/enrique/.local/lib/python3.8/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 "/home/enrique/.local/lib/python3.8/site-packages/web3/_utils/transactions.py", line 114, in fill_transaction_defaults default_val = default_getter(web3, transaction) File "/home/enrique/.local/lib/python3.8/site-packages/web3/_utils/transactions.py", line 60, in 'gas': lambda web3, tx: web3.eth.estimate_gas(tx), File "/home/enrique/.local/lib/python3.8/site-packages/web3/eth.py", line 855, in estimate_gas return self._estimate_gas(transaction, block_identifier) File "/home/enrique/.local/lib/python3.8/site-packages/web3/module.py", line 57, in caller result = w3.manager.request_blocking(method_str, File "/home/enrique/.local/lib/python3.8/site-packages/web3/manager.py", line 198, in request_blocking return self.formatted_response(response, File "/home/enrique/.local/lib/python3.8/site-packages/web3/manager.py", line 171, in formatted_response raise ValueError(response["error"]) ValueError: {'code': -32000, 'message': 'gas required exceeds allowance (0)'}
I'm pretty sure this can be resolved by adding the proper flag, but how do I set a gas allowance? Can anybody help please?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
