Pyth.sol 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. abstract contract Pyth is PythGetters, PythSetters, AbstractPyth {
  11. using BytesLib for bytes;
  12. function _initialize(
  13. address wormhole,
  14. uint16 pyth2WormholeChainId,
  15. bytes32 pyth2WormholeEmitter
  16. ) internal {
  17. setWormhole(wormhole);
  18. setPyth2WormholeChainId(pyth2WormholeChainId);
  19. setPyth2WormholeEmitter(pyth2WormholeEmitter);
  20. }
  21. function updatePriceBatchFromVm(bytes calldata encodedVm) private 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 data source chain/emitter ID");
  25. PythInternalStructs.BatchPriceAttestation memory batch = parseBatchPriceAttestation(vm.payload);
  26. uint freshPrices = 0;
  27. for (uint i = 0; i < batch.attestations.length; i++) {
  28. PythInternalStructs.PriceAttestation memory attestation = batch.attestations[i];
  29. PythInternalStructs.PriceInfo memory newPriceInfo = createNewPriceInfo(attestation);
  30. PythInternalStructs.PriceInfo memory latestPrice = latestPriceInfo(attestation.priceId);
  31. bool fresh = false;
  32. if(newPriceInfo.priceFeed.price.publishTime > latestPrice.priceFeed.price.publishTime) {
  33. freshPrices += 1;
  34. fresh = true;
  35. setLatestPriceInfo(attestation.priceId, newPriceInfo);
  36. }
  37. emit PriceFeedUpdate(attestation.priceId, fresh, vm.emitterChainId, vm.sequence, latestPrice.priceFeed.price.publishTime,
  38. newPriceInfo.priceFeed.price.publishTime, newPriceInfo.priceFeed.price.price, newPriceInfo.priceFeed.price.conf);
  39. }
  40. emit BatchPriceFeedUpdate(vm.emitterChainId, vm.sequence, batch.attestations.length, freshPrices);
  41. return batch;
  42. }
  43. function updatePriceFeeds(bytes[] calldata updateData) public override payable {
  44. uint requiredFee = getUpdateFee(updateData);
  45. require(msg.value >= requiredFee, "insufficient paid fee amount");
  46. for(uint i = 0; i < updateData.length; i++) {
  47. updatePriceBatchFromVm(updateData[i]);
  48. }
  49. emit UpdatePriceFeeds(msg.sender, updateData.length, requiredFee);
  50. }
  51. /// This method is deprecated, please use the `getUpdateFee(bytes[])` instead.
  52. function getUpdateFee(uint updateDataSize) public view returns (uint feeAmount) {
  53. return singleUpdateFeeInWei() * updateDataSize;
  54. }
  55. function getUpdateFee(bytes[] calldata updateData) public override view returns (uint feeAmount) {
  56. return singleUpdateFeeInWei() * updateData.length;
  57. }
  58. function createNewPriceInfo(PythInternalStructs.PriceAttestation memory pa) private view returns (PythInternalStructs.PriceInfo memory info) {
  59. info.attestationTime = pa.attestationTime;
  60. info.arrivalTime = block.timestamp;
  61. info.arrivalBlock = block.number;
  62. info.priceFeed.id = pa.priceId;
  63. PythInternalStructs.PriceAttestationStatus status = PythInternalStructs.PriceAttestationStatus(pa.status);
  64. if (status == PythInternalStructs.PriceAttestationStatus.TRADING) {
  65. info.priceFeed.price.price = pa.price;
  66. info.priceFeed.price.conf = pa.conf;
  67. info.priceFeed.price.publishTime = pa.publishTime;
  68. info.priceFeed.emaPrice.publishTime = pa.publishTime;
  69. } else {
  70. info.priceFeed.price.price = pa.prevPrice;
  71. info.priceFeed.price.conf = pa.prevConf;
  72. info.priceFeed.price.publishTime = pa.prevPublishTime;
  73. // The EMA is last updated when the aggregate had trading status,
  74. // so, we use prev_publish_time (the time when the aggregate last had trading status).
  75. info.priceFeed.emaPrice.publishTime = pa.prevPublishTime;
  76. }
  77. info.priceFeed.price.expo = pa.expo;
  78. info.priceFeed.emaPrice.price = pa.emaPrice;
  79. info.priceFeed.emaPrice.conf = pa.emaConf;
  80. info.priceFeed.emaPrice.expo = pa.expo;
  81. return info;
  82. }
  83. function verifyPythVM(IWormhole.VM memory vm) private view returns (bool valid) {
  84. return isValidDataSource(vm.emitterChainId, vm.emitterAddress);
  85. }
  86. function parseBatchPriceAttestation(bytes memory encoded) public pure returns (PythInternalStructs.BatchPriceAttestation memory bpa) {
  87. uint index = 0;
  88. // Check header
  89. bpa.header.magic = encoded.toUint32(index);
  90. index += 4;
  91. require(bpa.header.magic == 0x50325748, "invalid magic value");
  92. bpa.header.versionMajor = encoded.toUint16(index);
  93. index += 2;
  94. require(bpa.header.versionMajor == 3, "invalid version major, expected 3");
  95. bpa.header.versionMinor = encoded.toUint16(index);
  96. index += 2;
  97. require(bpa.header.versionMinor >= 0, "invalid version minor, expected 0 or more");
  98. bpa.header.hdrSize = encoded.toUint16(index);
  99. index += 2;
  100. // NOTE(2022-04-19): Currently, only payloadId comes after
  101. // hdrSize. Future extra header fields must be read using a
  102. // separate offset to respect hdrSize, i.e.:
  103. //
  104. // uint hdrIndex = 0;
  105. // bpa.header.payloadId = encoded.toUint8(index + hdrIndex);
  106. // hdrIndex += 1;
  107. //
  108. // bpa.header.someNewField = encoded.toUint32(index + hdrIndex);
  109. // hdrIndex += 4;
  110. //
  111. // // Skip remaining unknown header bytes
  112. // index += bpa.header.hdrSize;
  113. bpa.header.payloadId = encoded.toUint8(index);
  114. // Skip remaining unknown header bytes
  115. index += bpa.header.hdrSize;
  116. // Payload ID of 2 required for batch headerBa
  117. require(bpa.header.payloadId == 2, "invalid payload ID, expected 2 for BatchPriceAttestation");
  118. // Parse the number of attestations
  119. bpa.nAttestations = encoded.toUint16(index);
  120. index += 2;
  121. // Parse the attestation size
  122. bpa.attestationSize = encoded.toUint16(index);
  123. index += 2;
  124. require(encoded.length == (index + (bpa.attestationSize * bpa.nAttestations)), "invalid BatchPriceAttestation size");
  125. bpa.attestations = new PythInternalStructs.PriceAttestation[](bpa.nAttestations);
  126. // Deserialize each attestation
  127. for (uint j=0; j < bpa.nAttestations; j++) {
  128. // NOTE: We don't advance the global index immediately.
  129. // attestationIndex is an attestation-local offset used
  130. // for readability and easier debugging.
  131. uint attestationIndex = 0;
  132. // Attestation
  133. bpa.attestations[j].productId = encoded.toBytes32(index + attestationIndex);
  134. attestationIndex += 32;
  135. bpa.attestations[j].priceId = encoded.toBytes32(index + attestationIndex);
  136. attestationIndex += 32;
  137. bpa.attestations[j].price = int64(encoded.toUint64(index + attestationIndex));
  138. attestationIndex += 8;
  139. bpa.attestations[j].conf = encoded.toUint64(index + attestationIndex);
  140. attestationIndex += 8;
  141. bpa.attestations[j].expo = int32(encoded.toUint32(index + attestationIndex));
  142. attestationIndex += 4;
  143. bpa.attestations[j].emaPrice = int64(encoded.toUint64(index + attestationIndex));
  144. attestationIndex += 8;
  145. bpa.attestations[j].emaConf = encoded.toUint64(index + attestationIndex);
  146. attestationIndex += 8;
  147. bpa.attestations[j].status = encoded.toUint8(index + attestationIndex);
  148. attestationIndex += 1;
  149. bpa.attestations[j].numPublishers = encoded.toUint32(index + attestationIndex);
  150. attestationIndex += 4;
  151. bpa.attestations[j].maxNumPublishers = encoded.toUint32(index + attestationIndex);
  152. attestationIndex += 4;
  153. bpa.attestations[j].attestationTime = encoded.toUint64(index + attestationIndex);
  154. attestationIndex += 8;
  155. bpa.attestations[j].publishTime = encoded.toUint64(index + attestationIndex);
  156. attestationIndex += 8;
  157. bpa.attestations[j].prevPublishTime = encoded.toUint64(index + attestationIndex);
  158. attestationIndex += 8;
  159. bpa.attestations[j].prevPrice = int64(encoded.toUint64(index + attestationIndex));
  160. attestationIndex += 8;
  161. bpa.attestations[j].prevConf = encoded.toUint64(index + attestationIndex);
  162. attestationIndex += 8;
  163. require(attestationIndex <= bpa.attestationSize, "INTERNAL: Consumed more than `attestationSize` bytes");
  164. // Respect specified attestation size for forward-compat
  165. index += bpa.attestationSize;
  166. }
  167. }
  168. function queryPriceFeed(bytes32 id) public view override returns (PythStructs.PriceFeed memory priceFeed){
  169. // Look up the latest price info for the given ID
  170. PythInternalStructs.PriceInfo memory info = latestPriceInfo(id);
  171. require(info.priceFeed.id != 0, "price feed for the given id is not pushed or does not exist");
  172. return info.priceFeed;
  173. }
  174. function priceFeedExists(bytes32 id) public override view returns (bool) {
  175. PythInternalStructs.PriceInfo memory info = latestPriceInfo(id);
  176. return (info.priceFeed.id != 0);
  177. }
  178. function getValidTimePeriod() public override view returns (uint) {
  179. return validTimePeriodSeconds();
  180. }
  181. function version() public pure returns (string memory) {
  182. return "1.1.0";
  183. }
  184. }