q9f / eth.rb

a straightforward library to build, sign, and broadcast ethereum transactions anywhere you can run ruby.
https://q9f.github.io/eth.rb
Apache License 2.0
196 stars 85 forks source link

Unable to make ERC20 Transfer or any other transfer on self-hosted GETH Blockchain/Node #258

Closed waqaarali closed 1 month ago

waqaarali commented 6 months ago

Error:

Eth::Client::ContractExecutionError (insufficient funds for gas * price + value: balance 0, tx cost 921164820000000, overshot 921164820000000)

client = Eth::Client.create(@rpc_url)
puts client.inspect
puts "Chain ID: #{client.chain_id}"

# <Eth::Contract::GBP:0x00007f83a560e890>
# <Eth::Client::Http:0x00007f83d49ef700 @id=0, @max_priority_fee_per_gas=0.101e10, @max_fee_per_gas=0.4269e11, @host="dlt.xxxx.com", @port=443, @ssl=true, @uri=#<URI::HTTPS https://dlt.xxxx.com>>
# Chain ID: 12345

puts "Contract Name: #{client.call(@contract, "name")}"
puts "Contract Symbol: #{client.call(@contract, "symbol")}"
puts "Contract Total Supply: #{client.call(@contract, "totalSupply")}"
puts "Contract Wallet Balance: #{client.call(@contract, "balanceOf", @wallet_address)}"

# Contract Name: Great British Pound Stable coin
# Contract Symbol: GBP
# Contract Total Supply: 100000000000000000000000000
# Contract Wallet Balance: 99898899000000000000000000

tx = client.transfer_erc20_and_wait(@contract, to, 1, sender_key: @private_key, legacy: true, gas_limit: 21578)
puts "Tx: #{tx}"

# Eth::Client::ContractExecutionError (insufficient funds for gas * price + value: balance 0, tx cost 921164820000000, overshot 921164820000000)

Any ideas what I may be doing wrong? I tried a normal transfer too but I keep getting the same error. My default coin for this GETH Blockchain is called "CUSH". Please advise further, thanks! I did manage to make sending transactions work via calling the RPC directly, but sadly couldn't get it to work for ERC20, so I thought this would be a better approach. Please advise further.

q9f commented 6 months ago

Hi, you don't have any Ether to pay transaction fees.

You can see it in the error message: "balance 0"

waqaarali commented 6 months ago

It should use the native token of the blockchain which is "CUSH". As you can see when I print the wallet balance, it's a number greater than 1. I've rebuilt the same code in JS and have no issues at the moment. I only seem to be getting the issue over here.

Could it be because it isn't detecting the blockchain's currency or something? Is there an extra config for this somewhere?

This blockchain is a private one I have setup using GETH.

q9f commented 6 months ago

You only print out the balance of the GBP token (99898899000000000000000000), not the CUSH balance (0?).

Try client.get_balance()

waqaarali commented 6 months ago

So how would I get this to work? My CUSH balance is 900k+ and my GBP balance (ERC-20) is 900k+. But when I try to send either CUSH (native token) or the ERC 20, it says insufficient funds as above

q9f commented 6 months ago

In that case, the node you are connected to is out of sync. How did you initialize the client?

waqaarali commented 6 months ago

It can't be out of sync, I am replicating the same functionality into Node.js and it works without any issue sadly. Either I'm missing something or something is broken.

This is how I initialize the client:

# Contract
@contract = Eth::Contract.from_abi(name: "GBP", address: @contract_address, abi: @erc20_abi)

# Client
client = Eth::Client.create(@rpc_url) # URL of RPC 
balance = client.get_balance(@wallet_address)
transfer = client.transfer_and_wait(to, 1, sender_key: @private_key, legacy: true)

# Regular Transaction

transaction = client.transfer_and_wait(to, 1, sender_key: @private_key, legacy: true)

# ERC 20

transaction_erc20 = client.transfer_erc20_and_wait(@contract, to, 0, sender_key: @private_key, legacy: true, gas_limit: 21578)

# Tried this too but same error

manual_erc20 = client.transact_and_wait(@contract, "transfer", address: to, amount: 0, sender_key: @private_key, legacy: true, gas_limit: 21578)

# These transactions are not all run at once, I have placed them here so you can see what I am trying to do

This blockchain is GETH clone with only 3 nodes (boot node, rpc endpoint & 1 miner)

q9f commented 1 month ago

There is nothing I can do here, the error you are seeing is from your client (not from my library):

insufficient funds for gas * price + value: balance 0, tx cost 921164820000000, overshot 921164820000000