Web3j Transfer sendFunds returns Error insufficient funds for gas price value

0 votes

When using the web3j lib for blockchain transaction in my private test blockchain I'm currently running into the titled response error: *insufficient funds for gas * price + value*

The account from which I want to transfer some ether has a balance of 10000 ether. The gas price I logged out has a value of 18000000000 as BigInt (it's WEI?) and the gas limit is the default used from the web3j has a value of 21000.

So the question is why I getting the transaction not working?I want to transfer 10 ether for example:

TransactionReceipt transactionReceipt = Transfer.sendFunds(web3, credentials, toAccount, BigDecimal.valueOf(10.0), Convert.Unit.ETHER).send();

More details

the genesis file looks like this:

{
  "config": {
      "chainId": 9999,
      "homesteadBlock": 0,
      "eip155Block": 0,
      "eip158Block": 0,
      "byzantiumBlock": 0
   },
   "difficulty": "400",
   "gasLimit": "2100000",
   "alloc": {
      "0x9b6301bf2cfe11066dbd641f91a2b82e0783130e": { 
          "balance": "100000000000000000000000" 
      }
   }
}

The code looks like this:

// create new account 
Admin admin = Admin.build(new HttpService());
NewAccountIdentifier newAccount = admin.personalNewAccount("PASSWORD").send();

// get current created account
Web3j web3 = Web3.build(new HttpService());
EthAccounts accounts = web3.ethAccounts().send();
String lastAccount = Iterables.getLast(accounts.getAccounts());
// get creadentials for the first account having some ether
String firstAccount = web3.ethAccounts().send().getAccounts().get(0);
Credentials credentials = Credentials.create(firstAccount);

// get current balance for first account
EthGetBalance balance = admin.ethGetBalance(firstAccount, DefaultBlockParameterName.LATEST).send();
BigDecimal balanceVaue = Convert.fromWei(balance.getBalance().toString(), Convert.Unit.ETHER);

// create transaction to give the new created account some ether from the first one
// log some stuff
System.out.println("Account: " + firstAccount);
System.out.println("Account balance: " + balanceVaue);
System.out.println("Gas Price admin.ethGasPrice() in Ether: " + Convert.fromWei(gasPrice.toString(), Convert.Unit.ETHER));
System.out.println("Transfer Gas Limit in Ether: " + Convert.fromWei(Transfer.GAS_LIMIT.toString(), Convert.Unit.ETHER));
System.out.println("Transfer Gas Price in Ether: " + Convert.fromWei(Transfer.GAS_PRICE.toString(), Convert.Unit.ETHER));

TransactionReceipt transactionReceipt = Transfer.sendFunds(web3, credentials, lastAccount, BigDecimal.valueOf(10.0), Convert.Unit.ETHER).send();
String transactionHash = transactionReceipt.getTransactionHash();

This causes the above transaction error: insufficient funds for gas * price + value

Here is the logging output:

Account: 0x9b6301bf2cfe11066dbd641f91a2b82e0783130e
Account balance: 100000
Gas Price admin.ethGasPrice() in Ether: 1.8E-8
Transfer Gas Limit in Ether: 2.1E-14
Transfer Gas Price in Ether: 2.2E-8
Funds transfer triggered ...
Jun 16, 2018 in Blockchain by aryya
• 7,450 points
2,758 views

1 answer to this question.

0 votes

DISCLAIMER. This may not necessarily be an answer to your question. But I wanted to post it as an answer, as a comment does not allow me to express the below that easily.

mutable struct Blockchain
    chain::Array{Dict{String, Any}}
    current_transactions::Array{String}
    block::Dict{String, Any}
    new_block::Function

    function Blockchain(chain::Array{Dict{String, Any}}, current::Array{String})    
        new(chain, current, new_block(previous_hash=1, proof=100)) 
        # ^issue calling function within the constructor here
    end
end

Here, I assume that you are trying to add some member function functionality to your struct, as you have already stated that you are coming from a C++ background. However, this is not Julian. In Julia, as @crstnbr has already suggested, we need to define global functions that act on objects. The convention is that you add ! at the end of the function to indicate that the function will be changing at least one of its arguments.

Then, by checking the definition of your new_block:

function new_block(proof::String, previous_hash::String=nothing)
    block = Dict(
    "index" => length(chain) + 1, # which chain?
    "timestamp" => time(),
    "transactions" => current_transactions, # which current_transactions?
    "proof" => proof,
    "previous_hash" => previous_hash | hash(chain[end]), # which chain?
    )
    current_transactions = []
    append!(chain, block) # which chain?
    return block
end
I notice a couple of serious mistakes. First, there are some undefined variables that you try to use such as chain and current_transactions. I assume, again from C++, you have thought that new_block would be a member function of Blockchain, and hence, could see its chain member variable. This is not how Julia works. Second problem is how you try to call new_block:
new_block(previous_hash=1, proof=100)

This call is totally wrong. The above call notation relies on keyword arguments; however, your function definition only has positional arguments. To be able to support keyword arguments, you need to change your function definition to read as below:

function new_block(; proof::String, previous_hash::String=nothing)
  #                ^ note the semi-colon here
  # ...
end

And last, you define proof and previous_hash to be of type String, but call them with 1, 100and nothing, which are of type Int, Int and Void.

I could not understand your design choice for the Blockchain application in your mind, but I strongly suggest that you should go step-by-step with simpler examples to learn the language. For instance, if you just try the below examples, you will understand how type annotations work in Julia:

Main> f(s::String = nothing) = s
f (generic function with 2 methods)

Main> f()
ERROR: MethodError: no method matching f(::Void)
Closest candidates are:
  f(::String) at none:1
  f() at none:1
Stacktrace:
 [1] f() at ./none:1
 [2] eval(::Module, ::Any) at ./boot.jl:235

Main> g(s::String) = s
g (generic function with 1 method)

Main> g(100)
ERROR: MethodError: no method matching g(::Int64)
Closest candidates are:
  g(::String) at none:1
Stacktrace:
 [1] eval(::Module, ::Any) at ./boot.jl:235

Main> h1(var1 = 1, var2 = 100) = var1 + var2
h1 (generic function with 3 methods)

Main> h1(var2 = 5, var1 = 6)
ERROR: function h1 does not accept keyword arguments
Stacktrace:
 [1] kwfunc(::Any) at ./boot.jl:237
 [2] eval(::Module, ::Any) at ./boot.jl:235

One last comment is that as far as I can see from your example, you do not need mutable struct. struct should simply help you with your design --- you can still add to/modify its chain, current_transactions and block variables. Check, again, the simpler example below:

Main> struct MyType
         a::Vector{Float64}
       end

Main> m = MyType([1,2,3]);

Main> append!(m.a, 4);

Main> m
MyType([1.0, 2.0, 3.0, 4.0])

You can think of MyType's a variable, in the above example, as being double * const a in C++terms. You are not allowed to change a to point to a different memory location, but you can modify the memory location pointed-to by a.

In short, you should definitely try to learn the language from the official documentation step-by-step, and post questions here involving really minimal examples. Your example is convoluted in that sense.

answered Jun 16, 2018 by charlie_brown
• 7,720 points

Related Questions In Blockchain

0 votes
1 answer

How to solve “insufficient funds for gas * price + value” error?

Change the following line Credentials credentials = Credentials.create(firstAccount); To ...READ MORE

answered Aug 1, 2018 in Blockchain by slayer
• 29,350 points
5,265 views
0 votes
1 answer

Web3j v3.3.1 : Error while generating compiled solidity smart contracts which returns array of struct

Solidity does not support returning structs in ...READ MORE

answered Jun 19, 2018 in Blockchain by aryya
• 7,450 points
1,422 views
0 votes
1 answer
0 votes
1 answer

Transfer the gas value of contract to my own address

Works fine for me: pragma solidity ^0.4.0; contract Test ...READ MORE

answered Oct 10, 2018 in Blockchain by Omkar
• 69,210 points
374 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,703 views
+1 vote
1 answer
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,237 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP