Skip to content

instruments

sequenceDiagram
    participant Client
    participant Express as Express App
    participant Handler as generateInstrumentsHandler
    participant Generator as generateInstrumentNames
    participant Processor as processAndUpdateInstruments
    participant Redis
    participant Strike as generateStrikes

    Client->>Express: POST /instruments
    Express->>Handler: Pass Redis client + route request

    alt Request body is not empty
        Handler->>Client: Return 400 Error (Bad Request)
    else Request body is empty
        Note over Handler: Loop through all markets (BTC, ETH, ARB)

        loop For each market in SupportedBaseTokens
            Handler->>Generator: Call generateInstrumentNames(market, redis)

            Generator->>Redis: Get index price (index_price:market)
            Redis-->>Generator: Return index price data

            Generator->>Generator: Get next maturities

            par For each maturity (Promise.all)
                Generator->>Generator: Calculate TTM for maturity
                Generator->>Redis: Get SVI params for maturity (svi:market:maturity)
                Redis-->>Generator: Return SVI parameters
                Generator->>Generator: Calculate ATM IV using getIVFromSVI
                Generator->>Strike: generateStrikes(market, indexPrice, atmVol, ttm)
                Strike-->>Generator: Return array of strikes
                Generator->>Generator: flatMap strikes to call/put instruments
            end

            Generator->>Generator: Flatten results from all maturities
            Generator-->>Handler: Return instrumentNames array

            Handler->>Processor: processAndUpdateInstruments(redis, market, newInstrumentNames)

            Processor->>Redis: Get existing instruments (smembers instruments:market)
            Redis-->>Processor: Return existing instruments

            Processor->>Processor: Filter non-expired instruments
            Processor->>Processor: Identify expired instruments to remove

            Processor->>Redis: Start transaction (multi)

            opt If expired instruments exist
                Processor->>Redis: Remove expired instruments (srem)
            end

            opt If new instruments exist
                Processor->>Redis: Add new instruments in chunks (sadd)
            end

            Processor->>Redis: Update timestamp (set instruments:market:updated_at)
            Processor->>Redis: Execute transaction (exec)

            Processor->>Redis: Get final count (scard instruments:market)
            Redis-->>Processor: Return final count

            Processor->>Processor: Log results
            Processor-->>Handler: Return void

            alt Market processing succeeds
                Handler->>Handler: Add to successfulMarkets list
            else Market processing fails
                Handler->>Handler: Add to failedMarkets list and log error
            end
        end

        Handler->>Handler: Log summary of successful/failed markets
        Handler->>Client: Return 200 Success (empty response)
    end

    alt Error in overall processing
        Handler->>Client: Return 500 Error (Server Error)
    end