# Rex2

This page is an informative summary of the Rex2 specification. For the full normative definition, see the Rex2 spec in the mega-evm repository.

## Summary

Rex2 makes two changes. First, it restores the `SELFDESTRUCT` opcode with post-Cancun ([EIP-6780](https://eips.ethereum.org/EIPS/eip-6780)) semantics — MiniRex had completely disabled it. Second, it introduces the [**KeylessDeploy**](https://docs.megaeth.com/spec/system-contracts/keyless-deploy) system contract, which enables deterministic cross-chain contract deployment via Nick's Method with a gas limit override to account for MegaETH's different [gas pricing](https://docs.megaeth.com/spec/megaevm/dual-gas-model).

## What Changed

### SELFDESTRUCT Re-Enabled (EIP-6780)

#### Previous behavior

* `SELFDESTRUCT` halts execution with `InvalidFEOpcode`.

#### New behavior

* `SELFDESTRUCT` is a valid opcode.
* If the contract was created in the same transaction, `SELFDESTRUCT` removes the contract's code and storage and transfers the remaining balance to the target address.
* If the contract was not created in the same transaction, `SELFDESTRUCT` only transfers the remaining balance to the target address — code and storage are preserved.

This is the standard EIP-6780 behavior already used across Ethereum post-Cancun.

### Oracle Bytecode Update

#### Previous behavior

* Oracle bytecode version `1.0.0` (code hash `0xe9b044afb735a0f569faeb248088b4f267578f60722f87d06ec3867b250a2c34`).
* No `sendHint` function.

#### New behavior

* Oracle bytecode version `1.1.0` (code hash `0x06df675a69e53ea2a3c948521e330b3801740fede324a1cef2044418f8e09242`).
* Adds the `sendHint(bytes32,bytes)` function to the deployed bytecode.
* Introduces [call interception](https://docs.megaeth.com/spec/system-contracts/interception) for `sendHint` hint forwarding.

### KeylessDeploy System Contract

#### Previous behavior

* No system-level support for keyless deployment with gas limit overrides.
* Contracts that deploy via keyless transactions on Ethereum may run out of gas on MegaETH due to different gas pricing.

#### New behavior

* A system contract at `0x6342000000000000000000000000000000000003` provides keyless deployment.
* The contract intercepts calls at depth 0 only (direct transaction calls).

```solidity
interface IKeylessDeploy {
    function keylessDeploy(
        bytes calldata keylessDeploymentTransaction,
        uint256 gasLimitOverride
    ) external returns (uint64 gasUsed, address deployedAddress, bytes memory errorData);
}
```

* `keylessDeploymentTransaction` is the RLP-encoded pre-EIP-155 transaction.
* `gasLimitOverride` replaces the gas limit in the original transaction.
* The sandbox charges a fixed overhead of 100,000 gas (deducted from the caller's total gas, tracked as [compute gas](https://docs.megaeth.com/spec/reference/glossary#compute-gas) from Rex3 onward) for RLP decoding, signature recovery, and state filtering.
* Calls from other contracts (depth > 0) are not intercepted and fall through to on-chain bytecode, reverting with `NotIntercepted()`.
* Value-bearing calls are rejected.
* Unknown selectors are not intercepted and fall through to on-chain bytecode.

## Developer Impact

**SELFDESTRUCT is available again.** Contracts that previously needed workarounds for the disabled opcode can now use `SELFDESTRUCT` with EIP-6780 semantics. Only contracts created in the same transaction can be fully destroyed — pre-existing contracts will only have their balance transferred.

**Contracts can be deployed via Nick's Method on MegaETH.** The KeylessDeploy system contract executes pre-signed keyless deployment transactions with an adjusted gas limit. The `gasLimitOverride` parameter accounts for MegaETH's different gas costs without changing the signed transaction (which would change the deployment address).

**KeylessDeploy only works at depth 0.** Calling KeylessDeploy from within another contract is not intercepted — it must be the direct transaction target. This prevents wrap-and-revert attacks.

## Safety and Compatibility

All pre-Rex2 behavior is unchanged.

The SELFDESTRUCT restoration uses the same [EIP-6780](https://eips.ethereum.org/EIPS/eip-6780) semantics already implemented in the underlying revm. The "same transaction" check means only contracts created via CREATE or CREATE2 within the currently executing transaction are eligible for full destruction.

KeylessDeploy sandbox state is merged into main execution on both success and execution failure. On execution failure the sandbox itself has already reverted most state changes, so the merged state contains only the signer's nonce increment and balance deduction — not the failed deployment artifacts. Validation failures do not reach sandbox execution and therefore do not merge any state.

## References

* [mega-evm repository](https://github.com/megaeth-labs/mega-evm)
* [Keyless Deployment](https://docs.megaeth.com/spec/system-contracts/keyless-deploy) — detailed usage guide and examples
* [EIP-6780](https://eips.ethereum.org/EIPS/eip-6780) — SELFDESTRUCT only in same transaction
