Skip to content

Commit

Permalink
feat: DataParser.sol doc
Browse files Browse the repository at this point in the history
  • Loading branch information
JordyRo1 committed Nov 3, 2024
1 parent 1a57e5d commit 5a6a9b0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
32 changes: 29 additions & 3 deletions solidity/src/Pragma.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,27 @@ import "./libraries/DataParser.sol";
/// @notice The Pragma contract.
contract Pragma is Initializable, UUPSUpgradeable, OwnableUpgradeable, IPragma, PragmaDecoder {
/* STORAGE */

/// @notice The period, in seconds, for which data is considered valid (default 60s)
uint256 public validTimePeriodSeconds;

/// @notice The fee in wei required for a single update.
uint256 public singleUpdateFeeInWei;

/* CONSTRUCTOR */

constructor() {
_disableInitializers();
}

/* INITIALIZER */

/// @notice Initializes the contract with initial parameters.
/// @param _hyperlane Address of the Hyperlane contract deployed on the chain.
/// @param initial_owner Address of the owner of the contract.
/// @param _dataSourceEmitterChainIds Registry of valid chain IDs for data source emitters.
/// @param _dataSourceEmitterAddresses Registry of valid data source emitters addresses for their respective chains.
/// @param _validTimePeriodSeconds Maximum period in seconds a data feed is valid.
function initialize(
address _hyperlane,
address initial_owner,
Expand All @@ -46,6 +60,8 @@ contract Pragma is Initializable, UUPSUpgradeable, OwnableUpgradeable, IPragma,
singleUpdateFeeInWei = _singleUpdateFeeInWei;
}

/// @notice Authorizes the contract upgrade to a new implementation address.
/// @param newImplementation Address of the new implementation contract.
function _authorizeUpgrade(address newImplementation) internal override onlyOwner {}

/// @inheritdoc IPragma
Expand All @@ -59,20 +75,22 @@ contract Pragma is Initializable, UUPSUpgradeable, OwnableUpgradeable, IPragma,
}
}
uint256 requiredFee = getTotalFee(totalNumUpdates);
if (msg.value < requiredFee) {
revert ErrorsLib.InsufficientFee();
}
if (msg.value < requiredFee) revert ErrorsLib.InsufficientFee();
}

/// @inheritdoc IPragma
function getUpdateFee(bytes[] calldata updateData) external view returns (uint256 feeAmount) {
return 0;
}

/// @notice Calculates the total fee required for a specified number of updates.
/// @param totalNumUpdates Number of updates to process.
/// @return requiredFee The calculated fee.
function getTotalFee(uint256 totalNumUpdates) private view returns (uint256 requiredFee) {
return totalNumUpdates * singleUpdateFeeInWei;
}

/// @inheritdoc IPragma
function getSpotMedianNoOlderThan(bytes32 id, uint256 age) external view returns (SpotMedian memory data) {
data = spotMedianFeeds[id];
if (data.metadata.timestamp == 0) {
Expand All @@ -84,6 +102,7 @@ contract Pragma is Initializable, UUPSUpgradeable, OwnableUpgradeable, IPragma,
return data;
}

/// @inheritdoc IPragma
function getTwapNoOlderThan(bytes32 id, uint256 age) external view returns (TWAP memory data) {
data = twapFeeds[id];
if (data.metadata.timestamp == 0) {
Expand All @@ -94,6 +113,7 @@ contract Pragma is Initializable, UUPSUpgradeable, OwnableUpgradeable, IPragma,
}
}

/// @inheritdoc IPragma
function getRealizedVolatilityNoOlderThan(bytes32 id, uint256 age)
external
view
Expand All @@ -108,6 +128,7 @@ contract Pragma is Initializable, UUPSUpgradeable, OwnableUpgradeable, IPragma,
}
}

/// @inheritdoc IPragma
function getOptionsNoOlderThan(bytes32 id, uint256 age) external view returns (Options memory data) {
data = optionsFeeds[id];
if (data.metadata.timestamp == 0) {
Expand All @@ -118,6 +139,7 @@ contract Pragma is Initializable, UUPSUpgradeable, OwnableUpgradeable, IPragma,
}
}

/// @inheritdoc IPragma
function getPerpNoOlderThan(bytes32 id, uint256 age) external view returns (Perp memory data) {
data = perpFeeds[id];
if (data.metadata.timestamp == 0) {
Expand Down Expand Up @@ -146,10 +168,14 @@ contract Pragma is Initializable, UUPSUpgradeable, OwnableUpgradeable, IPragma,
}
}

/// @notice Retrieves the valid time period for data in seconds.
/// @return validTimePeriodSeconds The configured time period.
function getValidTimePeriod() public view returns (uint256) {
return validTimePeriodSeconds;
}

/// @notice Allows the owner to withdraw the specified amount from the contract.
/// @param amount The amount in wei to withdraw.
function withdrawFunds(uint256 amount) external onlyOwner {
require(amount <= address(this).balance, "Insufficient balance");
(bool success,) = owner().call{value: amount}("");
Expand Down
7 changes: 2 additions & 5 deletions solidity/src/libraries/DataParser.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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.
Expand All @@ -14,17 +13,15 @@ import "./ErrorsLib.sol";
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) {

// 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.
// 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
Expand Down

0 comments on commit 5a6a9b0

Please sign in to comment.