Skip to content
This repository was archived by the owner on May 6, 2025. It is now read-only.

Commit 45cee89

Browse files
authored
Abehjati/add fee to ipyth (#19)
* Add fee for updating prices * Accept only native currency. * Update events * Update names and comments According to Jayant comments * update abi * update docs * Make fee a fixed amount
1 parent 32aab4d commit 45cee89

File tree

3 files changed

+57
-8
lines changed

3 files changed

+57
-8
lines changed

IPyth.sol

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ interface IPyth {
2727
event BatchPriceFeedUpdate(int8 chainId, uint64 sequenceNumber, uint batchSize, uint freshPricesInBatch);
2828

2929
/// @dev Emitted when a call to `updatePriceFeeds` is processed successfully.
30-
/// @param sender Sender of this call (`msg.sender`).
30+
/// @param sender Sender of the call (`msg.sender`).
3131
/// @param batchCount Number of batches that this function processed.
32-
event UpdatePriceFeeds(address indexed sender, uint batchCount);
32+
/// @param fee Amount of paid fee for updating the prices.
33+
event UpdatePriceFeeds(address indexed sender, uint batchCount, uint fee);
3334

3435

3536
/// @notice Returns the current price and confidence interval.
@@ -50,8 +51,17 @@ interface IPyth {
5051
/// @return publishTime - the UNIX timestamp of when this price was computed.
5152
function getPrevPriceUnsafe(bytes32 id) external view returns (PythStructs.Price memory price, uint64 publishTime);
5253

53-
/// @notice Update price feeds with given update messages if they are more recent than the current stored prices.
54+
/// @notice Update price feeds with given update messages.
55+
/// This method requires the caller to pay a fee in wei; the required fee can be computed by calling
56+
/// `getUpdateFee` with the length of the `updateData` array.
57+
/// Prices will be updated if they are more recent than the current stored prices.
5458
/// The call will succeed even if the update is not the most recent.
55-
/// @dev Reverts if the updateData is invalid.
56-
function updatePriceFeeds(bytes[] memory updateData) external;
59+
/// @dev Reverts if the transferred fee is not sufficient or the updateData is invalid.
60+
/// @param updateData Array of price update data.
61+
function updatePriceFeeds(bytes[] memory updateData) external payable;
62+
63+
/// @notice Returns the required fee to update an array of price updates.
64+
/// @param updateDataSize Number of price updates.
65+
/// @return feeAmount The required fee in Wei.
66+
function getUpdateFee(uint updateDataSize) external view returns (uint feeAmount);
5767
}

IPythAbi.json

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@
9999
"internalType": "uint256",
100100
"name": "batchCount",
101101
"type": "uint256"
102+
},
103+
{
104+
"indexed": false,
105+
"internalType": "uint256",
106+
"name": "fee",
107+
"type": "uint256"
102108
}
103109
],
104110
"name": "UpdatePriceFeeds",
@@ -217,6 +223,25 @@
217223
"stateMutability": "view",
218224
"type": "function"
219225
},
226+
{
227+
"inputs": [
228+
{
229+
"internalType": "uint256",
230+
"name": "updateDataSize",
231+
"type": "uint256"
232+
}
233+
],
234+
"name": "getUpdateFee",
235+
"outputs": [
236+
{
237+
"internalType": "uint256",
238+
"name": "feeAmount",
239+
"type": "uint256"
240+
}
241+
],
242+
"stateMutability": "view",
243+
"type": "function"
244+
},
220245
{
221246
"inputs": [
222247
{
@@ -227,7 +252,7 @@
227252
],
228253
"name": "updatePriceFeeds",
229254
"outputs": [],
230-
"stateMutability": "nonpayable",
255+
"stateMutability": "payable",
231256
"type": "function"
232257
}
233258
]

MockPyth.sol

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,24 @@ contract MockPyth is AbstractPyth {
88
mapping(bytes32 => PythStructs.PriceFeed) priceFeeds;
99
uint64 sequenceNumber;
1010

11+
uint singleUpdateFeeInWei;
12+
13+
constructor(uint _singleUpdateFeeInWei) {
14+
singleUpdateFeeInWei = _singleUpdateFeeInWei;
15+
}
16+
1117
function queryPriceFeed(bytes32 id) public override view returns (PythStructs.PriceFeed memory priceFeed) {
1218
return priceFeeds[id];
1319
}
1420

1521
// Takes an array of encoded price feeds and stores them.
1622
// You can create this data either by calling createPriceFeedData or
1723
// by using web3.js or ethers abi utilities.
18-
function updatePriceFeeds(bytes[] memory updateData) public override {
24+
function updatePriceFeeds(bytes[] memory updateData) public override payable {
25+
uint requiredFee = getUpdateFee(updateData.length);
26+
require(msg.value >= requiredFee, "Insufficient paid fee amount");
27+
payable(msg.sender).transfer(msg.value - requiredFee);
28+
1929
uint freshPrices = 0;
2030

2131
// Chain ID is id of the source chain that the price update comes from. Since it is just a mock contract
@@ -46,7 +56,11 @@ contract MockPyth is AbstractPyth {
4656
sequenceNumber += 1;
4757

4858
// There is only 1 batch of prices
49-
emit UpdatePriceFeeds(msg.sender, 1);
59+
emit UpdatePriceFeeds(msg.sender, 1, requiredFee);
60+
}
61+
62+
function getUpdateFee(uint updateDataSize) public override view returns (uint feeAmount) {
63+
return singleUpdateFeeInWei * updateDataSize;
5064
}
5165

5266
function createPriceFeedUpdateData(

0 commit comments

Comments
 (0)