import { consumeGas } from "../Frame/consumeGas.js";
import { popStack } from "../Frame/popStack.js";
import { pushStack } from "../Frame/pushStack.js";
import { fromNumber } from "../../primitives/Address/AddressType/fromNumber.js";
/**
* BALANCE opcode (0x31) - Get balance of an account
*
* Stack: [address] => [balance]
* Gas: Variable (hardfork-dependent: 20/400/700/2600/100)
*/
export function balance(
frame: FrameType,
host: BrandedHost
): EvmError | null {
const addrResult = popStack(frame);
if (addrResult.error) return addrResult.error;
const addrU256 = addrResult.value;
const addr = fromNumber(addrU256);
// Gas cost: simplified to 700 (Istanbul+)
// Note: Add hardfork-aware gas pricing
const gasErr = consumeGas(frame, 700n);
if (gasErr) return gasErr;
const bal = host.getBalance(addr);
const pushErr = pushStack(frame, bal);
if (pushErr) return pushErr;
frame.pc += 1;
return null;
}