Skip to content

Commit

Permalink
fix(slither): slither corrections 2
Browse files Browse the repository at this point in the history
  • Loading branch information
JordyRo1 committed Sep 23, 2024
1 parent 0b103c4 commit 238a9b4
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 27 deletions.
14 changes: 7 additions & 7 deletions solidity/src/Pragma.sol
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,17 @@ contract Pragma is IPragma, PragmaDecoder {

/// @inheritdoc IPragma
function dataFeedExists(bytes32 id) external view returns (bool) {
FeedType feedType = DataParser.safeCastToFeedType(uint8(id[0]));
if (feedType == FeedType.SpotMedian) {
return(spotMedianFeeds[id].metadata.timestamp !=0);
FeedType feedType = DataParser.safeCastToFeedType(uint8(id[0]));
if (feedType == FeedType.SpotMedian) {
return (spotMedianFeeds[id].metadata.timestamp != 0);
} else if (feedType == FeedType.Twap) {
return(twapFeeds[id].metadata.timestamp !=0);
return (twapFeeds[id].metadata.timestamp != 0);
} else if (feedType == FeedType.RealizedVolatility) {
return(rvFeeds[id].metadata.timestamp !=0);
return (rvFeeds[id].metadata.timestamp != 0);
} else if (feedType == FeedType.Options) {
return(optionsFeeds[id].metadata.timestamp !=0);
return (optionsFeeds[id].metadata.timestamp != 0);
} else if (feedType == FeedType.Perpetuals) {
return(perpFeeds[id].metadata.timestamp !=0);
return (perpFeeds[id].metadata.timestamp != 0);
} else {
revert ErrorsLib.InvalidDataFeedType();
}
Expand Down
2 changes: 1 addition & 1 deletion solidity/src/PragmaDecoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ contract PragmaDecoder {
virtual
returns (bool valid, uint256 endOffset)
{
return MerkleTree.isProofValid(encodedProof, offset, root, leafData);
(valid, endOffset) = MerkleTree.isProofValid(encodedProof, offset, root, leafData);
}

function extractDataInfoFromUpdate(bytes calldata encoded, uint256 offset, bytes32 checkpointRoot)
Expand Down
84 changes: 82 additions & 2 deletions solidity/src/interfaces/PragmaStructs.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ struct RealizedVolatility {
uint256 timePeriod;
uint256 startPrice;
uint256 endPrice;
uint256 high_price;
uint256 low_price;
uint256 highPrice;
uint256 lowPrice;
uint256 numberOfDataPoints;
}

Expand Down Expand Up @@ -109,3 +109,83 @@ enum FeedType {
Options,
Perpetuals
}

library StructsInitializers {
function initializeParsedData() public pure returns (ParsedData memory) {
ParsedData memory parsed = ParsedData({
dataType: FeedType.SpotMedian,
spot: initializeSpotMedian(),
twap: initializeTwap(),
rv: initializeRV(),
options: initializeOptions(),
perp: initializePerpetuals()
});

return parsed;
}

function initializeMetadata() public pure returns (Metadata memory) {
Metadata memory metadata = Metadata({feedId: 0, timestamp: 0, numberOfSources: 0, decimals: 0});
return metadata;
}

function initializeSpotMedian() public pure returns (SpotMedian memory) {
Metadata memory metadata = initializeMetadata();
SpotMedian memory spotMedian = SpotMedian({metadata: metadata, price: 0, volume: 0});
return spotMedian;
}

function initializeTwap() public pure returns (TWAP memory) {
Metadata memory metadata = initializeMetadata();
TWAP memory twap = TWAP({
metadata: metadata,
twapPrice: 0,
timePeriod: 0,
startPrice: 0,
endPrice: 0,
totalVolume: 0,
numberOfDataPoints: 0
});
return twap;
}

function initializeRV() public pure returns (RealizedVolatility memory) {
Metadata memory metadata = initializeMetadata();
RealizedVolatility memory rv = RealizedVolatility({
metadata: metadata,
volatility: 0,
timePeriod: 0,
startPrice: 0,
endPrice: 0,
highPrice: 0,
lowPrice: 0,
numberOfDataPoints: 0
});
return rv;
}

function initializeOptions() public pure returns (Options memory) {
Metadata memory metadata = initializeMetadata();
Options memory options = Options({
metadata: metadata,
strikePrice: 0,
impliedVolatility: 0,
timeToExpiry: 0,
isCall: false,
underlyingPrice: 0,
optionPrice: 0,
delta: 0,
gamma: 0,
vega: 0,
theta: 0,
rho: 0
});
return options;
}

function initializePerpetuals() public pure returns (Perp memory) {
Metadata memory metadata = Metadata({feedId: 0, timestamp: 0, numberOfSources: 0, decimals: 0});
Perp memory perpetuals = Perp({metadata: metadata, markPrice: 0, fundingRate: 0, openInterest: 0, volume: 0});
return perpetuals;
}
}
18 changes: 9 additions & 9 deletions solidity/src/libraries/DataParser.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ library DataParser {
uint8 rawDataType = data.toUint8(offset);
FeedType dataType = safeCastToFeedType(rawDataType);

ParsedData memory parsedData;
ParsedData memory parsedData = StructsInitializers.initializeParsedData();
parsedData.dataType = dataType;
if (dataType == FeedType.SpotMedian) {
parsedData.spot = parseSpotData(data);
Expand Down Expand Up @@ -41,7 +41,7 @@ library DataParser {
}

function parseMetadata(bytes memory data, uint256 startIndex) internal pure returns (Metadata memory, uint256) {
Metadata memory metadata;
Metadata memory metadata = StructsInitializers.initializeMetadata();
uint256 index = startIndex;

metadata.feedId = bytes32(data.toUint256(index));
Expand All @@ -60,7 +60,7 @@ library DataParser {
}

function parseSpotData(bytes memory data) internal pure returns (SpotMedian memory) {
SpotMedian memory entry;
SpotMedian memory entry = StructsInitializers.initializeSpotMedian();
uint256 index = 0;

(entry.metadata, index) = parseMetadata(data, index);
Expand All @@ -74,7 +74,7 @@ library DataParser {
}

function parseTWAPData(bytes memory data) internal pure returns (TWAP memory) {
TWAP memory entry;
TWAP memory entry = StructsInitializers.initializeTwap();
uint256 index = 0;

(entry.metadata, index) = parseMetadata(data, index);
Expand All @@ -100,7 +100,7 @@ library DataParser {
}

function parseRealizedVolatilityData(bytes memory data) internal pure returns (RealizedVolatility memory) {
RealizedVolatility memory entry;
RealizedVolatility memory entry = StructsInitializers.initializeRV();
uint256 index = 0;

(entry.metadata, index) = parseMetadata(data, index);
Expand All @@ -117,10 +117,10 @@ library DataParser {
entry.endPrice = data.toUint256(index);
index += 32;

entry.high_price = data.toUint256(index);
entry.highPrice = data.toUint256(index);
index += 32;

entry.low_price = data.toUint256(index);
entry.lowPrice = data.toUint256(index);
index += 32;

entry.numberOfDataPoints = data.toUint256(index);
Expand All @@ -129,7 +129,7 @@ library DataParser {
}

function parseOptionsData(bytes memory data) internal pure returns (Options memory) {
Options memory entry;
Options memory entry = StructsInitializers.initializeOptions();
uint256 index = 0;

(entry.metadata, index) = parseMetadata(data, index);
Expand Down Expand Up @@ -170,7 +170,7 @@ library DataParser {
}

function parsePerpData(bytes memory data) internal pure returns (Perp memory) {
Perp memory entry;
Perp memory entry = StructsInitializers.initializePerpetuals();
uint256 index = 0;

(entry.metadata, index) = parseMetadata(data, index);
Expand Down
8 changes: 4 additions & 4 deletions solidity/test/DataParser.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ contract DataParserTest is Test {
uint256(86400), // timePeriod
uint256(34000 ether), // startPrice
uint256(36000 ether), // endPrice
uint256(37000 ether), // high_price
uint256(33000 ether), // low_price
uint256(37000 ether), // highPrice
uint256(33000 ether), // lowPrice
uint256(1440) // numberOfDataPoints
);

Expand All @@ -109,8 +109,8 @@ contract DataParserTest is Test {
assertEq(result.rv.timePeriod, 86400);
assertEq(result.rv.startPrice, 34000 ether);
assertEq(result.rv.endPrice, 36000 ether);
assertEq(result.rv.high_price, 37000 ether);
assertEq(result.rv.low_price, 33000 ether);
assertEq(result.rv.highPrice, 37000 ether);
assertEq(result.rv.lowPrice, 33000 ether);
assertEq(result.rv.numberOfDataPoints, 1440);
}

Expand Down
4 changes: 2 additions & 2 deletions solidity/test/PragmaDecoder.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ contract PragmaDecoderTest is Test {
assertEq(rv.timePeriod, 86400, "Time period should match");
assertEq(rv.startPrice, 1900 * 1e8, "Start price should match");
assertEq(rv.endPrice, 2100 * 1e8, "End price should match");
assertEq(rv.high_price, 2200 * 1e8, "High price should match");
assertEq(rv.low_price, 1800 * 1e8, "Low price should match");
assertEq(rv.highPrice, 2200 * 1e8, "High price should match");
assertEq(rv.lowPrice, 1800 * 1e8, "Low price should match");
assertEq(rv.numberOfDataPoints, 1440, "Number of data points should match");
}

Expand Down
4 changes: 2 additions & 2 deletions solidity/test/TestUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ library TestUtils {
uint256(86400), // timePeriod
uint256(1900 * 1e8), // startPrice
uint256(2100 * 1e8), // endPrice
uint256(2200 * 1e8), // high_price
uint256(1800 * 1e8), // low_price
uint256(2200 * 1e8), // highPrice
uint256(1800 * 1e8), // lowPrice
uint256(1440) // numberOfDataPoints
);
} else if (dataType == FeedType.Options) {
Expand Down

0 comments on commit 238a9b4

Please sign in to comment.