Skip to content

Commit

Permalink
Reverts on Zero feed IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
alexroan committed Sep 18, 2023
1 parent fc35f03 commit 9c15ef2
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/GMXAutomationBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ contract GMXAutomationBase is Ownable2Step {

// ERRORS
error GMXAutomationBase_OnlyForwarder();
error GMXAutomationBase_ZeroIndexTokenFeedId();
error GMXAutomationBase_ZeroLongTokenFeedId();
error GMXAutomationBase_ZeroShortTokenFeedId();

// IMMUTABLES
DataStore public immutable i_dataStore;
Expand Down Expand Up @@ -79,21 +82,24 @@ contract GMXAutomationBase is Ownable2Step {
function _addPropsToMapping(Market.Props memory marketProps) internal {
if (marketProps.indexToken != address(0)) {
uint256 indexTokenFeedId = uint256(i_dataStore.getBytes32(Keys.realtimeFeedIdKey(marketProps.indexToken)));
if (indexTokenFeedId != 0 && !s_feedIdToMarketTokenMap.contains(indexTokenFeedId)) {
if (indexTokenFeedId == 0) revert GMXAutomationBase_ZeroIndexTokenFeedId();
if (!s_feedIdToMarketTokenMap.contains(indexTokenFeedId)) {
s_feedIdToMarketTokenMap.set(indexTokenFeedId, marketProps.indexToken);
}
}

if (marketProps.longToken != address(0)) {
uint256 longTokenFeedId = uint256(i_dataStore.getBytes32(Keys.realtimeFeedIdKey(marketProps.longToken)));
if (longTokenFeedId != 0 && !s_feedIdToMarketTokenMap.contains(longTokenFeedId)) {
if (longTokenFeedId == 0) revert GMXAutomationBase_ZeroLongTokenFeedId();
if (!s_feedIdToMarketTokenMap.contains(longTokenFeedId)) {
s_feedIdToMarketTokenMap.set(longTokenFeedId, marketProps.longToken);
}
}

if (marketProps.shortToken != address(0)) {
uint256 shortTokenFeedId = uint256(i_dataStore.getBytes32(Keys.realtimeFeedIdKey(marketProps.shortToken)));
if (shortTokenFeedId != 0 && !s_feedIdToMarketTokenMap.contains(shortTokenFeedId)) {
if (shortTokenFeedId == 0) revert GMXAutomationBase_ZeroShortTokenFeedId();
if (!s_feedIdToMarketTokenMap.contains(shortTokenFeedId)) {
s_feedIdToMarketTokenMap.set(shortTokenFeedId, marketProps.shortToken);
}
}
Expand Down
84 changes: 84 additions & 0 deletions test/GMXAutomationBase.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,90 @@ contract GMXAutomationBaseTest__addPropsToMapping is Test {
assertEq(s_gmxAutomation.feedIdToMarketTokenMapGet(uint256(longTokenFeedId)), marketProps.longToken);
assertEq(s_gmxAutomation.feedIdToMarketTokenMapGet(uint256(shortTokenFeedId)), marketProps.shortToken);
}

function test__addPropsToMapping_ZeroIndexTokenFeedId_reverts() public {
Market.Props memory marketProps;
marketProps.indexToken = address(1);
marketProps.longToken = address(2);
marketProps.shortToken = address(3);

bytes32 indexTokenFeedId;
bytes32 longTokenFeedId = bytes32("100");
bytes32 shortTokenFeedId = bytes32("101");
vm.mockCall(
DATA_STORE_ADDRESS,
abi.encodeWithSelector(DataStore.getBytes32.selector, Keys.realtimeFeedIdKey(marketProps.indexToken)),
abi.encode(indexTokenFeedId)
);
vm.mockCall(
DATA_STORE_ADDRESS,
abi.encodeWithSelector(DataStore.getBytes32.selector, Keys.realtimeFeedIdKey(marketProps.longToken)),
abi.encode(longTokenFeedId)
);
vm.mockCall(
DATA_STORE_ADDRESS,
abi.encodeWithSelector(DataStore.getBytes32.selector, Keys.realtimeFeedIdKey(marketProps.shortToken)),
abi.encode(shortTokenFeedId)
);
vm.expectRevert(GMXAutomationBase.GMXAutomationBase_ZeroIndexTokenFeedId.selector);
s_gmxAutomation.addPropsToMapping(marketProps);
}

function test__addPropsToMapping_ZeroLongTokenFeedId_reverts() public {
Market.Props memory marketProps;
marketProps.indexToken = address(1);
marketProps.longToken = address(2);
marketProps.shortToken = address(3);

bytes32 indexTokenFeedId = bytes32("99");
bytes32 longTokenFeedId;
bytes32 shortTokenFeedId = bytes32("101");
vm.mockCall(
DATA_STORE_ADDRESS,
abi.encodeWithSelector(DataStore.getBytes32.selector, Keys.realtimeFeedIdKey(marketProps.indexToken)),
abi.encode(indexTokenFeedId)
);
vm.mockCall(
DATA_STORE_ADDRESS,
abi.encodeWithSelector(DataStore.getBytes32.selector, Keys.realtimeFeedIdKey(marketProps.longToken)),
abi.encode(longTokenFeedId)
);
vm.mockCall(
DATA_STORE_ADDRESS,
abi.encodeWithSelector(DataStore.getBytes32.selector, Keys.realtimeFeedIdKey(marketProps.shortToken)),
abi.encode(shortTokenFeedId)
);
vm.expectRevert(GMXAutomationBase.GMXAutomationBase_ZeroLongTokenFeedId.selector);
s_gmxAutomation.addPropsToMapping(marketProps);
}

function test__addPropsToMapping_ZeroShortTokenFeedId_reverts() public {
Market.Props memory marketProps;
marketProps.indexToken = address(1);
marketProps.longToken = address(2);
marketProps.shortToken = address(3);

bytes32 indexTokenFeedId = bytes32("99");
bytes32 longTokenFeedId = bytes32("100");
bytes32 shortTokenFeedId;
vm.mockCall(
DATA_STORE_ADDRESS,
abi.encodeWithSelector(DataStore.getBytes32.selector, Keys.realtimeFeedIdKey(marketProps.indexToken)),
abi.encode(indexTokenFeedId)
);
vm.mockCall(
DATA_STORE_ADDRESS,
abi.encodeWithSelector(DataStore.getBytes32.selector, Keys.realtimeFeedIdKey(marketProps.longToken)),
abi.encode(longTokenFeedId)
);
vm.mockCall(
DATA_STORE_ADDRESS,
abi.encodeWithSelector(DataStore.getBytes32.selector, Keys.realtimeFeedIdKey(marketProps.shortToken)),
abi.encode(shortTokenFeedId)
);
vm.expectRevert(GMXAutomationBase.GMXAutomationBase_ZeroShortTokenFeedId.selector);
s_gmxAutomation.addPropsToMapping(marketProps);
}
}

contract GMXAutomationBaseTest__flushMapping is Test {
Expand Down

0 comments on commit 9c15ef2

Please sign in to comment.