pyth.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. const jsonfile = require('jsonfile');
  2. const elliptic = require('elliptic');
  3. const BigNumber = require('bignumber.js');
  4. const PythSDK = artifacts.require("PythSDK");
  5. const { deployProxy, upgradeProxy } = require('@openzeppelin/truffle-upgrades');
  6. const {expectRevert} = require('@openzeppelin/test-helpers');
  7. const Wormhole = artifacts.require("Wormhole");
  8. const PythProxy = artifacts.require("PythProxy");
  9. const MockPythProxyUpgrade = artifacts.require("MockPythProxyUpgrade");
  10. const testSigner1PK = "cfb12303a19cde580bb4dd771639b0d26bc68353645571a8cff516ab2ee113a0";
  11. const testSigner2PK = "892330666a850761e7370376430bb8c2aa1494072d3bfeaed0c4fa3d5a9135fe";
  12. contract("Pyth", function () {
  13. const testSigner1 = web3.eth.accounts.privateKeyToAccount(testSigner1PK);
  14. const testSigner2 = web3.eth.accounts.privateKeyToAccount(testSigner2PK);
  15. const testChainId = "2";
  16. const testGovernanceChainId = "3";
  17. const testGovernanceContract = "0x0000000000000000000000000000000000000000000000000000000000000004";
  18. const testPyth2WormholeChainId = "1";
  19. const testPyth2WormholeEmitter = "0x71f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b";
  20. const notOwnerError = "Ownable: caller is not the owner -- Reason given: Ownable: caller is not the owner.";
  21. beforeEach(async function () {
  22. this.pythProxy = await deployProxy(
  23. PythProxy,
  24. [
  25. testChainId,
  26. (await Wormhole.deployed()).address,
  27. testPyth2WormholeChainId,
  28. testPyth2WormholeEmitter,
  29. ]
  30. );
  31. });
  32. it("should be initialized with the correct signers and values", async function(){
  33. // chain id
  34. const chainId = await this.pythProxy.chainId();
  35. assert.equal(chainId, testChainId);
  36. // pyth2wormhole
  37. const pyth2wormChain = await this.pythProxy.pyth2WormholeChainId();
  38. assert.equal(pyth2wormChain, testPyth2WormholeChainId);
  39. const pyth2wormEmitter = await this.pythProxy.pyth2WormholeEmitter();
  40. assert.equal(pyth2wormEmitter, testPyth2WormholeEmitter);
  41. })
  42. it("should allow upgrades from the owner", async function(){
  43. // Check that the owner is the default account Truffle
  44. // has configured for the network. upgradeProxy will send
  45. // transactions from the default account.
  46. const accounts = await web3.eth.getAccounts();
  47. const defaultAccount = accounts[0];
  48. const owner = await this.pythProxy.owner();
  49. assert.equal(owner, defaultAccount);
  50. // Try and upgrade the proxy
  51. const newImplementation = await upgradeProxy(
  52. this.pythProxy.address, MockPythProxyUpgrade);
  53. // Check that the new upgrade is successful
  54. assert.equal(await newImplementation.isUpgradeActive(), true);
  55. assert.equal(this.pythProxy.address, newImplementation.address);
  56. })
  57. it("should allow ownership transfer", async function(){
  58. // Check that the owner is the default account Truffle
  59. // has configured for the network.
  60. const accounts = await web3.eth.getAccounts();
  61. const defaultAccount = accounts[0];
  62. assert.equal(await this.pythProxy.owner(), defaultAccount);
  63. // Check that another account can't transfer the ownership
  64. await expectRevert(this.pythProxy.transferOwnership(accounts[1], {from: accounts[1]}), notOwnerError);
  65. // Transfer the ownership to another account
  66. await this.pythProxy.transferOwnership(accounts[2], {from: defaultAccount});
  67. assert.equal(await this.pythProxy.owner(), accounts[2]);
  68. // Check that the original account can't transfer the ownership back to itself
  69. await expectRevert(this.pythProxy.transferOwnership(defaultAccount, {from: defaultAccount}), notOwnerError);
  70. // Check that the new owner can transfer the ownership back to the original account
  71. await this.pythProxy.transferOwnership(defaultAccount, {from: accounts[2]});
  72. assert.equal(await this.pythProxy.owner(), defaultAccount);
  73. })
  74. it("should not allow upgrades from the another account", async function(){
  75. // This test is slightly convoluted as, due to a limitation of Truffle,
  76. // we cannot specify which account upgradeProxy send transactions from:
  77. // it will always use the default account.
  78. //
  79. // Therefore, we transfer the ownership to another account first,
  80. // and then attempt an upgrade using the default account.
  81. // Check that the owner is the default account Truffle
  82. // has configured for the network.
  83. const accounts = await web3.eth.getAccounts();
  84. const defaultAccount = accounts[0];
  85. assert.equal(await this.pythProxy.owner(), defaultAccount);
  86. // Transfer the ownership to another account
  87. const newOwnerAccount = accounts[1];
  88. await this.pythProxy.transferOwnership(newOwnerAccount, {from: defaultAccount});
  89. assert.equal(await this.pythProxy.owner(), newOwnerAccount);
  90. // Try and upgrade using the default account, which will fail
  91. // because we are no longer the owner.
  92. await expectRevert(upgradeProxy(this.pythProxy.address, MockPythProxyUpgrade), notOwnerError);
  93. })
  94. const rawBatchPriceAttestation = "0x"+"503257480002020004009650325748000201c0e11df4c58a4e53f2bc059ba57a7c8f30ddada70b5bdc3753f90b824b64dd73c1902e05cdf03bc089a943d921f87ccd0e3e1b774b5660d037b9f428c0d3305e01000000000000071dfffffffb00000000000005f70000000132959bbd00000000c8bfed5f00000000000000030000000041c7b65b00000000c8bfed5f0000000000000003010000000000TTTTTTTT503257480002017090c4ecf0309718d04c5a162c08aa4b78f533f688fa2f3ccd7be74c2a253a54fd4caca566fc44a9d6585420959d13897877c606477b3f0e7f247295b7275620010000000000000440fffffffb00000000000005fb000000015cfe8c9d00000000e3dbaa7f00000000000000020000000041c7c5bb00000000e3dbaa7f0000000000000007010000000000TTTTTTTT503257480002012f064374f55cb2efbbef29329de3b652013a76261876c55a1caf3a489c721ccd8c5dd422900917e8e26316fe598e8f062058d390644e0e36d42c187298420ccd010000000000000609fffffffb00000000000005cd00000001492c19bd00000000dd92071f00000000000000020000000041c7d3fb00000000dd92071f0000000000000001010000000000TTTTTTTT5032574800020171ddabd1a2c1fb6d6c4707b245b7c0ab6af0ae7b96b2ff866954a0b71124aee517fbe895e5416ddb4d5af9d83c599ee2c4f94cb25e8597f9e5978bd63a7cdcb70100000000000007bcfffffffb00000000000005e2000000014db2995d00000000dd8f775f00000000000000020000000041c7df9b00000000dd8f775f0000000000000003010000000000TTTTTTTT";
  95. function encodeTimestamp(timestamp) {
  96. return timestamp.toString(16).padStart(8, "0");
  97. }
  98. function generateRawBatchAttestation(timestamp) {
  99. return rawBatchPriceAttestation.replace(/TTTTTTTT/g, encodeTimestamp(timestamp));
  100. }
  101. it("should parse batch price attestation correctly", async function() {
  102. const magic = 1345476424;
  103. const version = 2;
  104. let timestamp = 1647273460;
  105. let rawBatch = generateRawBatchAttestation(timestamp);
  106. let parsed = await this.pythProxy.parseBatchPriceAttestation(rawBatch);
  107. // Check the header
  108. assert.equal(parsed.header.magic, magic);
  109. assert.equal(parsed.header.version, version);
  110. assert.equal(parsed.header.payloadId, 2);
  111. assert.equal(parsed.nAttestations, 4);
  112. assert.equal(parsed.attestationSize, 150);
  113. assert.equal(parsed.attestations.length, 4);
  114. // Attestation #1
  115. assert.equal(parsed.attestations[0].header.magic, magic);
  116. assert.equal(parsed.attestations[0].header.version, version);
  117. assert.equal(parsed.attestations[0].header.payloadId, 1);
  118. assert.equal(parsed.attestations[0].productId, "0xc0e11df4c58a4e53f2bc059ba57a7c8f30ddada70b5bdc3753f90b824b64dd73");
  119. assert.equal(parsed.attestations[0].priceId, "0xc1902e05cdf03bc089a943d921f87ccd0e3e1b774b5660d037b9f428c0d3305e");
  120. assert.equal(parsed.attestations[0].priceType, 1);
  121. assert.equal(parsed.attestations[0].price, 1821);
  122. assert.equal(parsed.attestations[0].exponent, -5);
  123. assert.equal(parsed.attestations[0].emaPrice.value, 1527);
  124. assert.equal(parsed.attestations[0].emaPrice.numerator, 5143632829);
  125. assert.equal(parsed.attestations[0].emaPrice.denominator, 3368021343);
  126. assert.equal(parsed.attestations[0].emaConf.value, 3);
  127. assert.equal(parsed.attestations[0].emaConf.numerator, 1103607387);
  128. assert.equal(parsed.attestations[0].emaConf.denominator, 3368021343);
  129. assert.equal(parsed.attestations[0].confidenceInterval, 3);
  130. assert.equal(parsed.attestations[0].status, 1);
  131. assert.equal(parsed.attestations[0].corpAct, 0);
  132. assert.equal(parsed.attestations[0].timestamp, timestamp);
  133. // Attestation #2
  134. assert.equal(parsed.attestations[1].header.magic, magic);
  135. assert.equal(parsed.attestations[1].header.version, version);
  136. assert.equal(parsed.attestations[1].header.payloadId, 1);
  137. assert.equal(parsed.attestations[1].productId, "0x7090c4ecf0309718d04c5a162c08aa4b78f533f688fa2f3ccd7be74c2a253a54");
  138. assert.equal(parsed.attestations[1].priceId, "0xfd4caca566fc44a9d6585420959d13897877c606477b3f0e7f247295b7275620");
  139. assert.equal(parsed.attestations[1].priceType, 1);
  140. assert.equal(parsed.attestations[1].price, 1088);
  141. assert.equal(parsed.attestations[1].exponent, -5);
  142. assert.equal(parsed.attestations[1].emaPrice.value, 1531);
  143. assert.equal(parsed.attestations[1].emaPrice.numerator, 5855153309);
  144. assert.equal(parsed.attestations[1].emaPrice.denominator, 3822824063);
  145. assert.equal(parsed.attestations[1].emaConf.value, 2);
  146. assert.equal(parsed.attestations[1].emaConf.numerator, 1103611323);
  147. assert.equal(parsed.attestations[1].emaConf.denominator, 3822824063);
  148. assert.equal(parsed.attestations[1].confidenceInterval, 7);
  149. assert.equal(parsed.attestations[1].status, 1);
  150. assert.equal(parsed.attestations[1].corpAct, 0);
  151. assert.equal(parsed.attestations[1].timestamp, timestamp);
  152. // Attestation #3
  153. assert.equal(parsed.attestations[2].header.magic, magic);
  154. assert.equal(parsed.attestations[2].header.version, version);
  155. assert.equal(parsed.attestations[2].header.payloadId, 1);
  156. assert.equal(parsed.attestations[2].productId, "0x2f064374f55cb2efbbef29329de3b652013a76261876c55a1caf3a489c721ccd");
  157. assert.equal(parsed.attestations[2].priceId, "0x8c5dd422900917e8e26316fe598e8f062058d390644e0e36d42c187298420ccd");
  158. assert.equal(parsed.attestations[2].priceType, 1);
  159. assert.equal(parsed.attestations[2].price, 1545);
  160. assert.equal(parsed.attestations[2].exponent, -5);
  161. assert.equal(parsed.attestations[2].emaPrice.value, 1485);
  162. assert.equal(parsed.attestations[2].emaPrice.numerator, 5522594237);
  163. assert.equal(parsed.attestations[2].emaPrice.denominator, 3717334815);
  164. assert.equal(parsed.attestations[2].emaConf.value, 2);
  165. assert.equal(parsed.attestations[2].emaConf.numerator, 1103614971);
  166. assert.equal(parsed.attestations[2].emaConf.denominator, 3717334815);
  167. assert.equal(parsed.attestations[2].confidenceInterval, 1);
  168. assert.equal(parsed.attestations[2].status, 1);
  169. assert.equal(parsed.attestations[2].corpAct, 0);
  170. assert.equal(parsed.attestations[2].timestamp, timestamp);
  171. // Attestation #4
  172. assert.equal(parsed.attestations[3].header.magic, magic);
  173. assert.equal(parsed.attestations[3].header.version, version);
  174. assert.equal(parsed.attestations[3].header.payloadId, 1);
  175. assert.equal(parsed.attestations[3].productId, "0x71ddabd1a2c1fb6d6c4707b245b7c0ab6af0ae7b96b2ff866954a0b71124aee5");
  176. assert.equal(parsed.attestations[3].priceId, "0x17fbe895e5416ddb4d5af9d83c599ee2c4f94cb25e8597f9e5978bd63a7cdcb7");
  177. assert.equal(parsed.attestations[3].priceType, 1);
  178. assert.equal(parsed.attestations[3].price, 1980);
  179. assert.equal(parsed.attestations[3].exponent, -5);
  180. assert.equal(parsed.attestations[3].emaPrice.value, 1506);
  181. assert.equal(parsed.attestations[3].emaPrice.numerator, 5598517597);
  182. assert.equal(parsed.attestations[3].emaPrice.denominator, 3717166943);
  183. assert.equal(parsed.attestations[3].emaConf.value, 2);
  184. assert.equal(parsed.attestations[3].emaConf.numerator, 1103617947);
  185. assert.equal(parsed.attestations[3].emaConf.denominator, 3717166943);
  186. assert.equal(parsed.attestations[3].confidenceInterval, 3);
  187. assert.equal(parsed.attestations[3].status, 1);
  188. assert.equal(parsed.attestations[3].corpAct, 0);
  189. assert.equal(parsed.attestations[3].timestamp, timestamp);
  190. })
  191. async function attest(contract, data) {
  192. const vm = await signAndEncodeVM(
  193. 1,
  194. 1,
  195. testPyth2WormholeChainId,
  196. testPyth2WormholeEmitter,
  197. 0,
  198. data,
  199. [
  200. testSigner1PK
  201. ],
  202. 0,
  203. 0
  204. );
  205. await contract.attestPriceBatch("0x"+vm);
  206. }
  207. it("should attest price updates over wormhole", async function() {
  208. let rawBatch = generateRawBatchAttestation(1647273460);
  209. await attest(this.pythProxy, rawBatch);
  210. })
  211. it("should cache price updates", async function() {
  212. let currentTimestamp = (await web3.eth.getBlock("latest")).timestamp;
  213. let rawBatch = generateRawBatchAttestation(currentTimestamp);
  214. await attest(this.pythProxy, rawBatch);
  215. let first = await this.pythProxy.queryPriceFeed("0xc1902e05cdf03bc089a943d921f87ccd0e3e1b774b5660d037b9f428c0d3305e");
  216. assert.equal(first.priceFeed.id, "0xc1902e05cdf03bc089a943d921f87ccd0e3e1b774b5660d037b9f428c0d3305e");
  217. assert.equal(first.priceFeed.productId, "0xc0e11df4c58a4e53f2bc059ba57a7c8f30ddada70b5bdc3753f90b824b64dd73");
  218. assert.equal(first.priceFeed.price, 1821);
  219. assert.equal(first.priceFeed.conf, 3);
  220. assert.equal(first.priceFeed.expo, -5);
  221. assert.equal(first.priceFeed.status.toString(), PythSDK.PriceStatus.TRADING.toString());
  222. assert.equal(first.priceFeed.numPublishers, 0);
  223. assert.equal(first.priceFeed.maxNumPublishers, 0);
  224. assert.equal(first.priceFeed.emaPrice, 1527);
  225. assert.equal(first.priceFeed.emaConf, 3);
  226. let second = await this.pythProxy.queryPriceFeed("0xfd4caca566fc44a9d6585420959d13897877c606477b3f0e7f247295b7275620");
  227. assert.equal(second.priceFeed.id, "0xfd4caca566fc44a9d6585420959d13897877c606477b3f0e7f247295b7275620");
  228. assert.equal(second.priceFeed.productId, "0x7090c4ecf0309718d04c5a162c08aa4b78f533f688fa2f3ccd7be74c2a253a54");
  229. assert.equal(second.priceFeed.price, 1088);
  230. assert.equal(second.priceFeed.conf, 7);
  231. assert.equal(second.priceFeed.expo, -5);
  232. assert.equal(second.priceFeed.status.toString(), PythSDK.PriceStatus.TRADING.toString());
  233. assert.equal(second.priceFeed.numPublishers, 0);
  234. assert.equal(second.priceFeed.maxNumPublishers, 0);
  235. assert.equal(second.priceFeed.emaPrice, 1531);
  236. assert.equal(second.priceFeed.emaConf, 2);
  237. })
  238. it("should fail transaction if a price is not found", async function() {
  239. await expectRevert(
  240. this.pythProxy.queryPriceFeed(
  241. "0xc1902e05cdf03bc089a943d921f87ccd0e3e1b774b5660d037b9f428c0d3305e"),
  242. "no price feed found for the given price id");
  243. })
  244. it("should show stale cached prices as unknown", async function() {
  245. let smallestTimestamp = 1;
  246. let rawBatch = generateRawBatchAttestation(smallestTimestamp);
  247. await attest(this.pythProxy, rawBatch);
  248. let all_price_ids = ["0xc1902e05cdf03bc089a943d921f87ccd0e3e1b774b5660d037b9f428c0d3305e",
  249. "0xfd4caca566fc44a9d6585420959d13897877c606477b3f0e7f247295b7275620",
  250. "0x8c5dd422900917e8e26316fe598e8f062058d390644e0e36d42c187298420ccd",
  251. "0x17fbe895e5416ddb4d5af9d83c599ee2c4f94cb25e8597f9e5978bd63a7cdcb7"
  252. ];
  253. for (var i = 0; i < all_price_ids.length; i++) {
  254. const price_id = all_price_ids[i];
  255. let priceFeedResult = await this.pythProxy.queryPriceFeed(price_id);
  256. assert.equal(priceFeedResult.priceFeed.status.toString(), PythSDK.PriceStatus.UNKNOWN.toString());
  257. }
  258. })
  259. it("should show cached prices too far into the future as unknown", async function() {
  260. let largestTimestamp = 4294967295;
  261. let rawBatch = generateRawBatchAttestation(largestTimestamp);
  262. await attest(this.pythProxy, rawBatch);
  263. let all_price_ids = ["0xc1902e05cdf03bc089a943d921f87ccd0e3e1b774b5660d037b9f428c0d3305e",
  264. "0xfd4caca566fc44a9d6585420959d13897877c606477b3f0e7f247295b7275620",
  265. "0x8c5dd422900917e8e26316fe598e8f062058d390644e0e36d42c187298420ccd",
  266. "0x17fbe895e5416ddb4d5af9d83c599ee2c4f94cb25e8597f9e5978bd63a7cdcb7"
  267. ];
  268. for (var i = 0; i < all_price_ids.length; i++) {
  269. const price_id = all_price_ids[i];
  270. let priceFeedResult = await this.pythProxy.queryPriceFeed(price_id);
  271. assert.equal(priceFeedResult.priceFeed.status.toString(), PythSDK.PriceStatus.UNKNOWN.toString());
  272. }
  273. })
  274. it("should only cache updates for new prices", async function() {
  275. // This test sends two batches of updates, for the same Price IDs. The second batch contains
  276. // different price values to the first batch, but only the first and last updates in
  277. // the second batch have a newer timestamp than those in the first batch, and so these
  278. // are the only two which should be cached.
  279. let currentTimestamp = (await web3.eth.getBlock("latest")).timestamp;
  280. let encodedCurrentTimestamp = encodeTimestamp(currentTimestamp);
  281. let encodedNewerTimestamp = encodeTimestamp(currentTimestamp + 1);
  282. const firstBatch = generateRawBatchAttestation(currentTimestamp);
  283. let secondBatch = "0x"+"503257480002020004009650325748000201c0e11df4c58a4e53f2bc059ba57a7c8f30ddada70b5bdc3753f90b824b64dd73c1902e05cdf03bc089a943d921f87ccd0e3e1b774b5660d037b9f428c0d3305e01000000000000073dfffffffb00000000000005470000000132959bbd00000000c8bfed5f00000000000000030000000041c7b65b00000000c8bfed5f0000000000000003010000000000"+encodedNewerTimestamp+"503257480002017090c4ecf0309718d04c5a162c08aa4b78f533f688fa2f3ccd7be74c2a253a54fd4caca566fc44a9d6585420959d13897877c606477b3f0e7f247295b7275620010000000000000450fffffffb00000000000005fb000000015cfe8c9d00000000e3dbaa7f00000000000000020000000041c7c5bb00000000e3dbaa7f0000000000000007010000000000"+encodedCurrentTimestamp+"503257480002012f064374f55cb2efbbef29329de3b652013a76261876c55a1caf3a489c721ccd8c5dd422900917e8e26316fe598e8f062058d390644e0e36d42c187298420ccd010000000000000659fffffffb00000000000005cd00000001492c19bd00000000dd92071f00000000000000020000000041c7d3fb00000000dd92071f0000000000000001010000000000"+encodedCurrentTimestamp+"5032574800020181ddabd1a2c1fb6d6c4707b245b7c0ab6af0ae7b96b2ff866954a0b71124aee517fbe895e5416ddb4d5af9d83c599ee2c4f94cb25e8597f9e5978bd63a7cdcb70100000000000007bDfffffffb00000000000005e2000000014db2995d00000000dd8f775f00000000000000020000000041c7df9b00000000dd8f775f0000000000000003010000000000"+encodedNewerTimestamp;
  284. let all_price_ids = ["0xc1902e05cdf03bc089a943d921f87ccd0e3e1b774b5660d037b9f428c0d3305e",
  285. "0xfd4caca566fc44a9d6585420959d13897877c606477b3f0e7f247295b7275620",
  286. "0x8c5dd422900917e8e26316fe598e8f062058d390644e0e36d42c187298420ccd",
  287. "0x17fbe895e5416ddb4d5af9d83c599ee2c4f94cb25e8597f9e5978bd63a7cdcb7"
  288. ];
  289. // Send the first batch
  290. await attest(this.pythProxy, firstBatch);
  291. let prices_after_first_update = {};
  292. for (var i = 0; i < all_price_ids.length; i++) {
  293. const price_id = all_price_ids[i];
  294. prices_after_first_update[price_id] = await this.pythProxy.queryPriceFeed(price_id);
  295. }
  296. // Send the second batch
  297. await attest(this.pythProxy, secondBatch);
  298. let prices_after_second_update = {};
  299. for (var i = 0; i < all_price_ids.length; i++) {
  300. const price_id = all_price_ids[i];
  301. prices_after_second_update[price_id] = await this.pythProxy.queryPriceFeed(price_id);
  302. }
  303. // Price IDs which have newer timestamps
  304. let new_price_updates = [
  305. "0xc1902e05cdf03bc089a943d921f87ccd0e3e1b774b5660d037b9f428c0d3305e",
  306. "0x17fbe895e5416ddb4d5af9d83c599ee2c4f94cb25e8597f9e5978bd63a7cdcb7"
  307. ];
  308. // Price IDs which have older timestamps
  309. let old_price_updates = [
  310. "0xfd4caca566fc44a9d6585420959d13897877c606477b3f0e7f247295b7275620",
  311. "0x8c5dd422900917e8e26316fe598e8f062058d390644e0e36d42c187298420ccd"];
  312. // Check that the new price updates have been updated
  313. for (var i = 0; i < new_price_updates.length; i++) {
  314. const price_id = new_price_updates[i];
  315. assert.notEqual(prices_after_first_update[price_id].priceFeed.price, prices_after_second_update[price_id].priceFeed.price);
  316. }
  317. // Check that the old price updates have been discarded
  318. for (var i = 0; i < old_price_updates.length; i++) {
  319. const price_id = old_price_updates[i];
  320. assert.equal(prices_after_first_update[price_id].priceFeed.price, prices_after_second_update[price_id].priceFeed.price);
  321. assert.equal(prices_after_first_update[price_id].priceFeed.conf, prices_after_second_update[price_id].priceFeed.conf);
  322. assert.equal(prices_after_first_update[price_id].priceFeed.expo, prices_after_second_update[price_id].priceFeed.expo);
  323. assert.equal(prices_after_first_update[price_id].priceFeed.status.toString(), prices_after_second_update[price_id].priceFeed.status.toString());
  324. assert.equal(prices_after_first_update[price_id].priceFeed.numPublishers, prices_after_second_update[price_id].priceFeed.numPublishers);
  325. assert.equal(prices_after_first_update[price_id].priceFeed.maxNumPublishers, prices_after_second_update[price_id].priceFeed.maxNumPublishers);
  326. assert.equal(prices_after_first_update[price_id].priceFeed.emaPrice, prices_after_second_update[price_id].priceFeed.emaPrice);
  327. assert.equal(prices_after_first_update[price_id].priceFeed.emaConf, prices_after_second_update[price_id].priceFeed.emaConf);
  328. }
  329. })
  330. });
  331. const signAndEncodeVM = async function (
  332. timestamp,
  333. nonce,
  334. emitterChainId,
  335. emitterAddress,
  336. sequence,
  337. data,
  338. signers,
  339. guardianSetIndex,
  340. consistencyLevel
  341. ) {
  342. const body = [
  343. web3.eth.abi.encodeParameter("uint32", timestamp).substring(2 + (64 - 8)),
  344. web3.eth.abi.encodeParameter("uint32", nonce).substring(2 + (64 - 8)),
  345. web3.eth.abi.encodeParameter("uint16", emitterChainId).substring(2 + (64 - 4)),
  346. web3.eth.abi.encodeParameter("bytes32", emitterAddress).substring(2),
  347. web3.eth.abi.encodeParameter("uint64", sequence).substring(2 + (64 - 16)),
  348. web3.eth.abi.encodeParameter("uint8", consistencyLevel).substring(2 + (64 - 2)),
  349. data.substr(2)
  350. ]
  351. const hash = web3.utils.soliditySha3(web3.utils.soliditySha3("0x" + body.join("")))
  352. let signatures = "";
  353. for (let i in signers) {
  354. const ec = new elliptic.ec("secp256k1");
  355. const key = ec.keyFromPrivate(signers[i]);
  356. const signature = key.sign(hash.substr(2), {canonical: true});
  357. const packSig = [
  358. web3.eth.abi.encodeParameter("uint8", i).substring(2 + (64 - 2)),
  359. zeroPadBytes(signature.r.toString(16), 32),
  360. zeroPadBytes(signature.s.toString(16), 32),
  361. web3.eth.abi.encodeParameter("uint8", signature.recoveryParam).substr(2 + (64 - 2)),
  362. ]
  363. signatures += packSig.join("")
  364. }
  365. const vm = [
  366. web3.eth.abi.encodeParameter("uint8", 1).substring(2 + (64 - 2)),
  367. web3.eth.abi.encodeParameter("uint32", guardianSetIndex).substring(2 + (64 - 8)),
  368. web3.eth.abi.encodeParameter("uint8", signers.length).substring(2 + (64 - 2)),
  369. signatures,
  370. body.join("")
  371. ].join("");
  372. return vm
  373. }
  374. function zeroPadBytes(value, length) {
  375. while (value.length < 2 * length) {
  376. value = "0" + value;
  377. }
  378. return value;
  379. }