# ConfidentialERC20.sol

### Overview

[ConfidentialERC20.sol](https://github.com/FhenixProtocol/redact/blob/master/packages/hardhat/contracts/ConfidentialERC20.sol) (and [ConfidentialETH.sol](https://github.com/FhenixProtocol/redact/blob/master/packages/hardhat/contracts/ConfidentialETH.sol)) is an implementation of the FHERC20 standard that enables confidential token operations for any ERC20 token. It wraps existing ERC20 tokens to provide confidential balance and transfer functionality while maintaining compatibility with the original token's properties.

### Key Concepts

#### Token Wrapping

* ConfidentialERC20 wraps existing ERC20 tokens to provide confidential functionality
* The wrapped token maintains the same name (prefixed with "Confidential") and symbol (prefixed with "e")
* Decimals are preserved from the original token
* The original token can be deposited and withdrawn through the contract using `encrypt` and `decrypt`

#### Confidential Balances

* Inherits all FHE functionality from FHERC20
* Balances are stored as encrypted values (`euint128`)
* Uses **indicated** balances for UI/UX compatibility
* See FHERC20 Documentation for detailed balance mechanics: [fherc20.sol](https://docs.redact.money/architecture/fherc20.sol "mention")

#### Claim System

The contract implements a claim system through the `ConfidentialClaim.sol` contract that enables:

* Creation of confidential claims for token amounts
* Decryption of claimed amounts
* Tracking of user claims
* Batch claiming functionality

### External Functions

#### View Functions

**`underlying()`**

* Returns the address of the underlying ERC20 token
* Used to identify the original token being wrapped

**`isConfidentialERC20()`**

* Returns `true` to indicate this is a ConfidentialERC20 token

**`getClaim(uint256 ctHash)`**

* Returns claim information for a specific encrypted amount
* Includes requested amount, decrypted amount, and claim status
* Only accessible to authorized parties

**`getUserClaims(address user)`**

* Returns all claims associated with a user
* Includes both pending and claimed amounts
* Only accessible to authorized parties

#### State-Changing Functions

**`encrypt(address to, uint128 amount)`**

* Deposits the specified amount of the underlying token
* Mints an equivalent amount of confidential tokens
* Requires approval of the underlying ERC20

**`decrypt(address to, uint128 amount)`**

* Burns the specified amount of confidential tokens
* Transfers the equivalent amount of the underlying token
* Requires sufficient confidential token balance

### ConfidentialETH

ConfidentialETH is a special case of ConfidentialERC20 that wraps the native ETH token. ConfidentialETH allows encrypting of both ETH (using `encryptETH` ) and WETH (using `encryptWETH`). When decrypting ConfidentialETH, it is always decrypted as ETH, never WETH.&#x20;

**Symbol**

* ConfidentialETH symbol is `eETH`

#### Encrypt Process

* Uses `msg.value` instead of token approval when encrypting `ETH` into `eETH`
* No need for separate token approval

#### Withdrawal Process

* Unwraps WETH back to ETH
* Sends native ETH to the recipient
* Handles gas considerations for ETH transfers

### Claim System Details (ConfidentialClaim.sol)

#### Claim Structure

```solidity
struct Claim {
    uint256 ctHash;           // Hash of the encrypted amount
    uint128 requestedAmount;  // Original requested amount
    uint128 decryptedAmount;  // Decrypted amount after processing
    bool decrypted;           // Whether the amount has been decrypted
    address to;               // Claim recipient
    bool claimed;             // Whether the claim has been processed
}
```

#### Claim Process

1. **Creation**
   * A claim is created with an encrypted amount
   * The claim is associated with a specific recipient
   * The encrypted amount is stored with its hash
2. **Decryption**
   * The encrypted amount is decrypted using FHE
   * The decrypted amount is verified against the original request
   * The claim status is updated
3. **Processing**
   * The decrypted amount is transferred to the recipient
   * The claim is marked as claimed
   * The claim is removed from the user's active claims

#### Batch Operations

* Users can claim multiple amounts in a single transaction
* Gas efficient for processing multiple claims
* Maintains confidentiality of individual amounts

### Events

#### `Deposit(address indexed user, uint256 amount)`

* Emitted when underlying tokens are deposited
* Includes the deposit amount

#### `Withdraw(address indexed user, uint256 amount)`

* Emitted when confidential tokens are withdrawn
* Includes the withdrawal amount

#### `ClaimCreated(address indexed user, uint256 ctHash, uint256 amount)`

* Emitted when a new claim is created
* Includes the claim hash and amount

#### `ClaimProcessed(address indexed user, uint256 ctHash, uint256 amount)`

* Emitted when a claim is processed
* Includes the claim hash and decrypted amount
