i'm studyng ethereum and i have a question, how can ethereum, during the transaction, verify that my balance is enough for execute the transaction? It is the current smart contract that doeas this check, of is the EVM, that in some way, retrieve data from the world state tree? Thank you in adavance!
1 Answers
ETH balance:
The blockchain stores state changes, which are used to calculate the current ETH balance of an address.
You can theoretically create and broadcast a transaction that spends more than your current balance. But the tx is going to revert.
Validation whether the sender has enough balance to cover the gas fee and tx value
, is performed on the EVM level. You can find its implementation for example in the go-ethereum
source code. It first checks if the sender can buy enough gas (preCheck() calls buyGas()
that errors if the balance is not sufficient), and then checks if the sender has enough balance to cover the value
(
canTransfer()
reference and definition).
Token balance:
The token balance is stored in the token contract storage. (In some cases, the balances might be stored in another contract, but it's still some contract's storage.)
Most token contracts' logic contains validation whether the sender has enough token balance to send the tokens. If they don't have enough token balance, the contract creates an invalid EVM opcode, that results in reverting the transaction (so the token balance is not changed). Or sometimes the contract just lets the Ethereum transaction pass, but it doesn't update any token balance.
Example code of the validation on OpenZeppelin's implementation of ERC-20 token: GitHub link (the require()
statement in the _transfer()
function)
Only few token contracts are faulty and don't have this validation. But a faulty implementation might allow the sender to send more tokens than they currently own.

- 40,554
- 8
- 72
- 100
-
I feel that this does not answer the question. My understanding of the question is: When does a miner/node check that an account has enough Ether to pay for gas costs? Is it at the JSON-RPC level, the EVM, or somewhere else? I am asking myself the same question – Donut Nov 17 '21 at 14:20
-
@Donut Thank you for the followup. I found a logical mistake in the first half of my original answer (my knowledge about the topic was more limited 8 months ago when I first posted it) and corrected that, along with explaining the validation that you're asking about. – Petr Hejda Nov 17 '21 at 15:34
-
Great :) Nice finding and great that you added that information to your answer. I was looking for the functionality myself. – Donut Nov 18 '21 at 06:10