Skip to content

Commit b950569

Browse files
aditya520cctdaniel
authored andcommitted
update
1 parent 596be48 commit b950569

File tree

7 files changed

+1687
-923
lines changed

7 files changed

+1687
-923
lines changed

target_chains/cosmwasm/examples/cw-contract/Cargo.lock

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

target_chains/ethereum/contracts/contracts/pyth/Pyth.sol

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,88 @@ abstract contract Pyth is
308308
);
309309
}
310310

311+
function parseTwapPriceFeedUpdates(
312+
bytes[2][] calldata updateData,
313+
bytes32[] calldata priceIds,
314+
unit8 windowSize
315+
)
316+
external
317+
payable
318+
override
319+
returns (PythStructs.TwapPriceFeed[] memory twapPriceFeeds)
320+
{
321+
{
322+
revert(updateData.length != 2, PythErrors.InvalidUpdateData());
323+
uint requiredFee = getUpdateFee(updateData[0]);
324+
325+
// Check if the two updateData contains the same number of priceUpdates
326+
if (requiredFee != getUpdateFee(updateData[1])) {
327+
revert PythErrors.InvalidUpdateData();
328+
}
329+
if (msg.value < requiredFee) revert PythErrors.InsufficientFee();
330+
}
331+
332+
// Parse the updateData
333+
twapPriceFeeds = new PythStructs.TwapPriceFeed[](priceIds.length);
334+
for (uint i = 0; i < updateData[0].length; i++) {
335+
if (
336+
(updateData[0][i].length > 4 &&
337+
UnsafeCalldataBytesLib.toUint32(updateData[0][i], 0) ==
338+
ACCUMULATOR_MAGIC) &&
339+
(updateData[1][i].length > 4 &&
340+
UnsafeCalldataBytesLib.toUint32(updateData[1][i], 0) ==
341+
ACCUMULATOR_MAGIC)
342+
) {
343+
// Parse the accumulator update
344+
uint offsetFirst;
345+
uint offsetSecond;
346+
{
347+
UpdateType updateType;
348+
(offsetFirst, updateType) = extractUpdateTypeFromAccumulatorHeader(updateData[0][i]);
349+
if (updateType != UpdateType.WormholeMerkle) {
350+
revert PythErrors.InvalidUpdateData();
351+
}
352+
(offsetSecond, updateType) = extractUpdateTypeFromAccumulatorHeader(updateData[1][i]);
353+
if (updateType != UpdateType.WormholeMerkle) {
354+
revert PythErrors.InvalidUpdateData();
355+
}
356+
}
357+
358+
bytes20 digestFirst;
359+
bytes20 digestSecond;
360+
uint8 numUpdatesFirst;
361+
uint8 numUpdatesSecond;
362+
bytes calldata encodedFirst;
363+
bytes calldata encodedSecond;
364+
(
365+
offsetFirst,
366+
digestFirst,
367+
numUpdates,
368+
encodedFirst
369+
) = extractWormholeMerkleHeaderDigestAndNumUpdatesAndEncodedFromAccumulatorUpdate(
370+
updateData[0][i],
371+
offsetFirst
372+
);
373+
(
374+
offsetSecond,
375+
digestSecond,
376+
numUpdates,
377+
encodedSecond
378+
) = extractWormholeMerkleHeaderDigestAndNumUpdatesAndEncodedFromAccumulatorUpdate(
379+
updateData[1][i],
380+
offsetSecond);
381+
382+
if (numUpdatesFirst != numUpdatesSecond) {
383+
revert PythErrors.InvalidUpdateData();
384+
}
385+
386+
387+
} else {
388+
revert PythErrors.InvalidUpdateData();
389+
}
390+
}
391+
}
392+
311393
function parsePriceFeedUpdatesUnique(
312394
bytes[] calldata updateData,
313395
bytes32[] calldata priceIds,

target_chains/ethereum/contracts/contracts/pyth/PythAccumulator.sol

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ abstract contract PythAccumulator is PythGetters, PythSetters, AbstractPyth {
2525
}
2626

2727
enum MessageType {
28-
PriceFeed
28+
PriceFeed,
29+
TwapPriceFeed
2930
}
3031

3132
// This method is also used by batch attestation but moved here
@@ -262,6 +263,11 @@ abstract contract PythAccumulator is PythGetters, PythSetters, AbstractPyth {
262263
encodedMessage,
263264
1
264265
);
266+
} else if (messageType == MessageType.TwapPriceFeed) {
267+
(twapPriceInfo, priceId) = parseTwapPriceFeedMessage(
268+
encodedMessage,
269+
1
270+
);
265271
} else {
266272
revert PythErrors.InvalidUpdateData();
267273
}
@@ -335,6 +341,70 @@ abstract contract PythAccumulator is PythGetters, PythSetters, AbstractPyth {
335341
}
336342
}
337343

