Pyth.sol 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // contracts/Bridge.sol
  2. // SPDX-License-Identifier: Apache 2
  3. pragma solidity ^0.8.0;
  4. import "../libraries/external/BytesLib.sol";
  5. import "@pythnetwork/pyth-sdk-solidity/AbstractPyth.sol";
  6. import "@pythnetwork/pyth-sdk-solidity/PythStructs.sol";
  7. import "./PythGetters.sol";
  8. import "./PythSetters.sol";
  9. import "./PythInternalStructs.sol";
  10. contract Pyth is PythGetters, PythSetters, AbstractPyth {
  11. using BytesLib for bytes;
  12. function initialize(
  13. address wormhole,
  14. uint16 pyth2WormholeChainId,
  15. bytes32 pyth2WormholeEmitter
  16. ) virtual public {
  17. setWormhole(wormhole);
  18. setPyth2WormholeChainId(pyth2WormholeChainId);
  19. setPyth2WormholeEmitter(pyth2WormholeEmitter);
  20. }
  21. function updatePriceBatchFromVm(bytes memory encodedVm) public returns (PythInternalStructs.BatchPriceAttestation memory bpa) {
  22. (IWormhole.VM memory vm, bool valid, string memory reason) = wormhole().parseAndVerifyVM(encodedVm);
  23. require(valid, reason);
  24. require(verifyPythVM(vm), "invalid emitter");
  25. PythInternalStructs.BatchPriceAttestation memory batch = parseBatchPriceAttestation(vm.payload);
  26. for (uint i = 0; i < batch.attestations.length; i++) {
  27. PythInternalStructs.PriceAttestation memory attestation = batch.attestations[i];
  28. PythInternalStructs.PriceInfo memory latestPrice = latestPriceInfo(attestation.priceId);
  29. if(attestation.timestamp > latestPrice.attestationTime) {
  30. setLatestPriceInfo(attestation.priceId, newPriceInfo(attestation));
  31. }
  32. }
  33. return batch;
  34. }
  35. function newPriceInfo(PythInternalStructs.PriceAttestation memory pa) private view returns (PythInternalStructs.PriceInfo memory info) {
  36. info.attestationTime = pa.timestamp;
  37. info.arrivalTime = block.timestamp;
  38. info.arrivalBlock = block.number;
  39. info.priceFeed.id = pa.priceId;
  40. info.priceFeed.price = pa.price;
  41. info.priceFeed.conf = pa.confidenceInterval;
  42. info.priceFeed.status = PythStructs.PriceStatus(pa.status);
  43. info.priceFeed.expo = pa.exponent;
  44. info.priceFeed.emaPrice = pa.emaPrice.value;
  45. info.priceFeed.emaConf = uint64(pa.emaConf.value);
  46. info.priceFeed.productId = pa.productId;
  47. // These aren't sent in the wire format yet
  48. info.priceFeed.numPublishers = 0;
  49. info.priceFeed.maxNumPublishers = 0;
  50. return info;
  51. }
  52. function verifyPythVM(IWormhole.VM memory vm) private view returns (bool valid) {
  53. if (vm.emitterChainId != pyth2WormholeChainId()) {
  54. return false;
  55. }
  56. if (vm.emitterAddress != pyth2WormholeEmitter()) {
  57. return false;
  58. }
  59. return true;
  60. }
  61. function parseBatchPriceAttestation(bytes memory encoded) public pure returns (PythInternalStructs.BatchPriceAttestation memory bpa) {
  62. uint index = 0;
  63. // Check header
  64. bpa.header.magic = encoded.toUint32(index);
  65. index += 4;
  66. require(bpa.header.magic == 0x50325748, "invalid magic value");
  67. bpa.header.version = encoded.toUint16(index);
  68. index += 2;
  69. require(bpa.header.version == 2, "invalid version");
  70. bpa.header.payloadId = encoded.toUint8(index);
  71. index += 1;
  72. // Payload ID of 2 required for batch header
  73. require(bpa.header.payloadId == 2, "invalid payload ID");
  74. // Parse the number of attestations
  75. bpa.nAttestations = encoded.toUint16(index);
  76. index += 2;
  77. // Parse the attestation size
  78. bpa.attestationSize = encoded.toUint16(index);
  79. index += 2;
  80. require(encoded.length == (index + (bpa.attestationSize * bpa.nAttestations)), "invalid BatchPriceAttestation size");
  81. bpa.attestations = new PythInternalStructs.PriceAttestation[](bpa.nAttestations);
  82. // Deserialize each attestation
  83. for (uint j=0; j < bpa.nAttestations; j++) {
  84. // NOTE: We don't advance the global index immediately.
  85. // attestationIndex is an attestation-local offset used
  86. // for readability and easier debugging.
  87. uint attestationIndex = 0;
  88. // Header
  89. bpa.attestations[j].header.magic = encoded.toUint32(index + attestationIndex);
  90. attestationIndex += 4;
  91. require(bpa.attestations[j].header.magic == 0x50325748, "invalid magic value");
  92. bpa.attestations[j].header.version = encoded.toUint16(index + attestationIndex);
  93. attestationIndex += 2;
  94. require(bpa.attestations[j].header.version == 2, "invalid version");
  95. bpa.attestations[j].header.payloadId = encoded.toUint8(index + attestationIndex);
  96. attestationIndex += 1;
  97. // Payload ID of 1 required for individual attestation
  98. require(bpa.attestations[j].header.payloadId == 1, "invalid payload ID");
  99. // Attestation
  100. bpa.attestations[j].productId = encoded.toBytes32(index + attestationIndex);
  101. attestationIndex += 32;
  102. bpa.attestations[j].priceId = encoded.toBytes32(index + attestationIndex);
  103. attestationIndex += 32;
  104. bpa.attestations[j].priceType = encoded.toUint8(index + attestationIndex);
  105. attestationIndex += 1;
  106. bpa.attestations[j].price = int64(encoded.toUint64(index + attestationIndex));
  107. attestationIndex += 8;
  108. bpa.attestations[j].exponent = int32(encoded.toUint32(index + attestationIndex));
  109. attestationIndex += 4;
  110. bpa.attestations[j].emaPrice.value = int64(encoded.toUint64(index + attestationIndex));
  111. attestationIndex += 8;
  112. bpa.attestations[j].emaPrice.numerator = int64(encoded.toUint64(index + attestationIndex));
  113. attestationIndex += 8;
  114. bpa.attestations[j].emaPrice.denominator = int64(encoded.toUint64(index + attestationIndex));
  115. attestationIndex += 8;
  116. bpa.attestations[j].emaConf.value = int64(encoded.toUint64(index + attestationIndex));
  117. attestationIndex += 8;
  118. bpa.attestations[j].emaConf.numerator = int64(encoded.toUint64(index + attestationIndex));
  119. attestationIndex += 8;
  120. bpa.attestations[j].emaConf.denominator = int64(encoded.toUint64(index + attestationIndex));
  121. attestationIndex += 8;
  122. bpa.attestations[j].confidenceInterval = encoded.toUint64(index + attestationIndex);
  123. attestationIndex += 8;
  124. bpa.attestations[j].status = encoded.toUint8(index + attestationIndex);
  125. attestationIndex += 1;
  126. bpa.attestations[j].corpAct = encoded.toUint8(index + attestationIndex);
  127. attestationIndex += 1;
  128. bpa.attestations[j].timestamp = encoded.toUint64(index + attestationIndex);
  129. attestationIndex += 8;
  130. bpa.attestations[j].num_publishers = encoded.toUint32(index + attestationIndex);
  131. attestationIndex += 4;
  132. bpa.attestations[j].max_num_publishers = encoded.toUint32(index + attestationIndex);
  133. attestationIndex += 4;
  134. bpa.attestations[j].publish_time = encoded.toUint64(index + attestationIndex);
  135. attestationIndex += 8;
  136. bpa.attestations[j].prev_publish_time = encoded.toUint64(index + attestationIndex);
  137. attestationIndex += 8;
  138. bpa.attestations[j].prev_price = int64(encoded.toUint64(index + attestationIndex));
  139. attestationIndex += 8;
  140. bpa.attestations[j].prev_conf = encoded.toUint64(index + attestationIndex);
  141. attestationIndex += 8;
  142. require(attestationIndex <= bpa.attestationSize, "INTERNAL: Consumed more than `attestationSize` bytes");
  143. // Respect specified attestation size for forward-compat
  144. index += bpa.attestationSize;
  145. }
  146. }
  147. /// Maximum acceptable time period before price is considered to be stale.
  148. ///
  149. /// This includes attestation delay which currently might up to a minute.
  150. uint private constant VALID_TIME_PERIOD_SECS = 180;
  151. function queryPriceFeed(bytes32 id) public view override returns (PythStructs.PriceFeed memory priceFeed){
  152. // Look up the latest price info for the given ID
  153. PythInternalStructs.PriceInfo memory info = latestPriceInfo(id);
  154. require(info.priceFeed.id != 0, "no price feed found for the given price id");
  155. // Check that there is not a significant difference between this chain's time
  156. // and the attestation time. This is a last-resort safety net, and this check
  157. // will be iterated on in the future.
  158. if (diff(block.timestamp, info.attestationTime) > VALID_TIME_PERIOD_SECS) {
  159. info.priceFeed.status = PythStructs.PriceStatus.UNKNOWN;
  160. }
  161. return info.priceFeed;
  162. }
  163. function diff(uint x, uint y) private pure returns (uint) {
  164. if (x > y) {
  165. return x - y;
  166. } else {
  167. return y - x;
  168. }
  169. }
  170. }