[eth] Gas improvement: Optimize getPrice and getEmaPrice (#389)
* Optimize getPrice and getEmaPrice
This commit is contained in:
parent
b1afaacabf
commit
bb06fdb831
|
@ -235,6 +235,38 @@ abstract contract Pyth is PythGetters, PythSetters, AbstractPyth {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This is an overwrite of the same method in AbstractPyth.sol
|
||||||
|
// to be more gas efficient. It cannot move to PythGetters as it
|
||||||
|
// is overwriting the interface. Even indirect calling of a similar
|
||||||
|
// method from PythGetter has some gas overhead.
|
||||||
|
function getPriceUnsafe(
|
||||||
|
bytes32 id
|
||||||
|
) public view override returns (PythStructs.Price memory price) {
|
||||||
|
PythInternalStructs.PriceInfo storage info = _state.latestPriceInfo[id];
|
||||||
|
price.publishTime = info.publishTime;
|
||||||
|
price.expo = info.expo;
|
||||||
|
price.price = info.price;
|
||||||
|
price.conf = info.conf;
|
||||||
|
|
||||||
|
require(price.publishTime != 0, "price feed for the given id is not pushed or does not exist");
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is an overwrite of the same method in AbstractPyth.sol
|
||||||
|
// to be more gas efficient. It cannot move to PythGetters as it
|
||||||
|
// is overwriting the interface. Even indirect calling of a similar
|
||||||
|
// method from PythGetter has some gas overhead.
|
||||||
|
function getEmaPriceUnsafe(
|
||||||
|
bytes32 id
|
||||||
|
) public view override returns (PythStructs.Price memory price) {
|
||||||
|
PythInternalStructs.PriceInfo storage info = _state.latestPriceInfo[id];
|
||||||
|
price.publishTime = info.publishTime;
|
||||||
|
price.expo = info.expo;
|
||||||
|
price.price = info.emaPrice;
|
||||||
|
price.conf = info.emaConf;
|
||||||
|
|
||||||
|
require(price.publishTime != 0, "price feed for the given id is not pushed or does not exist");
|
||||||
|
}
|
||||||
|
|
||||||
function parsePriceFeedUpdates(
|
function parsePriceFeedUpdates(
|
||||||
bytes[] calldata updateData,
|
bytes[] calldata updateData,
|
||||||
bytes32[] calldata priceIds,
|
bytes32[] calldata priceIds,
|
||||||
|
|
|
@ -130,6 +130,15 @@ contract GasBenchmark is Test, WormholeTestUtils, PythTestUtils {
|
||||||
pyth.getPrice(priceIds[0]);
|
pyth.getPrice(priceIds[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function testBenchmarkGetEmaPrice() public {
|
||||||
|
// Set the block timestamp to 0. As prices have < 10 timestamp and staleness
|
||||||
|
// is set to 60 seconds, the getPrice should work as expected.
|
||||||
|
vm.warp(0);
|
||||||
|
|
||||||
|
pyth.getEmaPrice(priceIds[0]);
|
||||||
|
}
|
||||||
|
|
||||||
function testBenchmarkGetUpdateFee() public view {
|
function testBenchmarkGetUpdateFee() public view {
|
||||||
pyth.getUpdateFee(freshPricesUpdateData);
|
pyth.getUpdateFee(freshPricesUpdateData);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue