Skip to content

Commit

Permalink
feat: DataParser.sol documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
JordyRo1 committed Nov 2, 2024
1 parent 3415dd8 commit 5c060d6
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion solidity/src/libraries/DataParser.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,35 @@ import "./BytesLib.sol";
import "../interfaces/PragmaStructs.sol";
import "./ErrorsLib.sol";


/// @title DataParser Library
/// @notice A library for parsing various data feed types into structured data.
/// @dev This library uses `BytesLib` for handling byte data, and assumes that data is encoded in a specific format.
/// The calldata format can be found on https://docs.pragmaoracle.com/
/// @custom:security-contact security@pragma.build
library DataParser {
using BytesLib for bytes;


/// @notice Parses the raw byte data and returns a structured `ParsedData` type.
/// @dev Determines the feed type based on the data and parses accordingly.
/// Reverts if the data feed type is invalid.
/// @param data The raw byte-encoded data to parse.
/// @return ParsedData struct containing the parsed data.
function parse(bytes memory data) internal pure returns (ParsedData memory) {
uint8 offset = 2; // type of feed after the asset class

// Each update data has a feedId as first 31-bytes (and stored as 32-bytes). The two first bytes are allocated to the asset class.
// The two next bytes are respectively for the feed type and the feed type variant. Finally the remaining 27 bytes are allocated to the
// pair id. In order to retrieve the feed type, we need to skip the first 2 bytes.
uint8 offset = 2; // skips the asset class.

// Extract the feed type
uint8 rawDataType = data.toUint8(offset);
FeedType dataType = safeCastToFeedType(rawDataType);
ParsedData memory parsedData = StructsInitializers.initializeParsedData();
parsedData.dataType = dataType;

// Fill the storage based on the feed type
if (dataType == FeedType.SpotMedian) {
parsedData.spot = parseSpotData(data);
} else if (dataType == FeedType.Twap) {
Expand All @@ -32,6 +51,10 @@ library DataParser {
return parsedData;
}

/// @notice Safely casts a raw data type value to `FeedType`.
/// @dev Reverts if the raw data type exceeds the maximum value of `FeedType`.
/// @param rawDataType The raw data type byte.
/// @return FeedType The casted `FeedType`.
function safeCastToFeedType(uint8 rawDataType) internal pure returns (FeedType) {
if (rawDataType <= uint8(type(FeedType).max)) {
return FeedType(rawDataType);
Expand All @@ -40,6 +63,11 @@ library DataParser {
}
}

/// @notice Parses metadata information from the byte data starting at a given index.
/// @param data The raw byte-encoded data.
/// @param startIndex The starting index for metadata parsing.
/// @return metadata The parsed `Metadata` structure.
/// @return index The updated index after parsing metadata.
function parseMetadata(bytes memory data, uint256 startIndex) internal pure returns (Metadata memory, uint256) {
Metadata memory metadata = StructsInitializers.initializeMetadata();
uint256 index = startIndex;
Expand All @@ -59,6 +87,9 @@ library DataParser {
return (metadata, index);
}

/// @notice Parses SpotMedian data from the byte data.
/// @param data The raw byte-encoded data.
/// @return SpotMedian A `SpotMedian` struct with parsed data.
function parseSpotData(bytes memory data) internal pure returns (SpotMedian memory) {
SpotMedian memory entry = StructsInitializers.initializeSpotMedian();
uint256 index = 0;
Expand All @@ -74,6 +105,9 @@ library DataParser {
return entry;
}

/// @notice Parses TWAP (Time-Weighted Average Price) data from the byte data.
/// @param data The raw byte-encoded data.
/// @return TWAP A `TWAP` struct with parsed data.
function parseTWAPData(bytes memory data) internal pure returns (TWAP memory) {
TWAP memory entry = StructsInitializers.initializeTwap();
uint256 index = 0;
Expand All @@ -100,6 +134,9 @@ library DataParser {
return entry;
}

/// @notice Parses realized volatility data from the byte data.
/// @param data The raw byte-encoded data.
/// @return RealizedVolatility A `RealizedVolatility` struct with parsed data.
function parseRealizedVolatilityData(bytes memory data) internal pure returns (RealizedVolatility memory) {
RealizedVolatility memory entry = StructsInitializers.initializeRV();
uint256 index = 0;
Expand Down Expand Up @@ -129,6 +166,9 @@ library DataParser {
return entry;
}

/// @notice Parses options data from the byte data.
/// @param data The raw byte-encoded data.
/// @return Options An `Options` struct with parsed data.
function parseOptionsData(bytes memory data) internal pure returns (Options memory) {
Options memory entry = StructsInitializers.initializeOptions();
uint256 index = 0;
Expand Down Expand Up @@ -170,6 +210,9 @@ library DataParser {
return entry;
}

/// @notice Parses perpetuals data from the byte data.
/// @param data The raw byte-encoded data.
/// @return Perp A `Perp` struct with parsed data.
function parsePerpData(bytes memory data) internal pure returns (Perp memory) {
Perp memory entry = StructsInitializers.initializePerpetuals();
uint256 index = 0;
Expand Down

0 comments on commit 5c060d6

Please sign in to comment.