Просмотр исходного кода

feat(pyth): add getTwapUpdateFee function to calculate TWAP update fees

Daniel Chew 6 месяцев назад
Родитель
Сommit
4891ad25f9

+ 8 - 1
target_chains/ethereum/contracts/contracts/pyth/Pyth.sol

@@ -120,6 +120,13 @@ abstract contract Pyth is
         return getTotalFee(totalNumUpdates);
     }
 
+    function getTwapUpdateFee() public view override returns (uint feeAmount) {
+        // In the accumulator update data a single update can contain
+        // up to 255 messages and we charge a singleUpdateFee per each
+        // message
+        return singleUpdateFeeInWei() + transactionFeeInWei();
+    }
+
     // This is an overwrite of the same method in AbstractPyth.sol
     // to be more gas efficient.
     function updatePriceFeedsIfNecessary(
@@ -454,7 +461,7 @@ abstract contract Pyth is
             revert PythErrors.InvalidUpdateData();
         }
 
-        uint requiredFee = getUpdateFee(updateData);
+        uint requiredFee = getTwapUpdateFee();
         if (msg.value < requiredFee) revert PythErrors.InsufficientFee();
 
         // Process start update data

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

@@ -533,7 +533,7 @@ contract PythTest is Test, WormholeTestUtils, PythTestUtils {
             )
         );
 
-        uint updateFee = pyth.getUpdateFee(updateData);
+        uint updateFee = pyth.getTwapUpdateFee();
 
         // Parse the TWAP updates
         PythStructs.TwapPriceFeed[] memory twapPriceFeeds = pyth
@@ -613,7 +613,7 @@ contract PythTest is Test, WormholeTestUtils, PythTestUtils {
             )
         );
 
-        uint updateFee = pyth.getUpdateFee(updateData);
+        uint updateFee = pyth.getTwapUpdateFee();
 
         vm.expectRevert(PythErrors.InvalidTwapUpdateDataSet.selector);
         pyth.parseTwapPriceFeedUpdates{value: updateFee}(updateData, priceIds);
@@ -664,7 +664,7 @@ contract PythTest is Test, WormholeTestUtils, PythTestUtils {
             )
         );
 
-        uint updateFee = pyth.getUpdateFee(updateData);
+        uint updateFee = pyth.getTwapUpdateFee();
 
         vm.expectRevert(PythErrors.InvalidTwapUpdateDataSet.selector);
         pyth.parseTwapPriceFeedUpdates{value: updateFee}(updateData, priceIds);
@@ -711,7 +711,7 @@ contract PythTest is Test, WormholeTestUtils, PythTestUtils {
             )
         );
 
-        uint updateFee = pyth.getUpdateFee(updateData);
+        uint updateFee = pyth.getTwapUpdateFee();
 
         vm.expectRevert(PythErrors.InvalidTwapUpdateDataSet.selector);
         pyth.parseTwapPriceFeedUpdates{value: updateFee}(updateData, priceIds);
@@ -758,7 +758,7 @@ contract PythTest is Test, WormholeTestUtils, PythTestUtils {
             )
         );
 
-        uint updateFee = pyth.getUpdateFee(updateData);
+        uint updateFee = pyth.getTwapUpdateFee();
 
         vm.expectRevert(PythErrors.InvalidTwapUpdateData.selector);
         pyth.parseTwapPriceFeedUpdates{value: updateFee}(updateData, priceIds);
@@ -800,7 +800,7 @@ contract PythTest is Test, WormholeTestUtils, PythTestUtils {
             )
         );
 
-        uint updateFee = pyth.getUpdateFee(updateData);
+        uint updateFee = pyth.getTwapUpdateFee();
 
         vm.expectRevert(PythErrors.InsufficientFee.selector);
         pyth.parseTwapPriceFeedUpdates{value: updateFee - 1}(
@@ -849,7 +849,7 @@ contract PythTest is Test, WormholeTestUtils, PythTestUtils {
             config
         );
 
-        uint updateFee = pyth.getUpdateFee(updateData);
+        uint updateFee = pyth.getTwapUpdateFee();
 
         // Parse the TWAP updates
         PythStructs.TwapPriceFeed[] memory twapPriceFeeds = pyth
@@ -924,7 +924,7 @@ contract PythTest is Test, WormholeTestUtils, PythTestUtils {
             config
         );
 
-        uint updateFee = pyth.getUpdateFee(updateData);
+        uint updateFee = pyth.getTwapUpdateFee();
 
         vm.expectRevert(PythErrors.InvalidUpdateData.selector);
         pyth.parseTwapPriceFeedUpdates{value: updateFee}(updateData, priceIds);
@@ -976,7 +976,7 @@ contract PythTest is Test, WormholeTestUtils, PythTestUtils {
             config
         );
 
-        uint updateFee = pyth.getUpdateFee(updateData);
+        uint updateFee = pyth.getTwapUpdateFee();
 
         // Should revert because one of the requested price IDs is not found in the updates
         vm.expectRevert(PythErrors.PriceFeedNotFoundWithinRange.selector);

+ 4 - 0
target_chains/ethereum/sdk/solidity/IPyth.sol

@@ -94,6 +94,10 @@ interface IPyth is IPythEvents {
         bytes[] calldata updateData
     ) external view returns (uint feeAmount);
 
+    /// @notice Returns the required fee to update a TWAP price.
+    /// @return feeAmount The required fee in Wei.
+    function getTwapUpdateFee() external view returns (uint feeAmount);
+
     /// @notice Parse `updateData` and return price feeds of the given `priceIds` if they are all published
     /// within `minPublishTime` and `maxPublishTime`.
     ///

+ 4 - 0
target_chains/ethereum/sdk/solidity/MockPyth.sol

@@ -80,6 +80,10 @@ contract MockPyth is AbstractPyth {
         return singleUpdateFeeInWei * updateData.length;
     }
 
+    function getTwapUpdateFee() public view override returns (uint feeAmount) {
+        return singleUpdateFeeInWei;
+    }
+
     function parsePriceFeedUpdatesInternal(
         bytes[] calldata updateData,
         bytes32[] calldata priceIds,

+ 13 - 0
target_chains/ethereum/sdk/solidity/abis/AbstractPyth.json

@@ -344,6 +344,19 @@
     "stateMutability": "view",
     "type": "function"
   },
+  {
+    "inputs": [],
+    "name": "getTwapUpdateFee",
+    "outputs": [
+      {
+        "internalType": "uint256",
+        "name": "feeAmount",
+        "type": "uint256"
+      }
+    ],
+    "stateMutability": "view",
+    "type": "function"
+  },
   {
     "inputs": [
       {

+ 13 - 0
target_chains/ethereum/sdk/solidity/abis/IPyth.json

@@ -247,6 +247,19 @@
     "stateMutability": "view",
     "type": "function"
   },
+  {
+    "inputs": [],
+    "name": "getTwapUpdateFee",
+    "outputs": [
+      {
+        "internalType": "uint256",
+        "name": "feeAmount",
+        "type": "uint256"
+      }
+    ],
+    "stateMutability": "view",
+    "type": "function"
+  },
   {
     "inputs": [
       {

+ 13 - 0
target_chains/ethereum/sdk/solidity/abis/MockPyth.json

@@ -483,6 +483,19 @@
     "stateMutability": "view",
     "type": "function"
   },
+  {
+    "inputs": [],
+    "name": "getTwapUpdateFee",
+    "outputs": [
+      {
+        "internalType": "uint256",
+        "name": "feeAmount",
+        "type": "uint256"
+      }
+    ],
+    "stateMutability": "view",
+    "type": "function"
+  },
   {
     "inputs": [
       {