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

Abehjati/add fee to ipyth #19

Merged
merged 8 commits into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions IPyth.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ interface IPyth {
event BatchPriceFeedUpdate(int8 chainId, uint64 sequenceNumber, uint batchSize, uint freshPricesInBatch);

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


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

/// @notice Update price feeds with given update messages if they are more recent than the current stored prices.
/// @notice Update price feeds with given update messages.
/// This method requires the caller to pay a fee in wei; the required fee can be computed by calling
/// `getUpdateFee` with the length of the `updateData` array.
/// Prices will be updated if they are more recent than the current stored prices.
/// The call will succeed even if the update is not the most recent.
/// @dev Reverts if the updateData is invalid.
function updatePriceFeeds(bytes[] memory updateData) external;
/// @dev Reverts if the transferred fee is not sufficient or the updateData is invalid.
/// @param updateData Array of price update data.
function updatePriceFeeds(bytes[] memory updateData) external payable;

/// @notice Returns the required fee to update an array of price updates.
/// @param updateDataSize Number of price updates.
/// @return feeAmount The required fee in Wei.
function getUpdateFee(uint updateDataSize) external view returns (uint feeAmount);
}
27 changes: 26 additions & 1 deletion IPythAbi.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@
"internalType": "uint256",
"name": "batchCount",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "fee",
"type": "uint256"
}
],
"name": "UpdatePriceFeeds",
Expand Down Expand Up @@ -217,6 +223,25 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "updateDataSize",
"type": "uint256"
}
],
"name": "getUpdateFee",
"outputs": [
{
"internalType": "uint256",
"name": "feeAmount",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
Expand All @@ -227,7 +252,7 @@
],
"name": "updatePriceFeeds",
"outputs": [],
"stateMutability": "nonpayable",
"stateMutability": "payable",
"type": "function"
}
]
18 changes: 16 additions & 2 deletions MockPyth.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,24 @@ contract MockPyth is AbstractPyth {
mapping(bytes32 => PythStructs.PriceFeed) priceFeeds;
uint64 sequenceNumber;

uint singleUpdateFeeInWei;

constructor(uint _singleUpdateFeeInWei) {
singleUpdateFeeInWei = _singleUpdateFeeInWei;
}

function queryPriceFeed(bytes32 id) public override view returns (PythStructs.PriceFeed memory priceFeed) {
return priceFeeds[id];
}

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

uint freshPrices = 0;

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

// There is only 1 batch of prices
emit UpdatePriceFeeds(msg.sender, 1);
emit UpdatePriceFeeds(msg.sender, 1, requiredFee);
}

function getUpdateFee(uint updateDataSize) public override view returns (uint feeAmount) {
return singleUpdateFeeInWei * updateDataSize;
}

function createPriceFeedUpdateData(
Expand Down