344+
function parseTwapPriceFeedMessage(
345+
bytes calldata encodedTwapPriceFeed,
346+
uint offset
347+
)
348+
private
349+
pure
350+
returns (
351+
PythInternalStructs.TwapPriceInfo memory twapPriceInfo,
352+
bytes32 priceId
353+
)
354+
{
355+
unchecked {
356+
priceId = UnsafeCalldataBytesLib.toBytes32(
357+
encodedPriceFeed,
358+
offset
359+
);
360+
offset += 32;
361+
362+
twapPriceInfo.cumulativePrice = int128(
363+
UnsafeCalldataBytesLib.toUint64(encodedPriceFeed, offset)
364+
);
365+
offset += 16;
366+
367+
twapPriceInfo.cumulativeConf = UnsafeCalldataBytesLib.toUint128(
368+
encodedPriceFeed,
369+
offset
370+
);
371+
offset += 16;
372+
373+
twapPriceInfo.numDownSlots = UnsafeCalldataBytesLib.toUint64(
374+
encodedPriceFeed,
375+
offset
376+
);
377+
offset += 8;
378+
379+
twapPriceInfo.publishSlot = UnsafeCalldataBytesLib.toUint64(
380+
encodedPriceFeed,
381+
offset
382+
);
383+
offset += 8;
384+
385+
twapPriceInfo.publishTime = UnsafeCalldataBytesLib.toUint64(
386+
encodedPriceFeed,
387+
offset
388+
);
389+
offset += 8;
390+
391+
twapPriceInfo.prevPublishTime = UnsafeCalldataBytesLib.toUint64(
392+
encodedPriceFeed,
393+
offset
394+
);
395+
offset += 8;
396+
397+
twapPriceInfo.expo = int32(
398+
UnsafeCalldataBytesLib.toUint32(encodedPriceFeed, offset)
399+
);
400+
offset += 4;
401+
402+
if (offset > encodedPriceFeed.length)
403+
revert PythErrors.InvalidUpdateData();
404+
}
405+
}
406+
407+
338408
function updatePriceInfosFromAccumulatorUpdate(
339409
bytes calldata accumulatorUpdate
340410
) internal returns (uint8 numUpdates) {

target_chains/ethereum/contracts/contracts/pyth/PythInternalStructs.sol

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@ contract PythInternalStructs {
2626
uint64 emaConf;
2727
}
2828

29+
struct TwapPriceInfo {
30+
// slot 1
31+
int128 cumulativePrice;
32+
uint128 cumulativeConf;
33+
34+
// slot 2
35+
uint64 numDownSlots;
36+
uint64 publishSlot;
37+
int64 publishTime;
38+
int64 prevPublishTime;
39+
// slot 3
40+
41+
int32 expo;
42+
}
43+
2944
struct DataSource {
3045
uint16 chainId;
3146
bytes32 emitterAddress;

target_chains/ethereum/sdk/solidity/PythStructs.sol

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,17 @@ contract PythStructs {
3030
// Latest available exponentially-weighted moving average price
3131
Price emaPrice;
3232
}
33-
}
33+
34+
struct TwapPriceFeed {
35+
// The price ID.
36+
bytes32 id;
37+
// Start time of the TWAP
38+
uint startTime;
39+
// End time of the TWAP
40+
uint endTime;
41+
// TWAP price
42+
Price cumulativePrice;
43+
// Down slot ratio
44+
uint32 downSlotRatio;
45+
}
46+
1}

target_chains/fuel/contracts/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)