full-blockchain-solidity-course-py icon indicating copy to clipboard operation
full-blockchain-solidity-course-py copied to clipboard

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

Open Kaditcuy opened this issue 2 years ago • 1 comments

Good day, everyone! I 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.

Kaditcuy avatar Apr 07 '22 09:04 Kaditcuy

Hello @Kaditcuy have you enough funds on your wallet? if not you should revisit a faucet and get more eth fo the transaction fees.

cromewar avatar Apr 17 '22 12:04 cromewar