Skip to content

Commit 4891ad2

Browse files
committed
feat(pyth): add getTwapUpdateFee function to calculate TWAP update fees
1 parent 78aff34 commit 4891ad2

File tree

7 files changed

+64
-10
lines changed

7 files changed

+64
-10
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ abstract contract Pyth is
120120
return getTotalFee(totalNumUpdates);
121121
}
122122

123+
function getTwapUpdateFee() public view override returns (uint feeAmount) {
124+
// In the accumulator update data a single update can contain
125+
// up to 255 messages and we charge a singleUpdateFee per each
126+
// message
127+
return singleUpdateFeeInWei() + transactionFeeInWei();
128+
}
129+
123130
// This is an overwrite of the same method in AbstractPyth.sol
124131
// to be more gas efficient.
125132
function updatePriceFeedsIfNecessary(
@@ -454,7 +461,7 @@ abstract contract Pyth is
454461
revert PythErrors.InvalidUpdateData();
455462
}
456463

457-
uint requiredFee = getUpdateFee(updateData);
464+
uint requiredFee = getTwapUpdateFee();
458465
if (msg.value < requiredFee) revert PythErrors.InsufficientFee();
459466

460467
// Process start update data

target_chains/ethereum/contracts/forge-test/Pyth.t.sol

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ contract PythTest is Test, WormholeTestUtils, PythTestUtils {
533533
)
534534
);
535535

536-
uint updateFee = pyth.getUpdateFee(updateData);
536+
uint updateFee = pyth.getTwapUpdateFee();
537537

538538
// Parse the TWAP updates
539539
PythStructs.TwapPriceFeed[] memory twapPriceFeeds = pyth
@@ -613,7 +613,7 @@ contract PythTest is Test, WormholeTestUtils, PythTestUtils {
613613
)
614614
);
615615

616-
uint updateFee = pyth.getUpdateFee(updateData);
616+
uint updateFee = pyth.getTwapUpdateFee();
617617

618618
vm.expectRevert(PythErrors.InvalidTwapUpdateDataSet.selector);
619619
pyth.parseTwapPriceFeedUpdates{value: updateFee}(updateData, priceIds);
@@ -664,7 +664,7 @@ contract PythTest is Test, WormholeTestUtils, PythTestUtils {
664664
)
665665
);
666666

667-
uint updateFee = pyth.getUpdateFee(updateData);
667+
uint updateFee = pyth.getTwapUpdateFee();
668668

669669
vm.expectRevert(PythErrors.InvalidTwapUpdateDataSet.selector);
670670
pyth.parseTwapPriceFeedUpdates{value: updateFee}(updateData, priceIds);
@@ -711,7 +711,7 @@ contract PythTest is Test, WormholeTestUtils, PythTestUtils {
711711
)
712712
);
713713

714-
uint updateFee = pyth.getUpdateFee(updateData);
714+
uint updateFee = pyth.getTwapUpdateFee();
715715

716716
vm.expectRevert(PythErrors.InvalidTwapUpdateDataSet.selector);
717717
pyth.parseTwapPriceFeedUpdates{value: updateFee}(updateData, priceIds);
@@ -758,7 +758,7 @@ contract PythTest is Test, WormholeTestUtils, PythTestUtils {
758758
)
759759
);
760760

761-
uint updateFee = pyth.getUpdateFee(updateData);
761+
uint updateFee = pyth.getTwapUpdateFee();
762762

763763
vm.expectRevert(PythErrors.InvalidTwapUpdateData.selector);
764764
pyth.parseTwapPriceFeedUpdates{value: updateFee}(updateData, priceIds);
@@ -800,7 +800,7 @@ contract PythTest is Test, WormholeTestUtils, PythTestUtils {
800800
)
801801
);
802802

803-
uint updateFee = pyth.getUpdateFee(updateData);
803+
uint updateFee = pyth.getTwapUpdateFee();
804804

805805
vm.expectRevert(PythErrors.InsufficientFee.selector);
806806
pyth.parseTwapPriceFeedUpdates{value: updateFee - 1}(
@@ -849,7 +849,7 @@ contract PythTest is Test, WormholeTestUtils, PythTestUtils {
849849
config
850850
);
851851

852-
uint updateFee = pyth.getUpdateFee(updateData);
852+
uint updateFee = pyth.getTwapUpdateFee();
853853

854854
// Parse the TWAP updates
855855
PythStructs.TwapPriceFeed[] memory twapPriceFeeds = pyth
@@ -924,7 +924,7 @@ contract PythTest is Test, WormholeTestUtils, PythTestUtils {
924924
config
925925
);
926926

927-
uint updateFee = pyth.getUpdateFee(updateData);
927+
uint updateFee = pyth.getTwapUpdateFee();
928928

929929
vm.expectRevert(PythErrors.InvalidUpdateData.selector);
930930
pyth.parseTwapPriceFeedUpdates{value: updateFee}(updateData, priceIds);
@@ -976,7 +976,7 @@ contract PythTest is Test, WormholeTestUtils, PythTestUtils {
976976
config
977977
);
978978

979-
uint updateFee = pyth.getUpdateFee(updateData);
979+
uint updateFee = pyth.getTwapUpdateFee();
980980

981981
// Should revert because one of the requested price IDs is not found in the updates
982982
vm.expectRevert(PythErrors.PriceFeedNotFoundWithinRange.selector);

target_chains/ethereum/sdk/solidity/IPyth.sol

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ interface IPyth is IPythEvents {
9494
bytes[] calldata updateData
9595
) external view returns (uint feeAmount);
9696

97+
/// @notice Returns the required fee to update a TWAP price.
98+
/// @return feeAmount The required fee in Wei.
99+
function getTwapUpdateFee() external view returns (uint feeAmount);
100+
97101
/// @notice Parse `updateData` and return price feeds of the given `priceIds` if they are all published
98102
/// within `minPublishTime` and `maxPublishTime`.
99103
///

target_chains/ethereum/sdk/solidity/MockPyth.sol

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ contract MockPyth is AbstractPyth {
8080
return singleUpdateFeeInWei * updateData.length;
8181
}
8282

83+
function getTwapUpdateFee() public view override returns (uint feeAmount) {
84+
return singleUpdateFeeInWei;
85+
}
86+
8387
function parsePriceFeedUpdatesInternal(
8488
bytes[] calldata updateData,
8589
bytes32[] calldata priceIds,

target_chains/ethereum/sdk/solidity/abis/AbstractPyth.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,19 @@
344344
"stateMutability": "view",
345345
"type": "function"
346346
},
347+
{
348+
"inputs": [],
349+
"name": "getTwapUpdateFee",
350+
"outputs": [
351+
{
352+
"internalType": "uint256",
353+
"name": "feeAmount",
354+
"type": "uint256"
355+
}
356+
],
357+
"stateMutability": "view",
358+
"type": "function"
359+
},
347360
{
348361
"inputs": [
349362
{

target_chains/ethereum/sdk/solidity/abis/IPyth.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,19 @@
247247
"stateMutability": "view",
248248
"type": "function"
249249
},
250+
{
251+
"inputs": [],
252+
"name": "getTwapUpdateFee",
253+
"outputs": [
254+
{
255+
"internalType": "uint256",
256+
"name": "feeAmount",
257+
"type": "uint256"
258+
}
259+
],
260+
"stateMutability": "view",
261+
"type": "function"
262+
},
250263
{
251264
"inputs": [
252265
{

target_chains/ethereum/sdk/solidity/abis/MockPyth.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,19 @@
483483
"stateMutability": "view",
484484
"type": "function"
485485
},
486+
{
487+
"inputs": [],
488+
"name": "getTwapUpdateFee",
489+
"outputs": [
490+
{
491+
"internalType": "uint256",
492+
"name": "feeAmount",
493+
"type": "uint256"
494+
}
495+
],
496+
"stateMutability": "view",
497+
"type": "function"
498+
},
486499
{
487500
"inputs": [
488501
{

0 commit comments

Comments
 (0)