Skip to content

[FEATURE]: Add Indexed Parameters to Events for Efficient Off-Chain Querying #30

@rohans02

Description

@rohans02

Feature and its Use Cases

Problem

Critical event parameters are not indexed, making off-chain filtering inefficient for frontends, indexers, and analytics tools.

Current (inefficient):

// Factory.sol
event TNTCreated(address indexed owner, address tntAddress);  // ❌ tntAddress not indexed

// TNT.sol
event TokenIssued(address indexed issuer, address indexed user, uint256 tokenId);  // ❌ tokenId not indexed
event TokenRevoked(address indexed revoker, uint256 tokenId);  // ❌ tokenId not indexed

Without indexing, clients must fetch ALL events and filter client-side:

// ❌ Inefficient: Download all events, filter locally
const all = await factory.queryFilter(factory.filters.TNTCreated());
const filtered = all.filter(e => e.args.tntAddress === myAddress);

Solution

Add indexed to 3 parameters across 2 files:

// Factory.sol (Line ~15)
event TNTCreated(address indexed owner, address indexed tntAddress);  // ✅

// TNT.sol (Lines ~38-39)
event TokenIssued(address indexed issuer, address indexed user, uint256 indexed tokenId);  // ✅
event TokenRevoked(address indexed revoker, uint256 indexed tokenId);  // ✅

After (efficient):

// ✅ Filter at RPC/node level
const filtered = await factory.queryFilter(
  factory.filters.TNTCreated(null, specificTNTAddress)
);

Benefits

  1. Efficient Filtering - Query specific TNT addresses/token IDs at node level
  2. Improved Indexing - The Graph and similar tools can create DB indexes
  3. Reduced RPC Load - No need to download all events for client-side filtering
  4. Better Analytics - Track specific contracts/tokens without full scans

Gas Cost: +375 gas per indexed parameter (~1,125 gas total increase per TNT creation)

Testing

Create test/EventIndexing.test.js:

  1. Verify TNTCreated has 3 topics (signature + 2 indexed params)
  2. Verify TokenIssued has 4 topics (signature + 3 indexed params)
  3. Verify TokenRevoked has 3 topics (signature + 2 indexed params)
  4. Test filtering by indexed parameters
  5. Measure gas cost increase

Technical Details

Files to modify:

  • contracts/src/Factory.sol - TNTCreated event
  • contracts/src/TNT.sol - TokenIssued, TokenRevoked events

Additional Context

No response

Code of Conduct

  • I have joined the Discord server and will post updates there
  • I have searched existing issues to avoid duplicates

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions