Pyth.sol 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // contracts/Bridge.sol
  2. // SPDX-License-Identifier: Apache 2
  3. pragma solidity ^0.8.0;
  4. import "../libraries/external/BytesLib.sol";
  5. import "./PythGetters.sol";
  6. import "./PythSetters.sol";
  7. import "./PythStructs.sol";
  8. import "./PythGovernance.sol";
  9. contract Pyth is PythGovernance {
  10. using BytesLib for bytes;
  11. function attestPriceBatch(bytes memory encodedVm) public returns (PythStructs.BatchPriceAttestation memory bpa) {
  12. (IWormhole.VM memory vm, bool valid, string memory reason) = wormhole().parseAndVerifyVM(encodedVm);
  13. require(valid, reason);
  14. require(verifyPythVM(vm), "invalid emitter");
  15. PythStructs.BatchPriceAttestation memory batch = parseBatchPriceAttestation(vm.payload);
  16. for (uint i = 0; i < batch.attestations.length; i++) {
  17. PythStructs.PriceAttestation memory attestation = batch.attestations[i];
  18. PythStructs.PriceInfo memory latestPrice = latestPriceInfo(attestation.priceId);
  19. if(attestation.timestamp > latestPrice.attestation_time) {
  20. setLatestPriceInfo(attestation.priceId, newPriceInfo(attestation));
  21. }
  22. }
  23. return batch;
  24. }
  25. function newPriceInfo(PythStructs.PriceAttestation memory pa) private view returns (PythStructs.PriceInfo memory info) {
  26. info.attestation_time = pa.timestamp;
  27. info.arrival_time = block.timestamp;
  28. info.arrival_block = block.number;
  29. info.price.id = pa.priceId;
  30. info.price.price = pa.price;
  31. info.price.conf = pa.confidenceInterval;
  32. info.price.status = PythSDK.PriceStatus(pa.status);
  33. info.price.expo = pa.exponent;
  34. info.price.emaPrice = pa.emaPrice.value;
  35. info.price.emaConf = uint64(pa.emaConf.value);
  36. info.price.productId = pa.productId;
  37. // These aren't sent in the wire format yet
  38. info.price.numPublishers = 0;
  39. info.price.maxNumPublishers = 0;
  40. return info;
  41. }
  42. function verifyPythVM(IWormhole.VM memory vm) public view returns (bool valid) {
  43. if (vm.emitterChainId != pyth2WormholeChainId()) {
  44. return false;
  45. }
  46. if (vm.emitterAddress != pyth2WormholeEmitter()) {
  47. return false;
  48. }
  49. return true;
  50. }
  51. function parseBatchPriceAttestation(bytes memory encoded) public pure returns (PythStructs.BatchPriceAttestation memory bpa) {
  52. uint index = 0;
  53. // Check header
  54. bpa.header.magic = encoded.toUint32(index);
  55. index += 4;
  56. require(bpa.header.magic == 0x50325748, "invalid magic value");
  57. bpa.header.version = encoded.toUint16(index);
  58. index += 2;
  59. require(bpa.header.version == 2, "invalid version");
  60. bpa.header.payloadId = encoded.toUint8(index);
  61. index += 1;
  62. // Payload ID of 2 required for batch header
  63. require(bpa.header.payloadId == 2, "invalid payload ID");
  64. // Parse the number of attestations
  65. bpa.nAttestations = encoded.toUint16(index);
  66. index += 2;
  67. // Parse the attestation size
  68. bpa.attestationSize = encoded.toUint16(index);
  69. index += 2;
  70. require(encoded.length == (index + (bpa.attestationSize * bpa.nAttestations)), "invalid BatchPriceAttestation size");
  71. bpa.attestations = new PythStructs.PriceAttestation[](bpa.nAttestations);
  72. // Deserialize each attestation
  73. for (uint j=0; j < bpa.nAttestations; j++) {
  74. // Header
  75. bpa.attestations[j].header.magic = encoded.toUint32(index);
  76. index += 4;
  77. require(bpa.attestations[j].header.magic == 0x50325748, "invalid magic value");
  78. bpa.attestations[j].header.version = encoded.toUint16(index);
  79. index += 2;
  80. require(bpa.attestations[j].header.version == 2, "invalid version");
  81. bpa.attestations[j].header.payloadId = encoded.toUint8(index);
  82. index += 1;
  83. // Payload ID of 1 required for individual attestation
  84. require(bpa.attestations[j].header.payloadId == 1, "invalid payload ID");
  85. // Attestation
  86. bpa.attestations[j].productId = encoded.toBytes32(index);
  87. index += 32;
  88. bpa.attestations[j].priceId = encoded.toBytes32(index);
  89. index += 32;
  90. bpa.attestations[j].priceType = encoded.toUint8(index);
  91. index += 1;
  92. bpa.attestations[j].price = int64(encoded.toUint64(index));
  93. index += 8;
  94. bpa.attestations[j].exponent = int32(encoded.toUint32(index));
  95. index += 4;
  96. bpa.attestations[j].emaPrice.value = int64(encoded.toUint64(index));
  97. index += 8;
  98. bpa.attestations[j].emaPrice.numerator = int64(encoded.toUint64(index));
  99. index += 8;
  100. bpa.attestations[j].emaPrice.denominator = int64(encoded.toUint64(index));
  101. index += 8;
  102. bpa.attestations[j].emaConf.value = int64(encoded.toUint64(index));
  103. index += 8;
  104. bpa.attestations[j].emaConf.numerator = int64(encoded.toUint64(index));
  105. index += 8;
  106. bpa.attestations[j].emaConf.denominator = int64(encoded.toUint64(index));
  107. index += 8;
  108. bpa.attestations[j].confidenceInterval = encoded.toUint64(index);
  109. index += 8;
  110. bpa.attestations[j].status = encoded.toUint8(index);
  111. index += 1;
  112. bpa.attestations[j].corpAct = encoded.toUint8(index);
  113. index += 1;
  114. bpa.attestations[j].timestamp = encoded.toUint64(index);
  115. index += 8;
  116. }
  117. }
  118. }