Skip to main content

Try it Live

Run EventLog examples in the interactive playground

    Indexed Parameter Limits

    Solidity events can have maximum 3 indexed parameters (topic1-topic3):
    // Maximum indexed parameters
    event MaxIndexed(
      address indexed param1,
      address indexed param2,
      uint256 indexed param3,
      uint256 param4  // Not indexed (in data)
    ) /* anonymous */;
    
    Anonymous events can have 4 indexed parameters (topic0-topic3):
    event AnonymousEvent(
      address indexed param1,
      address indexed param2,
      uint256 indexed param3,
      uint256 indexed param4
    ) anonymous;
    

    Usage Patterns

    Extracting Transfer Parameters

    import { EventLog, Address, Hash } from 'tevm';
    
    // Transfer(address indexed from, address indexed to, uint256 value)
    const TRANSFER_SIG = Hash('0xddf252ad...');
    
    function decodeTransfer(log: EventLog) {
      const sig = log.getTopic0();
      if (!sig || !Hash.equals(sig, TRANSFER_SIG)) {
        throw new Error('Not a Transfer event');
      }
    
      const [fromHash, toHash] = log.getIndexedTopics();
    
      // Extract addresses from topic hashes (last 20 bytes)
      const from = Address(fromHash.slice(12));
      const to = Address(toHash.slice(12));
    
      // Decode value from data
      const value = new DataView(log.data.buffer).getBigUint64(24, false);
    
      return { from, to, value };
    }
    
    const transfer = decodeTransfer(transferLog);
    console.log('From:', Address.toHex(transfer.from));
    console.log('To:', Address.toHex(transfer.to));
    console.log('Value:', transfer.value);
    

    Validating Parameter Count

    import { EventLog } from 'tevm';
    
    function validateEventStructure(log: EventLog, expectedCount: number): boolean {
      const indexed = log.getIndexedTopics();
    
      if (indexed.length !== expectedCount) {
        console.error(
          `Expected ${expectedCount} indexed params, got ${indexed.length}`
        );
        return false;
      }
    
      return true;
    }
    
    // Transfer has 2 indexed parameters (from, to)
    validateEventStructure(transferLog, 2); // true
    
    // Approval has 2 indexed parameters (owner, spender)
    validateEventStructure(approvalLog, 2); // true
    

    Destructuring Parameters

    import { EventLog } from 'tevm';
    
    // Approval(address indexed owner, address indexed spender, uint256 value)
    const log = EventLog({ /* ... */ });
    
    const [ownerHash, spenderHash] = log.getIndexedTopics();
    
    console.log('Owner:', Hash.toHex(ownerHash));
    console.log('Spender:', Hash.toHex(spenderHash));
    

    Empty Indexed Topics

    import { EventLog, Address, Hash } from 'tevm';
    
    // Event with no indexed parameters
    const log = EventLog({
      address: contractAddress,
      topics: [Hash('0xeventSig...')], // Only signature
      data: eventData,
    });
    
    const indexed = log.getIndexedTopics();
    console.log(indexed.length); // 0 (empty array)
    

    Anonymous Event Handling

    import { EventLog } from 'tevm';
    
    // Anonymous events: topic0 is first indexed param, NOT signature
    const anonymousLog = EventLog({
      address: contractAddress,
      topics: [param1Hash, param2Hash, param3Hash], // All indexed params
      data: Bytes(),
    });
    
    // getIndexedTopics returns topic1-topic3 (NOT all params)
    const indexed = anonymousLog.getIndexedTopics();
    console.log(indexed.length); // 2 (param2 and param3)
    
    // To get ALL indexed params from anonymous event:
    const allParams = [anonymousLog.getTopic0(), ...indexed].filter(Boolean);
    console.log(allParams.length); // 3
    

    Extracting Addresses from Topics

    Addresses in topics are left-padded to 32 bytes. Extract the address by taking the last 20 bytes:
    import { Address, Hash } from 'tevm';
    
    const topicHash = Hash('0x000000000000000000000000a1b2c3d4e5f67890abcdef1234567890abcdef12');
    
    // Extract address (last 20 bytes)
    const address = Address(topicHash.slice(12));
    
    console.log(Address.toHex(address));
    // '0xa1b2c3d4e5f67890abcdef1234567890abcdef12'
    

    See Also