Error Codes
# SwapCore Error Code Reference
This document provides a comprehensive mapping of all error codes used in the SwapCore contract.
## Error Code System
Error codes are organized by category for easier maintenance and debugging. Each category is assigned a range of 100 numbers.
---
## Configuration Errors (100-199)
| Error Code | Description | Original Message |
|------------|-------------|------------------|
| **E100** | Invalid whitelist address | "Invalid whitelist address" |
| **E101** | Whitelist address not set | "Whitelist address not set" |
| **E102** | Invalid admin address | "Invalid admin address" |
| **E103** | Invalid WETH address | "Invalid WETH address" |
**When these occur:** During contract deployment or configuration setup.
---
## Authorization Errors (200-299)
| Error Code | Description | Original Message |
|------------|-------------|------------------|
| **E200** | Not authorized | "Not authorized" |
| **E201** | User not whitelisted | "User not whitelisted" / "Not whitelisted" |
| **E202** | Not market owner | "Not market owner" / "Not the owner of the market" |
| **E203** | Not swap owner | "Not swap owner" |
| **E204** | Invalid role | "Invalid role" |
**When these occur:** When a user attempts to perform an action they don't have permission for.
---
## Market Errors (300-399)
| Error Code | Description | Original Message |
|------------|-------------|------------------|
| **E300** | Market does not exist | "Market does not exist" |
| **E301** | Market already exists | "Market already exists" |
| **E302** | Market is paused | "Market is paused" |
| **E303** | All markets are paused | "All markets are paused" / "Global is paused" |
| **E304** | Market buySwaps paused | "Market buySwaps paused" |
| **E305** | All buySwaps paused | "All buySwaps paused" |
**When these occur:** When interacting with markets that are in an invalid state or don't exist.
---
## Collateral Errors (400-499)
| Error Code | Description | Original Message | Additional Info |
|------------|-------------|------------------|-----------------|
| **E400** | Invalid amount | "Invalid amount" | Amount is 0 or negative |
| **E401** | Insufficient collateral | "Insufficient collateral" | General collateral insufficiency |
| **E402** | Insufficient buyer collateral | "Insufficient buyer collateral" | Legacy error (replaced by E403) |
| **E403** | Insufficient buyer collateral (with values) | Custom error with balance and required | Returns actual balance and required amount |
| **E404** | Insufficient pool collateral | "Insufficient pool collateral" | Pool doesn't have enough to back swap |
| **E405** | Insufficient available collateral | "Insufficient available collateral" | Not enough unlocked collateral |
| **E406** | Insufficient shares | "Insufficient shares" | LP doesn't have enough shares |
| **E407** | No pool shares exist | "No pool shares exist" | Pool is empty |
| **E408** | Withdrawal too large | "Withdrawal too large" | Would make totalCollateral < lockedCollateral |
| **E409** | Cap reached | "Cap reached" | Max collateral balance exceeded |
| **E410** | Creator cant supply | "Creator cant supply" | Creator role cannot supply collateral |
| **E411** | Creator cant withdrawal | "Creator cant withdrawal" | Creator role cannot withdraw |
**When these occur:** During collateral operations (supply, withdraw, swap creation).
### Deposit/Withdrawal Pause Errors (450-469)
| Error Code | Description | Original Message |
|------------|-------------|------------------|
| **E450** | All LP deposits are paused | "All LP deposits are paused" |
| **E451** | Market LP deposits paused | "Market LP deposits paused" |
| **E452** | All LP withdrawals are paused | "All LP withdrawals are paused" |
| **E453** | Market LP withdrawals paused | "Market LP withdrawals paused" |
| **E454** | Buyer withdrawals paused | "Buyer withdrawals paused" |
**When these occur:** When deposit/withdrawal operations are temporarily disabled.
---
## Swap Errors (500-599)
| Error Code | Description | Original Message |
|------------|-------------|------------------|
| **E500** | Swap does not exist | "Swap does not exist" |
| **E501** | Swap already settled | "Swap already settled" |
| **E502** | Swap is expired | "Swap is expired" |
| **E503** | Trade too small | "Trade too small" |
| **E504** | Trade too large | "Trade too large" |
| **E505** | Wrong swap market | "Wrong swap market" |
| **E506** | Exit too early | "Exit too early" |
**When these occur:** During swap operations, settlements, and early exits.
### Early Exit Errors (550-559)
| Error Code | Description | Original Message |
|------------|-------------|------------------|
| **E550** | Early exit not allowed | "Early exit not allowed" |
| **E551** | Already disabled | "Already disabled" |
**When these occur:** When attempting early exit operations that are not permitted.
---
## Fee Errors (600-699)
| Error Code | Description | Original Message |
|------------|-------------|------------------|
| **E600** | Not adjustable | "Not adjustable" |
| **E601** | Incorrect fee range | "Incorrect fee range" |
| **E602** | Fee too small | "Fee too small" |
| **E603** | Fee cannot exceed 20% | "Fee cannot exceed 20%" / "Cannot exceed 20%" |
| **E604** | Cannot exceed 10% | "Cannot exceed 10%" |
| **E605** | Fees not adjustable | "Fees not adjustable" |
**When these occur:** During admin operations to adjust market fees.
---
## Validation Errors (700-799)
| Error Code | Description | Original Message |
|------------|-------------|------------------|
| **E700** | Invalid parameters | "Invalid parameters" |
| **E701** | Rounding error too large - use fullAmount | "Rounding error too large - use fullAmount" |
| **E702** | Trade size exceeds available liquidity | "Trade size exceeds available liquidity" |
**When these occur:** During parameter validation for various operations and liquidity checks.
---
## Usage Examples
### Reading Error Codes in Frontend
```javascript
try {
await swapCore.buySwap(marketId, notionalAmount);
} catch (error) {
if (error.message.includes('E403')) {
// Parse the error to get balance and required values
console.log('Insufficient buyer collateral');
} else if (error.message.includes('E300')) {
console.log('Market does not exist');
}
}
```
### Testing for Specific Errors
```javascript
// In your tests
await expect(
swapCore.buySwap(invalidMarketId, notionalAmount)
).to.be.revertedWithCustomError(Errors, 'E300');
```
---
## Migration Guide
### Before (String Messages)
```solidity
require(markets[marketId].exists, "Market does not exist");
require(amount > 0, "Invalid amount");
```
### After (Error Codes)
```solidity
if (!markets[marketId].exists) revert Errors.E300();
if (amount == 0) revert Errors.E400();
```
---
## Gas Savings
Using error codes instead of string messages provides significant gas savings:
- **String message**: ~50-100 gas per character
- **Error code**: Fixed ~21 gas for revert operation
- **Typical savings**: 1,000-2,000 gas per failed transaction
For a contract with 54 require statements like SwapCore, this can save substantial gas across all operations.
---
## Error Code Location Map
| Function | Error Codes Used |
|----------|------------------|
| `constructor` | E100, E102, E103 |
| `_isUserWhitelisted` | E101 |
| `createMarket` | E200 |
| `_initializeMarket` | E301 |
| `getPosition` | E204, E300 |
| `withdrawCollateral` | E201, E204, E300, E400, E401, E405, E406, E407, E408 |
| `updateMarketRateIndex` | E300 |
| `isUserLpInMarket` | E300 |
| `supplyCollateral` | E201, E300 |
| `buySwap` | E201, E300, E302, E403, E404, E503, E504 |
| `getSwapNetAmount` | E300, E500 |
| `makePayment` | E300 |
| `exitSwapEarly` | E203, E300, E302, E500, E501, E502, E505, E506, E550 |
| `settleAllMarketsLP` | E303 |
| `settleAllSwapsBuyer` | E303 |
| `setMarketUtilFeeWad` | E300, E600, E601, E602, E603 |
| `setMarketMaxAdjustmentImpliedBaseRate` | E300, E604, E605 |
| `setEarlyExitFee` | E300, E603, E605 |
| `disableMarketFeeAdjustments` | E300, E605 |
| `disableMarketEarlyExit` | E300, E551 |
| `setMarketOwner` | E202, E300 |
| `pauseMarket` | E300 |
| **Modifiers** | |
| `marketOwner` | E202 |
| `notPaused` | E201, E302, E303 |
| `canSupplyCollateral` | E201, E409, E410, E451 |
| `canWithdrawCollateral` | E201, E411, E453, E454 |
| `canBuySwaps` | E201, E304, E305 |
| **SwapFormulas Library** | |
| `calculateUtilizationFee` | E702 |
| `calculateImbalanceAdjustedRate` | E702 |
---
## Version History
- **v1.0.0** - Initial error code system implementation
- Replaced 54 string-based require statements
- Organized into 7 major categories
- Added special error E403 with parameters for better debugging
---
## Contributing
When adding new error codes:
1. Choose the appropriate category (100s, 200s, etc.)
2. Add the error to `Errors.sol`
3. Update this documentation
4. Add the error to the location map
5. Include usage examples if the error is complex
---
## Questions?
For questions about specific error codes or to report issues, please contact the development teamLast updated