pyth.js 19 KB

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