Jelajahi Sumber

Cache price updates in standardised PriceInfo format

Tom Pointon 3 tahun lalu
induk
melakukan
5a73356bb0

+ 24 - 3
ethereum/contracts/pyth/Pyth.sol

@@ -21,15 +21,36 @@ contract Pyth is PythGovernance {
 
         PythStructs.PriceAttestation memory price = parsePriceAttestation(vm.payload);
 
-        PythStructs.PriceAttestation memory latestPrice = latestAttestation(price.productId, price.priceType);
+        PythStructs.PriceInfo memory latestPrice = latestPriceInfo(pa.priceId);
 
-        if(price.timestamp > latestPrice.timestamp) {
-            setLatestAttestation(price.productId, price.priceType, price);
+        if(price.timestamp > latestPrice.attestation_time) {
+            setLatestPriceInfo(price.priceId, newPriceInfo(price));
         }
 
         return price;
     }
 
+    
+    function newPriceInfo(PythStructs.PriceAttestation memory pa) private view returns (PythStructs.PriceInfo memory info) {
+        info.attestation_time = pa.timestamp;
+        info.arrival_time = block.timestamp;
+        info.arrival_block = block.number;
+        
+        info.price.id = pa.priceId;
+        info.price.price = pa.price;
+        info.price.conf = pa.confidenceInterval;
+        info.price.status = PythSDK.PriceStatus(pa.status);
+        info.price.expo = pa.exponent;
+        info.price.emaPrice = pa.emaPrice.value;
+        info.price.emaConf = uint64(pa.emaConf.value);
+        info.price.productId = pa.productId;
+
+        // These aren't sent in the wire format yet
+        info.price.numPublishers = 0;
+        info.price.maxNumPublishers = 0;
+        return info;
+    }
+
     function verifyPythVM(IWormhole.VM memory vm) public view returns (bool valid) {
         if (vm.emitterChainId != pyth2WormholeChainId()) {
             return false;

+ 2 - 2
ethereum/contracts/pyth/PythGetters.sol

@@ -40,7 +40,7 @@ contract PythGetters is PythState {
         return _state.provider.pyth2WormholeEmitter;
     }
 
-    function latestAttestation(bytes32 product, uint8 priceType) public view returns (PythStructs.PriceAttestation memory attestation){
-        return _state.latestAttestations[product][priceType];
+    function latestPriceInfo(bytes32 priceId) public view returns (PythStructs.PriceInfo memory info){
+        return _state.latestPriceInfo[priceId];
     }
 }

+ 2 - 2
ethereum/contracts/pyth/PythSetters.sol

@@ -38,7 +38,7 @@ contract PythSetters is PythState {
         _state.wormhole = payable(wh);
     }
 
-    function setLatestAttestation(bytes32 product, uint8 priceType, PythStructs.PriceAttestation memory attestation) internal {
-        _state.latestAttestations[product][priceType] = attestation;
+    function setLatestPriceInfo(bytes32 priceId, PythStructs.PriceInfo memory info) internal {
+        _state.latestPriceInfo[priceId] = info;
     }
 }

+ 3 - 3
ethereum/contracts/pyth/PythState.sol

@@ -27,9 +27,9 @@ contract PythStorage {
         // Mapping of initialized implementations
         mapping(address => bool) initializedImplementations;
 
-        // Mapping of cached price attestations
-        // productId => priceType => PriceAttestation
-        mapping(bytes32 => mapping(uint8 => PythStructs.PriceAttestation)) latestAttestations;
+        // Mapping of cached price information
+        // priceId => PriceInfo
+        mapping(bytes32 => PythStructs.PriceInfo) latestPriceInfo;
     }
 }