Design effective event systems for smart contracts enabling efficient indexing, filtering, and off-chain tracking.
## ROLE
You are a smart contract developer focused on event-driven architectures and off-chain data indexing.
## CONTEXT
I need to design the event system for my smart contract.
## CONTRACT INFO
- Contract type: ${{TYPE}}
- Key state changes: ${{STATE_CHANGES}}
- Off-chain needs: ${{OFFCHAIN_NEEDS}}
- Indexing requirements: ${{INDEXING}}
## TASK
Design comprehensive event system:
**1. EVENT FUNDAMENTALS**
```solidity
// Basic event structure
event Transfer(
address indexed from, // Indexed = filterable
address indexed to, // Indexed = filterable
uint256 amount // Not indexed = cheaper
);
// Indexed vs non-indexed
// - Indexed: Can filter/search (3 max for non-anonymous)
// - Non-indexed: Cheaper, data only
```
**2. BEST PRACTICES**
```solidity
// DO: Clear, descriptive events
event TokensMinted(
address indexed recipient,
uint256 amount,
uint256 timestamp
);
// DON'T: Generic, unclear events
event Action(address user, uint256 value);
// DO: Include all relevant data
event Swap(
address indexed sender,
address indexed tokenIn,
address indexed tokenOut,
uint256 amountIn,
uint256 amountOut,
uint256 fee
);
// DON'T: Require additional calls to understand
event Swap(uint256 indexed id);
```
**3. STATE CHANGE EVENTS**
```solidity
contract StateEvents {
// Administrative changes
event OwnershipTransferred(
address indexed previousOwner,
address indexed newOwner
);
event Paused(address account);
event Unpaused(address account);
// Configuration changes
event FeeUpdated(uint256 oldFee, uint256 newFee);
event WhitelistUpdated(
address indexed account,
bool indexed status
);
}
```
**4. FINANCIAL EVENTS**
```solidity
contract DeFiEvents {
event Deposit(
address indexed user,
address indexed token,
uint256 amount,
uint256 shares
);
event Withdraw(
address indexed user,
address indexed token,
uint256 amount,
uint256 shares
);
event Swap(
address indexed user,
address indexed tokenIn,
address indexed tokenOut,
uint256 amountIn,
uint256 amountOut
);
event Liquidation(
address indexed liquidator,
address indexed borrower,
uint256 repaidAmount,
uint256 collateralSeized
);
}
```
**5. NFT EVENTS**
```solidity
contract NFTEvents {
// ERC721 standard
event Transfer(
address indexed from,
address indexed to,
uint256 indexed tokenId
);
// Custom events
event TokenMinted(
address indexed to,
uint256 indexed tokenId,
string metadataURI
);
event MetadataUpdated(
uint256 indexed tokenId,
string oldURI,
string newURI
);
}
```
**6. INDEXING CONSIDERATIONS**
```solidity
// For The Graph / Subgraphs
// Use indexed for fields you'll filter by
event Sale(
uint256 indexed tokenId, // Filter by token
address indexed seller, // Filter by seller
address indexed buyer, // Filter by buyer
uint256 price, // Data only
uint256 timestamp // Data only
);
// Max 3 indexed for non-anonymous events
// Choose wisely based on query patterns
```
**7. GAS OPTIMIZATION**
```solidity
// Events are relatively cheap
// ~375 gas base + 375 per indexed topic + 8 per byte
// Use bytes32 instead of string for fixed data
event ConfigUpdated(bytes32 indexed key, bytes32 value);
// Pack multiple booleans
event FlagsUpdated(uint256 flags);
```
**8. EVENT DOCUMENTATION**
```solidity
/// @notice Emitted when tokens are transferred
/// @param from The sender address (zero for mints)
/// @param to The recipient address (zero for burns)
/// @param amount The number of tokens transferred
event Transfer(
address indexed from,
address indexed to,
uint256 amount
);
```
**9. RECOMMENDED EVENTS BY CONTRACT TYPE**
| Contract Type | Essential Events |
|--------------|------------------|
| Token | Transfer, Approval |
| NFT | Transfer, Approval, MetadataUpdate |
| DeFi | Deposit, Withdraw, Swap, Liquidation |
| Governance | ProposalCreated, Voted, Executed |
| Staking | Staked, Withdrawn, RewardPaid |Or press ⌘C to copy
Replace these placeholders with your own content before using the prompt.
[{TYPE][{STATE_CHANGES][{OFFCHAIN_NEEDS][{INDEXING]{
// Administrative changes
event OwnershipTransferred(
address indexed previousOwner,
address indexed newOwner
);
event Paused(address account);
event Unpaused(address account);
// Configuration changes
event FeeUpdated(uint256 oldFee, uint256 newFee);
event WhitelistUpdated(
address indexed account,
bool indexed status
);
}{
event Deposit(
address indexed user,
address indexed token,
uint256 amount,
uint256 shares
);
event Withdraw(
address indexed user,
address indexed token,
uint256 amount,
uint256 shares
);
event Swap(
address indexed user,
address indexed tokenIn,
address indexed tokenOut,
uint256 amountIn,
uint256 amountOut
);
event Liquidation(
address indexed liquidator,
address indexed borrower,
uint256 repaidAmount,
uint256 collateralSeized
);
}{
// ERC721 standard
event Transfer(
address indexed from,
address indexed to,
uint256 indexed tokenId
);
// Custom events
event TokenMinted(
address indexed to,
uint256 indexed tokenId,
string metadataURI
);
event MetadataUpdated(
uint256 indexed tokenId,
string oldURI,
string newURI
);
}