# Decorators

**1. Decorator Architecture Overview**

***Behavioral Add-ons for Trustless Task Execution***

```mermaid
flowchart LR  
    BaseTask[Base Task] --> Decorate  
    Decorate -->|Attach| Security[Security Decorator]  
    Decorate -->|Attach| Compliance[Compliance Decorator]  
    Decorate -->|Attach| Automation[Auto-Retry Decorator]  
    style Decorate fill:#8f8,stroke:#333  
```

**Key Properties**:

* Immutable via ERC-5643 standard
* Stackable execution order
* Gas-aware priority scheduling

***

#### **2. Core Decorator Types**

**Security Decorators**

```solidity
// contracts/SecurityDecorator.sol  
function applyZKProof(  
    bytes32 taskHash,  
    bytes calldata zkProof  
) external override {  
    require(verifyProof(taskHash, zkProof), "Invalid Proof");  
    _attachDecorator(taskHash, DecoratorType.ZK_PROTECTED);  
}  
```

*Sample Use Cases*:

* Private transaction validation
* Hidden bidding strategies

**Compliance Decorators**

```yaml
# compliance-decorator.yaml  
type: "aml_check"  
chains:  
  - polygon  
  - avalanche  
params:  
  kyc_provider: "Chainalysis"  
  risk_threshold: "0.8"  
  auto_report: true  
```

*Enforced By*:

* FATF Travel Rule V2
* EU MiCA article 45

**Automation Decorators**

```python
class RetryDecorator(TaskDecorator):  
    def __init__(self, max_retries=3, backoff=1.5):  
        self._max_retries = max_retries  
        self._backoff = backoff  

    def execute(self, task):  
        for attempt in range(self._max_retries):  
            try:  
                return task.execute()  
            except BlockchainError as e:  
                sleep(self._backoff ** attempt)  
        raise AutoFailException(task)  
```

***

#### **3. Decorator Stacking Protocol**

**Execution Order Priority**

```mermaid
graph TD  
    A[Pre-Hook Decorators] --> B[Core Task]  
    B --> C[Post-Hook Decorators]  
    A --> A1[Gas Optimization]  
    A --> A2[Input Validation]  
    C --> C1[Fee Payment]  
    C --> C2[Audit Trail]  
```

**Priority Matrix**

| Decorator Type    | Execution Phase | Gas Impact |
| ----------------- | --------------- | ---------- |
| Input Validation  | Pre-Execution   | -12%       |
| Risk Analysis     | Pre-Execution   | +8%        |
| Gas Refunder      | Post-Execution  | -24%       |
| Multi-Sig Approve | Post-Execution  | +15%       |

***

#### **4. Decorator Management**

**On-Chain Registration**

```solidity
contract DecoratorRegistry {  
    struct Decorator {  
        address developer;  
        uint256 securityDeposit;  
        bytes32 versionHash;  
    }  

    mapping(bytes32 => Decorator) public decorators;  

    function registerDecorator(  
        bytes32 decoratorHash,  
        bytes calldata wasmBinary  
    ) external payable {  
        require(msg.value >= 500 AGPT, "Insufficient deposit");  
        decorators[decoratorHash] = Decorator(  
            msg.sender,  
            msg.value,  
            keccak256(wasmBinary)  
        );  
    }  
}  
```

**DAO Governance Controls**

```yaml
governance_rules:  
  decorator_approval:  
    quorum: 40%  
    voting_period: 3 days  
  malicious_decorator:  
    slash_percent: 85%  
    blacklist_duration: 180 days  
```

***

#### **5. Implementation Example**

**DeFi Swap with Decorators**

```yaml
task: "cross_chain_swap"  
params:  
  from: ETH  
  to: AGPT  
  amount: 1.5  
decorators:  
  pre_execution:  
    - type: "slippage_check"  
      max_slippage: 1.5%  
    - type: "kyc_verifier"  
      provider: "Jumio"  
  post_execution:  
    - type: "fee_optimizer"  
      strategy: "gas_refund_v2"  
    - type: "tax_report"  
```

**Resulting Workflow**

```mermaid
sequenceDiagram  
    Task->>SlippageCheck: Validate ≤1.5%  
    SlippageCheck->>KYCVerifier: Confirm Wallet  
    KYCVerifier->>Swap: Execute Trade  
    Swap->>FeeOptimizer: Claim Gas Refund  
    FeeOptimizer->>TaxReport: Generate Form 8949  
```

***

#### **6. Decorator Use Case Matrix**

| Industry   | Critical Decorators                  | Risk Mitigation |
| ---------- | ------------------------------------ | --------------- |
| Healthcare | HIPAA Logger, ZK-PII Masking         | 98%             |
| Gaming     | Anti-Cheat Verifier, NFT Royalties   | 89%             |
| Government | FOIA Auditor, Voting Proof Archiver  | 100%            |
| Energy     | Carbon Credit Tracker, Grid Balancer | 95%             |

***

#### **7. Debugging & Optimization**

**Common Issues Table**

| Error Code | Symptom             | Resolution Protocol          |
| ---------- | ------------------- | ---------------------------- |
| DEC-401    | Decorator Collision | Use `DecoratorResolver` CLI  |
| DEC-412    | Priority Loop       | Enable Stack Analyzer        |
| DEC-430    | Gas Overdraw        | Attach GasEstimator Pre-Hook |

**Optimization Techniques**

```bash
agentgpt decorator analyze \  
  --task "high_frequency_trade" \  
  --recommend \  
  --gas-limit 2500000  
```

***

**Version Compatibility**:

* ERC-5643 Decorator Standard v2.1
* AgentGPT Core v3.8+


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://agent-gpt.gitbook.io/agent-gpt/concepts/decorators.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
