Skip to content

Commit 514f58f

Browse files
committed
feat(lazer) Convert market Session to enum
1 parent 63e2d29 commit 514f58f

File tree

5 files changed

+59
-18
lines changed

5 files changed

+59
-18
lines changed

lazer/contracts/evm/script/fetch_pyth_payload.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ response_feed3=$(curl -X GET "$API_URL" \
1212
--header "Content-Type: application/json" \
1313
--data-raw '{
1414
"priceFeedIds": [3],
15-
"properties": ["price", "bestBidPrice", "bestAskPrice", "publisherCount", "exponent", "confidence"],
15+
"properties": ["price", "bestBidPrice", "bestAskPrice", "publisherCount", "exponent", "confidence", "marketSession"],
1616
"chains": ["evm"],
1717
"channel": "fixed_rate@200ms",
1818
"deliveryFormat": "json",

lazer/contracts/evm/src/PythLazerLib.sol

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,10 +412,18 @@ library PythLazerLib {
412412
} else if (
413413
property == PythLazerStructs.PriceFeedProperty.MarketSession
414414
) {
415-
(feed._marketSession, pos) = parseFeedValueInt16(
415+
int16 marketSessionValue;
416+
(marketSessionValue, pos) = parseFeedValueInt16(
416417
payload,
417418
pos
418419
);
420+
require(
421+
marketSessionValue >= 0 && marketSessionValue <= 4,
422+
"Invalid market session value"
423+
);
424+
feed._marketSession = PythLazerStructs.MarketSession(
425+
uint8(uint16(marketSessionValue))
426+
);
419427
_setPresent(
420428
feed,
421429
uint8(PythLazerStructs.PriceFeedProperty.MarketSession)
@@ -771,7 +779,7 @@ library PythLazerLib {
771779
/// @notice Get market session (reverts if not exists)
772780
function getMarketSession(
773781
PythLazerStructs.Feed memory feed
774-
) public pure returns (int16) {
782+
) public pure returns (PythLazerStructs.MarketSession) {
775783
require(
776784
isMarketSessionRequested(feed),
777785
"Market session is not requested for the timestamp"

lazer/contracts/evm/src/PythLazerStructs.sol

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ library PythLazerStructs {
3333
Present
3434
}
3535

36+
enum MarketSession {
37+
Regular,
38+
PreMarket,
39+
PostMarket,
40+
OverNight,
41+
Closed
42+
}
43+
3644
struct Feed {
3745
// NOTE: Do not access fields directly. Use PythLazerLib getters (getPrice, hasPrice, isPriceRequested, etc.)
3846

@@ -53,8 +61,8 @@ library PythLazerStructs {
5361
int64 _fundingRate;
5462
uint64 _fundingTimestamp;
5563
uint64 _fundingRateInterval;
56-
// Slot 4: 2 bytes
57-
int16 _marketSession;
64+
// Slot 4: 1 byte
65+
MarketSession _marketSession;
5866
}
5967

6068
struct Update {

lazer/contracts/evm/test/PythLazer.t.sol

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,10 @@ contract PythLazerTest is Test {
219219
assertEq(feed._fundingRate, 123456);
220220
assertEq(feed._fundingTimestamp, 1234567890);
221221
assertEq(feed._fundingRateInterval, 3600);
222-
assertEq(feed._marketSession, 0);
222+
assertEq(
223+
uint8(feed._marketSession),
224+
uint8(PythLazerStructs.MarketSession.Regular)
225+
);
223226

224227
// Verify exists flags (all should be set)
225228
assertTrue(PythLazerLib.hasPrice(feed));
@@ -395,10 +398,16 @@ contract PythLazerTest is Test {
395398

396399
PythLazerStructs.Feed memory feed = update.feeds[0];
397400

398-
assertEq(feed._marketSession, 1);
401+
assertEq(
402+
uint8(feed._marketSession),
403+
uint8(PythLazerStructs.MarketSession.PreMarket)
404+
);
399405
assertTrue(PythLazerLib.hasMarketSession(feed));
400406
assertTrue(PythLazerLib.isMarketSessionRequested(feed));
401-
assertEq(PythLazerLib.getMarketSession(feed), 1);
407+
assertEq(
408+
uint8(PythLazerLib.getMarketSession(feed)),
409+
uint8(PythLazerStructs.MarketSession.PreMarket)
410+
);
402411

403412
// Test different market session values
404413
bytes[] memory properties2 = new bytes[](3);
@@ -419,8 +428,14 @@ contract PythLazerTest is Test {
419428
.parseUpdateFromPayload(payload2);
420429

421430
PythLazerStructs.Feed memory feed2 = update2.feeds[0];
422-
assertEq(feed2._marketSession, 2);
423-
assertEq(PythLazerLib.getMarketSession(feed2), 2);
431+
assertEq(
432+
uint8(feed2._marketSession),
433+
uint8(PythLazerStructs.MarketSession.PostMarket)
434+
);
435+
assertEq(
436+
uint8(PythLazerLib.getMarketSession(feed2)),
437+
uint8(PythLazerStructs.MarketSession.PostMarket)
438+
);
424439
}
425440

426441
/// @notice Test MarketSession getter when not requested

lazer/contracts/evm/test/PythLazerApi.t.sol

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,19 @@ contract PythLazerApiTest is Test {
156156
PythLazerLib.hasMarketSession(feed3),
157157
"Feed 3: if market session is requested, it should be present"
158158
);
159-
int16 marketSession = PythLazerLib.getMarketSession(feed3);
160-
// MarketSession should be a valid value (0-4 based on enum)
161-
assertGe(marketSession, 0, "Feed 3: market session should be >= 0");
162-
assertLe(marketSession, 4, "Feed 3: market session should be <= 4");
159+
PythLazerStructs.MarketSession marketSession = PythLazerLib
160+
.getMarketSession(feed3);
161+
// MarketSession should be a valid enum value (0-4)
162+
assertGe(
163+
uint8(marketSession),
164+
0,
165+
"Feed 3: market session should be >= 0"
166+
);
167+
assertLe(
168+
uint8(marketSession),
169+
4,
170+
"Feed 3: market session should be <= 4"
171+
);
163172
}
164173

165174
// Verify parsed values match API reference values exactly
@@ -318,15 +327,16 @@ contract PythLazerApiTest is Test {
318327
PythLazerLib.hasMarketSession(feed112),
319328
"Feed 112: if market session is requested, it should be present"
320329
);
321-
int16 marketSession = PythLazerLib.getMarketSession(feed112);
322-
// MarketSession should be a valid value (0-4 based on enum)
330+
PythLazerStructs.MarketSession marketSession = PythLazerLib
331+
.getMarketSession(feed112);
332+
// MarketSession should be a valid enum value (0-4)
323333
assertGe(
324-
marketSession,
334+
uint8(marketSession),
325335
0,
326336
"Feed 112: market session should be >= 0"
327337
);
328338
assertLe(
329-
marketSession,
339+
uint8(marketSession),
330340
4,
331341
"Feed 112: market session should be <= 4"
332342
);

0 commit comments

Comments
 (0)