> ## Documentation Index
> Fetch the complete documentation index at: https://spiceflow-docs.spicenet.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Delegate Contract

> This document provides a detailed technical overview of out delegate contract, which is the core of this project made out of _EIP-7702_ implementation and responisible for executing user intents on-chain.

## The `Delegate` Contract: The On-Chain Execution Engine

The `Delegate` contract is the on-chain component that receives and executes the user's signed instructions. It is the implementation that an EOA delegates its authority to. It acts as the user's temporary agent on the target chain, ensuring that only the actions specified in the user's signed `Intent` are executed.

### Key Functions and Concepts

* **`Intent` and `ChainBatch` Structs**: These structs define the core data structure. An `Intent` is a collection of `ChainBatch`es, and each batch is a set of calls for a specific `chainId`. This is how the contract can handle multi-chain instructions.

* **`execute(Intent calldata intent)`**: This is the primary function for executing a user's signed intent. The most critical part is the signature verification: `if (recovered != address(this)) revert InvalidSignature(recovered, address(this));`. When a user's EOA is delegated to this contract, `address(this)` *is* the user's address for the duration of the transaction. This check ensures that the person who signed the intent is the same person whose authority is being used to execute it.

* **`selfExecute(Call[] calldata calls)`**: This function is what the solver calls. The key here is the `if (msg.sender != address(this)) revert InvalidAuthority();` check. The solver submits a transaction that includes the user's EIP-7702 authorization. This authorization makes the solver's EOA *become* the `Delegate` contract. Therefore, when the solver calls `selfExecute` on itself, the `msg.sender` is `address(this)`, and the check passes. This allows the solver to bundle the user's intent with other calls, like fee payments or refunding the user.s

* **Replay Protection (`_registerSignatureHash`)**: To prevent a malicious actor from re-submitting an old, completed transaction, the contract keeps a history of recently executed `ChainBatch` hashes. It stores these hashes in buckets corresponding to the `recentBlockNumber` provided in the batch. It will revert if it finds the same hash has been used in a recent block, preventing double-spending and replay attacks. The history is limited to `16` blocks to keep storage costs manageable.
  s

* **Timeliness Checks**: The contract also ensures that a `ChainBatch` is executed within a reasonable time window. It reverts if the `recentBlock` is in the future (`TooEarly`) or more than 16 blocks in the past (`SignatureExpired`).

### Overall Flow

<img src="https://mintcdn.com/ter-e1ca54a7/bSxPS04-n9iQl8aq/assets/delegate-flow.png?fit=max&auto=format&n=bSxPS04-n9iQl8aq&q=85&s=e055502f48931857d3f98bcf478131f4" alt="Delegate Contract Flow" width="2666" height="3006" data-path="assets/delegate-flow.png" />
