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 network would refuse this transaction as invalid:
Most likely you would not find any miner that would include this transaction in a block. And if you had a miner that would include the tx in a block, rest of the network would refuse this (invalid) block, and accept someone else's (valid) block.
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.