Skip to content

Commit

Permalink
fix:
Browse files Browse the repository at this point in the history
  • Loading branch information
JordyRo1 committed Nov 3, 2024
1 parent fdce3ca commit b71b23e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 66 deletions.
41 changes: 12 additions & 29 deletions solidity/test/DataParser.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ pragma solidity ^0.8.0;
import "forge-std/Test.sol";
import "../src/libraries/DataParser.sol";
import "../src/libraries/ErrorsLib.sol";
import "./utils/TestConstants.sol";

contract DataParserTest is Test {
function testParseSpotMedianEntry() public pure {
bytes32 feedId = bytes32(
abi.encodePacked(
uint16(0),
///CRYPTO
uint8(0), //SPOT
uint8(0), //VARIANT
bytes32("BTC/USD")
TestConstantsLib.CRYPTO, TestConstantsLib.SPOT, TestConstantsLib.UNIQUE, TestConstantsLib.BTC_USD
)
);
bytes memory data = abi.encodePacked(
Expand All @@ -39,11 +36,7 @@ contract DataParserTest is Test {
function testParseTWAPEntry() public pure {
bytes32 feedId = bytes32(
abi.encodePacked(
uint16(0),
///CRYPTO
uint8(1), //TWAP
uint8(0), //VARIANT
bytes32("ETH/USD")
TestConstantsLib.CRYPTO, TestConstantsLib.TWAP, TestConstantsLib.UNIQUE, TestConstantsLib.ETH_USD
)
);
bytes memory data = abi.encodePacked(
Expand Down Expand Up @@ -77,11 +70,10 @@ contract DataParserTest is Test {
function testParseRealizedVolatilityEntry() public pure {
bytes32 feedId = bytes32(
abi.encodePacked(
uint16(0),
///CRYPTO
uint8(2), //RV
uint8(0), //VARIANT
bytes32("BTC/USD")
TestConstantsLib.CRYPTO,
TestConstantsLib.REALIZED_VOLATILITY,
TestConstantsLib.UNIQUE,
TestConstantsLib.BTC_USD
)
);
bytes memory data = abi.encodePacked(
Expand Down Expand Up @@ -117,11 +109,7 @@ contract DataParserTest is Test {
function testParseOptionsEntry() public pure {
bytes32 feedId = bytes32(
abi.encodePacked(
uint16(0),
///CRYPTO
uint8(3), //Option
uint8(0), //VARIANT
bytes32("ETH/USD")
TestConstantsLib.CRYPTO, TestConstantsLib.OPTIONS, TestConstantsLib.UNIQUE, TestConstantsLib.ETH_USD
)
);
bytes memory data = abi.encodePacked(
Expand Down Expand Up @@ -165,11 +153,7 @@ contract DataParserTest is Test {
function testParsePerpEntry() public pure {
bytes32 feedId = bytes32(
abi.encodePacked(
uint16(0),
///CRYPTO
uint8(4), //PERP
uint8(0), //VARIANT
bytes32("BTC/USD")
TestConstantsLib.CRYPTO, TestConstantsLib.PERP, TestConstantsLib.UNIQUE, TestConstantsLib.BTC_USD
)
);
bytes memory data = abi.encodePacked(
Expand Down Expand Up @@ -199,11 +183,10 @@ contract DataParserTest is Test {
function testParseUnknownDataType() public {
bytes32 feedId = bytes32(
abi.encodePacked(
uint16(0),
///CRYPTO
TestConstantsLib.CRYPTO,
uint8(20), //Unkown data type
uint8(0),
bytes32("BTC/USD")
TestConstantsLib.UNIQUE,
TestConstantsLib.BTC_USD
)
);
bytes memory data = abi.encodePacked(
Expand Down
39 changes: 11 additions & 28 deletions solidity/test/PragmaDecoder.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ contract PragmaHarnessTest is Test {
pragmaHarness = PragmaHarness(TestUtils.configurePragmaContract(dataType));
}

function setupRaw() public {
pragmaHarness = PragmaHarness(TestUtils.configurePragmaRawContract());
function setupRealPragmaHarness() public {
pragmaHarness = PragmaHarness(TestUtils.setupRealEnvironment());
}

function testUpdateRawFeed() public {
setupRaw();
function testUpdateRealFeed() public {
setupRealPragmaHarness();
// encoded update
bytes memory encodedUpdate =
hex"0100000170030100c1ec5070f1a4868b8e6bfa5bbd31ac77605c5af1a739bc4e7758d4ca1d88fa8835c1460646b647c4c4403b324c2297a04d70b84888dc873021f80d6d70ed015e1c00031b8b0000000067225b1100611a3d0060240f2bccef7e64f920eec05c5bfffbc48c6ceaa4efca8748772b60cbafc30536953cdd0dd5b8e24428e4fb6eab5c143daba15f62b24606e50d822508faefd53032e26a3b1d1510dfe82a2ab8d6c0fc0f010dcdd3c410ba2f9fdad3479b1400031b8b15704e0efd1955cfe1c1182ba083bd5309707bdd795397cbbbb106cfc9b29bb001000100000000000000000000004254432f555344000000000000000000000000000000000000000067225b1100080800000000000000000000068aa5cb9d63000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004254432f5553440000000067225b11";
Expand All @@ -46,11 +46,7 @@ contract PragmaHarnessTest is Test {
_setUp(FeedType.SpotMedian);
bytes32 feedId = bytes32(
abi.encodePacked(
uint16(0),
///CRYPTO
uint8(0), //SPOT
uint8(0), //VARIANT
TestConstantsLib.ETH_USD
TestConstantsLib.CRYPTO, TestConstantsLib.SPOT, TestConstantsLib.UNIQUE, TestConstantsLib.ETH_USD
)
);

Expand All @@ -73,11 +69,7 @@ contract PragmaHarnessTest is Test {
_setUp(FeedType.Twap);
bytes32 feedId = bytes32(
abi.encodePacked(
uint16(0),
///CRYPTO
uint8(1), //TWAP
uint8(0), // VARIANT,
TestConstantsLib.BTC_USD
TestConstantsLib.CRYPTO, TestConstantsLib.TWAP, TestConstantsLib.UNIQUE, TestConstantsLib.BTC_USD
)
);
bytes memory encodedUpdate = TestUtils.createEncodedUpdate(FeedType.Twap, feedId);
Expand All @@ -103,10 +95,9 @@ contract PragmaHarnessTest is Test {
_setUp(FeedType.RealizedVolatility);
bytes32 feedId = bytes32(
abi.encodePacked(
uint16(0),
///CRYPTO
uint8(2), //RV
uint8(0), //VARIANT
TestConstantsLib.CRYPTO,
TestConstantsLib.REALIZED_VOLATILITY,
TestConstantsLib.UNIQUE,
TestConstantsLib.BTC_USD
)
);
Expand All @@ -132,11 +123,7 @@ contract PragmaHarnessTest is Test {
_setUp(FeedType.Options);
bytes32 feedId = bytes32(
abi.encodePacked(
uint16(0),
///CRYPTO
uint8(3), //Options
uint8(0),
TestConstantsLib.BTC_USD
TestConstantsLib.CRYPTO, TestConstantsLib.OPTIONS, TestConstantsLib.UNIQUE, TestConstantsLib.BTC_USD
)
);
bytes memory encodedUpdate = TestUtils.createEncodedUpdate(FeedType.Options, feedId);
Expand Down Expand Up @@ -167,11 +154,7 @@ contract PragmaHarnessTest is Test {
_setUp(FeedType.Perpetuals);
bytes32 feedId = bytes32(
abi.encodePacked(
uint16(0),
///CRYPTO
uint8(4), //Perp
uint8(0), //VARIANT
TestConstantsLib.BTC_USD
TestConstantsLib.CRYPTO, TestConstantsLib.PERP, TestConstantsLib.UNIQUE, TestConstantsLib.BTC_USD
)
);
bytes memory encodedUpdate = TestUtils.createEncodedUpdate(FeedType.Perpetuals, feedId);
Expand Down
12 changes: 7 additions & 5 deletions solidity/test/TestUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import "../src/interfaces/IHyperlane.sol";
import "./PragmaDecoder.t.sol";
import "../src/Hyperlane.sol";
import "../src/Pragma.sol";
import "./utils/TestConstants.sol";
import {TransparentUpgradeableProxy} from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";

contract PragmaHarness is Pragma {
Expand Down Expand Up @@ -90,7 +91,7 @@ library TestUtils {
);

return abi.encodePacked(
uint8(3), // version
TestConstantsLib.HYPERLANE_VERSION,
signatures,
uint32(0), // nonce
uint64(block.timestamp), // timestamp
Expand Down Expand Up @@ -183,9 +184,9 @@ library TestUtils {
bytes memory hyMsg = createHyperlaneMessage(hyMsgPayload, feedId);

return abi.encodePacked(
uint8(1), // majorVersion
uint8(0), // minorVersion
uint8(0), // trailingHeaderSize
TestConstantsLib.MAJOR_VERSION,
TestConstantsLib.MINOR_VERSION,
TestConstantsLib.TRAILING_HEADER_SIZE,
uint16(hyMsg.length), // hyMsgSize
hyMsg
);
Expand Down Expand Up @@ -292,7 +293,8 @@ library TestUtils {
return address(proxy);
}

function configurePragmaRawContract() internal returns (address) {
// This function reproduce a real evm configuration for testing purposes
function setupRealEnvironment() internal returns (address) {
address[] memory validatorSets = new address[](1);
validatorSets[0] = address(0xF6311461A6d8b44cb3F62b2FCd47570A28443ca0);
IHyperlane hyperlane = IHyperlane(setUpHyperlane(uint8(validatorSets.length), validatorSets));
Expand Down
8 changes: 4 additions & 4 deletions solidity/test/utils/PragmaTestUtils.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import "../../src/libraries/MerkleTree.sol";
import "./RandTestUtils.t.sol";
import "./HyperlaneTestUtils.t.sol";
import {DataFeedType} from "../../src/interfaces/IPragma.sol";
import "../utils/TestConstants.sol";

abstract contract PragmaTestUtils is Test, RandTestUtils, HyperlaneTestUtils {
uint32 constant SOURCE_EMITTER_CHAIN_ID = 0x1;
bytes32 constant SOURCE_EMITTER_ADDRESS = 0x03dA250675D8c2BB7cef7E1b7FDFe17aA4D5752Ed82A9333e4F9a12b22E521aa;

uint256 constant SINGLE_UPDATE_FEE_IN_WEI = 1;
uint256 constant VALID_TIME_PERIOD_IN_SECONDS = 60;
uint32 constant MOCK_NONCE_VALUE = 1234;
Expand Down Expand Up @@ -114,9 +114,9 @@ abstract contract PragmaTestUtils is Test, RandTestUtils, HyperlaneTestUtils {
}

hyMerkleUpdateData = abi.encodePacked(
uint8(1), // major version
uint8(0), // minor version
uint8(0), // trailing header size
TestConstantsLib.MAJOR_VERSION,
TestConstantsLib.MINOR_VERSION,
TestConstantsLib.TRAILING_HEADER_SIZE,
uint16(updateData.length),
updateData,
uint8(dataFeedMessages.length)
Expand Down
21 changes: 21 additions & 0 deletions solidity/test/utils/TestConstants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,29 @@
library TestConstantsLib {
/// @dev The hyperlane version
uint8 internal constant HYPERLANE_VERSION = 3;
/// @dev Major version
uint8 internal constant MAJOR_VERSION = 1;
/// @dev Minor version
uint8 internal constant MINOR_VERSION = 0;
/// @dev Minor version
uint8 internal constant TRAILING_HEADER_SIZE = 0;
/// @dev ETH/USD as uint256
uint256 internal constant ETH_USD = uint256(0x4554482f555344);
/// @dev BTC/USD as uint256
uint256 internal constant BTC_USD = uint256(0x4254432f555344);

/// @dev CRYPTO encoding (0 -> Crypto, see https://docs.pragma.build/ for reference)
uint16 internal constant CRYPTO = uint16(0);
/// @dev UNIQUE encoding (0 -> Unique,see https://docs.pragma.build/ for reference)
uint8 internal constant UNIQUE = uint8(0);
/// @dev SPOT encoding (0 -> Spot, see https://docs.pragma.build/ for reference)
uint8 internal constant SPOT = uint8(0);
/// @dev TWAP encoding (1 -> Twap, see https://docs.pragma.build/ for reference)
uint8 internal constant TWAP = uint8(1);
/// @dev REALIZED_VOLATILITY encoding (2 -> Realized volatility, see https://docs.pragma.build/ for reference)
uint8 internal constant REALIZED_VOLATILITY = uint8(2);
/// @dev OPTIONS encoding (3 -> Crypto, see https://docs.pragma.build/ for reference)
uint8 internal constant OPTIONS = uint8(3);
/// @dev PERP encoding (4 -> Crypto, see https://docs.pragma.build/ for reference)
uint8 internal constant PERP = uint8(4);
}

0 comments on commit b71b23e

Please sign in to comment.