Files
explorer-monorepo/docs/WETH9_RATIO_ISSUE_REVIEW.md

246 lines
6.5 KiB
Markdown

# WETH9 1:1 Ratio Issue Review
## Problem Statement
**Issue**: When converting ETH to WETH9, the ratio of 1:1 is not being used/maintained.
## Critical Analysis
### Expected Behavior
WETH9 (Wrapped Ether) **MUST** maintain a strict 1:1 ratio:
- **1 ETH deposited = 1 WETH9 minted** (exactly)
- No fees should be deducted
- No slippage should occur
- Contract ETH balance = Total WETH9 supply
### Standard WETH9 Implementation
The standard `deposit()` function should be:
```solidity
function deposit() public payable {
balanceOf[msg.sender] += msg.value; // Direct 1:1 mapping
totalSupply += msg.value; // Direct 1:1 mapping
Deposit(msg.sender, msg.value);
}
```
**Key Points:**
- `msg.value` is added directly (no calculations)
- No fees are deducted
- No conversions are performed
- The ratio is inherently 1:1 by design
## Potential Issues
### 1. Contract Implementation Problem
**If the contract has been modified:**
- Fees may have been added
- Calculations may have been introduced
- The ratio may have been intentionally changed
**Verification:**
```bash
# Check contract bytecode
cast code "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" \
--rpc-url "http://192.168.11.250:8545" > weth9_bytecode.bin
# Compare with standard WETH9 bytecode
# Or decompile and inspect deposit() function
```
### 2. Script Calculation Error
**If scripts are calculating incorrectly:**
- Amount conversion errors
- Fee deductions in scripts
- Rounding errors
**Verification:**
```bash
# Test with verification script
./scripts/verify-weth9-ratio.sh [private_key] 0.001
# This will show exact amounts and ratio
```
### 3. Display/Precision Issues
**If it's a display problem:**
- UI rounding errors
- Decimal precision issues
- MetaMask display bug (known issue with decimals=0)
**Verification:**
- Always check raw wei values, not displayed amounts
- Use `cast` commands to get exact values
### 4. Gas Fee Confusion
**If users confuse gas with wrap amount:**
- Gas fees are separate from wrap amount
- Wrapping 1 ETH costs 1 ETH + gas fees
- But you receive exactly 1 WETH9
**Example:**
```
Wrap 1 ETH:
- ETH spent: 1 ETH + 0.001 ETH (gas) = 1.001 ETH total
- WETH9 received: 1 WETH9 ✅
- Ratio: 1:1 ✅ (gas is separate)
```
## Investigation Steps
### Step 1: Run Verification Script
```bash
./scripts/verify-weth9-ratio.sh [private_key] 0.001
```
This will:
- Wrap a test amount (0.001 ETH)
- Measure exact WETH9 received
- Calculate the ratio
- Report if ratio is exactly 1:1
### Step 2: Manual Verification
```bash
# 1. Get initial state
DEPLOYER=$(cast wallet address --private-key "0x...")
INITIAL_ETH=$(cast balance "$DEPLOYER" --rpc-url "http://192.168.11.250:8545")
INITIAL_WETH9=$(cast call "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" \
"balanceOf(address)" "$DEPLOYER" --rpc-url "http://192.168.11.250:8545")
# 2. Wrap exact amount
TEST_AMOUNT_WEI=$(cast --to-wei 0.001 ether)
cast send "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" "deposit()" \
--value "$TEST_AMOUNT_WEI" \
--rpc-url "http://192.168.11.250:8545" \
--private-key "0x..." \
--gas-price 5000000000
# 3. Wait and check
sleep 15
FINAL_WETH9=$(cast call "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" \
"balanceOf(address)" "$DEPLOYER" --rpc-url "http://192.168.11.250:8545")
# 4. Calculate ratio
WETH9_RECEIVED=$(echo "$FINAL_WETH9 - $INITIAL_WETH9" | bc)
echo "Expected: $TEST_AMOUNT_WEI wei"
echo "Received: $WETH9_RECEIVED wei"
# Should be equal!
```
### Step 3: Check Contract State
```bash
# Contract should hold exactly the total supply in ETH
CONTRACT_ETH=$(cast balance "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" \
--rpc-url "http://192.168.11.250:8545")
TOTAL_SUPPLY=$(cast call "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" \
"totalSupply()" --rpc-url "http://192.168.11.250:8545")
echo "Contract ETH: $CONTRACT_ETH wei"
echo "Total Supply: $TOTAL_SUPPLY wei"
# Should be equal!
```
## Script Review
### Current Implementation
The `wrap-and-bridge-to-ethereum.sh` script:
1. **Uses exact conversion:**
```bash
AMOUNT_WEI=$(cast --to-wei "$AMOUNT" ether)
```
✅ Correct - uses `cast --to-wei` for exact conversion
2. **Sends exact amount:**
```bash
cast send "$WETH9_ADDRESS" "deposit()" --value "$WRAP_AMOUNT_WEI"
```
✅ Correct - sends exact wei amount
3. **No fee deductions:**
✅ Correct - no fees are deducted from wrap amount
4. **Verifies ratio:**
```bash
# Now includes ratio verification
if [ "$WETH9_RECEIVED" = "$WRAP_AMOUNT_WEI" ]; then
log_success "✓ 1:1 ratio verified"
fi
```
✅ Updated - now verifies 1:1 ratio
## What to Do If Ratio is NOT 1:1
### If Verification Fails
1. **Document the Issue**
- Record exact amounts (in wei)
- Note transaction hash
- Capture calculated ratio
2. **Check Contract Implementation**
- Inspect contract bytecode
- Compare with standard WETH9
- Look for modifications
3. **Review Transaction**
```bash
cast tx [transaction_hash] --rpc-url "http://192.168.11.250:8545"
```
- Verify value sent
- Check events emitted
- Confirm transaction succeeded
4. **Report Critical Bug**
- This is a critical issue if ratio is not 1:1
- WETH9 must maintain 1:1 parity
- May require contract fix or replacement
## Tools Created
1. **Verification Script**: `scripts/verify-weth9-ratio.sh`
- Tests actual 1:1 ratio
- Reports exact amounts and ratio
- Validates contract backing
2. **Updated Wrap Script**: `scripts/wrap-and-bridge-to-ethereum.sh`
- Now includes ratio verification
- Warns if ratio is not 1:1
- Provides detailed logging
3. **Documentation**:
- `docs/WETH9_1_TO_1_RATIO_VERIFICATION.md` - Detailed verification guide
- `docs/WETH9_RATIO_ISSUE_REVIEW.md` - This document
## Next Steps
1. **Run Verification**: Test the actual ratio using the verification script
2. **Review Results**: If ratio is not 1:1, investigate contract implementation
3. **Fix if Needed**: If contract is modified, may need to deploy standard WETH9
4. **Update Documentation**: Document findings and solutions
## Summary
- **WETH9 MUST maintain 1:1 ratio** - this is fundamental
- **Scripts are correct** - they use exact amounts and no fees
- **Verification tools created** - use them to test actual ratio
- **If ratio fails** - this is a critical bug requiring investigation
The issue may be:
- Contract implementation (most likely if ratio is wrong)
- Display/calculation error (less likely)
- Gas fee confusion (user misunderstanding)
Use the verification script to determine the actual cause.