pyth.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. const jsonfile = require('jsonfile');
  2. const elliptic = require('elliptic');
  3. const BigNumber = require('bignumber.js');
  4. const PythSDK = artifacts.require("PythSDK");
  5. const Wormhole = artifacts.require("Wormhole");
  6. const PythDataBridge = artifacts.require("PythDataBridge");
  7. const PythImplementation = artifacts.require("PythImplementation");
  8. const MockPythImplementation = artifacts.require("MockPythImplementation");
  9. const testSigner1PK = "cfb12303a19cde580bb4dd771639b0d26bc68353645571a8cff516ab2ee113a0";
  10. const testSigner2PK = "892330666a850761e7370376430bb8c2aa1494072d3bfeaed0c4fa3d5a9135fe";
  11. const WormholeImplementationFullABI = jsonfile.readFileSync("build/contracts/Implementation.json").abi
  12. const P2WImplementationFullABI = jsonfile.readFileSync("build/contracts/PythImplementation.json").abi
  13. contract("Pyth", function () {
  14. const testSigner1 = web3.eth.accounts.privateKeyToAccount(testSigner1PK);
  15. const testSigner2 = web3.eth.accounts.privateKeyToAccount(testSigner2PK);
  16. const testChainId = "2";
  17. const testGovernanceChainId = "3";
  18. const testGovernanceContract = "0x0000000000000000000000000000000000000000000000000000000000000004";
  19. const testPyth2WormholeChainId = "1";
  20. const testPyth2WormholeEmitter = "0x71f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b";
  21. it("should be initialized with the correct signers and values", async function(){
  22. const initialized = new web3.eth.Contract(P2WImplementationFullABI, PythDataBridge.address);
  23. // chain id
  24. const chainId = await initialized.methods.chainId().call();
  25. assert.equal(chainId, testChainId);
  26. // governance
  27. const governanceChainId = await initialized.methods.governanceChainId().call();
  28. assert.equal(governanceChainId, testGovernanceChainId);
  29. const governanceContract = await initialized.methods.governanceContract().call();
  30. assert.equal(governanceContract, testGovernanceContract);
  31. // pyth2wormhole
  32. const pyth2wormChain = await initialized.methods.pyth2WormholeChainId().call();
  33. assert.equal(pyth2wormChain, testPyth2WormholeChainId);
  34. const pyth2wormEmitter = await initialized.methods.pyth2WormholeEmitter().call();
  35. assert.equal(pyth2wormEmitter, testPyth2WormholeEmitter);
  36. })
  37. const rawBatchPriceAttestation = "0x"+"503257480002020004009650325748000201c0e11df4c58a4e53f2bc059ba57a7c8f30ddada70b5bdc3753f90b824b64dd73c1902e05cdf03bc089a943d921f87ccd0e3e1b774b5660d037b9f428c0d3305e01000000000000071dfffffffb00000000000005f70000000132959bbd00000000c8bfed5f00000000000000030000000041c7b65b00000000c8bfed5f0000000000000003010000000000622f65f4503257480002017090c4ecf0309718d04c5a162c08aa4b78f533f688fa2f3ccd7be74c2a253a54fd4caca566fc44a9d6585420959d13897877c606477b3f0e7f247295b7275620010000000000000440fffffffb00000000000005fb000000015cfe8c9d00000000e3dbaa7f00000000000000020000000041c7c5bb00000000e3dbaa7f0000000000000007010000000000622f65f4503257480002012f064374f55cb2efbbef29329de3b652013a76261876c55a1caf3a489c721ccd8c5dd422900917e8e26316fe598e8f062058d390644e0e36d42c187298420ccd010000000000000609fffffffb00000000000005cd00000001492c19bd00000000dd92071f00000000000000020000000041c7d3fb00000000dd92071f0000000000000001010000000000622f65f45032574800020171ddabd1a2c1fb6d6c4707b245b7c0ab6af0ae7b96b2ff866954a0b71124aee517fbe895e5416ddb4d5af9d83c599ee2c4f94cb25e8597f9e5978bd63a7cdcb70100000000000007bcfffffffb00000000000005e2000000014db2995d00000000dd8f775f00000000000000020000000041c7df9b00000000dd8f775f0000000000000003010000000000622f65f4";
  38. it("should parse batch price attestation correctly", async function() {
  39. const initialized = new web3.eth.Contract(P2WImplementationFullABI, PythDataBridge.address);
  40. const magic = 1345476424;
  41. const version = 2;
  42. let parsed = await initialized.methods.parseBatchPriceAttestation(rawBatchPriceAttestation).call();
  43. // Check the header
  44. assert.equal(parsed.header.magic, magic);
  45. assert.equal(parsed.header.version, version);
  46. assert.equal(parsed.header.payloadId, 2);
  47. assert.equal(parsed.nAttestations, 4);
  48. assert.equal(parsed.attestationSize, 150);
  49. assert.equal(parsed.attestations.length, 4);
  50. // Attestation #1
  51. assert.equal(parsed.attestations[0].header.magic, magic);
  52. assert.equal(parsed.attestations[0].header.version, version);
  53. assert.equal(parsed.attestations[0].header.payloadId, 1);
  54. assert.equal(parsed.attestations[0].productId, "0xc0e11df4c58a4e53f2bc059ba57a7c8f30ddada70b5bdc3753f90b824b64dd73");
  55. assert.equal(parsed.attestations[0].priceId, "0xc1902e05cdf03bc089a943d921f87ccd0e3e1b774b5660d037b9f428c0d3305e");
  56. assert.equal(parsed.attestations[0].priceType, 1);
  57. assert.equal(parsed.attestations[0].price, 1821);
  58. assert.equal(parsed.attestations[0].exponent, -5);
  59. assert.equal(parsed.attestations[0].emaPrice.value, 1527);
  60. assert.equal(parsed.attestations[0].emaPrice.numerator, 5143632829);
  61. assert.equal(parsed.attestations[0].emaPrice.denominator, 3368021343);
  62. assert.equal(parsed.attestations[0].emaConf.value, 3);
  63. assert.equal(parsed.attestations[0].emaConf.numerator, 1103607387);
  64. assert.equal(parsed.attestations[0].emaConf.denominator, 3368021343);
  65. assert.equal(parsed.attestations[0].confidenceInterval, 3);
  66. assert.equal(parsed.attestations[0].status, 1);
  67. assert.equal(parsed.attestations[0].corpAct, 0);
  68. assert.equal(parsed.attestations[0].timestamp, 1647273460);
  69. // Attestation #2
  70. assert.equal(parsed.attestations[1].header.magic, magic);
  71. assert.equal(parsed.attestations[1].header.version, version);
  72. assert.equal(parsed.attestations[1].header.payloadId, 1);
  73. assert.equal(parsed.attestations[1].productId, "0x7090c4ecf0309718d04c5a162c08aa4b78f533f688fa2f3ccd7be74c2a253a54");
  74. assert.equal(parsed.attestations[1].priceId, "0xfd4caca566fc44a9d6585420959d13897877c606477b3f0e7f247295b7275620");
  75. assert.equal(parsed.attestations[1].priceType, 1);
  76. assert.equal(parsed.attestations[1].price, 1088);
  77. assert.equal(parsed.attestations[1].exponent, -5);
  78. assert.equal(parsed.attestations[1].emaPrice.value, 1531);
  79. assert.equal(parsed.attestations[1].emaPrice.numerator, 5855153309);
  80. assert.equal(parsed.attestations[1].emaPrice.denominator, 3822824063);
  81. assert.equal(parsed.attestations[1].emaConf.value, 2);
  82. assert.equal(parsed.attestations[1].emaConf.numerator, 1103611323);
  83. assert.equal(parsed.attestations[1].emaConf.denominator, 3822824063);
  84. assert.equal(parsed.attestations[1].confidenceInterval, 7);
  85. assert.equal(parsed.attestations[1].status, 1);
  86. assert.equal(parsed.attestations[1].corpAct, 0);
  87. assert.equal(parsed.attestations[1].timestamp, 1647273460);
  88. // Attestation #3
  89. assert.equal(parsed.attestations[2].header.magic, magic);
  90. assert.equal(parsed.attestations[2].header.version, version);
  91. assert.equal(parsed.attestations[2].header.payloadId, 1);
  92. assert.equal(parsed.attestations[2].productId, "0x2f064374f55cb2efbbef29329de3b652013a76261876c55a1caf3a489c721ccd");
  93. assert.equal(parsed.attestations[2].priceId, "0x8c5dd422900917e8e26316fe598e8f062058d390644e0e36d42c187298420ccd");
  94. assert.equal(parsed.attestations[2].priceType, 1);
  95. assert.equal(parsed.attestations[2].price, 1545);
  96. assert.equal(parsed.attestations[2].exponent, -5);
  97. assert.equal(parsed.attestations[2].emaPrice.value, 1485);
  98. assert.equal(parsed.attestations[2].emaPrice.numerator, 5522594237);
  99. assert.equal(parsed.attestations[2].emaPrice.denominator, 3717334815);
  100. assert.equal(parsed.attestations[2].emaConf.value, 2);
  101. assert.equal(parsed.attestations[2].emaConf.numerator, 1103614971);
  102. assert.equal(parsed.attestations[2].emaConf.denominator, 3717334815);
  103. assert.equal(parsed.attestations[2].confidenceInterval, 1);
  104. assert.equal(parsed.attestations[2].status, 1);
  105. assert.equal(parsed.attestations[2].corpAct, 0);
  106. assert.equal(parsed.attestations[2].timestamp, 1647273460);
  107. // Attestation #4
  108. assert.equal(parsed.attestations[3].header.magic, magic);
  109. assert.equal(parsed.attestations[3].header.version, version);
  110. assert.equal(parsed.attestations[3].header.payloadId, 1);
  111. assert.equal(parsed.attestations[3].productId, "0x71ddabd1a2c1fb6d6c4707b245b7c0ab6af0ae7b96b2ff866954a0b71124aee5");
  112. assert.equal(parsed.attestations[3].priceId, "0x17fbe895e5416ddb4d5af9d83c599ee2c4f94cb25e8597f9e5978bd63a7cdcb7");
  113. assert.equal(parsed.attestations[3].priceType, 1);
  114. assert.equal(parsed.attestations[3].price, 1980);
  115. assert.equal(parsed.attestations[3].exponent, -5);
  116. assert.equal(parsed.attestations[3].emaPrice.value, 1506);
  117. assert.equal(parsed.attestations[3].emaPrice.numerator, 5598517597);
  118. assert.equal(parsed.attestations[3].emaPrice.denominator, 3717166943);
  119. assert.equal(parsed.attestations[3].emaConf.value, 2);
  120. assert.equal(parsed.attestations[3].emaConf.numerator, 1103617947);
  121. assert.equal(parsed.attestations[3].emaConf.denominator, 3717166943);
  122. assert.equal(parsed.attestations[3].confidenceInterval, 3);
  123. assert.equal(parsed.attestations[3].status, 1);
  124. assert.equal(parsed.attestations[3].corpAct, 0);
  125. assert.equal(parsed.attestations[3].timestamp, 1647273460);
  126. })
  127. async function attest(contract, data) {
  128. const accounts = await web3.eth.getAccounts();
  129. const vm = await signAndEncodeVM(
  130. 1,
  131. 1,
  132. testPyth2WormholeChainId,
  133. testPyth2WormholeEmitter,
  134. 0,
  135. data,
  136. [
  137. testSigner1PK
  138. ],
  139. 0,
  140. 0
  141. );
  142. let result = await contract.methods.attestPriceBatch("0x"+vm).send({
  143. value : 0,
  144. from : accounts[0],
  145. gasLimit : 2000000
  146. });
  147. }
  148. it("should attest price updates over wormhole", async function() {
  149. const initialized = new web3.eth.Contract(P2WImplementationFullABI, PythDataBridge.address);
  150. await attest(initialized, rawBatchPriceAttestation);
  151. })
  152. it("should cache price updates", async function() {
  153. const initialized = new web3.eth.Contract(P2WImplementationFullABI, PythDataBridge.address);
  154. await attest(initialized, rawBatchPriceAttestation);
  155. let first = await initialized.methods.latestPriceInfo("0xc1902e05cdf03bc089a943d921f87ccd0e3e1b774b5660d037b9f428c0d3305e").call();
  156. assert.equal(first.price.id, "0xc1902e05cdf03bc089a943d921f87ccd0e3e1b774b5660d037b9f428c0d3305e");
  157. assert.equal(first.price.productId, "0xc0e11df4c58a4e53f2bc059ba57a7c8f30ddada70b5bdc3753f90b824b64dd73");
  158. assert.equal(first.price.price, 1821);
  159. assert.equal(first.price.conf, 3);
  160. assert.equal(first.price.expo, -5);
  161. assert.equal(first.price.status.toString(), PythSDK.PriceStatus.TRADING.toString());
  162. assert.equal(first.price.numPublishers, 0);
  163. assert.equal(first.price.maxNumPublishers, 0);
  164. assert.equal(first.price.emaPrice, 1527);
  165. assert.equal(first.price.emaConf, 3);
  166. assert.equal(first.attestation_time, 1647273460);
  167. let second = await initialized.methods.latestPriceInfo("0xfd4caca566fc44a9d6585420959d13897877c606477b3f0e7f247295b7275620").call();
  168. assert.equal(second.price.id, "0xfd4caca566fc44a9d6585420959d13897877c606477b3f0e7f247295b7275620");
  169. assert.equal(second.price.productId, "0x7090c4ecf0309718d04c5a162c08aa4b78f533f688fa2f3ccd7be74c2a253a54");
  170. assert.equal(second.price.price, 1088);
  171. assert.equal(second.price.conf, 7);
  172. assert.equal(second.price.expo, -5);
  173. assert.equal(second.price.status.toString(), PythSDK.PriceStatus.TRADING.toString());
  174. assert.equal(second.price.numPublishers, 0);
  175. assert.equal(second.price.maxNumPublishers, 0);
  176. assert.equal(second.price.emaPrice, 1531);
  177. assert.equal(second.price.emaConf, 2);
  178. assert.equal(second.attestation_time, 1647273460);
  179. })
  180. it("should only cache updates for new prices", async function() {
  181. // This test sends two batches of updates, for the same Price IDs. The second batch contains
  182. // different price values to the first batch, but only the first and last updates in
  183. // the second batch have a newer timestamp than those in the first batch, and so these
  184. // are the only two which should be cached.
  185. const initialized = new web3.eth.Contract(P2WImplementationFullABI, PythDataBridge.address);
  186. let secondBatchPriceAttestation = "0x"+"503257480002020004009650325748000201c0e11df4c58a4e53f2bc059ba57a7c8f30ddada70b5bdc3753f90b824b64dd73c1902e05cdf03bc089a943d921f87ccd0e3e1b774b5660d037b9f428c0d3305e01000000000000073dfffffffb00000000000005470000000132959bbd00000000c8bfed5f00000000000000030000000041c7b65b00000000c8bfed5f0000000000000003010000000000622f65f5503257480002017090c4ecf0309718d04c5a162c08aa4b78f533f688fa2f3ccd7be74c2a253a54fd4caca566fc44a9d6585420959d13897877c606477b3f0e7f247295b7275620010000000000000450fffffffb00000000000005fb000000015cfe8c9d00000000e3dbaa7f00000000000000020000000041c7c5bb00000000e3dbaa7f0000000000000007010000000000622f65f4503257480002012f064374f55cb2efbbef29329de3b652013a76261876c55a1caf3a489c721ccd8c5dd422900917e8e26316fe598e8f062058d390644e0e36d42c187298420ccd010000000000000659fffffffb00000000000005cd00000001492c19bd00000000dd92071f00000000000000020000000041c7d3fb00000000dd92071f0000000000000001010000000000622f65f45032574800020181ddabd1a2c1fb6d6c4707b245b7c0ab6af0ae7b96b2ff866954a0b71124aee517fbe895e5416ddb4d5af9d83c599ee2c4f94cb25e8597f9e5978bd63a7cdcb70100000000000007bDfffffffb00000000000005e2000000014db2995d00000000dd8f775f00000000000000020000000041c7df9b00000000dd8f775f0000000000000003010000000000622f65f5";
  187. let all_price_ids = ["0xc1902e05cdf03bc089a943d921f87ccd0e3e1b774b5660d037b9f428c0d3305e",
  188. "0xfd4caca566fc44a9d6585420959d13897877c606477b3f0e7f247295b7275620",
  189. "0x8c5dd422900917e8e26316fe598e8f062058d390644e0e36d42c187298420ccd",
  190. "0x17fbe895e5416ddb4d5af9d83c599ee2c4f94cb25e8597f9e5978bd63a7cdcb7"
  191. ];
  192. // Send the first batch
  193. await attest(initialized, rawBatchPriceAttestation);
  194. let prices_after_first_update = {};
  195. for (var i = 0; i < all_price_ids.length; i++) {
  196. const price_id = all_price_ids[i];
  197. prices_after_first_update[price_id] = await initialized.methods.latestPriceInfo(price_id).call();
  198. }
  199. // Send the second batch
  200. await attest(initialized, secondBatchPriceAttestation);
  201. let prices_after_second_update = {};
  202. for (var i = 0; i < all_price_ids.length; i++) {
  203. const price_id = all_price_ids[i];
  204. prices_after_second_update[price_id] = await initialized.methods.latestPriceInfo(price_id).call();
  205. }
  206. // Price IDs which have newer timestamps
  207. let new_price_updates = [
  208. "0xc1902e05cdf03bc089a943d921f87ccd0e3e1b774b5660d037b9f428c0d3305e",
  209. "0x17fbe895e5416ddb4d5af9d83c599ee2c4f94cb25e8597f9e5978bd63a7cdcb7"
  210. ];
  211. // Price IDs which have older timestamps
  212. let old_price_updates = [
  213. "0xfd4caca566fc44a9d6585420959d13897877c606477b3f0e7f247295b7275620",
  214. "0x8c5dd422900917e8e26316fe598e8f062058d390644e0e36d42c187298420ccd"];
  215. // Check that the new price updates have been updated
  216. for (var i = 0; i < new_price_updates.length; i++) {
  217. const price_id = new_price_updates[i];
  218. assert.notEqual(prices_after_first_update[price_id].price.price, prices_after_second_update[price_id].price.price);
  219. assert.notEqual(prices_after_first_update[price_id].attestation_time, prices_after_second_update[price_id].attestation_time);
  220. }
  221. // Check that the old price updates have been discarded
  222. for (var i = 0; i < old_price_updates.length; i++) {
  223. const price_id = old_price_updates[i];
  224. assert.equal(prices_after_first_update[price_id].price.price, prices_after_second_update[price_id].price.price);
  225. assert.equal(prices_after_first_update[price_id].price.conf, prices_after_second_update[price_id].price.conf);
  226. assert.equal(prices_after_first_update[price_id].price.expo, prices_after_second_update[price_id].price.expo);
  227. assert.equal(prices_after_first_update[price_id].price.status.toString(), prices_after_second_update[price_id].price.status.toString());
  228. assert.equal(prices_after_first_update[price_id].price.numPublishers, prices_after_second_update[price_id].price.numPublishers);
  229. assert.equal(prices_after_first_update[price_id].price.maxNumPublishers, prices_after_second_update[price_id].price.maxNumPublishers);
  230. assert.equal(prices_after_first_update[price_id].price.emaPrice, prices_after_second_update[price_id].price.emaPrice);
  231. assert.equal(prices_after_first_update[price_id].price.emaConf, prices_after_second_update[price_id].price.emaConf);
  232. assert.equal(prices_after_first_update[price_id].attestation_time, prices_after_second_update[price_id].attestation_time);
  233. assert.equal(prices_after_first_update[price_id].arrival_time, prices_after_second_update[price_id].arrival_time);
  234. }
  235. })
  236. });
  237. const signAndEncodeVM = async function (
  238. timestamp,
  239. nonce,
  240. emitterChainId,
  241. emitterAddress,
  242. sequence,
  243. data,
  244. signers,
  245. guardianSetIndex,
  246. consistencyLevel
  247. ) {
  248. const body = [
  249. web3.eth.abi.encodeParameter("uint32", timestamp).substring(2 + (64 - 8)),
  250. web3.eth.abi.encodeParameter("uint32", nonce).substring(2 + (64 - 8)),
  251. web3.eth.abi.encodeParameter("uint16", emitterChainId).substring(2 + (64 - 4)),
  252. web3.eth.abi.encodeParameter("bytes32", emitterAddress).substring(2),
  253. web3.eth.abi.encodeParameter("uint64", sequence).substring(2 + (64 - 16)),
  254. web3.eth.abi.encodeParameter("uint8", consistencyLevel).substring(2 + (64 - 2)),
  255. data.substr(2)
  256. ]
  257. const hash = web3.utils.soliditySha3(web3.utils.soliditySha3("0x" + body.join("")))
  258. let signatures = "";
  259. for (let i in signers) {
  260. const ec = new elliptic.ec("secp256k1");
  261. const key = ec.keyFromPrivate(signers[i]);
  262. const signature = key.sign(hash.substr(2), {canonical: true});
  263. const packSig = [
  264. web3.eth.abi.encodeParameter("uint8", i).substring(2 + (64 - 2)),
  265. zeroPadBytes(signature.r.toString(16), 32),
  266. zeroPadBytes(signature.s.toString(16), 32),
  267. web3.eth.abi.encodeParameter("uint8", signature.recoveryParam).substr(2 + (64 - 2)),
  268. ]
  269. signatures += packSig.join("")
  270. }
  271. const vm = [
  272. web3.eth.abi.encodeParameter("uint8", 1).substring(2 + (64 - 2)),
  273. web3.eth.abi.encodeParameter("uint32", guardianSetIndex).substring(2 + (64 - 8)),
  274. web3.eth.abi.encodeParameter("uint8", signers.length).substring(2 + (64 - 2)),
  275. signatures,
  276. body.join("")
  277. ].join("");
  278. return vm
  279. }
  280. function zeroPadBytes(value, length) {
  281. while (value.length < 2 * length) {
  282. value = "0" + value;
  283. }
  284. return value;
  285. }