|
| 1 | +# Native Currency Support in 1inch Limit Order Protocol |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The 1inch Limit Order Protocol works with ERC-20 tokens, but doesn't handle native blockchain currency (ETH, BNB, MATIC, etc.). Native currencies cannot directly approve spending allowances or be transferred using standard ERC-20 interfaces. The `NativeOrderFactory` extension solve this problem by creating a proxy system that wraps native currency into WETH (Wrapped ETH) while maintaining full compatibility with the existing limit order infrastructure. |
| 6 | + |
| 7 | +```mermaid |
| 8 | +graph LR |
| 9 | + A[User with Native Currency] -->|Problem| B[Limit Order Protocol] |
| 10 | + B -->|Requires| C[ERC-20 Interface] |
| 11 | + |
| 12 | + A -->|Solution| D[NativeOrderFactory] |
| 13 | + D -->|Creates| E[Proxy Contract] |
| 14 | + E -->|Wraps to WETH| F[Compatible with Protocol] |
| 15 | + F --> B |
| 16 | +``` |
| 17 | + |
| 18 | +## Architecture |
| 19 | + |
| 20 | +The native order extension consists of two main contracts working in tandem: |
| 21 | + |
| 22 | +1. **NativeOrderFactory**: The factory contract that deploys deterministic proxy clones |
| 23 | +2. **NativeOrderImpl**: The implementation contract that handles WETH conversion and order management |
| 24 | + |
| 25 | +## Order Creation Flow |
| 26 | + |
| 27 | +When a user wants to create a limit order with native currency, the following sequence occurs: |
| 28 | + |
| 29 | +```mermaid |
| 30 | +sequenceDiagram |
| 31 | + participant User |
| 32 | + participant Factory as NativeOrderFactory |
| 33 | + participant Clone as Proxy Clone |
| 34 | + participant WETH |
| 35 | + participant LOP as Limit Order Protocol |
| 36 | + participant Resolver |
| 37 | + |
| 38 | + User->>+Factory: create(order) + {value: ETH} |
| 39 | + Note over Factory: Validate order parameters |
| 40 | + Factory->>Factory: Calculate deterministic address |
| 41 | + Factory->>Clone: Deploy clone (CREATE2) |
| 42 | + Factory->>Clone: depositAndApprove{value: ETH}() |
| 43 | + Clone->>WETH: deposit(ETH) |
| 44 | + WETH-->>Clone: WETH tokens |
| 45 | + Clone->>WETH: approve(LOP, amount) |
| 46 | + Clone-->>Factory: Success |
| 47 | + Factory->>Factory: Emit NativeOrderCreated event |
| 48 | + Factory-->>-User: Return clone address |
| 49 | + |
| 50 | + Note over User,Resolver: Order is now ready to be filled |
| 51 | + Resolver->>LOP: Fills the order |
| 52 | +``` |
| 53 | + |
| 54 | +### Key Steps Explained: |
| 55 | + |
| 56 | +1. **Order Validation**: The factory validates that: |
| 57 | + - The maker is the message sender |
| 58 | + - A valid receiver is specified |
| 59 | + - The native currency value matches the order's making amount |
| 60 | + |
| 61 | +2. **Deterministic Clone Creation**: Uses CREATE2 with the order hash as salt, ensuring: |
| 62 | + - The same order always produces the same proxy address |
| 63 | + - Address can be predicted before deployment |
| 64 | + - Prevents duplicate orders |
| 65 | + |
| 66 | +3. **WETH Conversion**: The proxy immediately: |
| 67 | + - Wraps the received native currency (e.g. ETH) into wrapped tokens (e.g. WETH) |
| 68 | + - Approves the Limit Order Protocol to spend the wrapped tokens |
| 69 | + |
| 70 | +## Signature Validation (ERC-1271) |
| 71 | + |
| 72 | +The proxy implements ERC-1271 to validate signatures without requiring private keys: |
| 73 | + |
| 74 | +```mermaid |
| 75 | +flowchart TD |
| 76 | + A[LOP requests signature validation] --> B[isValidSignature called on proxy] |
| 77 | + B --> D[Calculate expected proxy address from order hash] |
| 78 | + D --> E{Address matches?} |
| 79 | + E -->|No| F[Return invalid] |
| 80 | + E -->|Yes| G[Patch order maker to proxy address] |
| 81 | + G --> H[Calculate order hash] |
| 82 | + H --> I{Hash matches?} |
| 83 | + I -->|No| F |
| 84 | + I -->|Yes| J[Return valid signature selector] |
| 85 | + |
| 86 | + style A fill:#f9f,stroke:#333,stroke-width:2px |
| 87 | + style J fill:#bfb,stroke:#333,stroke-width:2px |
| 88 | + style F fill:#fbb,stroke:#333,stroke-width:2px |
| 89 | +``` |
| 90 | + |
| 91 | +This mechanism allows the proxy to "sign" orders without having a private key, using deterministic addressing as proof of validity. |
| 92 | + |
| 93 | +## Cancellation Mechanisms |
| 94 | + |
| 95 | +Native order can be cancelled either by Maker itself or by a Resolver. Cancellation by Resolver requires small part of native currency to compensate for gas costs and incentivize the cancellation. |
| 96 | + |
| 97 | +##### User Cancellation |
| 98 | +- Only the original order maker can cancel their own order |
| 99 | +- Returns the full remaining wrapped token balance as native currency |
| 100 | +- No time restrictions |
| 101 | + |
| 102 | +##### Resolver Cancellation |
| 103 | +- Requires holding the access token for the Resolver |
| 104 | +- Only works on expired orders |
| 105 | +- Provides gas compensation to the resolver (capped at `basefee * 30,000 * 1.1`) |
| 106 | + |
| 107 | +```mermaid |
| 108 | +flowchart TD |
| 109 | + A[Order Active] --> B{Who cancels?} |
| 110 | + B -->|Original Maker| C[cancelOrder] |
| 111 | + B -->|Resolver| D[cancelExpiredOrderByResolver] |
| 112 | + |
| 113 | + C --> E[Verify maker identity] |
| 114 | + E --> F[Withdraw WETH to ETH] |
| 115 | + F --> G[Send ETH to maker] |
| 116 | + |
| 117 | + D --> H{Order expired?} |
| 118 | + H -->|No| I[Revert] |
| 119 | + H -->|Yes| J{Cancellation delay passed?} |
| 120 | + J -->|No| I |
| 121 | + J -->|Yes| K[Calculate resolver reward] |
| 122 | + K --> L[Withdraw WETH to ETH] |
| 123 | + L --> M[Send reward to resolver] |
| 124 | + L --> N[Send remainder to maker] |
| 125 | + |
| 126 | + style A fill:#bbf,stroke:#333,stroke-width:2px |
| 127 | + style G fill:#bfb,stroke:#333,stroke-width:2px |
| 128 | + style M fill:#bfb,stroke:#333,stroke-width:2px |
| 129 | + style N fill:#bfb,stroke:#333,stroke-width:2px |
| 130 | + style I fill:#fbb,stroke:#333,stroke-width:2px |
| 131 | +``` |
| 132 | + |
| 133 | +### Fund Recovery |
| 134 | +The `rescueFunds` function allows recovery of accidentally sent tokens while protecting active order funds: |
| 135 | +- For wrapped tokens: Only allows withdrawal of excess funds beyond the active order amount |
| 136 | +- For other tokens: Direct transfer to specified recipient |
| 137 | +- Restricted to resolver role for additional security |
0 commit comments