Skip to content

Commit

Permalink
fix(refactor): first draft changes
Browse files Browse the repository at this point in the history
  • Loading branch information
JordyRo1 committed Sep 12, 2024
1 parent 2e440ea commit a85ce05
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
13 changes: 11 additions & 2 deletions solidity/src/libraries/DataParser.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ pragma solidity ^0.8.0;

import "./BytesLib.sol";
import "../interfaces/PragmaStructs.sol";
import "./ErrorsLib.sol";

library DataParser {
using BytesLib for bytes;

function parse(bytes memory data) internal pure returns (ParsedData memory) {
uint8 offset = 2; // type feed stored after asset class
uint16 rawDataType = data.toUint16(offset);
FeedType dataType = FeedType(rawDataType);
FeedType dataType = safeCastToFeedType(rawDataType);

ParsedData memory parsedData;
parsedData.dataType = dataType;
Expand All @@ -25,12 +26,20 @@ library DataParser {
} else if (dataType == FeedType.Perpetuals) {
parsedData.perp = parsePerpData(data);
} else {
revert("Unknown data type");
revert ErrorsLib.InvalidDataFeedType();
}

return parsedData;
}

function safeCastToFeedType(uint16 rawDataType) internal pure returns (FeedType) {
if (rawDataType <= uint16(type(FeedType).max)) {
return FeedType(rawDataType);
} else {
revert ErrorsLib.InvalidDataFeedType();
}
}

function parseMetadata(bytes memory data, uint256 startIndex) internal pure returns (Metadata memory, uint256) {
Metadata memory metadata;
uint256 index = startIndex;
Expand Down
1 change: 0 additions & 1 deletion solidity/src/libraries/MerkleTree.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
pragma solidity ^0.8.0;

import "./UnsafeCalldataBytesLib.sol";
import "forge-std/console2.sol";

/**
* @dev This library provides methods to construct and verify Merkle Tree proofs efficiently.
Expand Down
22 changes: 9 additions & 13 deletions solidity/test/DataParser.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ pragma solidity ^0.8.0;

import "forge-std/Test.sol";
import "../src/libraries/DataParser.sol";
import "../src/libraries/ErrorsLib.sol";


contract DataParserTest is Test {
function testParseSpotMedianEntry() public pure {
bytes32 feedId = bytes32(
abi.encodePacked(
uint16(0),
///CRYPTO
uint16(0),///CRYPTO
uint16(0), //SPOT
bytes32("BTC/USD")
)
Expand Down Expand Up @@ -37,8 +38,7 @@ contract DataParserTest is Test {
function testParseTWAPEntry() public pure {
bytes32 feedId = bytes32(
abi.encodePacked(
uint16(0),
///CRYPTO
uint16(0),///CRYPTO
uint16(1), //TWAP
bytes32("ETH/USD")
)
Expand Down Expand Up @@ -74,8 +74,7 @@ contract DataParserTest is Test {
function testParseRealizedVolatilityEntry() public pure {
bytes32 feedId = bytes32(
abi.encodePacked(
uint16(0),
///CRYPTO
uint16(0),///CRYPTO
uint16(2), //RV
bytes32("BTC/USD")
)
Expand Down Expand Up @@ -113,8 +112,7 @@ contract DataParserTest is Test {
function testParseOptionsEntry() public pure {
bytes32 feedId = bytes32(
abi.encodePacked(
uint16(0),
///CRYPTO
uint16(0),///CRYPTO
uint16(3), //Option
bytes32("ETH/USD")
)
Expand Down Expand Up @@ -160,8 +158,7 @@ contract DataParserTest is Test {
function testParsePerpEntry() public pure {
bytes32 feedId = bytes32(
abi.encodePacked(
uint16(0),
///CRYPTO
uint16(0),///CRYPTO
uint16(4), //PERP
bytes32("BTC/USD")
)
Expand Down Expand Up @@ -193,8 +190,7 @@ contract DataParserTest is Test {
function testParseUnknownDataType() public {
bytes32 feedId = bytes32(
abi.encodePacked(
uint16(0),
///CRYPTO
uint16(0), ///CRYPTO
uint16(10), //Unkown data type
bytes32("BTC/USD")
)
Expand All @@ -209,7 +205,7 @@ contract DataParserTest is Test {
uint256(1000 ether), // openInterest
uint256(500 ether) // volume
);
vm.expectRevert();
vm.expectRevert(abi.encodeWithSignature("InvalidDataFeedType()"));
DataParser.parse(data);
}
}
15 changes: 5 additions & 10 deletions solidity/test/PragmaDecoder.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@ contract PragmaDecoderTest is Test {
_setUp(FeedType.SpotMedian);
bytes32 feedId = bytes32(
abi.encodePacked(
uint16(0),
///CRYPTO
uint16(0),///CRYPTO
uint16(0), //SPOT
bytes32("ETH/USD")
)
Expand Down Expand Up @@ -202,8 +201,7 @@ contract PragmaDecoderTest is Test {
_setUp(FeedType.Twap);
bytes32 feedId = bytes32(
abi.encodePacked(
uint16(0),
///CRYPTO
uint16(0),///CRYPTO
uint16(1), //TWAP
bytes32("BTC/USD")
)
Expand Down Expand Up @@ -239,8 +237,7 @@ contract PragmaDecoderTest is Test {
_setUp(FeedType.RealizedVolatility);
bytes32 feedId = bytes32(
abi.encodePacked(
uint16(0),
///CRYPTO
uint16(0),///CRYPTO
uint16(2), //RV
bytes32("ETH/USD")
)
Expand Down Expand Up @@ -275,8 +272,7 @@ contract PragmaDecoderTest is Test {
_setUp(FeedType.Options);
bytes32 feedId = bytes32(
abi.encodePacked(
uint16(0),
///CRYPTO
uint16(0),///CRYPTO
uint16(3), //Options
bytes32("ETH/USD")
)
Expand Down Expand Up @@ -317,8 +313,7 @@ contract PragmaDecoderTest is Test {
_setUp(FeedType.Perpetuals);
bytes32 feedId = bytes32(
abi.encodePacked(
uint16(0),
///CRYPTO
uint16(0),///CRYPTO
uint16(4), //Perp
bytes32("ETH/USD")
)
Expand Down
7 changes: 4 additions & 3 deletions solidity/test/utils/PragmaTestUtils.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ abstract contract PragmaTestUtils is Test, RandTestUtils, HyperlaneTestUtils {
bytes32 constant SOURCE_EMITTER_ADDRESS = 0x03dA250675D8c2BB7cef7E1b7FDFe17aA4D5752Ed82A9333e4F9a12b22E521aa;

uint256 constant SINGLE_UPDATE_FEE_IN_WEI = 1;
uint256 constant VALID_timePeriod_IN_SECONDS = 60;
uint256 constant VALID_TIME_PERIOD_IN_SECONDS = 60;
uint64 constant MOCK_TIMESTAMP_VALUE= 1234;

function setUpPragma(address hyperlane) public returns (address) {
uint16[] memory emitterChainIds = new uint16[](1);
Expand All @@ -24,7 +25,7 @@ abstract contract PragmaTestUtils is Test, RandTestUtils, HyperlaneTestUtils {
emitterAddresses[0] = SOURCE_EMITTER_ADDRESS;

Pragma pragma_ = new Pragma(
hyperlane, emitterChainIds, emitterAddresses, VALID_timePeriod_IN_SECONDS, SINGLE_UPDATE_FEE_IN_WEI
hyperlane, emitterChainIds, emitterAddresses, VALID_TIME_PERIOD_IN_SECONDS, SINGLE_UPDATE_FEE_IN_WEI
);

return address(pragma_);
Expand Down Expand Up @@ -82,7 +83,7 @@ abstract contract PragmaTestUtils is Test, RandTestUtils, HyperlaneTestUtils {
bytes memory hyperlanePayload = abi.encodePacked(rootDigest);

bytes memory updateData = generateUpdateData(
1234, 0, config.source_chain_id, config.source_emitter_address, hyperlanePayload, config.numSigners
MOCK_TIMESTAMP_VALUE, 0, config.source_chain_id, config.source_emitter_address, hyperlanePayload, config.numSigners
);

if (config.brokenSignature) {
Expand Down

0 comments on commit a85ce05

Please sign in to comment.