Is there a way to solve this insufficient funds for gas when deploying a signed transaction to ganache?

3.9k Views Asked by At

Good day, everyone! I'm currently taking a 16-hour freecodecamp course on Solidity, Blockchain, and Smart Contracts, and I'm having trouble sending a simple signed transaction to Ganache and I keep getting this Value error message "ValueError: {'message': 'insufficient funds for gas * price + value', 'stack': 'Error: insufficient funds for gas * price + value\n at TransactionPool.prepareTransaction (/home/fingergod/.nvm/versions/node/v17.8.0/lib/node_modules/ganache/dist/node/1.js:2:131154)', 'code': -32003}".

P.S. I've already set my gas price to "gasPrice" while building transactions to be: "gasPrice": w3.eth.gas _price

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.8.13")

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

# compile solidity file
Compiled_solFile = compile_standard(
    {
        "language": "Solidity",
        "sources": {"SimpleStorage.sol": {"content": simple_storage_file}},
        "settings": {
            "outputSelection": {
                "*": {"*": ["abi", "metadata", "evm.bytecode", "evm.sourcecode"]}
            }
        },
    },
    solc_version="0.8.13",
)
# print(Compiled_solFile)

with open("compiled_code.json", "w") as file:
    json.dump(Compiled_solFile, file)

    # get bytecode
    bytecode = Compiled_solFile["contracts"]["SimpleStorage.sol"]["simpleStorage"][
        "evm"
    ]["bytecode"]["object"]
    # get abi
    abi = Compiled_solFile["contracts"]["SimpleStorage.sol"]["simpleStorage"]["abi"]
# print(abi)

# for conneecting to ganache
url = "hTTP://127.0.0.1:8545"
w3 = Web3(Web3.HTTPProvider(url))
chain_id = 1337
my_address = "0x15f029FEB462294b117AD56b1736c551c64a4D80"
private_key = os.getenv("PRIVATE_KEY")
print(private_key)

# Create the contract in python
SimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode)
print(SimpleStorage)

# get the nonce/latest transaction count
nonce = w3.eth.getTransactionCount(my_address)
print(nonce)

# 1. Build the transacion(needs;chainid,address,nonce)
# 2. Sign the transaction(needs;transaction,privatekey)
# 3. Send the signed transaction

# 1.
transaction = SimpleStorage.constructor().buildTransaction(
    {
        "chainId": chain_id,
        "gasPrice": w3.eth.gas_price,
        "from": my_address,
        "nonce": nonce,
    }
)
print(transaction)

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

# 3.
tx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)

Any assistance would be highly appreciated. I've been stranded here for the past two days.

2

There are 2 best solutions below

0
On

I think this has to do with your gasLimit value due to gas * price being equal to how much gas you are sending and not how much gas the operation costs. You might have seen from the course you are taking, but as a reminder; when you are sending a transaction to a contract, you are sending the amount of gas which is specified in the gasLimit attribute independent of how much it will cost. That means you are actually sending Wei equal to the gasLimit * gasPrice + value. Try to set your gasLimit attribute (in hex format for consistency) in transaction object to a value lower than your network default and maybe the gasPrice as well.

This should be an issue with insufficient account balance. Try sending the same transaction using an high-level function like transact() to see if it succeeds. If so, you can replicate the gasLimit and gasPrice from the tx details for your build_transaction().

Another note: You can omit the "gasPrice": w3.eth.gas_price line, because you are assigning the network default value, which is done automatically if you omit it, so there is no point to it unless you are going to assign another custom value.

0
On

If you are using the yarn package Ganache like I am the first wallet wont work even if its showing you have 1000eth present it wont work just put the secret key of the second wallet and check if it has balance

const balance = await wallet.getBalance();
console.log("Account balance:", ethers.utils.formatEther(balance));

This solution worked for me