Opcodes · stack · enabled

OP_DEPTH (0x74)

Pushes the number of items on the stack.

Verified
Stack in → out
∅ → n
Since
Original Bitcoin script
Cost
Base instruction cost 100 plus bytes pushed (2025 VM limits).
Bitcoin status
Same in Bitcoin (BTC)

Example

OP_1 OP_2 OP_DEPTH OP_2 OP_NUMEQUALVERIFY OP_DROP
Step Stack after (top on the right) What happens
OP_1 1
OP_2 1 2
OP_DEPTH 1 2 2 Two items were on the stack, so it pushes 2.
OP_2 1 2 2 2
OP_NUMEQUALVERIFY 1 2 The count matched, so the script goes on.
OP_DROP 1 One true item is left, as the clean-stack rule requires.

Step through it opcode by opcode in Bitauth IDE, the in-browser BCH VM debugger.

Where OP_DEPTH is used

  • Contracts Hand-written scripts, with OP_NUMEQUALVERIFY : A script can start with OP_DEPTH <n> OP_NUMEQUALVERIFY to reject an unlocking script that supplies the wrong number of items. CashScript does not emit OP_DEPTH; it tracks stack positions at compile time.
  • Implementation Libauth BCH VM : The reference JavaScript implementation that Bitauth IDE runs.

Sources

  1. Libauth BCH_2023_05 opcodes (bch-2023-opcodes.ts)