Velvet-Capital / Velvet-v4

V4 (thena+venus) on top of v3
Other
0 stars 0 forks source link

Replaced mapping with bitmap #47

Closed Havoc19 closed 2 weeks ago

Havoc19 commented 2 weeks ago

Bitmap Token Tracking System

Overview

A bitmap can represent up to 256 tokens using a single uint256 variable, where each bit represents one token:

How It Works

Bit Positioning

Each token's position is determined by the least significant byte (LSB) of its address:

uint256 bitPos = uint256(uint160(token)) & 0xFF;

This maps each token address to a position between 0-255.

Token Operations

Adding Tokens

To mark a token as present, set its corresponding bit to 1:

tokenBitmap |= 1 << bitPos;

Removing Tokens

To remove a token, clear its bit to 0:

tokenBitmap &= ~(1 << bitPos);

Checking Token Presence

To verify if a token exists in the bitmap:

if (tokenBitmap & (1 << bitPos) != 0) {
    // Token is present
}

Implementation Details

verifyNewTokenList

This method validates new tokens being added:

  1. Creates a bitmap for tracking new tokens
  2. For each token in newTokens:
    • Verifies non-zero balance
    • Marks token in bitmap
  3. Validates each token in _ensoBuyTokens exists in bitmap

updateTokens

This method uses the bitmap for:

  1. Creating initial token balance bitmap
  2. Removing new tokens from bitmap
  3. Checking remaining token balances against dust limits

Technical Notes