EIP-5920 · Draft · Core · 2022-03-14
EIP-5920: PAY opcode
Proposes a PAY opcode that sends ether to an address without executing any of its code, removing reentrancy, griefing and wasted gas from plain ETH transfers.
What problem does it solve?
Today a contract sends ether with CALL, which hands execution to the recipient. That has side effects nobody asked for:
- Reentrancy: the recipient can call back into the sender. Even the 2300-gas stipend is enough to write transient storage (EIP-1153).
- Denial of service: the recipient can revert or burn gas, so payouts can be blocked.
- Wasted gas: running recipient code costs gas even when you only want to move value. Since EIP-7702, even EOAs can have delegated code, so "just send to an EOA" no longer guarantees no code runs.
A dedicated opcode that only moves value avoids all of this.
How it works
PAY (0xfc) pops addr then val (same order as CALL) and:
- Fails exceptionally inside a static call frame.
- Fails exceptionally if
addrhas any of its high 12 bytes set (not a clean 20-byte address). This keeps room for future address space extension. - Charges gas, marks
addrwarm, and transfersvalwei if the caller's balance is sufficient. - Pushes
1on success,0on failure. Currently only insufficient balance yields0.
No code at addr runs.
Gas is the sum of:
- account access: warm read cost if
addris already accessed, otherwise cold access cost (EIP-2929); GAS_NEW_ACCOUNTifaddrdoes not exist andvalis non-zero;GAS_CALL_VALUEifvalis non-zero.
PAY relies on networks having no empty accounts (EIP-7523).
Sample code
Pseudocode of the opcode, following the spec:
def op_pay(evm):
if evm.message.is_static:
raise ExceptionalHalt
addr = evm.stack.pop()
val = evm.stack.pop()
if addr >> 160 != 0:
raise ExceptionalHalt # high 12 bytes must be zero
addr = to_address(addr)
cost = WARM_STORAGE_READ_COST if addr in evm.accessed_addresses else COLD_ACCOUNT_ACCESS_COST
if val != 0 and not account_exists(addr):
cost += GAS_NEW_ACCOUNT
if val != 0:
cost += GAS_CALL_VALUE
charge_gas(evm, cost)
evm.accessed_addresses.add(addr)
if balance(evm.message.current_target) >= val:
move_ether(evm.message.current_target, addr, val) # no code executed
evm.stack.push(1)
else:
evm.stack.push(0)
Things to know
- Status is Draft. It is a Core change that needs a hard fork, and it is not part of any scheduled upgrade, so it is not available on mainnet.
- Contracts should not rely on the invalid-address halting behaviour; it may change.
- It does not create a new class of risk: ether can already be forced into a contract via
SELFDESTRUCT(restricted by EIP-6780) or as a block's coinbase. Contracts must never assume they control their own balance. - Until something like
PAYships, use pull-payment patterns or reentrancy guards when sending ether.